#include <kunit/test.h>
#include <drm/drm_colorop.h>
#include <drm/drm_kunit_helpers.h>
#include "dc.h"
#include "amdgpu.h"
#include "amdgpu_dm_colorop.h"
#include "amdgpu_dm_kunit_test_helpers.h"
static void dm_test_supported_degam_tfs_has_srgb_eotf(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, amdgpu_dm_supported_degam_tfs &
BIT(DRM_COLOROP_1D_CURVE_SRGB_EOTF));
}
static void dm_test_supported_degam_tfs_has_pq125_eotf(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, amdgpu_dm_supported_degam_tfs &
BIT(DRM_COLOROP_1D_CURVE_PQ_125_EOTF));
}
static void dm_test_supported_degam_tfs_has_bt2020_inv_oetf(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, amdgpu_dm_supported_degam_tfs &
BIT(DRM_COLOROP_1D_CURVE_BT2020_INV_OETF));
}
static void dm_test_supported_degam_tfs_has_gamma22(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, amdgpu_dm_supported_degam_tfs &
BIT(DRM_COLOROP_1D_CURVE_GAMMA22));
}
static void dm_test_supported_degam_tfs_no_extra_bits(struct kunit *test)
{
u64 expected = BIT(DRM_COLOROP_1D_CURVE_SRGB_EOTF) |
BIT(DRM_COLOROP_1D_CURVE_PQ_125_EOTF) |
BIT(DRM_COLOROP_1D_CURVE_BT2020_INV_OETF) |
BIT(DRM_COLOROP_1D_CURVE_GAMMA22);
KUNIT_EXPECT_EQ(test, amdgpu_dm_supported_degam_tfs, expected);
}
static void dm_test_supported_shaper_tfs_has_srgb_inv_eotf(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, amdgpu_dm_supported_shaper_tfs &
BIT(DRM_COLOROP_1D_CURVE_SRGB_INV_EOTF));
}
static void dm_test_supported_shaper_tfs_has_pq125_inv_eotf(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, amdgpu_dm_supported_shaper_tfs &
BIT(DRM_COLOROP_1D_CURVE_PQ_125_INV_EOTF));
}
static void dm_test_supported_shaper_tfs_has_bt2020_oetf(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, amdgpu_dm_supported_shaper_tfs &
BIT(DRM_COLOROP_1D_CURVE_BT2020_OETF));
}
static void dm_test_supported_shaper_tfs_has_gamma22_inv(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, amdgpu_dm_supported_shaper_tfs &
BIT(DRM_COLOROP_1D_CURVE_GAMMA22_INV));
}
static void dm_test_supported_shaper_tfs_no_extra_bits(struct kunit *test)
{
u64 expected = BIT(DRM_COLOROP_1D_CURVE_SRGB_INV_EOTF) |
BIT(DRM_COLOROP_1D_CURVE_PQ_125_INV_EOTF) |
BIT(DRM_COLOROP_1D_CURVE_BT2020_OETF) |
BIT(DRM_COLOROP_1D_CURVE_GAMMA22_INV);
KUNIT_EXPECT_EQ(test, amdgpu_dm_supported_shaper_tfs, expected);
}
static void dm_test_supported_blnd_tfs_has_srgb_eotf(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, amdgpu_dm_supported_blnd_tfs &
BIT(DRM_COLOROP_1D_CURVE_SRGB_EOTF));
}
static void dm_test_supported_blnd_tfs_has_pq125_eotf(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, amdgpu_dm_supported_blnd_tfs &
BIT(DRM_COLOROP_1D_CURVE_PQ_125_EOTF));
}
static void dm_test_supported_blnd_tfs_has_bt2020_inv_oetf(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, amdgpu_dm_supported_blnd_tfs &
BIT(DRM_COLOROP_1D_CURVE_BT2020_INV_OETF));
}
static void dm_test_supported_blnd_tfs_has_gamma22(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, amdgpu_dm_supported_blnd_tfs &
BIT(DRM_COLOROP_1D_CURVE_GAMMA22));
}
static void dm_test_supported_blnd_tfs_no_extra_bits(struct kunit *test)
{
u64 expected = BIT(DRM_COLOROP_1D_CURVE_SRGB_EOTF) |
BIT(DRM_COLOROP_1D_CURVE_PQ_125_EOTF) |
BIT(DRM_COLOROP_1D_CURVE_BT2020_INV_OETF) |
BIT(DRM_COLOROP_1D_CURVE_GAMMA22);
KUNIT_EXPECT_EQ(test, amdgpu_dm_supported_blnd_tfs, expected);
}
static void dm_test_degam_and_blnd_tfs_match(struct kunit *test)
{
KUNIT_EXPECT_EQ(test, amdgpu_dm_supported_degam_tfs,
amdgpu_dm_supported_blnd_tfs);
}
static void kunit_colorop_pipeline_destroy(void *drm)
{
drm_colorop_pipeline_destroy((struct drm_device *)drm);
}
static struct drm_plane *dm_colorop_alloc_plane(struct kunit *test, struct drm_device **drm)
{
struct drm_plane *plane;
struct device *dev;
dev = drm_kunit_helper_alloc_device(test);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
*drm = __drm_kunit_helper_alloc_drm_device(test, dev, sizeof(**drm), 0, 0);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, *drm);
plane = drm_kunit_helper_create_primary_plane(test, *drm, NULL, NULL, NULL, 0, NULL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, plane);
kunit_add_action(test, kunit_colorop_pipeline_destroy, *drm);
return plane;
}
static void dm_expect_colorop_pipeline(struct kunit *test, struct drm_device *drm,
const struct drm_prop_enum_list *list,
const enum drm_colorop_type *expected,
int expected_count)
{
struct drm_colorop *op, *first = NULL;
int i = 0;
drm_for_each_colorop(op, drm) {
if (op->base.id == (uint32_t)list->type) {
first = op;
break;
}
}
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, first);
for (op = first; op; op = op->next, i++) {
KUNIT_ASSERT_LT(test, i, expected_count);
KUNIT_EXPECT_EQ(test, op->type, expected[i]);
KUNIT_EXPECT_NOT_NULL(test, op->bypass_property);
}
KUNIT_EXPECT_EQ(test, i, expected_count);
}
static void dm_test_initialize_default_pipeline(struct kunit *test)
{
static const enum drm_colorop_type expected[] = {
DRM_COLOROP_1D_CURVE,
DRM_COLOROP_MULTIPLIER,
DRM_COLOROP_CTM_3X4,
DRM_COLOROP_1D_CURVE,
DRM_COLOROP_1D_LUT,
DRM_COLOROP_3D_LUT,
DRM_COLOROP_1D_CURVE,
DRM_COLOROP_1D_LUT,
};
struct drm_device *drm;
struct drm_plane *plane;
struct drm_prop_enum_list list = {};
int ret;
plane = dm_colorop_alloc_plane(test, &drm);
ret = amdgpu_dm_build_default_pipeline(drm, plane, true, &list);
KUNIT_ASSERT_EQ(test, ret, 0);
kfree(list.name);
dm_expect_colorop_pipeline(test, drm, &list, expected, ARRAY_SIZE(expected));
}
static void dm_test_initialize_default_pipeline_caps(struct kunit *test,
bool dpp_hw_3d_lut,
bool mpc_preblend,
const enum drm_colorop_type *expected,
int expected_count)
{
struct drm_prop_enum_list list = {};
struct amdgpu_device *adev;
struct drm_device *drm;
struct drm_plane *plane;
struct dc *dc;
int ret;
adev = dm_kunit_alloc_adev(test);
drm = &adev->ddev;
dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);
adev->dm.dc = dc;
adev->dm.dc->caps.color.dpp.hw_3d_lut = dpp_hw_3d_lut;
adev->dm.dc->caps.color.mpc.preblend = mpc_preblend;
plane = drm_kunit_helper_create_primary_plane(test, drm,
NULL, NULL, NULL, 0, NULL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, plane);
kunit_add_action(test, kunit_colorop_pipeline_destroy, drm);
ret = amdgpu_dm_initialize_default_pipeline(plane, &list);
KUNIT_ASSERT_EQ(test, ret, 0);
kfree(list.name);
dm_expect_colorop_pipeline(test, drm, &list, expected, expected_count);
}
static void dm_test_initialize_default_pipeline_dpp_3d_lut(struct kunit *test)
{
static const enum drm_colorop_type expected[] = {
DRM_COLOROP_1D_CURVE,
DRM_COLOROP_MULTIPLIER,
DRM_COLOROP_CTM_3X4,
DRM_COLOROP_1D_CURVE,
DRM_COLOROP_1D_LUT,
DRM_COLOROP_3D_LUT,
DRM_COLOROP_1D_CURVE,
DRM_COLOROP_1D_LUT,
};
dm_test_initialize_default_pipeline_caps(test, true, false,
expected, ARRAY_SIZE(expected));
}
static void dm_test_initialize_default_pipeline_mpc_preblend(struct kunit *test)
{
static const enum drm_colorop_type expected[] = {
DRM_COLOROP_1D_CURVE,
DRM_COLOROP_MULTIPLIER,
DRM_COLOROP_CTM_3X4,
DRM_COLOROP_1D_CURVE,
DRM_COLOROP_1D_LUT,
DRM_COLOROP_3D_LUT,
DRM_COLOROP_1D_CURVE,
DRM_COLOROP_1D_LUT,
};
dm_test_initialize_default_pipeline_caps(test, false, true,
expected, ARRAY_SIZE(expected));
}
static void dm_test_initialize_default_pipeline_no_3d_lut(struct kunit *test)
{
static const enum drm_colorop_type expected[] = {
DRM_COLOROP_1D_CURVE,
DRM_COLOROP_MULTIPLIER,
DRM_COLOROP_CTM_3X4,
DRM_COLOROP_1D_CURVE,
DRM_COLOROP_1D_LUT,
};
dm_test_initialize_default_pipeline_caps(test, false, false,
expected, ARRAY_SIZE(expected));
}
struct dm_colorop_fault {
int alloc_fail_at;
int init_fail_at;
int allocs;
int inits;
};
static struct dm_colorop_fault dm_colorop_fault;
static struct drm_colorop *dm_colorop_test_alloc(void)
{
if (dm_colorop_fault.allocs++ == dm_colorop_fault.alloc_fail_at)
return NULL;
return kzalloc_obj(struct drm_colorop);
}
static int dm_colorop_test_init_ret(int ret)
{
if (ret)
return ret;
if (dm_colorop_fault.inits++ == dm_colorop_fault.init_fail_at)
return -EINVAL;
return 0;
}
static int dm_colorop_test_curve_1d_init(struct drm_device *dev, struct drm_colorop *colorop,
struct drm_plane *plane,
const struct drm_colorop_funcs *funcs,
u64 supported_tfs, uint32_t flags)
{
return dm_colorop_test_init_ret(drm_plane_colorop_curve_1d_init(dev, colorop, plane,
funcs, supported_tfs,
flags));
}
static int dm_colorop_test_1d_lut_init(struct drm_device *dev, struct drm_colorop *colorop,
struct drm_plane *plane,
const struct drm_colorop_funcs *funcs,
uint32_t lut_size,
enum drm_colorop_lut1d_interpolation_type interpolation,
uint32_t flags)
{
return dm_colorop_test_init_ret(drm_plane_colorop_curve_1d_lut_init(dev, colorop, plane,
funcs, lut_size,
interpolation,
flags));
}
static int dm_colorop_test_ctm_3x4_init(struct drm_device *dev, struct drm_colorop *colorop,
struct drm_plane *plane,
const struct drm_colorop_funcs *funcs, uint32_t flags)
{
return dm_colorop_test_init_ret(drm_plane_colorop_ctm_3x4_init(dev, colorop, plane,
funcs, flags));
}
static int dm_colorop_test_mult_init(struct drm_device *dev, struct drm_colorop *colorop,
struct drm_plane *plane,
const struct drm_colorop_funcs *funcs, uint32_t flags)
{
return dm_colorop_test_init_ret(drm_plane_colorop_mult_init(dev, colorop, plane,
funcs, flags));
}
static int dm_colorop_test_3dlut_init(struct drm_device *dev, struct drm_colorop *colorop,
struct drm_plane *plane,
const struct drm_colorop_funcs *funcs, uint32_t lut_size,
enum drm_colorop_lut3d_interpolation_type interpolation,
uint32_t flags)
{
return dm_colorop_test_init_ret(drm_plane_colorop_3dlut_init(dev, colorop, plane, funcs,
lut_size, interpolation,
flags));
}
static const struct amdgpu_dm_colorop_kunit_ops dm_colorop_test_ops = {
.colorop_kzalloc_obj = dm_colorop_test_alloc,
.curve_1d_init = dm_colorop_test_curve_1d_init,
.curve_1d_lut_init = dm_colorop_test_1d_lut_init,
.ctm_3x4_init = dm_colorop_test_ctm_3x4_init,
.mult_init = dm_colorop_test_mult_init,
.lut3d_init = dm_colorop_test_3dlut_init,
};
static void dm_colorop_test_reset_ops(void *unused)
{
amdgpu_dm_colorop_kunit_set_ops(NULL);
}
static int dm_colorop_build_with_fault(struct kunit *test, struct drm_device **drm,
int alloc_fail_at, int init_fail_at)
{
struct drm_prop_enum_list list = {};
struct drm_plane *plane;
int ret;
plane = dm_colorop_alloc_plane(test, drm);
dm_colorop_fault = (struct dm_colorop_fault){
.alloc_fail_at = alloc_fail_at,
.init_fail_at = init_fail_at,
};
amdgpu_dm_colorop_kunit_set_ops(&dm_colorop_test_ops);
KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, dm_colorop_test_reset_ops, NULL), 0);
ret = amdgpu_dm_build_default_pipeline(*drm, plane, true, &list);
kfree(list.name);
return ret;
}
struct dm_colorop_step_param {
const char *name;
int index;
};
static const struct dm_colorop_step_param dm_colorop_step_params[] = {
{ "degam_tf", 0 },
{ "multiplier", 1 },
{ "ctm_3x4", 2 },
{ "shaper_tf", 3 },
{ "shaper_lut", 4 },
{ "lut3d", 5 },
{ "blnd_tf", 6 },
{ "blnd_lut", 7 },
};
KUNIT_ARRAY_PARAM_DESC(dm_colorop_step, dm_colorop_step_params, name);
static void dm_test_build_pipeline_alloc_fail(struct kunit *test)
{
const struct dm_colorop_step_param *param = test->param_value;
struct drm_device *drm;
int ret;
ret = dm_colorop_build_with_fault(test, &drm, param->index, -1);
KUNIT_EXPECT_EQ(test, ret, -ENOMEM);
KUNIT_EXPECT_EQ(test, dm_colorop_fault.allocs, param->index + 1);
KUNIT_EXPECT_EQ(test, dm_colorop_fault.inits, param->index);
KUNIT_EXPECT_EQ(test, drm->mode_config.num_colorop, 0);
}
static void dm_test_build_pipeline_init_fail(struct kunit *test)
{
const struct dm_colorop_step_param *param = test->param_value;
struct drm_device *drm;
int ret;
ret = dm_colorop_build_with_fault(test, &drm, -1, param->index);
KUNIT_EXPECT_EQ(test, ret, -EINVAL);
KUNIT_EXPECT_EQ(test, dm_colorop_fault.allocs, param->index + 1);
KUNIT_EXPECT_EQ(test, dm_colorop_fault.inits, param->index + 1);
KUNIT_EXPECT_EQ(test, drm->mode_config.num_colorop, 0);
}
static struct kunit_case dm_colorop_test_cases[] = {
KUNIT_CASE(dm_test_supported_degam_tfs_has_srgb_eotf),
KUNIT_CASE(dm_test_supported_degam_tfs_has_pq125_eotf),
KUNIT_CASE(dm_test_supported_degam_tfs_has_bt2020_inv_oetf),
KUNIT_CASE(dm_test_supported_degam_tfs_has_gamma22),
KUNIT_CASE(dm_test_supported_degam_tfs_no_extra_bits),
KUNIT_CASE(dm_test_supported_shaper_tfs_has_srgb_inv_eotf),
KUNIT_CASE(dm_test_supported_shaper_tfs_has_pq125_inv_eotf),
KUNIT_CASE(dm_test_supported_shaper_tfs_has_bt2020_oetf),
KUNIT_CASE(dm_test_supported_shaper_tfs_has_gamma22_inv),
KUNIT_CASE(dm_test_supported_shaper_tfs_no_extra_bits),
KUNIT_CASE(dm_test_supported_blnd_tfs_has_srgb_eotf),
KUNIT_CASE(dm_test_supported_blnd_tfs_has_pq125_eotf),
KUNIT_CASE(dm_test_supported_blnd_tfs_has_bt2020_inv_oetf),
KUNIT_CASE(dm_test_supported_blnd_tfs_has_gamma22),
KUNIT_CASE(dm_test_supported_blnd_tfs_no_extra_bits),
KUNIT_CASE(dm_test_degam_and_blnd_tfs_match),
KUNIT_CASE(dm_test_initialize_default_pipeline),
KUNIT_CASE(dm_test_initialize_default_pipeline_dpp_3d_lut),
KUNIT_CASE(dm_test_initialize_default_pipeline_mpc_preblend),
KUNIT_CASE(dm_test_initialize_default_pipeline_no_3d_lut),
KUNIT_CASE_PARAM(dm_test_build_pipeline_alloc_fail, dm_colorop_step_gen_params),
KUNIT_CASE_PARAM(dm_test_build_pipeline_init_fail, dm_colorop_step_gen_params),
{}
};
static struct kunit_suite dm_colorop_test_suite = {
.name = "amdgpu_dm_colorop",
.test_cases = dm_colorop_test_cases,
};
kunit_test_suite(dm_colorop_test_suite);
MODULE_LICENSE("Dual MIT/GPL");
MODULE_DESCRIPTION("KUnit tests for amdgpu_dm_colorop");
MODULE_AUTHOR("AMD"