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