summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_freesync_test.c
blob: 0ae636dcbbe4e42e301d1046a0e73884c503f6af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// SPDX-License-Identifier: GPL-2.0 OR MIT
/*
 * KUnit tests for amdgpu_dm_freesync.c
 *
 * Copyright 2026 Advanced Micro Devices, Inc.
 */

#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 "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"

/* Tests for amdgpu_dm_is_timing_unchanged_for_freesync() */

/**
 * dm_test_timing_unchanged_null_args - Test NULL crtc states return false
 * @test: The KUnit test context
 */
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));
}

/**
 * dm_test_timing_unchanged_identical_modes - Test identical modes are not "unchanged"
 * @test: The KUnit test context
 *
 * The helper only returns true when vtotal/vsync shift (vrr) while the rest
 * of the timing stays fixed, so identical modes must return false.
 */
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));
}

/**
 * dm_test_timing_unchanged_vrr_shift - Test vrr-style vtotal/vsync shift is detected
 * @test: The KUnit test context
 */
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;

	/* Same horizontal timing, vertical totals/sync shifted by 125 lines */
	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));
}

/**
 * dm_test_timing_unchanged_clock_changed - Test pixel clock change returns false
 * @test: The KUnit test context
 */
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));
}

/* Tests for amdgpu_dm_set_freesync_fixed_config() */

/**
 * dm_test_set_freesync_fixed_config_60hz - Test fixed refresh computed for 1080p60
 * @test: The KUnit test context
 */
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);
	/* 148500 kHz / (2200 * 1125) = 60 Hz = 60000000 uHz */
	KUNIT_EXPECT_EQ(test, dm_crtc_state.freesync_config.fixed_refresh_in_uhz,
			60000000U);
}

/* Tests for amdgpu_dm_is_dc_timing_adjust_needed() */

/**
 * dm_test_dc_timing_adjust_pending - Test a pending hw timing adjust forces true
 * @test: The KUnit test context
 */
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));
}

/**
 * dm_test_dc_timing_adjust_active_fixed - Test VRR active-fixed forces true
 * @test: The KUnit test context
 */
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));
}

/**
 * dm_test_dc_timing_adjust_vrr_toggle - Test a change in vrr active state forces true
 * @test: The KUnit test context
 */
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));
}

/**
 * dm_test_dc_timing_adjust_not_needed - Test steady-state timing needs no adjust
 * @test: The KUnit test context
 */
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));
}

/* Tests for amdgpu_dm_get_freesync_config_for_crtc() */

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;

	/* 1080p60 timing so drm_mode_vrefresh() == 60 */
	ctx->crtc_state->base.mode.clock = 148500;
	ctx->crtc_state->base.mode.htotal = 2200;
	ctx->crtc_state->base.mode.vtotal = 1125;

	return ctx;
}

/**
 * dm_test_freesync_config_writeback - Test writeback connector is left untouched
 * @test: The KUnit test context
 */
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;	/* sentinel: must stay set */

	amdgpu_dm_get_freesync_config_for_crtc(ctx->crtc_state, ctx->conn_state);

	/* Writeback: early return leaves vrr_supported sentinel untouched */
	KUNIT_EXPECT_TRUE(test, ctx->crtc_state->vrr_supported);
}

/**
 * dm_test_freesync_config_not_capable - Test a non-freesync sink reports UNSUPPORTED
 * @test: The KUnit test context
 */
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);
}

/**
 * dm_test_freesync_config_out_of_range - Test a refresh outside the range is UNSUPPORTED
 * @test: The KUnit test context
 */
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;	/* 60 < 90 -> out of range */
	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);
}

/**
 * dm_test_freesync_config_active_variable - Test vrr_enabled yields ACTIVE_VARIABLE
 * @test: The KUnit test context
 */
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);
}

/**
 * dm_test_freesync_config_inactive - Test supported-but-off yields INACTIVE
 * @test: The KUnit test context
 */
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);
}

/**
 * dm_test_freesync_config_active_fixed - Test freesync-video mode yields ACTIVE_FIXED
 * @test: The KUnit test context
 */
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;
	/* Pre-set fixed state selects the freesync-video (fixed) path */
	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;	/* ignored on the fixed path */

	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);
}

/* Tests for amdgpu_dm_reset_freesync_config_for_crtc() */

/**
 * dm_test_reset_freesync_config - Test reset clears vrr support and info packet
 * @test: The KUnit test context
 */
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 struct kunit_case amdgpu_dm_freesync_tests[] = {
	/* amdgpu_dm_is_timing_unchanged_for_freesync */
	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),
	/* amdgpu_dm_set_freesync_fixed_config */
	KUNIT_CASE(dm_test_set_freesync_fixed_config_60hz),
	/* amdgpu_dm_is_dc_timing_adjust_needed */
	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),
	/* amdgpu_dm_get_freesync_config_for_crtc */
	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),
	/* amdgpu_dm_reset_freesync_config_for_crtc */
	KUNIT_CASE(dm_test_reset_freesync_config),
	{}
};

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");