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