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