xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/drm_fb_helper.c (revision 05a8a89779f7a8fa32ffc0b531ab288244c60e3c)
1 /*	$NetBSD: drm_fb_helper.c,v 1.26 2021/12/19 12:08:18 riastradh Exp $	*/
2 
3 /*
4  * Copyright (c) 2006-2009 Red Hat Inc.
5  * Copyright (c) 2006-2008 Intel Corporation
6  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
7  *
8  * DRM framebuffer helper functions
9  *
10  * Permission to use, copy, modify, distribute, and sell this software and its
11  * documentation for any purpose is hereby granted without fee, provided that
12  * the above copyright notice appear in all copies and that both that copyright
13  * notice and this permission notice appear in supporting documentation, and
14  * that the name of the copyright holders not be used in advertising or
15  * publicity pertaining to distribution of the software without specific,
16  * written prior permission.  The copyright holders make no representations
17  * about the suitability of this software for any purpose.  It is provided "as
18  * is" without express or implied warranty.
19  *
20  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
22  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
23  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
24  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
25  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
26  * OF THIS SOFTWARE.
27  *
28  * Authors:
29  *      Dave Airlie <airlied@linux.ie>
30  *      Jesse Barnes <jesse.barnes@intel.com>
31  */
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: drm_fb_helper.c,v 1.26 2021/12/19 12:08:18 riastradh Exp $");
34 
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 
37 #include <linux/console.h>
38 #include <linux/dma-buf.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/slab.h>
42 #include <linux/sysrq.h>
43 #include <linux/vmalloc.h>
44 
45 #include <drm/drm_atomic.h>
46 #include <drm/drm_crtc.h>
47 #include <drm/drm_crtc_helper.h>
48 #include <drm/drm_drv.h>
49 #include <drm/drm_fb_helper.h>
50 #include <drm/drm_fourcc.h>
51 #include <drm/drm_print.h>
52 #include <drm/drm_vblank.h>
53 
54 #include "drm_crtc_helper_internal.h"
55 #include "drm_internal.h"
56 
57 #include <linux/nbsd-namespace.h>
58 
59 static bool drm_fbdev_emulation = true;
60 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
61 MODULE_PARM_DESC(fbdev_emulation,
62 		 "Enable legacy fbdev emulation [default=true]");
63 
64 static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
65 module_param(drm_fbdev_overalloc, int, 0444);
66 MODULE_PARM_DESC(drm_fbdev_overalloc,
67 		 "Overallocation of the fbdev buffer (%) [default="
68 		 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
69 
70 /*
71  * In order to keep user-space compatibility, we want in certain use-cases
72  * to keep leaking the fbdev physical address to the user-space program
73  * handling the fbdev buffer.
74  * This is a bad habit essentially kept into closed source opengl driver
75  * that should really be moved into open-source upstream projects instead
76  * of using legacy physical addresses in user space to communicate with
77  * other out-of-tree kernel modules.
78  *
79  * This module_param *should* be removed as soon as possible and be
80  * considered as a broken and legacy behaviour from a modern fbdev device.
81  */
82 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
83 static bool drm_leak_fbdev_smem = false;
84 module_param_unsafe(drm_leak_fbdev_smem, bool, 0600);
85 MODULE_PARM_DESC(drm_leak_fbdev_smem,
86 		 "Allow unsafe leaking fbdev physical smem address [default=false]");
87 #endif
88 
89 #ifdef __NetBSD__		/* XXX LIST_HEAD means something else */
90 static struct list_head kernel_fb_helper_list =
91     LIST_HEAD_INIT(kernel_fb_helper_list);
92 #define	kernel_fb_helper_lock	drm_kernel_fb_helper_lock
93 struct mutex kernel_fb_helper_lock;
94 #else
95 static LIST_HEAD(kernel_fb_helper_list);
96 static DEFINE_MUTEX(kernel_fb_helper_lock);
97 #endif
98 
99 /**
100  * DOC: fbdev helpers
101  *
102  * The fb helper functions are useful to provide an fbdev on top of a drm kernel
103  * mode setting driver. They can be used mostly independently from the crtc
104  * helper functions used by many drivers to implement the kernel mode setting
105  * interfaces.
106  *
107  * Drivers that support a dumb buffer with a virtual address and mmap support,
108  * should try out the generic fbdev emulation using drm_fbdev_generic_setup().
109  * It will automatically set up deferred I/O if the driver requires a shadow
110  * buffer.
111  *
112  * At runtime drivers should restore the fbdev console by using
113  * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback.
114  * They should also notify the fb helper code from updates to the output
115  * configuration by using drm_fb_helper_output_poll_changed() as their
116  * &drm_mode_config_funcs.output_poll_changed callback.
117  *
118  * For suspend/resume consider using drm_mode_config_helper_suspend() and
119  * drm_mode_config_helper_resume() which takes care of fbdev as well.
120  *
121  * All other functions exported by the fb helper library can be used to
122  * implement the fbdev driver interface by the driver.
123  *
124  * It is possible, though perhaps somewhat tricky, to implement race-free
125  * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
126  * helper must be called first to initialize the minimum required to make
127  * hotplug detection work. Drivers also need to make sure to properly set up
128  * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
129  * it is safe to enable interrupts and start processing hotplug events. At the
130  * same time, drivers should initialize all modeset objects such as CRTCs,
131  * encoders and connectors. To finish up the fbdev helper initialization, the
132  * drm_fb_helper_init() function is called. To probe for all attached displays
133  * and set up an initial configuration using the detected hardware, drivers
134  * should call drm_fb_helper_initial_config().
135  *
136  * If &drm_framebuffer_funcs.dirty is set, the
137  * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
138  * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
139  * away. This worker then calls the dirty() function ensuring that it will
140  * always run in process context since the fb_*() function could be running in
141  * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
142  * callback it will also schedule dirty_work with the damage collected from the
143  * mmap page writes.
144  *
145  * Deferred I/O is not compatible with SHMEM. Such drivers should request an
146  * fbdev shadow buffer and call drm_fbdev_generic_setup() instead.
147  */
148 
drm_fb_helper_restore_lut_atomic(struct drm_crtc * crtc)149 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
150 {
151 	uint16_t *r_base, *g_base, *b_base;
152 
153 	if (crtc->funcs->gamma_set == NULL)
154 		return;
155 
156 	r_base = crtc->gamma_store;
157 	g_base = r_base + crtc->gamma_size;
158 	b_base = g_base + crtc->gamma_size;
159 
160 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
161 			       crtc->gamma_size, NULL);
162 }
163 
164 /**
165  * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
166  * @info: fbdev registered by the helper
167  */
drm_fb_helper_debug_enter_fb(struct drm_fb_helper * helper)168 int drm_fb_helper_debug_enter_fb(struct drm_fb_helper *helper)
169 {
170 	const struct drm_crtc_helper_funcs *funcs;
171 	struct drm_mode_set *mode_set;
172 
173 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
174 		drm_client_for_each_modeset_unlocked(mode_set, &helper->client) {
175 			if (!mode_set->crtc->enabled)
176 				continue;
177 
178 			if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
179 				continue;
180 
181 			funcs =	mode_set->crtc->helper_private;
182 			if (funcs->mode_set_base_atomic == NULL)
183 				continue;
184 
185 			funcs->mode_set_base_atomic(mode_set->crtc,
186 						    mode_set->fb,
187 						    mode_set->x,
188 						    mode_set->y,
189 						    ENTER_ATOMIC_MODE_SET);
190 		}
191 	}
192 
193 	return 0;
194 }
195 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
196 
197 /**
198  * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
199  * @info: fbdev registered by the helper
200  */
drm_fb_helper_debug_leave_fb(struct drm_fb_helper * helper)201 int drm_fb_helper_debug_leave_fb(struct drm_fb_helper *helper)
202 {
203 	struct drm_client_dev *client = &helper->client;
204 	struct drm_device *dev = helper->dev;
205 	struct drm_crtc *crtc;
206 	const struct drm_crtc_helper_funcs *funcs;
207 	struct drm_mode_set *mode_set;
208 	struct drm_framebuffer *fb;
209 
210 	drm_client_for_each_modeset_unlocked(mode_set, client) {
211 		crtc = mode_set->crtc;
212 		if (drm_drv_uses_atomic_modeset(crtc->dev))
213 			continue;
214 
215 		funcs = crtc->helper_private;
216 		fb = crtc->primary->fb;
217 
218 		if (!crtc->enabled)
219 			continue;
220 
221 		if (!fb) {
222 			drm_err(dev, "no fb to restore?\n");
223 			continue;
224 		}
225 
226 		if (funcs->mode_set_base_atomic == NULL)
227 			continue;
228 
229 		drm_fb_helper_restore_lut_atomic(mode_set->crtc);
230 		funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
231 					    crtc->y, LEAVE_ATOMIC_MODE_SET);
232 	}
233 
234 	return 0;
235 }
236 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
237 
238 /**
239  * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
240  * @fb_helper: driver-allocated fbdev helper, can be NULL
241  *
242  * This should be called from driver's drm &drm_driver.lastclose callback
243  * when implementing an fbcon on top of kms using this helper. This ensures that
244  * the user isn't greeted with a black screen when e.g. X dies.
245  *
246  * RETURNS:
247  * Zero if everything went ok, negative error code otherwise.
248  */
drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper * fb_helper)249 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
250 {
251 	bool do_delayed;
252 	int ret;
253 
254 	if (!drm_fbdev_emulation || !fb_helper)
255 		return -ENODEV;
256 
257 	if (READ_ONCE(fb_helper->deferred_setup))
258 		return 0;
259 
260 	mutex_lock(&fb_helper->lock);
261 	/*
262 	 * TODO:
263 	 * We should bail out here if there is a master by dropping _force.
264 	 * Currently these igt tests fail if we do that:
265 	 * - kms_fbcon_fbt@psr
266 	 * - kms_fbcon_fbt@psr-suspend
267 	 *
268 	 * So first these tests need to be fixed so they drop master or don't
269 	 * have an fd open.
270 	 */
271 	ret = drm_client_modeset_commit_force(&fb_helper->client);
272 
273 	do_delayed = fb_helper->delayed_hotplug;
274 	if (do_delayed)
275 		fb_helper->delayed_hotplug = false;
276 	mutex_unlock(&fb_helper->lock);
277 
278 	if (do_delayed)
279 		drm_fb_helper_hotplug_event(fb_helper);
280 
281 	return ret;
282 }
283 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
284 
285 #ifdef CONFIG_MAGIC_SYSRQ
286 /*
287  * restore fbcon display for all kms driver's using this helper, used for sysrq
288  * and panic handling.
289  */
drm_fb_helper_force_kernel_mode(void)290 static bool drm_fb_helper_force_kernel_mode(void)
291 {
292 	bool ret, error = false;
293 	struct drm_fb_helper *helper;
294 
295 	if (list_empty(&kernel_fb_helper_list))
296 		return false;
297 
298 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
299 		struct drm_device *dev = helper->dev;
300 
301 		if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
302 			continue;
303 
304 		mutex_lock(&helper->lock);
305 		ret = drm_client_modeset_commit_force(&helper->client);
306 		if (ret)
307 			error = true;
308 		mutex_unlock(&helper->lock);
309 	}
310 	return error;
311 }
312 
drm_fb_helper_restore_work_fn(struct work_struct * ignored)313 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
314 {
315 	bool ret;
316 
317 	ret = drm_fb_helper_force_kernel_mode();
318 	if (ret == true)
319 		DRM_ERROR("Failed to restore crtc configuration\n");
320 }
321 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
322 
drm_fb_helper_sysrq(int dummy1)323 static void drm_fb_helper_sysrq(int dummy1)
324 {
325 	schedule_work(&drm_fb_helper_restore_work);
326 }
327 
328 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
329 	.handler = drm_fb_helper_sysrq,
330 	.help_msg = "force-fb(V)",
331 	.action_msg = "Restore framebuffer console",
332 };
333 #else
334 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
335 #endif
336 
337 #ifndef __NetBSD__		/* XXX fb info */
drm_fb_helper_dpms(struct fb_info * info,int dpms_mode)338 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
339 {
340 	struct drm_fb_helper *fb_helper = info->par;
341 
342 	mutex_lock(&fb_helper->lock);
343 	drm_client_modeset_dpms(&fb_helper->client, dpms_mode);
344 	mutex_unlock(&fb_helper->lock);
345 }
346 
347 /**
348  * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
349  * @blank: desired blanking state
350  * @info: fbdev registered by the helper
351  */
drm_fb_helper_blank(int blank,struct fb_info * info)352 int drm_fb_helper_blank(int blank, struct fb_info *info)
353 {
354 	if (oops_in_progress)
355 		return -EBUSY;
356 
357 	switch (blank) {
358 	/* Display: On; HSync: On, VSync: On */
359 	case FB_BLANK_UNBLANK:
360 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
361 		break;
362 	/* Display: Off; HSync: On, VSync: On */
363 	case FB_BLANK_NORMAL:
364 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
365 		break;
366 	/* Display: Off; HSync: Off, VSync: On */
367 	case FB_BLANK_HSYNC_SUSPEND:
368 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
369 		break;
370 	/* Display: Off; HSync: On, VSync: Off */
371 	case FB_BLANK_VSYNC_SUSPEND:
372 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
373 		break;
374 	/* Display: Off; HSync: Off, VSync: Off */
375 	case FB_BLANK_POWERDOWN:
376 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
377 		break;
378 	}
379 	return 0;
380 }
381 EXPORT_SYMBOL(drm_fb_helper_blank);
382 #endif
383 
drm_fb_helper_resume_worker(struct work_struct * work)384 static void drm_fb_helper_resume_worker(struct work_struct *work)
385 {
386 #ifndef __NetBSD__		/* XXX fb suspend */
387 	struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
388 						    resume_work);
389 
390 	console_lock();
391 	fb_set_suspend(helper->fbdev, 0);
392 	console_unlock();
393 #endif
394 }
395 
drm_fb_helper_dirty_blit_real(struct drm_fb_helper * fb_helper,struct drm_clip_rect * clip)396 static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper,
397 					  struct drm_clip_rect *clip)
398 {
399 #ifndef __NetBSD__		/* XXX fb dirty */
400 	struct drm_framebuffer *fb = fb_helper->fb;
401 	unsigned int cpp = fb->format->cpp[0];
402 	size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp;
403 	void *src = (char *)fb_helper->fbdev->screen_buffer + offset;
404 	void *dst = (char *)fb_helper->buffer->vaddr + offset;
405 	size_t len = (clip->x2 - clip->x1) * cpp;
406 	unsigned int y;
407 
408 	for (y = clip->y1; y < clip->y2; y++) {
409 		memcpy(dst, src, len);
410 		src = (char *)src + fb->pitches[0];
411 		dst = (char *)dst + fb->pitches[0];
412 	}
413 #endif
414 }
415 
drm_fb_helper_dirty_work(struct work_struct * work)416 static void drm_fb_helper_dirty_work(struct work_struct *work)
417 {
418 	struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
419 						    dirty_work);
420 	struct drm_clip_rect *clip = &helper->dirty_clip;
421 	struct drm_clip_rect clip_copy;
422 	unsigned long flags;
423 	void *vaddr;
424 
425 	spin_lock_irqsave(&helper->dirty_lock, flags);
426 	clip_copy = *clip;
427 	clip->x1 = clip->y1 = ~0;
428 	clip->x2 = clip->y2 = 0;
429 	spin_unlock_irqrestore(&helper->dirty_lock, flags);
430 
431 	/* call dirty callback only when it has been really touched */
432 	if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) {
433 
434 		/* Generic fbdev uses a shadow buffer */
435 		if (helper->buffer) {
436 			vaddr = drm_client_buffer_vmap(helper->buffer);
437 			if (IS_ERR(vaddr))
438 				return;
439 			drm_fb_helper_dirty_blit_real(helper, &clip_copy);
440 		}
441 		if (helper->fb->funcs->dirty)
442 			helper->fb->funcs->dirty(helper->fb, NULL, 0, 0,
443 						 &clip_copy, 1);
444 
445 		if (helper->buffer)
446 			drm_client_buffer_vunmap(helper->buffer);
447 	}
448 }
449 
450 /**
451  * drm_fb_helper_prepare - setup a drm_fb_helper structure
452  * @dev: DRM device
453  * @helper: driver-allocated fbdev helper structure to set up
454  * @funcs: pointer to structure of functions associate with this helper
455  *
456  * Sets up the bare minimum to make the framebuffer helper usable. This is
457  * useful to implement race-free initialization of the polling helpers.
458  */
drm_fb_helper_prepare(struct drm_device * dev,struct drm_fb_helper * helper,const struct drm_fb_helper_funcs * funcs)459 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
460 			   const struct drm_fb_helper_funcs *funcs)
461 {
462 	INIT_LIST_HEAD(&helper->kernel_fb_list);
463 	spin_lock_init(&helper->dirty_lock);
464 	INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
465 	INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
466 	helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
467 	mutex_init(&helper->lock);
468 	helper->funcs = funcs;
469 	helper->dev = dev;
470 }
471 EXPORT_SYMBOL(drm_fb_helper_prepare);
472 
473 /**
474  * drm_fb_helper_init - initialize a &struct drm_fb_helper
475  * @dev: drm device
476  * @fb_helper: driver-allocated fbdev helper structure to initialize
477  * @max_conn_count: max connector count (not used)
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,int max_conn_count)489 int drm_fb_helper_init(struct drm_device *dev,
490 		       struct drm_fb_helper *fb_helper,
491 		       int max_conn_count)
492 {
493 	int ret;
494 
495 	if (!drm_fbdev_emulation) {
496 		dev->fb_helper = fb_helper;
497 		return 0;
498 	}
499 
500 	/*
501 	 * If this is not the generic fbdev client, initialize a drm_client
502 	 * without callbacks so we can use the modesets.
503 	 */
504 	if (!fb_helper->client.funcs) {
505 		ret = drm_client_init(dev, &fb_helper->client, "drm_fb_helper", NULL);
506 		if (ret) {
507 			/*
508 			 * XXX Undo what drm_fb_helper_prepare did
509 			 * since the caller will not be going through
510 			 * drm_fb_helper_fini.
511 			 */
512 			mutex_destroy(&fb_helper->lock);
513 			spin_lock_destroy(&fb_helper->dirty_lock);
514 			return ret;
515 		}
516 	}
517 
518 	dev->fb_helper = fb_helper;
519 
520 	return 0;
521 }
522 EXPORT_SYMBOL(drm_fb_helper_init);
523 
524 /**
525  * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
526  * @fb_helper: driver-allocated fbdev helper
527  *
528  * A helper to alloc fb_info and the members cmap and apertures. Called
529  * by the driver within the fb_probe fb_helper callback function. Drivers do not
530  * need to release the allocated fb_info structure themselves, this is
531  * automatically done when calling drm_fb_helper_fini().
532  *
533  * RETURNS:
534  * fb_info pointer if things went okay, pointer containing error code
535  * otherwise
536  */
537 #ifndef __NetBSD__		/* XXX fb info */
drm_fb_helper_alloc_fbi(struct drm_fb_helper * fb_helper)538 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
539 {
540 	struct device *dev = fb_helper->dev->dev;
541 	struct fb_info *info;
542 	int ret;
543 
544 	info = framebuffer_alloc(0, dev);
545 	if (!info)
546 		return ERR_PTR(-ENOMEM);
547 
548 	ret = fb_alloc_cmap(&info->cmap, 256, 0);
549 	if (ret)
550 		goto err_release;
551 
552 	info->apertures = alloc_apertures(1);
553 	if (!info->apertures) {
554 		ret = -ENOMEM;
555 		goto err_free_cmap;
556 	}
557 
558 	fb_helper->fbdev = info;
559 	info->skip_vt_switch = true;
560 
561 	return info;
562 
563 err_free_cmap:
564 	fb_dealloc_cmap(&info->cmap);
565 err_release:
566 	framebuffer_release(info);
567 	return ERR_PTR(ret);
568 }
569 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
570 #endif
571 
572 /**
573  * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
574  * @fb_helper: driver-allocated fbdev helper, can be NULL
575  *
576  * A wrapper around unregister_framebuffer, to release the fb_info
577  * framebuffer device. This must be called before releasing all resources for
578  * @fb_helper by calling drm_fb_helper_fini().
579  */
drm_fb_helper_unregister_fbi(struct drm_fb_helper * fb_helper)580 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
581 {
582 #ifdef __NetBSD__
583 	/* XXX errno NetBSD->Linux */
584 	int ret = -config_detach(fb_helper->fbdev, DETACH_FORCE);
585 	if (ret)
586 		DRM_ERROR("failed to detach drm framebuffer: %d\n", ret);
587 #else
588 	if (fb_helper && fb_helper->fbdev)
589 		unregister_framebuffer(fb_helper->fbdev);
590 #endif
591 }
592 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
593 
594 /**
595  * drm_fb_helper_fini - finialize a &struct drm_fb_helper
596  * @fb_helper: driver-allocated fbdev helper, can be NULL
597  *
598  * This cleans up all remaining resources associated with @fb_helper.
599  */
drm_fb_helper_fini(struct drm_fb_helper * fb_helper)600 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
601 {
602 #ifndef __NetBSD__
603 	struct fb_info *info;
604 #endif
605 
606 	if (!fb_helper)
607 		return;
608 
609 	fb_helper->dev->fb_helper = NULL;
610 
611 	if (!drm_fbdev_emulation)
612 		return;
613 
614 	cancel_work_sync(&fb_helper->resume_work);
615 	cancel_work_sync(&fb_helper->dirty_work);
616 
617 #ifndef __NetBSD__
618 	info = fb_helper->fbdev;
619 	if (info) {
620 		if (info->cmap.len)
621 			fb_dealloc_cmap(&info->cmap);
622 		framebuffer_release(info);
623 	}
624 #endif
625 	fb_helper->fbdev = NULL;
626 
627 	mutex_lock(&kernel_fb_helper_lock);
628 	if (!list_empty(&fb_helper->kernel_fb_list)) {
629 		list_del(&fb_helper->kernel_fb_list);
630 		if (list_empty(&kernel_fb_helper_list))
631 			unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
632 	}
633 	mutex_unlock(&kernel_fb_helper_lock);
634 
635 	mutex_destroy(&fb_helper->lock);
636 	spin_lock_destroy(&fb_helper->dirty_lock);
637 
638 	if (!fb_helper->client.funcs)
639 		drm_client_release(&fb_helper->client);
640 }
641 EXPORT_SYMBOL(drm_fb_helper_fini);
642 
643 #ifndef __NetBSD__		/* XXX fb dirty */
644 
drm_fbdev_use_shadow_fb(struct drm_fb_helper * fb_helper)645 static bool drm_fbdev_use_shadow_fb(struct drm_fb_helper *fb_helper)
646 {
647 	struct drm_device *dev = fb_helper->dev;
648 	struct drm_framebuffer *fb = fb_helper->fb;
649 
650 	return dev->mode_config.prefer_shadow_fbdev ||
651 	       dev->mode_config.prefer_shadow ||
652 	       fb->funcs->dirty;
653 }
654 
drm_fb_helper_dirty(struct fb_info * info,u32 x,u32 y,u32 width,u32 height)655 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
656 				u32 width, u32 height)
657 {
658 	struct drm_fb_helper *helper = info->par;
659 	struct drm_clip_rect *clip = &helper->dirty_clip;
660 	unsigned long flags;
661 
662 	if (!drm_fbdev_use_shadow_fb(helper))
663 		return;
664 
665 	spin_lock_irqsave(&helper->dirty_lock, flags);
666 	clip->x1 = min_t(u32, clip->x1, x);
667 	clip->y1 = min_t(u32, clip->y1, y);
668 	clip->x2 = max_t(u32, clip->x2, x + width);
669 	clip->y2 = max_t(u32, clip->y2, y + height);
670 	spin_unlock_irqrestore(&helper->dirty_lock, flags);
671 
672 	schedule_work(&helper->dirty_work);
673 }
674 
675 #endif
676 
677 #ifndef __NetBSD__		/* XXX fb deferred */
678 
679 /**
680  * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
681  * @info: fb_info struct pointer
682  * @pagelist: list of dirty mmap framebuffer pages
683  *
684  * This function is used as the &fb_deferred_io.deferred_io
685  * callback function for flushing the fbdev mmap writes.
686  */
drm_fb_helper_deferred_io(struct fb_info * info,struct list_head * pagelist)687 void drm_fb_helper_deferred_io(struct fb_info *info,
688 			       struct list_head *pagelist)
689 {
690 	unsigned long start, end, min, max;
691 	struct page *page;
692 	u32 y1, y2;
693 
694 	min = ULONG_MAX;
695 	max = 0;
696 	list_for_each_entry(page, pagelist, lru) {
697 		start = page->index << PAGE_SHIFT;
698 		end = start + PAGE_SIZE - 1;
699 		min = min(min, start);
700 		max = max(max, end);
701 	}
702 
703 	if (min < max) {
704 		y1 = min / info->fix.line_length;
705 		y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
706 			   info->var.yres);
707 		drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
708 	}
709 }
710 EXPORT_SYMBOL(drm_fb_helper_deferred_io);
711 
712 /**
713  * drm_fb_helper_sys_read - wrapper around fb_sys_read
714  * @info: fb_info struct pointer
715  * @buf: userspace buffer to read from framebuffer memory
716  * @count: number of bytes to read from framebuffer memory
717  * @ppos: read offset within framebuffer memory
718  *
719  * A wrapper around fb_sys_read implemented by fbdev core
720  */
drm_fb_helper_sys_read(struct fb_info * info,char __user * buf,size_t count,loff_t * ppos)721 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
722 			       size_t count, loff_t *ppos)
723 {
724 	return fb_sys_read(info, buf, count, ppos);
725 }
726 EXPORT_SYMBOL(drm_fb_helper_sys_read);
727 
728 /**
729  * drm_fb_helper_sys_write - wrapper around fb_sys_write
730  * @info: fb_info struct pointer
731  * @buf: userspace buffer to write to framebuffer memory
732  * @count: number of bytes to write to framebuffer memory
733  * @ppos: write offset within framebuffer memory
734  *
735  * A wrapper around fb_sys_write implemented by fbdev core
736  */
drm_fb_helper_sys_write(struct fb_info * info,const char __user * buf,size_t count,loff_t * ppos)737 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
738 				size_t count, loff_t *ppos)
739 {
740 	ssize_t ret;
741 
742 	ret = fb_sys_write(info, buf, count, ppos);
743 	if (ret > 0)
744 		drm_fb_helper_dirty(info, 0, 0, info->var.xres,
745 				    info->var.yres);
746 
747 	return ret;
748 }
749 EXPORT_SYMBOL(drm_fb_helper_sys_write);
750 
751 /**
752  * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
753  * @info: fbdev registered by the helper
754  * @rect: info about rectangle to fill
755  *
756  * A wrapper around sys_fillrect implemented by fbdev core
757  */
drm_fb_helper_sys_fillrect(struct fb_info * info,const struct fb_fillrect * rect)758 void drm_fb_helper_sys_fillrect(struct fb_info *info,
759 				const struct fb_fillrect *rect)
760 {
761 	sys_fillrect(info, rect);
762 	drm_fb_helper_dirty(info, rect->dx, rect->dy,
763 			    rect->width, rect->height);
764 }
765 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
766 
767 /**
768  * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
769  * @info: fbdev registered by the helper
770  * @area: info about area to copy
771  *
772  * A wrapper around sys_copyarea implemented by fbdev core
773  */
drm_fb_helper_sys_copyarea(struct fb_info * info,const struct fb_copyarea * area)774 void drm_fb_helper_sys_copyarea(struct fb_info *info,
775 				const struct fb_copyarea *area)
776 {
777 	sys_copyarea(info, area);
778 	drm_fb_helper_dirty(info, area->dx, area->dy,
779 			    area->width, area->height);
780 }
781 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
782 
783 /**
784  * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
785  * @info: fbdev registered by the helper
786  * @image: info about image to blit
787  *
788  * A wrapper around sys_imageblit implemented by fbdev core
789  */
drm_fb_helper_sys_imageblit(struct fb_info * info,const struct fb_image * image)790 void drm_fb_helper_sys_imageblit(struct fb_info *info,
791 				 const struct fb_image *image)
792 {
793 	sys_imageblit(info, image);
794 	drm_fb_helper_dirty(info, image->dx, image->dy,
795 			    image->width, image->height);
796 }
797 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
798 
799 /**
800  * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
801  * @info: fbdev registered by the helper
802  * @rect: info about rectangle to fill
803  *
804  * A wrapper around cfb_fillrect implemented by fbdev core
805  */
drm_fb_helper_cfb_fillrect(struct fb_info * info,const struct fb_fillrect * rect)806 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
807 				const struct fb_fillrect *rect)
808 {
809 	cfb_fillrect(info, rect);
810 	drm_fb_helper_dirty(info, rect->dx, rect->dy,
811 			    rect->width, rect->height);
812 }
813 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
814 
815 /**
816  * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
817  * @info: fbdev registered by the helper
818  * @area: info about area to copy
819  *
820  * A wrapper around cfb_copyarea implemented by fbdev core
821  */
drm_fb_helper_cfb_copyarea(struct fb_info * info,const struct fb_copyarea * area)822 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
823 				const struct fb_copyarea *area)
824 {
825 	cfb_copyarea(info, area);
826 	drm_fb_helper_dirty(info, area->dx, area->dy,
827 			    area->width, area->height);
828 }
829 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
830 
831 /**
832  * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
833  * @info: fbdev registered by the helper
834  * @image: info about image to blit
835  *
836  * A wrapper around cfb_imageblit implemented by fbdev core
837  */
drm_fb_helper_cfb_imageblit(struct fb_info * info,const struct fb_image * image)838 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
839 				 const struct fb_image *image)
840 {
841 	cfb_imageblit(info, image);
842 	drm_fb_helper_dirty(info, image->dx, image->dy,
843 			    image->width, image->height);
844 }
845 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
846 #endif	/* __NetBSD__ */
847 
848 #ifdef __NetBSD__		/* XXX fb info */
drm_fb_helper_set_suspend(struct drm_fb_helper * fb_helper,bool suspend)849 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
850 {
851 }
852 #else
853 /**
854  * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
855  * @fb_helper: driver-allocated fbdev helper, can be NULL
856  * @suspend: whether to suspend or resume
857  *
858  * A wrapper around fb_set_suspend implemented by fbdev core.
859  * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
860  * the lock yourself
861  */
drm_fb_helper_set_suspend(struct drm_fb_helper * fb_helper,bool suspend)862 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
863 {
864 	if (fb_helper && fb_helper->fbdev)
865 		fb_set_suspend(fb_helper->fbdev, suspend);
866 }
867 EXPORT_SYMBOL(drm_fb_helper_set_suspend);
868 #endif
869 
870 /**
871  * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
872  *                                      takes the console lock
873  * @fb_helper: driver-allocated fbdev helper, can be NULL
874  * @suspend: whether to suspend or resume
875  *
876  * A wrapper around fb_set_suspend() that takes the console lock. If the lock
877  * isn't available on resume, a worker is tasked with waiting for the lock
878  * to become available. The console lock can be pretty contented on resume
879  * due to all the printk activity.
880  *
881  * This function can be called multiple times with the same state since
882  * &fb_info.state is checked to see if fbdev is running or not before locking.
883  *
884  * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
885  */
drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper * fb_helper,bool suspend)886 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
887 					bool suspend)
888 {
889 	if (!fb_helper || !fb_helper->fbdev)
890 		return;
891 
892 	/* make sure there's no pending/ongoing resume */
893 	flush_work(&fb_helper->resume_work);
894 
895 #ifndef __NetBSD__		/* XXX fb suspend */
896 	if (suspend) {
897 		if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
898 			return;
899 
900 		console_lock();
901 
902 	} else {
903 		if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
904 			return;
905 
906 		if (!console_trylock()) {
907 			schedule_work(&fb_helper->resume_work);
908 			return;
909 		}
910 	}
911 
912 	fb_set_suspend(fb_helper->fbdev, suspend);
913 	console_unlock();
914 #endif
915 }
916 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
917 
918 #ifndef __NetBSD__
919 
setcmap_pseudo_palette(struct fb_cmap * cmap,struct fb_info * info)920 static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
921 {
922 	u32 *palette = (u32 *)info->pseudo_palette;
923 	int i;
924 
925 	if (cmap->start + cmap->len > 16)
926 		return -EINVAL;
927 
928 	for (i = 0; i < cmap->len; ++i) {
929 		u16 red = cmap->red[i];
930 		u16 green = cmap->green[i];
931 		u16 blue = cmap->blue[i];
932 		u32 value;
933 
934 		red >>= 16 - info->var.red.length;
935 		green >>= 16 - info->var.green.length;
936 		blue >>= 16 - info->var.blue.length;
937 		value = (red << info->var.red.offset) |
938 			(green << info->var.green.offset) |
939 			(blue << info->var.blue.offset);
940 		if (info->var.transp.length > 0) {
941 			u32 mask = (1 << info->var.transp.length) - 1;
942 
943 			mask <<= info->var.transp.offset;
944 			value |= mask;
945 		}
946 		palette[cmap->start + i] = value;
947 	}
948 
949 	return 0;
950 }
951 
setcmap_legacy(struct fb_cmap * cmap,struct fb_info * info)952 static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
953 {
954 	struct drm_fb_helper *fb_helper = info->par;
955 	struct drm_mode_set *modeset;
956 	struct drm_crtc *crtc;
957 	u16 *r, *g, *b;
958 	int ret = 0;
959 
960 	drm_modeset_lock_all(fb_helper->dev);
961 	drm_client_for_each_modeset(modeset, &fb_helper->client) {
962 		crtc = modeset->crtc;
963 		if (!crtc->funcs->gamma_set || !crtc->gamma_size)
964 			return -EINVAL;
965 
966 		if (cmap->start + cmap->len > crtc->gamma_size)
967 			return -EINVAL;
968 
969 		r = crtc->gamma_store;
970 		g = r + crtc->gamma_size;
971 		b = g + crtc->gamma_size;
972 
973 		memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
974 		memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
975 		memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
976 
977 		ret = crtc->funcs->gamma_set(crtc, r, g, b,
978 					     crtc->gamma_size, NULL);
979 		if (ret)
980 			return ret;
981 	}
982 	drm_modeset_unlock_all(fb_helper->dev);
983 
984 	return ret;
985 }
986 
setcmap_new_gamma_lut(struct drm_crtc * crtc,struct fb_cmap * cmap)987 static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
988 						       struct fb_cmap *cmap)
989 {
990 	struct drm_device *dev = crtc->dev;
991 	struct drm_property_blob *gamma_lut;
992 	struct drm_color_lut *lut;
993 	int size = crtc->gamma_size;
994 	int i;
995 
996 	if (!size || cmap->start + cmap->len > size)
997 		return ERR_PTR(-EINVAL);
998 
999 	gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
1000 	if (IS_ERR(gamma_lut))
1001 		return gamma_lut;
1002 
1003 	lut = gamma_lut->data;
1004 	if (cmap->start || cmap->len != size) {
1005 		u16 *r = crtc->gamma_store;
1006 		u16 *g = r + crtc->gamma_size;
1007 		u16 *b = g + crtc->gamma_size;
1008 
1009 		for (i = 0; i < cmap->start; i++) {
1010 			lut[i].red = r[i];
1011 			lut[i].green = g[i];
1012 			lut[i].blue = b[i];
1013 		}
1014 		for (i = cmap->start + cmap->len; i < size; i++) {
1015 			lut[i].red = r[i];
1016 			lut[i].green = g[i];
1017 			lut[i].blue = b[i];
1018 		}
1019 	}
1020 
1021 	for (i = 0; i < cmap->len; i++) {
1022 		lut[cmap->start + i].red = cmap->red[i];
1023 		lut[cmap->start + i].green = cmap->green[i];
1024 		lut[cmap->start + i].blue = cmap->blue[i];
1025 	}
1026 
1027 	return gamma_lut;
1028 }
1029 
setcmap_atomic(struct fb_cmap * cmap,struct fb_info * info)1030 static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
1031 {
1032 	struct drm_fb_helper *fb_helper = info->par;
1033 	struct drm_device *dev = fb_helper->dev;
1034 	struct drm_property_blob *gamma_lut = NULL;
1035 	struct drm_modeset_acquire_ctx ctx;
1036 	struct drm_crtc_state *crtc_state;
1037 	struct drm_atomic_state *state;
1038 	struct drm_mode_set *modeset;
1039 	struct drm_crtc *crtc;
1040 	u16 *r, *g, *b;
1041 	bool replaced;
1042 	int ret = 0;
1043 
1044 	drm_modeset_acquire_init(&ctx, 0);
1045 
1046 	state = drm_atomic_state_alloc(dev);
1047 	if (!state) {
1048 		ret = -ENOMEM;
1049 		goto out_ctx;
1050 	}
1051 
1052 	state->acquire_ctx = &ctx;
1053 retry:
1054 	drm_client_for_each_modeset(modeset, &fb_helper->client) {
1055 		crtc = modeset->crtc;
1056 
1057 		if (!gamma_lut)
1058 			gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
1059 		if (IS_ERR(gamma_lut)) {
1060 			ret = PTR_ERR(gamma_lut);
1061 			gamma_lut = NULL;
1062 			goto out_state;
1063 		}
1064 
1065 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
1066 		if (IS_ERR(crtc_state)) {
1067 			ret = PTR_ERR(crtc_state);
1068 			goto out_state;
1069 		}
1070 
1071 		replaced  = drm_property_replace_blob(&crtc_state->degamma_lut,
1072 						      NULL);
1073 		replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
1074 		replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
1075 						      gamma_lut);
1076 		crtc_state->color_mgmt_changed |= replaced;
1077 	}
1078 
1079 	ret = drm_atomic_commit(state);
1080 	if (ret)
1081 		goto out_state;
1082 
1083 	drm_client_for_each_modeset(modeset, &fb_helper->client) {
1084 		crtc = modeset->crtc;
1085 
1086 		r = crtc->gamma_store;
1087 		g = r + crtc->gamma_size;
1088 		b = g + crtc->gamma_size;
1089 
1090 		memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1091 		memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1092 		memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1093 	}
1094 
1095 out_state:
1096 	if (ret == -EDEADLK)
1097 		goto backoff;
1098 
1099 	drm_property_blob_put(gamma_lut);
1100 	drm_atomic_state_put(state);
1101 out_ctx:
1102 	drm_modeset_drop_locks(&ctx);
1103 	drm_modeset_acquire_fini(&ctx);
1104 
1105 	return ret;
1106 
1107 backoff:
1108 	drm_atomic_state_clear(state);
1109 	drm_modeset_backoff(&ctx);
1110 	goto retry;
1111 }
1112 
1113 /**
1114  * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1115  * @cmap: cmap to set
1116  * @info: fbdev registered by the helper
1117  */
drm_fb_helper_setcmap(struct fb_cmap * cmap,struct fb_info * info)1118 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1119 {
1120 	struct drm_fb_helper *fb_helper = info->par;
1121 	struct drm_device *dev = fb_helper->dev;
1122 	int ret;
1123 
1124 	if (oops_in_progress)
1125 		return -EBUSY;
1126 
1127 	mutex_lock(&fb_helper->lock);
1128 
1129 	if (!drm_master_internal_acquire(dev)) {
1130 		ret = -EBUSY;
1131 		goto unlock;
1132 	}
1133 
1134 	mutex_lock(&fb_helper->client.modeset_mutex);
1135 	if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1136 		ret = setcmap_pseudo_palette(cmap, info);
1137 	else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1138 		ret = setcmap_atomic(cmap, info);
1139 	else
1140 		ret = setcmap_legacy(cmap, info);
1141 	mutex_unlock(&fb_helper->client.modeset_mutex);
1142 
1143 	drm_master_internal_release(dev);
1144 unlock:
1145 	mutex_unlock(&fb_helper->lock);
1146 
1147 	return ret;
1148 }
1149 EXPORT_SYMBOL(drm_fb_helper_setcmap);
1150 
1151 /**
1152  * drm_fb_helper_ioctl - legacy ioctl implementation
1153  * @info: fbdev registered by the helper
1154  * @cmd: ioctl command
1155  * @arg: ioctl argument
1156  *
1157  * A helper to implement the standard fbdev ioctl. Only
1158  * FBIO_WAITFORVSYNC is implemented for now.
1159  */
drm_fb_helper_ioctl(struct fb_info * info,unsigned int cmd,unsigned long arg)1160 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1161 			unsigned long arg)
1162 {
1163 	struct drm_fb_helper *fb_helper = info->par;
1164 	struct drm_device *dev = fb_helper->dev;
1165 	struct drm_crtc *crtc;
1166 	int ret = 0;
1167 
1168 	mutex_lock(&fb_helper->lock);
1169 	if (!drm_master_internal_acquire(dev)) {
1170 		ret = -EBUSY;
1171 		goto unlock;
1172 	}
1173 
1174 	switch (cmd) {
1175 	case FBIO_WAITFORVSYNC:
1176 		/*
1177 		 * Only consider the first CRTC.
1178 		 *
1179 		 * This ioctl is supposed to take the CRTC number as
1180 		 * an argument, but in fbdev times, what that number
1181 		 * was supposed to be was quite unclear, different
1182 		 * drivers were passing that argument differently
1183 		 * (some by reference, some by value), and most of the
1184 		 * userspace applications were just hardcoding 0 as an
1185 		 * argument.
1186 		 *
1187 		 * The first CRTC should be the integrated panel on
1188 		 * most drivers, so this is the best choice we can
1189 		 * make. If we're not smart enough here, one should
1190 		 * just consider switch the userspace to KMS.
1191 		 */
1192 		crtc = fb_helper->client.modesets[0].crtc;
1193 
1194 		/*
1195 		 * Only wait for a vblank event if the CRTC is
1196 		 * enabled, otherwise just don't do anythintg,
1197 		 * not even report an error.
1198 		 */
1199 		ret = drm_crtc_vblank_get(crtc);
1200 		if (!ret) {
1201 			drm_crtc_wait_one_vblank(crtc);
1202 			drm_crtc_vblank_put(crtc);
1203 		}
1204 
1205 		ret = 0;
1206 		break;
1207 	default:
1208 		ret = -ENOTTY;
1209 	}
1210 
1211 	drm_master_internal_release(dev);
1212 unlock:
1213 	mutex_unlock(&fb_helper->lock);
1214 	return ret;
1215 }
1216 EXPORT_SYMBOL(drm_fb_helper_ioctl);
1217 
drm_fb_pixel_format_equal(const struct fb_var_screeninfo * var_1,const struct fb_var_screeninfo * var_2)1218 static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1,
1219 				      const struct fb_var_screeninfo *var_2)
1220 {
1221 	return var_1->bits_per_pixel == var_2->bits_per_pixel &&
1222 	       var_1->grayscale == var_2->grayscale &&
1223 	       var_1->red.offset == var_2->red.offset &&
1224 	       var_1->red.length == var_2->red.length &&
1225 	       var_1->red.msb_right == var_2->red.msb_right &&
1226 	       var_1->green.offset == var_2->green.offset &&
1227 	       var_1->green.length == var_2->green.length &&
1228 	       var_1->green.msb_right == var_2->green.msb_right &&
1229 	       var_1->blue.offset == var_2->blue.offset &&
1230 	       var_1->blue.length == var_2->blue.length &&
1231 	       var_1->blue.msb_right == var_2->blue.msb_right &&
1232 	       var_1->transp.offset == var_2->transp.offset &&
1233 	       var_1->transp.length == var_2->transp.length &&
1234 	       var_1->transp.msb_right == var_2->transp.msb_right;
1235 }
1236 
drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo * var,u8 depth)1237 static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo *var,
1238 					 u8 depth)
1239 {
1240 	switch (depth) {
1241 	case 8:
1242 		var->red.offset = 0;
1243 		var->green.offset = 0;
1244 		var->blue.offset = 0;
1245 		var->red.length = 8; /* 8bit DAC */
1246 		var->green.length = 8;
1247 		var->blue.length = 8;
1248 		var->transp.offset = 0;
1249 		var->transp.length = 0;
1250 		break;
1251 	case 15:
1252 		var->red.offset = 10;
1253 		var->green.offset = 5;
1254 		var->blue.offset = 0;
1255 		var->red.length = 5;
1256 		var->green.length = 5;
1257 		var->blue.length = 5;
1258 		var->transp.offset = 15;
1259 		var->transp.length = 1;
1260 		break;
1261 	case 16:
1262 		var->red.offset = 11;
1263 		var->green.offset = 5;
1264 		var->blue.offset = 0;
1265 		var->red.length = 5;
1266 		var->green.length = 6;
1267 		var->blue.length = 5;
1268 		var->transp.offset = 0;
1269 		break;
1270 	case 24:
1271 		var->red.offset = 16;
1272 		var->green.offset = 8;
1273 		var->blue.offset = 0;
1274 		var->red.length = 8;
1275 		var->green.length = 8;
1276 		var->blue.length = 8;
1277 		var->transp.offset = 0;
1278 		var->transp.length = 0;
1279 		break;
1280 	case 32:
1281 		var->red.offset = 16;
1282 		var->green.offset = 8;
1283 		var->blue.offset = 0;
1284 		var->red.length = 8;
1285 		var->green.length = 8;
1286 		var->blue.length = 8;
1287 		var->transp.offset = 24;
1288 		var->transp.length = 8;
1289 		break;
1290 	default:
1291 		break;
1292 	}
1293 }
1294 
1295 /**
1296  * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
1297  * @var: screeninfo to check
1298  * @info: fbdev registered by the helper
1299  */
drm_fb_helper_check_var(struct fb_var_screeninfo * var,struct fb_info * info)1300 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1301 			    struct fb_info *info)
1302 {
1303 	struct drm_fb_helper *fb_helper = info->par;
1304 	struct drm_framebuffer *fb = fb_helper->fb;
1305 	struct drm_device *dev = fb_helper->dev;
1306 
1307 	if (in_dbg_master())
1308 		return -EINVAL;
1309 
1310 	if (var->pixclock != 0) {
1311 		drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n");
1312 		var->pixclock = 0;
1313 	}
1314 
1315 	if ((drm_format_info_block_width(fb->format, 0) > 1) ||
1316 	    (drm_format_info_block_height(fb->format, 0) > 1))
1317 		return -EINVAL;
1318 
1319 	/*
1320 	 * Changes struct fb_var_screeninfo are currently not pushed back
1321 	 * to KMS, hence fail if different settings are requested.
1322 	 */
1323 	if (var->bits_per_pixel > fb->format->cpp[0] * 8 ||
1324 	    var->xres > fb->width || var->yres > fb->height ||
1325 	    var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1326 		drm_dbg_kms(dev, "fb requested width/height/bpp can't fit in current fb "
1327 			  "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1328 			  var->xres, var->yres, var->bits_per_pixel,
1329 			  var->xres_virtual, var->yres_virtual,
1330 			  fb->width, fb->height, fb->format->cpp[0] * 8);
1331 		return -EINVAL;
1332 	}
1333 
1334 	/*
1335 	 * Workaround for SDL 1.2, which is known to be setting all pixel format
1336 	 * fields values to zero in some cases. We treat this situation as a
1337 	 * kind of "use some reasonable autodetected values".
1338 	 */
1339 	if (!var->red.offset     && !var->green.offset    &&
1340 	    !var->blue.offset    && !var->transp.offset   &&
1341 	    !var->red.length     && !var->green.length    &&
1342 	    !var->blue.length    && !var->transp.length   &&
1343 	    !var->red.msb_right  && !var->green.msb_right &&
1344 	    !var->blue.msb_right && !var->transp.msb_right) {
1345 		drm_fb_helper_fill_pixel_fmt(var, fb->format->depth);
1346 	}
1347 
1348 	/*
1349 	 * Likewise, bits_per_pixel should be rounded up to a supported value.
1350 	 */
1351 	var->bits_per_pixel = fb->format->cpp[0] * 8;
1352 
1353 	/*
1354 	 * drm fbdev emulation doesn't support changing the pixel format at all,
1355 	 * so reject all pixel format changing requests.
1356 	 */
1357 	if (!drm_fb_pixel_format_equal(var, &info->var)) {
1358 		drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel format\n");
1359 		return -EINVAL;
1360 	}
1361 
1362 	return 0;
1363 }
1364 EXPORT_SYMBOL(drm_fb_helper_check_var);
1365 
1366 /**
1367  * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1368  * @info: fbdev registered by the helper
1369  *
1370  * This will let fbcon do the mode init and is called at initialization time by
1371  * the fbdev core when registering the driver, and later on through the hotplug
1372  * callback.
1373  */
drm_fb_helper_set_par(struct fb_info * info)1374 int drm_fb_helper_set_par(struct fb_info *info)
1375 {
1376 	struct drm_fb_helper *fb_helper = info->par;
1377 	struct fb_var_screeninfo *var = &info->var;
1378 
1379 	if (oops_in_progress)
1380 		return -EBUSY;
1381 
1382 	if (var->pixclock != 0) {
1383 		drm_err(fb_helper->dev, "PIXEL CLOCK SET\n");
1384 		return -EINVAL;
1385 	}
1386 
1387 	drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1388 
1389 	return 0;
1390 }
1391 EXPORT_SYMBOL(drm_fb_helper_set_par);
1392 
pan_set(struct drm_fb_helper * fb_helper,int x,int y)1393 static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
1394 {
1395 	struct drm_mode_set *mode_set;
1396 
1397 	mutex_lock(&fb_helper->client.modeset_mutex);
1398 	drm_client_for_each_modeset(mode_set, &fb_helper->client) {
1399 		mode_set->x = x;
1400 		mode_set->y = y;
1401 	}
1402 	mutex_unlock(&fb_helper->client.modeset_mutex);
1403 }
1404 
pan_display_atomic(struct fb_var_screeninfo * var,struct fb_info * info)1405 static int pan_display_atomic(struct fb_var_screeninfo *var,
1406 			      struct fb_info *info)
1407 {
1408 	struct drm_fb_helper *fb_helper = info->par;
1409 	int ret;
1410 
1411 	pan_set(fb_helper, var->xoffset, var->yoffset);
1412 
1413 	ret = drm_client_modeset_commit_force(&fb_helper->client);
1414 	if (!ret) {
1415 		info->var.xoffset = var->xoffset;
1416 		info->var.yoffset = var->yoffset;
1417 	} else
1418 		pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
1419 
1420 	return ret;
1421 }
1422 
pan_display_legacy(struct fb_var_screeninfo * var,struct fb_info * info)1423 static int pan_display_legacy(struct fb_var_screeninfo *var,
1424 			      struct fb_info *info)
1425 {
1426 	struct drm_fb_helper *fb_helper = info->par;
1427 	struct drm_client_dev *client = &fb_helper->client;
1428 	struct drm_mode_set *modeset;
1429 	int ret = 0;
1430 
1431 	mutex_lock(&client->modeset_mutex);
1432 	drm_modeset_lock_all(fb_helper->dev);
1433 	drm_client_for_each_modeset(modeset, client) {
1434 		modeset->x = var->xoffset;
1435 		modeset->y = var->yoffset;
1436 
1437 		if (modeset->num_connectors) {
1438 			ret = drm_mode_set_config_internal(modeset);
1439 			if (!ret) {
1440 				info->var.xoffset = var->xoffset;
1441 				info->var.yoffset = var->yoffset;
1442 			}
1443 		}
1444 	}
1445 	drm_modeset_unlock_all(fb_helper->dev);
1446 	mutex_unlock(&client->modeset_mutex);
1447 
1448 	return ret;
1449 }
1450 
1451 /**
1452  * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1453  * @var: updated screen information
1454  * @info: fbdev registered by the helper
1455  */
drm_fb_helper_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)1456 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1457 			      struct fb_info *info)
1458 {
1459 	struct drm_fb_helper *fb_helper = info->par;
1460 	struct drm_device *dev = fb_helper->dev;
1461 	int ret;
1462 
1463 	if (oops_in_progress)
1464 		return -EBUSY;
1465 
1466 	mutex_lock(&fb_helper->lock);
1467 	if (!drm_master_internal_acquire(dev)) {
1468 		ret = -EBUSY;
1469 		goto unlock;
1470 	}
1471 
1472 	if (drm_drv_uses_atomic_modeset(dev))
1473 		ret = pan_display_atomic(var, info);
1474 	else
1475 		ret = pan_display_legacy(var, info);
1476 
1477 	drm_master_internal_release(dev);
1478 unlock:
1479 	mutex_unlock(&fb_helper->lock);
1480 
1481 	return ret;
1482 }
1483 EXPORT_SYMBOL(drm_fb_helper_pan_display);
1484 
1485 #endif
1486 
1487 /*
1488  * Allocates the backing storage and sets up the fbdev info structure through
1489  * the ->fb_probe callback.
1490  */
drm_fb_helper_single_fb_probe(struct drm_fb_helper * fb_helper,int preferred_bpp)1491 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1492 					 int preferred_bpp)
1493 {
1494 	struct drm_client_dev *client = &fb_helper->client;
1495 	struct drm_device *dev = fb_helper->dev;
1496 	int ret = 0;
1497 	int crtc_count = 0;
1498 	struct drm_connector_list_iter conn_iter;
1499 	struct drm_fb_helper_surface_size sizes;
1500 	struct drm_connector *connector;
1501 	struct drm_mode_set *mode_set;
1502 	int best_depth = 0;
1503 
1504 	memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1505 	sizes.surface_depth = 24;
1506 	sizes.surface_bpp = 32;
1507 	sizes.fb_width = (u32)-1;
1508 	sizes.fb_height = (u32)-1;
1509 
1510 	/*
1511 	 * If driver picks 8 or 16 by default use that for both depth/bpp
1512 	 * to begin with
1513 	 */
1514 	if (preferred_bpp != sizes.surface_bpp)
1515 		sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1516 
1517 	drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
1518 	drm_client_for_each_connector_iter(connector, &conn_iter) {
1519 		struct drm_cmdline_mode *cmdline_mode;
1520 
1521 		cmdline_mode = &connector->cmdline_mode;
1522 
1523 		if (cmdline_mode->bpp_specified) {
1524 			switch (cmdline_mode->bpp) {
1525 			case 8:
1526 				sizes.surface_depth = sizes.surface_bpp = 8;
1527 				break;
1528 			case 15:
1529 				sizes.surface_depth = 15;
1530 				sizes.surface_bpp = 16;
1531 				break;
1532 			case 16:
1533 				sizes.surface_depth = sizes.surface_bpp = 16;
1534 				break;
1535 			case 24:
1536 				sizes.surface_depth = sizes.surface_bpp = 24;
1537 				break;
1538 			case 32:
1539 				sizes.surface_depth = 24;
1540 				sizes.surface_bpp = 32;
1541 				break;
1542 			}
1543 			break;
1544 		}
1545 	}
1546 	drm_connector_list_iter_end(&conn_iter);
1547 
1548 	/*
1549 	 * If we run into a situation where, for example, the primary plane
1550 	 * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth
1551 	 * 16) we need to scale down the depth of the sizes we request.
1552 	 */
1553 	mutex_lock(&client->modeset_mutex);
1554 	drm_client_for_each_modeset(mode_set, client) {
1555 		struct drm_crtc *crtc = mode_set->crtc;
1556 		struct drm_plane *plane = crtc->primary;
1557 		int j;
1558 
1559 		drm_dbg_kms(dev, "test CRTC %u primary plane\n", drm_crtc_index(crtc));
1560 
1561 		for (j = 0; j < plane->format_count; j++) {
1562 			const struct drm_format_info *fmt;
1563 
1564 			fmt = drm_format_info(plane->format_types[j]);
1565 
1566 			/*
1567 			 * Do not consider YUV or other complicated formats
1568 			 * for framebuffers. This means only legacy formats
1569 			 * are supported (fmt->depth is a legacy field) but
1570 			 * the framebuffer emulation can only deal with such
1571 			 * formats, specifically RGB/BGA formats.
1572 			 */
1573 			if (fmt->depth == 0)
1574 				continue;
1575 
1576 			/* We found a perfect fit, great */
1577 			if (fmt->depth == sizes.surface_depth) {
1578 				best_depth = fmt->depth;
1579 				break;
1580 			}
1581 
1582 			/* Skip depths above what we're looking for */
1583 			if (fmt->depth > sizes.surface_depth)
1584 				continue;
1585 
1586 			/* Best depth found so far */
1587 			if (fmt->depth > best_depth)
1588 				best_depth = fmt->depth;
1589 		}
1590 	}
1591 	if (sizes.surface_depth != best_depth && best_depth) {
1592 		drm_info(dev, "requested bpp %d, scaled depth down to %d",
1593 			 sizes.surface_bpp, best_depth);
1594 		sizes.surface_depth = best_depth;
1595 	}
1596 
1597 	/* first up get a count of crtcs now in use and new min/maxes width/heights */
1598 	crtc_count = 0;
1599 	drm_client_for_each_modeset(mode_set, client) {
1600 		struct drm_display_mode *desired_mode;
1601 		int x, y, j;
1602 		/* in case of tile group, are we the last tile vert or horiz?
1603 		 * If no tile group you are always the last one both vertically
1604 		 * and horizontally
1605 		 */
1606 		bool lastv = true, lasth = true;
1607 
1608 		desired_mode = mode_set->mode;
1609 
1610 		if (!desired_mode)
1611 			continue;
1612 
1613 		crtc_count++;
1614 
1615 		x = mode_set->x;
1616 		y = mode_set->y;
1617 
1618 		sizes.surface_width  = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1619 		sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
1620 
1621 		for (j = 0; j < mode_set->num_connectors; j++) {
1622 			struct drm_connector *connector = mode_set->connectors[j];
1623 
1624 			if (connector->has_tile &&
1625 			    desired_mode->hdisplay == connector->tile_h_size &&
1626 			    desired_mode->vdisplay == connector->tile_v_size) {
1627 				lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1628 				lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1629 				/* cloning to multiple tiles is just crazy-talk, so: */
1630 				break;
1631 			}
1632 		}
1633 
1634 		if (lasth)
1635 			sizes.fb_width  = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1636 		if (lastv)
1637 			sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
1638 	}
1639 	mutex_unlock(&client->modeset_mutex);
1640 
1641 	if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1642 		drm_info(dev, "Cannot find any crtc or sizes\n");
1643 
1644 		/* First time: disable all crtc's.. */
1645 		if (!fb_helper->deferred_setup)
1646 			drm_client_modeset_commit(client);
1647 		return -EAGAIN;
1648 	}
1649 
1650 	/* Handle our overallocation */
1651 	sizes.surface_height *= drm_fbdev_overalloc;
1652 	sizes.surface_height /= 100;
1653 
1654 	/* push down into drivers */
1655 	ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1656 	if (ret < 0)
1657 		return ret;
1658 
1659 	strcpy(fb_helper->fb->comm, "[fbcon]");
1660 	return 0;
1661 }
1662 
1663 #ifndef __NetBSD__		/* XXX fb info */
drm_fb_helper_fill_fix(struct fb_info * info,uint32_t pitch,uint32_t depth)1664 static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1665 				   uint32_t depth)
1666 {
1667 	info->fix.type = FB_TYPE_PACKED_PIXELS;
1668 	info->fix.visual = depth == 8 ? 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 
1687 	WARN_ON((drm_format_info_block_width(fb->format, 0) > 1) ||
1688 		(drm_format_info_block_height(fb->format, 0) > 1));
1689 	info->pseudo_palette = fb_helper->pseudo_palette;
1690 	info->var.xres_virtual = fb->width;
1691 	info->var.yres_virtual = fb->height;
1692 	info->var.bits_per_pixel = fb->format->cpp[0] * 8;
1693 	info->var.accel_flags = FB_ACCELF_TEXT;
1694 	info->var.xoffset = 0;
1695 	info->var.yoffset = 0;
1696 	info->var.activate = FB_ACTIVATE_NOW;
1697 
1698 	drm_fb_helper_fill_pixel_fmt(&info->var, fb->format->depth);
1699 
1700 	info->var.xres = fb_width;
1701 	info->var.yres = fb_height;
1702 }
1703 
1704 /**
1705  * drm_fb_helper_fill_info - initializes fbdev information
1706  * @info: fbdev instance to set up
1707  * @fb_helper: fb helper instance to use as template
1708  * @sizes: describes fbdev size and scanout surface size
1709  *
1710  * Sets up the variable and fixed fbdev metainformation from the given fb helper
1711  * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
1712  *
1713  * Drivers should call this (or their equivalent setup code) from their
1714  * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1715  * backing storage framebuffer.
1716  */
drm_fb_helper_fill_info(struct fb_info * info,struct drm_fb_helper * fb_helper,struct drm_fb_helper_surface_size * sizes)1717 void drm_fb_helper_fill_info(struct fb_info *info,
1718 			     struct drm_fb_helper *fb_helper,
1719 			     struct drm_fb_helper_surface_size *sizes)
1720 {
1721 	struct drm_framebuffer *fb = fb_helper->fb;
1722 
1723 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
1724 	drm_fb_helper_fill_var(info, fb_helper,
1725 			       sizes->fb_width, sizes->fb_height);
1726 
1727 	info->par = fb_helper;
1728 	snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb",
1729 		 fb_helper->dev->driver->name);
1730 
1731 }
1732 EXPORT_SYMBOL(drm_fb_helper_fill_info);
1733 #endif
1734 
1735 /*
1736  * This is a continuation of drm_setup_crtcs() that sets up anything related
1737  * to the framebuffer. During initialization, drm_setup_crtcs() is called before
1738  * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
1739  * So, any setup that touches those fields needs to be done here instead of in
1740  * drm_setup_crtcs().
1741  */
drm_setup_crtcs_fb(struct drm_fb_helper * fb_helper)1742 static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
1743 {
1744 	struct drm_client_dev *client = &fb_helper->client;
1745 #ifndef __NetBSD__		/* XXX fb info */
1746 	struct drm_connector_list_iter conn_iter;
1747 	struct fb_info *info = fb_helper->fbdev;
1748 	struct drm_connector *connector;
1749 #endif
1750 	unsigned int rotation, sw_rotations = 0;
1751 	struct drm_mode_set *modeset;
1752 
1753 	mutex_lock(&client->modeset_mutex);
1754 	drm_client_for_each_modeset(modeset, client) {
1755 		if (!modeset->num_connectors)
1756 			continue;
1757 
1758 		modeset->fb = fb_helper->fb;
1759 
1760 		if (drm_client_rotation(modeset, &rotation))
1761 			/* Rotating in hardware, fbcon should not rotate */
1762 			sw_rotations |= DRM_MODE_ROTATE_0;
1763 		else
1764 			sw_rotations |= rotation;
1765 	}
1766 	mutex_unlock(&client->modeset_mutex);
1767 
1768 #ifndef __NetBSD__		/* XXX fb info */
1769 	drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
1770 	drm_client_for_each_connector_iter(connector, &conn_iter) {
1771 
1772 		/* use first connected connector for the physical dimensions */
1773 		if (connector->status == connector_status_connected) {
1774 			info->var.width = connector->display_info.width_mm;
1775 			info->var.height = connector->display_info.height_mm;
1776 			break;
1777 		}
1778 	}
1779 	drm_connector_list_iter_end(&conn_iter);
1780 
1781 	switch (sw_rotations) {
1782 	case DRM_MODE_ROTATE_0:
1783 		info->fbcon_rotate_hint = FB_ROTATE_UR;
1784 		break;
1785 	case DRM_MODE_ROTATE_90:
1786 		info->fbcon_rotate_hint = FB_ROTATE_CCW;
1787 		break;
1788 	case DRM_MODE_ROTATE_180:
1789 		info->fbcon_rotate_hint = FB_ROTATE_UD;
1790 		break;
1791 	case DRM_MODE_ROTATE_270:
1792 		info->fbcon_rotate_hint = FB_ROTATE_CW;
1793 		break;
1794 	default:
1795 		/*
1796 		 * Multiple bits are set / multiple rotations requested
1797 		 * fbcon cannot handle separate rotation settings per
1798 		 * output, so fallback to unrotated.
1799 		 */
1800 		info->fbcon_rotate_hint = FB_ROTATE_UR;
1801 	}
1802 #endif
1803 }
1804 
1805 /* Note: Drops fb_helper->lock before returning. */
1806 static int
__drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper * fb_helper,int bpp_sel)1807 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
1808 					  int bpp_sel)
1809 {
1810 	struct drm_device *dev = fb_helper->dev;
1811 #ifndef __NetBSD__		/* XXX fb info */
1812 	struct fb_info *info;
1813 #endif
1814 	unsigned int width, height;
1815 	int ret;
1816 
1817 	width = dev->mode_config.max_width;
1818 	height = dev->mode_config.max_height;
1819 
1820 	drm_client_modeset_probe(&fb_helper->client, width, height);
1821 	ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1822 	if (ret < 0) {
1823 		if (ret == -EAGAIN) {
1824 			fb_helper->preferred_bpp = bpp_sel;
1825 			fb_helper->deferred_setup = true;
1826 			ret = 0;
1827 		}
1828 		mutex_unlock(&fb_helper->lock);
1829 
1830 		return ret;
1831 	}
1832 	drm_setup_crtcs_fb(fb_helper);
1833 
1834 	fb_helper->deferred_setup = false;
1835 
1836 #ifndef __NetBSD__		/* XXX fb info */
1837 	info = fb_helper->fbdev;
1838 	info->var.pixclock = 0;
1839 	/* Shamelessly allow physical address leaking to userspace */
1840 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
1841 	if (!drm_leak_fbdev_smem)
1842 #endif
1843 		/* don't leak any physical addresses to userspace */
1844 		info->flags |= FBINFO_HIDE_SMEM_START;
1845 #endif
1846 
1847 	/* Need to drop locks to avoid recursive deadlock in
1848 	 * register_framebuffer. This is ok because the only thing left to do is
1849 	 * register the fbdev emulation instance in kernel_fb_helper_list. */
1850 	mutex_unlock(&fb_helper->lock);
1851 
1852 #ifndef __NetBSD__		/* XXX fb info */
1853 	ret = register_framebuffer(info);
1854 	if (ret < 0)
1855 		return ret;
1856 
1857 	dev_info(dev->dev, "fb%d: %s frame buffer device\n",
1858 		 info->node, info->fix.id);
1859 #endif
1860 
1861 	mutex_lock(&kernel_fb_helper_lock);
1862 	if (list_empty(&kernel_fb_helper_list))
1863 		register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1864 
1865 	list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1866 	mutex_unlock(&kernel_fb_helper_lock);
1867 
1868 	return 0;
1869 }
1870 
1871 /**
1872  * drm_fb_helper_initial_config - setup a sane initial connector configuration
1873  * @fb_helper: fb_helper device struct
1874  * @bpp_sel: bpp value to use for the framebuffer configuration
1875  *
1876  * Scans the CRTCs and connectors and tries to put together an initial setup.
1877  * At the moment, this is a cloned configuration across all heads with
1878  * a new framebuffer object as the backing store.
1879  *
1880  * Note that this also registers the fbdev and so allows userspace to call into
1881  * the driver through the fbdev interfaces.
1882  *
1883  * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
1884  * to let the driver allocate and initialize the fbdev info structure and the
1885  * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided
1886  * as a helper to setup simple default values for the fbdev info structure.
1887  *
1888  * HANG DEBUGGING:
1889  *
1890  * When you have fbcon support built-in or already loaded, this function will do
1891  * a full modeset to setup the fbdev console. Due to locking misdesign in the
1892  * VT/fbdev subsystem that entire modeset sequence has to be done while holding
1893  * console_lock. Until console_unlock is called no dmesg lines will be sent out
1894  * to consoles, not even serial console. This means when your driver crashes,
1895  * you will see absolutely nothing else but a system stuck in this function,
1896  * with no further output. Any kind of printk() you place within your own driver
1897  * or in the drm core modeset code will also never show up.
1898  *
1899  * Standard debug practice is to run the fbcon setup without taking the
1900  * console_lock as a hack, to be able to see backtraces and crashes on the
1901  * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
1902  * cmdline option.
1903  *
1904  * The other option is to just disable fbdev emulation since very likely the
1905  * first modeset from userspace will crash in the same way, and is even easier
1906  * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
1907  * kernel cmdline option.
1908  *
1909  * RETURNS:
1910  * Zero if everything went ok, nonzero otherwise.
1911  */
drm_fb_helper_initial_config(struct drm_fb_helper * fb_helper,int bpp_sel)1912 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1913 {
1914 	int ret;
1915 
1916 	if (!drm_fbdev_emulation)
1917 		return 0;
1918 
1919 	mutex_lock(&fb_helper->lock);
1920 	ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
1921 
1922 	return ret;
1923 }
1924 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1925 
1926 /**
1927  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1928  *                               probing all the outputs attached to the fb
1929  * @fb_helper: driver-allocated fbdev helper, can be NULL
1930  *
1931  * Scan the connectors attached to the fb_helper and try to put together a
1932  * setup after notification of a change in output configuration.
1933  *
1934  * Called at runtime, takes the mode config locks to be able to check/change the
1935  * modeset configuration. Must be run from process context (which usually means
1936  * either the output polling work or a work item launched from the driver's
1937  * hotplug interrupt).
1938  *
1939  * Note that drivers may call this even before calling
1940  * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
1941  * for a race-free fbcon setup and will make sure that the fbdev emulation will
1942  * not miss any hotplug events.
1943  *
1944  * RETURNS:
1945  * 0 on success and a non-zero error code otherwise.
1946  */
drm_fb_helper_hotplug_event(struct drm_fb_helper * fb_helper)1947 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1948 {
1949 	int err = 0;
1950 
1951 	if (!drm_fbdev_emulation || !fb_helper)
1952 		return 0;
1953 
1954 	mutex_lock(&fb_helper->lock);
1955 	if (fb_helper->deferred_setup) {
1956 		err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
1957 				fb_helper->preferred_bpp);
1958 		return err;
1959 	}
1960 
1961 	if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) {
1962 		fb_helper->delayed_hotplug = true;
1963 		mutex_unlock(&fb_helper->lock);
1964 		return err;
1965 	}
1966 
1967 	drm_master_internal_release(fb_helper->dev);
1968 
1969 	drm_dbg_kms(fb_helper->dev, "\n");
1970 
1971 	drm_client_modeset_probe(&fb_helper->client, fb_helper->fb->width, fb_helper->fb->height);
1972 	drm_setup_crtcs_fb(fb_helper);
1973 	mutex_unlock(&fb_helper->lock);
1974 
1975 #ifdef __NetBSD__
1976 	drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1977 #else
1978 	drm_fb_helper_set_par(fb_helper->fbdev);
1979 #endif
1980 
1981 	return 0;
1982 }
1983 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1984 
1985 /**
1986  * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
1987  * @dev: DRM device
1988  *
1989  * This function can be used as the &drm_driver->lastclose callback for drivers
1990  * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
1991  */
drm_fb_helper_lastclose(struct drm_device * dev)1992 void drm_fb_helper_lastclose(struct drm_device *dev)
1993 {
1994 	drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
1995 }
1996 EXPORT_SYMBOL(drm_fb_helper_lastclose);
1997 
1998 /**
1999  * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
2000  *                                     helper for fbdev emulation
2001  * @dev: DRM device
2002  *
2003  * This function can be used as the
2004  * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
2005  * need to call drm_fb_helper_hotplug_event().
2006  */
drm_fb_helper_output_poll_changed(struct drm_device * dev)2007 void drm_fb_helper_output_poll_changed(struct drm_device *dev)
2008 {
2009 	drm_fb_helper_hotplug_event(dev->fb_helper);
2010 }
2011 EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
2012 
2013 #ifndef __NetBSD__		/* XXX fb info */
2014 
2015 /* @user: 1=userspace, 0=fbcon */
drm_fbdev_fb_open(struct fb_info * info,int user)2016 static int drm_fbdev_fb_open(struct fb_info *info, int user)
2017 {
2018 	struct drm_fb_helper *fb_helper = info->par;
2019 
2020 	/* No need to take a ref for fbcon because it unbinds on unregister */
2021 	if (user && !try_module_get(fb_helper->dev->driver->fops->owner))
2022 		return -ENODEV;
2023 
2024 	return 0;
2025 }
2026 
drm_fbdev_fb_release(struct fb_info * info,int user)2027 static int drm_fbdev_fb_release(struct fb_info *info, int user)
2028 {
2029 	struct drm_fb_helper *fb_helper = info->par;
2030 
2031 	if (user)
2032 		module_put(fb_helper->dev->driver->fops->owner);
2033 
2034 	return 0;
2035 }
2036 
2037 #endif	/* __NetBSD__ */
2038 
drm_fbdev_cleanup(struct drm_fb_helper * fb_helper)2039 static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper)
2040 {
2041 #ifndef __NetBSD__		/* XXX fb info */
2042 	struct fb_info *fbi = fb_helper->fbdev;
2043 #endif
2044 	void *shadow = NULL;
2045 
2046 	if (!fb_helper->dev)
2047 		return;
2048 
2049 #ifndef __NetBSD__		/* XXX fb info */
2050 	if (fbi && fbi->fbdefio) {
2051 		fb_deferred_io_cleanup(fbi);
2052 		shadow = fbi->screen_buffer;
2053 	}
2054 #endif
2055 
2056 	drm_fb_helper_fini(fb_helper);
2057 
2058 	vfree(shadow);
2059 
2060 	drm_client_framebuffer_delete(fb_helper->buffer);
2061 }
2062 
drm_fbdev_release(struct drm_fb_helper * fb_helper)2063 static void drm_fbdev_release(struct drm_fb_helper *fb_helper)
2064 {
2065 	drm_fbdev_cleanup(fb_helper);
2066 	drm_client_release(&fb_helper->client);
2067 	kfree(fb_helper);
2068 }
2069 
2070 
2071 #ifndef __NetBSD__		/* XXX fb info */
2072 /*
2073  * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
2074  * unregister_framebuffer() or fb_release().
2075  */
drm_fbdev_fb_destroy(struct fb_info * info)2076 static void drm_fbdev_fb_destroy(struct fb_info *info)
2077 {
2078 	drm_fbdev_release(info->par);
2079 }
2080 
drm_fbdev_fb_mmap(struct fb_info * info,struct vm_area_struct * vma)2081 static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
2082 {
2083 	struct drm_fb_helper *fb_helper = info->par;
2084 
2085 	if (fb_helper->dev->driver->gem_prime_mmap)
2086 		return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma);
2087 	else
2088 		return -ENODEV;
2089 }
2090 
2091 static const struct fb_ops drm_fbdev_fb_ops = {
2092 	.owner		= THIS_MODULE,
2093 	DRM_FB_HELPER_DEFAULT_OPS,
2094 	.fb_open	= drm_fbdev_fb_open,
2095 	.fb_release	= drm_fbdev_fb_release,
2096 	.fb_destroy	= drm_fbdev_fb_destroy,
2097 	.fb_mmap	= drm_fbdev_fb_mmap,
2098 	.fb_read	= drm_fb_helper_sys_read,
2099 	.fb_write	= drm_fb_helper_sys_write,
2100 	.fb_fillrect	= drm_fb_helper_sys_fillrect,
2101 	.fb_copyarea	= drm_fb_helper_sys_copyarea,
2102 	.fb_imageblit	= drm_fb_helper_sys_imageblit,
2103 };
2104 
2105 static struct fb_deferred_io drm_fbdev_defio = {
2106 	.delay		= HZ / 20,
2107 	.deferred_io	= drm_fb_helper_deferred_io,
2108 };
2109 
2110 #endif	/* __NetBSD__ */
2111 /*
2112  * This function uses the client API to create a framebuffer backed by a dumb buffer.
2113  *
2114  * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect,
2115  * fb_copyarea, fb_imageblit.
2116  */
drm_fb_helper_generic_probe(struct drm_fb_helper * fb_helper,struct drm_fb_helper_surface_size * sizes)2117 static int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
2118 				       struct drm_fb_helper_surface_size *sizes)
2119 {
2120 	struct drm_client_dev *client = &fb_helper->client;
2121 	struct drm_device *dev = fb_helper->dev;
2122 	struct drm_client_buffer *buffer;
2123 	struct drm_framebuffer *fb;
2124 #ifndef __NetBSD__		/* XXX fb info */
2125 	struct fb_info *fbi;
2126 #endif
2127 	u32 format;
2128 	void *vaddr;
2129 
2130 	drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n",
2131 		    sizes->surface_width, sizes->surface_height,
2132 		    sizes->surface_bpp);
2133 
2134 	format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
2135 	buffer = drm_client_framebuffer_create(client, sizes->surface_width,
2136 					       sizes->surface_height, format);
2137 	if (IS_ERR(buffer))
2138 		return PTR_ERR(buffer);
2139 
2140 	fb_helper->buffer = buffer;
2141 	fb_helper->fb = buffer->fb;
2142 	fb = buffer->fb;
2143 
2144 #ifdef __NetBSD__		/* XXX fb info */
2145 	__USE(fb);
2146 	__USE(vaddr);
2147 #else
2148 	fbi = drm_fb_helper_alloc_fbi(fb_helper);
2149 	if (IS_ERR(fbi))
2150 		return PTR_ERR(fbi);
2151 
2152 	fbi->fbops = &drm_fbdev_fb_ops;
2153 	fbi->screen_size = fb->height * fb->pitches[0];
2154 	fbi->fix.smem_len = fbi->screen_size;
2155 
2156 	drm_fb_helper_fill_info(fbi, fb_helper, sizes);
2157 
2158 	if (drm_fbdev_use_shadow_fb(fb_helper)) {
2159 		fbi->screen_buffer = vzalloc(fbi->screen_size);
2160 		if (!fbi->screen_buffer)
2161 			return -ENOMEM;
2162 
2163 		fbi->fbdefio = &drm_fbdev_defio;
2164 
2165 		fb_deferred_io_init(fbi);
2166 	} else {
2167 		/* buffer is mapped for HW framebuffer */
2168 		vaddr = drm_client_buffer_vmap(fb_helper->buffer);
2169 		if (IS_ERR(vaddr))
2170 			return PTR_ERR(vaddr);
2171 
2172 		fbi->screen_buffer = vaddr;
2173 		/* Shamelessly leak the physical address to user-space */
2174 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
2175 		if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0)
2176 			fbi->fix.smem_start =
2177 				page_to_phys(virt_to_page(fbi->screen_buffer));
2178 #endif
2179 	}
2180 #endif	/* __NetBSD__ */
2181 
2182 	return 0;
2183 }
2184 
2185 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = {
2186 	.fb_probe = drm_fb_helper_generic_probe,
2187 };
2188 
drm_fbdev_client_unregister(struct drm_client_dev * client)2189 static void drm_fbdev_client_unregister(struct drm_client_dev *client)
2190 {
2191 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
2192 
2193 #ifndef __NetBSD__		/* XXX fb info */
2194 	if (fb_helper->fbdev)
2195 		/* drm_fbdev_fb_destroy() takes care of cleanup */
2196 		drm_fb_helper_unregister_fbi(fb_helper);
2197 	else
2198 #endif
2199 		drm_fbdev_release(fb_helper);
2200 }
2201 
drm_fbdev_client_restore(struct drm_client_dev * client)2202 static int drm_fbdev_client_restore(struct drm_client_dev *client)
2203 {
2204 	drm_fb_helper_lastclose(client->dev);
2205 
2206 	return 0;
2207 }
2208 
drm_fbdev_client_hotplug(struct drm_client_dev * client)2209 static int drm_fbdev_client_hotplug(struct drm_client_dev *client)
2210 {
2211 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
2212 	struct drm_device *dev = client->dev;
2213 	int ret;
2214 
2215 	/* Setup is not retried if it has failed */
2216 	if (!fb_helper->dev && fb_helper->funcs)
2217 		return 0;
2218 
2219 	if (dev->fb_helper)
2220 		return drm_fb_helper_hotplug_event(dev->fb_helper);
2221 
2222 	if (!dev->mode_config.num_connector) {
2223 		drm_dbg_kms(dev, "No connectors found, will not create framebuffer!\n");
2224 		return 0;
2225 	}
2226 
2227 	drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs);
2228 
2229 	ret = drm_fb_helper_init(dev, fb_helper, 0);
2230 	if (ret)
2231 		goto err;
2232 
2233 	if (!drm_drv_uses_atomic_modeset(dev))
2234 		drm_helper_disable_unused_functions(dev);
2235 
2236 	ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp);
2237 	if (ret)
2238 		goto err_cleanup;
2239 
2240 	return 0;
2241 
2242 err_cleanup:
2243 	drm_fbdev_cleanup(fb_helper);
2244 err:
2245 	fb_helper->dev = NULL;
2246 	fb_helper->fbdev = NULL;
2247 
2248 	drm_err(dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret);
2249 
2250 	return ret;
2251 }
2252 
2253 static const struct drm_client_funcs drm_fbdev_client_funcs = {
2254 	.owner		= THIS_MODULE,
2255 	.unregister	= drm_fbdev_client_unregister,
2256 	.restore	= drm_fbdev_client_restore,
2257 	.hotplug	= drm_fbdev_client_hotplug,
2258 };
2259 
2260 /**
2261  * drm_fbdev_generic_setup() - Setup generic fbdev emulation
2262  * @dev: DRM device
2263  * @preferred_bpp: Preferred bits per pixel for the device.
2264  *                 @dev->mode_config.preferred_depth is used if this is zero.
2265  *
2266  * This function sets up generic fbdev emulation for drivers that supports
2267  * dumb buffers with a virtual address and that can be mmap'ed.
2268  *
2269  * Restore, hotplug events and teardown are all taken care of. Drivers that do
2270  * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
2271  * Simple drivers might use drm_mode_config_helper_suspend().
2272  *
2273  * Drivers that set the dirty callback on their framebuffer will get a shadow
2274  * fbdev buffer that is blitted onto the real buffer. This is done in order to
2275  * make deferred I/O work with all kinds of buffers. A shadow buffer can be
2276  * requested explicitly by setting struct drm_mode_config.prefer_shadow or
2277  * struct drm_mode_config.prefer_shadow_fbdev to true beforehand. This is
2278  * required to use generic fbdev emulation with SHMEM helpers.
2279  *
2280  * This function is safe to call even when there are no connectors present.
2281  * Setup will be retried on the next hotplug event.
2282  *
2283  * The fbdev is destroyed by drm_dev_unregister().
2284  *
2285  * Returns:
2286  * Zero on success or negative error code on failure.
2287  */
drm_fbdev_generic_setup(struct drm_device * dev,unsigned int preferred_bpp)2288 int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
2289 {
2290 	struct drm_fb_helper *fb_helper;
2291 	int ret;
2292 
2293 	WARN(dev->fb_helper, "fb_helper is already set!\n");
2294 
2295 	if (!drm_fbdev_emulation)
2296 		return 0;
2297 
2298 	fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
2299 	if (!fb_helper)
2300 		return -ENOMEM;
2301 
2302 	ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs);
2303 	if (ret) {
2304 		kfree(fb_helper);
2305 		drm_err(dev, "Failed to register client: %d\n", ret);
2306 		return ret;
2307 	}
2308 
2309 	if (!preferred_bpp)
2310 		preferred_bpp = dev->mode_config.preferred_depth;
2311 	if (!preferred_bpp)
2312 		preferred_bpp = 32;
2313 	fb_helper->preferred_bpp = preferred_bpp;
2314 
2315 	ret = drm_fbdev_client_hotplug(&fb_helper->client);
2316 	if (ret)
2317 		drm_dbg_kms(dev, "client hotplug ret=%d\n", ret);
2318 
2319 	drm_client_register(&fb_helper->client);
2320 
2321 	return 0;
2322 }
2323 EXPORT_SYMBOL(drm_fbdev_generic_setup);
2324 
2325 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
2326  * but the module doesn't depend on any fb console symbols.  At least
2327  * attempt to load fbcon to avoid leaving the system without a usable console.
2328  */
drm_fb_helper_modinit(void)2329 int __init drm_fb_helper_modinit(void)
2330 {
2331 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2332 	const char name[] = "fbcon";
2333 	struct module *fbcon;
2334 
2335 	mutex_lock(&module_mutex);
2336 	fbcon = find_module(name);
2337 	mutex_unlock(&module_mutex);
2338 
2339 	if (!fbcon)
2340 		request_module_nowait(name);
2341 #endif
2342 	return 0;
2343 }
2344 EXPORT_SYMBOL(drm_fb_helper_modinit);
2345