Add prepare_fb and cleanup_fb colorop helper hooks for the DMA 3D LUT
colorop to pin and unpin its framebuffer, tying the pin to the colorop
state. prepare_fb calls drm_gem_colorop_helper_prepare_fb() to handle
implicit fencing.

A plane-only update does not necessarily carry the colorop states, so
call drm_atomic_add_affected_colorops() in atomic_check. The plane's
prepare_fb can then take the pinned framebuffer from the new colorop
state and write the LUT address into a newly created dc_plane_state.

Signed-off-by: Harry Wentland <[email protected]>
---
 .../amd/display/amdgpu_dm/amdgpu_dm_colorop.c | 91 ++++++++++++++++++-
 .../amd/display/amdgpu_dm/amdgpu_dm_colorop.h |  3 +
 .../amd/display/amdgpu_dm/amdgpu_dm_plane.c   | 39 +++++++-
 3 files changed, 129 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_colorop.c 
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_colorop.c
index 9d7720a9bd2e..5e6e40b5fd7d 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_colorop.c
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_colorop.c
@@ -29,8 +29,12 @@
 #include <drm/drm_property.h>
 #include <drm/drm_colorop.h>
 #include <drm/drm_fourcc.h>
+#include <drm/drm_gem_atomic_helper.h>
+#include <drm/drm_gem_framebuffer_helper.h>
+#include <drm/drm_modeset_helper_vtables.h>
 
 #include "amdgpu.h"
+#include "amdgpu_display.h"
 #include "amdgpu_dm_colorop.h"
 #include "dm_helpers.h"
 #include "dc.h"
@@ -122,9 +126,88 @@ EXPORT_IF_KUNIT(amdgpu_dm_colorop_kunit_set_ops);
 #define colorop_ctm_3x4_init           drm_plane_colorop_ctm_3x4_init
 #define colorop_mult_init              drm_plane_colorop_mult_init
 #define colorop_3dlut_init             drm_plane_colorop_3dlut_init
-
 #endif
 
+bool amdgpu_dm_colorop_dma_3dlut_active(const struct drm_colorop *colorop,
+                                       const struct drm_colorop_state 
*colorop_state)
+{
+       return colorop->type == DRM_COLOROP_3D_LUT_DMA && colorop_state &&
+              !colorop_state->bypass && colorop_state->fb;
+}
+
+/* Pinned while its colorop state is current, since DC re-fetches the LUT */
+static int dm_colorop_prepare_fb(struct drm_colorop *colorop,
+                                struct drm_colorop_state *new_state)
+{
+       struct drm_framebuffer *fb = new_state->fb;
+       struct amdgpu_framebuffer *afb;
+       struct amdgpu_device *adev;
+       struct amdgpu_bo *rbo;
+       uint32_t domain;
+       int r;
+
+       if (!amdgpu_dm_colorop_dma_3dlut_active(colorop, new_state))
+               return 0;
+
+       r = drm_gem_colorop_helper_prepare_fb(colorop, new_state);
+       if (r)
+               return r;
+
+       afb = to_amdgpu_framebuffer(fb);
+       rbo = gem_to_amdgpu_bo(drm_gem_fb_get_obj(fb, 0));
+       adev = amdgpu_ttm_adev(rbo->tbo.bdev);
+
+       r = amdgpu_bo_reserve(rbo, true);
+       if (r) {
+               drm_err(adev_to_drm(adev), "fail to reserve 3D LUT bo (%d)\n", 
r);
+               return r;
+       }
+
+       domain = amdgpu_display_supported_domains(adev, rbo->flags);
+       rbo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
+       r = amdgpu_bo_pin(rbo, domain);
+       if (r) {
+               if (r != -ERESTARTSYS)
+                       drm_err(adev_to_drm(adev), "Failed to pin 3D LUT bo 
(%d)\n", r);
+               amdgpu_bo_unreserve(rbo);
+               return r;
+       }
+
+       r = amdgpu_ttm_alloc_gart(&rbo->tbo);
+       if (r) {
+               amdgpu_bo_unpin(rbo);
+               amdgpu_bo_unreserve(rbo);
+               return r;
+       }
+
+       amdgpu_bo_unreserve(rbo);
+       afb->address = amdgpu_bo_gpu_offset(rbo);
+       amdgpu_bo_ref(rbo);
+
+       return 0;
+}
+
+static void dm_colorop_cleanup_fb(struct drm_colorop *colorop,
+                                 struct drm_colorop_state *old_state)
+{
+       struct amdgpu_bo *rbo;
+
+       if (!amdgpu_dm_colorop_dma_3dlut_active(colorop, old_state))
+               return;
+
+       rbo = gem_to_amdgpu_bo(drm_gem_fb_get_obj(old_state->fb, 0));
+       if (amdgpu_bo_reserve(rbo, false))
+               return;
+       amdgpu_bo_unpin(rbo);
+       amdgpu_bo_unreserve(rbo);
+       amdgpu_bo_unref(&rbo);
+}
+
+static const struct drm_colorop_helper_funcs dm_colorop_helper_funcs = {
+       .prepare_fb = dm_colorop_prepare_fb,
+       .cleanup_fb = dm_colorop_cleanup_fb,
+};
+
 STATIC_IFN_KUNIT int
 amdgpu_dm_build_default_pipeline(struct drm_device *dev, struct drm_plane 
*plane,
                                  bool hw_3d_lut, bool dma_3d_lut,
@@ -245,16 +328,18 @@ amdgpu_dm_build_default_pipeline(struct drm_device *dev, 
struct drm_plane *plane
                        goto cleanup;
                }
 
-               if (dma_3d_lut)
+               if (dma_3d_lut) {
                        ret = drm_plane_colorop_3dlut_dma_init(dev, ops[i], 
plane,
                                        &dm_colorop_funcs, 
amdgpu_dm_3dlut_dma_modes,
                                        ARRAY_SIZE(amdgpu_dm_3dlut_dma_modes),
                                        DRM_COLOROP_FLAG_ALLOW_BYPASS);
-               else
+                       drm_colorop_helper_add(ops[i], 
&dm_colorop_helper_funcs);
+               } else {
                        ret = colorop_3dlut_init(dev, ops[i], plane,
                                        &dm_colorop_funcs, LUT3D_SIZE,
                                        
DRM_COLOROP_LUT3D_INTERPOLATION_TETRAHEDRAL,
                                        DRM_COLOROP_FLAG_ALLOW_BYPASS);
+               }
                if (ret)
                        goto cleanup;
 
diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_colorop.h 
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_colorop.h
index 510c367f8ca5..58604773db75 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_colorop.h
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_colorop.h
@@ -35,6 +35,9 @@ extern const u64 amdgpu_dm_supported_fm;
 int amdgpu_dm_initialize_default_pipeline(struct drm_plane *plane, struct 
drm_prop_enum_list *list);
 int amdgpu_dm_initialize_dma_3dlut_pipeline(struct drm_plane *plane, struct 
drm_prop_enum_list *list);
 
+bool amdgpu_dm_colorop_dma_3dlut_active(const struct drm_colorop *colorop,
+                                       const struct drm_colorop_state 
*colorop_state);
+
 #if IS_ENABLED(CONFIG_DRM_AMD_DC_KUNIT_TEST)
 #include <drm/drm_colorop.h>
 
diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c 
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
index 21fe005fe583..469041145196 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_plane.c
@@ -32,6 +32,7 @@
 #include <drm/drm_plane_helper.h>
 #include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_fourcc.h>
+#include <drm/drm_colorop.h>
 
 #include "amdgpu.h"
 #include "dc.h"
@@ -1208,6 +1209,23 @@ int amdgpu_dm_plane_fill_plane_buffer_attributes(struct 
amdgpu_device *adev,
 }
 EXPORT_IF_KUNIT(amdgpu_dm_plane_fill_plane_buffer_attributes);
 
+/* Pinned by the colorop's prepare_fb, which runs before the plane's */
+static struct drm_framebuffer *
+dm_plane_lut3d_dma_fb(struct drm_plane_state *plane_state)
+{
+       struct drm_colorop *colorop;
+
+       for (colorop = plane_state->color_pipeline; colorop; colorop = 
colorop->next) {
+               struct drm_colorop_state *cs =
+                       drm_atomic_get_new_colorop_state(plane_state->state, 
colorop);
+
+               if (amdgpu_dm_colorop_dma_3dlut_active(colorop, cs))
+                       return cs->fb;
+       }
+
+       return NULL;
+}
+
 STATIC_IFN_KUNIT int amdgpu_dm_plane_helper_prepare_fb(struct drm_plane *plane,
                                                       struct drm_plane_state 
*new_state)
 {
@@ -1216,6 +1234,7 @@ STATIC_IFN_KUNIT int 
amdgpu_dm_plane_helper_prepare_fb(struct drm_plane *plane,
        struct amdgpu_device *adev;
        struct amdgpu_bo *rbo;
        struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
+       struct drm_framebuffer *lut_fb;
        uint32_t domain;
        int r;
 
@@ -1295,6 +1314,15 @@ STATIC_IFN_KUNIT int 
amdgpu_dm_plane_helper_prepare_fb(struct drm_plane *plane,
                        &plane_state->tiling_info, &plane_state->plane_size,
                        &plane_state->dcc, &plane_state->address,
                        afb->tmz_surface);
+
+               lut_fb = dm_plane_lut3d_dma_fb(new_state);
+               if (lut_fb) {
+                       uint64_t addr = to_amdgpu_framebuffer(lut_fb)->address +
+                                       lut_fb->offsets[0];
+
+                       plane_state->cm.lut3d_dma.addr.grph.addr.low_part = 
lower_32_bits(addr);
+                       plane_state->cm.lut3d_dma.addr.grph.addr.high_part = 
upper_32_bits(addr);
+               }
        }
 
        return 0;
@@ -1533,6 +1561,16 @@ STATIC_IFN_KUNIT int amdgpu_dm_plane_atomic_check(struct 
drm_plane *plane,
 
        dm_plane_state = to_dm_plane_state(new_plane_state);
 
+       /*
+        * Pull the plane's colorops into the atomic state so the checks below
+        * see them, even on a plane-only update that does not touch color.
+        */
+       if (new_plane_state->color_pipeline) {
+               ret = drm_atomic_add_affected_colorops(state, plane);
+               if (ret)
+                       return ret;
+       }
+
        if (!dm_plane_state->dc_state)
                return 0;
 
@@ -2375,4 +2413,3 @@ bool amdgpu_dm_plane_is_video_format(uint32_t format)
        return false;
 }
 EXPORT_IF_KUNIT(amdgpu_dm_plane_is_video_format);
-
-- 
2.56.0

Reply via email to