1 // SPDX-License-Identifier: GPL-2.0-or-later 2 3 #include <linux/dma-resv.h> 4 #include <linux/dma-fence-chain.h> 5 #include <linux/export.h> 6 7 #include <drm/drm_atomic_state_helper.h> 8 #include <drm/drm_atomic_uapi.h> 9 #include <drm/drm_framebuffer.h> 10 #include <drm/drm_gem.h> 11 #include <drm/drm_gem_atomic_helper.h> 12 #include <drm/drm_gem_framebuffer_helper.h> 13 #include <drm/drm_simple_kms_helper.h> 14 15 #include "drm_internal.h" 16 17 /** 18 * DOC: overview 19 * 20 * The GEM atomic helpers library implements generic atomic-commit 21 * functions for drivers that use GEM objects. Currently, it provides 22 * synchronization helpers, and plane state and framebuffer BO mappings 23 * for planes with shadow buffers. 24 * 25 * Before scanout, a plane's framebuffer needs to be synchronized with 26 * possible writers that draw into the framebuffer. All drivers should 27 * call drm_gem_plane_helper_prepare_fb() from their implementation of 28 * struct &drm_plane_helper.prepare_fb . It sets the plane's fence from 29 * the framebuffer so that the DRM core can synchronize access automatically. 30 * drm_gem_plane_helper_prepare_fb() can also be used directly as 31 * implementation of prepare_fb. 32 * 33 * .. code-block:: c 34 * 35 * #include <drm/drm_gem_atomic_helper.h> 36 * 37 * struct drm_plane_helper_funcs driver_plane_helper_funcs = { 38 * ..., 39 * . prepare_fb = drm_gem_plane_helper_prepare_fb, 40 * }; 41 * 42 * A driver using a shadow buffer copies the content of the shadow buffers 43 * into the HW's framebuffer memory during an atomic update. This requires 44 * a mapping of the shadow buffer into kernel address space. The mappings 45 * cannot be established by commit-tail functions, such as atomic_update, 46 * as this would violate locking rules around dma_buf_vmap(). 47 * 48 * The helpers for shadow-buffered planes establish and release mappings, 49 * and provide struct drm_shadow_plane_state, which stores the plane's mapping 50 * for commit-tail functions. 51 * 52 * Shadow-buffered planes can easily be enabled by using the provided macros 53 * %DRM_GEM_SHADOW_PLANE_FUNCS and %DRM_GEM_SHADOW_PLANE_HELPER_FUNCS. 54 * These macros set up the plane and plane-helper callbacks to point to the 55 * shadow-buffer helpers. 56 * 57 * .. code-block:: c 58 * 59 * #include <drm/drm_gem_atomic_helper.h> 60 * 61 * struct drm_plane_funcs driver_plane_funcs = { 62 * ..., 63 * DRM_GEM_SHADOW_PLANE_FUNCS, 64 * }; 65 * 66 * struct drm_plane_helper_funcs driver_plane_helper_funcs = { 67 * ..., 68 * DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, 69 * }; 70 * 71 * In the driver's atomic-update function, shadow-buffer mappings are available 72 * from the plane state. Use to_drm_shadow_plane_state() to upcast from 73 * struct drm_plane_state. 74 * 75 * .. code-block:: c 76 * 77 * void driver_plane_atomic_update(struct drm_plane *plane, 78 * struct drm_plane_state *old_plane_state) 79 * { 80 * struct drm_plane_state *plane_state = plane->state; 81 * struct drm_shadow_plane_state *shadow_plane_state = 82 * to_drm_shadow_plane_state(plane_state); 83 * 84 * // access shadow buffer via shadow_plane_state->map 85 * } 86 * 87 * A mapping address for each of the framebuffer's buffer object is stored in 88 * struct &drm_shadow_plane_state.map. The mappings are valid while the state 89 * is being used. 90 */ 91 92 /* 93 * Plane Helpers 94 */ 95 96 /** 97 * drm_gem_plane_helper_prepare_fb() - Prepare a GEM backed framebuffer 98 * @plane: Plane 99 * @state: Plane state the fence will be attached to 100 * 101 * This function extracts the exclusive fence from &drm_gem_object.resv and 102 * attaches it to plane state for the atomic helper to wait on. This is 103 * necessary to correctly implement implicit synchronization for any buffers 104 * shared as a struct &dma_buf. This function can be used as the 105 * &drm_plane_helper_funcs.prepare_fb callback. 106 * 107 * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple 108 * GEM based framebuffer drivers which have their buffers always pinned in 109 * memory. 110 * 111 * This function is the default implementation for GEM drivers of 112 * &drm_plane_helper_funcs.prepare_fb if no callback is provided. 113 */ 114 int drm_gem_plane_helper_prepare_fb(struct drm_plane *plane, 115 struct drm_plane_state *state) 116 { 117 struct dma_fence *fence = dma_fence_get(state->fence); 118 enum dma_resv_usage usage; 119 size_t i; 120 int ret; 121 122 if (!state->fb) 123 return 0; 124 125 /* 126 * Only add the kernel fences here if there is already a fence set via 127 * explicit fencing interfaces on the atomic ioctl. 128 * 129 * This way explicit fencing can be used to overrule implicit fencing, 130 * which is important to make explicit fencing use-cases work: One 131 * example is using one buffer for 2 screens with different refresh 132 * rates. Implicit fencing will clamp rendering to the refresh rate of 133 * the slower screen, whereas explicit fence allows 2 independent 134 * render and display loops on a single buffer. If a driver allows 135 * obeys both implicit and explicit fences for plane updates, then it 136 * will break all the benefits of explicit fencing. 137 */ 138 usage = fence ? DMA_RESV_USAGE_KERNEL : DMA_RESV_USAGE_WRITE; 139 140 for (i = 0; i < state->fb->format->num_planes; ++i) { 141 struct drm_gem_object *obj = drm_gem_fb_get_obj(state->fb, i); 142 struct dma_fence *new; 143 144 if (!obj) { 145 ret = -EINVAL; 146 goto error; 147 } 148 149 ret = dma_resv_get_singleton(obj->resv, usage, &new); 150 if (ret) 151 goto error; 152 153 if (new && fence) { 154 struct dma_fence_chain *chain = dma_fence_chain_alloc(); 155 156 if (!chain) { 157 ret = -ENOMEM; 158 goto error; 159 } 160 161 dma_fence_chain_init(chain, fence, new, 1); 162 fence = &chain->base; 163 164 } else if (new) { 165 fence = new; 166 } 167 } 168 169 dma_fence_put(state->fence); 170 state->fence = fence; 171 return 0; 172 173 error: 174 dma_fence_put(fence); 175 return ret; 176 } 177 EXPORT_SYMBOL_GPL(drm_gem_plane_helper_prepare_fb); 178 179 /* 180 * Shadow-buffered Planes 181 */ 182 183 /** 184 * __drm_gem_shadow_plane_state_init - initializes shadow-buffered plane state 185 * @plane: the plane 186 * @shadow_plane_state: the shadow-buffered plane state 187 * 188 * This function initializes a pre-allocated shadow-buffered plane state. 189 * Helpful for drivers that subclass struct drm_shadow_plane_state. 190 */ 191 void __drm_gem_shadow_plane_state_init(struct drm_plane *plane, 192 struct drm_shadow_plane_state *shadow_plane_state) 193 { 194 __drm_atomic_helper_plane_state_init(&shadow_plane_state->base, plane); 195 drm_format_conv_state_init(&shadow_plane_state->fmtcnv_state); 196 } 197 EXPORT_SYMBOL(__drm_gem_shadow_plane_state_init); 198 199 /** 200 * drm_gem_create_shadow_plane_state - creates a shadow-buffered plane state 201 * @plane: the plane 202 * 203 * This function implements struct &drm_plane_funcs.atomic_create_state for 204 * shadow-buffered planes. It assumes the new state to be of type 205 * struct drm_shadow_plane_state. 206 * 207 * Returns: 208 * A pointer to a new plane state on success, or an ERR_PTR()-encoded 209 * error code otherwise. 210 */ 211 struct drm_plane_state *drm_gem_create_shadow_plane_state(struct drm_plane *plane) 212 { 213 struct drm_shadow_plane_state *shadow_plane_state; 214 215 shadow_plane_state = kzalloc_obj(*shadow_plane_state); 216 if (!shadow_plane_state) 217 return ERR_PTR(-ENOMEM); 218 219 __drm_gem_shadow_plane_state_init(plane, shadow_plane_state); 220 221 return &shadow_plane_state->base; 222 } 223 EXPORT_SYMBOL(drm_gem_create_shadow_plane_state); 224 225 /** 226 * __drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state 227 * @plane: the plane 228 * @new_shadow_plane_state: the new shadow-buffered plane state 229 * 230 * This function duplicates shadow-buffered plane state. This is helpful for drivers 231 * that subclass struct drm_shadow_plane_state. 232 * 233 * The function does not duplicate existing mappings of the shadow buffers. 234 * Mappings are maintained during the atomic commit by the plane's prepare_fb 235 * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb() 236 * for corresponding helpers. 237 */ 238 void 239 __drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane, 240 struct drm_shadow_plane_state *new_shadow_plane_state) 241 { 242 struct drm_plane_state *plane_state = plane->state; 243 struct drm_shadow_plane_state *shadow_plane_state = 244 to_drm_shadow_plane_state(plane_state); 245 246 __drm_atomic_helper_plane_duplicate_state(plane, &new_shadow_plane_state->base); 247 248 drm_format_conv_state_copy(&new_shadow_plane_state->fmtcnv_state, 249 &shadow_plane_state->fmtcnv_state); 250 } 251 EXPORT_SYMBOL(__drm_gem_duplicate_shadow_plane_state); 252 253 /** 254 * drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state 255 * @plane: the plane 256 * 257 * This function implements struct &drm_plane_funcs.atomic_duplicate_state for 258 * shadow-buffered planes. It assumes the existing state to be of type 259 * struct drm_shadow_plane_state and it allocates the new state to be of this 260 * type. 261 * 262 * The function does not duplicate existing mappings of the shadow buffers. 263 * Mappings are maintained during the atomic commit by the plane's prepare_fb 264 * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb() 265 * for corresponding helpers. 266 * 267 * Returns: 268 * A pointer to a new plane state on success, or NULL otherwise. 269 */ 270 struct drm_plane_state * 271 drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane) 272 { 273 struct drm_plane_state *plane_state = plane->state; 274 struct drm_shadow_plane_state *new_shadow_plane_state; 275 276 if (!plane_state) 277 return NULL; 278 279 new_shadow_plane_state = kzalloc_obj(*new_shadow_plane_state); 280 if (!new_shadow_plane_state) 281 return NULL; 282 __drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state); 283 284 return &new_shadow_plane_state->base; 285 } 286 EXPORT_SYMBOL(drm_gem_duplicate_shadow_plane_state); 287 288 /** 289 * __drm_gem_destroy_shadow_plane_state - cleans up shadow-buffered plane state 290 * @shadow_plane_state: the shadow-buffered plane state 291 * 292 * This function cleans up shadow-buffered plane state. Helpful for drivers that 293 * subclass struct drm_shadow_plane_state. 294 */ 295 void __drm_gem_destroy_shadow_plane_state(struct drm_shadow_plane_state *shadow_plane_state) 296 { 297 drm_format_conv_state_release(&shadow_plane_state->fmtcnv_state); 298 __drm_atomic_helper_plane_destroy_state(&shadow_plane_state->base); 299 } 300 EXPORT_SYMBOL(__drm_gem_destroy_shadow_plane_state); 301 302 /** 303 * drm_gem_destroy_shadow_plane_state - deletes shadow-buffered plane state 304 * @plane: the plane 305 * @plane_state: the plane state of type struct drm_shadow_plane_state 306 * 307 * This function implements struct &drm_plane_funcs.atomic_destroy_state 308 * for shadow-buffered planes. It expects that mappings of shadow buffers 309 * have been released already. 310 */ 311 void drm_gem_destroy_shadow_plane_state(struct drm_plane *plane, 312 struct drm_plane_state *plane_state) 313 { 314 struct drm_shadow_plane_state *shadow_plane_state = 315 to_drm_shadow_plane_state(plane_state); 316 317 __drm_gem_destroy_shadow_plane_state(shadow_plane_state); 318 kfree(shadow_plane_state); 319 } 320 EXPORT_SYMBOL(drm_gem_destroy_shadow_plane_state); 321 322 /** 323 * drm_gem_begin_shadow_fb_access - prepares shadow framebuffers for CPU access 324 * @plane: the plane 325 * @plane_state: the plane state of type struct drm_shadow_plane_state 326 * 327 * This function implements struct &drm_plane_helper_funcs.begin_fb_access. It 328 * maps all buffer objects of the plane's framebuffer into kernel address 329 * space and stores them in struct &drm_shadow_plane_state.map. The first data 330 * bytes are available in struct &drm_shadow_plane_state.data. 331 * 332 * See drm_gem_end_shadow_fb_access() for cleanup. 333 * 334 * Returns: 335 * 0 on success, or a negative errno code otherwise. 336 */ 337 int drm_gem_begin_shadow_fb_access(struct drm_plane *plane, struct drm_plane_state *plane_state) 338 { 339 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); 340 struct drm_framebuffer *fb = plane_state->fb; 341 342 if (!fb) 343 return 0; 344 345 return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data); 346 } 347 EXPORT_SYMBOL(drm_gem_begin_shadow_fb_access); 348 349 /** 350 * drm_gem_end_shadow_fb_access - releases shadow framebuffers from CPU access 351 * @plane: the plane 352 * @plane_state: the plane state of type struct drm_shadow_plane_state 353 * 354 * This function implements struct &drm_plane_helper_funcs.end_fb_access. It 355 * undoes all effects of drm_gem_begin_shadow_fb_access() in reverse order. 356 * 357 * See drm_gem_begin_shadow_fb_access() for more information. 358 */ 359 void drm_gem_end_shadow_fb_access(struct drm_plane *plane, struct drm_plane_state *plane_state) 360 { 361 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); 362 struct drm_framebuffer *fb = plane_state->fb; 363 364 if (!fb) 365 return; 366 367 drm_gem_fb_vunmap(fb, shadow_plane_state->map); 368 } 369 EXPORT_SYMBOL(drm_gem_end_shadow_fb_access); 370 371 /** 372 * drm_gem_simple_kms_begin_shadow_fb_access - prepares shadow framebuffers for CPU access 373 * @pipe: the simple display pipe 374 * @plane_state: the plane state of type struct drm_shadow_plane_state 375 * 376 * This function implements struct drm_simple_display_funcs.begin_fb_access. 377 * 378 * See drm_gem_begin_shadow_fb_access() for details and 379 * drm_gem_simple_kms_cleanup_shadow_fb() for cleanup. 380 * 381 * Returns: 382 * 0 on success, or a negative errno code otherwise. 383 */ 384 int drm_gem_simple_kms_begin_shadow_fb_access(struct drm_simple_display_pipe *pipe, 385 struct drm_plane_state *plane_state) 386 { 387 return drm_gem_begin_shadow_fb_access(&pipe->plane, plane_state); 388 } 389 EXPORT_SYMBOL(drm_gem_simple_kms_begin_shadow_fb_access); 390 391 /** 392 * drm_gem_simple_kms_end_shadow_fb_access - releases shadow framebuffers from CPU access 393 * @pipe: the simple display pipe 394 * @plane_state: the plane state of type struct drm_shadow_plane_state 395 * 396 * This function implements struct drm_simple_display_funcs.end_fb_access. 397 * It undoes all effects of drm_gem_simple_kms_begin_shadow_fb_access() in 398 * reverse order. 399 * 400 * See drm_gem_simple_kms_begin_shadow_fb_access(). 401 */ 402 void drm_gem_simple_kms_end_shadow_fb_access(struct drm_simple_display_pipe *pipe, 403 struct drm_plane_state *plane_state) 404 { 405 drm_gem_end_shadow_fb_access(&pipe->plane, plane_state); 406 } 407 EXPORT_SYMBOL(drm_gem_simple_kms_end_shadow_fb_access); 408 409 /** 410 * drm_gem_simple_kms_create_shadow_plane_state - creates shadow-buffered plane state 411 * @pipe: the simple display pipe 412 * 413 * This function implements struct drm_simple_display_pipe_funcs.create_plane_state 414 * for shadow-buffered planes. 415 * 416 * Returns: 417 * A pointer to a new plane state on success, or an ERR_PTR()-encoded 418 * error code otherwise. 419 */ 420 struct drm_plane_state * 421 drm_gem_simple_kms_create_shadow_plane_state(struct drm_simple_display_pipe *pipe) 422 { 423 return drm_gem_create_shadow_plane_state(&pipe->plane); 424 } 425 EXPORT_SYMBOL(drm_gem_simple_kms_create_shadow_plane_state); 426 427 /** 428 * drm_gem_simple_kms_duplicate_shadow_plane_state - duplicates shadow-buffered plane state 429 * @pipe: the simple display pipe 430 * 431 * This function implements struct drm_simple_display_funcs.duplicate_plane_state 432 * for shadow-buffered planes. It does not duplicate existing mappings of the shadow 433 * buffers. Mappings are maintained during the atomic commit by the plane's prepare_fb 434 * and cleanup_fb helpers. 435 * 436 * Returns: 437 * A pointer to a new plane state on success, or NULL otherwise. 438 */ 439 struct drm_plane_state * 440 drm_gem_simple_kms_duplicate_shadow_plane_state(struct drm_simple_display_pipe *pipe) 441 { 442 return drm_gem_duplicate_shadow_plane_state(&pipe->plane); 443 } 444 EXPORT_SYMBOL(drm_gem_simple_kms_duplicate_shadow_plane_state); 445 446 /** 447 * drm_gem_simple_kms_destroy_shadow_plane_state - resets shadow-buffered plane state 448 * @pipe: the simple display pipe 449 * @plane_state: the plane state of type struct drm_shadow_plane_state 450 * 451 * This function implements struct drm_simple_display_funcs.destroy_plane_state 452 * for shadow-buffered planes. It expects that mappings of shadow buffers 453 * have been released already. 454 */ 455 void drm_gem_simple_kms_destroy_shadow_plane_state(struct drm_simple_display_pipe *pipe, 456 struct drm_plane_state *plane_state) 457 { 458 drm_gem_destroy_shadow_plane_state(&pipe->plane, plane_state); 459 } 460 EXPORT_SYMBOL(drm_gem_simple_kms_destroy_shadow_plane_state); 461