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