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