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