xref: /linux/drivers/gpu/drm/drm_fb_helper.c (revision b9a14d54ab2bf0c09409f373a2120de65046178a)
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/pci.h>
34 #include <linux/sysrq.h>
35 #include <linux/vga_switcheroo.h>
36 
37 #include <drm/drm_atomic.h>
38 #include <drm/drm_drv.h>
39 #include <drm/drm_fb_helper.h>
40 #include <drm/drm_fourcc.h>
41 #include <drm/drm_framebuffer.h>
42 #include <drm/drm_modeset_helper_vtables.h>
43 #include <drm/drm_print.h>
44 #include <drm/drm_vblank.h>
45 
46 #include "drm_internal.h"
47 #include "drm_crtc_internal.h"
48 
49 static bool drm_fbdev_emulation = true;
50 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
51 MODULE_PARM_DESC(fbdev_emulation,
52 		 "Enable legacy fbdev emulation [default=true]");
53 
54 static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
55 module_param(drm_fbdev_overalloc, int, 0444);
56 MODULE_PARM_DESC(drm_fbdev_overalloc,
57 		 "Overallocation of the fbdev buffer (%) [default="
58 		 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
59 
60 /*
61  * In order to keep user-space compatibility, we want in certain use-cases
62  * to keep leaking the fbdev physical address to the user-space program
63  * handling the fbdev buffer.
64  *
65  * This is a bad habit, essentially kept to support closed-source OpenGL
66  * drivers that should really be moved into open-source upstream projects
67  * instead of using legacy physical addresses in user space to communicate
68  * with other out-of-tree kernel modules.
69  *
70  * This module_param *should* be removed as soon as possible and be
71  * considered as a broken and legacy behaviour from a modern fbdev device.
72  */
73 static bool drm_leak_fbdev_smem;
74 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
75 module_param_unsafe(drm_leak_fbdev_smem, bool, 0600);
76 MODULE_PARM_DESC(drm_leak_fbdev_smem,
77 		 "Allow unsafe leaking fbdev physical smem address [default=false]");
78 #endif
79 
80 static LIST_HEAD(kernel_fb_helper_list);
81 static DEFINE_MUTEX(kernel_fb_helper_lock);
82 
83 /**
84  * DOC: fbdev helpers
85  *
86  * The fb helper functions are useful to provide an fbdev on top of a drm kernel
87  * mode setting driver. They can be used mostly independently from the crtc
88  * helper functions used by many drivers to implement the kernel mode setting
89  * interfaces. Drivers that use one of the shared memory managers, TTM, SHMEM,
90  * DMA, should instead use the corresponding fbdev emulation.
91  *
92  * For suspend/resume consider using drm_mode_config_helper_suspend() and
93  * drm_mode_config_helper_resume() which takes care of fbdev as well.
94  *
95  * All other functions exported by the fb helper library can be used to
96  * implement the fbdev driver interface by the driver.
97  *
98  * It is possible, though perhaps somewhat tricky, to implement race-free
99  * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
100  * helper must be called first to initialize the minimum required to make
101  * hotplug detection work. Drivers also need to make sure to properly set up
102  * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
103  * it is safe to enable interrupts and start processing hotplug events. At the
104  * same time, drivers should initialize all modeset objects such as CRTCs,
105  * encoders and connectors. To finish up the fbdev helper initialization, the
106  * drm_fb_helper_init() function is called. To probe for all attached displays
107  * and set up an initial configuration using the detected hardware, drivers
108  * should call drm_fb_helper_initial_config().
109  *
110  * If &drm_framebuffer_funcs.dirty is set, the
111  * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
112  * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
113  * away. This worker then calls the dirty() function ensuring that it will
114  * always run in process context since the fb_*() function could be running in
115  * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
116  * callback it will also schedule dirty_work with the damage collected from the
117  * mmap page writes.
118  */
119 
120 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
121 {
122 	uint16_t *r_base, *g_base, *b_base;
123 
124 	if (crtc->funcs->gamma_set == NULL)
125 		return;
126 
127 	r_base = crtc->gamma_store;
128 	g_base = r_base + crtc->gamma_size;
129 	b_base = g_base + crtc->gamma_size;
130 
131 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
132 			       crtc->gamma_size, NULL);
133 }
134 
135 /**
136  * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
137  * @info: fbdev registered by the helper
138  */
139 int drm_fb_helper_debug_enter(struct fb_info *info)
140 {
141 	struct drm_fb_helper *helper = info->par;
142 	const struct drm_crtc_helper_funcs *funcs;
143 	struct drm_mode_set *mode_set;
144 
145 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
146 		mutex_lock(&helper->client.modeset_mutex);
147 		drm_client_for_each_modeset(mode_set, &helper->client) {
148 			if (!mode_set->crtc->enabled)
149 				continue;
150 
151 			funcs =	mode_set->crtc->helper_private;
152 			if (funcs->mode_set_base_atomic == NULL)
153 				continue;
154 
155 			if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
156 				continue;
157 
158 			funcs->mode_set_base_atomic(mode_set->crtc,
159 						    mode_set->fb,
160 						    mode_set->x,
161 						    mode_set->y,
162 						    ENTER_ATOMIC_MODE_SET);
163 		}
164 		mutex_unlock(&helper->client.modeset_mutex);
165 	}
166 
167 	return 0;
168 }
169 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
170 
171 /**
172  * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
173  * @info: fbdev registered by the helper
174  */
175 int drm_fb_helper_debug_leave(struct fb_info *info)
176 {
177 	struct drm_fb_helper *helper = info->par;
178 	struct drm_client_dev *client = &helper->client;
179 	struct drm_device *dev = helper->dev;
180 	struct drm_crtc *crtc;
181 	const struct drm_crtc_helper_funcs *funcs;
182 	struct drm_mode_set *mode_set;
183 	struct drm_framebuffer *fb;
184 
185 	mutex_lock(&client->modeset_mutex);
186 	drm_client_for_each_modeset(mode_set, client) {
187 		crtc = mode_set->crtc;
188 		if (drm_drv_uses_atomic_modeset(crtc->dev))
189 			continue;
190 
191 		funcs = crtc->helper_private;
192 		fb = crtc->primary->fb;
193 
194 		if (!crtc->enabled)
195 			continue;
196 
197 		if (!fb) {
198 			drm_err(dev, "no fb to restore?\n");
199 			continue;
200 		}
201 
202 		if (funcs->mode_set_base_atomic == NULL)
203 			continue;
204 
205 		drm_fb_helper_restore_lut_atomic(mode_set->crtc);
206 		funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
207 					    crtc->y, LEAVE_ATOMIC_MODE_SET);
208 	}
209 	mutex_unlock(&client->modeset_mutex);
210 
211 	return 0;
212 }
213 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
214 
215 static int
216 __drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper,
217 					    bool force)
218 {
219 	bool do_delayed;
220 	int ret;
221 
222 	if (!drm_fbdev_emulation || !fb_helper)
223 		return -ENODEV;
224 
225 	if (READ_ONCE(fb_helper->deferred_setup))
226 		return 0;
227 
228 	mutex_lock(&fb_helper->lock);
229 	if (force) {
230 		/*
231 		 * Yes this is the _locked version which expects the master lock
232 		 * to be held. But for forced restores we're intentionally
233 		 * racing here, see drm_fb_helper_set_par().
234 		 */
235 		ret = drm_client_modeset_commit_locked(&fb_helper->client);
236 	} else {
237 		ret = drm_client_modeset_commit(&fb_helper->client);
238 	}
239 
240 	do_delayed = fb_helper->delayed_hotplug;
241 	if (do_delayed)
242 		fb_helper->delayed_hotplug = false;
243 	mutex_unlock(&fb_helper->lock);
244 
245 	if (do_delayed)
246 		drm_fb_helper_hotplug_event(fb_helper);
247 
248 	return ret;
249 }
250 
251 /**
252  * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
253  * @fb_helper: driver-allocated fbdev helper, can be NULL
254  *
255  * This helper should be called from fbdev emulation's &drm_client_funcs.restore
256  * callback. It ensures that the user isn't greeted with a black screen when the
257  * userspace compositor releases the display device.
258  *
259  * Returns:
260  * 0 on success, or a negative errno code otherwise.
261  */
262 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
263 {
264 	return __drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, false);
265 }
266 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
267 
268 #ifdef CONFIG_MAGIC_SYSRQ
269 /* emergency restore, don't bother with error reporting */
270 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
271 {
272 	struct drm_fb_helper *helper;
273 
274 	mutex_lock(&kernel_fb_helper_lock);
275 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
276 		struct drm_device *dev = helper->dev;
277 
278 		if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
279 			continue;
280 
281 		mutex_lock(&helper->lock);
282 		drm_client_modeset_commit_locked(&helper->client);
283 		mutex_unlock(&helper->lock);
284 	}
285 	mutex_unlock(&kernel_fb_helper_lock);
286 }
287 
288 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
289 
290 static void drm_fb_helper_sysrq(u8 dummy1)
291 {
292 	schedule_work(&drm_fb_helper_restore_work);
293 }
294 
295 static const struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
296 	.handler = drm_fb_helper_sysrq,
297 	.help_msg = "force-fb(v)",
298 	.action_msg = "Restore framebuffer console",
299 };
300 #else
301 static const struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
302 #endif
303 
304 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
305 {
306 	struct drm_fb_helper *fb_helper = info->par;
307 
308 	mutex_lock(&fb_helper->lock);
309 	drm_client_modeset_dpms(&fb_helper->client, dpms_mode);
310 	mutex_unlock(&fb_helper->lock);
311 }
312 
313 /**
314  * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
315  * @blank: desired blanking state
316  * @info: fbdev registered by the helper
317  */
318 int drm_fb_helper_blank(int blank, struct fb_info *info)
319 {
320 	if (oops_in_progress)
321 		return -EBUSY;
322 
323 	switch (blank) {
324 	/* Display: On; HSync: On, VSync: On */
325 	case FB_BLANK_UNBLANK:
326 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
327 		break;
328 	/* Display: Off; HSync: On, VSync: On */
329 	case FB_BLANK_NORMAL:
330 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
331 		break;
332 	/* Display: Off; HSync: Off, VSync: On */
333 	case FB_BLANK_HSYNC_SUSPEND:
334 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
335 		break;
336 	/* Display: Off; HSync: On, VSync: Off */
337 	case FB_BLANK_VSYNC_SUSPEND:
338 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
339 		break;
340 	/* Display: Off; HSync: Off, VSync: Off */
341 	case FB_BLANK_POWERDOWN:
342 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
343 		break;
344 	}
345 	return 0;
346 }
347 EXPORT_SYMBOL(drm_fb_helper_blank);
348 
349 static void drm_fb_helper_resume_worker(struct work_struct *work)
350 {
351 	struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
352 						    resume_work);
353 
354 	console_lock();
355 	fb_set_suspend(helper->info, 0);
356 	console_unlock();
357 }
358 
359 static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper)
360 {
361 	struct drm_device *dev = helper->dev;
362 	struct drm_clip_rect *clip = &helper->damage_clip;
363 	struct drm_clip_rect clip_copy;
364 	unsigned long flags;
365 	int ret;
366 
367 	if (drm_WARN_ON_ONCE(dev, !helper->funcs->fb_dirty))
368 		return;
369 
370 	spin_lock_irqsave(&helper->damage_lock, flags);
371 	clip_copy = *clip;
372 	clip->x1 = clip->y1 = ~0;
373 	clip->x2 = clip->y2 = 0;
374 	spin_unlock_irqrestore(&helper->damage_lock, flags);
375 
376 	ret = helper->funcs->fb_dirty(helper, &clip_copy);
377 	if (ret)
378 		goto err;
379 
380 	return;
381 
382 err:
383 	/*
384 	 * Restore damage clip rectangle on errors. The next run
385 	 * of the damage worker will perform the update.
386 	 */
387 	spin_lock_irqsave(&helper->damage_lock, flags);
388 	clip->x1 = min_t(u32, clip->x1, clip_copy.x1);
389 	clip->y1 = min_t(u32, clip->y1, clip_copy.y1);
390 	clip->x2 = max_t(u32, clip->x2, clip_copy.x2);
391 	clip->y2 = max_t(u32, clip->y2, clip_copy.y2);
392 	spin_unlock_irqrestore(&helper->damage_lock, flags);
393 }
394 
395 static void drm_fb_helper_damage_work(struct work_struct *work)
396 {
397 	struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, damage_work);
398 
399 	drm_fb_helper_fb_dirty(helper);
400 }
401 
402 /**
403  * drm_fb_helper_prepare - setup a drm_fb_helper structure
404  * @dev: DRM device
405  * @helper: driver-allocated fbdev helper structure to set up
406  * @preferred_bpp: Preferred bits per pixel for the device.
407  * @funcs: pointer to structure of functions associate with this helper
408  *
409  * Sets up the bare minimum to make the framebuffer helper usable. This is
410  * useful to implement race-free initialization of the polling helpers.
411  */
412 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
413 			   unsigned int preferred_bpp,
414 			   const struct drm_fb_helper_funcs *funcs)
415 {
416 	/*
417 	 * Pick a preferred bpp of 32 if no value has been given. This
418 	 * will select XRGB8888 for the framebuffer formats. All drivers
419 	 * have to support XRGB8888 for backwards compatibility with legacy
420 	 * userspace, so it's the safe choice here.
421 	 *
422 	 * TODO: Replace struct drm_mode_config.preferred_depth and this
423 	 *       bpp value with a preferred format that is given as struct
424 	 *       drm_format_info. Then derive all other values from the
425 	 *       format.
426 	 */
427 	if (!preferred_bpp)
428 		preferred_bpp = 32;
429 
430 	INIT_LIST_HEAD(&helper->kernel_fb_list);
431 	spin_lock_init(&helper->damage_lock);
432 	INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
433 	INIT_WORK(&helper->damage_work, drm_fb_helper_damage_work);
434 	helper->damage_clip.x1 = helper->damage_clip.y1 = ~0;
435 	mutex_init(&helper->lock);
436 	helper->funcs = funcs;
437 	helper->dev = dev;
438 	helper->preferred_bpp = preferred_bpp;
439 }
440 EXPORT_SYMBOL(drm_fb_helper_prepare);
441 
442 /**
443  * drm_fb_helper_unprepare - clean up a drm_fb_helper structure
444  * @fb_helper: driver-allocated fbdev helper structure to set up
445  *
446  * Cleans up the framebuffer helper. Inverse of drm_fb_helper_prepare().
447  */
448 void drm_fb_helper_unprepare(struct drm_fb_helper *fb_helper)
449 {
450 	mutex_destroy(&fb_helper->lock);
451 }
452 EXPORT_SYMBOL(drm_fb_helper_unprepare);
453 
454 /**
455  * drm_fb_helper_init - initialize a &struct drm_fb_helper
456  * @dev: drm device
457  * @fb_helper: driver-allocated fbdev helper structure to initialize
458  *
459  * This allocates the structures for the fbdev helper with the given limits.
460  * Note that this won't yet touch the hardware (through the driver interfaces)
461  * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
462  * to allow driver writes more control over the exact init sequence.
463  *
464  * Drivers must call drm_fb_helper_prepare() before calling this function.
465  *
466  * RETURNS:
467  * Zero if everything went ok, nonzero otherwise.
468  */
469 int drm_fb_helper_init(struct drm_device *dev,
470 		       struct drm_fb_helper *fb_helper)
471 {
472 	int ret;
473 
474 	/*
475 	 * If this is not the generic fbdev client, initialize a drm_client
476 	 * without callbacks so we can use the modesets.
477 	 */
478 	if (!fb_helper->client.funcs) {
479 		ret = drm_client_init(dev, &fb_helper->client, "drm_fb_helper", NULL);
480 		if (ret)
481 			return ret;
482 	}
483 
484 	dev->fb_helper = fb_helper;
485 
486 	return 0;
487 }
488 EXPORT_SYMBOL(drm_fb_helper_init);
489 
490 /**
491  * drm_fb_helper_alloc_info - allocate fb_info and some of its members
492  * @fb_helper: driver-allocated fbdev helper
493  *
494  * A helper to alloc fb_info and the member cmap. Called by the driver
495  * within the struct &drm_driver.fbdev_probe callback function. Drivers do
496  * not need to release the allocated fb_info structure themselves, this is
497  * automatically done when calling drm_fb_helper_fini().
498  *
499  * RETURNS:
500  * fb_info pointer if things went okay, pointer containing error code
501  * otherwise
502  */
503 struct fb_info *drm_fb_helper_alloc_info(struct drm_fb_helper *fb_helper)
504 {
505 	struct device *dev = fb_helper->dev->dev;
506 	struct fb_info *info;
507 	int ret;
508 
509 	info = framebuffer_alloc(0, dev);
510 	if (!info)
511 		return ERR_PTR(-ENOMEM);
512 
513 	if (!drm_leak_fbdev_smem)
514 		info->flags |= FBINFO_HIDE_SMEM_START;
515 
516 	ret = fb_alloc_cmap(&info->cmap, 256, 0);
517 	if (ret)
518 		goto err_release;
519 
520 	fb_helper->info = info;
521 	info->skip_vt_switch = true;
522 
523 	info->skip_panic = drm_panic_is_enabled(fb_helper->dev);
524 	return info;
525 
526 err_release:
527 	framebuffer_release(info);
528 	return ERR_PTR(ret);
529 }
530 EXPORT_SYMBOL(drm_fb_helper_alloc_info);
531 
532 /**
533  * drm_fb_helper_release_info - release fb_info and its members
534  * @fb_helper: driver-allocated fbdev helper
535  *
536  * A helper to release fb_info and the member cmap.  Drivers do not
537  * need to release the allocated fb_info structure themselves, this is
538  * automatically done when calling drm_fb_helper_fini().
539  */
540 void drm_fb_helper_release_info(struct drm_fb_helper *fb_helper)
541 {
542 	struct fb_info *info = fb_helper->info;
543 
544 	if (!info)
545 		return;
546 
547 	fb_helper->info = NULL;
548 
549 	if (info->cmap.len)
550 		fb_dealloc_cmap(&info->cmap);
551 	framebuffer_release(info);
552 }
553 EXPORT_SYMBOL(drm_fb_helper_release_info);
554 
555 /**
556  * drm_fb_helper_unregister_info - unregister fb_info framebuffer device
557  * @fb_helper: driver-allocated fbdev helper, must not be NULL
558  *
559  * A wrapper around unregister_framebuffer, to release the fb_info
560  * framebuffer device. This must be called before releasing all resources for
561  * @fb_helper by calling drm_fb_helper_fini().
562  */
563 void drm_fb_helper_unregister_info(struct drm_fb_helper *fb_helper)
564 {
565 	struct fb_info *info = fb_helper->info;
566 	struct device *dev = info->device;
567 
568 	if (dev_is_pci(dev))
569 		vga_switcheroo_client_fb_set(to_pci_dev(dev), NULL);
570 	unregister_framebuffer(fb_helper->info);
571 }
572 EXPORT_SYMBOL(drm_fb_helper_unregister_info);
573 
574 /**
575  * drm_fb_helper_fini - finialize a &struct drm_fb_helper
576  * @fb_helper: driver-allocated fbdev helper, can be NULL
577  *
578  * This cleans up all remaining resources associated with @fb_helper.
579  */
580 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
581 {
582 	if (!fb_helper)
583 		return;
584 
585 	fb_helper->dev->fb_helper = NULL;
586 
587 	if (!drm_fbdev_emulation)
588 		return;
589 
590 	cancel_work_sync(&fb_helper->resume_work);
591 	cancel_work_sync(&fb_helper->damage_work);
592 
593 	drm_fb_helper_release_info(fb_helper);
594 
595 	mutex_lock(&kernel_fb_helper_lock);
596 	if (!list_empty(&fb_helper->kernel_fb_list)) {
597 		list_del(&fb_helper->kernel_fb_list);
598 		if (list_empty(&kernel_fb_helper_list))
599 			unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
600 	}
601 	mutex_unlock(&kernel_fb_helper_lock);
602 
603 	if (!fb_helper->client.funcs)
604 		drm_client_release(&fb_helper->client);
605 }
606 EXPORT_SYMBOL(drm_fb_helper_fini);
607 
608 static void drm_fb_helper_add_damage_clip(struct drm_fb_helper *helper, u32 x, u32 y,
609 					  u32 width, u32 height)
610 {
611 	struct drm_clip_rect *clip = &helper->damage_clip;
612 	unsigned long flags;
613 
614 	spin_lock_irqsave(&helper->damage_lock, flags);
615 	clip->x1 = min_t(u32, clip->x1, x);
616 	clip->y1 = min_t(u32, clip->y1, y);
617 	clip->x2 = max_t(u32, clip->x2, x + width);
618 	clip->y2 = max_t(u32, clip->y2, y + height);
619 	spin_unlock_irqrestore(&helper->damage_lock, flags);
620 }
621 
622 static void drm_fb_helper_damage(struct drm_fb_helper *helper, u32 x, u32 y,
623 				 u32 width, u32 height)
624 {
625 	/*
626 	 * This function may be invoked by panic() to flush the frame
627 	 * buffer, where all CPUs except the panic CPU are stopped.
628 	 * During the following schedule_work(), the panic CPU needs
629 	 * the worker_pool lock, which might be held by a stopped CPU,
630 	 * causing schedule_work() and panic() to block. Return early on
631 	 * oops_in_progress to prevent this blocking.
632 	 */
633 	if (oops_in_progress)
634 		return;
635 
636 	drm_fb_helper_add_damage_clip(helper, x, y, width, height);
637 
638 	schedule_work(&helper->damage_work);
639 }
640 
641 /*
642  * Convert memory region into area of scanlines and pixels per
643  * scanline. The parameters off and len must not reach beyond
644  * the end of the framebuffer.
645  */
646 static void drm_fb_helper_memory_range_to_clip(struct fb_info *info, off_t off, size_t len,
647 					       struct drm_rect *clip)
648 {
649 	u32 line_length = info->fix.line_length;
650 	u32 fb_height = info->var.yres;
651 	off_t end = off + len;
652 	u32 x1 = 0;
653 	u32 y1 = off / line_length;
654 	u32 x2 = info->var.xres;
655 	u32 y2 = DIV_ROUND_UP(end, line_length);
656 
657 	/* Don't allow any of them beyond the bottom bound of display area */
658 	if (y1 > fb_height)
659 		y1 = fb_height;
660 	if (y2 > fb_height)
661 		y2 = fb_height;
662 
663 	if ((y2 - y1) == 1) {
664 		/*
665 		 * We've only written to a single scanline. Try to reduce
666 		 * the number of horizontal pixels that need an update.
667 		 */
668 		off_t bit_off = (off % line_length) * 8;
669 		off_t bit_end = (end % line_length) * 8;
670 
671 		x1 = bit_off / info->var.bits_per_pixel;
672 		x2 = DIV_ROUND_UP(bit_end, info->var.bits_per_pixel);
673 	}
674 
675 	drm_rect_init(clip, x1, y1, x2 - x1, y2 - y1);
676 }
677 
678 /* Don't use in new code. */
679 void drm_fb_helper_damage_range(struct fb_info *info, off_t off, size_t len)
680 {
681 	struct drm_fb_helper *fb_helper = info->par;
682 	struct drm_rect damage_area;
683 
684 	drm_fb_helper_memory_range_to_clip(info, off, len, &damage_area);
685 	drm_fb_helper_damage(fb_helper, damage_area.x1, damage_area.y1,
686 			     drm_rect_width(&damage_area),
687 			     drm_rect_height(&damage_area));
688 }
689 EXPORT_SYMBOL(drm_fb_helper_damage_range);
690 
691 /* Don't use in new code. */
692 void drm_fb_helper_damage_area(struct fb_info *info, u32 x, u32 y, u32 width, u32 height)
693 {
694 	struct drm_fb_helper *fb_helper = info->par;
695 
696 	drm_fb_helper_damage(fb_helper, x, y, width, height);
697 }
698 EXPORT_SYMBOL(drm_fb_helper_damage_area);
699 
700 /**
701  * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
702  * @info: fb_info struct pointer
703  * @pagereflist: list of mmap framebuffer pages that have to be flushed
704  *
705  * This function is used as the &fb_deferred_io.deferred_io
706  * callback function for flushing the fbdev mmap writes.
707  */
708 void drm_fb_helper_deferred_io(struct fb_info *info, struct list_head *pagereflist)
709 {
710 	struct drm_fb_helper *helper = info->par;
711 	unsigned long start, end, min_off, max_off, total_size;
712 	struct fb_deferred_io_pageref *pageref;
713 	struct drm_rect damage_area;
714 
715 	min_off = ULONG_MAX;
716 	max_off = 0;
717 	list_for_each_entry(pageref, pagereflist, list) {
718 		start = pageref->offset;
719 		end = start + PAGE_SIZE;
720 		min_off = min(min_off, start);
721 		max_off = max(max_off, end);
722 	}
723 
724 	/*
725 	 * As we can only track pages, we might reach beyond the end
726 	 * of the screen and account for non-existing scanlines. Hence,
727 	 * keep the covered memory area within the screen buffer.
728 	 */
729 	if (info->screen_size)
730 		total_size = info->screen_size;
731 	else
732 		total_size = info->fix.smem_len;
733 	max_off = min(max_off, total_size);
734 
735 	if (min_off < max_off) {
736 		drm_fb_helper_memory_range_to_clip(info, min_off, max_off - min_off, &damage_area);
737 		drm_fb_helper_damage(helper, damage_area.x1, damage_area.y1,
738 				     drm_rect_width(&damage_area),
739 				     drm_rect_height(&damage_area));
740 	}
741 }
742 EXPORT_SYMBOL(drm_fb_helper_deferred_io);
743 
744 /**
745  * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
746  * @fb_helper: driver-allocated fbdev helper, can be NULL
747  * @suspend: whether to suspend or resume
748  *
749  * A wrapper around fb_set_suspend implemented by fbdev core.
750  * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
751  * the lock yourself
752  */
753 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
754 {
755 	if (fb_helper && fb_helper->info)
756 		fb_set_suspend(fb_helper->info, suspend);
757 }
758 EXPORT_SYMBOL(drm_fb_helper_set_suspend);
759 
760 /**
761  * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
762  *                                      takes the console lock
763  * @fb_helper: driver-allocated fbdev helper, can be NULL
764  * @suspend: whether to suspend or resume
765  *
766  * A wrapper around fb_set_suspend() that takes the console lock. If the lock
767  * isn't available on resume, a worker is tasked with waiting for the lock
768  * to become available. The console lock can be pretty contented on resume
769  * due to all the printk activity.
770  *
771  * This function can be called multiple times with the same state since
772  * &fb_info.state is checked to see if fbdev is running or not before locking.
773  *
774  * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
775  */
776 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
777 					bool suspend)
778 {
779 	if (!fb_helper || !fb_helper->info)
780 		return;
781 
782 	/* make sure there's no pending/ongoing resume */
783 	flush_work(&fb_helper->resume_work);
784 
785 	if (suspend) {
786 		if (fb_helper->info->state != FBINFO_STATE_RUNNING)
787 			return;
788 
789 		console_lock();
790 
791 	} else {
792 		if (fb_helper->info->state == FBINFO_STATE_RUNNING)
793 			return;
794 
795 		if (!console_trylock()) {
796 			schedule_work(&fb_helper->resume_work);
797 			return;
798 		}
799 	}
800 
801 	fb_set_suspend(fb_helper->info, suspend);
802 	console_unlock();
803 }
804 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
805 
806 static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
807 {
808 	u32 *palette = (u32 *)info->pseudo_palette;
809 	int i;
810 
811 	if (cmap->start + cmap->len > 16)
812 		return -EINVAL;
813 
814 	for (i = 0; i < cmap->len; ++i) {
815 		u16 red = cmap->red[i];
816 		u16 green = cmap->green[i];
817 		u16 blue = cmap->blue[i];
818 		u32 value;
819 
820 		red >>= 16 - info->var.red.length;
821 		green >>= 16 - info->var.green.length;
822 		blue >>= 16 - info->var.blue.length;
823 		value = (red << info->var.red.offset) |
824 			(green << info->var.green.offset) |
825 			(blue << info->var.blue.offset);
826 		if (info->var.transp.length > 0) {
827 			u32 mask = (1 << info->var.transp.length) - 1;
828 
829 			mask <<= info->var.transp.offset;
830 			value |= mask;
831 		}
832 		palette[cmap->start + i] = value;
833 	}
834 
835 	return 0;
836 }
837 
838 static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
839 {
840 	struct drm_fb_helper *fb_helper = info->par;
841 	struct drm_mode_set *modeset;
842 	struct drm_crtc *crtc;
843 	u16 *r, *g, *b;
844 	int ret = 0;
845 
846 	drm_modeset_lock_all(fb_helper->dev);
847 	drm_client_for_each_modeset(modeset, &fb_helper->client) {
848 		crtc = modeset->crtc;
849 		if (!crtc->funcs->gamma_set || !crtc->gamma_size) {
850 			ret = -EINVAL;
851 			goto out;
852 		}
853 
854 		if (cmap->start + cmap->len > crtc->gamma_size) {
855 			ret = -EINVAL;
856 			goto out;
857 		}
858 
859 		r = crtc->gamma_store;
860 		g = r + crtc->gamma_size;
861 		b = g + crtc->gamma_size;
862 
863 		memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
864 		memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
865 		memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
866 
867 		ret = crtc->funcs->gamma_set(crtc, r, g, b,
868 					     crtc->gamma_size, NULL);
869 		if (ret)
870 			goto out;
871 	}
872 out:
873 	drm_modeset_unlock_all(fb_helper->dev);
874 
875 	return ret;
876 }
877 
878 static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
879 						       struct fb_cmap *cmap)
880 {
881 	struct drm_device *dev = crtc->dev;
882 	struct drm_property_blob *gamma_lut;
883 	struct drm_color_lut *lut;
884 	int size = crtc->gamma_size;
885 	int i;
886 
887 	if (!size || cmap->start + cmap->len > size)
888 		return ERR_PTR(-EINVAL);
889 
890 	gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
891 	if (IS_ERR(gamma_lut))
892 		return gamma_lut;
893 
894 	lut = gamma_lut->data;
895 	if (cmap->start || cmap->len != size) {
896 		u16 *r = crtc->gamma_store;
897 		u16 *g = r + crtc->gamma_size;
898 		u16 *b = g + crtc->gamma_size;
899 
900 		for (i = 0; i < cmap->start; i++) {
901 			lut[i].red = r[i];
902 			lut[i].green = g[i];
903 			lut[i].blue = b[i];
904 		}
905 		for (i = cmap->start + cmap->len; i < size; i++) {
906 			lut[i].red = r[i];
907 			lut[i].green = g[i];
908 			lut[i].blue = b[i];
909 		}
910 	}
911 
912 	for (i = 0; i < cmap->len; i++) {
913 		lut[cmap->start + i].red = cmap->red[i];
914 		lut[cmap->start + i].green = cmap->green[i];
915 		lut[cmap->start + i].blue = cmap->blue[i];
916 	}
917 
918 	return gamma_lut;
919 }
920 
921 static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
922 {
923 	struct drm_fb_helper *fb_helper = info->par;
924 	struct drm_device *dev = fb_helper->dev;
925 	struct drm_property_blob *gamma_lut = NULL;
926 	struct drm_modeset_acquire_ctx ctx;
927 	struct drm_crtc_state *crtc_state;
928 	struct drm_atomic_state *state;
929 	struct drm_mode_set *modeset;
930 	struct drm_crtc *crtc;
931 	u16 *r, *g, *b;
932 	bool replaced;
933 	int ret = 0;
934 
935 	drm_modeset_acquire_init(&ctx, 0);
936 
937 	state = drm_atomic_state_alloc(dev);
938 	if (!state) {
939 		ret = -ENOMEM;
940 		goto out_ctx;
941 	}
942 
943 	state->acquire_ctx = &ctx;
944 retry:
945 	drm_client_for_each_modeset(modeset, &fb_helper->client) {
946 		crtc = modeset->crtc;
947 
948 		if (!gamma_lut)
949 			gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
950 		if (IS_ERR(gamma_lut)) {
951 			ret = PTR_ERR(gamma_lut);
952 			gamma_lut = NULL;
953 			goto out_state;
954 		}
955 
956 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
957 		if (IS_ERR(crtc_state)) {
958 			ret = PTR_ERR(crtc_state);
959 			goto out_state;
960 		}
961 
962 		/*
963 		 * FIXME: This always uses gamma_lut. Some HW have only
964 		 * degamma_lut, in which case we should reset gamma_lut and set
965 		 * degamma_lut. See drm_crtc_legacy_gamma_set().
966 		 */
967 		replaced  = drm_property_replace_blob(&crtc_state->degamma_lut,
968 						      NULL);
969 		replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
970 		replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
971 						      gamma_lut);
972 		crtc_state->color_mgmt_changed |= replaced;
973 	}
974 
975 	ret = drm_atomic_commit(state);
976 	if (ret)
977 		goto out_state;
978 
979 	drm_client_for_each_modeset(modeset, &fb_helper->client) {
980 		crtc = modeset->crtc;
981 
982 		r = crtc->gamma_store;
983 		g = r + crtc->gamma_size;
984 		b = g + crtc->gamma_size;
985 
986 		memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
987 		memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
988 		memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
989 	}
990 
991 out_state:
992 	if (ret == -EDEADLK)
993 		goto backoff;
994 
995 	drm_property_blob_put(gamma_lut);
996 	drm_atomic_state_put(state);
997 out_ctx:
998 	drm_modeset_drop_locks(&ctx);
999 	drm_modeset_acquire_fini(&ctx);
1000 
1001 	return ret;
1002 
1003 backoff:
1004 	drm_atomic_state_clear(state);
1005 	drm_modeset_backoff(&ctx);
1006 	goto retry;
1007 }
1008 
1009 /**
1010  * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1011  * @cmap: cmap to set
1012  * @info: fbdev registered by the helper
1013  */
1014 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1015 {
1016 	struct drm_fb_helper *fb_helper = info->par;
1017 	struct drm_device *dev = fb_helper->dev;
1018 	int ret;
1019 
1020 	if (oops_in_progress)
1021 		return -EBUSY;
1022 
1023 	mutex_lock(&fb_helper->lock);
1024 
1025 	if (!drm_master_internal_acquire(dev)) {
1026 		ret = -EBUSY;
1027 		goto unlock;
1028 	}
1029 
1030 	mutex_lock(&fb_helper->client.modeset_mutex);
1031 	if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1032 		ret = setcmap_pseudo_palette(cmap, info);
1033 	else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1034 		ret = setcmap_atomic(cmap, info);
1035 	else
1036 		ret = setcmap_legacy(cmap, info);
1037 	mutex_unlock(&fb_helper->client.modeset_mutex);
1038 
1039 	drm_master_internal_release(dev);
1040 unlock:
1041 	mutex_unlock(&fb_helper->lock);
1042 
1043 	return ret;
1044 }
1045 EXPORT_SYMBOL(drm_fb_helper_setcmap);
1046 
1047 /**
1048  * drm_fb_helper_ioctl - legacy ioctl implementation
1049  * @info: fbdev registered by the helper
1050  * @cmd: ioctl command
1051  * @arg: ioctl argument
1052  *
1053  * A helper to implement the standard fbdev ioctl. Only
1054  * FBIO_WAITFORVSYNC is implemented for now.
1055  */
1056 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1057 			unsigned long arg)
1058 {
1059 	struct drm_fb_helper *fb_helper = info->par;
1060 	struct drm_device *dev = fb_helper->dev;
1061 	struct drm_crtc *crtc;
1062 	int ret = 0;
1063 
1064 	mutex_lock(&fb_helper->lock);
1065 	if (!drm_master_internal_acquire(dev)) {
1066 		ret = -EBUSY;
1067 		goto unlock;
1068 	}
1069 
1070 	switch (cmd) {
1071 	case FBIO_WAITFORVSYNC:
1072 		/*
1073 		 * Only consider the first CRTC.
1074 		 *
1075 		 * This ioctl is supposed to take the CRTC number as
1076 		 * an argument, but in fbdev times, what that number
1077 		 * was supposed to be was quite unclear, different
1078 		 * drivers were passing that argument differently
1079 		 * (some by reference, some by value), and most of the
1080 		 * userspace applications were just hardcoding 0 as an
1081 		 * argument.
1082 		 *
1083 		 * The first CRTC should be the integrated panel on
1084 		 * most drivers, so this is the best choice we can
1085 		 * make. If we're not smart enough here, one should
1086 		 * just consider switch the userspace to KMS.
1087 		 */
1088 		crtc = fb_helper->client.modesets[0].crtc;
1089 
1090 		/*
1091 		 * Only wait for a vblank event if the CRTC is
1092 		 * enabled, otherwise just don't do anythintg,
1093 		 * not even report an error.
1094 		 */
1095 		ret = drm_crtc_vblank_get(crtc);
1096 		if (!ret) {
1097 			drm_crtc_wait_one_vblank(crtc);
1098 			drm_crtc_vblank_put(crtc);
1099 		}
1100 
1101 		ret = 0;
1102 		break;
1103 	default:
1104 		ret = -ENOTTY;
1105 	}
1106 
1107 	drm_master_internal_release(dev);
1108 unlock:
1109 	mutex_unlock(&fb_helper->lock);
1110 	return ret;
1111 }
1112 EXPORT_SYMBOL(drm_fb_helper_ioctl);
1113 
1114 static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1,
1115 				      const struct fb_var_screeninfo *var_2)
1116 {
1117 	return var_1->bits_per_pixel == var_2->bits_per_pixel &&
1118 	       var_1->grayscale == var_2->grayscale &&
1119 	       var_1->red.offset == var_2->red.offset &&
1120 	       var_1->red.length == var_2->red.length &&
1121 	       var_1->red.msb_right == var_2->red.msb_right &&
1122 	       var_1->green.offset == var_2->green.offset &&
1123 	       var_1->green.length == var_2->green.length &&
1124 	       var_1->green.msb_right == var_2->green.msb_right &&
1125 	       var_1->blue.offset == var_2->blue.offset &&
1126 	       var_1->blue.length == var_2->blue.length &&
1127 	       var_1->blue.msb_right == var_2->blue.msb_right &&
1128 	       var_1->transp.offset == var_2->transp.offset &&
1129 	       var_1->transp.length == var_2->transp.length &&
1130 	       var_1->transp.msb_right == var_2->transp.msb_right;
1131 }
1132 
1133 static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo *var,
1134 					 const struct drm_format_info *format)
1135 {
1136 	u8 depth = format->depth;
1137 
1138 	if (format->is_color_indexed) {
1139 		var->red.offset = 0;
1140 		var->green.offset = 0;
1141 		var->blue.offset = 0;
1142 		var->red.length = depth;
1143 		var->green.length = depth;
1144 		var->blue.length = depth;
1145 		var->transp.offset = 0;
1146 		var->transp.length = 0;
1147 		return;
1148 	}
1149 
1150 	switch (depth) {
1151 	case 15:
1152 		var->red.offset = 10;
1153 		var->green.offset = 5;
1154 		var->blue.offset = 0;
1155 		var->red.length = 5;
1156 		var->green.length = 5;
1157 		var->blue.length = 5;
1158 		var->transp.offset = 15;
1159 		var->transp.length = 1;
1160 		break;
1161 	case 16:
1162 		var->red.offset = 11;
1163 		var->green.offset = 5;
1164 		var->blue.offset = 0;
1165 		var->red.length = 5;
1166 		var->green.length = 6;
1167 		var->blue.length = 5;
1168 		var->transp.offset = 0;
1169 		break;
1170 	case 24:
1171 		var->red.offset = 16;
1172 		var->green.offset = 8;
1173 		var->blue.offset = 0;
1174 		var->red.length = 8;
1175 		var->green.length = 8;
1176 		var->blue.length = 8;
1177 		var->transp.offset = 0;
1178 		var->transp.length = 0;
1179 		break;
1180 	case 32:
1181 		var->red.offset = 16;
1182 		var->green.offset = 8;
1183 		var->blue.offset = 0;
1184 		var->red.length = 8;
1185 		var->green.length = 8;
1186 		var->blue.length = 8;
1187 		var->transp.offset = 24;
1188 		var->transp.length = 8;
1189 		break;
1190 	default:
1191 		break;
1192 	}
1193 }
1194 
1195 static void __fill_var(struct fb_var_screeninfo *var, struct fb_info *info,
1196 		       struct drm_framebuffer *fb)
1197 {
1198 	int i;
1199 
1200 	var->xres_virtual = fb->width;
1201 	var->yres_virtual = fb->height;
1202 	var->accel_flags = 0;
1203 	var->bits_per_pixel = drm_format_info_bpp(fb->format, 0);
1204 
1205 	var->height = info->var.height;
1206 	var->width = info->var.width;
1207 
1208 	var->left_margin = var->right_margin = 0;
1209 	var->upper_margin = var->lower_margin = 0;
1210 	var->hsync_len = var->vsync_len = 0;
1211 	var->sync = var->vmode = 0;
1212 	var->rotate = 0;
1213 	var->colorspace = 0;
1214 	for (i = 0; i < 4; i++)
1215 		var->reserved[i] = 0;
1216 }
1217 
1218 /**
1219  * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
1220  * @var: screeninfo to check
1221  * @info: fbdev registered by the helper
1222  */
1223 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1224 			    struct fb_info *info)
1225 {
1226 	struct drm_fb_helper *fb_helper = info->par;
1227 	struct drm_framebuffer *fb = fb_helper->fb;
1228 	const struct drm_format_info *format = fb->format;
1229 	struct drm_device *dev = fb_helper->dev;
1230 	unsigned int bpp;
1231 
1232 	if (in_dbg_master())
1233 		return -EINVAL;
1234 
1235 	if (var->pixclock != 0) {
1236 		drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n");
1237 		var->pixclock = 0;
1238 	}
1239 
1240 	switch (format->format) {
1241 	case DRM_FORMAT_C1:
1242 	case DRM_FORMAT_C2:
1243 	case DRM_FORMAT_C4:
1244 		/* supported format with sub-byte pixels */
1245 		break;
1246 
1247 	default:
1248 		if ((drm_format_info_block_width(format, 0) > 1) ||
1249 		    (drm_format_info_block_height(format, 0) > 1))
1250 			return -EINVAL;
1251 		break;
1252 	}
1253 
1254 	/*
1255 	 * Changes struct fb_var_screeninfo are currently not pushed back
1256 	 * to KMS, hence fail if different settings are requested.
1257 	 */
1258 	bpp = drm_format_info_bpp(format, 0);
1259 	if (var->bits_per_pixel > bpp ||
1260 	    var->xres > fb->width || var->yres > fb->height ||
1261 	    var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1262 		drm_dbg_kms(dev, "fb requested width/height/bpp can't fit in current fb "
1263 			  "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1264 			  var->xres, var->yres, var->bits_per_pixel,
1265 			  var->xres_virtual, var->yres_virtual,
1266 			  fb->width, fb->height, bpp);
1267 		return -EINVAL;
1268 	}
1269 
1270 	__fill_var(var, info, fb);
1271 
1272 	/*
1273 	 * fb_pan_display() validates this, but fb_set_par() doesn't and just
1274 	 * falls over. Note that __fill_var above adjusts y/res_virtual.
1275 	 */
1276 	if (var->yoffset > var->yres_virtual - var->yres ||
1277 	    var->xoffset > var->xres_virtual - var->xres)
1278 		return -EINVAL;
1279 
1280 	/* We neither support grayscale nor FOURCC (also stored in here). */
1281 	if (var->grayscale > 0)
1282 		return -EINVAL;
1283 
1284 	if (var->nonstd)
1285 		return -EINVAL;
1286 
1287 	/*
1288 	 * Workaround for SDL 1.2, which is known to be setting all pixel format
1289 	 * fields values to zero in some cases. We treat this situation as a
1290 	 * kind of "use some reasonable autodetected values".
1291 	 */
1292 	if (!var->red.offset     && !var->green.offset    &&
1293 	    !var->blue.offset    && !var->transp.offset   &&
1294 	    !var->red.length     && !var->green.length    &&
1295 	    !var->blue.length    && !var->transp.length   &&
1296 	    !var->red.msb_right  && !var->green.msb_right &&
1297 	    !var->blue.msb_right && !var->transp.msb_right) {
1298 		drm_fb_helper_fill_pixel_fmt(var, format);
1299 	}
1300 
1301 	/*
1302 	 * drm fbdev emulation doesn't support changing the pixel format at all,
1303 	 * so reject all pixel format changing requests.
1304 	 */
1305 	if (!drm_fb_pixel_format_equal(var, &info->var)) {
1306 		drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel format\n");
1307 		return -EINVAL;
1308 	}
1309 
1310 	return 0;
1311 }
1312 EXPORT_SYMBOL(drm_fb_helper_check_var);
1313 
1314 /**
1315  * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1316  * @info: fbdev registered by the helper
1317  *
1318  * This will let fbcon do the mode init and is called at initialization time by
1319  * the fbdev core when registering the driver, and later on through the hotplug
1320  * callback.
1321  */
1322 int drm_fb_helper_set_par(struct fb_info *info)
1323 {
1324 	struct drm_fb_helper *fb_helper = info->par;
1325 	struct fb_var_screeninfo *var = &info->var;
1326 	bool force;
1327 
1328 	if (oops_in_progress)
1329 		return -EBUSY;
1330 
1331 	/*
1332 	 * Normally we want to make sure that a kms master takes precedence over
1333 	 * fbdev, to avoid fbdev flickering and occasionally stealing the
1334 	 * display status. But Xorg first sets the vt back to text mode using
1335 	 * the KDSET IOCTL with KD_TEXT, and only after that drops the master
1336 	 * status when exiting.
1337 	 *
1338 	 * In the past this was caught by drm_fb_helper_lastclose(), but on
1339 	 * modern systems where logind always keeps a drm fd open to orchestrate
1340 	 * the vt switching, this doesn't work.
1341 	 *
1342 	 * To not break the userspace ABI we have this special case here, which
1343 	 * is only used for the above case. Everything else uses the normal
1344 	 * commit function, which ensures that we never steal the display from
1345 	 * an active drm master.
1346 	 */
1347 	force = var->activate & FB_ACTIVATE_KD_TEXT;
1348 
1349 	__drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, force);
1350 
1351 	return 0;
1352 }
1353 EXPORT_SYMBOL(drm_fb_helper_set_par);
1354 
1355 static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
1356 {
1357 	struct drm_mode_set *mode_set;
1358 
1359 	mutex_lock(&fb_helper->client.modeset_mutex);
1360 	drm_client_for_each_modeset(mode_set, &fb_helper->client) {
1361 		mode_set->x = x;
1362 		mode_set->y = y;
1363 	}
1364 	mutex_unlock(&fb_helper->client.modeset_mutex);
1365 }
1366 
1367 static int pan_display_atomic(struct fb_var_screeninfo *var,
1368 			      struct fb_info *info)
1369 {
1370 	struct drm_fb_helper *fb_helper = info->par;
1371 	int ret;
1372 
1373 	pan_set(fb_helper, var->xoffset, var->yoffset);
1374 
1375 	ret = drm_client_modeset_commit_locked(&fb_helper->client);
1376 	if (!ret) {
1377 		info->var.xoffset = var->xoffset;
1378 		info->var.yoffset = var->yoffset;
1379 	} else
1380 		pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
1381 
1382 	return ret;
1383 }
1384 
1385 static int pan_display_legacy(struct fb_var_screeninfo *var,
1386 			      struct fb_info *info)
1387 {
1388 	struct drm_fb_helper *fb_helper = info->par;
1389 	struct drm_client_dev *client = &fb_helper->client;
1390 	struct drm_mode_set *modeset;
1391 	int ret = 0;
1392 
1393 	mutex_lock(&client->modeset_mutex);
1394 	drm_modeset_lock_all(fb_helper->dev);
1395 	drm_client_for_each_modeset(modeset, client) {
1396 		modeset->x = var->xoffset;
1397 		modeset->y = var->yoffset;
1398 
1399 		if (modeset->num_connectors) {
1400 			ret = drm_mode_set_config_internal(modeset);
1401 			if (!ret) {
1402 				info->var.xoffset = var->xoffset;
1403 				info->var.yoffset = var->yoffset;
1404 			}
1405 		}
1406 	}
1407 	drm_modeset_unlock_all(fb_helper->dev);
1408 	mutex_unlock(&client->modeset_mutex);
1409 
1410 	return ret;
1411 }
1412 
1413 /**
1414  * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1415  * @var: updated screen information
1416  * @info: fbdev registered by the helper
1417  */
1418 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1419 			      struct fb_info *info)
1420 {
1421 	struct drm_fb_helper *fb_helper = info->par;
1422 	struct drm_device *dev = fb_helper->dev;
1423 	int ret;
1424 
1425 	if (oops_in_progress)
1426 		return -EBUSY;
1427 
1428 	mutex_lock(&fb_helper->lock);
1429 	if (!drm_master_internal_acquire(dev)) {
1430 		ret = -EBUSY;
1431 		goto unlock;
1432 	}
1433 
1434 	if (drm_drv_uses_atomic_modeset(dev))
1435 		ret = pan_display_atomic(var, info);
1436 	else
1437 		ret = pan_display_legacy(var, info);
1438 
1439 	drm_master_internal_release(dev);
1440 unlock:
1441 	mutex_unlock(&fb_helper->lock);
1442 
1443 	return ret;
1444 }
1445 EXPORT_SYMBOL(drm_fb_helper_pan_display);
1446 
1447 static uint32_t drm_fb_helper_find_format(struct drm_fb_helper *fb_helper, const uint32_t *formats,
1448 					  size_t format_count, unsigned int color_mode)
1449 {
1450 	struct drm_device *dev = fb_helper->dev;
1451 	uint32_t format;
1452 	size_t i;
1453 
1454 	format = drm_driver_color_mode_format(dev, color_mode);
1455 	if (!format) {
1456 		drm_info(dev, "unsupported color mode of %d\n", color_mode);
1457 		return DRM_FORMAT_INVALID;
1458 	}
1459 
1460 	for (i = 0; i < format_count; ++i) {
1461 		if (formats[i] == format)
1462 			return format;
1463 	}
1464 	drm_warn(dev, "format %p4cc not supported\n", &format);
1465 
1466 	return DRM_FORMAT_INVALID;
1467 }
1468 
1469 static int __drm_fb_helper_find_sizes(struct drm_fb_helper *fb_helper,
1470 				      struct drm_fb_helper_surface_size *sizes)
1471 {
1472 	struct drm_client_dev *client = &fb_helper->client;
1473 	struct drm_device *dev = fb_helper->dev;
1474 	int crtc_count = 0;
1475 	struct drm_connector_list_iter conn_iter;
1476 	struct drm_connector *connector;
1477 	struct drm_mode_set *mode_set;
1478 	uint32_t surface_format = DRM_FORMAT_INVALID;
1479 	const struct drm_format_info *info;
1480 
1481 	memset(sizes, 0, sizeof(*sizes));
1482 	sizes->fb_width = (u32)-1;
1483 	sizes->fb_height = (u32)-1;
1484 
1485 	drm_client_for_each_modeset(mode_set, client) {
1486 		struct drm_crtc *crtc = mode_set->crtc;
1487 		struct drm_plane *plane = crtc->primary;
1488 
1489 		drm_dbg_kms(dev, "test CRTC %u primary plane\n", drm_crtc_index(crtc));
1490 
1491 		drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
1492 		drm_client_for_each_connector_iter(connector, &conn_iter) {
1493 			struct drm_cmdline_mode *cmdline_mode = &connector->cmdline_mode;
1494 
1495 			if (!cmdline_mode->bpp_specified)
1496 				continue;
1497 
1498 			surface_format = drm_fb_helper_find_format(fb_helper,
1499 								   plane->format_types,
1500 								   plane->format_count,
1501 								   cmdline_mode->bpp);
1502 			if (surface_format != DRM_FORMAT_INVALID)
1503 				break; /* found supported format */
1504 		}
1505 		drm_connector_list_iter_end(&conn_iter);
1506 
1507 		if (surface_format != DRM_FORMAT_INVALID)
1508 			break; /* found supported format */
1509 
1510 		/* try preferred color mode */
1511 		surface_format = drm_fb_helper_find_format(fb_helper,
1512 							   plane->format_types,
1513 							   plane->format_count,
1514 							   fb_helper->preferred_bpp);
1515 		if (surface_format != DRM_FORMAT_INVALID)
1516 			break; /* found supported format */
1517 	}
1518 
1519 	if (surface_format == DRM_FORMAT_INVALID) {
1520 		/*
1521 		 * If none of the given color modes works, fall back
1522 		 * to XRGB8888. Drivers are expected to provide this
1523 		 * format for compatibility with legacy applications.
1524 		 */
1525 		drm_warn(dev, "No compatible format found\n");
1526 		surface_format = drm_driver_legacy_fb_format(dev, 32, 24);
1527 	}
1528 
1529 	info = drm_format_info(surface_format);
1530 	sizes->surface_bpp = drm_format_info_bpp(info, 0);
1531 	sizes->surface_depth = info->depth;
1532 
1533 	/* first up get a count of crtcs now in use and new min/maxes width/heights */
1534 	crtc_count = 0;
1535 	drm_client_for_each_modeset(mode_set, client) {
1536 		struct drm_display_mode *desired_mode;
1537 		int x, y, j;
1538 		/* in case of tile group, are we the last tile vert or horiz?
1539 		 * If no tile group you are always the last one both vertically
1540 		 * and horizontally
1541 		 */
1542 		bool lastv = true, lasth = true;
1543 
1544 		desired_mode = mode_set->mode;
1545 
1546 		if (!desired_mode)
1547 			continue;
1548 
1549 		crtc_count++;
1550 
1551 		x = mode_set->x;
1552 		y = mode_set->y;
1553 
1554 		sizes->surface_width  =
1555 			max_t(u32, desired_mode->hdisplay + x, sizes->surface_width);
1556 		sizes->surface_height =
1557 			max_t(u32, desired_mode->vdisplay + y, sizes->surface_height);
1558 
1559 		for (j = 0; j < mode_set->num_connectors; j++) {
1560 			struct drm_connector *connector = mode_set->connectors[j];
1561 
1562 			if (connector->has_tile &&
1563 			    desired_mode->hdisplay == connector->tile_h_size &&
1564 			    desired_mode->vdisplay == connector->tile_v_size) {
1565 				lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1566 				lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1567 				/* cloning to multiple tiles is just crazy-talk, so: */
1568 				break;
1569 			}
1570 		}
1571 
1572 		if (lasth)
1573 			sizes->fb_width  = min_t(u32, desired_mode->hdisplay + x, sizes->fb_width);
1574 		if (lastv)
1575 			sizes->fb_height = min_t(u32, desired_mode->vdisplay + y, sizes->fb_height);
1576 	}
1577 
1578 	if (crtc_count == 0 || sizes->fb_width == -1 || sizes->fb_height == -1) {
1579 		drm_info(dev, "Cannot find any crtc or sizes\n");
1580 		return -EAGAIN;
1581 	}
1582 
1583 	return 0;
1584 }
1585 
1586 static int drm_fb_helper_find_sizes(struct drm_fb_helper *fb_helper,
1587 				    struct drm_fb_helper_surface_size *sizes)
1588 {
1589 	struct drm_client_dev *client = &fb_helper->client;
1590 	struct drm_device *dev = fb_helper->dev;
1591 	struct drm_mode_config *config = &dev->mode_config;
1592 	int ret;
1593 
1594 	mutex_lock(&client->modeset_mutex);
1595 	ret = __drm_fb_helper_find_sizes(fb_helper, sizes);
1596 	mutex_unlock(&client->modeset_mutex);
1597 
1598 	if (ret)
1599 		return ret;
1600 
1601 	/* Handle our overallocation */
1602 	sizes->surface_height *= drm_fbdev_overalloc;
1603 	sizes->surface_height /= 100;
1604 	if (sizes->surface_height > config->max_height) {
1605 		drm_dbg_kms(dev, "Fbdev over-allocation too large; clamping height to %d\n",
1606 			    config->max_height);
1607 		sizes->surface_height = config->max_height;
1608 	}
1609 
1610 	return 0;
1611 }
1612 
1613 /*
1614  * Allocates the backing storage and sets up the fbdev info structure through
1615  * the ->fbdev_probe callback.
1616  */
1617 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper)
1618 {
1619 	struct drm_client_dev *client = &fb_helper->client;
1620 	struct drm_device *dev = fb_helper->dev;
1621 	struct drm_fb_helper_surface_size sizes;
1622 	struct fb_info *info;
1623 	int ret;
1624 
1625 	ret = drm_fb_helper_find_sizes(fb_helper, &sizes);
1626 	if (ret) {
1627 		/* First time: disable all crtc's.. */
1628 		if (!fb_helper->deferred_setup)
1629 			drm_client_modeset_commit(client);
1630 		return ret;
1631 	}
1632 
1633 	/* push down into drivers */
1634 	if (dev->driver->fbdev_probe)
1635 		ret = dev->driver->fbdev_probe(fb_helper, &sizes);
1636 	else if (fb_helper->funcs)
1637 		ret = fb_helper->funcs->fb_probe(fb_helper, &sizes);
1638 	if (ret < 0)
1639 		return ret;
1640 
1641 	strcpy(fb_helper->fb->comm, "[fbcon]");
1642 
1643 	info = fb_helper->info;
1644 
1645 	/* Set the fb info for vgaswitcheroo clients. Does nothing otherwise. */
1646 	if (dev_is_pci(info->device))
1647 		vga_switcheroo_client_fb_set(to_pci_dev(info->device), info);
1648 
1649 	return 0;
1650 }
1651 
1652 static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1653 				   bool is_color_indexed)
1654 {
1655 	info->fix.type = FB_TYPE_PACKED_PIXELS;
1656 	info->fix.visual = is_color_indexed ? FB_VISUAL_PSEUDOCOLOR
1657 					    : FB_VISUAL_TRUECOLOR;
1658 	info->fix.mmio_start = 0;
1659 	info->fix.mmio_len = 0;
1660 	info->fix.type_aux = 0;
1661 	info->fix.xpanstep = 1; /* doing it in hw */
1662 	info->fix.ypanstep = 1; /* doing it in hw */
1663 	info->fix.ywrapstep = 0;
1664 	info->fix.accel = FB_ACCEL_NONE;
1665 
1666 	info->fix.line_length = pitch;
1667 }
1668 
1669 static void drm_fb_helper_fill_var(struct fb_info *info,
1670 				   struct drm_fb_helper *fb_helper,
1671 				   uint32_t fb_width, uint32_t fb_height)
1672 {
1673 	struct drm_framebuffer *fb = fb_helper->fb;
1674 	const struct drm_format_info *format = fb->format;
1675 
1676 	switch (format->format) {
1677 	case DRM_FORMAT_C1:
1678 	case DRM_FORMAT_C2:
1679 	case DRM_FORMAT_C4:
1680 		/* supported format with sub-byte pixels */
1681 		break;
1682 
1683 	default:
1684 		WARN_ON((drm_format_info_block_width(format, 0) > 1) ||
1685 			(drm_format_info_block_height(format, 0) > 1));
1686 		break;
1687 	}
1688 
1689 	info->pseudo_palette = fb_helper->pseudo_palette;
1690 	info->var.xoffset = 0;
1691 	info->var.yoffset = 0;
1692 	__fill_var(&info->var, info, fb);
1693 	info->var.activate = FB_ACTIVATE_NOW;
1694 
1695 	drm_fb_helper_fill_pixel_fmt(&info->var, format);
1696 
1697 	info->var.xres = fb_width;
1698 	info->var.yres = fb_height;
1699 }
1700 
1701 /**
1702  * drm_fb_helper_fill_info - initializes fbdev information
1703  * @info: fbdev instance to set up
1704  * @fb_helper: fb helper instance to use as template
1705  * @sizes: describes fbdev size and scanout surface size
1706  *
1707  * Sets up the variable and fixed fbdev metainformation from the given fb helper
1708  * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
1709  *
1710  * Drivers should call this (or their equivalent setup code) from their
1711  * &drm_driver.fbdev_probe callback after having allocated the fbdev
1712  * backing storage framebuffer.
1713  */
1714 void drm_fb_helper_fill_info(struct fb_info *info,
1715 			     struct drm_fb_helper *fb_helper,
1716 			     struct drm_fb_helper_surface_size *sizes)
1717 {
1718 	struct drm_framebuffer *fb = fb_helper->fb;
1719 
1720 	drm_fb_helper_fill_fix(info, fb->pitches[0],
1721 			       fb->format->is_color_indexed);
1722 	drm_fb_helper_fill_var(info, fb_helper,
1723 			       sizes->fb_width, sizes->fb_height);
1724 
1725 	info->par = fb_helper;
1726 	/*
1727 	 * The DRM drivers fbdev emulation device name can be confusing if the
1728 	 * driver name also has a "drm" suffix on it. Leading to names such as
1729 	 * "simpledrmdrmfb" in /proc/fb. Unfortunately, it's an uAPI and can't
1730 	 * be changed due user-space tools (e.g: pm-utils) matching against it.
1731 	 */
1732 	snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb",
1733 		 fb_helper->dev->driver->name);
1734 
1735 }
1736 EXPORT_SYMBOL(drm_fb_helper_fill_info);
1737 
1738 /*
1739  * This is a continuation of drm_setup_crtcs() that sets up anything related
1740  * to the framebuffer. During initialization, drm_setup_crtcs() is called before
1741  * the framebuffer has been allocated (fb_helper->fb and fb_helper->info).
1742  * So, any setup that touches those fields needs to be done here instead of in
1743  * drm_setup_crtcs().
1744  */
1745 static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
1746 {
1747 	struct drm_client_dev *client = &fb_helper->client;
1748 	struct drm_connector_list_iter conn_iter;
1749 	struct fb_info *info = fb_helper->info;
1750 	unsigned int rotation, sw_rotations = 0;
1751 	struct drm_connector *connector;
1752 	struct drm_mode_set *modeset;
1753 
1754 	mutex_lock(&client->modeset_mutex);
1755 	drm_client_for_each_modeset(modeset, client) {
1756 		if (!modeset->num_connectors)
1757 			continue;
1758 
1759 		modeset->fb = fb_helper->fb;
1760 
1761 		if (drm_client_rotation(modeset, &rotation))
1762 			/* Rotating in hardware, fbcon should not rotate */
1763 			sw_rotations |= DRM_MODE_ROTATE_0;
1764 		else
1765 			sw_rotations |= rotation;
1766 	}
1767 	mutex_unlock(&client->modeset_mutex);
1768 
1769 	drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
1770 	drm_client_for_each_connector_iter(connector, &conn_iter) {
1771 
1772 		/* use first connected connector for the physical dimensions */
1773 		if (connector->status == connector_status_connected) {
1774 			info->var.width = connector->display_info.width_mm;
1775 			info->var.height = connector->display_info.height_mm;
1776 			break;
1777 		}
1778 	}
1779 	drm_connector_list_iter_end(&conn_iter);
1780 
1781 	switch (sw_rotations) {
1782 	case DRM_MODE_ROTATE_0:
1783 		info->fbcon_rotate_hint = FB_ROTATE_UR;
1784 		break;
1785 	case DRM_MODE_ROTATE_90:
1786 		info->fbcon_rotate_hint = FB_ROTATE_CCW;
1787 		break;
1788 	case DRM_MODE_ROTATE_180:
1789 		info->fbcon_rotate_hint = FB_ROTATE_UD;
1790 		break;
1791 	case DRM_MODE_ROTATE_270:
1792 		info->fbcon_rotate_hint = FB_ROTATE_CW;
1793 		break;
1794 	default:
1795 		/*
1796 		 * Multiple bits are set / multiple rotations requested
1797 		 * fbcon cannot handle separate rotation settings per
1798 		 * output, so fallback to unrotated.
1799 		 */
1800 		info->fbcon_rotate_hint = FB_ROTATE_UR;
1801 	}
1802 }
1803 
1804 /* Note: Drops fb_helper->lock before returning. */
1805 static int
1806 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper)
1807 {
1808 	struct drm_device *dev = fb_helper->dev;
1809 	struct fb_info *info;
1810 	unsigned int width, height;
1811 	int ret;
1812 
1813 	width = dev->mode_config.max_width;
1814 	height = dev->mode_config.max_height;
1815 
1816 	drm_client_modeset_probe(&fb_helper->client, width, height);
1817 	ret = drm_fb_helper_single_fb_probe(fb_helper);
1818 	if (ret < 0) {
1819 		if (ret == -EAGAIN) {
1820 			fb_helper->deferred_setup = true;
1821 			ret = 0;
1822 		}
1823 		mutex_unlock(&fb_helper->lock);
1824 
1825 		return ret;
1826 	}
1827 	drm_setup_crtcs_fb(fb_helper);
1828 
1829 	fb_helper->deferred_setup = false;
1830 
1831 	info = fb_helper->info;
1832 	info->var.pixclock = 0;
1833 
1834 	/* Need to drop locks to avoid recursive deadlock in
1835 	 * register_framebuffer. This is ok because the only thing left to do is
1836 	 * register the fbdev emulation instance in kernel_fb_helper_list. */
1837 	mutex_unlock(&fb_helper->lock);
1838 
1839 	ret = register_framebuffer(info);
1840 	if (ret < 0)
1841 		return ret;
1842 
1843 	drm_info(dev, "fb%d: %s frame buffer device\n",
1844 		 info->node, info->fix.id);
1845 
1846 	mutex_lock(&kernel_fb_helper_lock);
1847 	if (list_empty(&kernel_fb_helper_list))
1848 		register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1849 
1850 	list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1851 	mutex_unlock(&kernel_fb_helper_lock);
1852 
1853 	return 0;
1854 }
1855 
1856 /**
1857  * drm_fb_helper_initial_config - setup a sane initial connector configuration
1858  * @fb_helper: fb_helper device struct
1859  *
1860  * Scans the CRTCs and connectors and tries to put together an initial setup.
1861  * At the moment, this is a cloned configuration across all heads with
1862  * a new framebuffer object as the backing store.
1863  *
1864  * Note that this also registers the fbdev and so allows userspace to call into
1865  * the driver through the fbdev interfaces.
1866  *
1867  * This function will call down into the &drm_driver.fbdev_probe callback
1868  * to let the driver allocate and initialize the fbdev info structure and the
1869  * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided
1870  * as a helper to setup simple default values for the fbdev info structure.
1871  *
1872  * HANG DEBUGGING:
1873  *
1874  * When you have fbcon support built-in or already loaded, this function will do
1875  * a full modeset to setup the fbdev console. Due to locking misdesign in the
1876  * VT/fbdev subsystem that entire modeset sequence has to be done while holding
1877  * console_lock. Until console_unlock is called no dmesg lines will be sent out
1878  * to consoles, not even serial console. This means when your driver crashes,
1879  * you will see absolutely nothing else but a system stuck in this function,
1880  * with no further output. Any kind of printk() you place within your own driver
1881  * or in the drm core modeset code will also never show up.
1882  *
1883  * Standard debug practice is to run the fbcon setup without taking the
1884  * console_lock as a hack, to be able to see backtraces and crashes on the
1885  * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
1886  * cmdline option.
1887  *
1888  * The other option is to just disable fbdev emulation since very likely the
1889  * first modeset from userspace will crash in the same way, and is even easier
1890  * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
1891  * kernel cmdline option.
1892  *
1893  * RETURNS:
1894  * Zero if everything went ok, nonzero otherwise.
1895  */
1896 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper)
1897 {
1898 	int ret;
1899 
1900 	if (!drm_fbdev_emulation)
1901 		return 0;
1902 
1903 	mutex_lock(&fb_helper->lock);
1904 	ret = __drm_fb_helper_initial_config_and_unlock(fb_helper);
1905 
1906 	return ret;
1907 }
1908 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1909 
1910 /**
1911  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1912  *                               probing all the outputs attached to the fb
1913  * @fb_helper: driver-allocated fbdev helper, can be NULL
1914  *
1915  * Scan the connectors attached to the fb_helper and try to put together a
1916  * setup after notification of a change in output configuration.
1917  *
1918  * Called at runtime, takes the mode config locks to be able to check/change the
1919  * modeset configuration. Must be run from process context (which usually means
1920  * either the output polling work or a work item launched from the driver's
1921  * hotplug interrupt).
1922  *
1923  * Note that drivers may call this even before calling
1924  * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
1925  * for a race-free fbcon setup and will make sure that the fbdev emulation will
1926  * not miss any hotplug events.
1927  *
1928  * RETURNS:
1929  * 0 on success and a non-zero error code otherwise.
1930  */
1931 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1932 {
1933 	int err = 0;
1934 
1935 	if (!drm_fbdev_emulation || !fb_helper)
1936 		return 0;
1937 
1938 	mutex_lock(&fb_helper->lock);
1939 	if (fb_helper->deferred_setup) {
1940 		err = __drm_fb_helper_initial_config_and_unlock(fb_helper);
1941 		return err;
1942 	}
1943 
1944 	if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) {
1945 		fb_helper->delayed_hotplug = true;
1946 		mutex_unlock(&fb_helper->lock);
1947 		return err;
1948 	}
1949 
1950 	drm_master_internal_release(fb_helper->dev);
1951 
1952 	drm_dbg_kms(fb_helper->dev, "\n");
1953 
1954 	drm_client_modeset_probe(&fb_helper->client, fb_helper->fb->width, fb_helper->fb->height);
1955 	drm_setup_crtcs_fb(fb_helper);
1956 	mutex_unlock(&fb_helper->lock);
1957 
1958 	drm_fb_helper_set_par(fb_helper->info);
1959 
1960 	return 0;
1961 }
1962 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1963 
1964 /**
1965  * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
1966  * @dev: DRM device
1967  *
1968  * This function is obsolete. Call drm_fb_helper_restore_fbdev_mode_unlocked()
1969  * instead.
1970  */
1971 void drm_fb_helper_lastclose(struct drm_device *dev)
1972 {
1973 	drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
1974 }
1975 EXPORT_SYMBOL(drm_fb_helper_lastclose);
1976