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