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