]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - drivers/gpu/drm/drm_fb_helper.c
Merge tag 'drm-intel-next-2019-05-24' of git://anongit.freedesktop.org/drm/drm-intel...
[thirdparty/kernel/stable.git] / drivers / gpu / drm / drm_fb_helper.c
1 /*
2 * Copyright (c) 2006-2009 Red Hat Inc.
3 * Copyright (c) 2006-2008 Intel Corporation
4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5 *
6 * DRM framebuffer helper functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Dave Airlie <airlied@linux.ie>
28 * Jesse Barnes <jesse.barnes@intel.com>
29 */
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32 #include <linux/console.h>
33 #include <linux/dma-buf.h>
34 #include <linux/kernel.h>
35 #include <linux/sysrq.h>
36 #include <linux/slab.h>
37 #include <linux/module.h>
38 #include <drm/drmP.h>
39 #include <drm/drm_crtc.h>
40 #include <drm/drm_fb_helper.h>
41 #include <drm/drm_crtc_helper.h>
42 #include <drm/drm_atomic.h>
43 #include <drm/drm_atomic_helper.h>
44
45 #include "drm_crtc_internal.h"
46 #include "drm_crtc_helper_internal.h"
47 #include "drm_internal.h"
48
49 static bool drm_fbdev_emulation = true;
50 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
51 MODULE_PARM_DESC(fbdev_emulation,
52 "Enable legacy fbdev emulation [default=true]");
53
54 static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
55 module_param(drm_fbdev_overalloc, int, 0444);
56 MODULE_PARM_DESC(drm_fbdev_overalloc,
57 "Overallocation of the fbdev buffer (%) [default="
58 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
59
60 /*
61 * In order to keep user-space compatibility, we want in certain use-cases
62 * to keep leaking the fbdev physical address to the user-space program
63 * handling the fbdev buffer.
64 * This is a bad habit essentially kept into closed source opengl driver
65 * that should really be moved into open-source upstream projects instead
66 * of using legacy physical addresses in user space to communicate with
67 * other out-of-tree kernel modules.
68 *
69 * This module_param *should* be removed as soon as possible and be
70 * considered as a broken and legacy behaviour from a modern fbdev device.
71 */
72 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
73 static bool drm_leak_fbdev_smem = false;
74 module_param_unsafe(drm_leak_fbdev_smem, bool, 0600);
75 MODULE_PARM_DESC(drm_leak_fbdev_smem,
76 "Allow unsafe leaking fbdev physical smem address [default=false]");
77 #endif
78
79 static LIST_HEAD(kernel_fb_helper_list);
80 static DEFINE_MUTEX(kernel_fb_helper_lock);
81
82 /**
83 * DOC: fbdev helpers
84 *
85 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
86 * mode setting driver. They can be used mostly independently from the crtc
87 * helper functions used by many drivers to implement the kernel mode setting
88 * interfaces.
89 *
90 * Drivers that support a dumb buffer with a virtual address and mmap support,
91 * should try out the generic fbdev emulation using drm_fbdev_generic_setup().
92 *
93 * Setup fbdev emulation by calling drm_fb_helper_fbdev_setup() and tear it
94 * down by calling drm_fb_helper_fbdev_teardown().
95 *
96 * Drivers that need to handle connector hotplugging (e.g. dp mst) can't use
97 * the setup helper and will need to do the whole four-step setup process with
98 * drm_fb_helper_prepare(), drm_fb_helper_init(),
99 * drm_fb_helper_single_add_all_connectors(), enable hotplugging and
100 * drm_fb_helper_initial_config() to avoid a possible race window.
101 *
102 * At runtime drivers should restore the fbdev console by using
103 * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback.
104 * They should also notify the fb helper code from updates to the output
105 * configuration by using drm_fb_helper_output_poll_changed() as their
106 * &drm_mode_config_funcs.output_poll_changed callback.
107 *
108 * For suspend/resume consider using drm_mode_config_helper_suspend() and
109 * drm_mode_config_helper_resume() which takes care of fbdev as well.
110 *
111 * All other functions exported by the fb helper library can be used to
112 * implement the fbdev driver interface by the driver.
113 *
114 * It is possible, though perhaps somewhat tricky, to implement race-free
115 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
116 * helper must be called first to initialize the minimum required to make
117 * hotplug detection work. Drivers also need to make sure to properly set up
118 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
119 * it is safe to enable interrupts and start processing hotplug events. At the
120 * same time, drivers should initialize all modeset objects such as CRTCs,
121 * encoders and connectors. To finish up the fbdev helper initialization, the
122 * drm_fb_helper_init() function is called. To probe for all attached displays
123 * and set up an initial configuration using the detected hardware, drivers
124 * should call drm_fb_helper_single_add_all_connectors() followed by
125 * drm_fb_helper_initial_config().
126 *
127 * If &drm_framebuffer_funcs.dirty is set, the
128 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
129 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
130 * away. This worker then calls the dirty() function ensuring that it will
131 * always run in process context since the fb_*() function could be running in
132 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
133 * callback it will also schedule dirty_work with the damage collected from the
134 * mmap page writes. Drivers can use drm_fb_helper_defio_init() to setup
135 * deferred I/O (coupled with drm_fb_helper_fbdev_teardown()).
136 */
137
138 #define drm_fb_helper_for_each_connector(fbh, i__) \
139 for (({ lockdep_assert_held(&(fbh)->lock); }), \
140 i__ = 0; i__ < (fbh)->connector_count; i__++)
141
142 static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
143 struct drm_connector *connector)
144 {
145 struct drm_fb_helper_connector *fb_conn;
146 struct drm_fb_helper_connector **temp;
147 unsigned int count;
148
149 if (!drm_fbdev_emulation)
150 return 0;
151
152 lockdep_assert_held(&fb_helper->lock);
153
154 count = fb_helper->connector_count + 1;
155
156 if (count > fb_helper->connector_info_alloc_count) {
157 size_t size = count * sizeof(fb_conn);
158
159 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
160 if (!temp)
161 return -ENOMEM;
162
163 fb_helper->connector_info_alloc_count = count;
164 fb_helper->connector_info = temp;
165 }
166
167 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
168 if (!fb_conn)
169 return -ENOMEM;
170
171 drm_connector_get(connector);
172 fb_conn->connector = connector;
173 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
174
175 return 0;
176 }
177
178 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
179 struct drm_connector *connector)
180 {
181 int err;
182
183 if (!fb_helper)
184 return 0;
185
186 mutex_lock(&fb_helper->lock);
187 err = __drm_fb_helper_add_one_connector(fb_helper, connector);
188 mutex_unlock(&fb_helper->lock);
189
190 return err;
191 }
192 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
193
194 /**
195 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
196 * emulation helper
197 * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL
198 *
199 * This functions adds all the available connectors for use with the given
200 * fb_helper. This is a separate step to allow drivers to freely assign
201 * connectors to the fbdev, e.g. if some are reserved for special purposes or
202 * not adequate to be used for the fbcon.
203 *
204 * This function is protected against concurrent connector hotadds/removals
205 * using drm_fb_helper_add_one_connector() and
206 * drm_fb_helper_remove_one_connector().
207 */
208 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
209 {
210 struct drm_device *dev;
211 struct drm_connector *connector;
212 struct drm_connector_list_iter conn_iter;
213 int i, ret = 0;
214
215 if (!drm_fbdev_emulation || !fb_helper)
216 return 0;
217
218 dev = fb_helper->dev;
219
220 mutex_lock(&fb_helper->lock);
221 drm_connector_list_iter_begin(dev, &conn_iter);
222 drm_for_each_connector_iter(connector, &conn_iter) {
223 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
224 continue;
225
226 ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
227 if (ret)
228 goto fail;
229 }
230 goto out;
231
232 fail:
233 drm_fb_helper_for_each_connector(fb_helper, i) {
234 struct drm_fb_helper_connector *fb_helper_connector =
235 fb_helper->connector_info[i];
236
237 drm_connector_put(fb_helper_connector->connector);
238
239 kfree(fb_helper_connector);
240 fb_helper->connector_info[i] = NULL;
241 }
242 fb_helper->connector_count = 0;
243 out:
244 drm_connector_list_iter_end(&conn_iter);
245 mutex_unlock(&fb_helper->lock);
246
247 return ret;
248 }
249 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
250
251 static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
252 struct drm_connector *connector)
253 {
254 struct drm_fb_helper_connector *fb_helper_connector;
255 int i, j;
256
257 if (!drm_fbdev_emulation)
258 return 0;
259
260 lockdep_assert_held(&fb_helper->lock);
261
262 drm_fb_helper_for_each_connector(fb_helper, i) {
263 if (fb_helper->connector_info[i]->connector == connector)
264 break;
265 }
266
267 if (i == fb_helper->connector_count)
268 return -EINVAL;
269 fb_helper_connector = fb_helper->connector_info[i];
270 drm_connector_put(fb_helper_connector->connector);
271
272 for (j = i + 1; j < fb_helper->connector_count; j++)
273 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
274
275 fb_helper->connector_count--;
276 kfree(fb_helper_connector);
277
278 return 0;
279 }
280
281 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
282 struct drm_connector *connector)
283 {
284 int err;
285
286 if (!fb_helper)
287 return 0;
288
289 mutex_lock(&fb_helper->lock);
290 err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
291 mutex_unlock(&fb_helper->lock);
292
293 return err;
294 }
295 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
296
297 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
298 {
299 uint16_t *r_base, *g_base, *b_base;
300
301 if (crtc->funcs->gamma_set == NULL)
302 return;
303
304 r_base = crtc->gamma_store;
305 g_base = r_base + crtc->gamma_size;
306 b_base = g_base + crtc->gamma_size;
307
308 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
309 crtc->gamma_size, NULL);
310 }
311
312 /**
313 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
314 * @info: fbdev registered by the helper
315 */
316 int drm_fb_helper_debug_enter(struct fb_info *info)
317 {
318 struct drm_fb_helper *helper = info->par;
319 const struct drm_crtc_helper_funcs *funcs;
320 int i;
321
322 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
323 for (i = 0; i < helper->crtc_count; i++) {
324 struct drm_mode_set *mode_set =
325 &helper->crtc_info[i].mode_set;
326
327 if (!mode_set->crtc->enabled)
328 continue;
329
330 funcs = mode_set->crtc->helper_private;
331 if (funcs->mode_set_base_atomic == NULL)
332 continue;
333
334 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
335 continue;
336
337 funcs->mode_set_base_atomic(mode_set->crtc,
338 mode_set->fb,
339 mode_set->x,
340 mode_set->y,
341 ENTER_ATOMIC_MODE_SET);
342 }
343 }
344
345 return 0;
346 }
347 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
348
349 /**
350 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
351 * @info: fbdev registered by the helper
352 */
353 int drm_fb_helper_debug_leave(struct fb_info *info)
354 {
355 struct drm_fb_helper *helper = info->par;
356 struct drm_crtc *crtc;
357 const struct drm_crtc_helper_funcs *funcs;
358 struct drm_framebuffer *fb;
359 int i;
360
361 for (i = 0; i < helper->crtc_count; i++) {
362 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
363
364 crtc = mode_set->crtc;
365 if (drm_drv_uses_atomic_modeset(crtc->dev))
366 continue;
367
368 funcs = crtc->helper_private;
369 fb = crtc->primary->fb;
370
371 if (!crtc->enabled)
372 continue;
373
374 if (!fb) {
375 DRM_ERROR("no fb to restore??\n");
376 continue;
377 }
378
379 if (funcs->mode_set_base_atomic == NULL)
380 continue;
381
382 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
383 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
384 crtc->y, LEAVE_ATOMIC_MODE_SET);
385 }
386
387 return 0;
388 }
389 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
390
391 /* Check if the plane can hw rotate to match panel orientation */
392 static bool drm_fb_helper_panel_rotation(struct drm_mode_set *modeset,
393 unsigned int *rotation)
394 {
395 struct drm_connector *connector = modeset->connectors[0];
396 struct drm_plane *plane = modeset->crtc->primary;
397 u64 valid_mask = 0;
398 unsigned int i;
399
400 if (!modeset->num_connectors)
401 return false;
402
403 switch (connector->display_info.panel_orientation) {
404 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
405 *rotation = DRM_MODE_ROTATE_180;
406 break;
407 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
408 *rotation = DRM_MODE_ROTATE_90;
409 break;
410 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
411 *rotation = DRM_MODE_ROTATE_270;
412 break;
413 default:
414 *rotation = DRM_MODE_ROTATE_0;
415 }
416
417 /*
418 * TODO: support 90 / 270 degree hardware rotation,
419 * depending on the hardware this may require the framebuffer
420 * to be in a specific tiling format.
421 */
422 if (*rotation != DRM_MODE_ROTATE_180 || !plane->rotation_property)
423 return false;
424
425 for (i = 0; i < plane->rotation_property->num_values; i++)
426 valid_mask |= (1ULL << plane->rotation_property->values[i]);
427
428 if (!(*rotation & valid_mask))
429 return false;
430
431 return true;
432 }
433
434 static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
435 {
436 struct drm_device *dev = fb_helper->dev;
437 struct drm_plane_state *plane_state;
438 struct drm_plane *plane;
439 struct drm_atomic_state *state;
440 int i, ret;
441 struct drm_modeset_acquire_ctx ctx;
442
443 drm_modeset_acquire_init(&ctx, 0);
444
445 state = drm_atomic_state_alloc(dev);
446 if (!state) {
447 ret = -ENOMEM;
448 goto out_ctx;
449 }
450
451 state->acquire_ctx = &ctx;
452 retry:
453 drm_for_each_plane(plane, dev) {
454 plane_state = drm_atomic_get_plane_state(state, plane);
455 if (IS_ERR(plane_state)) {
456 ret = PTR_ERR(plane_state);
457 goto out_state;
458 }
459
460 plane_state->rotation = DRM_MODE_ROTATE_0;
461
462 /* disable non-primary: */
463 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
464 continue;
465
466 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
467 if (ret != 0)
468 goto out_state;
469 }
470
471 for (i = 0; i < fb_helper->crtc_count; i++) {
472 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
473 struct drm_plane *primary = mode_set->crtc->primary;
474 unsigned int rotation;
475
476 if (drm_fb_helper_panel_rotation(mode_set, &rotation)) {
477 /* Cannot fail as we've already gotten the plane state above */
478 plane_state = drm_atomic_get_new_plane_state(state, primary);
479 plane_state->rotation = rotation;
480 }
481
482 ret = __drm_atomic_helper_set_config(mode_set, state);
483 if (ret != 0)
484 goto out_state;
485
486 /*
487 * __drm_atomic_helper_set_config() sets active when a
488 * mode is set, unconditionally clear it if we force DPMS off
489 */
490 if (!active) {
491 struct drm_crtc *crtc = mode_set->crtc;
492 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
493
494 crtc_state->active = false;
495 }
496 }
497
498 ret = drm_atomic_commit(state);
499
500 out_state:
501 if (ret == -EDEADLK)
502 goto backoff;
503
504 drm_atomic_state_put(state);
505 out_ctx:
506 drm_modeset_drop_locks(&ctx);
507 drm_modeset_acquire_fini(&ctx);
508
509 return ret;
510
511 backoff:
512 drm_atomic_state_clear(state);
513 drm_modeset_backoff(&ctx);
514
515 goto retry;
516 }
517
518 static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
519 {
520 struct drm_device *dev = fb_helper->dev;
521 struct drm_plane *plane;
522 int i, ret = 0;
523
524 drm_modeset_lock_all(fb_helper->dev);
525 drm_for_each_plane(plane, dev) {
526 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
527 drm_plane_force_disable(plane);
528
529 if (plane->rotation_property)
530 drm_mode_plane_set_obj_prop(plane,
531 plane->rotation_property,
532 DRM_MODE_ROTATE_0);
533 }
534
535 for (i = 0; i < fb_helper->crtc_count; i++) {
536 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
537 struct drm_crtc *crtc = mode_set->crtc;
538
539 if (crtc->funcs->cursor_set2) {
540 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
541 if (ret)
542 goto out;
543 } else if (crtc->funcs->cursor_set) {
544 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
545 if (ret)
546 goto out;
547 }
548
549 ret = drm_mode_set_config_internal(mode_set);
550 if (ret)
551 goto out;
552 }
553 out:
554 drm_modeset_unlock_all(fb_helper->dev);
555
556 return ret;
557 }
558
559 static int restore_fbdev_mode_force(struct drm_fb_helper *fb_helper)
560 {
561 struct drm_device *dev = fb_helper->dev;
562
563 if (drm_drv_uses_atomic_modeset(dev))
564 return restore_fbdev_mode_atomic(fb_helper, true);
565 else
566 return restore_fbdev_mode_legacy(fb_helper);
567 }
568
569 static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
570 {
571 struct drm_device *dev = fb_helper->dev;
572 int ret;
573
574 if (!drm_master_internal_acquire(dev))
575 return -EBUSY;
576
577 ret = restore_fbdev_mode_force(fb_helper);
578
579 drm_master_internal_release(dev);
580
581 return ret;
582 }
583
584 /**
585 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
586 * @fb_helper: driver-allocated fbdev helper, can be NULL
587 *
588 * This should be called from driver's drm &drm_driver.lastclose callback
589 * when implementing an fbcon on top of kms using this helper. This ensures that
590 * the user isn't greeted with a black screen when e.g. X dies.
591 *
592 * RETURNS:
593 * Zero if everything went ok, negative error code otherwise.
594 */
595 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
596 {
597 bool do_delayed;
598 int ret;
599
600 if (!drm_fbdev_emulation || !fb_helper)
601 return -ENODEV;
602
603 if (READ_ONCE(fb_helper->deferred_setup))
604 return 0;
605
606 mutex_lock(&fb_helper->lock);
607 /*
608 * TODO:
609 * We should bail out here if there is a master by dropping _force.
610 * Currently these igt tests fail if we do that:
611 * - kms_fbcon_fbt@psr
612 * - kms_fbcon_fbt@psr-suspend
613 *
614 * So first these tests need to be fixed so they drop master or don't
615 * have an fd open.
616 */
617 ret = restore_fbdev_mode_force(fb_helper);
618
619 do_delayed = fb_helper->delayed_hotplug;
620 if (do_delayed)
621 fb_helper->delayed_hotplug = false;
622 mutex_unlock(&fb_helper->lock);
623
624 if (do_delayed)
625 drm_fb_helper_hotplug_event(fb_helper);
626
627 return ret;
628 }
629 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
630
631 #ifdef CONFIG_MAGIC_SYSRQ
632 /*
633 * restore fbcon display for all kms driver's using this helper, used for sysrq
634 * and panic handling.
635 */
636 static bool drm_fb_helper_force_kernel_mode(void)
637 {
638 bool ret, error = false;
639 struct drm_fb_helper *helper;
640
641 if (list_empty(&kernel_fb_helper_list))
642 return false;
643
644 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
645 struct drm_device *dev = helper->dev;
646
647 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
648 continue;
649
650 mutex_lock(&helper->lock);
651 ret = restore_fbdev_mode_force(helper);
652 if (ret)
653 error = true;
654 mutex_unlock(&helper->lock);
655 }
656 return error;
657 }
658
659 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
660 {
661 bool ret;
662
663 ret = drm_fb_helper_force_kernel_mode();
664 if (ret == true)
665 DRM_ERROR("Failed to restore crtc configuration\n");
666 }
667 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
668
669 static void drm_fb_helper_sysrq(int dummy1)
670 {
671 schedule_work(&drm_fb_helper_restore_work);
672 }
673
674 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
675 .handler = drm_fb_helper_sysrq,
676 .help_msg = "force-fb(V)",
677 .action_msg = "Restore framebuffer console",
678 };
679 #else
680 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
681 #endif
682
683 static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
684 {
685 struct drm_device *dev = fb_helper->dev;
686 struct drm_connector *connector;
687 struct drm_mode_set *modeset;
688 int i, j;
689
690 drm_modeset_lock_all(dev);
691 for (i = 0; i < fb_helper->crtc_count; i++) {
692 modeset = &fb_helper->crtc_info[i].mode_set;
693
694 if (!modeset->crtc->enabled)
695 continue;
696
697 for (j = 0; j < modeset->num_connectors; j++) {
698 connector = modeset->connectors[j];
699 connector->funcs->dpms(connector, dpms_mode);
700 drm_object_property_set_value(&connector->base,
701 dev->mode_config.dpms_property, dpms_mode);
702 }
703 }
704 drm_modeset_unlock_all(dev);
705 }
706
707 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
708 {
709 struct drm_fb_helper *fb_helper = info->par;
710 struct drm_device *dev = fb_helper->dev;
711
712 /*
713 * For each CRTC in this fb, turn the connectors on/off.
714 */
715 mutex_lock(&fb_helper->lock);
716 if (!drm_master_internal_acquire(dev))
717 goto unlock;
718
719 if (drm_drv_uses_atomic_modeset(dev))
720 restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
721 else
722 dpms_legacy(fb_helper, dpms_mode);
723
724 drm_master_internal_release(dev);
725 unlock:
726 mutex_unlock(&fb_helper->lock);
727 }
728
729 /**
730 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
731 * @blank: desired blanking state
732 * @info: fbdev registered by the helper
733 */
734 int drm_fb_helper_blank(int blank, struct fb_info *info)
735 {
736 if (oops_in_progress)
737 return -EBUSY;
738
739 switch (blank) {
740 /* Display: On; HSync: On, VSync: On */
741 case FB_BLANK_UNBLANK:
742 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
743 break;
744 /* Display: Off; HSync: On, VSync: On */
745 case FB_BLANK_NORMAL:
746 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
747 break;
748 /* Display: Off; HSync: Off, VSync: On */
749 case FB_BLANK_HSYNC_SUSPEND:
750 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
751 break;
752 /* Display: Off; HSync: On, VSync: Off */
753 case FB_BLANK_VSYNC_SUSPEND:
754 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
755 break;
756 /* Display: Off; HSync: Off, VSync: Off */
757 case FB_BLANK_POWERDOWN:
758 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
759 break;
760 }
761 return 0;
762 }
763 EXPORT_SYMBOL(drm_fb_helper_blank);
764
765 static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
766 struct drm_mode_set *modeset)
767 {
768 int i;
769
770 for (i = 0; i < modeset->num_connectors; i++) {
771 drm_connector_put(modeset->connectors[i]);
772 modeset->connectors[i] = NULL;
773 }
774 modeset->num_connectors = 0;
775
776 drm_mode_destroy(helper->dev, modeset->mode);
777 modeset->mode = NULL;
778
779 /* FIXME should hold a ref? */
780 modeset->fb = NULL;
781 }
782
783 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
784 {
785 int i;
786
787 for (i = 0; i < helper->connector_count; i++) {
788 drm_connector_put(helper->connector_info[i]->connector);
789 kfree(helper->connector_info[i]);
790 }
791 kfree(helper->connector_info);
792
793 for (i = 0; i < helper->crtc_count; i++) {
794 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
795
796 drm_fb_helper_modeset_release(helper, modeset);
797 kfree(modeset->connectors);
798 }
799 kfree(helper->crtc_info);
800 }
801
802 static void drm_fb_helper_resume_worker(struct work_struct *work)
803 {
804 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
805 resume_work);
806
807 console_lock();
808 fb_set_suspend(helper->fbdev, 0);
809 console_unlock();
810 }
811
812 static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper,
813 struct drm_clip_rect *clip)
814 {
815 struct drm_framebuffer *fb = fb_helper->fb;
816 unsigned int cpp = fb->format->cpp[0];
817 size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp;
818 void *src = fb_helper->fbdev->screen_buffer + offset;
819 void *dst = fb_helper->buffer->vaddr + offset;
820 size_t len = (clip->x2 - clip->x1) * cpp;
821 unsigned int y;
822
823 for (y = clip->y1; y < clip->y2; y++) {
824 memcpy(dst, src, len);
825 src += fb->pitches[0];
826 dst += fb->pitches[0];
827 }
828 }
829
830 static void drm_fb_helper_dirty_work(struct work_struct *work)
831 {
832 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
833 dirty_work);
834 struct drm_clip_rect *clip = &helper->dirty_clip;
835 struct drm_clip_rect clip_copy;
836 unsigned long flags;
837
838 spin_lock_irqsave(&helper->dirty_lock, flags);
839 clip_copy = *clip;
840 clip->x1 = clip->y1 = ~0;
841 clip->x2 = clip->y2 = 0;
842 spin_unlock_irqrestore(&helper->dirty_lock, flags);
843
844 /* call dirty callback only when it has been really touched */
845 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) {
846 /* Generic fbdev uses a shadow buffer */
847 if (helper->buffer)
848 drm_fb_helper_dirty_blit_real(helper, &clip_copy);
849 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
850 }
851 }
852
853 /**
854 * drm_fb_helper_prepare - setup a drm_fb_helper structure
855 * @dev: DRM device
856 * @helper: driver-allocated fbdev helper structure to set up
857 * @funcs: pointer to structure of functions associate with this helper
858 *
859 * Sets up the bare minimum to make the framebuffer helper usable. This is
860 * useful to implement race-free initialization of the polling helpers.
861 */
862 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
863 const struct drm_fb_helper_funcs *funcs)
864 {
865 INIT_LIST_HEAD(&helper->kernel_fb_list);
866 spin_lock_init(&helper->dirty_lock);
867 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
868 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
869 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
870 mutex_init(&helper->lock);
871 helper->funcs = funcs;
872 helper->dev = dev;
873 }
874 EXPORT_SYMBOL(drm_fb_helper_prepare);
875
876 /**
877 * drm_fb_helper_init - initialize a &struct drm_fb_helper
878 * @dev: drm device
879 * @fb_helper: driver-allocated fbdev helper structure to initialize
880 * @max_conn_count: max connector count
881 *
882 * This allocates the structures for the fbdev helper with the given limits.
883 * Note that this won't yet touch the hardware (through the driver interfaces)
884 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
885 * to allow driver writes more control over the exact init sequence.
886 *
887 * Drivers must call drm_fb_helper_prepare() before calling this function.
888 *
889 * RETURNS:
890 * Zero if everything went ok, nonzero otherwise.
891 */
892 int drm_fb_helper_init(struct drm_device *dev,
893 struct drm_fb_helper *fb_helper,
894 int max_conn_count)
895 {
896 struct drm_crtc *crtc;
897 struct drm_mode_config *config = &dev->mode_config;
898 int i;
899
900 if (!drm_fbdev_emulation) {
901 dev->fb_helper = fb_helper;
902 return 0;
903 }
904
905 if (!max_conn_count)
906 return -EINVAL;
907
908 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
909 if (!fb_helper->crtc_info)
910 return -ENOMEM;
911
912 fb_helper->crtc_count = config->num_crtc;
913 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
914 if (!fb_helper->connector_info) {
915 kfree(fb_helper->crtc_info);
916 return -ENOMEM;
917 }
918 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
919 fb_helper->connector_count = 0;
920
921 for (i = 0; i < fb_helper->crtc_count; i++) {
922 fb_helper->crtc_info[i].mode_set.connectors =
923 kcalloc(max_conn_count,
924 sizeof(struct drm_connector *),
925 GFP_KERNEL);
926
927 if (!fb_helper->crtc_info[i].mode_set.connectors)
928 goto out_free;
929 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
930 }
931
932 i = 0;
933 drm_for_each_crtc(crtc, dev) {
934 fb_helper->crtc_info[i].mode_set.crtc = crtc;
935 i++;
936 }
937
938 dev->fb_helper = fb_helper;
939
940 return 0;
941 out_free:
942 drm_fb_helper_crtc_free(fb_helper);
943 return -ENOMEM;
944 }
945 EXPORT_SYMBOL(drm_fb_helper_init);
946
947 /**
948 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
949 * @fb_helper: driver-allocated fbdev helper
950 *
951 * A helper to alloc fb_info and the members cmap and apertures. Called
952 * by the driver within the fb_probe fb_helper callback function. Drivers do not
953 * need to release the allocated fb_info structure themselves, this is
954 * automatically done when calling drm_fb_helper_fini().
955 *
956 * RETURNS:
957 * fb_info pointer if things went okay, pointer containing error code
958 * otherwise
959 */
960 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
961 {
962 struct device *dev = fb_helper->dev->dev;
963 struct fb_info *info;
964 int ret;
965
966 info = framebuffer_alloc(0, dev);
967 if (!info)
968 return ERR_PTR(-ENOMEM);
969
970 ret = fb_alloc_cmap(&info->cmap, 256, 0);
971 if (ret)
972 goto err_release;
973
974 info->apertures = alloc_apertures(1);
975 if (!info->apertures) {
976 ret = -ENOMEM;
977 goto err_free_cmap;
978 }
979
980 fb_helper->fbdev = info;
981 info->skip_vt_switch = true;
982
983 return info;
984
985 err_free_cmap:
986 fb_dealloc_cmap(&info->cmap);
987 err_release:
988 framebuffer_release(info);
989 return ERR_PTR(ret);
990 }
991 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
992
993 /**
994 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
995 * @fb_helper: driver-allocated fbdev helper, can be NULL
996 *
997 * A wrapper around unregister_framebuffer, to release the fb_info
998 * framebuffer device. This must be called before releasing all resources for
999 * @fb_helper by calling drm_fb_helper_fini().
1000 */
1001 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
1002 {
1003 if (fb_helper && fb_helper->fbdev)
1004 unregister_framebuffer(fb_helper->fbdev);
1005 }
1006 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
1007
1008 /**
1009 * drm_fb_helper_fini - finialize a &struct drm_fb_helper
1010 * @fb_helper: driver-allocated fbdev helper, can be NULL
1011 *
1012 * This cleans up all remaining resources associated with @fb_helper. Must be
1013 * called after drm_fb_helper_unlink_fbi() was called.
1014 */
1015 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
1016 {
1017 struct fb_info *info;
1018
1019 if (!fb_helper)
1020 return;
1021
1022 fb_helper->dev->fb_helper = NULL;
1023
1024 if (!drm_fbdev_emulation)
1025 return;
1026
1027 cancel_work_sync(&fb_helper->resume_work);
1028 cancel_work_sync(&fb_helper->dirty_work);
1029
1030 info = fb_helper->fbdev;
1031 if (info) {
1032 if (info->cmap.len)
1033 fb_dealloc_cmap(&info->cmap);
1034 framebuffer_release(info);
1035 }
1036 fb_helper->fbdev = NULL;
1037
1038 mutex_lock(&kernel_fb_helper_lock);
1039 if (!list_empty(&fb_helper->kernel_fb_list)) {
1040 list_del(&fb_helper->kernel_fb_list);
1041 if (list_empty(&kernel_fb_helper_list))
1042 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1043 }
1044 mutex_unlock(&kernel_fb_helper_lock);
1045
1046 mutex_destroy(&fb_helper->lock);
1047 drm_fb_helper_crtc_free(fb_helper);
1048
1049 }
1050 EXPORT_SYMBOL(drm_fb_helper_fini);
1051
1052 /**
1053 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
1054 * @fb_helper: driver-allocated fbdev helper, can be NULL
1055 *
1056 * A wrapper around unlink_framebuffer implemented by fbdev core
1057 */
1058 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
1059 {
1060 if (fb_helper && fb_helper->fbdev)
1061 unlink_framebuffer(fb_helper->fbdev);
1062 }
1063 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
1064
1065 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
1066 u32 width, u32 height)
1067 {
1068 struct drm_fb_helper *helper = info->par;
1069 struct drm_clip_rect *clip = &helper->dirty_clip;
1070 unsigned long flags;
1071
1072 if (!helper->fb->funcs->dirty)
1073 return;
1074
1075 spin_lock_irqsave(&helper->dirty_lock, flags);
1076 clip->x1 = min_t(u32, clip->x1, x);
1077 clip->y1 = min_t(u32, clip->y1, y);
1078 clip->x2 = max_t(u32, clip->x2, x + width);
1079 clip->y2 = max_t(u32, clip->y2, y + height);
1080 spin_unlock_irqrestore(&helper->dirty_lock, flags);
1081
1082 schedule_work(&helper->dirty_work);
1083 }
1084
1085 /**
1086 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
1087 * @info: fb_info struct pointer
1088 * @pagelist: list of dirty mmap framebuffer pages
1089 *
1090 * This function is used as the &fb_deferred_io.deferred_io
1091 * callback function for flushing the fbdev mmap writes.
1092 */
1093 void drm_fb_helper_deferred_io(struct fb_info *info,
1094 struct list_head *pagelist)
1095 {
1096 unsigned long start, end, min, max;
1097 struct page *page;
1098 u32 y1, y2;
1099
1100 min = ULONG_MAX;
1101 max = 0;
1102 list_for_each_entry(page, pagelist, lru) {
1103 start = page->index << PAGE_SHIFT;
1104 end = start + PAGE_SIZE - 1;
1105 min = min(min, start);
1106 max = max(max, end);
1107 }
1108
1109 if (min < max) {
1110 y1 = min / info->fix.line_length;
1111 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
1112 info->var.yres);
1113 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
1114 }
1115 }
1116 EXPORT_SYMBOL(drm_fb_helper_deferred_io);
1117
1118 /**
1119 * drm_fb_helper_defio_init - fbdev deferred I/O initialization
1120 * @fb_helper: driver-allocated fbdev helper
1121 *
1122 * This function allocates &fb_deferred_io, sets callback to
1123 * drm_fb_helper_deferred_io(), delay to 50ms and calls fb_deferred_io_init().
1124 * It should be called from the &drm_fb_helper_funcs->fb_probe callback.
1125 * drm_fb_helper_fbdev_teardown() cleans up deferred I/O.
1126 *
1127 * NOTE: A copy of &fb_ops is made and assigned to &info->fbops. This is done
1128 * because fb_deferred_io_cleanup() clears &fbops->fb_mmap and would thereby
1129 * affect other instances of that &fb_ops.
1130 *
1131 * Returns:
1132 * 0 on success or a negative error code on failure.
1133 */
1134 int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper)
1135 {
1136 struct fb_info *info = fb_helper->fbdev;
1137 struct fb_deferred_io *fbdefio;
1138 struct fb_ops *fbops;
1139
1140 fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
1141 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
1142 if (!fbdefio || !fbops) {
1143 kfree(fbdefio);
1144 kfree(fbops);
1145 return -ENOMEM;
1146 }
1147
1148 info->fbdefio = fbdefio;
1149 fbdefio->delay = msecs_to_jiffies(50);
1150 fbdefio->deferred_io = drm_fb_helper_deferred_io;
1151
1152 *fbops = *info->fbops;
1153 info->fbops = fbops;
1154
1155 fb_deferred_io_init(info);
1156
1157 return 0;
1158 }
1159 EXPORT_SYMBOL(drm_fb_helper_defio_init);
1160
1161 /**
1162 * drm_fb_helper_sys_read - wrapper around fb_sys_read
1163 * @info: fb_info struct pointer
1164 * @buf: userspace buffer to read from framebuffer memory
1165 * @count: number of bytes to read from framebuffer memory
1166 * @ppos: read offset within framebuffer memory
1167 *
1168 * A wrapper around fb_sys_read implemented by fbdev core
1169 */
1170 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
1171 size_t count, loff_t *ppos)
1172 {
1173 return fb_sys_read(info, buf, count, ppos);
1174 }
1175 EXPORT_SYMBOL(drm_fb_helper_sys_read);
1176
1177 /**
1178 * drm_fb_helper_sys_write - wrapper around fb_sys_write
1179 * @info: fb_info struct pointer
1180 * @buf: userspace buffer to write to framebuffer memory
1181 * @count: number of bytes to write to framebuffer memory
1182 * @ppos: write offset within framebuffer memory
1183 *
1184 * A wrapper around fb_sys_write implemented by fbdev core
1185 */
1186 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
1187 size_t count, loff_t *ppos)
1188 {
1189 ssize_t ret;
1190
1191 ret = fb_sys_write(info, buf, count, ppos);
1192 if (ret > 0)
1193 drm_fb_helper_dirty(info, 0, 0, info->var.xres,
1194 info->var.yres);
1195
1196 return ret;
1197 }
1198 EXPORT_SYMBOL(drm_fb_helper_sys_write);
1199
1200 /**
1201 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
1202 * @info: fbdev registered by the helper
1203 * @rect: info about rectangle to fill
1204 *
1205 * A wrapper around sys_fillrect implemented by fbdev core
1206 */
1207 void drm_fb_helper_sys_fillrect(struct fb_info *info,
1208 const struct fb_fillrect *rect)
1209 {
1210 sys_fillrect(info, rect);
1211 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1212 rect->width, rect->height);
1213 }
1214 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
1215
1216 /**
1217 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
1218 * @info: fbdev registered by the helper
1219 * @area: info about area to copy
1220 *
1221 * A wrapper around sys_copyarea implemented by fbdev core
1222 */
1223 void drm_fb_helper_sys_copyarea(struct fb_info *info,
1224 const struct fb_copyarea *area)
1225 {
1226 sys_copyarea(info, area);
1227 drm_fb_helper_dirty(info, area->dx, area->dy,
1228 area->width, area->height);
1229 }
1230 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
1231
1232 /**
1233 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
1234 * @info: fbdev registered by the helper
1235 * @image: info about image to blit
1236 *
1237 * A wrapper around sys_imageblit implemented by fbdev core
1238 */
1239 void drm_fb_helper_sys_imageblit(struct fb_info *info,
1240 const struct fb_image *image)
1241 {
1242 sys_imageblit(info, image);
1243 drm_fb_helper_dirty(info, image->dx, image->dy,
1244 image->width, image->height);
1245 }
1246 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1247
1248 /**
1249 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1250 * @info: fbdev registered by the helper
1251 * @rect: info about rectangle to fill
1252 *
1253 * A wrapper around cfb_fillrect implemented by fbdev core
1254 */
1255 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1256 const struct fb_fillrect *rect)
1257 {
1258 cfb_fillrect(info, rect);
1259 drm_fb_helper_dirty(info, rect->dx, rect->dy,
1260 rect->width, rect->height);
1261 }
1262 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1263
1264 /**
1265 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1266 * @info: fbdev registered by the helper
1267 * @area: info about area to copy
1268 *
1269 * A wrapper around cfb_copyarea implemented by fbdev core
1270 */
1271 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1272 const struct fb_copyarea *area)
1273 {
1274 cfb_copyarea(info, area);
1275 drm_fb_helper_dirty(info, area->dx, area->dy,
1276 area->width, area->height);
1277 }
1278 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1279
1280 /**
1281 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1282 * @info: fbdev registered by the helper
1283 * @image: info about image to blit
1284 *
1285 * A wrapper around cfb_imageblit implemented by fbdev core
1286 */
1287 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1288 const struct fb_image *image)
1289 {
1290 cfb_imageblit(info, image);
1291 drm_fb_helper_dirty(info, image->dx, image->dy,
1292 image->width, image->height);
1293 }
1294 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1295
1296 /**
1297 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1298 * @fb_helper: driver-allocated fbdev helper, can be NULL
1299 * @suspend: whether to suspend or resume
1300 *
1301 * A wrapper around fb_set_suspend implemented by fbdev core.
1302 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
1303 * the lock yourself
1304 */
1305 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
1306 {
1307 if (fb_helper && fb_helper->fbdev)
1308 fb_set_suspend(fb_helper->fbdev, suspend);
1309 }
1310 EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1311
1312 /**
1313 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
1314 * takes the console lock
1315 * @fb_helper: driver-allocated fbdev helper, can be NULL
1316 * @suspend: whether to suspend or resume
1317 *
1318 * A wrapper around fb_set_suspend() that takes the console lock. If the lock
1319 * isn't available on resume, a worker is tasked with waiting for the lock
1320 * to become available. The console lock can be pretty contented on resume
1321 * due to all the printk activity.
1322 *
1323 * This function can be called multiple times with the same state since
1324 * &fb_info.state is checked to see if fbdev is running or not before locking.
1325 *
1326 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
1327 */
1328 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
1329 bool suspend)
1330 {
1331 if (!fb_helper || !fb_helper->fbdev)
1332 return;
1333
1334 /* make sure there's no pending/ongoing resume */
1335 flush_work(&fb_helper->resume_work);
1336
1337 if (suspend) {
1338 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
1339 return;
1340
1341 console_lock();
1342
1343 } else {
1344 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
1345 return;
1346
1347 if (!console_trylock()) {
1348 schedule_work(&fb_helper->resume_work);
1349 return;
1350 }
1351 }
1352
1353 fb_set_suspend(fb_helper->fbdev, suspend);
1354 console_unlock();
1355 }
1356 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
1357
1358 static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
1359 {
1360 u32 *palette = (u32 *)info->pseudo_palette;
1361 int i;
1362
1363 if (cmap->start + cmap->len > 16)
1364 return -EINVAL;
1365
1366 for (i = 0; i < cmap->len; ++i) {
1367 u16 red = cmap->red[i];
1368 u16 green = cmap->green[i];
1369 u16 blue = cmap->blue[i];
1370 u32 value;
1371
1372 red >>= 16 - info->var.red.length;
1373 green >>= 16 - info->var.green.length;
1374 blue >>= 16 - info->var.blue.length;
1375 value = (red << info->var.red.offset) |
1376 (green << info->var.green.offset) |
1377 (blue << info->var.blue.offset);
1378 if (info->var.transp.length > 0) {
1379 u32 mask = (1 << info->var.transp.length) - 1;
1380
1381 mask <<= info->var.transp.offset;
1382 value |= mask;
1383 }
1384 palette[cmap->start + i] = value;
1385 }
1386
1387 return 0;
1388 }
1389
1390 static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
1391 {
1392 struct drm_fb_helper *fb_helper = info->par;
1393 struct drm_crtc *crtc;
1394 u16 *r, *g, *b;
1395 int i, ret = 0;
1396
1397 drm_modeset_lock_all(fb_helper->dev);
1398 for (i = 0; i < fb_helper->crtc_count; i++) {
1399 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1400 if (!crtc->funcs->gamma_set || !crtc->gamma_size)
1401 return -EINVAL;
1402
1403 if (cmap->start + cmap->len > crtc->gamma_size)
1404 return -EINVAL;
1405
1406 r = crtc->gamma_store;
1407 g = r + crtc->gamma_size;
1408 b = g + crtc->gamma_size;
1409
1410 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1411 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1412 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1413
1414 ret = crtc->funcs->gamma_set(crtc, r, g, b,
1415 crtc->gamma_size, NULL);
1416 if (ret)
1417 return ret;
1418 }
1419 drm_modeset_unlock_all(fb_helper->dev);
1420
1421 return ret;
1422 }
1423
1424 static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
1425 struct fb_cmap *cmap)
1426 {
1427 struct drm_device *dev = crtc->dev;
1428 struct drm_property_blob *gamma_lut;
1429 struct drm_color_lut *lut;
1430 int size = crtc->gamma_size;
1431 int i;
1432
1433 if (!size || cmap->start + cmap->len > size)
1434 return ERR_PTR(-EINVAL);
1435
1436 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
1437 if (IS_ERR(gamma_lut))
1438 return gamma_lut;
1439
1440 lut = gamma_lut->data;
1441 if (cmap->start || cmap->len != size) {
1442 u16 *r = crtc->gamma_store;
1443 u16 *g = r + crtc->gamma_size;
1444 u16 *b = g + crtc->gamma_size;
1445
1446 for (i = 0; i < cmap->start; i++) {
1447 lut[i].red = r[i];
1448 lut[i].green = g[i];
1449 lut[i].blue = b[i];
1450 }
1451 for (i = cmap->start + cmap->len; i < size; i++) {
1452 lut[i].red = r[i];
1453 lut[i].green = g[i];
1454 lut[i].blue = b[i];
1455 }
1456 }
1457
1458 for (i = 0; i < cmap->len; i++) {
1459 lut[cmap->start + i].red = cmap->red[i];
1460 lut[cmap->start + i].green = cmap->green[i];
1461 lut[cmap->start + i].blue = cmap->blue[i];
1462 }
1463
1464 return gamma_lut;
1465 }
1466
1467 static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
1468 {
1469 struct drm_fb_helper *fb_helper = info->par;
1470 struct drm_device *dev = fb_helper->dev;
1471 struct drm_property_blob *gamma_lut = NULL;
1472 struct drm_modeset_acquire_ctx ctx;
1473 struct drm_crtc_state *crtc_state;
1474 struct drm_atomic_state *state;
1475 struct drm_crtc *crtc;
1476 u16 *r, *g, *b;
1477 int i, ret = 0;
1478 bool replaced;
1479
1480 drm_modeset_acquire_init(&ctx, 0);
1481
1482 state = drm_atomic_state_alloc(dev);
1483 if (!state) {
1484 ret = -ENOMEM;
1485 goto out_ctx;
1486 }
1487
1488 state->acquire_ctx = &ctx;
1489 retry:
1490 for (i = 0; i < fb_helper->crtc_count; i++) {
1491 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1492
1493 if (!gamma_lut)
1494 gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
1495 if (IS_ERR(gamma_lut)) {
1496 ret = PTR_ERR(gamma_lut);
1497 gamma_lut = NULL;
1498 goto out_state;
1499 }
1500
1501 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1502 if (IS_ERR(crtc_state)) {
1503 ret = PTR_ERR(crtc_state);
1504 goto out_state;
1505 }
1506
1507 replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
1508 NULL);
1509 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
1510 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
1511 gamma_lut);
1512 crtc_state->color_mgmt_changed |= replaced;
1513 }
1514
1515 ret = drm_atomic_commit(state);
1516 if (ret)
1517 goto out_state;
1518
1519 for (i = 0; i < fb_helper->crtc_count; i++) {
1520 crtc = fb_helper->crtc_info[i].mode_set.crtc;
1521
1522 r = crtc->gamma_store;
1523 g = r + crtc->gamma_size;
1524 b = g + crtc->gamma_size;
1525
1526 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1527 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1528 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1529 }
1530
1531 out_state:
1532 if (ret == -EDEADLK)
1533 goto backoff;
1534
1535 drm_property_blob_put(gamma_lut);
1536 drm_atomic_state_put(state);
1537 out_ctx:
1538 drm_modeset_drop_locks(&ctx);
1539 drm_modeset_acquire_fini(&ctx);
1540
1541 return ret;
1542
1543 backoff:
1544 drm_atomic_state_clear(state);
1545 drm_modeset_backoff(&ctx);
1546 goto retry;
1547 }
1548
1549 /**
1550 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1551 * @cmap: cmap to set
1552 * @info: fbdev registered by the helper
1553 */
1554 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1555 {
1556 struct drm_fb_helper *fb_helper = info->par;
1557 struct drm_device *dev = fb_helper->dev;
1558 int ret;
1559
1560 if (oops_in_progress)
1561 return -EBUSY;
1562
1563 mutex_lock(&fb_helper->lock);
1564
1565 if (!drm_master_internal_acquire(dev)) {
1566 ret = -EBUSY;
1567 goto unlock;
1568 }
1569
1570 if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1571 ret = setcmap_pseudo_palette(cmap, info);
1572 else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1573 ret = setcmap_atomic(cmap, info);
1574 else
1575 ret = setcmap_legacy(cmap, info);
1576
1577 drm_master_internal_release(dev);
1578 unlock:
1579 mutex_unlock(&fb_helper->lock);
1580
1581 return ret;
1582 }
1583 EXPORT_SYMBOL(drm_fb_helper_setcmap);
1584
1585 /**
1586 * drm_fb_helper_ioctl - legacy ioctl implementation
1587 * @info: fbdev registered by the helper
1588 * @cmd: ioctl command
1589 * @arg: ioctl argument
1590 *
1591 * A helper to implement the standard fbdev ioctl. Only
1592 * FBIO_WAITFORVSYNC is implemented for now.
1593 */
1594 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1595 unsigned long arg)
1596 {
1597 struct drm_fb_helper *fb_helper = info->par;
1598 struct drm_device *dev = fb_helper->dev;
1599 struct drm_mode_set *mode_set;
1600 struct drm_crtc *crtc;
1601 int ret = 0;
1602
1603 mutex_lock(&fb_helper->lock);
1604 if (!drm_master_internal_acquire(dev)) {
1605 ret = -EBUSY;
1606 goto unlock;
1607 }
1608
1609 switch (cmd) {
1610 case FBIO_WAITFORVSYNC:
1611 /*
1612 * Only consider the first CRTC.
1613 *
1614 * This ioctl is supposed to take the CRTC number as
1615 * an argument, but in fbdev times, what that number
1616 * was supposed to be was quite unclear, different
1617 * drivers were passing that argument differently
1618 * (some by reference, some by value), and most of the
1619 * userspace applications were just hardcoding 0 as an
1620 * argument.
1621 *
1622 * The first CRTC should be the integrated panel on
1623 * most drivers, so this is the best choice we can
1624 * make. If we're not smart enough here, one should
1625 * just consider switch the userspace to KMS.
1626 */
1627 mode_set = &fb_helper->crtc_info[0].mode_set;
1628 crtc = mode_set->crtc;
1629
1630 /*
1631 * Only wait for a vblank event if the CRTC is
1632 * enabled, otherwise just don't do anythintg,
1633 * not even report an error.
1634 */
1635 ret = drm_crtc_vblank_get(crtc);
1636 if (!ret) {
1637 drm_crtc_wait_one_vblank(crtc);
1638 drm_crtc_vblank_put(crtc);
1639 }
1640
1641 ret = 0;
1642 break;
1643 default:
1644 ret = -ENOTTY;
1645 }
1646
1647 drm_master_internal_release(dev);
1648 unlock:
1649 mutex_unlock(&fb_helper->lock);
1650 return ret;
1651 }
1652 EXPORT_SYMBOL(drm_fb_helper_ioctl);
1653
1654 static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1,
1655 const struct fb_var_screeninfo *var_2)
1656 {
1657 return var_1->bits_per_pixel == var_2->bits_per_pixel &&
1658 var_1->grayscale == var_2->grayscale &&
1659 var_1->red.offset == var_2->red.offset &&
1660 var_1->red.length == var_2->red.length &&
1661 var_1->red.msb_right == var_2->red.msb_right &&
1662 var_1->green.offset == var_2->green.offset &&
1663 var_1->green.length == var_2->green.length &&
1664 var_1->green.msb_right == var_2->green.msb_right &&
1665 var_1->blue.offset == var_2->blue.offset &&
1666 var_1->blue.length == var_2->blue.length &&
1667 var_1->blue.msb_right == var_2->blue.msb_right &&
1668 var_1->transp.offset == var_2->transp.offset &&
1669 var_1->transp.length == var_2->transp.length &&
1670 var_1->transp.msb_right == var_2->transp.msb_right;
1671 }
1672
1673 static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo *var,
1674 u8 depth)
1675 {
1676 switch (depth) {
1677 case 8:
1678 var->red.offset = 0;
1679 var->green.offset = 0;
1680 var->blue.offset = 0;
1681 var->red.length = 8; /* 8bit DAC */
1682 var->green.length = 8;
1683 var->blue.length = 8;
1684 var->transp.offset = 0;
1685 var->transp.length = 0;
1686 break;
1687 case 15:
1688 var->red.offset = 10;
1689 var->green.offset = 5;
1690 var->blue.offset = 0;
1691 var->red.length = 5;
1692 var->green.length = 5;
1693 var->blue.length = 5;
1694 var->transp.offset = 15;
1695 var->transp.length = 1;
1696 break;
1697 case 16:
1698 var->red.offset = 11;
1699 var->green.offset = 5;
1700 var->blue.offset = 0;
1701 var->red.length = 5;
1702 var->green.length = 6;
1703 var->blue.length = 5;
1704 var->transp.offset = 0;
1705 break;
1706 case 24:
1707 var->red.offset = 16;
1708 var->green.offset = 8;
1709 var->blue.offset = 0;
1710 var->red.length = 8;
1711 var->green.length = 8;
1712 var->blue.length = 8;
1713 var->transp.offset = 0;
1714 var->transp.length = 0;
1715 break;
1716 case 32:
1717 var->red.offset = 16;
1718 var->green.offset = 8;
1719 var->blue.offset = 0;
1720 var->red.length = 8;
1721 var->green.length = 8;
1722 var->blue.length = 8;
1723 var->transp.offset = 24;
1724 var->transp.length = 8;
1725 break;
1726 default:
1727 break;
1728 }
1729 }
1730
1731 /**
1732 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
1733 * @var: screeninfo to check
1734 * @info: fbdev registered by the helper
1735 */
1736 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1737 struct fb_info *info)
1738 {
1739 struct drm_fb_helper *fb_helper = info->par;
1740 struct drm_framebuffer *fb = fb_helper->fb;
1741
1742 if (in_dbg_master())
1743 return -EINVAL;
1744
1745 if (var->pixclock != 0) {
1746 DRM_DEBUG("fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n");
1747 var->pixclock = 0;
1748 }
1749
1750 if ((drm_format_info_block_width(fb->format, 0) > 1) ||
1751 (drm_format_info_block_height(fb->format, 0) > 1))
1752 return -EINVAL;
1753
1754 /*
1755 * Changes struct fb_var_screeninfo are currently not pushed back
1756 * to KMS, hence fail if different settings are requested.
1757 */
1758 if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
1759 var->xres > fb->width || var->yres > fb->height ||
1760 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1761 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
1762 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1763 var->xres, var->yres, var->bits_per_pixel,
1764 var->xres_virtual, var->yres_virtual,
1765 fb->width, fb->height, fb->format->cpp[0] * 8);
1766 return -EINVAL;
1767 }
1768
1769 /*
1770 * Workaround for SDL 1.2, which is known to be setting all pixel format
1771 * fields values to zero in some cases. We treat this situation as a
1772 * kind of "use some reasonable autodetected values".
1773 */
1774 if (!var->red.offset && !var->green.offset &&
1775 !var->blue.offset && !var->transp.offset &&
1776 !var->red.length && !var->green.length &&
1777 !var->blue.length && !var->transp.length &&
1778 !var->red.msb_right && !var->green.msb_right &&
1779 !var->blue.msb_right && !var->transp.msb_right) {
1780 drm_fb_helper_fill_pixel_fmt(var, fb->format->depth);
1781 }
1782
1783 /*
1784 * drm fbdev emulation doesn't support changing the pixel format at all,
1785 * so reject all pixel format changing requests.
1786 */
1787 if (!drm_fb_pixel_format_equal(var, &info->var)) {
1788 DRM_DEBUG("fbdev emulation doesn't support changing the pixel format\n");
1789 return -EINVAL;
1790 }
1791
1792 return 0;
1793 }
1794 EXPORT_SYMBOL(drm_fb_helper_check_var);
1795
1796 /**
1797 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1798 * @info: fbdev registered by the helper
1799 *
1800 * This will let fbcon do the mode init and is called at initialization time by
1801 * the fbdev core when registering the driver, and later on through the hotplug
1802 * callback.
1803 */
1804 int drm_fb_helper_set_par(struct fb_info *info)
1805 {
1806 struct drm_fb_helper *fb_helper = info->par;
1807 struct fb_var_screeninfo *var = &info->var;
1808
1809 if (oops_in_progress)
1810 return -EBUSY;
1811
1812 if (var->pixclock != 0) {
1813 DRM_ERROR("PIXEL CLOCK SET\n");
1814 return -EINVAL;
1815 }
1816
1817 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1818
1819 return 0;
1820 }
1821 EXPORT_SYMBOL(drm_fb_helper_set_par);
1822
1823 static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
1824 {
1825 int i;
1826
1827 for (i = 0; i < fb_helper->crtc_count; i++) {
1828 struct drm_mode_set *mode_set;
1829
1830 mode_set = &fb_helper->crtc_info[i].mode_set;
1831
1832 mode_set->x = x;
1833 mode_set->y = y;
1834 }
1835 }
1836
1837 static int pan_display_atomic(struct fb_var_screeninfo *var,
1838 struct fb_info *info)
1839 {
1840 struct drm_fb_helper *fb_helper = info->par;
1841 int ret;
1842
1843 pan_set(fb_helper, var->xoffset, var->yoffset);
1844
1845 ret = restore_fbdev_mode_atomic(fb_helper, true);
1846 if (!ret) {
1847 info->var.xoffset = var->xoffset;
1848 info->var.yoffset = var->yoffset;
1849 } else
1850 pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
1851
1852 return ret;
1853 }
1854
1855 static int pan_display_legacy(struct fb_var_screeninfo *var,
1856 struct fb_info *info)
1857 {
1858 struct drm_fb_helper *fb_helper = info->par;
1859 struct drm_mode_set *modeset;
1860 int ret = 0;
1861 int i;
1862
1863 drm_modeset_lock_all(fb_helper->dev);
1864 for (i = 0; i < fb_helper->crtc_count; i++) {
1865 modeset = &fb_helper->crtc_info[i].mode_set;
1866
1867 modeset->x = var->xoffset;
1868 modeset->y = var->yoffset;
1869
1870 if (modeset->num_connectors) {
1871 ret = drm_mode_set_config_internal(modeset);
1872 if (!ret) {
1873 info->var.xoffset = var->xoffset;
1874 info->var.yoffset = var->yoffset;
1875 }
1876 }
1877 }
1878 drm_modeset_unlock_all(fb_helper->dev);
1879
1880 return ret;
1881 }
1882
1883 /**
1884 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1885 * @var: updated screen information
1886 * @info: fbdev registered by the helper
1887 */
1888 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1889 struct fb_info *info)
1890 {
1891 struct drm_fb_helper *fb_helper = info->par;
1892 struct drm_device *dev = fb_helper->dev;
1893 int ret;
1894
1895 if (oops_in_progress)
1896 return -EBUSY;
1897
1898 mutex_lock(&fb_helper->lock);
1899 if (!drm_master_internal_acquire(dev)) {
1900 ret = -EBUSY;
1901 goto unlock;
1902 }
1903
1904 if (drm_drv_uses_atomic_modeset(dev))
1905 ret = pan_display_atomic(var, info);
1906 else
1907 ret = pan_display_legacy(var, info);
1908
1909 drm_master_internal_release(dev);
1910 unlock:
1911 mutex_unlock(&fb_helper->lock);
1912
1913 return ret;
1914 }
1915 EXPORT_SYMBOL(drm_fb_helper_pan_display);
1916
1917 /*
1918 * Allocates the backing storage and sets up the fbdev info structure through
1919 * the ->fb_probe callback.
1920 */
1921 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1922 int preferred_bpp)
1923 {
1924 int ret = 0;
1925 int crtc_count = 0;
1926 int i;
1927 struct drm_fb_helper_surface_size sizes;
1928 int best_depth = 0;
1929
1930 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1931 sizes.surface_depth = 24;
1932 sizes.surface_bpp = 32;
1933 sizes.fb_width = (u32)-1;
1934 sizes.fb_height = (u32)-1;
1935
1936 /*
1937 * If driver picks 8 or 16 by default use that for both depth/bpp
1938 * to begin with
1939 */
1940 if (preferred_bpp != sizes.surface_bpp)
1941 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1942
1943 drm_fb_helper_for_each_connector(fb_helper, i) {
1944 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1945 struct drm_cmdline_mode *cmdline_mode;
1946
1947 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1948
1949 if (cmdline_mode->bpp_specified) {
1950 switch (cmdline_mode->bpp) {
1951 case 8:
1952 sizes.surface_depth = sizes.surface_bpp = 8;
1953 break;
1954 case 15:
1955 sizes.surface_depth = 15;
1956 sizes.surface_bpp = 16;
1957 break;
1958 case 16:
1959 sizes.surface_depth = sizes.surface_bpp = 16;
1960 break;
1961 case 24:
1962 sizes.surface_depth = sizes.surface_bpp = 24;
1963 break;
1964 case 32:
1965 sizes.surface_depth = 24;
1966 sizes.surface_bpp = 32;
1967 break;
1968 }
1969 break;
1970 }
1971 }
1972
1973 /*
1974 * If we run into a situation where, for example, the primary plane
1975 * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth
1976 * 16) we need to scale down the depth of the sizes we request.
1977 */
1978 for (i = 0; i < fb_helper->crtc_count; i++) {
1979 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
1980 struct drm_crtc *crtc = mode_set->crtc;
1981 struct drm_plane *plane = crtc->primary;
1982 int j;
1983
1984 DRM_DEBUG("test CRTC %d primary plane\n", i);
1985
1986 for (j = 0; j < plane->format_count; j++) {
1987 const struct drm_format_info *fmt;
1988
1989 fmt = drm_format_info(plane->format_types[j]);
1990
1991 /*
1992 * Do not consider YUV or other complicated formats
1993 * for framebuffers. This means only legacy formats
1994 * are supported (fmt->depth is a legacy field) but
1995 * the framebuffer emulation can only deal with such
1996 * formats, specifically RGB/BGA formats.
1997 */
1998 if (fmt->depth == 0)
1999 continue;
2000
2001 /* We found a perfect fit, great */
2002 if (fmt->depth == sizes.surface_depth) {
2003 best_depth = fmt->depth;
2004 break;
2005 }
2006
2007 /* Skip depths above what we're looking for */
2008 if (fmt->depth > sizes.surface_depth)
2009 continue;
2010
2011 /* Best depth found so far */
2012 if (fmt->depth > best_depth)
2013 best_depth = fmt->depth;
2014 }
2015 }
2016 if (sizes.surface_depth != best_depth && best_depth) {
2017 DRM_INFO("requested bpp %d, scaled depth down to %d",
2018 sizes.surface_bpp, best_depth);
2019 sizes.surface_depth = best_depth;
2020 }
2021
2022 /* first up get a count of crtcs now in use and new min/maxes width/heights */
2023 crtc_count = 0;
2024 for (i = 0; i < fb_helper->crtc_count; i++) {
2025 struct drm_display_mode *desired_mode;
2026 struct drm_mode_set *mode_set;
2027 int x, y, j;
2028 /* in case of tile group, are we the last tile vert or horiz?
2029 * If no tile group you are always the last one both vertically
2030 * and horizontally
2031 */
2032 bool lastv = true, lasth = true;
2033
2034 mode_set = &fb_helper->crtc_info[i].mode_set;
2035 desired_mode = mode_set->mode;
2036
2037 if (!desired_mode)
2038 continue;
2039
2040 crtc_count++;
2041
2042 x = mode_set->x;
2043 y = mode_set->y;
2044
2045 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
2046 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
2047
2048 for (j = 0; j < mode_set->num_connectors; j++) {
2049 struct drm_connector *connector = mode_set->connectors[j];
2050
2051 if (connector->has_tile) {
2052 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
2053 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
2054 /* cloning to multiple tiles is just crazy-talk, so: */
2055 break;
2056 }
2057 }
2058
2059 if (lasth)
2060 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
2061 if (lastv)
2062 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
2063 }
2064
2065 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
2066 DRM_INFO("Cannot find any crtc or sizes\n");
2067
2068 /* First time: disable all crtc's.. */
2069 if (!fb_helper->deferred_setup)
2070 restore_fbdev_mode(fb_helper);
2071 return -EAGAIN;
2072 }
2073
2074 /* Handle our overallocation */
2075 sizes.surface_height *= drm_fbdev_overalloc;
2076 sizes.surface_height /= 100;
2077
2078 /* push down into drivers */
2079 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
2080 if (ret < 0)
2081 return ret;
2082
2083 strcpy(fb_helper->fb->comm, "[fbcon]");
2084 return 0;
2085 }
2086
2087 static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
2088 uint32_t depth)
2089 {
2090 info->fix.type = FB_TYPE_PACKED_PIXELS;
2091 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
2092 FB_VISUAL_TRUECOLOR;
2093 info->fix.mmio_start = 0;
2094 info->fix.mmio_len = 0;
2095 info->fix.type_aux = 0;
2096 info->fix.xpanstep = 1; /* doing it in hw */
2097 info->fix.ypanstep = 1; /* doing it in hw */
2098 info->fix.ywrapstep = 0;
2099 info->fix.accel = FB_ACCEL_NONE;
2100
2101 info->fix.line_length = pitch;
2102 }
2103
2104 static void drm_fb_helper_fill_var(struct fb_info *info,
2105 struct drm_fb_helper *fb_helper,
2106 uint32_t fb_width, uint32_t fb_height)
2107 {
2108 struct drm_framebuffer *fb = fb_helper->fb;
2109
2110 WARN_ON((drm_format_info_block_width(fb->format, 0) > 1) ||
2111 (drm_format_info_block_height(fb->format, 0) > 1));
2112 info->pseudo_palette = fb_helper->pseudo_palette;
2113 info->var.xres_virtual = fb->width;
2114 info->var.yres_virtual = fb->height;
2115 info->var.bits_per_pixel = fb->format->cpp[0] * 8;
2116 info->var.accel_flags = FB_ACCELF_TEXT;
2117 info->var.xoffset = 0;
2118 info->var.yoffset = 0;
2119 info->var.activate = FB_ACTIVATE_NOW;
2120
2121 drm_fb_helper_fill_pixel_fmt(&info->var, fb->format->depth);
2122
2123 info->var.xres = fb_width;
2124 info->var.yres = fb_height;
2125 }
2126
2127 /**
2128 * drm_fb_helper_fill_info - initializes fbdev information
2129 * @info: fbdev instance to set up
2130 * @fb_helper: fb helper instance to use as template
2131 * @sizes: describes fbdev size and scanout surface size
2132 *
2133 * Sets up the variable and fixed fbdev metainformation from the given fb helper
2134 * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
2135 *
2136 * Drivers should call this (or their equivalent setup code) from their
2137 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
2138 * backing storage framebuffer.
2139 */
2140 void drm_fb_helper_fill_info(struct fb_info *info,
2141 struct drm_fb_helper *fb_helper,
2142 struct drm_fb_helper_surface_size *sizes)
2143 {
2144 struct drm_framebuffer *fb = fb_helper->fb;
2145
2146 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
2147 drm_fb_helper_fill_var(info, fb_helper,
2148 sizes->fb_width, sizes->fb_height);
2149
2150 info->par = fb_helper;
2151 snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb",
2152 fb_helper->dev->driver->name);
2153
2154 }
2155 EXPORT_SYMBOL(drm_fb_helper_fill_info);
2156
2157 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
2158 uint32_t maxX,
2159 uint32_t maxY)
2160 {
2161 struct drm_connector *connector;
2162 int i, count = 0;
2163
2164 drm_fb_helper_for_each_connector(fb_helper, i) {
2165 connector = fb_helper->connector_info[i]->connector;
2166 count += connector->funcs->fill_modes(connector, maxX, maxY);
2167 }
2168
2169 return count;
2170 }
2171
2172 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
2173 {
2174 struct drm_display_mode *mode;
2175
2176 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
2177 if (mode->hdisplay > width ||
2178 mode->vdisplay > height)
2179 continue;
2180 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2181 return mode;
2182 }
2183 return NULL;
2184 }
2185 EXPORT_SYMBOL(drm_has_preferred_mode);
2186
2187 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
2188 {
2189 return fb_connector->connector->cmdline_mode.specified;
2190 }
2191
2192 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
2193 {
2194 struct drm_cmdline_mode *cmdline_mode;
2195 struct drm_display_mode *mode;
2196 bool prefer_non_interlace;
2197
2198 cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
2199 if (cmdline_mode->specified == false)
2200 return NULL;
2201
2202 /* attempt to find a matching mode in the list of modes
2203 * we have gotten so far, if not add a CVT mode that conforms
2204 */
2205 if (cmdline_mode->rb || cmdline_mode->margins)
2206 goto create_mode;
2207
2208 prefer_non_interlace = !cmdline_mode->interlace;
2209 again:
2210 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2211 /* check width/height */
2212 if (mode->hdisplay != cmdline_mode->xres ||
2213 mode->vdisplay != cmdline_mode->yres)
2214 continue;
2215
2216 if (cmdline_mode->refresh_specified) {
2217 if (mode->vrefresh != cmdline_mode->refresh)
2218 continue;
2219 }
2220
2221 if (cmdline_mode->interlace) {
2222 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
2223 continue;
2224 } else if (prefer_non_interlace) {
2225 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2226 continue;
2227 }
2228 return mode;
2229 }
2230
2231 if (prefer_non_interlace) {
2232 prefer_non_interlace = false;
2233 goto again;
2234 }
2235
2236 create_mode:
2237 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
2238 cmdline_mode);
2239 list_add(&mode->head, &fb_helper_conn->connector->modes);
2240 return mode;
2241 }
2242 EXPORT_SYMBOL(drm_pick_cmdline_mode);
2243
2244 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
2245 {
2246 bool enable;
2247
2248 if (connector->display_info.non_desktop)
2249 return false;
2250
2251 if (strict)
2252 enable = connector->status == connector_status_connected;
2253 else
2254 enable = connector->status != connector_status_disconnected;
2255
2256 return enable;
2257 }
2258
2259 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
2260 bool *enabled)
2261 {
2262 bool any_enabled = false;
2263 struct drm_connector *connector;
2264 int i = 0;
2265
2266 drm_fb_helper_for_each_connector(fb_helper, i) {
2267 connector = fb_helper->connector_info[i]->connector;
2268 enabled[i] = drm_connector_enabled(connector, true);
2269 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
2270 connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
2271
2272 any_enabled |= enabled[i];
2273 }
2274
2275 if (any_enabled)
2276 return;
2277
2278 drm_fb_helper_for_each_connector(fb_helper, i) {
2279 connector = fb_helper->connector_info[i]->connector;
2280 enabled[i] = drm_connector_enabled(connector, false);
2281 }
2282 }
2283
2284 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
2285 struct drm_display_mode **modes,
2286 struct drm_fb_offset *offsets,
2287 bool *enabled, int width, int height)
2288 {
2289 int count, i, j;
2290 bool can_clone = false;
2291 struct drm_fb_helper_connector *fb_helper_conn;
2292 struct drm_display_mode *dmt_mode, *mode;
2293
2294 /* only contemplate cloning in the single crtc case */
2295 if (fb_helper->crtc_count > 1)
2296 return false;
2297
2298 count = 0;
2299 drm_fb_helper_for_each_connector(fb_helper, i) {
2300 if (enabled[i])
2301 count++;
2302 }
2303
2304 /* only contemplate cloning if more than one connector is enabled */
2305 if (count <= 1)
2306 return false;
2307
2308 /* check the command line or if nothing common pick 1024x768 */
2309 can_clone = true;
2310 drm_fb_helper_for_each_connector(fb_helper, i) {
2311 if (!enabled[i])
2312 continue;
2313 fb_helper_conn = fb_helper->connector_info[i];
2314 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
2315 if (!modes[i]) {
2316 can_clone = false;
2317 break;
2318 }
2319 for (j = 0; j < i; j++) {
2320 if (!enabled[j])
2321 continue;
2322 if (!drm_mode_match(modes[j], modes[i],
2323 DRM_MODE_MATCH_TIMINGS |
2324 DRM_MODE_MATCH_CLOCK |
2325 DRM_MODE_MATCH_FLAGS |
2326 DRM_MODE_MATCH_3D_FLAGS))
2327 can_clone = false;
2328 }
2329 }
2330
2331 if (can_clone) {
2332 DRM_DEBUG_KMS("can clone using command line\n");
2333 return true;
2334 }
2335
2336 /* try and find a 1024x768 mode on each connector */
2337 can_clone = true;
2338 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
2339
2340 drm_fb_helper_for_each_connector(fb_helper, i) {
2341 if (!enabled[i])
2342 continue;
2343
2344 fb_helper_conn = fb_helper->connector_info[i];
2345 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
2346 if (drm_mode_match(mode, dmt_mode,
2347 DRM_MODE_MATCH_TIMINGS |
2348 DRM_MODE_MATCH_CLOCK |
2349 DRM_MODE_MATCH_FLAGS |
2350 DRM_MODE_MATCH_3D_FLAGS))
2351 modes[i] = mode;
2352 }
2353 if (!modes[i])
2354 can_clone = false;
2355 }
2356
2357 if (can_clone) {
2358 DRM_DEBUG_KMS("can clone using 1024x768\n");
2359 return true;
2360 }
2361 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
2362 return false;
2363 }
2364
2365 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
2366 struct drm_display_mode **modes,
2367 struct drm_fb_offset *offsets,
2368 int idx,
2369 int h_idx, int v_idx)
2370 {
2371 struct drm_fb_helper_connector *fb_helper_conn;
2372 int i;
2373 int hoffset = 0, voffset = 0;
2374
2375 drm_fb_helper_for_each_connector(fb_helper, i) {
2376 fb_helper_conn = fb_helper->connector_info[i];
2377 if (!fb_helper_conn->connector->has_tile)
2378 continue;
2379
2380 if (!modes[i] && (h_idx || v_idx)) {
2381 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
2382 fb_helper_conn->connector->base.id);
2383 continue;
2384 }
2385 if (fb_helper_conn->connector->tile_h_loc < h_idx)
2386 hoffset += modes[i]->hdisplay;
2387
2388 if (fb_helper_conn->connector->tile_v_loc < v_idx)
2389 voffset += modes[i]->vdisplay;
2390 }
2391 offsets[idx].x = hoffset;
2392 offsets[idx].y = voffset;
2393 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
2394 return 0;
2395 }
2396
2397 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
2398 struct drm_display_mode **modes,
2399 struct drm_fb_offset *offsets,
2400 bool *enabled, int width, int height)
2401 {
2402 struct drm_fb_helper_connector *fb_helper_conn;
2403 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
2404 u64 conn_configured = 0;
2405 int tile_pass = 0;
2406 int i;
2407
2408 retry:
2409 drm_fb_helper_for_each_connector(fb_helper, i) {
2410 fb_helper_conn = fb_helper->connector_info[i];
2411
2412 if (conn_configured & BIT_ULL(i))
2413 continue;
2414
2415 if (enabled[i] == false) {
2416 conn_configured |= BIT_ULL(i);
2417 continue;
2418 }
2419
2420 /* first pass over all the untiled connectors */
2421 if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
2422 continue;
2423
2424 if (tile_pass == 1) {
2425 if (fb_helper_conn->connector->tile_h_loc != 0 ||
2426 fb_helper_conn->connector->tile_v_loc != 0)
2427 continue;
2428
2429 } else {
2430 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
2431 fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
2432 /* if this tile_pass doesn't cover any of the tiles - keep going */
2433 continue;
2434
2435 /*
2436 * find the tile offsets for this pass - need to find
2437 * all tiles left and above
2438 */
2439 drm_get_tile_offsets(fb_helper, modes, offsets,
2440 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
2441 }
2442 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
2443 fb_helper_conn->connector->base.id);
2444
2445 /* got for command line mode first */
2446 modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
2447 if (!modes[i]) {
2448 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
2449 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
2450 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
2451 }
2452 /* No preferred modes, pick one off the list */
2453 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
2454 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
2455 break;
2456 }
2457 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2458 "none");
2459 conn_configured |= BIT_ULL(i);
2460 }
2461
2462 if ((conn_configured & mask) != mask) {
2463 tile_pass++;
2464 goto retry;
2465 }
2466 return true;
2467 }
2468
2469 static bool connector_has_possible_crtc(struct drm_connector *connector,
2470 struct drm_crtc *crtc)
2471 {
2472 struct drm_encoder *encoder;
2473 int i;
2474
2475 drm_connector_for_each_possible_encoder(connector, encoder, i) {
2476 if (encoder->possible_crtcs & drm_crtc_mask(crtc))
2477 return true;
2478 }
2479
2480 return false;
2481 }
2482
2483 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2484 struct drm_fb_helper_crtc **best_crtcs,
2485 struct drm_display_mode **modes,
2486 int n, int width, int height)
2487 {
2488 int c, o;
2489 struct drm_connector *connector;
2490 int my_score, best_score, score;
2491 struct drm_fb_helper_crtc **crtcs, *crtc;
2492 struct drm_fb_helper_connector *fb_helper_conn;
2493
2494 if (n == fb_helper->connector_count)
2495 return 0;
2496
2497 fb_helper_conn = fb_helper->connector_info[n];
2498 connector = fb_helper_conn->connector;
2499
2500 best_crtcs[n] = NULL;
2501 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
2502 if (modes[n] == NULL)
2503 return best_score;
2504
2505 crtcs = kcalloc(fb_helper->connector_count,
2506 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2507 if (!crtcs)
2508 return best_score;
2509
2510 my_score = 1;
2511 if (connector->status == connector_status_connected)
2512 my_score++;
2513 if (drm_has_cmdline_mode(fb_helper_conn))
2514 my_score++;
2515 if (drm_has_preferred_mode(fb_helper_conn, width, height))
2516 my_score++;
2517
2518 /*
2519 * select a crtc for this connector and then attempt to configure
2520 * remaining connectors
2521 */
2522 for (c = 0; c < fb_helper->crtc_count; c++) {
2523 crtc = &fb_helper->crtc_info[c];
2524
2525 if (!connector_has_possible_crtc(connector,
2526 crtc->mode_set.crtc))
2527 continue;
2528
2529 for (o = 0; o < n; o++)
2530 if (best_crtcs[o] == crtc)
2531 break;
2532
2533 if (o < n) {
2534 /* ignore cloning unless only a single crtc */
2535 if (fb_helper->crtc_count > 1)
2536 continue;
2537
2538 if (!drm_mode_equal(modes[o], modes[n]))
2539 continue;
2540 }
2541
2542 crtcs[n] = crtc;
2543 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2544 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
2545 width, height);
2546 if (score > best_score) {
2547 best_score = score;
2548 memcpy(best_crtcs, crtcs,
2549 fb_helper->connector_count *
2550 sizeof(struct drm_fb_helper_crtc *));
2551 }
2552 }
2553
2554 kfree(crtcs);
2555 return best_score;
2556 }
2557
2558 static struct drm_fb_helper_crtc *
2559 drm_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
2560 {
2561 int i;
2562
2563 for (i = 0; i < fb_helper->crtc_count; i++)
2564 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
2565 return &fb_helper->crtc_info[i];
2566
2567 return NULL;
2568 }
2569
2570 /* Try to read the BIOS display configuration and use it for the initial config */
2571 static bool drm_fb_helper_firmware_config(struct drm_fb_helper *fb_helper,
2572 struct drm_fb_helper_crtc **crtcs,
2573 struct drm_display_mode **modes,
2574 struct drm_fb_offset *offsets,
2575 bool *enabled, int width, int height)
2576 {
2577 struct drm_device *dev = fb_helper->dev;
2578 unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
2579 unsigned long conn_configured, conn_seq, mask;
2580 int i, j;
2581 bool *save_enabled;
2582 bool fallback = true, ret = true;
2583 int num_connectors_enabled = 0;
2584 int num_connectors_detected = 0;
2585 struct drm_modeset_acquire_ctx ctx;
2586
2587 if (!drm_drv_uses_atomic_modeset(dev))
2588 return false;
2589
2590 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
2591 if (!save_enabled)
2592 return false;
2593
2594 drm_modeset_acquire_init(&ctx, 0);
2595
2596 while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
2597 drm_modeset_backoff(&ctx);
2598
2599 memcpy(save_enabled, enabled, count);
2600 mask = GENMASK(count - 1, 0);
2601 conn_configured = 0;
2602 retry:
2603 conn_seq = conn_configured;
2604 for (i = 0; i < count; i++) {
2605 struct drm_fb_helper_connector *fb_conn;
2606 struct drm_connector *connector;
2607 struct drm_encoder *encoder;
2608 struct drm_fb_helper_crtc *new_crtc;
2609
2610 fb_conn = fb_helper->connector_info[i];
2611 connector = fb_conn->connector;
2612
2613 if (conn_configured & BIT(i))
2614 continue;
2615
2616 if (conn_seq == 0 && !connector->has_tile)
2617 continue;
2618
2619 if (connector->status == connector_status_connected)
2620 num_connectors_detected++;
2621
2622 if (!enabled[i]) {
2623 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
2624 connector->name);
2625 conn_configured |= BIT(i);
2626 continue;
2627 }
2628
2629 if (connector->force == DRM_FORCE_OFF) {
2630 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
2631 connector->name);
2632 enabled[i] = false;
2633 continue;
2634 }
2635
2636 encoder = connector->state->best_encoder;
2637 if (!encoder || WARN_ON(!connector->state->crtc)) {
2638 if (connector->force > DRM_FORCE_OFF)
2639 goto bail;
2640
2641 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
2642 connector->name);
2643 enabled[i] = false;
2644 conn_configured |= BIT(i);
2645 continue;
2646 }
2647
2648 num_connectors_enabled++;
2649
2650 new_crtc = drm_fb_helper_crtc(fb_helper, connector->state->crtc);
2651
2652 /*
2653 * Make sure we're not trying to drive multiple connectors
2654 * with a single CRTC, since our cloning support may not
2655 * match the BIOS.
2656 */
2657 for (j = 0; j < count; j++) {
2658 if (crtcs[j] == new_crtc) {
2659 DRM_DEBUG_KMS("fallback: cloned configuration\n");
2660 goto bail;
2661 }
2662 }
2663
2664 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
2665 connector->name);
2666
2667 /* go for command line mode first */
2668 modes[i] = drm_pick_cmdline_mode(fb_conn);
2669
2670 /* try for preferred next */
2671 if (!modes[i]) {
2672 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
2673 connector->name, connector->has_tile);
2674 modes[i] = drm_has_preferred_mode(fb_conn, width,
2675 height);
2676 }
2677
2678 /* No preferred mode marked by the EDID? Are there any modes? */
2679 if (!modes[i] && !list_empty(&connector->modes)) {
2680 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
2681 connector->name);
2682 modes[i] = list_first_entry(&connector->modes,
2683 struct drm_display_mode,
2684 head);
2685 }
2686
2687 /* last resort: use current mode */
2688 if (!modes[i]) {
2689 /*
2690 * IMPORTANT: We want to use the adjusted mode (i.e.
2691 * after the panel fitter upscaling) as the initial
2692 * config, not the input mode, which is what crtc->mode
2693 * usually contains. But since our current
2694 * code puts a mode derived from the post-pfit timings
2695 * into crtc->mode this works out correctly.
2696 *
2697 * This is crtc->mode and not crtc->state->mode for the
2698 * fastboot check to work correctly.
2699 */
2700 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
2701 connector->name);
2702 modes[i] = &connector->state->crtc->mode;
2703 }
2704 crtcs[i] = new_crtc;
2705
2706 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
2707 connector->name,
2708 connector->state->crtc->base.id,
2709 connector->state->crtc->name,
2710 modes[i]->hdisplay, modes[i]->vdisplay,
2711 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "");
2712
2713 fallback = false;
2714 conn_configured |= BIT(i);
2715 }
2716
2717 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
2718 goto retry;
2719
2720 /*
2721 * If the BIOS didn't enable everything it could, fall back to have the
2722 * same user experiencing of lighting up as much as possible like the
2723 * fbdev helper library.
2724 */
2725 if (num_connectors_enabled != num_connectors_detected &&
2726 num_connectors_enabled < dev->mode_config.num_crtc) {
2727 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
2728 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
2729 num_connectors_detected);
2730 fallback = true;
2731 }
2732
2733 if (fallback) {
2734 bail:
2735 DRM_DEBUG_KMS("Not using firmware configuration\n");
2736 memcpy(enabled, save_enabled, count);
2737 ret = false;
2738 }
2739
2740 drm_modeset_drop_locks(&ctx);
2741 drm_modeset_acquire_fini(&ctx);
2742
2743 kfree(save_enabled);
2744 return ret;
2745 }
2746
2747 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
2748 u32 width, u32 height)
2749 {
2750 struct drm_device *dev = fb_helper->dev;
2751 struct drm_fb_helper_crtc **crtcs;
2752 struct drm_display_mode **modes;
2753 struct drm_fb_offset *offsets;
2754 bool *enabled;
2755 int i;
2756
2757 DRM_DEBUG_KMS("\n");
2758 /* prevent concurrent modification of connector_count by hotplug */
2759 lockdep_assert_held(&fb_helper->lock);
2760
2761 crtcs = kcalloc(fb_helper->connector_count,
2762 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2763 modes = kcalloc(fb_helper->connector_count,
2764 sizeof(struct drm_display_mode *), GFP_KERNEL);
2765 offsets = kcalloc(fb_helper->connector_count,
2766 sizeof(struct drm_fb_offset), GFP_KERNEL);
2767 enabled = kcalloc(fb_helper->connector_count,
2768 sizeof(bool), GFP_KERNEL);
2769 if (!crtcs || !modes || !enabled || !offsets) {
2770 DRM_ERROR("Memory allocation failed\n");
2771 goto out;
2772 }
2773
2774 mutex_lock(&fb_helper->dev->mode_config.mutex);
2775 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
2776 DRM_DEBUG_KMS("No connectors reported connected with modes\n");
2777 drm_enable_connectors(fb_helper, enabled);
2778
2779 if (!drm_fb_helper_firmware_config(fb_helper, crtcs, modes, offsets,
2780 enabled, width, height)) {
2781 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2782 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2783 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
2784
2785 if (!drm_target_cloned(fb_helper, modes, offsets,
2786 enabled, width, height) &&
2787 !drm_target_preferred(fb_helper, modes, offsets,
2788 enabled, width, height))
2789 DRM_ERROR("Unable to find initial modes\n");
2790
2791 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2792 width, height);
2793
2794 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
2795 }
2796 mutex_unlock(&fb_helper->dev->mode_config.mutex);
2797
2798 /* need to set the modesets up here for use later */
2799 /* fill out the connector<->crtc mappings into the modesets */
2800 for (i = 0; i < fb_helper->crtc_count; i++)
2801 drm_fb_helper_modeset_release(fb_helper,
2802 &fb_helper->crtc_info[i].mode_set);
2803
2804 drm_fb_helper_for_each_connector(fb_helper, i) {
2805 struct drm_display_mode *mode = modes[i];
2806 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
2807 struct drm_fb_offset *offset = &offsets[i];
2808
2809 if (mode && fb_crtc) {
2810 struct drm_mode_set *modeset = &fb_crtc->mode_set;
2811 struct drm_connector *connector =
2812 fb_helper->connector_info[i]->connector;
2813
2814 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2815 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
2816
2817 modeset->mode = drm_mode_duplicate(dev, mode);
2818 drm_connector_get(connector);
2819 modeset->connectors[modeset->num_connectors++] = connector;
2820 modeset->x = offset->x;
2821 modeset->y = offset->y;
2822 }
2823 }
2824 out:
2825 kfree(crtcs);
2826 kfree(modes);
2827 kfree(offsets);
2828 kfree(enabled);
2829 }
2830
2831 /*
2832 * This is a continuation of drm_setup_crtcs() that sets up anything related
2833 * to the framebuffer. During initialization, drm_setup_crtcs() is called before
2834 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
2835 * So, any setup that touches those fields needs to be done here instead of in
2836 * drm_setup_crtcs().
2837 */
2838 static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
2839 {
2840 struct fb_info *info = fb_helper->fbdev;
2841 unsigned int rotation, sw_rotations = 0;
2842 int i;
2843
2844 for (i = 0; i < fb_helper->crtc_count; i++) {
2845 struct drm_mode_set *modeset = &fb_helper->crtc_info[i].mode_set;
2846
2847 if (!modeset->num_connectors)
2848 continue;
2849
2850 modeset->fb = fb_helper->fb;
2851
2852 if (drm_fb_helper_panel_rotation(modeset, &rotation))
2853 /* Rotating in hardware, fbcon should not rotate */
2854 sw_rotations |= DRM_MODE_ROTATE_0;
2855 else
2856 sw_rotations |= rotation;
2857 }
2858
2859 mutex_lock(&fb_helper->dev->mode_config.mutex);
2860 drm_fb_helper_for_each_connector(fb_helper, i) {
2861 struct drm_connector *connector =
2862 fb_helper->connector_info[i]->connector;
2863
2864 /* use first connected connector for the physical dimensions */
2865 if (connector->status == connector_status_connected) {
2866 info->var.width = connector->display_info.width_mm;
2867 info->var.height = connector->display_info.height_mm;
2868 break;
2869 }
2870 }
2871 mutex_unlock(&fb_helper->dev->mode_config.mutex);
2872
2873 switch (sw_rotations) {
2874 case DRM_MODE_ROTATE_0:
2875 info->fbcon_rotate_hint = FB_ROTATE_UR;
2876 break;
2877 case DRM_MODE_ROTATE_90:
2878 info->fbcon_rotate_hint = FB_ROTATE_CCW;
2879 break;
2880 case DRM_MODE_ROTATE_180:
2881 info->fbcon_rotate_hint = FB_ROTATE_UD;
2882 break;
2883 case DRM_MODE_ROTATE_270:
2884 info->fbcon_rotate_hint = FB_ROTATE_CW;
2885 break;
2886 default:
2887 /*
2888 * Multiple bits are set / multiple rotations requested
2889 * fbcon cannot handle separate rotation settings per
2890 * output, so fallback to unrotated.
2891 */
2892 info->fbcon_rotate_hint = FB_ROTATE_UR;
2893 }
2894 }
2895
2896 /* Note: Drops fb_helper->lock before returning. */
2897 static int
2898 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
2899 int bpp_sel)
2900 {
2901 struct drm_device *dev = fb_helper->dev;
2902 struct fb_info *info;
2903 unsigned int width, height;
2904 int ret;
2905
2906 width = dev->mode_config.max_width;
2907 height = dev->mode_config.max_height;
2908
2909 drm_setup_crtcs(fb_helper, width, height);
2910 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2911 if (ret < 0) {
2912 if (ret == -EAGAIN) {
2913 fb_helper->preferred_bpp = bpp_sel;
2914 fb_helper->deferred_setup = true;
2915 ret = 0;
2916 }
2917 mutex_unlock(&fb_helper->lock);
2918
2919 return ret;
2920 }
2921 drm_setup_crtcs_fb(fb_helper);
2922
2923 fb_helper->deferred_setup = false;
2924
2925 info = fb_helper->fbdev;
2926 info->var.pixclock = 0;
2927 /* Shamelessly allow physical address leaking to userspace */
2928 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
2929 if (!drm_leak_fbdev_smem)
2930 #endif
2931 /* don't leak any physical addresses to userspace */
2932 info->flags |= FBINFO_HIDE_SMEM_START;
2933
2934 /* Need to drop locks to avoid recursive deadlock in
2935 * register_framebuffer. This is ok because the only thing left to do is
2936 * register the fbdev emulation instance in kernel_fb_helper_list. */
2937 mutex_unlock(&fb_helper->lock);
2938
2939 ret = register_framebuffer(info);
2940 if (ret < 0)
2941 return ret;
2942
2943 dev_info(dev->dev, "fb%d: %s frame buffer device\n",
2944 info->node, info->fix.id);
2945
2946 mutex_lock(&kernel_fb_helper_lock);
2947 if (list_empty(&kernel_fb_helper_list))
2948 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
2949
2950 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
2951 mutex_unlock(&kernel_fb_helper_lock);
2952
2953 return 0;
2954 }
2955
2956 /**
2957 * drm_fb_helper_initial_config - setup a sane initial connector configuration
2958 * @fb_helper: fb_helper device struct
2959 * @bpp_sel: bpp value to use for the framebuffer configuration
2960 *
2961 * Scans the CRTCs and connectors and tries to put together an initial setup.
2962 * At the moment, this is a cloned configuration across all heads with
2963 * a new framebuffer object as the backing store.
2964 *
2965 * Note that this also registers the fbdev and so allows userspace to call into
2966 * the driver through the fbdev interfaces.
2967 *
2968 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
2969 * to let the driver allocate and initialize the fbdev info structure and the
2970 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided
2971 * as a helper to setup simple default values for the fbdev info structure.
2972 *
2973 * HANG DEBUGGING:
2974 *
2975 * When you have fbcon support built-in or already loaded, this function will do
2976 * a full modeset to setup the fbdev console. Due to locking misdesign in the
2977 * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2978 * console_lock. Until console_unlock is called no dmesg lines will be sent out
2979 * to consoles, not even serial console. This means when your driver crashes,
2980 * you will see absolutely nothing else but a system stuck in this function,
2981 * with no further output. Any kind of printk() you place within your own driver
2982 * or in the drm core modeset code will also never show up.
2983 *
2984 * Standard debug practice is to run the fbcon setup without taking the
2985 * console_lock as a hack, to be able to see backtraces and crashes on the
2986 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2987 * cmdline option.
2988 *
2989 * The other option is to just disable fbdev emulation since very likely the
2990 * first modeset from userspace will crash in the same way, and is even easier
2991 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
2992 * kernel cmdline option.
2993 *
2994 * RETURNS:
2995 * Zero if everything went ok, nonzero otherwise.
2996 */
2997 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
2998 {
2999 int ret;
3000
3001 if (!drm_fbdev_emulation)
3002 return 0;
3003
3004 mutex_lock(&fb_helper->lock);
3005 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
3006
3007 return ret;
3008 }
3009 EXPORT_SYMBOL(drm_fb_helper_initial_config);
3010
3011 /**
3012 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
3013 * probing all the outputs attached to the fb
3014 * @fb_helper: driver-allocated fbdev helper, can be NULL
3015 *
3016 * Scan the connectors attached to the fb_helper and try to put together a
3017 * setup after notification of a change in output configuration.
3018 *
3019 * Called at runtime, takes the mode config locks to be able to check/change the
3020 * modeset configuration. Must be run from process context (which usually means
3021 * either the output polling work or a work item launched from the driver's
3022 * hotplug interrupt).
3023 *
3024 * Note that drivers may call this even before calling
3025 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
3026 * for a race-free fbcon setup and will make sure that the fbdev emulation will
3027 * not miss any hotplug events.
3028 *
3029 * RETURNS:
3030 * 0 on success and a non-zero error code otherwise.
3031 */
3032 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
3033 {
3034 int err = 0;
3035
3036 if (!drm_fbdev_emulation || !fb_helper)
3037 return 0;
3038
3039 mutex_lock(&fb_helper->lock);
3040 if (fb_helper->deferred_setup) {
3041 err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
3042 fb_helper->preferred_bpp);
3043 return err;
3044 }
3045
3046 if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) {
3047 fb_helper->delayed_hotplug = true;
3048 mutex_unlock(&fb_helper->lock);
3049 return err;
3050 }
3051
3052 drm_master_internal_release(fb_helper->dev);
3053
3054 DRM_DEBUG_KMS("\n");
3055
3056 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
3057 drm_setup_crtcs_fb(fb_helper);
3058 mutex_unlock(&fb_helper->lock);
3059
3060 drm_fb_helper_set_par(fb_helper->fbdev);
3061
3062 return 0;
3063 }
3064 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
3065
3066 /**
3067 * drm_fb_helper_fbdev_setup() - Setup fbdev emulation
3068 * @dev: DRM device
3069 * @fb_helper: fbdev helper structure to set up
3070 * @funcs: fbdev helper functions
3071 * @preferred_bpp: Preferred bits per pixel for the device.
3072 * @dev->mode_config.preferred_depth is used if this is zero.
3073 * @max_conn_count: Maximum number of connectors.
3074 * @dev->mode_config.num_connector is used if this is zero.
3075 *
3076 * This function sets up fbdev emulation and registers fbdev for access by
3077 * userspace. If all connectors are disconnected, setup is deferred to the next
3078 * time drm_fb_helper_hotplug_event() is called.
3079 * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback
3080 * function.
3081 *
3082 * Use drm_fb_helper_fbdev_teardown() to destroy the fbdev.
3083 *
3084 * See also: drm_fb_helper_initial_config(), drm_fbdev_generic_setup().
3085 *
3086 * Returns:
3087 * Zero on success or negative error code on failure.
3088 */
3089 int drm_fb_helper_fbdev_setup(struct drm_device *dev,
3090 struct drm_fb_helper *fb_helper,
3091 const struct drm_fb_helper_funcs *funcs,
3092 unsigned int preferred_bpp,
3093 unsigned int max_conn_count)
3094 {
3095 int ret;
3096
3097 if (!preferred_bpp)
3098 preferred_bpp = dev->mode_config.preferred_depth;
3099 if (!preferred_bpp)
3100 preferred_bpp = 32;
3101
3102 if (!max_conn_count)
3103 max_conn_count = dev->mode_config.num_connector;
3104 if (!max_conn_count) {
3105 DRM_DEV_ERROR(dev->dev, "fbdev: No connectors\n");
3106 return -EINVAL;
3107 }
3108
3109 drm_fb_helper_prepare(dev, fb_helper, funcs);
3110
3111 ret = drm_fb_helper_init(dev, fb_helper, max_conn_count);
3112 if (ret < 0) {
3113 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to initialize (ret=%d)\n", ret);
3114 return ret;
3115 }
3116
3117 ret = drm_fb_helper_single_add_all_connectors(fb_helper);
3118 if (ret < 0) {
3119 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to add connectors (ret=%d)\n", ret);
3120 goto err_drm_fb_helper_fini;
3121 }
3122
3123 if (!drm_drv_uses_atomic_modeset(dev))
3124 drm_helper_disable_unused_functions(dev);
3125
3126 ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp);
3127 if (ret < 0) {
3128 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to set configuration (ret=%d)\n", ret);
3129 goto err_drm_fb_helper_fini;
3130 }
3131
3132 return 0;
3133
3134 err_drm_fb_helper_fini:
3135 drm_fb_helper_fbdev_teardown(dev);
3136
3137 return ret;
3138 }
3139 EXPORT_SYMBOL(drm_fb_helper_fbdev_setup);
3140
3141 /**
3142 * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation
3143 * @dev: DRM device
3144 *
3145 * This function unregisters fbdev if not already done and cleans up the
3146 * associated resources including the &drm_framebuffer.
3147 * The driver is responsible for freeing the &drm_fb_helper structure which is
3148 * stored in &drm_device->fb_helper. Do note that this pointer has been cleared
3149 * when this function returns.
3150 *
3151 * In order to support device removal/unplug while file handles are still open,
3152 * drm_fb_helper_unregister_fbi() should be called on device removal and
3153 * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when
3154 * file handles are closed.
3155 */
3156 void drm_fb_helper_fbdev_teardown(struct drm_device *dev)
3157 {
3158 struct drm_fb_helper *fb_helper = dev->fb_helper;
3159 struct fb_ops *fbops = NULL;
3160
3161 if (!fb_helper)
3162 return;
3163
3164 /* Unregister if it hasn't been done already */
3165 if (fb_helper->fbdev && fb_helper->fbdev->dev)
3166 drm_fb_helper_unregister_fbi(fb_helper);
3167
3168 if (fb_helper->fbdev && fb_helper->fbdev->fbdefio) {
3169 fb_deferred_io_cleanup(fb_helper->fbdev);
3170 kfree(fb_helper->fbdev->fbdefio);
3171 fbops = fb_helper->fbdev->fbops;
3172 }
3173
3174 drm_fb_helper_fini(fb_helper);
3175 kfree(fbops);
3176
3177 if (fb_helper->fb)
3178 drm_framebuffer_remove(fb_helper->fb);
3179 }
3180 EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown);
3181
3182 /**
3183 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
3184 * @dev: DRM device
3185 *
3186 * This function can be used as the &drm_driver->lastclose callback for drivers
3187 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
3188 */
3189 void drm_fb_helper_lastclose(struct drm_device *dev)
3190 {
3191 drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
3192 }
3193 EXPORT_SYMBOL(drm_fb_helper_lastclose);
3194
3195 /**
3196 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
3197 * helper for fbdev emulation
3198 * @dev: DRM device
3199 *
3200 * This function can be used as the
3201 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
3202 * need to call drm_fb_helper_hotplug_event().
3203 */
3204 void drm_fb_helper_output_poll_changed(struct drm_device *dev)
3205 {
3206 drm_fb_helper_hotplug_event(dev->fb_helper);
3207 }
3208 EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
3209
3210 /* @user: 1=userspace, 0=fbcon */
3211 static int drm_fbdev_fb_open(struct fb_info *info, int user)
3212 {
3213 struct drm_fb_helper *fb_helper = info->par;
3214
3215 /* No need to take a ref for fbcon because it unbinds on unregister */
3216 if (user && !try_module_get(fb_helper->dev->driver->fops->owner))
3217 return -ENODEV;
3218
3219 return 0;
3220 }
3221
3222 static int drm_fbdev_fb_release(struct fb_info *info, int user)
3223 {
3224 struct drm_fb_helper *fb_helper = info->par;
3225
3226 if (user)
3227 module_put(fb_helper->dev->driver->fops->owner);
3228
3229 return 0;
3230 }
3231
3232 static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper)
3233 {
3234 struct fb_info *fbi = fb_helper->fbdev;
3235 struct fb_ops *fbops = NULL;
3236 void *shadow = NULL;
3237
3238 if (!fb_helper->dev)
3239 return;
3240
3241 if (fbi && fbi->fbdefio) {
3242 fb_deferred_io_cleanup(fbi);
3243 shadow = fbi->screen_buffer;
3244 fbops = fbi->fbops;
3245 }
3246
3247 drm_fb_helper_fini(fb_helper);
3248
3249 if (shadow) {
3250 vfree(shadow);
3251 kfree(fbops);
3252 }
3253
3254 drm_client_framebuffer_delete(fb_helper->buffer);
3255 }
3256
3257 static void drm_fbdev_release(struct drm_fb_helper *fb_helper)
3258 {
3259 drm_fbdev_cleanup(fb_helper);
3260 drm_client_release(&fb_helper->client);
3261 kfree(fb_helper);
3262 }
3263
3264 /*
3265 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
3266 * unregister_framebuffer() or fb_release().
3267 */
3268 static void drm_fbdev_fb_destroy(struct fb_info *info)
3269 {
3270 drm_fbdev_release(info->par);
3271 }
3272
3273 static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
3274 {
3275 struct drm_fb_helper *fb_helper = info->par;
3276
3277 if (fb_helper->dev->driver->gem_prime_mmap)
3278 return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma);
3279 else
3280 return -ENODEV;
3281 }
3282
3283 static struct fb_ops drm_fbdev_fb_ops = {
3284 .owner = THIS_MODULE,
3285 DRM_FB_HELPER_DEFAULT_OPS,
3286 .fb_open = drm_fbdev_fb_open,
3287 .fb_release = drm_fbdev_fb_release,
3288 .fb_destroy = drm_fbdev_fb_destroy,
3289 .fb_mmap = drm_fbdev_fb_mmap,
3290 .fb_read = drm_fb_helper_sys_read,
3291 .fb_write = drm_fb_helper_sys_write,
3292 .fb_fillrect = drm_fb_helper_sys_fillrect,
3293 .fb_copyarea = drm_fb_helper_sys_copyarea,
3294 .fb_imageblit = drm_fb_helper_sys_imageblit,
3295 };
3296
3297 static struct fb_deferred_io drm_fbdev_defio = {
3298 .delay = HZ / 20,
3299 .deferred_io = drm_fb_helper_deferred_io,
3300 };
3301
3302 /**
3303 * drm_fb_helper_generic_probe - Generic fbdev emulation probe helper
3304 * @fb_helper: fbdev helper structure
3305 * @sizes: describes fbdev size and scanout surface size
3306 *
3307 * This function uses the client API to create a framebuffer backed by a dumb buffer.
3308 *
3309 * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect,
3310 * fb_copyarea, fb_imageblit.
3311 *
3312 * Returns:
3313 * Zero on success or negative error code on failure.
3314 */
3315 int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
3316 struct drm_fb_helper_surface_size *sizes)
3317 {
3318 struct drm_client_dev *client = &fb_helper->client;
3319 struct drm_client_buffer *buffer;
3320 struct drm_framebuffer *fb;
3321 struct fb_info *fbi;
3322 u32 format;
3323
3324 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
3325 sizes->surface_width, sizes->surface_height,
3326 sizes->surface_bpp);
3327
3328 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
3329 buffer = drm_client_framebuffer_create(client, sizes->surface_width,
3330 sizes->surface_height, format);
3331 if (IS_ERR(buffer))
3332 return PTR_ERR(buffer);
3333
3334 fb_helper->buffer = buffer;
3335 fb_helper->fb = buffer->fb;
3336 fb = buffer->fb;
3337
3338 fbi = drm_fb_helper_alloc_fbi(fb_helper);
3339 if (IS_ERR(fbi))
3340 return PTR_ERR(fbi);
3341
3342 fbi->fbops = &drm_fbdev_fb_ops;
3343 fbi->screen_size = fb->height * fb->pitches[0];
3344 fbi->fix.smem_len = fbi->screen_size;
3345 fbi->screen_buffer = buffer->vaddr;
3346 /* Shamelessly leak the physical address to user-space */
3347 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
3348 if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0)
3349 fbi->fix.smem_start =
3350 page_to_phys(virt_to_page(fbi->screen_buffer));
3351 #endif
3352 drm_fb_helper_fill_info(fbi, fb_helper, sizes);
3353
3354 if (fb->funcs->dirty) {
3355 struct fb_ops *fbops;
3356 void *shadow;
3357
3358 /*
3359 * fb_deferred_io_cleanup() clears &fbops->fb_mmap so a per
3360 * instance version is necessary.
3361 */
3362 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
3363 shadow = vzalloc(fbi->screen_size);
3364 if (!fbops || !shadow) {
3365 kfree(fbops);
3366 vfree(shadow);
3367 return -ENOMEM;
3368 }
3369
3370 *fbops = *fbi->fbops;
3371 fbi->fbops = fbops;
3372 fbi->screen_buffer = shadow;
3373 fbi->fbdefio = &drm_fbdev_defio;
3374
3375 fb_deferred_io_init(fbi);
3376 }
3377
3378 return 0;
3379 }
3380 EXPORT_SYMBOL(drm_fb_helper_generic_probe);
3381
3382 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = {
3383 .fb_probe = drm_fb_helper_generic_probe,
3384 };
3385
3386 static void drm_fbdev_client_unregister(struct drm_client_dev *client)
3387 {
3388 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
3389
3390 if (fb_helper->fbdev)
3391 /* drm_fbdev_fb_destroy() takes care of cleanup */
3392 drm_fb_helper_unregister_fbi(fb_helper);
3393 else
3394 drm_fbdev_release(fb_helper);
3395 }
3396
3397 static int drm_fbdev_client_restore(struct drm_client_dev *client)
3398 {
3399 drm_fb_helper_lastclose(client->dev);
3400
3401 return 0;
3402 }
3403
3404 static int drm_fbdev_client_hotplug(struct drm_client_dev *client)
3405 {
3406 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
3407 struct drm_device *dev = client->dev;
3408 int ret;
3409
3410 /* Setup is not retried if it has failed */
3411 if (!fb_helper->dev && fb_helper->funcs)
3412 return 0;
3413
3414 if (dev->fb_helper)
3415 return drm_fb_helper_hotplug_event(dev->fb_helper);
3416
3417 if (!dev->mode_config.num_connector) {
3418 DRM_DEV_DEBUG(dev->dev, "No connectors found, will not create framebuffer!\n");
3419 return 0;
3420 }
3421
3422 drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs);
3423
3424 ret = drm_fb_helper_init(dev, fb_helper, dev->mode_config.num_connector);
3425 if (ret)
3426 goto err;
3427
3428 ret = drm_fb_helper_single_add_all_connectors(fb_helper);
3429 if (ret)
3430 goto err_cleanup;
3431
3432 if (!drm_drv_uses_atomic_modeset(dev))
3433 drm_helper_disable_unused_functions(dev);
3434
3435 ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp);
3436 if (ret)
3437 goto err_cleanup;
3438
3439 return 0;
3440
3441 err_cleanup:
3442 drm_fbdev_cleanup(fb_helper);
3443 err:
3444 fb_helper->dev = NULL;
3445 fb_helper->fbdev = NULL;
3446
3447 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret);
3448
3449 return ret;
3450 }
3451
3452 static const struct drm_client_funcs drm_fbdev_client_funcs = {
3453 .owner = THIS_MODULE,
3454 .unregister = drm_fbdev_client_unregister,
3455 .restore = drm_fbdev_client_restore,
3456 .hotplug = drm_fbdev_client_hotplug,
3457 };
3458
3459 /**
3460 * drm_fbdev_generic_setup() - Setup generic fbdev emulation
3461 * @dev: DRM device
3462 * @preferred_bpp: Preferred bits per pixel for the device.
3463 * @dev->mode_config.preferred_depth is used if this is zero.
3464 *
3465 * This function sets up generic fbdev emulation for drivers that supports
3466 * dumb buffers with a virtual address and that can be mmap'ed. If the driver
3467 * does not support these functions, it could use drm_fb_helper_fbdev_setup().
3468 *
3469 * Restore, hotplug events and teardown are all taken care of. Drivers that do
3470 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
3471 * Simple drivers might use drm_mode_config_helper_suspend().
3472 *
3473 * Drivers that set the dirty callback on their framebuffer will get a shadow
3474 * fbdev buffer that is blitted onto the real buffer. This is done in order to
3475 * make deferred I/O work with all kinds of buffers.
3476 *
3477 * This function is safe to call even when there are no connectors present.
3478 * Setup will be retried on the next hotplug event.
3479 *
3480 * The fbdev is destroyed by drm_dev_unregister().
3481 *
3482 * Returns:
3483 * Zero on success or negative error code on failure.
3484 */
3485 int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
3486 {
3487 struct drm_fb_helper *fb_helper;
3488 int ret;
3489
3490 WARN(dev->fb_helper, "fb_helper is already set!\n");
3491
3492 if (!drm_fbdev_emulation)
3493 return 0;
3494
3495 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
3496 if (!fb_helper)
3497 return -ENOMEM;
3498
3499 ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs);
3500 if (ret) {
3501 kfree(fb_helper);
3502 DRM_DEV_ERROR(dev->dev, "Failed to register client: %d\n", ret);
3503 return ret;
3504 }
3505
3506 if (!preferred_bpp)
3507 preferred_bpp = dev->mode_config.preferred_depth;
3508 if (!preferred_bpp)
3509 preferred_bpp = 32;
3510 fb_helper->preferred_bpp = preferred_bpp;
3511
3512 ret = drm_fbdev_client_hotplug(&fb_helper->client);
3513 if (ret)
3514 DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret);
3515
3516 drm_client_register(&fb_helper->client);
3517
3518 return 0;
3519 }
3520 EXPORT_SYMBOL(drm_fbdev_generic_setup);
3521
3522 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
3523 * but the module doesn't depend on any fb console symbols. At least
3524 * attempt to load fbcon to avoid leaving the system without a usable console.
3525 */
3526 int __init drm_fb_helper_modinit(void)
3527 {
3528 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
3529 const char name[] = "fbcon";
3530 struct module *fbcon;
3531
3532 mutex_lock(&module_mutex);
3533 fbcon = find_module(name);
3534 mutex_unlock(&module_mutex);
3535
3536 if (!fbcon)
3537 request_module_nowait(name);
3538 #endif
3539 return 0;
3540 }
3541 EXPORT_SYMBOL(drm_fb_helper_modinit);