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