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