1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
#include <kunit/test.h>
#include <drm/drm_atomic.h>
#include <drm/drm_connector.h>
#include <drm/drm_crtc.h>
#include <drm/drm_modes.h>
#include <drm/drm_vblank.h>
#include "dc.h"
#include "inc/core_types.h"
#include "amd_shared.h"
#include "amdgpu.h"
#include "amdgpu_mode.h"
#include "amdgpu_dm.h"
#include "amdgpu_dm_freesync.h"
#include "amdgpu_dm_kunit_test_helpers.h"
static u32 dm_test_freesync_vblank_counter(struct drm_crtc *crtc)
{
return 0;
}
static const struct drm_crtc_funcs dm_test_freesync_crtc_funcs = {
.get_vblank_counter = dm_test_freesync_vblank_counter,
};
static void dm_test_freesync_crtc_cleanup(void *data)
{
struct amdgpu_crtc *acrtc = data;
drm_crtc_vblank_off(&acrtc->base);
list_del_init(&acrtc->base.head);
}
static void dm_test_timing_unchanged_null_args(struct kunit *test)
{
struct drm_crtc_state crtc_state = { 0 };
KUNIT_EXPECT_FALSE(test,
amdgpu_dm_is_timing_unchanged_for_freesync(NULL, &crtc_state));
KUNIT_EXPECT_FALSE(test,
amdgpu_dm_is_timing_unchanged_for_freesync(&crtc_state, NULL));
}
static void dm_test_timing_unchanged_identical_modes(struct kunit *test)
{
struct drm_crtc_state old_state = { 0 };
struct drm_crtc_state new_state = { 0 };
old_state.mode.clock = 148500;
old_state.mode.hdisplay = 1920;
old_state.mode.vdisplay = 1080;
old_state.mode.htotal = 2200;
old_state.mode.vtotal = 1125;
new_state.mode = old_state.mode;
KUNIT_EXPECT_FALSE(test,
amdgpu_dm_is_timing_unchanged_for_freesync(&old_state, &new_state));
}
static void dm_test_timing_unchanged_vrr_shift(struct kunit *test)
{
struct drm_crtc_state old_state = { 0 };
struct drm_crtc_state new_state = { 0 };
old_state.mode.clock = 148500;
old_state.mode.hdisplay = 1920;
old_state.mode.vdisplay = 1080;
old_state.mode.htotal = 2200;
old_state.mode.vtotal = 1125;
old_state.mode.hsync_start = 2008;
old_state.mode.vsync_start = 1084;
old_state.mode.hsync_end = 2052;
old_state.mode.vsync_end = 1089;
new_state.mode = old_state.mode;
new_state.mode.vtotal = 1250;
new_state.mode.vsync_start = 1209;
new_state.mode.vsync_end = 1214;
KUNIT_EXPECT_TRUE(test,
amdgpu_dm_is_timing_unchanged_for_freesync(&old_state, &new_state));
}
static void dm_test_timing_unchanged_clock_changed(struct kunit *test)
{
struct drm_crtc_state old_state = { 0 };
struct drm_crtc_state new_state = { 0 };
old_state.mode.clock = 148500;
old_state.mode.htotal = 2200;
old_state.mode.vtotal = 1125;
old_state.mode.vsync_start = 1084;
old_state.mode.vsync_end = 1089;
new_state.mode = old_state.mode;
new_state.mode.clock = 297000;
new_state.mode.vtotal = 1250;
new_state.mode.vsync_start = 1209;
new_state.mode.vsync_end = 1214;
KUNIT_EXPECT_FALSE(test,
amdgpu_dm_is_timing_unchanged_for_freesync(&old_state, &new_state));
}
static void dm_test_set_freesync_fixed_config_60hz(struct kunit *test)
{
struct dm_crtc_state dm_crtc_state = { 0 };
dm_crtc_state.base.mode.clock = 148500;
dm_crtc_state.base.mode.htotal = 2200;
dm_crtc_state.base.mode.vtotal = 1125;
amdgpu_dm_set_freesync_fixed_config(&dm_crtc_state);
KUNIT_EXPECT_EQ(test, (int)dm_crtc_state.freesync_config.state,
(int)VRR_STATE_ACTIVE_FIXED);
KUNIT_EXPECT_EQ(test, dm_crtc_state.freesync_config.fixed_refresh_in_uhz,
60000000U);
}
static void dm_test_dc_timing_adjust_pending(struct kunit *test)
{
struct dm_crtc_state *old_state, *new_state;
struct dc_stream_state *stream;
old_state = kunit_kzalloc(test, sizeof(*old_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, old_state);
new_state = kunit_kzalloc(test, sizeof(*new_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, new_state);
stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
new_state->stream = stream;
stream->adjust.timing_adjust_pending = 1;
KUNIT_EXPECT_TRUE(test, amdgpu_dm_is_dc_timing_adjust_needed(old_state, new_state));
}
static void dm_test_dc_timing_adjust_active_fixed(struct kunit *test)
{
struct dm_crtc_state *old_state, *new_state;
struct dc_stream_state *stream;
old_state = kunit_kzalloc(test, sizeof(*old_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, old_state);
new_state = kunit_kzalloc(test, sizeof(*new_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, new_state);
stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
new_state->stream = stream;
new_state->freesync_config.state = VRR_STATE_ACTIVE_FIXED;
KUNIT_EXPECT_TRUE(test, amdgpu_dm_is_dc_timing_adjust_needed(old_state, new_state));
}
static void dm_test_dc_timing_adjust_vrr_toggle(struct kunit *test)
{
struct dm_crtc_state *old_state, *new_state;
struct dc_stream_state *stream;
old_state = kunit_kzalloc(test, sizeof(*old_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, old_state);
new_state = kunit_kzalloc(test, sizeof(*new_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, new_state);
stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
new_state->stream = stream;
old_state->freesync_config.state = VRR_STATE_ACTIVE_VARIABLE;
new_state->freesync_config.state = VRR_STATE_INACTIVE;
KUNIT_EXPECT_TRUE(test, amdgpu_dm_is_dc_timing_adjust_needed(old_state, new_state));
}
static void dm_test_dc_timing_adjust_not_needed(struct kunit *test)
{
struct dm_crtc_state *old_state, *new_state;
struct dc_stream_state *stream;
old_state = kunit_kzalloc(test, sizeof(*old_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, old_state);
new_state = kunit_kzalloc(test, sizeof(*new_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, new_state);
stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
new_state->stream = stream;
old_state->freesync_config.state = VRR_STATE_INACTIVE;
new_state->freesync_config.state = VRR_STATE_INACTIVE;
KUNIT_EXPECT_FALSE(test, amdgpu_dm_is_dc_timing_adjust_needed(old_state, new_state));
}
struct dm_test_freesync_ctx {
struct amdgpu_dm_connector *aconnector;
struct dm_crtc_state *crtc_state;
struct dm_connector_state *conn_state;
struct dc_stream_state *stream;
};
static struct dm_test_freesync_ctx *dm_test_freesync_ctx_alloc(struct kunit *test)
{
struct dm_test_freesync_ctx *ctx;
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, ctx);
ctx->aconnector = kunit_kzalloc(test, sizeof(*ctx->aconnector), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, ctx->aconnector);
ctx->crtc_state = kunit_kzalloc(test, sizeof(*ctx->crtc_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, ctx->crtc_state);
ctx->conn_state = kunit_kzalloc(test, sizeof(*ctx->conn_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, ctx->conn_state);
ctx->stream = dm_kunit_alloc_stream(test, NULL);
ctx->conn_state->base.connector = &ctx->aconnector->base;
ctx->aconnector->base.connector_type = DRM_MODE_CONNECTOR_DisplayPort;
ctx->crtc_state->stream = ctx->stream;
ctx->crtc_state->base.mode.clock = 148500;
ctx->crtc_state->base.mode.htotal = 2200;
ctx->crtc_state->base.mode.vtotal = 1125;
return ctx;
}
static void dm_test_freesync_config_writeback(struct kunit *test)
{
struct dm_test_freesync_ctx *ctx = dm_test_freesync_ctx_alloc(test);
ctx->aconnector->base.connector_type = DRM_MODE_CONNECTOR_WRITEBACK;
ctx->conn_state->freesync_capable = true;
ctx->aconnector->min_vfreq = 48;
ctx->aconnector->max_vfreq = 120;
ctx->crtc_state->vrr_supported = true;
amdgpu_dm_get_freesync_config_for_crtc(ctx->crtc_state, ctx->conn_state);
KUNIT_EXPECT_TRUE(test, ctx->crtc_state->vrr_supported);
}
static void dm_test_freesync_config_not_capable(struct kunit *test)
{
struct dm_test_freesync_ctx *ctx = dm_test_freesync_ctx_alloc(test);
ctx->conn_state->freesync_capable = false;
ctx->aconnector->min_vfreq = 48;
ctx->aconnector->max_vfreq = 120;
amdgpu_dm_get_freesync_config_for_crtc(ctx->crtc_state, ctx->conn_state);
KUNIT_EXPECT_FALSE(test, ctx->crtc_state->vrr_supported);
KUNIT_EXPECT_EQ(test, (int)ctx->crtc_state->freesync_config.state,
(int)VRR_STATE_UNSUPPORTED);
}
static void dm_test_freesync_config_out_of_range(struct kunit *test)
{
struct dm_test_freesync_ctx *ctx = dm_test_freesync_ctx_alloc(test);
ctx->conn_state->freesync_capable = true;
ctx->aconnector->min_vfreq = 90;
ctx->aconnector->max_vfreq = 120;
amdgpu_dm_get_freesync_config_for_crtc(ctx->crtc_state, ctx->conn_state);
KUNIT_EXPECT_FALSE(test, ctx->crtc_state->vrr_supported);
KUNIT_EXPECT_EQ(test, (int)ctx->crtc_state->freesync_config.state,
(int)VRR_STATE_UNSUPPORTED);
}
static void dm_test_freesync_config_active_variable(struct kunit *test)
{
struct dm_test_freesync_ctx *ctx = dm_test_freesync_ctx_alloc(test);
ctx->conn_state->freesync_capable = true;
ctx->aconnector->min_vfreq = 48;
ctx->aconnector->max_vfreq = 120;
ctx->crtc_state->base.vrr_enabled = true;
amdgpu_dm_get_freesync_config_for_crtc(ctx->crtc_state, ctx->conn_state);
KUNIT_EXPECT_TRUE(test, ctx->crtc_state->vrr_supported);
KUNIT_EXPECT_TRUE(test, ctx->stream->ignore_msa_timing_param);
KUNIT_EXPECT_EQ(test, (int)ctx->crtc_state->freesync_config.state,
(int)VRR_STATE_ACTIVE_VARIABLE);
KUNIT_EXPECT_EQ(test, ctx->crtc_state->freesync_config.min_refresh_in_uhz,
48000000U);
KUNIT_EXPECT_EQ(test, ctx->crtc_state->freesync_config.max_refresh_in_uhz,
120000000U);
KUNIT_EXPECT_TRUE(test, ctx->crtc_state->freesync_config.vsif_supported);
KUNIT_EXPECT_TRUE(test, ctx->crtc_state->freesync_config.btr);
}
static void dm_test_freesync_config_inactive(struct kunit *test)
{
struct dm_test_freesync_ctx *ctx = dm_test_freesync_ctx_alloc(test);
ctx->conn_state->freesync_capable = true;
ctx->aconnector->min_vfreq = 48;
ctx->aconnector->max_vfreq = 120;
ctx->crtc_state->base.vrr_enabled = false;
amdgpu_dm_get_freesync_config_for_crtc(ctx->crtc_state, ctx->conn_state);
KUNIT_EXPECT_TRUE(test, ctx->crtc_state->vrr_supported);
KUNIT_EXPECT_EQ(test, (int)ctx->crtc_state->freesync_config.state,
(int)VRR_STATE_INACTIVE);
}
static void dm_test_freesync_config_active_fixed(struct kunit *test)
{
struct dm_test_freesync_ctx *ctx = dm_test_freesync_ctx_alloc(test);
ctx->conn_state->freesync_capable = true;
ctx->aconnector->min_vfreq = 48;
ctx->aconnector->max_vfreq = 120;
ctx->crtc_state->freesync_config.state = VRR_STATE_ACTIVE_FIXED;
ctx->crtc_state->freesync_config.fixed_refresh_in_uhz = 60000000;
ctx->crtc_state->base.vrr_enabled = true;
amdgpu_dm_get_freesync_config_for_crtc(ctx->crtc_state, ctx->conn_state);
KUNIT_EXPECT_TRUE(test, ctx->crtc_state->vrr_supported);
KUNIT_EXPECT_EQ(test, (int)ctx->crtc_state->freesync_config.state,
(int)VRR_STATE_ACTIVE_FIXED);
KUNIT_EXPECT_EQ(test, ctx->crtc_state->freesync_config.fixed_refresh_in_uhz,
60000000U);
}
static void dm_test_reset_freesync_config(struct kunit *test)
{
struct dm_crtc_state *crtc_state;
crtc_state = kunit_kzalloc(test, sizeof(*crtc_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, crtc_state);
crtc_state->vrr_supported = true;
crtc_state->vrr_infopacket.valid = true;
amdgpu_dm_reset_freesync_config_for_crtc(crtc_state);
KUNIT_EXPECT_FALSE(test, crtc_state->vrr_supported);
KUNIT_EXPECT_FALSE(test, crtc_state->vrr_infopacket.valid);
}
static void dm_test_update_freesync_state_no_stream(struct kunit *test)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct amdgpu_crtc *acrtc;
struct dm_crtc_state crtc_state = { 0 };
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
crtc_state.base.crtc = &acrtc->base;
amdgpu_dm_update_freesync_state_on_stream(&adev->dm, &crtc_state, NULL, NULL, 0);
KUNIT_SUCCEED(test);
}
static void dm_test_update_freesync_state_invalid_timing(struct kunit *test)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct dc_stream_state *stream = dm_kunit_alloc_stream(test, NULL);
struct amdgpu_crtc *acrtc;
struct dm_crtc_state crtc_state = { 0 };
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
crtc_state.base.crtc = &acrtc->base;
amdgpu_dm_update_freesync_state_on_stream(&adev->dm, &crtc_state, stream, NULL, 0);
KUNIT_SUCCEED(test);
}
static void dm_test_update_freesync_state_default_packet(struct kunit *test)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct dc_stream_state *stream = dm_kunit_alloc_stream(test, NULL);
struct amdgpu_crtc *acrtc;
struct dm_crtc_state crtc_state = { 0 };
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
crtc_state.base.crtc = &acrtc->base;
stream->timing.h_total = 2200;
stream->timing.v_total = 1125;
adev->dm.adev = adev;
amdgpu_dm_update_freesync_state_on_stream(&adev->dm, &crtc_state, stream, NULL, 0);
KUNIT_EXPECT_FALSE(test, crtc_state.freesync_vrr_info_changed);
KUNIT_EXPECT_FALSE(test, crtc_state.vrr_infopacket.valid);
KUNIT_EXPECT_FALSE(test, stream->allow_freesync);
}
static void dm_test_update_freesync_state_packet_changed(struct kunit *test)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct dc_stream_state *stream = dm_kunit_alloc_stream(test, NULL);
struct dc_plane_state *surface;
struct amdgpu_crtc *acrtc;
struct dm_crtc_state crtc_state = { 0 };
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
surface = kunit_kzalloc(test, sizeof(*surface), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, surface);
crtc_state.base.crtc = &acrtc->base;
crtc_state.vrr_infopacket.valid = true;
stream->timing.h_total = 2200;
stream->timing.v_total = 1125;
adev->family = AMDGPU_FAMILY_AI;
adev->dm.adev = adev;
amdgpu_dm_update_freesync_state_on_stream(&adev->dm, &crtc_state, stream, surface, 0);
KUNIT_EXPECT_TRUE(test, crtc_state.freesync_vrr_info_changed);
KUNIT_EXPECT_FALSE(test, crtc_state.vrr_infopacket.valid);
}
static void dm_test_update_freesync_state_pre_ai_active(struct kunit *test)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct dc_stream_state *stream = dm_kunit_alloc_stream(test, NULL);
struct dc_plane_state *surface;
struct amdgpu_crtc *acrtc;
struct dm_crtc_state crtc_state = { 0 };
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
surface = kunit_kzalloc(test, sizeof(*surface), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, surface);
crtc_state.base.crtc = &acrtc->base;
crtc_state.stream = stream;
crtc_state.freesync_config.state = VRR_STATE_ACTIVE_VARIABLE;
stream->timing.h_total = 2200;
stream->timing.v_total = 1125;
adev->dm.adev = adev;
adev->dm.dc = dm_kunit_alloc_dc_with_ctx(test);
adev->dm.dc->current_state = dm_kunit_alloc_dc_state(test);
amdgpu_dm_update_freesync_state_on_stream(&adev->dm, &crtc_state, stream, surface, 0);
KUNIT_EXPECT_FALSE(test, stream->adjust.timing_adjust_pending);
}
static void dm_test_update_freesync_state_pcon_version(struct kunit *test, u8 version)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct dc_link *link = dm_kunit_alloc_link(test);
struct dc_stream_state *stream = dm_kunit_alloc_stream(test, link);
struct amdgpu_dm_connector *aconnector;
struct amdgpu_crtc *acrtc;
struct dm_crtc_state crtc_state = { 0 };
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, aconnector);
crtc_state.base.crtc = &acrtc->base;
stream->timing.h_total = 2200;
stream->timing.v_total = 1125;
stream->dm_stream_context = aconnector;
aconnector->as_type = FREESYNC_TYPE_PCON_IN_WHITELIST;
aconnector->vsdb_info.amd_vsdb_version = version;
adev->dm.adev = adev;
amdgpu_dm_update_freesync_state_on_stream(&adev->dm, &crtc_state, stream, NULL, 0);
KUNIT_EXPECT_TRUE(test, stream->adaptive_sync_infopacket.valid);
KUNIT_EXPECT_EQ(test, stream->adaptive_sync_infopacket.hb2, (u8)AS_SDP_VER_1);
}
static void dm_test_update_freesync_state_pcon_v1(struct kunit *test)
{
dm_test_update_freesync_state_pcon_version(test, 1);
}
static void dm_test_update_freesync_state_pcon_v2(struct kunit *test)
{
dm_test_update_freesync_state_pcon_version(test, 2);
}
static void dm_test_update_freesync_state_pcon_v3(struct kunit *test)
{
dm_test_update_freesync_state_pcon_version(test, 3);
}
static void dm_test_update_freesync_state_pcon_default(struct kunit *test)
{
dm_test_update_freesync_state_pcon_version(test, 0);
}
static struct dc_info_packet
dm_test_freesync_packet(struct kunit *test, enum signal_type signal,
u8 vsdb_version, int sink_max_hz, int mode_hz)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct dc_link *link = dm_kunit_alloc_link(test);
struct dc_stream_state *stream = dm_kunit_alloc_stream(test, link);
struct amdgpu_dm_connector *aconnector;
struct mod_vrr_params *vrr;
struct amdgpu_crtc *acrtc;
struct dm_crtc_state crtc_state = { 0 };
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, aconnector);
crtc_state.base.crtc = &acrtc->base;
stream->timing.h_total = 2200;
stream->timing.v_total = 1125;
stream->signal = signal;
stream->dm_stream_context = aconnector;
aconnector->vsdb_info.amd_vsdb_version = vsdb_version;
aconnector->max_vfreq = sink_max_hz;
vrr = &acrtc->dm_irq_params.vrr_params;
vrr->send_info_frame = true;
vrr->state = VRR_STATE_INACTIVE;
vrr->min_refresh_in_uhz = 48000000;
vrr->max_refresh_in_uhz = mode_hz * 1000000;
adev->dm.adev = adev;
amdgpu_dm_update_freesync_state_on_stream(&adev->dm, &crtc_state, stream, NULL, 0);
return crtc_state.vrr_infopacket;
}
static void dm_test_update_freesync_state_dp_v2_saturates(struct kunit *test)
{
struct dc_info_packet p =
dm_test_freesync_packet(test, SIGNAL_TYPE_DISPLAY_PORT, 2, 320, 320);
KUNIT_EXPECT_EQ(test, p.hb3, 0x04);
KUNIT_EXPECT_EQ(test, p.sb[7], 255);
KUNIT_EXPECT_EQ(test, p.sb[8], 255);
}
static void dm_test_update_freesync_state_dp_v3_above_255(struct kunit *test)
{
struct dc_info_packet p =
dm_test_freesync_packet(test, SIGNAL_TYPE_DISPLAY_PORT, 3, 320, 320);
KUNIT_EXPECT_EQ(test, p.hb3, 3 << 2);
KUNIT_EXPECT_EQ(test, p.sb[8], 320 & 0xff);
KUNIT_EXPECT_EQ(test, p.sb[12], 320 >> 8);
}
static void dm_test_update_freesync_state_dp_v3_mode_240(struct kunit *test)
{
struct dc_info_packet p =
dm_test_freesync_packet(test, SIGNAL_TYPE_DISPLAY_PORT, 3, 320, 240);
KUNIT_EXPECT_EQ(test, p.hb3, 3 << 2);
KUNIT_EXPECT_EQ(test, p.sb[8], 240);
KUNIT_EXPECT_EQ(test, p.sb[12], 0);
}
static void dm_test_update_freesync_state_dp_v3_at_255(struct kunit *test)
{
struct dc_info_packet p =
dm_test_freesync_packet(test, SIGNAL_TYPE_DISPLAY_PORT, 3, 255, 255);
KUNIT_EXPECT_EQ(test, p.hb3, 0x04);
KUNIT_EXPECT_EQ(test, p.sb[8], 255);
}
static void dm_test_update_freesync_state_hdmi_v3_above_255(struct kunit *test)
{
struct dc_info_packet p =
dm_test_freesync_packet(test, SIGNAL_TYPE_HDMI_TYPE_A, 3, 320, 320);
KUNIT_EXPECT_EQ(test, p.hb1, 3);
KUNIT_EXPECT_EQ(test, p.sb[8], 320 & 0xff);
KUNIT_EXPECT_EQ(test, p.sb[12], 320 >> 8);
}
static void dm_test_update_stream_irq_parameters_no_stream(struct kunit *test)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct amdgpu_crtc *acrtc;
struct dm_crtc_state crtc_state = { 0 };
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
crtc_state.base.crtc = &acrtc->base;
amdgpu_dm_update_stream_irq_parameters(&adev->dm, &crtc_state);
KUNIT_SUCCEED(test);
}
static void dm_test_update_stream_irq_parameters_invalid_timing(struct kunit *test)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct amdgpu_crtc *acrtc;
struct dm_crtc_state crtc_state = { 0 };
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
crtc_state.base.crtc = &acrtc->base;
crtc_state.stream = dm_kunit_alloc_stream(test, NULL);
amdgpu_dm_update_stream_irq_parameters(&adev->dm, &crtc_state);
KUNIT_SUCCEED(test);
}
static void dm_test_update_stream_irq_parameters_unsupported(struct kunit *test)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct amdgpu_crtc *acrtc;
struct dm_crtc_state crtc_state = { 0 };
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
crtc_state.base.crtc = &acrtc->base;
crtc_state.stream = dm_kunit_alloc_stream(test, NULL);
crtc_state.stream->timing.h_total = 2200;
crtc_state.stream->timing.v_total = 1125;
adev->dm.adev = adev;
amdgpu_dm_update_stream_irq_parameters(&adev->dm, &crtc_state);
KUNIT_EXPECT_EQ(test, (int)crtc_state.freesync_config.state,
(int)VRR_STATE_UNSUPPORTED);
KUNIT_EXPECT_EQ(test, (int)acrtc->dm_irq_params.freesync_config.state,
(int)VRR_STATE_UNSUPPORTED);
}
static void dm_test_update_stream_irq_parameters_supported(struct kunit *test, bool vrr_enabled)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct amdgpu_crtc *acrtc;
struct dm_crtc_state crtc_state = { 0 };
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
crtc_state.base.crtc = &acrtc->base;
crtc_state.base.vrr_enabled = vrr_enabled;
crtc_state.stream = dm_kunit_alloc_stream(test, NULL);
crtc_state.stream->timing.h_total = 2200;
crtc_state.stream->timing.v_total = 1125;
crtc_state.vrr_supported = true;
crtc_state.freesync_config.min_refresh_in_uhz = 48000000;
crtc_state.freesync_config.max_refresh_in_uhz = 120000000;
adev->dm.adev = adev;
amdgpu_dm_update_stream_irq_parameters(&adev->dm, &crtc_state);
KUNIT_EXPECT_EQ(test, (int)crtc_state.freesync_config.state,
vrr_enabled ? (int)VRR_STATE_ACTIVE_VARIABLE : (int)VRR_STATE_INACTIVE);
}
static void dm_test_update_stream_irq_parameters_variable(struct kunit *test)
{
dm_test_update_stream_irq_parameters_supported(test, true);
}
static void dm_test_update_stream_irq_parameters_inactive(struct kunit *test)
{
dm_test_update_stream_irq_parameters_supported(test, false);
}
static void dm_test_update_stream_irq_parameters_fixed(struct kunit *test)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct amdgpu_crtc *acrtc;
struct dm_crtc_state crtc_state = { 0 };
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
crtc_state.base.crtc = &acrtc->base;
crtc_state.base.mode_changed = true;
crtc_state.stream = dm_kunit_alloc_stream(test, NULL);
crtc_state.stream->timing.h_total = 2200;
crtc_state.stream->timing.v_total = 1125;
crtc_state.vrr_supported = true;
crtc_state.freesync_config.state = VRR_STATE_ACTIVE_FIXED;
crtc_state.freesync_config.min_refresh_in_uhz = 48000000;
crtc_state.freesync_config.max_refresh_in_uhz = 120000000;
crtc_state.freesync_config.fixed_refresh_in_uhz = 60000000;
adev->dm.adev = adev;
amdgpu_dm_update_stream_irq_parameters(&adev->dm, &crtc_state);
KUNIT_EXPECT_EQ(test, (int)acrtc->dm_irq_params.vrr_params.state,
(int)VRR_STATE_ACTIVE_FIXED);
KUNIT_EXPECT_EQ(test, acrtc->dm_irq_params.vrr_params.fixed_refresh_in_uhz,
60000000U);
}
static void dm_test_handle_vrr_transition_inactive(struct kunit *test)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct dm_crtc_state old_state = { 0 };
struct dm_crtc_state new_state = { 0 };
adev->dm.adev = adev;
amdgpu_dm_handle_vrr_transition(&adev->dm, &old_state, &new_state);
KUNIT_SUCCEED(test);
}
static void dm_test_handle_vrr_transition_round_trip(struct kunit *test)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct dc_link *link = dm_kunit_alloc_link(test);
struct dc_stream_state *stream = dm_kunit_alloc_stream(test, link);
struct drm_vblank_crtc *vblank;
struct amdgpu_crtc *acrtc;
struct dm_crtc_state old_state = { 0 };
struct dm_crtc_state new_state = { 0 };
KUNIT_ASSERT_EQ(test, drm_vblank_init(&adev->ddev, 1), 0);
acrtc = kunit_kzalloc(test, sizeof(*acrtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, acrtc);
acrtc->base.dev = &adev->ddev;
acrtc->base.funcs = &dm_test_freesync_crtc_funcs;
INIT_LIST_HEAD(&acrtc->base.head);
list_add_tail(&acrtc->base.head, &adev->ddev.mode_config.crtc_list);
KUNIT_ASSERT_EQ(test,
kunit_add_action_or_reset(test, dm_test_freesync_crtc_cleanup, acrtc), 0);
acrtc->otg_inst = -1;
vblank = drm_crtc_vblank_crtc(&acrtc->base);
adev->ddev.vblank[0].enabled = true;
adev->dm.adev = adev;
adev->dm.dc = kunit_kzalloc(test, sizeof(*adev->dm.dc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, adev->dm.dc);
mutex_init(&adev->dm.dc_lock);
old_state.base.crtc = &acrtc->base;
old_state.base.active = true;
old_state.stream = stream;
new_state.base.crtc = &acrtc->base;
new_state.base.active = true;
new_state.stream = stream;
new_state.freesync_config.state = VRR_STATE_ACTIVE_VARIABLE;
amdgpu_dm_handle_vrr_transition(&adev->dm, &old_state, &new_state);
KUNIT_EXPECT_EQ(test, atomic_read(&vblank->refcount), 1);
amdgpu_dm_handle_vrr_transition(&adev->dm, &new_state, &old_state);
KUNIT_EXPECT_EQ(test, atomic_read(&vblank->refcount), 0);
}
static int dm_test_vrr_transition_refcount(struct kunit *test,
bool old_active, enum mod_vrr_state old_vrr,
bool new_active, enum mod_vrr_state new_vrr)
{
struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
struct amdgpu_display_manager *dm = dm_kunit_alloc_dm(test);
struct dm_crtc_state *old_state, *new_state;
struct drm_crtc *crtc;
int refcount;
KUNIT_ASSERT_EQ(test, drm_vblank_init(&adev->ddev, 1), 0);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(2, 1, 0);
dm->adev = adev;
mutex_init(&dm->dc_lock);
crtc = kunit_kzalloc(test, sizeof(*crtc), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, crtc);
crtc->dev = &adev->ddev;
old_state = kunit_kzalloc(test, sizeof(*old_state), GFP_KERNEL);
new_state = kunit_kzalloc(test, sizeof(*new_state), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, old_state);
KUNIT_ASSERT_NOT_NULL(test, new_state);
old_state->base.crtc = crtc;
old_state->base.active = old_active;
old_state->freesync_config.state = old_vrr;
new_state->base.crtc = crtc;
new_state->base.active = new_active;
new_state->freesync_config.state = new_vrr;
adev->ddev.vblank[0].enabled = true;
atomic_set(&adev->ddev.vblank[0].refcount, 2);
amdgpu_dm_handle_vrr_transition(dm, old_state, new_state);
refcount = atomic_read(&adev->ddev.vblank[0].refcount);
adev->ddev.vblank[0].enabled = false;
atomic_set(&adev->ddev.vblank[0].refcount, 0);
return refcount;
}
static void dm_test_vrr_transition_crtc_off_drops_ref(struct kunit *test)
{
KUNIT_EXPECT_EQ(test,
dm_test_vrr_transition_refcount(test, true, VRR_STATE_ACTIVE_VARIABLE,
false, VRR_STATE_ACTIVE_VARIABLE),
1);
}
static void dm_test_vrr_transition_crtc_on_takes_ref(struct kunit *test)
{
KUNIT_EXPECT_EQ(test,
dm_test_vrr_transition_refcount(test, false, VRR_STATE_UNSUPPORTED,
true, VRR_STATE_ACTIVE_VARIABLE),
3);
}
static void dm_test_vrr_transition_crtc_off_keeps_ref(struct kunit *test)
{
KUNIT_EXPECT_EQ(test,
dm_test_vrr_transition_refcount(test, false, VRR_STATE_UNSUPPORTED,
false, VRR_STATE_ACTIVE_VARIABLE),
2);
}
static struct kunit_case amdgpu_dm_freesync_tests[] = {
KUNIT_CASE(dm_test_timing_unchanged_null_args),
KUNIT_CASE(dm_test_timing_unchanged_identical_modes),
KUNIT_CASE(dm_test_timing_unchanged_vrr_shift),
KUNIT_CASE(dm_test_timing_unchanged_clock_changed),
KUNIT_CASE(dm_test_set_freesync_fixed_config_60hz),
KUNIT_CASE(dm_test_dc_timing_adjust_pending),
KUNIT_CASE(dm_test_dc_timing_adjust_active_fixed),
KUNIT_CASE(dm_test_dc_timing_adjust_vrr_toggle),
KUNIT_CASE(dm_test_dc_timing_adjust_not_needed),
KUNIT_CASE(dm_test_freesync_config_writeback),
KUNIT_CASE(dm_test_freesync_config_not_capable),
KUNIT_CASE(dm_test_freesync_config_out_of_range),
KUNIT_CASE(dm_test_freesync_config_active_variable),
KUNIT_CASE(dm_test_freesync_config_inactive),
KUNIT_CASE(dm_test_freesync_config_active_fixed),
KUNIT_CASE(dm_test_reset_freesync_config),
KUNIT_CASE(dm_test_update_freesync_state_no_stream),
KUNIT_CASE(dm_test_update_freesync_state_invalid_timing),
KUNIT_CASE(dm_test_update_freesync_state_default_packet),
KUNIT_CASE(dm_test_update_freesync_state_packet_changed),
KUNIT_CASE(dm_test_update_freesync_state_pre_ai_active),
KUNIT_CASE(dm_test_update_freesync_state_pcon_v1),
KUNIT_CASE(dm_test_update_freesync_state_pcon_v2),
KUNIT_CASE(dm_test_update_freesync_state_pcon_v3),
KUNIT_CASE(dm_test_update_freesync_state_pcon_default),
KUNIT_CASE(dm_test_update_freesync_state_dp_v2_saturates),
KUNIT_CASE(dm_test_update_freesync_state_dp_v3_above_255),
KUNIT_CASE(dm_test_update_freesync_state_dp_v3_mode_240),
KUNIT_CASE(dm_test_update_freesync_state_dp_v3_at_255),
KUNIT_CASE(dm_test_update_freesync_state_hdmi_v3_above_255),
KUNIT_CASE(dm_test_update_stream_irq_parameters_no_stream),
KUNIT_CASE(dm_test_update_stream_irq_parameters_invalid_timing),
KUNIT_CASE(dm_test_update_stream_irq_parameters_unsupported),
KUNIT_CASE(dm_test_update_stream_irq_parameters_variable),
KUNIT_CASE(dm_test_update_stream_irq_parameters_inactive),
KUNIT_CASE(dm_test_update_stream_irq_parameters_fixed),
KUNIT_CASE(dm_test_handle_vrr_transition_inactive),
KUNIT_CASE(dm_test_handle_vrr_transition_round_trip),
KUNIT_CASE(dm_test_vrr_transition_crtc_off_drops_ref),
KUNIT_CASE(dm_test_vrr_transition_crtc_on_takes_ref),
KUNIT_CASE(dm_test_vrr_transition_crtc_off_keeps_ref),
{}
};
static struct kunit_suite amdgpu_dm_freesync_test_suite = {
.name = "amdgpu_dm_freesync",
.test_cases = amdgpu_dm_freesync_tests,
};
kunit_test_suite(amdgpu_dm_freesync_test_suite);
MODULE_AUTHOR("AMD");
MODULE_DESCRIPTION("KUnit tests for amdgpu_dm_freesync");
MODULE_LICENSE("Dual MIT/GPL");