This patch adds colorops for custom 1D LUTs in the SHAPER and
BLND HW blocks.

With this change the following IGT tests pass:
kms_colorop --run plane-XR30-XR30-srgb_inv_eotf_lut
kms_colorop --run plane-XR30-XR30-srgb_inv_eotf_lut-srgb_eotf_lut

The color pipeline now consists of the following colorops:
1. 1D curve colorop
2. 1D curve colorop
3. 1D LUT
4. 1D curve colorop
5. 1D LUT

The 1D curve colorops support sRGB, BT2020, and PQ scaled to 125.0.

Signed-off-by: Alex Hung <alex.h...@amd.com>
Signed-off-by: Harry Wentland <harry.wentl...@amd.com>
Reviewed-by: Daniel Stone <dani...@collabora.com>
---
V10:
 - Support 32BIT RGB in 1D LUT with drm_color_lut_32 (Uma Shankar)
 - Remove redundant DRM_ERROR(...)

V9:
 - Update function names by _plane_ (Chaitanya Kumar Borah)

v8:
 - Return error when __set_output_tf fails (Leo Li)
 - 

v7:
 - Initialize uint32_t blend_size = 0 by default (kernel test robot)
 - Modify state->size to colorop->lut_size

 .../amd/display/amdgpu_dm/amdgpu_dm_color.c   | 306 +++++++++++++-----
 .../amd/display/amdgpu_dm/amdgpu_dm_colorop.c |  30 ++
 2 files changed, 257 insertions(+), 79 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c 
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c
index f645f9ded95f..2b97a262b1c0 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_color.c
@@ -73,6 +73,7 @@
  */
 
 #define MAX_DRM_LUT_VALUE 0xFFFF
+#define MAX_DRM_LUT_32_VALUE 0xFFFFFFFF
 #define SDR_WHITE_LEVEL_INIT_VALUE 80
 
 /**
@@ -342,6 +343,21 @@ __extract_blob_lut(const struct drm_property_blob *blob, 
uint32_t *size)
        return blob ? (struct drm_color_lut *)blob->data : NULL;
 }
 
+/**
+ * __extract_blob_lut_32 - Extracts the DRM lut and lut size from a blob.
+ * @blob: DRM color mgmt property blob
+ * @size: lut size
+ *
+ * Returns:
+ * DRM LUT or NULL
+ */
+static const struct drm_color_lut_32 *
+__extract_blob_lut_32(const struct drm_property_blob *blob, uint32_t *size)
+{
+       *size = blob ? drm_color_lut_32_size(blob) : 0;
+       return blob ? (struct drm_color_lut_32 *)blob->data : NULL;
+}
+
 /**
  * __is_lut_linear - check if the given lut is a linear mapping of values
  * @lut: given lut to check values
@@ -415,6 +431,30 @@ static void __drm_lut_to_dc_gamma(const struct 
drm_color_lut *lut,
        }
 }
 
+/**
+ * __drm_lut_32_to_dc_gamma - convert the drm_color_lut to dc_gamma.
+ * @lut: DRM lookup table for color conversion
+ * @gamma: DC gamma to set entries
+ * @is_legacy: legacy or atomic gamma
+ *
+ * The conversion depends on the size of the lut - whether or not it's legacy.
+ */
+static void __drm_lut_32_to_dc_gamma(const struct drm_color_lut_32 *lut, 
struct dc_gamma *gamma)
+{
+       uint32_t r, g, b;
+       int i;
+
+       for (i = 0; i < MAX_COLOR_LUT_ENTRIES; i++) {
+               r = drm_color_lut_extract(lut[i].red, 32);
+               g = drm_color_lut_extract(lut[i].green, 32);
+               b = drm_color_lut_extract(lut[i].blue, 32);
+
+               gamma->entries.red[i] = dc_fixpt_from_fraction(r, 
MAX_DRM_LUT_32_VALUE);
+               gamma->entries.green[i] = dc_fixpt_from_fraction(g, 
MAX_DRM_LUT_32_VALUE);
+               gamma->entries.blue[i] = dc_fixpt_from_fraction(b, 
MAX_DRM_LUT_32_VALUE);
+       }
+}
+
 /**
  * __drm_ctm_to_dc_matrix - converts a DRM CTM to a DC CSC float matrix
  * @ctm: DRM color transformation matrix
@@ -567,6 +607,63 @@ static int __set_output_tf(struct dc_transfer_func *func,
        return res ? 0 : -ENOMEM;
 }
 
+/**
+ * __set_output_tf_32 - calculates the output transfer function based on 
expected input space.
+ * @func: transfer function
+ * @lut: lookup table that defines the color space
+ * @lut_size: size of respective lut
+ * @has_rom: if ROM can be used for hardcoded curve
+ *
+ * Returns:
+ * 0 in case of success. -ENOMEM if fails.
+ */
+static int __set_output_tf_32(struct dc_transfer_func *func,
+                             const struct drm_color_lut_32 *lut, uint32_t 
lut_size,
+                             bool has_rom)
+{
+       struct dc_gamma *gamma = NULL;
+       struct calculate_buffer cal_buffer = {0};
+       bool res;
+
+       cal_buffer.buffer_index = -1;
+
+       if (lut_size) {
+               gamma = dc_create_gamma();
+               if (!gamma)
+                       return -ENOMEM;
+
+               gamma->num_entries = lut_size;
+               __drm_lut_32_to_dc_gamma(lut, gamma);
+       }
+
+       if (func->tf == TRANSFER_FUNCTION_LINEAR) {
+               /*
+                * Color module doesn't like calculating regamma params
+                * on top of a linear input. But degamma params can be used
+                * instead to simulate this.
+                */
+               if (gamma)
+                       gamma->type = GAMMA_CUSTOM;
+               res = mod_color_calculate_degamma_params(NULL, func,
+                                                        gamma, gamma != NULL);
+       } else {
+               /*
+                * Assume sRGB. The actual mapping will depend on whether the
+                * input was legacy or not.
+                */
+               if (gamma)
+                       gamma->type = GAMMA_CS_TFM_1D;
+               res = mod_color_calculate_regamma_params(func, gamma, gamma != 
NULL,
+                                                        has_rom, NULL, 
&cal_buffer);
+       }
+
+       if (gamma)
+               dc_gamma_release(&gamma);
+
+       return res ? 0 : -ENOMEM;
+}
+
+
 static int amdgpu_dm_set_atomic_regamma(struct dc_stream_state *stream,
                                        const struct drm_color_lut *regamma_lut,
                                        uint32_t regamma_size, bool has_rom,
@@ -640,6 +737,42 @@ static int __set_input_tf(struct dc_color_caps *caps, 
struct dc_transfer_func *f
        return res ? 0 : -ENOMEM;
 }
 
+/**
+ * __set_input_tf_32 - calculates the input transfer function based on expected
+ * input space.
+ * @caps: dc color capabilities
+ * @func: transfer function
+ * @lut: lookup table that defines the color space
+ * @lut_size: size of respective lut.
+ *
+ * Returns:
+ * 0 in case of success. -ENOMEM if fails.
+ */
+static int __set_input_tf_32(struct dc_color_caps *caps, struct 
dc_transfer_func *func,
+                            const struct drm_color_lut_32 *lut, uint32_t 
lut_size)
+{
+       struct dc_gamma *gamma = NULL;
+       bool res;
+
+       if (lut_size) {
+               gamma = dc_create_gamma();
+               if (!gamma)
+                       return -ENOMEM;
+
+               gamma->type = GAMMA_CUSTOM;
+               gamma->num_entries = lut_size;
+
+               __drm_lut_32_to_dc_gamma(lut, gamma);
+       }
+
+       res = mod_color_calculate_degamma_params(caps, func, gamma, gamma != 
NULL);
+
+       if (gamma)
+               dc_gamma_release(&gamma);
+
+       return res ? 0 : -ENOMEM;
+}
+
 static enum dc_transfer_func_predefined
 amdgpu_tf_to_dc_tf(enum amdgpu_transfer_function tf)
 {
@@ -1211,38 +1344,6 @@ __set_dm_plane_colorop_degamma(struct drm_plane_state 
*plane_state,
        return __set_colorop_in_tf_1d_curve(dc_plane_state, colorop_state);
 }
 
-static int
-__set_colorop_in_shaper_1d_curve(struct dc_plane_state *dc_plane_state,
-                      struct drm_colorop_state *colorop_state)
-{
-       struct dc_transfer_func *tf = &dc_plane_state->in_shaper_func;
-       struct drm_colorop *colorop = colorop_state->colorop;
-       struct drm_device *drm = colorop->dev;
-
-       if (colorop->type != DRM_COLOROP_1D_CURVE)
-               return -EINVAL;
-
-       if (!(BIT(colorop_state->curve_1d_type) & 
amdgpu_dm_supported_shaper_tfs))
-               return -EINVAL;
-
-       if (colorop_state->bypass) {
-               tf->type = TF_TYPE_BYPASS;
-               tf->tf = TRANSFER_FUNCTION_LINEAR;
-               return 0;
-       }
-
-       drm_dbg(drm, "Shaper colorop with ID: %d\n", colorop->base.id);
-
-       if (colorop->type == DRM_COLOROP_1D_CURVE) {
-               tf->type = TF_TYPE_DISTRIBUTED_POINTS;
-               tf->tf = 
amdgpu_colorop_tf_to_dc_tf(colorop_state->curve_1d_type);
-               tf->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
-               return __set_output_tf(tf, 0, 0, false);
-       }
-
-       return -EINVAL;
-}
-
 static int
 __set_dm_plane_colorop_shaper(struct drm_plane_state *plane_state,
                              struct dc_plane_state *dc_plane_state,
@@ -1251,64 +1352,66 @@ __set_dm_plane_colorop_shaper(struct drm_plane_state 
*plane_state,
        struct drm_colorop *old_colorop;
        struct drm_colorop_state *colorop_state = NULL, *new_colorop_state;
        struct drm_atomic_state *state = plane_state->state;
-       int i = 0;
+       enum dc_transfer_func_predefined default_tf = TRANSFER_FUNCTION_LINEAR;
+       struct dc_transfer_func *tf = &dc_plane_state->in_shaper_func;
+       const struct drm_color_lut_32 *shaper_lut;
+       struct drm_device *dev = colorop->dev;
+       uint32_t shaper_size;
+       int i = 0, ret = 0;
 
+       /* 1D Curve - SHAPER TF */
        old_colorop = colorop;
-
-       /* 2nd op: 1d curve - shaper */
        for_each_new_colorop_in_state(state, colorop, new_colorop_state, i) {
                if (new_colorop_state->colorop == old_colorop &&
                    (BIT(new_colorop_state->curve_1d_type) & 
amdgpu_dm_supported_shaper_tfs)) {
                        colorop_state = new_colorop_state;
                        break;
                }
-
-               if (new_colorop_state->colorop == old_colorop) {
-                       colorop_state = new_colorop_state;
-                       break;
-               }
        }
 
-       if (!colorop_state)
-               return -EINVAL;
-
-       return __set_colorop_in_shaper_1d_curve(dc_plane_state, colorop_state);
-}
-
-
-static int
-__set_colorop_1d_curve_blend_tf_lut(struct dc_plane_state *dc_plane_state,
-                                 struct drm_colorop_state *colorop_state)
-{
-
-       struct dc_transfer_func *tf = &dc_plane_state->blend_tf;
-       struct drm_colorop *colorop = colorop_state->colorop;
-       struct drm_device *drm = colorop->dev;
-       const struct drm_color_lut *blend_lut;
-       uint32_t blend_size = 0;
-
-       if (colorop->type != DRM_COLOROP_1D_CURVE)
-               return -EINVAL;
+       if (colorop_state && !colorop_state->bypass && colorop->type == 
DRM_COLOROP_1D_CURVE) {
+               drm_dbg(dev, "Shaper TF colorop with ID: %d\n", 
colorop->base.id);
+               tf->type = TF_TYPE_DISTRIBUTED_POINTS;
+               tf->tf = default_tf = 
amdgpu_colorop_tf_to_dc_tf(colorop_state->curve_1d_type);
+               tf->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
+               ret = __set_output_tf(tf, 0, 0, false);
+               if (ret)
+                       return ret;
+       }
 
-       if (!(BIT(colorop_state->curve_1d_type) & amdgpu_dm_supported_blnd_tfs))
+       /* 1D LUT - SHAPER LUT */
+       colorop = old_colorop->next;
+       if (!colorop) {
+               drm_dbg(dev, "no Shaper LUT colorop found\n");
                return -EINVAL;
-
-       if (colorop_state->bypass) {
-               tf->type = TF_TYPE_BYPASS;
-               tf->tf = TRANSFER_FUNCTION_LINEAR;
-               return 0;
        }
 
-       drm_dbg(drm, "Blend colorop with ID: %d\n", colorop->base.id);
+       old_colorop = colorop;
+       for_each_new_colorop_in_state(state, colorop, new_colorop_state, i) {
+               if (new_colorop_state->colorop == old_colorop &&
+                   new_colorop_state->colorop->type == DRM_COLOROP_1D_LUT) {
+                       colorop_state = new_colorop_state;
+                       break;
+               }
+       }
 
-       if (colorop->type == DRM_COLOROP_1D_CURVE) {
+       if (colorop_state && !colorop_state->bypass && colorop->type == 
DRM_COLOROP_1D_LUT) {
+               drm_dbg(dev, "Shaper LUT colorop with ID: %d\n", 
colorop->base.id);
                tf->type = TF_TYPE_DISTRIBUTED_POINTS;
-               tf->tf = 
amdgpu_colorop_tf_to_dc_tf(colorop_state->curve_1d_type);
+               tf->tf = default_tf;
                tf->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
-               return __set_input_tf(NULL, tf, blend_lut, blend_size);
+               shaper_lut = __extract_blob_lut_32(colorop_state->data, 
&shaper_size);
+               shaper_size = shaper_lut != NULL ? shaper_size : 0;
+
+               /* Custom LUT size must be the same as supported size */
+               if (shaper_size == colorop->lut_size) {
+                       ret = __set_output_tf_32(tf, shaper_lut, shaper_size, 
false);
+                       if (ret)
+                               return ret;
+               }
        }
 
-       return -EINVAL;
+       return 0;
 }
 
 static int
@@ -1319,28 +1422,63 @@ __set_dm_plane_colorop_blend(struct drm_plane_state 
*plane_state,
        struct drm_colorop *old_colorop;
        struct drm_colorop_state *colorop_state = NULL, *new_colorop_state;
        struct drm_atomic_state *state = plane_state->state;
+       enum dc_transfer_func_predefined default_tf = TRANSFER_FUNCTION_LINEAR;
+       struct dc_transfer_func *tf = &dc_plane_state->blend_tf;
+       const struct drm_color_lut_32 *blend_lut;
+       struct drm_device *dev = colorop->dev;
+       uint32_t blend_size;
        int i = 0;
 
+       /* 1D Curve - BLND TF */
        old_colorop = colorop;
-
-       /* 3nd op: 1d curve - blend */
        for_each_new_colorop_in_state(state, colorop, new_colorop_state, i) {
                if (new_colorop_state->colorop == old_colorop &&
                    (BIT(new_colorop_state->curve_1d_type) & 
amdgpu_dm_supported_blnd_tfs)) {
                        colorop_state = new_colorop_state;
                        break;
                }
+       }
+
+       if (colorop_state && !colorop_state->bypass && colorop->type == 
DRM_COLOROP_1D_CURVE &&
+           (BIT(colorop_state->curve_1d_type) & amdgpu_dm_supported_blnd_tfs)) 
{
+               drm_dbg(dev, "Blend TF colorop with ID: %d\n", 
colorop->base.id);
+               tf->type = TF_TYPE_DISTRIBUTED_POINTS;
+               tf->tf = default_tf = 
amdgpu_colorop_tf_to_dc_tf(colorop_state->curve_1d_type);
+               tf->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
+               __set_input_tf_32(NULL, tf, blend_lut, blend_size);
+       }
 
-               if (new_colorop_state->colorop == old_colorop) {
+       /* 1D Curve - BLND LUT */
+       colorop = old_colorop->next;
+       if (!colorop) {
+               drm_dbg(dev, "no Blend LUT colorop found\n");
+               return -EINVAL;
+       }
+
+       old_colorop = colorop;
+       for_each_new_colorop_in_state(state, colorop, new_colorop_state, i) {
+               if (new_colorop_state->colorop == old_colorop &&
+                   new_colorop_state->colorop->type == DRM_COLOROP_1D_LUT) {
                        colorop_state = new_colorop_state;
                        break;
                }
        }
 
-       if (!colorop_state)
-               return -EINVAL;
+       if (colorop_state && !colorop_state->bypass && colorop->type == 
DRM_COLOROP_1D_LUT &&
+           (BIT(colorop_state->curve_1d_type) & amdgpu_dm_supported_blnd_tfs)) 
{
+               drm_dbg(dev, "Blend LUT colorop with ID: %d\n", 
colorop->base.id);
+               tf->type = TF_TYPE_DISTRIBUTED_POINTS;
+               tf->tf = default_tf;
+               tf->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
+               blend_lut = __extract_blob_lut_32(colorop_state->data, 
&blend_size);
+               blend_size = blend_lut != NULL ? blend_size : 0;
 
-       return __set_colorop_1d_curve_blend_tf_lut(dc_plane_state, 
colorop_state);
+               /* Custom LUT size must be the same as supported size */
+               if (blend_size == colorop->lut_size)
+                       __set_input_tf_32(NULL, tf, blend_lut, blend_size);
+       }
+
+       return 0;
 }
 
 static int
@@ -1409,7 +1547,7 @@ amdgpu_dm_plane_set_colorop_properties(struct 
drm_plane_state *plane_state,
        if (ret)
                return ret;
 
-       /* 1D Curve - SHAPER TF */
+       /* 1D Curve & LUT - SHAPER TF & LUT */
        colorop = colorop->next;
        if (!colorop) {
                drm_dbg(dev, "no Shaper TF colorop found\n");
@@ -1420,7 +1558,12 @@ amdgpu_dm_plane_set_colorop_properties(struct 
drm_plane_state *plane_state,
        if (ret)
                return ret;
 
-       /* 1D Curve - BLND TF */
+       /* Shaper LUT colorop is already handled, just skip here */
+       colorop = colorop->next;
+       if (!colorop)
+               return -EINVAL;
+
+       /* 1D Curve & LUT - BLND TF & LUT */
        colorop = colorop->next;
        if (!colorop) {
                drm_dbg(dev, "no Blend TF colorop found\n");
@@ -1431,6 +1574,11 @@ amdgpu_dm_plane_set_colorop_properties(struct 
drm_plane_state *plane_state,
        if (ret)
                return ret;
 
+       /* BLND LUT colorop is already handled, just skip here */
+       colorop = colorop->next;
+       if (!colorop)
+               return -EINVAL;
+
        return 0;
 }
 
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 5c84c6a5a74a..427fe5050d0d 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,6 +29,7 @@
 #include <drm/drm_property.h>
 #include <drm/drm_colorop.h>
 
+#include "amdgpu.h"
 #include "amdgpu_dm_colorop.h"
 
 const u64 amdgpu_dm_supported_degam_tfs =
@@ -88,6 +89,21 @@ int amdgpu_dm_initialize_default_pipeline(struct drm_plane 
*plane, struct drm_pr
 
        i++;
 
+       /* 1D LUT - SHAPER LUT */
+       ops[i] = kzalloc(sizeof(struct drm_colorop), GFP_KERNEL);
+       if (!ops[i]) {
+               ret = -ENOMEM;
+               goto cleanup;
+       }
+
+       ret = drm_plane_colorop_curve_1d_lut_init(dev, ops[i], plane, 
MAX_COLOR_LUT_ENTRIES);
+       if (ret)
+               goto cleanup;
+
+       drm_colorop_set_next_property(ops[i-1], ops[i]);
+
+       i++;
+
        /* 1D curve - BLND TF */
        ops[i] = kzalloc(sizeof(struct drm_colorop), GFP_KERNEL);
        if (!ops[i]) {
@@ -101,6 +117,20 @@ int amdgpu_dm_initialize_default_pipeline(struct drm_plane 
*plane, struct drm_pr
 
        drm_colorop_set_next_property(ops[i-1], ops[i]);
 
+       i++;
+
+       /* 1D LUT - BLND LUT */
+       ops[i] = kzalloc(sizeof(struct drm_colorop), GFP_KERNEL);
+       if (!ops[i]) {
+               ret = -ENOMEM;
+               goto cleanup;
+       }
+
+       ret = drm_plane_colorop_curve_1d_lut_init(dev, ops[i], plane, 
MAX_COLOR_LUT_ENTRIES);
+       if (ret)
+               goto cleanup;
+
+       drm_colorop_set_next_property(ops[i-1], ops[i]);
        return 0;
 
 cleanup:
-- 
2.43.0

Reply via email to