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