Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 
0003 #include <linux/dma-resv.h>
0004 #include <linux/dma-fence-chain.h>
0005 
0006 #include <drm/drm_atomic_state_helper.h>
0007 #include <drm/drm_atomic_uapi.h>
0008 #include <drm/drm_framebuffer.h>
0009 #include <drm/drm_gem.h>
0010 #include <drm/drm_gem_atomic_helper.h>
0011 #include <drm/drm_gem_framebuffer_helper.h>
0012 #include <drm/drm_simple_kms_helper.h>
0013 
0014 #include "drm_internal.h"
0015 
0016 /**
0017  * DOC: overview
0018  *
0019  * The GEM atomic helpers library implements generic atomic-commit
0020  * functions for drivers that use GEM objects. Currently, it provides
0021  * synchronization helpers, and plane state and framebuffer BO mappings
0022  * for planes with shadow buffers.
0023  *
0024  * Before scanout, a plane's framebuffer needs to be synchronized with
0025  * possible writers that draw into the framebuffer. All drivers should
0026  * call drm_gem_plane_helper_prepare_fb() from their implementation of
0027  * struct &drm_plane_helper.prepare_fb . It sets the plane's fence from
0028  * the framebuffer so that the DRM core can synchronize access automatically.
0029  *
0030  * drm_gem_plane_helper_prepare_fb() can also be used directly as
0031  * implementation of prepare_fb. For drivers based on
0032  * struct drm_simple_display_pipe, drm_gem_simple_display_pipe_prepare_fb()
0033  * provides equivalent functionality.
0034  *
0035  * .. code-block:: c
0036  *
0037  *  #include <drm/drm_gem_atomic_helper.h>
0038  *
0039  *  struct drm_plane_helper_funcs driver_plane_helper_funcs = {
0040  *      ...,
0041  *      . prepare_fb = drm_gem_plane_helper_prepare_fb,
0042  *  };
0043  *
0044  *  struct drm_simple_display_pipe_funcs driver_pipe_funcs = {
0045  *      ...,
0046  *      . prepare_fb = drm_gem_simple_display_pipe_prepare_fb,
0047  *  };
0048  *
0049  * A driver using a shadow buffer copies the content of the shadow buffers
0050  * into the HW's framebuffer memory during an atomic update. This requires
0051  * a mapping of the shadow buffer into kernel address space. The mappings
0052  * cannot be established by commit-tail functions, such as atomic_update,
0053  * as this would violate locking rules around dma_buf_vmap().
0054  *
0055  * The helpers for shadow-buffered planes establish and release mappings,
0056  * and provide struct drm_shadow_plane_state, which stores the plane's mapping
0057  * for commit-tail functions.
0058  *
0059  * Shadow-buffered planes can easily be enabled by using the provided macros
0060  * %DRM_GEM_SHADOW_PLANE_FUNCS and %DRM_GEM_SHADOW_PLANE_HELPER_FUNCS.
0061  * These macros set up the plane and plane-helper callbacks to point to the
0062  * shadow-buffer helpers.
0063  *
0064  * .. code-block:: c
0065  *
0066  *  #include <drm/drm_gem_atomic_helper.h>
0067  *
0068  *  struct drm_plane_funcs driver_plane_funcs = {
0069  *      ...,
0070  *      DRM_GEM_SHADOW_PLANE_FUNCS,
0071  *  };
0072  *
0073  *  struct drm_plane_helper_funcs driver_plane_helper_funcs = {
0074  *      ...,
0075  *      DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
0076  *  };
0077  *
0078  * In the driver's atomic-update function, shadow-buffer mappings are available
0079  * from the plane state. Use to_drm_shadow_plane_state() to upcast from
0080  * struct drm_plane_state.
0081  *
0082  * .. code-block:: c
0083  *
0084  *  void driver_plane_atomic_update(struct drm_plane *plane,
0085  *                  struct drm_plane_state *old_plane_state)
0086  *  {
0087  *      struct drm_plane_state *plane_state = plane->state;
0088  *      struct drm_shadow_plane_state *shadow_plane_state =
0089  *          to_drm_shadow_plane_state(plane_state);
0090  *
0091  *      // access shadow buffer via shadow_plane_state->map
0092  *  }
0093  *
0094  * A mapping address for each of the framebuffer's buffer object is stored in
0095  * struct &drm_shadow_plane_state.map. The mappings are valid while the state
0096  * is being used.
0097  *
0098  * Drivers that use struct drm_simple_display_pipe can use
0099  * %DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS to initialize the rsp
0100  * callbacks. Access to shadow-buffer mappings is similar to regular
0101  * atomic_update.
0102  *
0103  * .. code-block:: c
0104  *
0105  *  struct drm_simple_display_pipe_funcs driver_pipe_funcs = {
0106  *      ...,
0107  *      DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
0108  *  };
0109  *
0110  *  void driver_pipe_enable(struct drm_simple_display_pipe *pipe,
0111  *              struct drm_crtc_state *crtc_state,
0112  *              struct drm_plane_state *plane_state)
0113  *  {
0114  *      struct drm_shadow_plane_state *shadow_plane_state =
0115  *          to_drm_shadow_plane_state(plane_state);
0116  *
0117  *      // access shadow buffer via shadow_plane_state->map
0118  *  }
0119  */
0120 
0121 /*
0122  * Plane Helpers
0123  */
0124 
0125 /**
0126  * drm_gem_plane_helper_prepare_fb() - Prepare a GEM backed framebuffer
0127  * @plane: Plane
0128  * @state: Plane state the fence will be attached to
0129  *
0130  * This function extracts the exclusive fence from &drm_gem_object.resv and
0131  * attaches it to plane state for the atomic helper to wait on. This is
0132  * necessary to correctly implement implicit synchronization for any buffers
0133  * shared as a struct &dma_buf. This function can be used as the
0134  * &drm_plane_helper_funcs.prepare_fb callback.
0135  *
0136  * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
0137  * GEM based framebuffer drivers which have their buffers always pinned in
0138  * memory.
0139  *
0140  * This function is the default implementation for GEM drivers of
0141  * &drm_plane_helper_funcs.prepare_fb if no callback is provided.
0142  */
0143 int drm_gem_plane_helper_prepare_fb(struct drm_plane *plane,
0144                     struct drm_plane_state *state)
0145 {
0146     struct dma_fence *fence = dma_fence_get(state->fence);
0147     enum dma_resv_usage usage;
0148     size_t i;
0149     int ret;
0150 
0151     if (!state->fb)
0152         return 0;
0153 
0154     /*
0155      * Only add the kernel fences here if there is already a fence set via
0156      * explicit fencing interfaces on the atomic ioctl.
0157      *
0158      * This way explicit fencing can be used to overrule implicit fencing,
0159      * which is important to make explicit fencing use-cases work: One
0160      * example is using one buffer for 2 screens with different refresh
0161      * rates. Implicit fencing will clamp rendering to the refresh rate of
0162      * the slower screen, whereas explicit fence allows 2 independent
0163      * render and display loops on a single buffer. If a driver allows
0164      * obeys both implicit and explicit fences for plane updates, then it
0165      * will break all the benefits of explicit fencing.
0166      */
0167     usage = fence ? DMA_RESV_USAGE_KERNEL : DMA_RESV_USAGE_WRITE;
0168 
0169     for (i = 0; i < state->fb->format->num_planes; ++i) {
0170         struct drm_gem_object *obj = drm_gem_fb_get_obj(state->fb, i);
0171         struct dma_fence *new;
0172 
0173         if (!obj) {
0174             ret = -EINVAL;
0175             goto error;
0176         }
0177 
0178         ret = dma_resv_get_singleton(obj->resv, usage, &new);
0179         if (ret)
0180             goto error;
0181 
0182         if (new && fence) {
0183             struct dma_fence_chain *chain = dma_fence_chain_alloc();
0184 
0185             if (!chain) {
0186                 ret = -ENOMEM;
0187                 goto error;
0188             }
0189 
0190             dma_fence_chain_init(chain, fence, new, 1);
0191             fence = &chain->base;
0192 
0193         } else if (new) {
0194             fence = new;
0195         }
0196     }
0197 
0198     dma_fence_put(state->fence);
0199     state->fence = fence;
0200     return 0;
0201 
0202 error:
0203     dma_fence_put(fence);
0204     return ret;
0205 }
0206 EXPORT_SYMBOL_GPL(drm_gem_plane_helper_prepare_fb);
0207 
0208 /**
0209  * drm_gem_simple_display_pipe_prepare_fb - prepare_fb helper for &drm_simple_display_pipe
0210  * @pipe: Simple display pipe
0211  * @plane_state: Plane state
0212  *
0213  * This function uses drm_gem_plane_helper_prepare_fb() to extract the fences
0214  * from &drm_gem_object.resv and attaches them to the plane state for the atomic
0215  * helper to wait on. This is necessary to correctly implement implicit
0216  * synchronization for any buffers shared as a struct &dma_buf. Drivers can use
0217  * this as their &drm_simple_display_pipe_funcs.prepare_fb callback.
0218  *
0219  * See drm_gem_plane_helper_prepare_fb() for a discussion of implicit and
0220  * explicit fencing in atomic modeset updates.
0221  */
0222 int drm_gem_simple_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
0223                        struct drm_plane_state *plane_state)
0224 {
0225     return drm_gem_plane_helper_prepare_fb(&pipe->plane, plane_state);
0226 }
0227 EXPORT_SYMBOL(drm_gem_simple_display_pipe_prepare_fb);
0228 
0229 /*
0230  * Shadow-buffered Planes
0231  */
0232 
0233 /**
0234  * __drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
0235  * @plane: the plane
0236  * @new_shadow_plane_state: the new shadow-buffered plane state
0237  *
0238  * This function duplicates shadow-buffered plane state. This is helpful for drivers
0239  * that subclass struct drm_shadow_plane_state.
0240  *
0241  * The function does not duplicate existing mappings of the shadow buffers.
0242  * Mappings are maintained during the atomic commit by the plane's prepare_fb
0243  * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb()
0244  * for corresponding helpers.
0245  */
0246 void
0247 __drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane,
0248                        struct drm_shadow_plane_state *new_shadow_plane_state)
0249 {
0250     __drm_atomic_helper_plane_duplicate_state(plane, &new_shadow_plane_state->base);
0251 }
0252 EXPORT_SYMBOL(__drm_gem_duplicate_shadow_plane_state);
0253 
0254 /**
0255  * drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
0256  * @plane: the plane
0257  *
0258  * This function implements struct &drm_plane_funcs.atomic_duplicate_state for
0259  * shadow-buffered planes. It assumes the existing state to be of type
0260  * struct drm_shadow_plane_state and it allocates the new state to be of this
0261  * type.
0262  *
0263  * The function does not duplicate existing mappings of the shadow buffers.
0264  * Mappings are maintained during the atomic commit by the plane's prepare_fb
0265  * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb()
0266  * for corresponding helpers.
0267  *
0268  * Returns:
0269  * A pointer to a new plane state on success, or NULL otherwise.
0270  */
0271 struct drm_plane_state *
0272 drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane)
0273 {
0274     struct drm_plane_state *plane_state = plane->state;
0275     struct drm_shadow_plane_state *new_shadow_plane_state;
0276 
0277     if (!plane_state)
0278         return NULL;
0279 
0280     new_shadow_plane_state = kzalloc(sizeof(*new_shadow_plane_state), GFP_KERNEL);
0281     if (!new_shadow_plane_state)
0282         return NULL;
0283     __drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
0284 
0285     return &new_shadow_plane_state->base;
0286 }
0287 EXPORT_SYMBOL(drm_gem_duplicate_shadow_plane_state);
0288 
0289 /**
0290  * __drm_gem_destroy_shadow_plane_state - cleans up shadow-buffered plane state
0291  * @shadow_plane_state: the shadow-buffered plane state
0292  *
0293  * This function cleans up shadow-buffered plane state. Helpful for drivers that
0294  * subclass struct drm_shadow_plane_state.
0295  */
0296 void __drm_gem_destroy_shadow_plane_state(struct drm_shadow_plane_state *shadow_plane_state)
0297 {
0298     __drm_atomic_helper_plane_destroy_state(&shadow_plane_state->base);
0299 }
0300 EXPORT_SYMBOL(__drm_gem_destroy_shadow_plane_state);
0301 
0302 /**
0303  * drm_gem_destroy_shadow_plane_state - deletes shadow-buffered plane state
0304  * @plane: the plane
0305  * @plane_state: the plane state of type struct drm_shadow_plane_state
0306  *
0307  * This function implements struct &drm_plane_funcs.atomic_destroy_state
0308  * for shadow-buffered planes. It expects that mappings of shadow buffers
0309  * have been released already.
0310  */
0311 void drm_gem_destroy_shadow_plane_state(struct drm_plane *plane,
0312                     struct drm_plane_state *plane_state)
0313 {
0314     struct drm_shadow_plane_state *shadow_plane_state =
0315         to_drm_shadow_plane_state(plane_state);
0316 
0317     __drm_gem_destroy_shadow_plane_state(shadow_plane_state);
0318     kfree(shadow_plane_state);
0319 }
0320 EXPORT_SYMBOL(drm_gem_destroy_shadow_plane_state);
0321 
0322 /**
0323  * __drm_gem_reset_shadow_plane - resets a shadow-buffered plane
0324  * @plane: the plane
0325  * @shadow_plane_state: the shadow-buffered plane state
0326  *
0327  * This function resets state for shadow-buffered planes. Helpful
0328  * for drivers that subclass struct drm_shadow_plane_state.
0329  */
0330 void __drm_gem_reset_shadow_plane(struct drm_plane *plane,
0331                   struct drm_shadow_plane_state *shadow_plane_state)
0332 {
0333     __drm_atomic_helper_plane_reset(plane, &shadow_plane_state->base);
0334 }
0335 EXPORT_SYMBOL(__drm_gem_reset_shadow_plane);
0336 
0337 /**
0338  * drm_gem_reset_shadow_plane - resets a shadow-buffered plane
0339  * @plane: the plane
0340  *
0341  * This function implements struct &drm_plane_funcs.reset_plane for
0342  * shadow-buffered planes. It assumes the current plane state to be
0343  * of type struct drm_shadow_plane and it allocates the new state of
0344  * this type.
0345  */
0346 void drm_gem_reset_shadow_plane(struct drm_plane *plane)
0347 {
0348     struct drm_shadow_plane_state *shadow_plane_state;
0349 
0350     if (plane->state) {
0351         drm_gem_destroy_shadow_plane_state(plane, plane->state);
0352         plane->state = NULL; /* must be set to NULL here */
0353     }
0354 
0355     shadow_plane_state = kzalloc(sizeof(*shadow_plane_state), GFP_KERNEL);
0356     if (!shadow_plane_state)
0357         return;
0358     __drm_gem_reset_shadow_plane(plane, shadow_plane_state);
0359 }
0360 EXPORT_SYMBOL(drm_gem_reset_shadow_plane);
0361 
0362 /**
0363  * drm_gem_prepare_shadow_fb - prepares shadow framebuffers
0364  * @plane: the plane
0365  * @plane_state: the plane state of type struct drm_shadow_plane_state
0366  *
0367  * This function implements struct &drm_plane_helper_funcs.prepare_fb. It
0368  * maps all buffer objects of the plane's framebuffer into kernel address
0369  * space and stores them in &struct drm_shadow_plane_state.map. The
0370  * framebuffer will be synchronized as part of the atomic commit.
0371  *
0372  * See drm_gem_cleanup_shadow_fb() for cleanup.
0373  *
0374  * Returns:
0375  * 0 on success, or a negative errno code otherwise.
0376  */
0377 int drm_gem_prepare_shadow_fb(struct drm_plane *plane, struct drm_plane_state *plane_state)
0378 {
0379     struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
0380     struct drm_framebuffer *fb = plane_state->fb;
0381     int ret;
0382 
0383     if (!fb)
0384         return 0;
0385 
0386     ret = drm_gem_plane_helper_prepare_fb(plane, plane_state);
0387     if (ret)
0388         return ret;
0389 
0390     return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
0391 }
0392 EXPORT_SYMBOL(drm_gem_prepare_shadow_fb);
0393 
0394 /**
0395  * drm_gem_cleanup_shadow_fb - releases shadow framebuffers
0396  * @plane: the plane
0397  * @plane_state: the plane state of type struct drm_shadow_plane_state
0398  *
0399  * This function implements struct &drm_plane_helper_funcs.cleanup_fb.
0400  * This function unmaps all buffer objects of the plane's framebuffer.
0401  *
0402  * See drm_gem_prepare_shadow_fb() for more information.
0403  */
0404 void drm_gem_cleanup_shadow_fb(struct drm_plane *plane, struct drm_plane_state *plane_state)
0405 {
0406     struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
0407     struct drm_framebuffer *fb = plane_state->fb;
0408 
0409     if (!fb)
0410         return;
0411 
0412     drm_gem_fb_vunmap(fb, shadow_plane_state->map);
0413 }
0414 EXPORT_SYMBOL(drm_gem_cleanup_shadow_fb);
0415 
0416 /**
0417  * drm_gem_simple_kms_prepare_shadow_fb - prepares shadow framebuffers
0418  * @pipe: the simple display pipe
0419  * @plane_state: the plane state of type struct drm_shadow_plane_state
0420  *
0421  * This function implements struct drm_simple_display_funcs.prepare_fb. It
0422  * maps all buffer objects of the plane's framebuffer into kernel address
0423  * space and stores them in struct drm_shadow_plane_state.map. The
0424  * framebuffer will be synchronized as part of the atomic commit.
0425  *
0426  * See drm_gem_simple_kms_cleanup_shadow_fb() for cleanup.
0427  *
0428  * Returns:
0429  * 0 on success, or a negative errno code otherwise.
0430  */
0431 int drm_gem_simple_kms_prepare_shadow_fb(struct drm_simple_display_pipe *pipe,
0432                      struct drm_plane_state *plane_state)
0433 {
0434     return drm_gem_prepare_shadow_fb(&pipe->plane, plane_state);
0435 }
0436 EXPORT_SYMBOL(drm_gem_simple_kms_prepare_shadow_fb);
0437 
0438 /**
0439  * drm_gem_simple_kms_cleanup_shadow_fb - releases shadow framebuffers
0440  * @pipe: the simple display pipe
0441  * @plane_state: the plane state of type struct drm_shadow_plane_state
0442  *
0443  * This function implements struct drm_simple_display_funcs.cleanup_fb.
0444  * This function unmaps all buffer objects of the plane's framebuffer.
0445  *
0446  * See drm_gem_simple_kms_prepare_shadow_fb().
0447  */
0448 void drm_gem_simple_kms_cleanup_shadow_fb(struct drm_simple_display_pipe *pipe,
0449                       struct drm_plane_state *plane_state)
0450 {
0451     drm_gem_cleanup_shadow_fb(&pipe->plane, plane_state);
0452 }
0453 EXPORT_SYMBOL(drm_gem_simple_kms_cleanup_shadow_fb);
0454 
0455 /**
0456  * drm_gem_simple_kms_reset_shadow_plane - resets a shadow-buffered plane
0457  * @pipe: the simple display pipe
0458  *
0459  * This function implements struct drm_simple_display_funcs.reset_plane
0460  * for shadow-buffered planes.
0461  */
0462 void drm_gem_simple_kms_reset_shadow_plane(struct drm_simple_display_pipe *pipe)
0463 {
0464     drm_gem_reset_shadow_plane(&pipe->plane);
0465 }
0466 EXPORT_SYMBOL(drm_gem_simple_kms_reset_shadow_plane);
0467 
0468 /**
0469  * drm_gem_simple_kms_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
0470  * @pipe: the simple display pipe
0471  *
0472  * This function implements struct drm_simple_display_funcs.duplicate_plane_state
0473  * for shadow-buffered planes. It does not duplicate existing mappings of the shadow
0474  * buffers. Mappings are maintained during the atomic commit by the plane's prepare_fb
0475  * and cleanup_fb helpers.
0476  *
0477  * Returns:
0478  * A pointer to a new plane state on success, or NULL otherwise.
0479  */
0480 struct drm_plane_state *
0481 drm_gem_simple_kms_duplicate_shadow_plane_state(struct drm_simple_display_pipe *pipe)
0482 {
0483     return drm_gem_duplicate_shadow_plane_state(&pipe->plane);
0484 }
0485 EXPORT_SYMBOL(drm_gem_simple_kms_duplicate_shadow_plane_state);
0486 
0487 /**
0488  * drm_gem_simple_kms_destroy_shadow_plane_state - resets shadow-buffered plane state
0489  * @pipe: the simple display pipe
0490  * @plane_state: the plane state of type struct drm_shadow_plane_state
0491  *
0492  * This function implements struct drm_simple_display_funcs.destroy_plane_state
0493  * for shadow-buffered planes. It expects that mappings of shadow buffers
0494  * have been released already.
0495  */
0496 void drm_gem_simple_kms_destroy_shadow_plane_state(struct drm_simple_display_pipe *pipe,
0497                            struct drm_plane_state *plane_state)
0498 {
0499     drm_gem_destroy_shadow_plane_state(&pipe->plane, plane_state);
0500 }
0501 EXPORT_SYMBOL(drm_gem_simple_kms_destroy_shadow_plane_state);