]> Git Repo - linux.git/blob - drivers/gpu/drm/drm_fb_helper.c
Merge tag 'drm-intel-next-2017-11-17-1' of git://anongit.freedesktop.org/drm/drm...
[linux.git] / drivers / gpu / drm / drm_fb_helper.c
1 /*
2  * Copyright (c) 2006-2009 Red Hat Inc.
3  * Copyright (c) 2006-2008 Intel Corporation
4  * Copyright (c) 2007 Dave Airlie <[email protected]>
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 <[email protected]>
28  *      Jesse Barnes <[email protected]>
29  */
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32 #include <linux/console.h>
33 #include <linux/kernel.h>
34 #include <linux/sysrq.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
37 #include <drm/drmP.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_crtc_helper.h>
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_atomic_helper.h>
43
44 #include "drm_crtc_helper_internal.h"
45
46 static bool drm_fbdev_emulation = true;
47 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
48 MODULE_PARM_DESC(fbdev_emulation,
49                  "Enable legacy fbdev emulation [default=true]");
50
51 static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
52 module_param(drm_fbdev_overalloc, int, 0444);
53 MODULE_PARM_DESC(drm_fbdev_overalloc,
54                  "Overallocation of the fbdev buffer (%) [default="
55                  __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
56
57 static LIST_HEAD(kernel_fb_helper_list);
58 static DEFINE_MUTEX(kernel_fb_helper_lock);
59
60 /**
61  * DOC: fbdev helpers
62  *
63  * The fb helper functions are useful to provide an fbdev on top of a drm kernel
64  * mode setting driver. They can be used mostly independently from the crtc
65  * helper functions used by many drivers to implement the kernel mode setting
66  * interfaces.
67  *
68  * Initialization is done as a four-step process with drm_fb_helper_prepare(),
69  * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
70  * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
71  * default behaviour can override the third step with their own code.
72  * Teardown is done with drm_fb_helper_fini() after the fbdev device is
73  * unregisters using drm_fb_helper_unregister_fbi().
74  *
75  * At runtime drivers should restore the fbdev console by calling
76  * drm_fb_helper_restore_fbdev_mode_unlocked() from their &drm_driver.lastclose
77  * callback.  They should also notify the fb helper code from updates to the
78  * output configuration by calling drm_fb_helper_hotplug_event(). For easier
79  * integration with the output polling code in drm_crtc_helper.c the modeset
80  * code provides a &drm_mode_config_funcs.output_poll_changed callback.
81  *
82  * All other functions exported by the fb helper library can be used to
83  * implement the fbdev driver interface by the driver.
84  *
85  * It is possible, though perhaps somewhat tricky, to implement race-free
86  * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
87  * helper must be called first to initialize the minimum required to make
88  * hotplug detection work. Drivers also need to make sure to properly set up
89  * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
90  * it is safe to enable interrupts and start processing hotplug events. At the
91  * same time, drivers should initialize all modeset objects such as CRTCs,
92  * encoders and connectors. To finish up the fbdev helper initialization, the
93  * drm_fb_helper_init() function is called. To probe for all attached displays
94  * and set up an initial configuration using the detected hardware, drivers
95  * should call drm_fb_helper_single_add_all_connectors() followed by
96  * drm_fb_helper_initial_config().
97  *
98  * If &drm_framebuffer_funcs.dirty is set, the
99  * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
100  * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
101  * away. This worker then calls the dirty() function ensuring that it will
102  * always run in process context since the fb_*() function could be running in
103  * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
104  * callback it will also schedule dirty_work with the damage collected from the
105  * mmap page writes.
106  */
107
108 #define drm_fb_helper_for_each_connector(fbh, i__) \
109         for (({ lockdep_assert_held(&(fbh)->lock); }), \
110              i__ = 0; i__ < (fbh)->connector_count; i__++)
111
112 static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
113                                              struct drm_connector *connector)
114 {
115         struct drm_fb_helper_connector *fb_conn;
116         struct drm_fb_helper_connector **temp;
117         unsigned int count;
118
119         if (!drm_fbdev_emulation)
120                 return 0;
121
122         lockdep_assert_held(&fb_helper->lock);
123
124         count = fb_helper->connector_count + 1;
125
126         if (count > fb_helper->connector_info_alloc_count) {
127                 size_t size = count * sizeof(fb_conn);
128
129                 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
130                 if (!temp)
131                         return -ENOMEM;
132
133                 fb_helper->connector_info_alloc_count = count;
134                 fb_helper->connector_info = temp;
135         }
136
137         fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
138         if (!fb_conn)
139                 return -ENOMEM;
140
141         drm_connector_get(connector);
142         fb_conn->connector = connector;
143         fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
144
145         return 0;
146 }
147
148 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
149                                     struct drm_connector *connector)
150 {
151         int err;
152
153         if (!fb_helper)
154                 return 0;
155
156         mutex_lock(&fb_helper->lock);
157         err = __drm_fb_helper_add_one_connector(fb_helper, connector);
158         mutex_unlock(&fb_helper->lock);
159
160         return err;
161 }
162 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
163
164 /**
165  * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
166  *                                             emulation helper
167  * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL
168  *
169  * This functions adds all the available connectors for use with the given
170  * fb_helper. This is a separate step to allow drivers to freely assign
171  * connectors to the fbdev, e.g. if some are reserved for special purposes or
172  * not adequate to be used for the fbcon.
173  *
174  * This function is protected against concurrent connector hotadds/removals
175  * using drm_fb_helper_add_one_connector() and
176  * drm_fb_helper_remove_one_connector().
177  */
178 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
179 {
180         struct drm_device *dev = fb_helper->dev;
181         struct drm_connector *connector;
182         struct drm_connector_list_iter conn_iter;
183         int i, ret = 0;
184
185         if (!drm_fbdev_emulation || !fb_helper)
186                 return 0;
187
188         mutex_lock(&fb_helper->lock);
189         drm_connector_list_iter_begin(dev, &conn_iter);
190         drm_for_each_connector_iter(connector, &conn_iter) {
191                 ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
192                 if (ret)
193                         goto fail;
194         }
195         goto out;
196
197 fail:
198         drm_fb_helper_for_each_connector(fb_helper, i) {
199                 struct drm_fb_helper_connector *fb_helper_connector =
200                         fb_helper->connector_info[i];
201
202                 drm_connector_put(fb_helper_connector->connector);
203
204                 kfree(fb_helper_connector);
205                 fb_helper->connector_info[i] = NULL;
206         }
207         fb_helper->connector_count = 0;
208 out:
209         drm_connector_list_iter_end(&conn_iter);
210         mutex_unlock(&fb_helper->lock);
211
212         return ret;
213 }
214 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
215
216 static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
217                                                 struct drm_connector *connector)
218 {
219         struct drm_fb_helper_connector *fb_helper_connector;
220         int i, j;
221
222         if (!drm_fbdev_emulation)
223                 return 0;
224
225         lockdep_assert_held(&fb_helper->lock);
226
227         drm_fb_helper_for_each_connector(fb_helper, i) {
228                 if (fb_helper->connector_info[i]->connector == connector)
229                         break;
230         }
231
232         if (i == fb_helper->connector_count)
233                 return -EINVAL;
234         fb_helper_connector = fb_helper->connector_info[i];
235         drm_connector_put(fb_helper_connector->connector);
236
237         for (j = i + 1; j < fb_helper->connector_count; j++)
238                 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
239
240         fb_helper->connector_count--;
241         kfree(fb_helper_connector);
242
243         return 0;
244 }
245
246 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
247                                        struct drm_connector *connector)
248 {
249         int err;
250
251         if (!fb_helper)
252                 return 0;
253
254         mutex_lock(&fb_helper->lock);
255         err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
256         mutex_unlock(&fb_helper->lock);
257
258         return err;
259 }
260 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
261
262 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
263 {
264         uint16_t *r_base, *g_base, *b_base;
265
266         if (crtc->funcs->gamma_set == NULL)
267                 return;
268
269         r_base = crtc->gamma_store;
270         g_base = r_base + crtc->gamma_size;
271         b_base = g_base + crtc->gamma_size;
272
273         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
274                                crtc->gamma_size, NULL);
275 }
276
277 /**
278  * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
279  * @info: fbdev registered by the helper
280  */
281 int drm_fb_helper_debug_enter(struct fb_info *info)
282 {
283         struct drm_fb_helper *helper = info->par;
284         const struct drm_crtc_helper_funcs *funcs;
285         int i;
286
287         list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
288                 for (i = 0; i < helper->crtc_count; i++) {
289                         struct drm_mode_set *mode_set =
290                                 &helper->crtc_info[i].mode_set;
291
292                         if (!mode_set->crtc->enabled)
293                                 continue;
294
295                         funcs = mode_set->crtc->helper_private;
296                         if (funcs->mode_set_base_atomic == NULL)
297                                 continue;
298
299                         if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
300                                 continue;
301
302                         funcs->mode_set_base_atomic(mode_set->crtc,
303                                                     mode_set->fb,
304                                                     mode_set->x,
305                                                     mode_set->y,
306                                                     ENTER_ATOMIC_MODE_SET);
307                 }
308         }
309
310         return 0;
311 }
312 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
313
314 /**
315  * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
316  * @info: fbdev registered by the helper
317  */
318 int drm_fb_helper_debug_leave(struct fb_info *info)
319 {
320         struct drm_fb_helper *helper = info->par;
321         struct drm_crtc *crtc;
322         const struct drm_crtc_helper_funcs *funcs;
323         struct drm_framebuffer *fb;
324         int i;
325
326         for (i = 0; i < helper->crtc_count; i++) {
327                 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
328
329                 crtc = mode_set->crtc;
330                 if (drm_drv_uses_atomic_modeset(crtc->dev))
331                         continue;
332
333                 funcs = crtc->helper_private;
334                 fb = crtc->primary->fb;
335
336                 if (!crtc->enabled)
337                         continue;
338
339                 if (!fb) {
340                         DRM_ERROR("no fb to restore??\n");
341                         continue;
342                 }
343
344                 if (funcs->mode_set_base_atomic == NULL)
345                         continue;
346
347                 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
348                 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
349                                             crtc->y, LEAVE_ATOMIC_MODE_SET);
350         }
351
352         return 0;
353 }
354 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
355
356 static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
357 {
358         struct drm_device *dev = fb_helper->dev;
359         struct drm_plane *plane;
360         struct drm_atomic_state *state;
361         int i, ret;
362         unsigned int plane_mask;
363         struct drm_modeset_acquire_ctx ctx;
364
365         drm_modeset_acquire_init(&ctx, 0);
366
367         state = drm_atomic_state_alloc(dev);
368         if (!state) {
369                 ret = -ENOMEM;
370                 goto out_ctx;
371         }
372
373         state->acquire_ctx = &ctx;
374 retry:
375         plane_mask = 0;
376         drm_for_each_plane(plane, dev) {
377                 struct drm_plane_state *plane_state;
378
379                 plane_state = drm_atomic_get_plane_state(state, plane);
380                 if (IS_ERR(plane_state)) {
381                         ret = PTR_ERR(plane_state);
382                         goto out_state;
383                 }
384
385                 plane_state->rotation = DRM_MODE_ROTATE_0;
386
387                 plane->old_fb = plane->fb;
388                 plane_mask |= 1 << drm_plane_index(plane);
389
390                 /* disable non-primary: */
391                 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
392                         continue;
393
394                 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
395                 if (ret != 0)
396                         goto out_state;
397         }
398
399         for (i = 0; i < fb_helper->crtc_count; i++) {
400                 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
401
402                 ret = __drm_atomic_helper_set_config(mode_set, state);
403                 if (ret != 0)
404                         goto out_state;
405
406                 /*
407                  * __drm_atomic_helper_set_config() sets active when a
408                  * mode is set, unconditionally clear it if we force DPMS off
409                  */
410                 if (!active) {
411                         struct drm_crtc *crtc = mode_set->crtc;
412                         struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
413
414                         crtc_state->active = false;
415                 }
416         }
417
418         ret = drm_atomic_commit(state);
419
420 out_state:
421         drm_atomic_clean_old_fb(dev, plane_mask, ret);
422
423         if (ret == -EDEADLK)
424                 goto backoff;
425
426         drm_atomic_state_put(state);
427 out_ctx:
428         drm_modeset_drop_locks(&ctx);
429         drm_modeset_acquire_fini(&ctx);
430
431         return ret;
432
433 backoff:
434         drm_atomic_state_clear(state);
435         drm_modeset_backoff(&ctx);
436
437         goto retry;
438 }
439
440 static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
441 {
442         struct drm_device *dev = fb_helper->dev;
443         struct drm_plane *plane;
444         int i, ret = 0;
445
446         drm_modeset_lock_all(fb_helper->dev);
447         drm_for_each_plane(plane, dev) {
448                 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
449                         drm_plane_force_disable(plane);
450
451                 if (plane->rotation_property)
452                         drm_mode_plane_set_obj_prop(plane,
453                                                     plane->rotation_property,
454                                                     DRM_MODE_ROTATE_0);
455         }
456
457         for (i = 0; i < fb_helper->crtc_count; i++) {
458                 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
459                 struct drm_crtc *crtc = mode_set->crtc;
460
461                 if (crtc->funcs->cursor_set2) {
462                         ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
463                         if (ret)
464                                 goto out;
465                 } else if (crtc->funcs->cursor_set) {
466                         ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
467                         if (ret)
468                                 goto out;
469                 }
470
471                 ret = drm_mode_set_config_internal(mode_set);
472                 if (ret)
473                         goto out;
474         }
475 out:
476         drm_modeset_unlock_all(fb_helper->dev);
477
478         return ret;
479 }
480
481 static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
482 {
483         struct drm_device *dev = fb_helper->dev;
484
485         if (drm_drv_uses_atomic_modeset(dev))
486                 return restore_fbdev_mode_atomic(fb_helper, true);
487         else
488                 return restore_fbdev_mode_legacy(fb_helper);
489 }
490
491 /**
492  * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
493  * @fb_helper: driver-allocated fbdev helper, can be NULL
494  *
495  * This should be called from driver's drm &drm_driver.lastclose callback
496  * when implementing an fbcon on top of kms using this helper. This ensures that
497  * the user isn't greeted with a black screen when e.g. X dies.
498  *
499  * RETURNS:
500  * Zero if everything went ok, negative error code otherwise.
501  */
502 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
503 {
504         bool do_delayed;
505         int ret;
506
507         if (!drm_fbdev_emulation || !fb_helper)
508                 return -ENODEV;
509
510         if (READ_ONCE(fb_helper->deferred_setup))
511                 return 0;
512
513         mutex_lock(&fb_helper->lock);
514         ret = restore_fbdev_mode(fb_helper);
515
516         do_delayed = fb_helper->delayed_hotplug;
517         if (do_delayed)
518                 fb_helper->delayed_hotplug = false;
519         mutex_unlock(&fb_helper->lock);
520
521         if (do_delayed)
522                 drm_fb_helper_hotplug_event(fb_helper);
523
524         return ret;
525 }
526 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
527
528 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
529 {
530         struct drm_device *dev = fb_helper->dev;
531         struct drm_crtc *crtc;
532         int bound = 0, crtcs_bound = 0;
533
534         /*
535          * Sometimes user space wants everything disabled, so don't steal the
536          * display if there's a master.
537          */
538         if (READ_ONCE(dev->master))
539                 return false;
540
541         drm_for_each_crtc(crtc, dev) {
542                 drm_modeset_lock(&crtc->mutex, NULL);
543                 if (crtc->primary->fb)
544                         crtcs_bound++;
545                 if (crtc->primary->fb == fb_helper->fb)
546                         bound++;
547                 drm_modeset_unlock(&crtc->mutex);
548         }
549
550         if (bound < crtcs_bound)
551                 return false;
552
553         return true;
554 }
555
556 #ifdef CONFIG_MAGIC_SYSRQ
557 /*
558  * restore fbcon display for all kms driver's using this helper, used for sysrq
559  * and panic handling.
560  */
561 static bool drm_fb_helper_force_kernel_mode(void)
562 {
563         bool ret, error = false;
564         struct drm_fb_helper *helper;
565
566         if (list_empty(&kernel_fb_helper_list))
567                 return false;
568
569         list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
570                 struct drm_device *dev = helper->dev;
571
572                 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
573                         continue;
574
575                 mutex_lock(&helper->lock);
576                 ret = restore_fbdev_mode(helper);
577                 if (ret)
578                         error = true;
579                 mutex_unlock(&helper->lock);
580         }
581         return error;
582 }
583
584 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
585 {
586         bool ret;
587
588         ret = drm_fb_helper_force_kernel_mode();
589         if (ret == true)
590                 DRM_ERROR("Failed to restore crtc configuration\n");
591 }
592 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
593
594 static void drm_fb_helper_sysrq(int dummy1)
595 {
596         schedule_work(&drm_fb_helper_restore_work);
597 }
598
599 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
600         .handler = drm_fb_helper_sysrq,
601         .help_msg = "force-fb(V)",
602         .action_msg = "Restore framebuffer console",
603 };
604 #else
605 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
606 #endif
607
608 static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
609 {
610         struct drm_device *dev = fb_helper->dev;
611         struct drm_crtc *crtc;
612         struct drm_connector *connector;
613         int i, j;
614
615         drm_modeset_lock_all(dev);
616         for (i = 0; i < fb_helper->crtc_count; i++) {
617                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
618
619                 if (!crtc->enabled)
620                         continue;
621
622                 /* Walk the connectors & encoders on this fb turning them on/off */
623                 drm_fb_helper_for_each_connector(fb_helper, j) {
624                         connector = fb_helper->connector_info[j]->connector;
625                         connector->funcs->dpms(connector, dpms_mode);
626                         drm_object_property_set_value(&connector->base,
627                                 dev->mode_config.dpms_property, dpms_mode);
628                 }
629         }
630         drm_modeset_unlock_all(dev);
631 }
632
633 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
634 {
635         struct drm_fb_helper *fb_helper = info->par;
636
637         /*
638          * For each CRTC in this fb, turn the connectors on/off.
639          */
640         mutex_lock(&fb_helper->lock);
641         if (!drm_fb_helper_is_bound(fb_helper)) {
642                 mutex_unlock(&fb_helper->lock);
643                 return;
644         }
645
646         if (drm_drv_uses_atomic_modeset(fb_helper->dev))
647                 restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
648         else
649                 dpms_legacy(fb_helper, dpms_mode);
650         mutex_unlock(&fb_helper->lock);
651 }
652
653 /**
654  * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
655  * @blank: desired blanking state
656  * @info: fbdev registered by the helper
657  */
658 int drm_fb_helper_blank(int blank, struct fb_info *info)
659 {
660         if (oops_in_progress)
661                 return -EBUSY;
662
663         switch (blank) {
664         /* Display: On; HSync: On, VSync: On */
665         case FB_BLANK_UNBLANK:
666                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
667                 break;
668         /* Display: Off; HSync: On, VSync: On */
669         case FB_BLANK_NORMAL:
670                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
671                 break;
672         /* Display: Off; HSync: Off, VSync: On */
673         case FB_BLANK_HSYNC_SUSPEND:
674                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
675                 break;
676         /* Display: Off; HSync: On, VSync: Off */
677         case FB_BLANK_VSYNC_SUSPEND:
678                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
679                 break;
680         /* Display: Off; HSync: Off, VSync: Off */
681         case FB_BLANK_POWERDOWN:
682                 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
683                 break;
684         }
685         return 0;
686 }
687 EXPORT_SYMBOL(drm_fb_helper_blank);
688
689 static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
690                                           struct drm_mode_set *modeset)
691 {
692         int i;
693
694         for (i = 0; i < modeset->num_connectors; i++) {
695                 drm_connector_put(modeset->connectors[i]);
696                 modeset->connectors[i] = NULL;
697         }
698         modeset->num_connectors = 0;
699
700         drm_mode_destroy(helper->dev, modeset->mode);
701         modeset->mode = NULL;
702
703         /* FIXME should hold a ref? */
704         modeset->fb = NULL;
705 }
706
707 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
708 {
709         int i;
710
711         for (i = 0; i < helper->connector_count; i++) {
712                 drm_connector_put(helper->connector_info[i]->connector);
713                 kfree(helper->connector_info[i]);
714         }
715         kfree(helper->connector_info);
716
717         for (i = 0; i < helper->crtc_count; i++) {
718                 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
719
720                 drm_fb_helper_modeset_release(helper, modeset);
721                 kfree(modeset->connectors);
722         }
723         kfree(helper->crtc_info);
724 }
725
726 static void drm_fb_helper_resume_worker(struct work_struct *work)
727 {
728         struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
729                                                     resume_work);
730
731         console_lock();
732         fb_set_suspend(helper->fbdev, 0);
733         console_unlock();
734 }
735
736 static void drm_fb_helper_dirty_work(struct work_struct *work)
737 {
738         struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
739                                                     dirty_work);
740         struct drm_clip_rect *clip = &helper->dirty_clip;
741         struct drm_clip_rect clip_copy;
742         unsigned long flags;
743
744         spin_lock_irqsave(&helper->dirty_lock, flags);
745         clip_copy = *clip;
746         clip->x1 = clip->y1 = ~0;
747         clip->x2 = clip->y2 = 0;
748         spin_unlock_irqrestore(&helper->dirty_lock, flags);
749
750         /* call dirty callback only when it has been really touched */
751         if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
752                 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
753 }
754
755 /**
756  * drm_fb_helper_prepare - setup a drm_fb_helper structure
757  * @dev: DRM device
758  * @helper: driver-allocated fbdev helper structure to set up
759  * @funcs: pointer to structure of functions associate with this helper
760  *
761  * Sets up the bare minimum to make the framebuffer helper usable. This is
762  * useful to implement race-free initialization of the polling helpers.
763  */
764 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
765                            const struct drm_fb_helper_funcs *funcs)
766 {
767         INIT_LIST_HEAD(&helper->kernel_fb_list);
768         spin_lock_init(&helper->dirty_lock);
769         INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
770         INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
771         helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
772         mutex_init(&helper->lock);
773         helper->funcs = funcs;
774         helper->dev = dev;
775 }
776 EXPORT_SYMBOL(drm_fb_helper_prepare);
777
778 /**
779  * drm_fb_helper_init - initialize a &struct drm_fb_helper
780  * @dev: drm device
781  * @fb_helper: driver-allocated fbdev helper structure to initialize
782  * @max_conn_count: max connector count
783  *
784  * This allocates the structures for the fbdev helper with the given limits.
785  * Note that this won't yet touch the hardware (through the driver interfaces)
786  * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
787  * to allow driver writes more control over the exact init sequence.
788  *
789  * Drivers must call drm_fb_helper_prepare() before calling this function.
790  *
791  * RETURNS:
792  * Zero if everything went ok, nonzero otherwise.
793  */
794 int drm_fb_helper_init(struct drm_device *dev,
795                        struct drm_fb_helper *fb_helper,
796                        int max_conn_count)
797 {
798         struct drm_crtc *crtc;
799         struct drm_mode_config *config = &dev->mode_config;
800         int i;
801
802         if (!drm_fbdev_emulation) {
803                 dev->fb_helper = fb_helper;
804                 return 0;
805         }
806
807         if (!max_conn_count)
808                 return -EINVAL;
809
810         fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
811         if (!fb_helper->crtc_info)
812                 return -ENOMEM;
813
814         fb_helper->crtc_count = config->num_crtc;
815         fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
816         if (!fb_helper->connector_info) {
817                 kfree(fb_helper->crtc_info);
818                 return -ENOMEM;
819         }
820         fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
821         fb_helper->connector_count = 0;
822
823         for (i = 0; i < fb_helper->crtc_count; i++) {
824                 fb_helper->crtc_info[i].mode_set.connectors =
825                         kcalloc(max_conn_count,
826                                 sizeof(struct drm_connector *),
827                                 GFP_KERNEL);
828
829                 if (!fb_helper->crtc_info[i].mode_set.connectors)
830                         goto out_free;
831                 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
832         }
833
834         i = 0;
835         drm_for_each_crtc(crtc, dev) {
836                 fb_helper->crtc_info[i].mode_set.crtc = crtc;
837                 i++;
838         }
839
840         dev->fb_helper = fb_helper;
841
842         return 0;
843 out_free:
844         drm_fb_helper_crtc_free(fb_helper);
845         return -ENOMEM;
846 }
847 EXPORT_SYMBOL(drm_fb_helper_init);
848
849 /**
850  * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
851  * @fb_helper: driver-allocated fbdev helper
852  *
853  * A helper to alloc fb_info and the members cmap and apertures. Called
854  * by the driver within the fb_probe fb_helper callback function. Drivers do not
855  * need to release the allocated fb_info structure themselves, this is
856  * automatically done when calling drm_fb_helper_fini().
857  *
858  * RETURNS:
859  * fb_info pointer if things went okay, pointer containing error code
860  * otherwise
861  */
862 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
863 {
864         struct device *dev = fb_helper->dev->dev;
865         struct fb_info *info;
866         int ret;
867
868         info = framebuffer_alloc(0, dev);
869         if (!info)
870                 return ERR_PTR(-ENOMEM);
871
872         ret = fb_alloc_cmap(&info->cmap, 256, 0);
873         if (ret)
874                 goto err_release;
875
876         info->apertures = alloc_apertures(1);
877         if (!info->apertures) {
878                 ret = -ENOMEM;
879                 goto err_free_cmap;
880         }
881
882         fb_helper->fbdev = info;
883
884         return info;
885
886 err_free_cmap:
887         fb_dealloc_cmap(&info->cmap);
888 err_release:
889         framebuffer_release(info);
890         return ERR_PTR(ret);
891 }
892 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
893
894 /**
895  * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
896  * @fb_helper: driver-allocated fbdev helper, can be NULL
897  *
898  * A wrapper around unregister_framebuffer, to release the fb_info
899  * framebuffer device. This must be called before releasing all resources for
900  * @fb_helper by calling drm_fb_helper_fini().
901  */
902 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
903 {
904         if (fb_helper && fb_helper->fbdev)
905                 unregister_framebuffer(fb_helper->fbdev);
906 }
907 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
908
909 /**
910  * drm_fb_helper_fini - finialize a &struct drm_fb_helper
911  * @fb_helper: driver-allocated fbdev helper, can be NULL
912  *
913  * This cleans up all remaining resources associated with @fb_helper. Must be
914  * called after drm_fb_helper_unlink_fbi() was called.
915  */
916 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
917 {
918         struct fb_info *info;
919
920         if (!fb_helper)
921                 return;
922
923         fb_helper->dev->fb_helper = NULL;
924
925         if (!drm_fbdev_emulation)
926                 return;
927
928         cancel_work_sync(&fb_helper->resume_work);
929         cancel_work_sync(&fb_helper->dirty_work);
930
931         info = fb_helper->fbdev;
932         if (info) {
933                 if (info->cmap.len)
934                         fb_dealloc_cmap(&info->cmap);
935                 framebuffer_release(info);
936         }
937         fb_helper->fbdev = NULL;
938
939         mutex_lock(&kernel_fb_helper_lock);
940         if (!list_empty(&fb_helper->kernel_fb_list)) {
941                 list_del(&fb_helper->kernel_fb_list);
942                 if (list_empty(&kernel_fb_helper_list))
943                         unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
944         }
945         mutex_unlock(&kernel_fb_helper_lock);
946
947         mutex_destroy(&fb_helper->lock);
948         drm_fb_helper_crtc_free(fb_helper);
949
950 }
951 EXPORT_SYMBOL(drm_fb_helper_fini);
952
953 /**
954  * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
955  * @fb_helper: driver-allocated fbdev helper, can be NULL
956  *
957  * A wrapper around unlink_framebuffer implemented by fbdev core
958  */
959 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
960 {
961         if (fb_helper && fb_helper->fbdev)
962                 unlink_framebuffer(fb_helper->fbdev);
963 }
964 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
965
966 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
967                                 u32 width, u32 height)
968 {
969         struct drm_fb_helper *helper = info->par;
970         struct drm_clip_rect *clip = &helper->dirty_clip;
971         unsigned long flags;
972
973         if (!helper->fb->funcs->dirty)
974                 return;
975
976         spin_lock_irqsave(&helper->dirty_lock, flags);
977         clip->x1 = min_t(u32, clip->x1, x);
978         clip->y1 = min_t(u32, clip->y1, y);
979         clip->x2 = max_t(u32, clip->x2, x + width);
980         clip->y2 = max_t(u32, clip->y2, y + height);
981         spin_unlock_irqrestore(&helper->dirty_lock, flags);
982
983         schedule_work(&helper->dirty_work);
984 }
985
986 /**
987  * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
988  * @info: fb_info struct pointer
989  * @pagelist: list of dirty mmap framebuffer pages
990  *
991  * This function is used as the &fb_deferred_io.deferred_io
992  * callback function for flushing the fbdev mmap writes.
993  */
994 void drm_fb_helper_deferred_io(struct fb_info *info,
995                                struct list_head *pagelist)
996 {
997         unsigned long start, end, min, max;
998         struct page *page;
999         u32 y1, y2;
1000
1001         min = ULONG_MAX;
1002         max = 0;
1003         list_for_each_entry(page, pagelist, lru) {
1004                 start = page->index << PAGE_SHIFT;
1005                 end = start + PAGE_SIZE - 1;
1006                 min = min(min, start);
1007                 max = max(max, end);
1008         }
1009
1010         if (min < max) {
1011                 y1 = min / info->fix.line_length;
1012                 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
1013                            info->var.yres);
1014                 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
1015         }
1016 }
1017 EXPORT_SYMBOL(drm_fb_helper_deferred_io);
1018
1019 /**
1020  * drm_fb_helper_sys_read - wrapper around fb_sys_read
1021  * @info: fb_info struct pointer
1022  * @buf: userspace buffer to read from framebuffer memory
1023  * @count: number of bytes to read from framebuffer memory
1024  * @ppos: read offset within framebuffer memory
1025  *
1026  * A wrapper around fb_sys_read implemented by fbdev core
1027  */
1028 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1029                                size_t count, loff_t *ppos)
1030 {
1031         return fb_sys_read(info, buf, count, ppos);
1032 }
1033 EXPORT_SYMBOL(drm_fb_helper_sys_read);
1034
1035 /**
1036  * drm_fb_helper_sys_write - wrapper around fb_sys_write
1037  * @info: fb_info struct pointer
1038  * @buf: userspace buffer to write to framebuffer memory
1039  * @count: number of bytes to write to framebuffer memory
1040  * @ppos: write offset within framebuffer memory
1041  *
1042  * A wrapper around fb_sys_write implemented by fbdev core
1043  */
1044 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1045                                 size_t count, loff_t *ppos)
1046 {
1047         ssize_t ret;
1048
1049         ret = fb_sys_write(info, buf, count, ppos);
1050         if (ret > 0)
1051                 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1052                                     info->var.yres);
1053
1054         return ret;
1055 }
1056 EXPORT_SYMBOL(drm_fb_helper_sys_write);
1057
1058 /**
1059  * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1060  * @info: fbdev registered by the helper
1061  * @rect: info about rectangle to fill
1062  *
1063  * A wrapper around sys_fillrect implemented by fbdev core
1064  */
1065 void drm_fb_helper_sys_fillrect(struct fb_info *info,
1066                                 const struct fb_fillrect *rect)
1067 {
1068         sys_fillrect(info, rect);
1069         drm_fb_helper_dirty(info, rect->dx, rect->dy,
1070                             rect->width, rect->height);
1071 }
1072 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1073
1074 /**
1075  * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1076  * @info: fbdev registered by the helper
1077  * @area: info about area to copy
1078  *
1079  * A wrapper around sys_copyarea implemented by fbdev core
1080  */
1081 void drm_fb_helper_sys_copyarea(struct fb_info *info,
1082                                 const struct fb_copyarea *area)
1083 {
1084         sys_copyarea(info, area);
1085         drm_fb_helper_dirty(info, area->dx, area->dy,
1086                             area->width, area->height);
1087 }
1088 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1089
1090 /**
1091  * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1092  * @info: fbdev registered by the helper
1093  * @image: info about image to blit
1094  *
1095  * A wrapper around sys_imageblit implemented by fbdev core
1096  */
1097 void drm_fb_helper_sys_imageblit(struct fb_info *info,
1098                                  const struct fb_image *image)
1099 {
1100         sys_imageblit(info, image);
1101         drm_fb_helper_dirty(info, image->dx, image->dy,
1102                             image->width, image->height);
1103 }
1104 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1105
1106 /**
1107  * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1108  * @info: fbdev registered by the helper
1109  * @rect: info about rectangle to fill
1110  *
1111  * A wrapper around cfb_imageblit implemented by fbdev core
1112  */
1113 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1114                                 const struct fb_fillrect *rect)
1115 {
1116         cfb_fillrect(info, rect);
1117         drm_fb_helper_dirty(info, rect->dx, rect->dy,
1118                             rect->width, rect->height);
1119 }
1120 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1121
1122 /**
1123  * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1124  * @info: fbdev registered by the helper
1125  * @area: info about area to copy
1126  *
1127  * A wrapper around cfb_copyarea implemented by fbdev core
1128  */
1129 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1130                                 const struct fb_copyarea *area)
1131 {
1132         cfb_copyarea(info, area);
1133         drm_fb_helper_dirty(info, area->dx, area->dy,
1134                             area->width, area->height);
1135 }
1136 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1137
1138 /**
1139  * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1140  * @info: fbdev registered by the helper
1141  * @image: info about image to blit
1142  *
1143  * A wrapper around cfb_imageblit implemented by fbdev core
1144  */
1145 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1146                                  const struct fb_image *image)
1147 {
1148         cfb_imageblit(info, image);
1149         drm_fb_helper_dirty(info, image->dx, image->dy,
1150                             image->width, image->height);
1151 }
1152 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1153
1154 /**
1155  * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1156  * @fb_helper: driver-allocated fbdev helper, can be NULL
1157  * @suspend: whether to suspend or resume
1158  *
1159  * A wrapper around fb_set_suspend implemented by fbdev core.
1160  * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1161  * the lock yourself
1162  */
1163 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
1164 {
1165         if (fb_helper && fb_helper->fbdev)
1166                 fb_set_suspend(fb_helper->fbdev, suspend);
1167 }
1168 EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1169
1170 /**
1171  * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1172  *                                      takes the console lock
1173  * @fb_helper: driver-allocated fbdev helper, can be NULL
1174  * @suspend: whether to suspend or resume
1175  *
1176  * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1177  * isn't available on resume, a worker is tasked with waiting for the lock
1178  * to become available. The console lock can be pretty contented on resume
1179  * due to all the printk activity.
1180  *
1181  * This function can be called multiple times with the same state since
1182  * &fb_info.state is checked to see if fbdev is running or not before locking.
1183  *
1184  * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1185  */
1186 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
1187                                         bool suspend)
1188 {
1189         if (!fb_helper || !fb_helper->fbdev)
1190                 return;
1191
1192         /* make sure there's no pending/ongoing resume */
1193         flush_work(&fb_helper->resume_work);
1194
1195         if (suspend) {
1196                 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1197                         return;
1198
1199                 console_lock();
1200
1201         } else {
1202                 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1203                         return;
1204
1205                 if (!console_trylock()) {
1206                         schedule_work(&fb_helper->resume_work);
1207                         return;
1208                 }
1209         }
1210
1211         fb_set_suspend(fb_helper->fbdev, suspend);
1212         console_unlock();
1213 }
1214 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1215
1216 static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
1217 {
1218         u32 *palette = (u32 *)info->pseudo_palette;
1219         int i;
1220
1221         if (cmap->start + cmap->len > 16)
1222                 return -EINVAL;
1223
1224         for (i = 0; i < cmap->len; ++i) {
1225                 u16 red = cmap->red[i];
1226                 u16 green = cmap->green[i];
1227                 u16 blue = cmap->blue[i];
1228                 u32 value;
1229
1230                 red >>= 16 - info->var.red.length;
1231                 green >>= 16 - info->var.green.length;
1232                 blue >>= 16 - info->var.blue.length;
1233                 value = (red << info->var.red.offset) |
1234                         (green << info->var.green.offset) |
1235                         (blue << info->var.blue.offset);
1236                 if (info->var.transp.length > 0) {
1237                         u32 mask = (1 << info->var.transp.length) - 1;
1238
1239                         mask <<= info->var.transp.offset;
1240                         value |= mask;
1241                 }
1242                 palette[cmap->start + i] = value;
1243         }
1244
1245         return 0;
1246 }
1247
1248 static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
1249 {
1250         struct drm_fb_helper *fb_helper = info->par;
1251         struct drm_crtc *crtc;
1252         u16 *r, *g, *b;
1253         int i, ret = 0;
1254
1255         drm_modeset_lock_all(fb_helper->dev);
1256         for (i = 0; i < fb_helper->crtc_count; i++) {
1257                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1258                 if (!crtc->funcs->gamma_set || !crtc->gamma_size)
1259                         return -EINVAL;
1260
1261                 if (cmap->start + cmap->len > crtc->gamma_size)
1262                         return -EINVAL;
1263
1264                 r = crtc->gamma_store;
1265                 g = r + crtc->gamma_size;
1266                 b = g + crtc->gamma_size;
1267
1268                 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1269                 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1270                 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1271
1272                 ret = crtc->funcs->gamma_set(crtc, r, g, b,
1273                                              crtc->gamma_size, NULL);
1274                 if (ret)
1275                         return ret;
1276         }
1277         drm_modeset_unlock_all(fb_helper->dev);
1278
1279         return ret;
1280 }
1281
1282 static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
1283                                                        struct fb_cmap *cmap)
1284 {
1285         struct drm_device *dev = crtc->dev;
1286         struct drm_property_blob *gamma_lut;
1287         struct drm_color_lut *lut;
1288         int size = crtc->gamma_size;
1289         int i;
1290
1291         if (!size || cmap->start + cmap->len > size)
1292                 return ERR_PTR(-EINVAL);
1293
1294         gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
1295         if (IS_ERR(gamma_lut))
1296                 return gamma_lut;
1297
1298         lut = (struct drm_color_lut *)gamma_lut->data;
1299         if (cmap->start || cmap->len != size) {
1300                 u16 *r = crtc->gamma_store;
1301                 u16 *g = r + crtc->gamma_size;
1302                 u16 *b = g + crtc->gamma_size;
1303
1304                 for (i = 0; i < cmap->start; i++) {
1305                         lut[i].red = r[i];
1306                         lut[i].green = g[i];
1307                         lut[i].blue = b[i];
1308                 }
1309                 for (i = cmap->start + cmap->len; i < size; i++) {
1310                         lut[i].red = r[i];
1311                         lut[i].green = g[i];
1312                         lut[i].blue = b[i];
1313                 }
1314         }
1315
1316         for (i = 0; i < cmap->len; i++) {
1317                 lut[cmap->start + i].red = cmap->red[i];
1318                 lut[cmap->start + i].green = cmap->green[i];
1319                 lut[cmap->start + i].blue = cmap->blue[i];
1320         }
1321
1322         return gamma_lut;
1323 }
1324
1325 static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
1326 {
1327         struct drm_fb_helper *fb_helper = info->par;
1328         struct drm_device *dev = fb_helper->dev;
1329         struct drm_property_blob *gamma_lut = NULL;
1330         struct drm_modeset_acquire_ctx ctx;
1331         struct drm_crtc_state *crtc_state;
1332         struct drm_atomic_state *state;
1333         struct drm_crtc *crtc;
1334         u16 *r, *g, *b;
1335         int i, ret = 0;
1336         bool replaced;
1337
1338         drm_modeset_acquire_init(&ctx, 0);
1339
1340         state = drm_atomic_state_alloc(dev);
1341         if (!state) {
1342                 ret = -ENOMEM;
1343                 goto out_ctx;
1344         }
1345
1346         state->acquire_ctx = &ctx;
1347 retry:
1348         for (i = 0; i < fb_helper->crtc_count; i++) {
1349                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1350
1351                 if (!gamma_lut)
1352                         gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
1353                 if (IS_ERR(gamma_lut)) {
1354                         ret = PTR_ERR(gamma_lut);
1355                         gamma_lut = NULL;
1356                         goto out_state;
1357                 }
1358
1359                 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1360                 if (IS_ERR(crtc_state)) {
1361                         ret = PTR_ERR(crtc_state);
1362                         goto out_state;
1363                 }
1364
1365                 replaced  = drm_property_replace_blob(&crtc_state->degamma_lut,
1366                                                       NULL);
1367                 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
1368                 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
1369                                                       gamma_lut);
1370                 crtc_state->color_mgmt_changed |= replaced;
1371         }
1372
1373         ret = drm_atomic_commit(state);
1374         if (ret)
1375                 goto out_state;
1376
1377         for (i = 0; i < fb_helper->crtc_count; i++) {
1378                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1379
1380                 r = crtc->gamma_store;
1381                 g = r + crtc->gamma_size;
1382                 b = g + crtc->gamma_size;
1383
1384                 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1385                 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1386                 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1387         }
1388
1389 out_state:
1390         if (ret == -EDEADLK)
1391                 goto backoff;
1392
1393         drm_property_blob_put(gamma_lut);
1394         drm_atomic_state_put(state);
1395 out_ctx:
1396         drm_modeset_drop_locks(&ctx);
1397         drm_modeset_acquire_fini(&ctx);
1398
1399         return ret;
1400
1401 backoff:
1402         drm_atomic_state_clear(state);
1403         drm_modeset_backoff(&ctx);
1404         goto retry;
1405 }
1406
1407 /**
1408  * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1409  * @cmap: cmap to set
1410  * @info: fbdev registered by the helper
1411  */
1412 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1413 {
1414         struct drm_fb_helper *fb_helper = info->par;
1415         int ret;
1416
1417         if (oops_in_progress)
1418                 return -EBUSY;
1419
1420         mutex_lock(&fb_helper->lock);
1421
1422         if (!drm_fb_helper_is_bound(fb_helper)) {
1423                 ret = -EBUSY;
1424                 goto out;
1425         }
1426
1427         if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1428                 ret = setcmap_pseudo_palette(cmap, info);
1429         else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1430                 ret = setcmap_atomic(cmap, info);
1431         else
1432                 ret = setcmap_legacy(cmap, info);
1433
1434 out:
1435         mutex_unlock(&fb_helper->lock);
1436
1437         return ret;
1438 }
1439 EXPORT_SYMBOL(drm_fb_helper_setcmap);
1440
1441 /**
1442  * drm_fb_helper_ioctl - legacy ioctl implementation
1443  * @info: fbdev registered by the helper
1444  * @cmd: ioctl command
1445  * @arg: ioctl argument
1446  *
1447  * A helper to implement the standard fbdev ioctl. Only
1448  * FBIO_WAITFORVSYNC is implemented for now.
1449  */
1450 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1451                         unsigned long arg)
1452 {
1453         struct drm_fb_helper *fb_helper = info->par;
1454         struct drm_mode_set *mode_set;
1455         struct drm_crtc *crtc;
1456         int ret = 0;
1457
1458         mutex_lock(&fb_helper->lock);
1459         if (!drm_fb_helper_is_bound(fb_helper)) {
1460                 ret = -EBUSY;
1461                 goto unlock;
1462         }
1463
1464         switch (cmd) {
1465         case FBIO_WAITFORVSYNC:
1466                 /*
1467                  * Only consider the first CRTC.
1468                  *
1469                  * This ioctl is supposed to take the CRTC number as
1470                  * an argument, but in fbdev times, what that number
1471                  * was supposed to be was quite unclear, different
1472                  * drivers were passing that argument differently
1473                  * (some by reference, some by value), and most of the
1474                  * userspace applications were just hardcoding 0 as an
1475                  * argument.
1476                  *
1477                  * The first CRTC should be the integrated panel on
1478                  * most drivers, so this is the best choice we can
1479                  * make. If we're not smart enough here, one should
1480                  * just consider switch the userspace to KMS.
1481                  */
1482                 mode_set = &fb_helper->crtc_info[0].mode_set;
1483                 crtc = mode_set->crtc;
1484
1485                 /*
1486                  * Only wait for a vblank event if the CRTC is
1487                  * enabled, otherwise just don't do anythintg,
1488                  * not even report an error.
1489                  */
1490                 ret = drm_crtc_vblank_get(crtc);
1491                 if (!ret) {
1492                         drm_crtc_wait_one_vblank(crtc);
1493                         drm_crtc_vblank_put(crtc);
1494                 }
1495
1496                 ret = 0;
1497                 goto unlock;
1498         default:
1499                 ret = -ENOTTY;
1500         }
1501
1502 unlock:
1503         mutex_unlock(&fb_helper->lock);
1504         return ret;
1505 }
1506 EXPORT_SYMBOL(drm_fb_helper_ioctl);
1507
1508 /**
1509  * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
1510  * @var: screeninfo to check
1511  * @info: fbdev registered by the helper
1512  */
1513 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1514                             struct fb_info *info)
1515 {
1516         struct drm_fb_helper *fb_helper = info->par;
1517         struct drm_framebuffer *fb = fb_helper->fb;
1518         int depth;
1519
1520         if (var->pixclock != 0 || in_dbg_master())
1521                 return -EINVAL;
1522
1523         /*
1524          * Changes struct fb_var_screeninfo are currently not pushed back
1525          * to KMS, hence fail if different settings are requested.
1526          */
1527         if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
1528             var->xres > fb->width || var->yres > fb->height ||
1529             var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1530                 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
1531                           "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1532                           var->xres, var->yres, var->bits_per_pixel,
1533                           var->xres_virtual, var->yres_virtual,
1534                           fb->width, fb->height, fb->format->cpp[0] * 8);
1535                 return -EINVAL;
1536         }
1537
1538         switch (var->bits_per_pixel) {
1539         case 16:
1540                 depth = (var->green.length == 6) ? 16 : 15;
1541                 break;
1542         case 32:
1543                 depth = (var->transp.length > 0) ? 32 : 24;
1544                 break;
1545         default:
1546                 depth = var->bits_per_pixel;
1547                 break;
1548         }
1549
1550         switch (depth) {
1551         case 8:
1552                 var->red.offset = 0;
1553                 var->green.offset = 0;
1554                 var->blue.offset = 0;
1555                 var->red.length = 8;
1556                 var->green.length = 8;
1557                 var->blue.length = 8;
1558                 var->transp.length = 0;
1559                 var->transp.offset = 0;
1560                 break;
1561         case 15:
1562                 var->red.offset = 10;
1563                 var->green.offset = 5;
1564                 var->blue.offset = 0;
1565                 var->red.length = 5;
1566                 var->green.length = 5;
1567                 var->blue.length = 5;
1568                 var->transp.length = 1;
1569                 var->transp.offset = 15;
1570                 break;
1571         case 16:
1572                 var->red.offset = 11;
1573                 var->green.offset = 5;
1574                 var->blue.offset = 0;
1575                 var->red.length = 5;
1576                 var->green.length = 6;
1577                 var->blue.length = 5;
1578                 var->transp.length = 0;
1579                 var->transp.offset = 0;
1580                 break;
1581         case 24:
1582                 var->red.offset = 16;
1583                 var->green.offset = 8;
1584                 var->blue.offset = 0;
1585                 var->red.length = 8;
1586                 var->green.length = 8;
1587                 var->blue.length = 8;
1588                 var->transp.length = 0;
1589                 var->transp.offset = 0;
1590                 break;
1591         case 32:
1592                 var->red.offset = 16;
1593                 var->green.offset = 8;
1594                 var->blue.offset = 0;
1595                 var->red.length = 8;
1596                 var->green.length = 8;
1597                 var->blue.length = 8;
1598                 var->transp.length = 8;
1599                 var->transp.offset = 24;
1600                 break;
1601         default:
1602                 return -EINVAL;
1603         }
1604         return 0;
1605 }
1606 EXPORT_SYMBOL(drm_fb_helper_check_var);
1607
1608 /**
1609  * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1610  * @info: fbdev registered by the helper
1611  *
1612  * This will let fbcon do the mode init and is called at initialization time by
1613  * the fbdev core when registering the driver, and later on through the hotplug
1614  * callback.
1615  */
1616 int drm_fb_helper_set_par(struct fb_info *info)
1617 {
1618         struct drm_fb_helper *fb_helper = info->par;
1619         struct fb_var_screeninfo *var = &info->var;
1620
1621         if (oops_in_progress)
1622                 return -EBUSY;
1623
1624         if (var->pixclock != 0) {
1625                 DRM_ERROR("PIXEL CLOCK SET\n");
1626                 return -EINVAL;
1627         }
1628
1629         drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1630
1631         return 0;
1632 }
1633 EXPORT_SYMBOL(drm_fb_helper_set_par);
1634
1635 static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
1636 {
1637         int i;
1638
1639         for (i = 0; i < fb_helper->crtc_count; i++) {
1640                 struct drm_mode_set *mode_set;
1641
1642                 mode_set = &fb_helper->crtc_info[i].mode_set;
1643
1644                 mode_set->x = x;
1645                 mode_set->y = y;
1646         }
1647 }
1648
1649 static int pan_display_atomic(struct fb_var_screeninfo *var,
1650                               struct fb_info *info)
1651 {
1652         struct drm_fb_helper *fb_helper = info->par;
1653         int ret;
1654
1655         pan_set(fb_helper, var->xoffset, var->yoffset);
1656
1657         ret = restore_fbdev_mode_atomic(fb_helper, true);
1658         if (!ret) {
1659                 info->var.xoffset = var->xoffset;
1660                 info->var.yoffset = var->yoffset;
1661         } else
1662                 pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
1663
1664         return ret;
1665 }
1666
1667 static int pan_display_legacy(struct fb_var_screeninfo *var,
1668                               struct fb_info *info)
1669 {
1670         struct drm_fb_helper *fb_helper = info->par;
1671         struct drm_mode_set *modeset;
1672         int ret = 0;
1673         int i;
1674
1675         drm_modeset_lock_all(fb_helper->dev);
1676         for (i = 0; i < fb_helper->crtc_count; i++) {
1677                 modeset = &fb_helper->crtc_info[i].mode_set;
1678
1679                 modeset->x = var->xoffset;
1680                 modeset->y = var->yoffset;
1681
1682                 if (modeset->num_connectors) {
1683                         ret = drm_mode_set_config_internal(modeset);
1684                         if (!ret) {
1685                                 info->var.xoffset = var->xoffset;
1686                                 info->var.yoffset = var->yoffset;
1687                         }
1688                 }
1689         }
1690         drm_modeset_unlock_all(fb_helper->dev);
1691
1692         return ret;
1693 }
1694
1695 /**
1696  * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1697  * @var: updated screen information
1698  * @info: fbdev registered by the helper
1699  */
1700 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1701                               struct fb_info *info)
1702 {
1703         struct drm_fb_helper *fb_helper = info->par;
1704         struct drm_device *dev = fb_helper->dev;
1705         int ret;
1706
1707         if (oops_in_progress)
1708                 return -EBUSY;
1709
1710         mutex_lock(&fb_helper->lock);
1711         if (!drm_fb_helper_is_bound(fb_helper)) {
1712                 mutex_unlock(&fb_helper->lock);
1713                 return -EBUSY;
1714         }
1715
1716         if (drm_drv_uses_atomic_modeset(dev))
1717                 ret = pan_display_atomic(var, info);
1718         else
1719                 ret = pan_display_legacy(var, info);
1720         mutex_unlock(&fb_helper->lock);
1721
1722         return ret;
1723 }
1724 EXPORT_SYMBOL(drm_fb_helper_pan_display);
1725
1726 /*
1727  * Allocates the backing storage and sets up the fbdev info structure through
1728  * the ->fb_probe callback.
1729  */
1730 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1731                                          int preferred_bpp)
1732 {
1733         int ret = 0;
1734         int crtc_count = 0;
1735         int i;
1736         struct drm_fb_helper_surface_size sizes;
1737         int gamma_size = 0;
1738
1739         memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1740         sizes.surface_depth = 24;
1741         sizes.surface_bpp = 32;
1742         sizes.fb_width = (u32)-1;
1743         sizes.fb_height = (u32)-1;
1744
1745         /* if driver picks 8 or 16 by default use that for both depth/bpp */
1746         if (preferred_bpp != sizes.surface_bpp)
1747                 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1748
1749         /* first up get a count of crtcs now in use and new min/maxes width/heights */
1750         drm_fb_helper_for_each_connector(fb_helper, i) {
1751                 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1752                 struct drm_cmdline_mode *cmdline_mode;
1753
1754                 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1755
1756                 if (cmdline_mode->bpp_specified) {
1757                         switch (cmdline_mode->bpp) {
1758                         case 8:
1759                                 sizes.surface_depth = sizes.surface_bpp = 8;
1760                                 break;
1761                         case 15:
1762                                 sizes.surface_depth = 15;
1763                                 sizes.surface_bpp = 16;
1764                                 break;
1765                         case 16:
1766                                 sizes.surface_depth = sizes.surface_bpp = 16;
1767                                 break;
1768                         case 24:
1769                                 sizes.surface_depth = sizes.surface_bpp = 24;
1770                                 break;
1771                         case 32:
1772                                 sizes.surface_depth = 24;
1773                                 sizes.surface_bpp = 32;
1774                                 break;
1775                         }
1776                         break;
1777                 }
1778         }
1779
1780         crtc_count = 0;
1781         for (i = 0; i < fb_helper->crtc_count; i++) {
1782                 struct drm_display_mode *desired_mode;
1783                 struct drm_mode_set *mode_set;
1784                 int x, y, j;
1785                 /* in case of tile group, are we the last tile vert or horiz?
1786                  * If no tile group you are always the last one both vertically
1787                  * and horizontally
1788                  */
1789                 bool lastv = true, lasth = true;
1790
1791                 desired_mode = fb_helper->crtc_info[i].desired_mode;
1792                 mode_set = &fb_helper->crtc_info[i].mode_set;
1793
1794                 if (!desired_mode)
1795                         continue;
1796
1797                 crtc_count++;
1798
1799                 x = fb_helper->crtc_info[i].x;
1800                 y = fb_helper->crtc_info[i].y;
1801
1802                 if (gamma_size == 0)
1803                         gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1804
1805                 sizes.surface_width  = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1806                 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
1807
1808                 for (j = 0; j < mode_set->num_connectors; j++) {
1809                         struct drm_connector *connector = mode_set->connectors[j];
1810
1811                         if (connector->has_tile) {
1812                                 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1813                                 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1814                                 /* cloning to multiple tiles is just crazy-talk, so: */
1815                                 break;
1816                         }
1817                 }
1818
1819                 if (lasth)
1820                         sizes.fb_width  = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1821                 if (lastv)
1822                         sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
1823         }
1824
1825         if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1826                 DRM_INFO("Cannot find any crtc or sizes\n");
1827
1828                 /* First time: disable all crtc's.. */
1829                 if (!fb_helper->deferred_setup && !READ_ONCE(fb_helper->dev->master))
1830                         restore_fbdev_mode(fb_helper);
1831                 return -EAGAIN;
1832         }
1833
1834         /* Handle our overallocation */
1835         sizes.surface_height *= drm_fbdev_overalloc;
1836         sizes.surface_height /= 100;
1837
1838         /* push down into drivers */
1839         ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1840         if (ret < 0)
1841                 return ret;
1842
1843         return 0;
1844 }
1845
1846 /**
1847  * drm_fb_helper_fill_fix - initializes fixed fbdev information
1848  * @info: fbdev registered by the helper
1849  * @pitch: desired pitch
1850  * @depth: desired depth
1851  *
1852  * Helper to fill in the fixed fbdev information useful for a non-accelerated
1853  * fbdev emulations. Drivers which support acceleration methods which impose
1854  * additional constraints need to set up their own limits.
1855  *
1856  * Drivers should call this (or their equivalent setup code) from their
1857  * &drm_fb_helper_funcs.fb_probe callback.
1858  */
1859 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1860                             uint32_t depth)
1861 {
1862         info->fix.type = FB_TYPE_PACKED_PIXELS;
1863         info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1864                 FB_VISUAL_TRUECOLOR;
1865         info->fix.mmio_start = 0;
1866         info->fix.mmio_len = 0;
1867         info->fix.type_aux = 0;
1868         info->fix.xpanstep = 1; /* doing it in hw */
1869         info->fix.ypanstep = 1; /* doing it in hw */
1870         info->fix.ywrapstep = 0;
1871         info->fix.accel = FB_ACCEL_NONE;
1872
1873         info->fix.line_length = pitch;
1874 }
1875 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1876
1877 /**
1878  * drm_fb_helper_fill_var - initalizes variable fbdev information
1879  * @info: fbdev instance to set up
1880  * @fb_helper: fb helper instance to use as template
1881  * @fb_width: desired fb width
1882  * @fb_height: desired fb height
1883  *
1884  * Sets up the variable fbdev metainformation from the given fb helper instance
1885  * and the drm framebuffer allocated in &drm_fb_helper.fb.
1886  *
1887  * Drivers should call this (or their equivalent setup code) from their
1888  * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1889  * backing storage framebuffer.
1890  */
1891 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1892                             uint32_t fb_width, uint32_t fb_height)
1893 {
1894         struct drm_framebuffer *fb = fb_helper->fb;
1895
1896         info->pseudo_palette = fb_helper->pseudo_palette;
1897         info->var.xres_virtual = fb->width;
1898         info->var.yres_virtual = fb->height;
1899         info->var.bits_per_pixel = fb->format->cpp[0] * 8;
1900         info->var.accel_flags = FB_ACCELF_TEXT;
1901         info->var.xoffset = 0;
1902         info->var.yoffset = 0;
1903         info->var.activate = FB_ACTIVATE_NOW;
1904
1905         switch (fb->format->depth) {
1906         case 8:
1907                 info->var.red.offset = 0;
1908                 info->var.green.offset = 0;
1909                 info->var.blue.offset = 0;
1910                 info->var.red.length = 8; /* 8bit DAC */
1911                 info->var.green.length = 8;
1912                 info->var.blue.length = 8;
1913                 info->var.transp.offset = 0;
1914                 info->var.transp.length = 0;
1915                 break;
1916         case 15:
1917                 info->var.red.offset = 10;
1918                 info->var.green.offset = 5;
1919                 info->var.blue.offset = 0;
1920                 info->var.red.length = 5;
1921                 info->var.green.length = 5;
1922                 info->var.blue.length = 5;
1923                 info->var.transp.offset = 15;
1924                 info->var.transp.length = 1;
1925                 break;
1926         case 16:
1927                 info->var.red.offset = 11;
1928                 info->var.green.offset = 5;
1929                 info->var.blue.offset = 0;
1930                 info->var.red.length = 5;
1931                 info->var.green.length = 6;
1932                 info->var.blue.length = 5;
1933                 info->var.transp.offset = 0;
1934                 break;
1935         case 24:
1936                 info->var.red.offset = 16;
1937                 info->var.green.offset = 8;
1938                 info->var.blue.offset = 0;
1939                 info->var.red.length = 8;
1940                 info->var.green.length = 8;
1941                 info->var.blue.length = 8;
1942                 info->var.transp.offset = 0;
1943                 info->var.transp.length = 0;
1944                 break;
1945         case 32:
1946                 info->var.red.offset = 16;
1947                 info->var.green.offset = 8;
1948                 info->var.blue.offset = 0;
1949                 info->var.red.length = 8;
1950                 info->var.green.length = 8;
1951                 info->var.blue.length = 8;
1952                 info->var.transp.offset = 24;
1953                 info->var.transp.length = 8;
1954                 break;
1955         default:
1956                 break;
1957         }
1958
1959         info->var.xres = fb_width;
1960         info->var.yres = fb_height;
1961 }
1962 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1963
1964 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1965                                                 uint32_t maxX,
1966                                                 uint32_t maxY)
1967 {
1968         struct drm_connector *connector;
1969         int i, count = 0;
1970
1971         drm_fb_helper_for_each_connector(fb_helper, i) {
1972                 connector = fb_helper->connector_info[i]->connector;
1973                 count += connector->funcs->fill_modes(connector, maxX, maxY);
1974         }
1975
1976         return count;
1977 }
1978
1979 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1980 {
1981         struct drm_display_mode *mode;
1982
1983         list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1984                 if (mode->hdisplay > width ||
1985                     mode->vdisplay > height)
1986                         continue;
1987                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1988                         return mode;
1989         }
1990         return NULL;
1991 }
1992 EXPORT_SYMBOL(drm_has_preferred_mode);
1993
1994 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1995 {
1996         return fb_connector->connector->cmdline_mode.specified;
1997 }
1998
1999 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
2000 {
2001         struct drm_cmdline_mode *cmdline_mode;
2002         struct drm_display_mode *mode;
2003         bool prefer_non_interlace;
2004
2005         cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
2006         if (cmdline_mode->specified == false)
2007                 return NULL;
2008
2009         /* attempt to find a matching mode in the list of modes
2010          *  we have gotten so far, if not add a CVT mode that conforms
2011          */
2012         if (cmdline_mode->rb || cmdline_mode->margins)
2013                 goto create_mode;
2014
2015         prefer_non_interlace = !cmdline_mode->interlace;
2016 again:
2017         list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2018                 /* check width/height */
2019                 if (mode->hdisplay != cmdline_mode->xres ||
2020                     mode->vdisplay != cmdline_mode->yres)
2021                         continue;
2022
2023                 if (cmdline_mode->refresh_specified) {
2024                         if (mode->vrefresh != cmdline_mode->refresh)
2025                                 continue;
2026                 }
2027
2028                 if (cmdline_mode->interlace) {
2029                         if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
2030                                 continue;
2031                 } else if (prefer_non_interlace) {
2032                         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2033                                 continue;
2034                 }
2035                 return mode;
2036         }
2037
2038         if (prefer_non_interlace) {
2039                 prefer_non_interlace = false;
2040                 goto again;
2041         }
2042
2043 create_mode:
2044         mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
2045                                                  cmdline_mode);
2046         list_add(&mode->head, &fb_helper_conn->connector->modes);
2047         return mode;
2048 }
2049 EXPORT_SYMBOL(drm_pick_cmdline_mode);
2050
2051 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
2052 {
2053         bool enable;
2054
2055         if (connector->display_info.non_desktop)
2056                 return false;
2057
2058         if (strict)
2059                 enable = connector->status == connector_status_connected;
2060         else
2061                 enable = connector->status != connector_status_disconnected;
2062
2063         return enable;
2064 }
2065
2066 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
2067                                   bool *enabled)
2068 {
2069         bool any_enabled = false;
2070         struct drm_connector *connector;
2071         int i = 0;
2072
2073         drm_fb_helper_for_each_connector(fb_helper, i) {
2074                 connector = fb_helper->connector_info[i]->connector;
2075                 enabled[i] = drm_connector_enabled(connector, true);
2076                 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
2077                               connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
2078
2079                 any_enabled |= enabled[i];
2080         }
2081
2082         if (any_enabled)
2083                 return;
2084
2085         drm_fb_helper_for_each_connector(fb_helper, i) {
2086                 connector = fb_helper->connector_info[i]->connector;
2087                 enabled[i] = drm_connector_enabled(connector, false);
2088         }
2089 }
2090
2091 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
2092                               struct drm_display_mode **modes,
2093                               struct drm_fb_offset *offsets,
2094                               bool *enabled, int width, int height)
2095 {
2096         int count, i, j;
2097         bool can_clone = false;
2098         struct drm_fb_helper_connector *fb_helper_conn;
2099         struct drm_display_mode *dmt_mode, *mode;
2100
2101         /* only contemplate cloning in the single crtc case */
2102         if (fb_helper->crtc_count > 1)
2103                 return false;
2104
2105         count = 0;
2106         drm_fb_helper_for_each_connector(fb_helper, i) {
2107                 if (enabled[i])
2108                         count++;
2109         }
2110
2111         /* only contemplate cloning if more than one connector is enabled */
2112         if (count <= 1)
2113                 return false;
2114
2115         /* check the command line or if nothing common pick 1024x768 */
2116         can_clone = true;
2117         drm_fb_helper_for_each_connector(fb_helper, i) {
2118                 if (!enabled[i])
2119                         continue;
2120                 fb_helper_conn = fb_helper->connector_info[i];
2121                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
2122                 if (!modes[i]) {
2123                         can_clone = false;
2124                         break;
2125                 }
2126                 for (j = 0; j < i; j++) {
2127                         if (!enabled[j])
2128                                 continue;
2129                         if (!drm_mode_equal(modes[j], modes[i]))
2130                                 can_clone = false;
2131                 }
2132         }
2133
2134         if (can_clone) {
2135                 DRM_DEBUG_KMS("can clone using command line\n");
2136                 return true;
2137         }
2138
2139         /* try and find a 1024x768 mode on each connector */
2140         can_clone = true;
2141         dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
2142
2143         drm_fb_helper_for_each_connector(fb_helper, i) {
2144                 if (!enabled[i])
2145                         continue;
2146
2147                 fb_helper_conn = fb_helper->connector_info[i];
2148                 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2149                         if (drm_mode_equal(mode, dmt_mode))
2150                                 modes[i] = mode;
2151                 }
2152                 if (!modes[i])
2153                         can_clone = false;
2154         }
2155
2156         if (can_clone) {
2157                 DRM_DEBUG_KMS("can clone using 1024x768\n");
2158                 return true;
2159         }
2160         DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2161         return false;
2162 }
2163
2164 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2165                                 struct drm_display_mode **modes,
2166                                 struct drm_fb_offset *offsets,
2167                                 int idx,
2168                                 int h_idx, int v_idx)
2169 {
2170         struct drm_fb_helper_connector *fb_helper_conn;
2171         int i;
2172         int hoffset = 0, voffset = 0;
2173
2174         drm_fb_helper_for_each_connector(fb_helper, i) {
2175                 fb_helper_conn = fb_helper->connector_info[i];
2176                 if (!fb_helper_conn->connector->has_tile)
2177                         continue;
2178
2179                 if (!modes[i] && (h_idx || v_idx)) {
2180                         DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2181                                       fb_helper_conn->connector->base.id);
2182                         continue;
2183                 }
2184                 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2185                         hoffset += modes[i]->hdisplay;
2186
2187                 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2188                         voffset += modes[i]->vdisplay;
2189         }
2190         offsets[idx].x = hoffset;
2191         offsets[idx].y = voffset;
2192         DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2193         return 0;
2194 }
2195
2196 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
2197                                  struct drm_display_mode **modes,
2198                                  struct drm_fb_offset *offsets,
2199                                  bool *enabled, int width, int height)
2200 {
2201         struct drm_fb_helper_connector *fb_helper_conn;
2202         const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2203         u64 conn_configured = 0;
2204         int tile_pass = 0;
2205         int i;
2206
2207 retry:
2208         drm_fb_helper_for_each_connector(fb_helper, i) {
2209                 fb_helper_conn = fb_helper->connector_info[i];
2210
2211                 if (conn_configured & BIT_ULL(i))
2212                         continue;
2213
2214                 if (enabled[i] == false) {
2215                         conn_configured |= BIT_ULL(i);
2216                         continue;
2217                 }
2218
2219                 /* first pass over all the untiled connectors */
2220                 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2221                         continue;
2222
2223                 if (tile_pass == 1) {
2224                         if (fb_helper_conn->connector->tile_h_loc != 0 ||
2225                             fb_helper_conn->connector->tile_v_loc != 0)
2226                                 continue;
2227
2228                 } else {
2229                         if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
2230                             fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2231                         /* if this tile_pass doesn't cover any of the tiles - keep going */
2232                                 continue;
2233
2234                         /*
2235                          * find the tile offsets for this pass - need to find
2236                          * all tiles left and above
2237                          */
2238                         drm_get_tile_offsets(fb_helper, modes, offsets,
2239                                              i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2240                 }
2241                 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
2242                               fb_helper_conn->connector->base.id);
2243
2244                 /* got for command line mode first */
2245                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
2246                 if (!modes[i]) {
2247                         DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2248                                       fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
2249                         modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
2250                 }
2251                 /* No preferred modes, pick one off the list */
2252                 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2253                         list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
2254                                 break;
2255                 }
2256                 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2257                           "none");
2258                 conn_configured |= BIT_ULL(i);
2259         }
2260
2261         if ((conn_configured & mask) != mask) {
2262                 tile_pass++;
2263                 goto retry;
2264         }
2265         return true;
2266 }
2267
2268 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2269                           struct drm_fb_helper_crtc **best_crtcs,
2270                           struct drm_display_mode **modes,
2271                           int n, int width, int height)
2272 {
2273         int c, o;
2274         struct drm_connector *connector;
2275         const struct drm_connector_helper_funcs *connector_funcs;
2276         struct drm_encoder *encoder;
2277         int my_score, best_score, score;
2278         struct drm_fb_helper_crtc **crtcs, *crtc;
2279         struct drm_fb_helper_connector *fb_helper_conn;
2280
2281         if (n == fb_helper->connector_count)
2282                 return 0;
2283
2284         fb_helper_conn = fb_helper->connector_info[n];
2285         connector = fb_helper_conn->connector;
2286
2287         best_crtcs[n] = NULL;
2288         best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
2289         if (modes[n] == NULL)
2290                 return best_score;
2291
2292         crtcs = kcalloc(fb_helper->connector_count,
2293                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2294         if (!crtcs)
2295                 return best_score;
2296
2297         my_score = 1;
2298         if (connector->status == connector_status_connected)
2299                 my_score++;
2300         if (drm_has_cmdline_mode(fb_helper_conn))
2301                 my_score++;
2302         if (drm_has_preferred_mode(fb_helper_conn, width, height))
2303                 my_score++;
2304
2305         connector_funcs = connector->helper_private;
2306
2307         /*
2308          * If the DRM device implements atomic hooks and ->best_encoder() is
2309          * NULL we fallback to the default drm_atomic_helper_best_encoder()
2310          * helper.
2311          */
2312         if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
2313             !connector_funcs->best_encoder)
2314                 encoder = drm_atomic_helper_best_encoder(connector);
2315         else
2316                 encoder = connector_funcs->best_encoder(connector);
2317
2318         if (!encoder)
2319                 goto out;
2320
2321         /*
2322          * select a crtc for this connector and then attempt to configure
2323          * remaining connectors
2324          */
2325         for (c = 0; c < fb_helper->crtc_count; c++) {
2326                 crtc = &fb_helper->crtc_info[c];
2327
2328                 if ((encoder->possible_crtcs & (1 << c)) == 0)
2329                         continue;
2330
2331                 for (o = 0; o < n; o++)
2332                         if (best_crtcs[o] == crtc)
2333                                 break;
2334
2335                 if (o < n) {
2336                         /* ignore cloning unless only a single crtc */
2337                         if (fb_helper->crtc_count > 1)
2338                                 continue;
2339
2340                         if (!drm_mode_equal(modes[o], modes[n]))
2341                                 continue;
2342                 }
2343
2344                 crtcs[n] = crtc;
2345                 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2346                 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
2347                                                   width, height);
2348                 if (score > best_score) {
2349                         best_score = score;
2350                         memcpy(best_crtcs, crtcs,
2351                                fb_helper->connector_count *
2352                                sizeof(struct drm_fb_helper_crtc *));
2353                 }
2354         }
2355 out:
2356         kfree(crtcs);
2357         return best_score;
2358 }
2359
2360 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2361                             u32 width, u32 height)
2362 {
2363         struct drm_device *dev = fb_helper->dev;
2364         struct drm_fb_helper_crtc **crtcs;
2365         struct drm_display_mode **modes;
2366         struct drm_fb_offset *offsets;
2367         bool *enabled;
2368         int i;
2369
2370         DRM_DEBUG_KMS("\n");
2371         /* prevent concurrent modification of connector_count by hotplug */
2372         lockdep_assert_held(&fb_helper->lock);
2373
2374         crtcs = kcalloc(fb_helper->connector_count,
2375                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2376         modes = kcalloc(fb_helper->connector_count,
2377                         sizeof(struct drm_display_mode *), GFP_KERNEL);
2378         offsets = kcalloc(fb_helper->connector_count,
2379                           sizeof(struct drm_fb_offset), GFP_KERNEL);
2380         enabled = kcalloc(fb_helper->connector_count,
2381                           sizeof(bool), GFP_KERNEL);
2382         if (!crtcs || !modes || !enabled || !offsets) {
2383                 DRM_ERROR("Memory allocation failed\n");
2384                 goto out;
2385         }
2386
2387         mutex_lock(&fb_helper->dev->mode_config.mutex);
2388         if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2389                 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
2390         drm_enable_connectors(fb_helper, enabled);
2391
2392         if (!(fb_helper->funcs->initial_config &&
2393               fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
2394                                                offsets,
2395                                                enabled, width, height))) {
2396                 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2397                 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2398                 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
2399
2400                 if (!drm_target_cloned(fb_helper, modes, offsets,
2401                                        enabled, width, height) &&
2402                     !drm_target_preferred(fb_helper, modes, offsets,
2403                                           enabled, width, height))
2404                         DRM_ERROR("Unable to find initial modes\n");
2405
2406                 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2407                               width, height);
2408
2409                 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
2410         }
2411         mutex_unlock(&fb_helper->dev->mode_config.mutex);
2412
2413         /* need to set the modesets up here for use later */
2414         /* fill out the connector<->crtc mappings into the modesets */
2415         for (i = 0; i < fb_helper->crtc_count; i++)
2416                 drm_fb_helper_modeset_release(fb_helper,
2417                                               &fb_helper->crtc_info[i].mode_set);
2418
2419         drm_fb_helper_for_each_connector(fb_helper, i) {
2420                 struct drm_display_mode *mode = modes[i];
2421                 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
2422                 struct drm_fb_offset *offset = &offsets[i];
2423
2424                 if (mode && fb_crtc) {
2425                         struct drm_mode_set *modeset = &fb_crtc->mode_set;
2426                         struct drm_connector *connector =
2427                                 fb_helper->connector_info[i]->connector;
2428
2429                         DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2430                                       mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
2431
2432                         fb_crtc->desired_mode = mode;
2433                         fb_crtc->x = offset->x;
2434                         fb_crtc->y = offset->y;
2435                         modeset->mode = drm_mode_duplicate(dev,
2436                                                            fb_crtc->desired_mode);
2437                         drm_connector_get(connector);
2438                         modeset->connectors[modeset->num_connectors++] = connector;
2439                         modeset->x = offset->x;
2440                         modeset->y = offset->y;
2441                 }
2442         }
2443 out:
2444         kfree(crtcs);
2445         kfree(modes);
2446         kfree(offsets);
2447         kfree(enabled);
2448 }
2449
2450 /*
2451  * This is a continuation of drm_setup_crtcs() that sets up anything related
2452  * to the framebuffer. During initialization, drm_setup_crtcs() is called before
2453  * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
2454  * So, any setup that touches those fields needs to be done here instead of in
2455  * drm_setup_crtcs().
2456  */
2457 static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
2458 {
2459         struct fb_info *info = fb_helper->fbdev;
2460         int i;
2461
2462         for (i = 0; i < fb_helper->crtc_count; i++)
2463                 if (fb_helper->crtc_info[i].mode_set.num_connectors)
2464                         fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
2465
2466         mutex_lock(&fb_helper->dev->mode_config.mutex);
2467         drm_fb_helper_for_each_connector(fb_helper, i) {
2468                 struct drm_connector *connector =
2469                                         fb_helper->connector_info[i]->connector;
2470
2471                 /* use first connected connector for the physical dimensions */
2472                 if (connector->status == connector_status_connected) {
2473                         info->var.width = connector->display_info.width_mm;
2474                         info->var.height = connector->display_info.height_mm;
2475                         break;
2476                 }
2477         }
2478         mutex_unlock(&fb_helper->dev->mode_config.mutex);
2479 }
2480
2481 /* Note: Drops fb_helper->lock before returning. */
2482 static int
2483 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
2484                                           int bpp_sel)
2485 {
2486         struct drm_device *dev = fb_helper->dev;
2487         struct fb_info *info;
2488         unsigned int width, height;
2489         int ret;
2490
2491         width = dev->mode_config.max_width;
2492         height = dev->mode_config.max_height;
2493
2494         drm_setup_crtcs(fb_helper, width, height);
2495         ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2496         if (ret < 0) {
2497                 if (ret == -EAGAIN) {
2498                         fb_helper->preferred_bpp = bpp_sel;
2499                         fb_helper->deferred_setup = true;
2500                         ret = 0;
2501                 }
2502                 mutex_unlock(&fb_helper->lock);
2503
2504                 return ret;
2505         }
2506         drm_setup_crtcs_fb(fb_helper);
2507
2508         fb_helper->deferred_setup = false;
2509
2510         info = fb_helper->fbdev;
2511         info->var.pixclock = 0;
2512
2513         /* Need to drop locks to avoid recursive deadlock in
2514          * register_framebuffer. This is ok because the only thing left to do is
2515          * register the fbdev emulation instance in kernel_fb_helper_list. */
2516         mutex_unlock(&fb_helper->lock);
2517
2518         ret = register_framebuffer(info);
2519         if (ret < 0)
2520                 return ret;
2521
2522         dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2523                  info->node, info->fix.id);
2524
2525         mutex_lock(&kernel_fb_helper_lock);
2526         if (list_empty(&kernel_fb_helper_list))
2527                 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2528
2529         list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
2530         mutex_unlock(&kernel_fb_helper_lock);
2531
2532         return 0;
2533 }
2534
2535 /**
2536  * drm_fb_helper_initial_config - setup a sane initial connector configuration
2537  * @fb_helper: fb_helper device struct
2538  * @bpp_sel: bpp value to use for the framebuffer configuration
2539  *
2540  * Scans the CRTCs and connectors and tries to put together an initial setup.
2541  * At the moment, this is a cloned configuration across all heads with
2542  * a new framebuffer object as the backing store.
2543  *
2544  * Note that this also registers the fbdev and so allows userspace to call into
2545  * the driver through the fbdev interfaces.
2546  *
2547  * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2548  * to let the driver allocate and initialize the fbdev info structure and the
2549  * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
2550  * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2551  * values for the fbdev info structure.
2552  *
2553  * HANG DEBUGGING:
2554  *
2555  * When you have fbcon support built-in or already loaded, this function will do
2556  * a full modeset to setup the fbdev console. Due to locking misdesign in the
2557  * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2558  * console_lock. Until console_unlock is called no dmesg lines will be sent out
2559  * to consoles, not even serial console. This means when your driver crashes,
2560  * you will see absolutely nothing else but a system stuck in this function,
2561  * with no further output. Any kind of printk() you place within your own driver
2562  * or in the drm core modeset code will also never show up.
2563  *
2564  * Standard debug practice is to run the fbcon setup without taking the
2565  * console_lock as a hack, to be able to see backtraces and crashes on the
2566  * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2567  * cmdline option.
2568  *
2569  * The other option is to just disable fbdev emulation since very likely the
2570  * first modeset from userspace will crash in the same way, and is even easier
2571  * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
2572  * kernel cmdline option.
2573  *
2574  * RETURNS:
2575  * Zero if everything went ok, nonzero otherwise.
2576  */
2577 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
2578 {
2579         int ret;
2580
2581         if (!drm_fbdev_emulation)
2582                 return 0;
2583
2584         mutex_lock(&fb_helper->lock);
2585         ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
2586
2587         return ret;
2588 }
2589 EXPORT_SYMBOL(drm_fb_helper_initial_config);
2590
2591 /**
2592  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
2593  *                               probing all the outputs attached to the fb
2594  * @fb_helper: driver-allocated fbdev helper, can be NULL
2595  *
2596  * Scan the connectors attached to the fb_helper and try to put together a
2597  * setup after notification of a change in output configuration.
2598  *
2599  * Called at runtime, takes the mode config locks to be able to check/change the
2600  * modeset configuration. Must be run from process context (which usually means
2601  * either the output polling work or a work item launched from the driver's
2602  * hotplug interrupt).
2603  *
2604  * Note that drivers may call this even before calling
2605  * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
2606  * for a race-free fbcon setup and will make sure that the fbdev emulation will
2607  * not miss any hotplug events.
2608  *
2609  * RETURNS:
2610  * 0 on success and a non-zero error code otherwise.
2611  */
2612 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
2613 {
2614         int err = 0;
2615
2616         if (!drm_fbdev_emulation || !fb_helper)
2617                 return 0;
2618
2619         mutex_lock(&fb_helper->lock);
2620         if (fb_helper->deferred_setup) {
2621                 err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
2622                                 fb_helper->preferred_bpp);
2623                 return err;
2624         }
2625
2626         if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
2627                 fb_helper->delayed_hotplug = true;
2628                 mutex_unlock(&fb_helper->lock);
2629                 return err;
2630         }
2631
2632         DRM_DEBUG_KMS("\n");
2633
2634         drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
2635         drm_setup_crtcs_fb(fb_helper);
2636         mutex_unlock(&fb_helper->lock);
2637
2638         drm_fb_helper_set_par(fb_helper->fbdev);
2639
2640         return 0;
2641 }
2642 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
2643
2644 /**
2645  * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
2646  * @dev: DRM device
2647  *
2648  * This function can be used as the &drm_driver->lastclose callback for drivers
2649  * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
2650  */
2651 void drm_fb_helper_lastclose(struct drm_device *dev)
2652 {
2653         drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
2654 }
2655 EXPORT_SYMBOL(drm_fb_helper_lastclose);
2656
2657 /**
2658  * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
2659  *                                     helper for fbdev emulation
2660  * @dev: DRM device
2661  *
2662  * This function can be used as the
2663  * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
2664  * need to call drm_fb_helper_hotplug_event().
2665  */
2666 void drm_fb_helper_output_poll_changed(struct drm_device *dev)
2667 {
2668         drm_fb_helper_hotplug_event(dev->fb_helper);
2669 }
2670 EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
2671
2672 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
2673  * but the module doesn't depend on any fb console symbols.  At least
2674  * attempt to load fbcon to avoid leaving the system without a usable console.
2675  */
2676 int __init drm_fb_helper_modinit(void)
2677 {
2678 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2679         const char name[] = "fbcon";
2680         struct module *fbcon;
2681
2682         mutex_lock(&module_mutex);
2683         fbcon = find_module(name);
2684         mutex_unlock(&module_mutex);
2685
2686         if (!fbcon)
2687                 request_module_nowait(name);
2688 #endif
2689         return 0;
2690 }
2691 EXPORT_SYMBOL(drm_fb_helper_modinit);
This page took 0.193173 seconds and 4 git commands to generate.