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