xref: /linux/drivers/gpu/drm/i915/display/tests/intel_dp_link_test.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2026 Intel Corporation
4  */
5 
6 #include <kunit/test.h>
7 
8 #include <linux/compiler.h>
9 #include <linux/device.h>
10 #include <linux/log2.h>
11 #include <linux/prandom.h>
12 #include <linux/random.h>
13 
14 #include <drm/display/drm_dp_helper.h>
15 
16 #include <drm/intel/display_member.h>
17 
18 #include "intel_connector.h"
19 #include "intel_display_core.h"
20 #include "intel_display_types.h"
21 #include "intel_dp_link_caps.h"
22 #include "intel_dp_link_training.h"
23 
24 #define LINK_TEST_NUM_LANE_CONFIGS(__max_lane_count) \
25 	(ilog2(__max_lane_count) + 1)
26 
27 #define LINK_TEST_NUM_CONFIGS(__num_rates, __max_lane_count) \
28 	((__num_rates) * LINK_TEST_NUM_LANE_CONFIGS(__max_lane_count))
29 
30 #define LINK_TEST_MAX_LANE_COUNT		((u32)4)
31 #define LINK_TEST_MAX_CONFIGS			LINK_TEST_NUM_CONFIGS(DP_MAX_SUPPORTED_RATES, \
32 								      LINK_TEST_MAX_LANE_COUNT)
33 
34 #define LINK_TEST_NUM_RANDOM_ITERATIONS		50
35 
36 struct test_ctx {
37 	struct {
38 		struct intel_display display;
39 		struct device device;
40 		struct __intel_generic_device generic_device;
41 
42 		struct intel_connector connector;
43 		struct intel_digital_port dig_port;
44 
45 		struct intel_crtc_state crtc_state;
46 	} dev;
47 
48 	const struct intel_dp_link_caps_test_ops *link_caps_ops;
49 	const struct intel_dp_link_training_test_ops *link_training_ops;
50 
51 	struct rnd_state rnd;
52 };
53 
54 struct link_rate_set {
55 	const int *entries;
56 	int size;
57 };
58 
59 struct link_config_set {
60 	struct intel_dp_link_config entries[LINK_TEST_MAX_CONFIGS];
61 	int size;
62 };
63 
64 struct test_config_table {
65 	struct kunit *test;
66 
67 	struct link_rate_set rates;
68 	int max_lane_count;
69 	struct link_config_set disabled_configs;
70 };
71 
72 static const int standard_dp_link_rates[] = {
73 	162000, 270000, 540000, 810000, 1000000, 1350000, 2000000
74 };
75 
76 #define LINK_TEST_NUM_STANDARD_RATES (ARRAY_SIZE(standard_dp_link_rates))
77 
78 #define INIT_STANDARD_TABLE(__test, __num_rates, __max_lane_count) { \
79 	.test = (__test), \
80 	.rates = { \
81 		.entries = standard_dp_link_rates, \
82 		.size = (__num_rates), \
83 	}, \
84 	.max_lane_count = (__max_lane_count), \
85 }
86 
87 static const struct link_config_set standard_dp_link_configs[] = {
88 	[INTEL_DP_LINK_CAPS_ORDER_KEY_BW] = {                        /* MBps    PBN    */
89 		.entries = {
90 			{ .rate =  162000, .lane_count = 1 }, /*  162.0    3.00 */
91 			{ .rate =  270000, .lane_count = 1 }, /*  270.0    5.00 */
92 			{ .rate =  162000, .lane_count = 2 }, /*  324.0    6.00 */
93 			{ .rate =  270000, .lane_count = 2 }, /*  540.0   10.00 */
94 			{ .rate =  540000, .lane_count = 1 }, /*  540.0   10.00 */
95 			{ .rate =  162000, .lane_count = 4 }, /*  648.0   12.00 */
96 			{ .rate =  810000, .lane_count = 1 }, /*  810.0   15.00 */
97 			{ .rate =  270000, .lane_count = 4 }, /* 1080.0   20.00 */
98 			{ .rate =  540000, .lane_count = 2 }, /* 1080.0   20.00 */
99 			{ .rate = 1000000, .lane_count = 1 }, /* 1208.9   22.39 */
100 			{ .rate =  810000, .lane_count = 2 }, /* 1620.0   30.00 */
101 			{ .rate = 1350000, .lane_count = 1 }, /* 1632.0   30.22 */
102 			{ .rate =  540000, .lane_count = 4 }, /* 2160.0   40.00 */
103 			{ .rate = 1000000, .lane_count = 2 }, /* 2417.8   44.77 */
104 			{ .rate = 2000000, .lane_count = 1 }, /* 2417.8   44.77 */
105 			{ .rate =  810000, .lane_count = 4 }, /* 3240.0   60.00 */
106 			{ .rate = 1350000, .lane_count = 2 }, /* 3264.0   60.44 */
107 			{ .rate = 1000000, .lane_count = 4 }, /* 4835.6   89.55 */
108 			{ .rate = 2000000, .lane_count = 2 }, /* 4835.6   89.55 */
109 			{ .rate = 1350000, .lane_count = 4 }, /* 6527.9  120.89 */
110 			{ .rate = 2000000, .lane_count = 4 }, /* 9671.1  179.09 */
111 		},
112 		.size = LINK_TEST_NUM_CONFIGS(ARRAY_SIZE(standard_dp_link_rates),
113 					      LINK_TEST_MAX_LANE_COUNT),
114 	},
115 	[INTEL_DP_LINK_CAPS_ORDER_KEY_RATE_LANE] = {
116 		.entries = {
117 			{ .rate = 162000,  .lane_count = 1 },
118 			{ .rate = 162000,  .lane_count = 2 },
119 			{ .rate = 162000,  .lane_count = 4 },
120 
121 			{ .rate = 270000,  .lane_count = 1 },
122 			{ .rate = 270000,  .lane_count = 2 },
123 			{ .rate = 270000,  .lane_count = 4 },
124 
125 			{ .rate = 540000,  .lane_count = 1 },
126 			{ .rate = 540000,  .lane_count = 2 },
127 			{ .rate = 540000,  .lane_count = 4 },
128 
129 			{ .rate = 810000,  .lane_count = 1 },
130 			{ .rate = 810000,  .lane_count = 2 },
131 			{ .rate = 810000,  .lane_count = 4 },
132 
133 			{ .rate = 1000000, .lane_count = 1 },
134 			{ .rate = 1000000, .lane_count = 2 },
135 			{ .rate = 1000000, .lane_count = 4 },
136 
137 			{ .rate = 1350000, .lane_count = 1 },
138 			{ .rate = 1350000, .lane_count = 2 },
139 			{ .rate = 1350000, .lane_count = 4 },
140 
141 			{ .rate = 2000000, .lane_count = 1 },
142 			{ .rate = 2000000, .lane_count = 2 },
143 			{ .rate = 2000000, .lane_count = 4 },
144 		},
145 		.size = LINK_TEST_NUM_CONFIGS(ARRAY_SIZE(standard_dp_link_rates),
146 					      LINK_TEST_MAX_LANE_COUNT),
147 	},
148 	[INTEL_DP_LINK_CAPS_ORDER_KEY_LANE_RATE] = {
149 		.entries = {
150 			{ .rate = 162000,  .lane_count = 1 },
151 			{ .rate = 270000,  .lane_count = 1 },
152 			{ .rate = 540000,  .lane_count = 1 },
153 			{ .rate = 810000,  .lane_count = 1 },
154 			{ .rate = 1000000, .lane_count = 1 },
155 			{ .rate = 1350000, .lane_count = 1 },
156 			{ .rate = 2000000, .lane_count = 1 },
157 
158 			{ .rate = 162000,  .lane_count = 2 },
159 			{ .rate = 270000,  .lane_count = 2 },
160 			{ .rate = 540000,  .lane_count = 2 },
161 			{ .rate = 810000,  .lane_count = 2 },
162 			{ .rate = 1000000, .lane_count = 2 },
163 			{ .rate = 1350000, .lane_count = 2 },
164 			{ .rate = 2000000, .lane_count = 2 },
165 
166 			{ .rate = 162000,  .lane_count = 4 },
167 			{ .rate = 270000,  .lane_count = 4 },
168 			{ .rate = 540000,  .lane_count = 4 },
169 			{ .rate = 810000,  .lane_count = 4 },
170 			{ .rate = 1000000, .lane_count = 4 },
171 			{ .rate = 1350000, .lane_count = 4 },
172 			{ .rate = 2000000, .lane_count = 4 },
173 		},
174 		.size = LINK_TEST_NUM_CONFIGS(ARRAY_SIZE(standard_dp_link_rates),
175 					      LINK_TEST_MAX_LANE_COUNT),
176 	},
177 };
178 
179 static int lookup_rate(const struct link_rate_set *rate_set, int rate)
180 {
181 	int i;
182 
183 	for (i = 0; i < rate_set->size; i++)
184 		if (rate_set->entries[i] == rate)
185 			return i;
186 
187 	return -1;
188 }
189 
190 static bool has_rate(const struct link_rate_set *rate_set, int rate)
191 {
192 	return lookup_rate(rate_set, rate) >= 0;
193 }
194 
195 static bool link_configs_match(const struct intel_dp_link_config *a,
196 			       const struct intel_dp_link_config *b)
197 {
198 	return a->rate == b->rate && a->lane_count == b->lane_count;
199 }
200 
201 static int lookup_config(const struct link_config_set *config_set,
202 			 const struct intel_dp_link_config *config)
203 {
204 	int i;
205 
206 	for (i = 0; i < config_set->size; i++)
207 		if (link_configs_match(&config_set->entries[i], config))
208 			return i;
209 
210 	return -1;
211 }
212 
213 static bool has_config(const struct link_config_set *config_set,
214 		       const struct intel_dp_link_config *config)
215 {
216 	return lookup_config(config_set, config) >= 0;
217 }
218 
219 static void add_config(struct kunit *test,
220 		       struct link_config_set *config_set,
221 		       const struct intel_dp_link_config *config)
222 {
223 	KUNIT_ASSERT_LT(test, config_set->size, ARRAY_SIZE(config_set->entries));
224 
225 	config_set->entries[config_set->size] = *config;
226 	config_set->size++;
227 }
228 
229 static const struct intel_dp_link_caps_order config_orders[] = {
230 	{
231 		.key = INTEL_DP_LINK_CAPS_ORDER_KEY_BW,
232 		.dir = INTEL_DP_LINK_CAPS_ORDER_DIR_ASC,
233 	}, {
234 		.key = INTEL_DP_LINK_CAPS_ORDER_KEY_BW,
235 		.dir = INTEL_DP_LINK_CAPS_ORDER_DIR_DESC,
236 	}, {
237 		.key = INTEL_DP_LINK_CAPS_ORDER_KEY_RATE_LANE,
238 		.dir = INTEL_DP_LINK_CAPS_ORDER_DIR_ASC,
239 	}, {
240 		.key = INTEL_DP_LINK_CAPS_ORDER_KEY_RATE_LANE,
241 		.dir = INTEL_DP_LINK_CAPS_ORDER_DIR_DESC,
242 	}, {
243 		.key = INTEL_DP_LINK_CAPS_ORDER_KEY_LANE_RATE,
244 		.dir = INTEL_DP_LINK_CAPS_ORDER_DIR_ASC,
245 	}, {
246 		.key = INTEL_DP_LINK_CAPS_ORDER_KEY_LANE_RATE,
247 		.dir = INTEL_DP_LINK_CAPS_ORDER_DIR_DESC,
248 	}
249 };
250 
251 static const struct link_config_set *
252 link_caps_config_order_key_to_set(struct kunit *test, enum intel_dp_link_caps_order_key key)
253 {
254 	return &standard_dp_link_configs[key];
255 }
256 
257 /*
258  * TEST: Baseline with fixed reference table
259  * -----------------------------------------
260  * Verify the link_caps config iterator using fixed standard DP config tables.
261  */
262 static void baseline_test_for_order(struct kunit *test,
263 				    struct intel_dp_link_caps *link_caps,
264 				    struct intel_dp_link_caps_order config_order)
265 {
266 	struct test_ctx *ctx = test->priv;
267 	const struct link_config_set *config_set =
268 		link_caps_config_order_key_to_set(test, config_order.key);
269 	const struct intel_dp_link_caps_test_ops *ops = ctx->link_caps_ops;
270 	struct intel_dp_link_config iter_config;
271 	struct intel_dp_link_caps_iter iter;
272 	int pos = 0;
273 
274 	ops->iter_start(&iter, link_caps, config_order, INTEL_DP_LINK_CAPS_FILTER_ALL);
275 	for_each_dp_link_config(&iter, &iter_config) {
276 		int idx = pos;
277 
278 		if (config_order.dir == INTEL_DP_LINK_CAPS_ORDER_DIR_DESC)
279 			idx = config_set->size - idx - 1;
280 
281 		KUNIT_EXPECT_TRUE(test, link_configs_match(&iter_config,
282 							   &config_set->entries[idx]));
283 
284 		pos++;
285 	}
286 	ops->iter_end(&iter);
287 }
288 
289 static void intel_dp_link_caps_test_baseline(struct kunit *test)
290 {
291 	struct test_ctx *ctx = test->priv;
292 	struct intel_dp_link_caps *link_caps = ctx->dev.dig_port.dp.link.caps;
293 	const struct intel_dp_link_caps_test_ops *ops =
294 		ctx->link_caps_ops;
295 	int i;
296 
297 	ops->update(link_caps,
298 		    standard_dp_link_rates, LINK_TEST_NUM_STANDARD_RATES,
299 		    LINK_TEST_MAX_LANE_COUNT,
300 		    true);
301 
302 	for (i = 0; i < ARRAY_SIZE(config_orders); i++)
303 		baseline_test_for_order(test, link_caps, config_orders[i]);
304 }
305 
306 static int get_num_configs(int num_rates, int max_lane_count)
307 {
308 	return num_rates * LINK_TEST_NUM_LANE_CONFIGS(max_lane_count);
309 }
310 
311 static int rand_in_range(struct test_ctx *ctx, int min, int max)
312 {
313 	return min + (prandom_u32_state(&ctx->rnd) % (max - min + 1));
314 }
315 
316 /*
317  * TEST: Update reset
318  * ------------------
319  * Verify that resetting link_caps with the DP standard rates/lane
320  * counts updates the configuration table accordingly for all
321  * combinations.
322  */
323 static void verify_bw_asc_config_order(struct kunit *test,
324 				       const struct intel_dp_link_config *last_config,
325 				       const struct intel_dp_link_config *config)
326 {
327 	int config_bw = drm_dp_max_dprx_data_rate(config->rate,
328 						  config->lane_count);
329 	int last_config_bw = drm_dp_max_dprx_data_rate(last_config->rate,
330 						       last_config->lane_count);
331 
332 	KUNIT_EXPECT_GE(test, config_bw, last_config_bw);
333 	if (config_bw == last_config_bw)
334 		KUNIT_EXPECT_GT(test, config->rate, last_config->rate);
335 }
336 
337 static void verify_bw_desc_config_order(struct kunit *test,
338 					const struct intel_dp_link_config *last_config,
339 					const struct intel_dp_link_config *config)
340 {
341 	int config_bw = drm_dp_max_dprx_data_rate(config->rate,
342 						  config->lane_count);
343 	int last_config_bw = drm_dp_max_dprx_data_rate(last_config->rate,
344 						       last_config->lane_count);
345 
346 	KUNIT_EXPECT_LE(test, config_bw, last_config_bw);
347 	if (config_bw == last_config_bw)
348 		KUNIT_EXPECT_LT(test, config->rate, last_config->rate);
349 }
350 
351 static void verify_rate_lane_asc_config_order(struct kunit *test,
352 					      const struct intel_dp_link_config *last_config,
353 					      const struct intel_dp_link_config *config)
354 {
355 	KUNIT_EXPECT_GE(test, config->rate, last_config->rate);
356 	if (config->rate == last_config->rate)
357 		KUNIT_EXPECT_GT(test, config->lane_count, last_config->lane_count);
358 }
359 
360 static void verify_rate_lane_desc_config_order(struct kunit *test,
361 					       const struct intel_dp_link_config *last_config,
362 					       const struct intel_dp_link_config *config)
363 {
364 	KUNIT_EXPECT_LE(test, config->rate, last_config->rate);
365 	if (config->rate == last_config->rate)
366 		KUNIT_EXPECT_LT(test, config->lane_count, last_config->lane_count);
367 }
368 
369 static void verify_lane_rate_asc_config_order(struct kunit *test,
370 					      const struct intel_dp_link_config *last_config,
371 					      const struct intel_dp_link_config *config)
372 {
373 	KUNIT_EXPECT_GE(test, config->lane_count, last_config->lane_count);
374 	if (config->lane_count == last_config->lane_count)
375 		KUNIT_EXPECT_GT(test, config->rate, last_config->rate);
376 }
377 
378 static void verify_lane_rate_desc_config_order(struct kunit *test,
379 					       const struct intel_dp_link_config *last_config,
380 					       const struct intel_dp_link_config *config)
381 {
382 	KUNIT_EXPECT_LE(test, config->lane_count, last_config->lane_count);
383 	if (config->lane_count == last_config->lane_count)
384 		KUNIT_EXPECT_LT(test, config->rate, last_config->rate);
385 }
386 
387 static void verify_config_order(struct kunit *test,
388 				struct intel_dp_link_caps_order config_order,
389 				const struct intel_dp_link_config *last_config,
390 				const struct intel_dp_link_config *config)
391 {
392 	switch (config_order.key) {
393 	case INTEL_DP_LINK_CAPS_ORDER_KEY_BW:
394 		if (config_order.dir == INTEL_DP_LINK_CAPS_ORDER_DIR_ASC)
395 			verify_bw_asc_config_order(test, last_config, config);
396 		else
397 			verify_bw_desc_config_order(test, last_config, config);
398 		break;
399 	case INTEL_DP_LINK_CAPS_ORDER_KEY_RATE_LANE:
400 		if (config_order.dir == INTEL_DP_LINK_CAPS_ORDER_DIR_ASC)
401 			verify_rate_lane_asc_config_order(test, last_config, config);
402 		else
403 			verify_rate_lane_desc_config_order(test, last_config, config);
404 		break;
405 	case INTEL_DP_LINK_CAPS_ORDER_KEY_LANE_RATE:
406 		if (config_order.dir == INTEL_DP_LINK_CAPS_ORDER_DIR_ASC)
407 			verify_lane_rate_asc_config_order(test, last_config, config);
408 		else
409 			verify_lane_rate_desc_config_order(test, last_config, config);
410 		break;
411 	default:
412 		KUNIT_FAIL_AND_ABORT(test, "Missing order key: %d", config_order.key);
413 	}
414 }
415 
416 static int expected_num_configs(const struct test_config_table *expected_table,
417 				const struct intel_dp_link_config *max_limits)
418 {
419 	int num_configs = 0;
420 	int lane_count;
421 	int rate_idx;
422 
423 	for (rate_idx = 0; rate_idx < expected_table->rates.size; rate_idx++) {
424 		for (lane_count = 1; lane_count <= expected_table->max_lane_count; lane_count <<= 1) {
425 			struct intel_dp_link_config config = {
426 				.rate = expected_table->rates.entries[rate_idx],
427 				.lane_count = lane_count,
428 			};
429 
430 			if (config.rate > max_limits->rate ||
431 			    config.lane_count > max_limits->lane_count)
432 				continue;
433 
434 			if (has_config(&expected_table->disabled_configs, &config))
435 				continue;
436 
437 			num_configs++;
438 		}
439 	}
440 
441 	return num_configs;
442 }
443 
444 static void
445 verify_link_caps_for_order(const struct test_config_table *expected_table,
446 			   struct intel_dp_link_caps *link_caps,
447 			   struct intel_dp_link_caps_order config_order,
448 			   const struct intel_dp_link_config *max_limits)
449 {
450 	struct kunit *test = expected_table->test;
451 	struct test_ctx *ctx = test->priv;
452 	const struct intel_dp_link_caps_test_ops *ops =
453 		ctx->link_caps_ops;
454 	struct intel_dp_link_config expected_max_bw_config = {};
455 	struct intel_dp_link_config actual_max_bw_config;
456 	struct intel_dp_link_config last_config = {};
457 	struct intel_dp_link_config old_max_limits;
458 	struct intel_dp_link_config iter_config;
459 	struct intel_dp_link_caps_iter iter;
460 	int num_actual_configs = 0;
461 	int max_bw = 0;
462 
463 	ops->get_max_limits(link_caps, &old_max_limits);
464 	ops->set_max_limits(link_caps, max_limits);
465 
466 	ops->iter_start(&iter, link_caps, config_order, INTEL_DP_LINK_CAPS_FILTER_ALL);
467 	for_each_dp_link_config(&iter, &iter_config) {
468 		int bw;
469 
470 		KUNIT_EXPECT_LE(test, iter_config.rate, max_limits->rate);
471 		KUNIT_EXPECT_LE(test, iter_config.lane_count, max_limits->lane_count);
472 
473 		num_actual_configs++;
474 
475 		/*
476 		 * Verify the config's rate/lane-count values and its ordering relative
477 		 * to the previous config.
478 		 */
479 		if (last_config.rate)
480 			verify_config_order(test, config_order, &last_config, &iter_config);
481 		last_config = iter_config;
482 
483 		KUNIT_EXPECT_TRUE(test, has_rate(&expected_table->rates,
484 						 iter_config.rate));
485 		KUNIT_EXPECT_LE(test, iter_config.lane_count,
486 				      expected_table->max_lane_count);
487 		KUNIT_EXPECT_TRUE(test, is_power_of_2(iter_config.lane_count));
488 
489 		/* Verify the config's disabled state */
490 		KUNIT_EXPECT_FALSE(test, has_config(&expected_table->disabled_configs,
491 						    &iter_config));
492 
493 		/*
494 		 * Update the max limits for allowed configs, verified at the
495 		 * end for the whole config table.
496 		 */
497 
498 		bw = drm_dp_max_dprx_data_rate(iter_config.rate, iter_config.lane_count);
499 		if (bw > max_bw ||
500 		    (bw == max_bw && iter_config.rate > expected_max_bw_config.rate)) {
501 			max_bw = bw;
502 			expected_max_bw_config = iter_config;
503 		}
504 	}
505 	ops->iter_end(&iter);
506 
507 	KUNIT_EXPECT_EQ(test, num_actual_configs, expected_num_configs(expected_table, max_limits));
508 
509 	ops->get_max_bw_config(link_caps, &actual_max_bw_config);
510 	KUNIT_EXPECT_TRUE(test, link_configs_match(&expected_max_bw_config,
511 						   &actual_max_bw_config));
512 
513 	KUNIT_ASSERT_TRUE(test, ops->set_max_limits(link_caps, &old_max_limits));
514 }
515 
516 static bool max_limits_valid(const struct test_config_table *expected_table,
517 			     const struct intel_dp_link_config *max_limits)
518 {
519 	int lane_count;
520 	int rate_idx;
521 
522 	for (rate_idx = 0; rate_idx < expected_table->rates.size; rate_idx++) {
523 		for (lane_count = 1; lane_count <= expected_table->max_lane_count; lane_count <<= 1) {
524 			struct intel_dp_link_config config = {
525 				.rate = expected_table->rates.entries[rate_idx],
526 				.lane_count = lane_count,
527 			};
528 
529 			if (has_config(&expected_table->disabled_configs, &config))
530 				continue;
531 
532 			if (config.rate <= max_limits->rate &&
533 			    config.lane_count <= max_limits->lane_count)
534 				return true;
535 		}
536 	}
537 
538 	return false;
539 }
540 
541 static void get_max_limits(const struct test_config_table *expected_table,
542 			   struct intel_dp_link_config *max_limits)
543 {
544 	int lane_count;
545 	int rate_idx;
546 
547 	max_limits->rate = 0;
548 	max_limits->lane_count = 0;
549 
550 	for (rate_idx = 0; rate_idx < expected_table->rates.size; rate_idx++) {
551 		for (lane_count = 1; lane_count <= expected_table->max_lane_count; lane_count <<= 1) {
552 			struct intel_dp_link_config config = {
553 				.rate = expected_table->rates.entries[rate_idx],
554 				.lane_count = lane_count,
555 			};
556 
557 			if (has_config(&expected_table->disabled_configs, &config))
558 				continue;
559 
560 			max_limits->rate = max(max_limits->rate, config.rate);
561 			max_limits->lane_count = max(max_limits->lane_count, config.lane_count);
562 		}
563 	}
564 }
565 
566 static void verify_link_caps(const struct test_config_table *expected_table,
567 			     struct intel_dp_link_caps *link_caps)
568 {
569 	struct kunit *test = expected_table->test;
570 	struct test_ctx *ctx = test->priv;
571 	const struct intel_dp_link_caps_test_ops *ops = ctx->link_caps_ops;
572 	struct intel_dp_link_config max_limits;
573 	int i;
574 
575 	get_max_limits(expected_table, &max_limits);
576 
577 	for (i = 0; i < ARRAY_SIZE(config_orders); i++) {
578 		int lane_count;
579 		int rate_idx;
580 
581 		verify_link_caps_for_order(expected_table, link_caps, config_orders[i], &max_limits);
582 		/*
583 		 * Verify iteration after setting the max limits to each
584 		 * configurations.
585 		 */
586 		for (rate_idx = 0; rate_idx < expected_table->rates.size; rate_idx++) {
587 			for (lane_count = 1; lane_count <= expected_table->max_lane_count; lane_count <<= 1) {
588 				struct intel_dp_link_config config = {
589 					.rate = expected_table->rates.entries[rate_idx],
590 					.lane_count = lane_count,
591 				};
592 
593 				if (!max_limits_valid(expected_table, &config)) {
594 					/* Verify that invalid max limits are rejected. */
595 					KUNIT_EXPECT_FALSE(test, ops->set_max_limits(link_caps, &config));
596 
597 					continue;
598 				}
599 
600 				verify_link_caps_for_order(expected_table, link_caps, config_orders[i],
601 							   &config);
602 			}
603 		}
604 	}
605 }
606 
607 static void update_link_caps_and_verify(struct test_config_table *expected_table,
608 					struct intel_dp_link_caps *link_caps,
609 					bool reset)
610 {
611 	struct kunit *test = expected_table->test;
612 	struct test_ctx *ctx = test->priv;
613 	const struct intel_dp_link_caps_test_ops *ops =
614 		ctx->link_caps_ops;
615 	bool link_params_changed;
616 
617 	link_params_changed = ops->update(link_caps,
618 					  expected_table->rates.entries,
619 					  expected_table->rates.size,
620 					  expected_table->max_lane_count,
621 					  reset);
622 	KUNIT_EXPECT_TRUE(test, !reset || link_params_changed);
623 
624 	/*
625 	 * ops->update() re-enables all configurations when called with
626 	 * reset=true, or changed link parameters.
627 	 */
628 	if (link_params_changed)
629 		expected_table->disabled_configs.size = 0;
630 
631 	verify_link_caps(expected_table, link_caps);
632 }
633 
634 static void intel_dp_link_caps_test_update_reset(struct kunit *test)
635 {
636 	struct test_ctx *ctx = test->priv;
637 	struct intel_dp_link_caps *link_caps = ctx->dev.dig_port.dp.link.caps;
638 	int max_lane_count;
639 	int num_rates;
640 
641 	for (max_lane_count = 1;
642 	     max_lane_count <= LINK_TEST_MAX_LANE_COUNT;
643 	     max_lane_count <<= 1) {
644 		for (num_rates = 1;
645 		     num_rates <= LINK_TEST_NUM_STANDARD_RATES;
646 		     num_rates++) {
647 			struct test_config_table expected_table =
648 				INIT_STANDARD_TABLE(test, num_rates,
649 						    max_lane_count);
650 
651 			update_link_caps_and_verify(&expected_table, link_caps, true);
652 		}
653 	}
654 }
655 
656 /*
657  * TEST: Update shrink and expand
658  * ------------------------------
659  * Verify that removing or adding supported rates/lane counts updates
660  * the configuration table accordingly.
661  */
662 static void disable_configs_and_verify(struct kunit *test,
663 				       struct intel_dp_link_caps *link_caps,
664 				       struct test_config_table *expected_table,
665 				       const struct link_config_set *config_set)
666 {
667 	struct test_ctx *ctx = test->priv;
668 	const struct intel_dp_link_caps_test_ops *ops =
669 		ctx->link_caps_ops;
670 	int i;
671 
672 	for (i = 0; i < config_set->size; i++) {
673 		KUNIT_ASSERT_FALSE(test, has_config(&expected_table->disabled_configs,
674 						    &config_set->entries[i]));
675 		add_config(test, &expected_table->disabled_configs, &config_set->entries[i]);
676 
677 		KUNIT_ASSERT_TRUE(test, ops->disable_config(link_caps, &config_set->entries[i]));
678 
679 		verify_link_caps(expected_table, link_caps);
680 	}
681 }
682 
683 static void disable_configs_for_shrink_and_verify(struct test_config_table *expected_table,
684 						  struct intel_dp_link_caps *link_caps)
685 {
686 	struct kunit *test = expected_table->test;
687 	struct link_config_set config_set = {};
688 	struct intel_dp_link_config max_config;
689 
690 	/*
691 	 * When configs shrink disable the config with the
692 	 * second-highest rate, lane params, so the disabled config
693 	 * stays around after the configs got shrunk.
694 	 */
695 	KUNIT_ASSERT_GE(test, expected_table->rates.size, 2);
696 	KUNIT_ASSERT_GE(test, expected_table->max_lane_count, 2);
697 
698 	max_config.rate = expected_table->rates.entries[expected_table->rates.size - 2];
699 	max_config.lane_count = expected_table->max_lane_count >> 1;
700 
701 	add_config(test, &config_set, &max_config);
702 	disable_configs_and_verify(test, link_caps, expected_table,
703 				   &config_set);
704 }
705 
706 static void disable_configs_for_expand_and_verify(struct test_config_table *expected_table,
707 						  struct intel_dp_link_caps *link_caps)
708 {
709 	struct kunit *test = expected_table->test;
710 	struct link_config_set config_set = {};
711 	struct intel_dp_link_config max_config;
712 
713 	KUNIT_ASSERT_GE(test, expected_table->rates.size, 1);
714 
715 	max_config.rate = expected_table->rates.entries[expected_table->rates.size - 1];
716 	max_config.lane_count = expected_table->max_lane_count;
717 
718 	add_config(test, &config_set, &max_config);
719 	disable_configs_and_verify(test, link_caps, expected_table,
720 				   &config_set);
721 }
722 
723 static void get_nth_rate_lane_config(const struct test_config_table *expected_table, int n,
724 				     struct intel_dp_link_config *config)
725 {
726 	int num_lane_configs = LINK_TEST_NUM_LANE_CONFIGS(expected_table->max_lane_count);
727 	int rate_idx = n / num_lane_configs;
728 	int lane_count_exp = n % num_lane_configs;
729 
730 	config->rate = expected_table->rates.entries[rate_idx];
731 	config->lane_count = 1 << lane_count_exp;
732 }
733 
734 static void test_update_rates_shrink(struct kunit *test, bool disable_configs)
735 {
736 	struct test_ctx *ctx = test->priv;
737 	struct intel_dp_link_caps *link_caps = ctx->dev.dig_port.dp.link.caps;
738 	struct test_config_table expected_table =
739 		INIT_STANDARD_TABLE(test, LINK_TEST_NUM_STANDARD_RATES,
740 					  LINK_TEST_MAX_LANE_COUNT);
741 
742 	update_link_caps_and_verify(&expected_table, link_caps, true);
743 
744 	while (expected_table.rates.size > 1) {
745 		if (disable_configs)
746 			disable_configs_for_shrink_and_verify(&expected_table, link_caps);
747 
748 		expected_table.rates.size--;
749 
750 		update_link_caps_and_verify(&expected_table, link_caps, false);
751 	}
752 }
753 
754 static void intel_dp_link_caps_test_update_rates_shrink(struct kunit *test)
755 {
756 	test_update_rates_shrink(test, false);
757 }
758 
759 static void intel_dp_link_caps_test_update_rates_shrink_disable(struct kunit *test)
760 {
761 	test_update_rates_shrink(test, true);
762 }
763 
764 static void test_update_rates_expand(struct kunit *test, bool disable_configs)
765 {
766 	struct test_ctx *ctx = test->priv;
767 	struct intel_dp_link_caps *link_caps = ctx->dev.dig_port.dp.link.caps;
768 	struct test_config_table expected_table =
769 		INIT_STANDARD_TABLE(test, 1, LINK_TEST_MAX_LANE_COUNT);
770 
771 	update_link_caps_and_verify(&expected_table, link_caps, true);
772 
773 	while (expected_table.rates.size < LINK_TEST_NUM_STANDARD_RATES) {
774 		if (disable_configs)
775 			disable_configs_for_expand_and_verify(&expected_table, link_caps);
776 
777 		expected_table.rates.size++;
778 
779 		update_link_caps_and_verify(&expected_table, link_caps, false);
780 	}
781 }
782 
783 static void intel_dp_link_caps_test_update_rates_expand(struct kunit *test)
784 {
785 	test_update_rates_expand(test, false);
786 }
787 
788 static void intel_dp_link_caps_test_update_rates_expand_disable(struct kunit *test)
789 {
790 	test_update_rates_expand(test, true);
791 }
792 
793 static void test_update_lanes_shrink(struct kunit *test, bool disable_configs)
794 {
795 	struct test_ctx *ctx = test->priv;
796 	struct intel_dp_link_caps *link_caps = ctx->dev.dig_port.dp.link.caps;
797 	struct test_config_table expected_table =
798 		INIT_STANDARD_TABLE(test, LINK_TEST_NUM_STANDARD_RATES,
799 					  LINK_TEST_MAX_LANE_COUNT);
800 
801 	update_link_caps_and_verify(&expected_table, link_caps, true);
802 
803 	while (expected_table.max_lane_count > 1) {
804 		if (disable_configs)
805 			disable_configs_for_shrink_and_verify(&expected_table, link_caps);
806 
807 		expected_table.max_lane_count >>= 1;
808 
809 		update_link_caps_and_verify(&expected_table, link_caps, false);
810 	}
811 }
812 
813 static void intel_dp_link_caps_test_update_lanes_shrink(struct kunit *test)
814 {
815 	test_update_lanes_shrink(test, false);
816 }
817 
818 static void intel_dp_link_caps_test_update_lanes_shrink_disable(struct kunit *test)
819 {
820 	test_update_lanes_shrink(test, true);
821 }
822 
823 static void test_update_lanes_expand(struct kunit *test, bool disable_configs)
824 {
825 	struct test_ctx *ctx = test->priv;
826 	struct intel_dp_link_caps *link_caps = ctx->dev.dig_port.dp.link.caps;
827 	struct test_config_table expected_table =
828 		INIT_STANDARD_TABLE(test, LINK_TEST_NUM_STANDARD_RATES, 1);
829 
830 	update_link_caps_and_verify(&expected_table, link_caps, true);
831 
832 	while (expected_table.max_lane_count < LINK_TEST_MAX_LANE_COUNT) {
833 		if (disable_configs)
834 			disable_configs_for_expand_and_verify(&expected_table, link_caps);
835 
836 		expected_table.max_lane_count <<= 1;
837 
838 		update_link_caps_and_verify(&expected_table, link_caps, false);
839 	}
840 }
841 
842 static void intel_dp_link_caps_test_update_lanes_expand(struct kunit *test)
843 {
844 	test_update_lanes_expand(test, false);
845 }
846 
847 static void intel_dp_link_caps_test_update_lanes_expand_disable(struct kunit *test)
848 {
849 	test_update_lanes_expand(test, true);
850 }
851 
852 static void disable_random_configs_and_verify(struct test_config_table *expected_table,
853 					      struct intel_dp_link_caps *link_caps)
854 {
855 	struct kunit *test = expected_table->test;
856 	struct test_ctx *ctx = test->priv;
857 	struct link_config_set config_set = {};
858 	u32 disabled_config_mask;
859 	int num_configs;
860 	int i;
861 
862 	num_configs = get_num_configs(expected_table->rates.size,
863 				      expected_table->max_lane_count);
864 	disabled_config_mask = prandom_u32_state(&ctx->rnd) &
865 			       GENMASK_U32(num_configs - 1, 0);
866 
867 	for (i = 0; i < num_configs; i++) {
868 		struct intel_dp_link_config config;
869 
870 		/* At least one config must remain enabled. */
871 		if (expected_table->disabled_configs.size +
872 		    config_set.size + 1 >= num_configs)
873 			break;
874 
875 		if (!(BIT(i) & disabled_config_mask))
876 			continue;
877 
878 		get_nth_rate_lane_config(expected_table, i, &config);
879 		/* Don't disable a config twice. */
880 		if (has_config(&expected_table->disabled_configs, &config))
881 			continue;
882 
883 		add_config(test, &config_set, &config);
884 	}
885 
886 	disable_configs_and_verify(test, link_caps, expected_table,
887 				   &config_set);
888 }
889 
890 static void get_params_shrink_step(struct test_ctx *ctx,
891 				   int num_rates, int max_lane_count,
892 				   int *rates_step, int *lanes_step)
893 {
894 	int shrink_mask;
895 
896 	*rates_step = 0;
897 	*lanes_step = 0;
898 
899 	if (num_rates == 1)
900 		shrink_mask = BIT(0);				/* shrink only lanes */
901 	else if (max_lane_count == 1)
902 		shrink_mask = BIT(1);				/* shrink only rates */
903 	else
904 		shrink_mask = rand_in_range(ctx,
905 					    BIT(0),
906 					    BIT(0) | BIT(1));	/* shrink one or both params */
907 
908 	if (shrink_mask & BIT(1))
909 		*rates_step = rand_in_range(ctx, 1, num_rates - 1);
910 
911 	if (shrink_mask & BIT(0))
912 		*lanes_step = rand_in_range(ctx, 1, ilog2(max_lane_count));
913 }
914 
915 static void get_params_expand_step(struct test_ctx *ctx,
916 				   int max_num_rates, int num_rates,
917 				   int max_supported_lane_count, int max_lane_count,
918 				   int *rates_step, int *lanes_step)
919 {
920 	int expand_mask;
921 
922 	*rates_step = 0;
923 	*lanes_step = 0;
924 
925 	if (num_rates == max_num_rates)
926 		expand_mask = BIT(0);				/* expand only lanes */
927 	else if (max_lane_count == max_supported_lane_count)
928 		expand_mask = BIT(1);				/* expand only rates */
929 	else
930 		expand_mask = rand_in_range(ctx,
931 					    BIT(0),
932 					    BIT(0) | BIT(1));	/* expand one or both params */
933 
934 	if (expand_mask & BIT(1))
935 		*rates_step = rand_in_range(ctx, 1, max_num_rates - num_rates);
936 
937 	if (expand_mask & BIT(0))
938 		*lanes_step = rand_in_range(ctx, 1, ilog2(max_supported_lane_count /
939 							  max_lane_count));
940 }
941 
942 static void test_update_params_shrink_random(struct kunit *test, bool disable_configs)
943 {
944 	struct test_ctx *ctx = test->priv;
945 	struct intel_dp_link_caps *link_caps = ctx->dev.dig_port.dp.link.caps;
946 	struct test_config_table expected_table =
947 		INIT_STANDARD_TABLE(test, LINK_TEST_NUM_STANDARD_RATES,
948 					  LINK_TEST_MAX_LANE_COUNT);
949 
950 	update_link_caps_and_verify(&expected_table, link_caps, true);
951 
952 	while (expected_table.rates.size > 1 || expected_table.max_lane_count > 1) {
953 		int rates_step;
954 		int lanes_step;
955 
956 		if (disable_configs)
957 			disable_random_configs_and_verify(&expected_table, link_caps);
958 
959 		get_params_shrink_step(ctx,
960 				       expected_table.rates.size,
961 				       expected_table.max_lane_count,
962 				       &rates_step, &lanes_step);
963 
964 		expected_table.rates.size -= rates_step;
965 		expected_table.max_lane_count >>= lanes_step;
966 
967 		update_link_caps_and_verify(&expected_table, link_caps, false);
968 	}
969 }
970 
971 static void intel_dp_link_caps_test_update_params_shrink_random(struct kunit *test)
972 {
973 	int i;
974 
975 	for (i = 0; i < LINK_TEST_NUM_RANDOM_ITERATIONS; i++)
976 		test_update_params_shrink_random(test, false);
977 }
978 
979 static void intel_dp_link_caps_test_update_params_shrink_disable_random(struct kunit *test)
980 {
981 	int i;
982 
983 	for (i = 0; i < LINK_TEST_NUM_RANDOM_ITERATIONS; i++)
984 		test_update_params_shrink_random(test, true);
985 }
986 
987 static void test_update_params_expand_random(struct kunit *test, bool disable_configs)
988 {
989 	struct test_ctx *ctx = test->priv;
990 	struct intel_dp_link_caps *link_caps = ctx->dev.dig_port.dp.link.caps;
991 	struct test_config_table expected_table =
992 		INIT_STANDARD_TABLE(test, 1, 1);
993 
994 	update_link_caps_and_verify(&expected_table, link_caps, true);
995 
996 	while (expected_table.rates.size < LINK_TEST_NUM_STANDARD_RATES ||
997 	       expected_table.max_lane_count < LINK_TEST_MAX_LANE_COUNT) {
998 		int rates_step;
999 		int lanes_step;
1000 
1001 		if (disable_configs)
1002 			disable_random_configs_and_verify(&expected_table, link_caps);
1003 
1004 		get_params_expand_step(ctx,
1005 				       LINK_TEST_NUM_STANDARD_RATES,
1006 				       expected_table.rates.size,
1007 				       LINK_TEST_MAX_LANE_COUNT,
1008 				       expected_table.max_lane_count,
1009 				       &rates_step, &lanes_step);
1010 
1011 		expected_table.rates.size += rates_step;
1012 		expected_table.max_lane_count <<= lanes_step;
1013 
1014 		update_link_caps_and_verify(&expected_table, link_caps, false);
1015 	}
1016 }
1017 
1018 static void intel_dp_link_caps_test_update_params_expand_random(struct kunit *test)
1019 {
1020 	int i;
1021 
1022 	for (i = 0; i < LINK_TEST_NUM_RANDOM_ITERATIONS; i++)
1023 		test_update_params_expand_random(test, false);
1024 }
1025 
1026 static void intel_dp_link_caps_test_update_params_expand_disable_random(struct kunit *test)
1027 {
1028 	int i;
1029 
1030 	for (i = 0; i < LINK_TEST_NUM_RANDOM_ITERATIONS; i++)
1031 		test_update_params_expand_random(test, true);
1032 }
1033 
1034 /*
1035  * TEST: Fallback sequence
1036  * -----------------------
1037  * Verify the eDP fallback logic to set the maximum supported configuration
1038  * as a preference.
1039  *
1040  * For DP SST and MST verify fallback selection from the connector's
1041  * maximum configuration and iteration of the resulting allowed
1042  * configurations.
1043  */
1044 static void intel_dp_link_test_fallback_for_edp(struct kunit *test)
1045 {
1046 	struct test_ctx *ctx = test->priv;
1047 	struct intel_dp_link_caps *link_caps = ctx->dev.dig_port.dp.link.caps;
1048 	struct test_config_table expected_table =
1049 		INIT_STANDARD_TABLE(test, LINK_TEST_NUM_STANDARD_RATES,
1050 					  LINK_TEST_MAX_LANE_COUNT);
1051 	struct intel_digital_port *dig_port = &ctx->dev.dig_port;
1052 	const struct intel_dp_link_training_test_ops *lt_ops =
1053 		ctx->link_training_ops;
1054 	const struct intel_dp_link_caps_test_ops *lc_ops =
1055 		ctx->link_caps_ops;
1056 	struct intel_dp_link_config min_config = {
1057 		.rate = expected_table.rates.entries[0],
1058 		.lane_count = 1,
1059 	};
1060 	struct intel_dp_link_config max_config = {
1061 		.rate = expected_table.rates.entries[expected_table.rates.size - 1],
1062 		.lane_count = LINK_TEST_MAX_LANE_COUNT,
1063 	};
1064 	struct intel_dp_link_caps_order order;
1065 	struct intel_dp_link_config iter_config;
1066 	struct intel_dp_link_caps_iter iter;
1067 	int fallback_err;
1068 
1069 	dig_port->base.type = INTEL_OUTPUT_EDP;
1070 	ctx->dev.dig_port.dp.use_max_params = false;
1071 
1072 	update_link_caps_and_verify(&expected_table, link_caps, true);
1073 
1074 	order = lc_ops->connector_compute_order(&ctx->dev.connector);
1075 
1076 	lc_ops->iter_start(&iter, link_caps, order, INTEL_DP_LINK_CAPS_FILTER_ALL);
1077 	for_each_dp_link_config(&iter, &iter_config)
1078 		break;
1079 	lc_ops->iter_end(&iter);
1080 
1081 	KUNIT_EXPECT_FALSE(test, ctx->dev.dig_port.dp.use_max_params);
1082 	KUNIT_EXPECT_TRUE(test, link_configs_match(&iter_config, &min_config));
1083 
1084 	ctx->dev.crtc_state.output_types = BIT(dig_port->base.type);
1085 	ctx->dev.crtc_state.port_clock = min_config.rate;
1086 	ctx->dev.crtc_state.lane_count = min_config.lane_count;
1087 
1088 	fallback_err = lt_ops->get_fallback_values(&ctx->dev.dig_port.dp, &ctx->dev.crtc_state);
1089 	KUNIT_EXPECT_EQ(test, fallback_err, 0);
1090 
1091 	/* The fallback should've changed the order. */
1092 	order = lc_ops->connector_compute_order(&ctx->dev.connector);
1093 
1094 	lc_ops->iter_start(&iter, link_caps, order, INTEL_DP_LINK_CAPS_FILTER_ALL);
1095 	for_each_dp_link_config(&iter, &iter_config)
1096 		break;
1097 	lc_ops->iter_end(&iter);
1098 
1099 	KUNIT_EXPECT_TRUE(test, ctx->dev.dig_port.dp.use_max_params);
1100 	KUNIT_EXPECT_TRUE(test, link_configs_match(&iter_config, &max_config));
1101 }
1102 
1103 static bool test_fallback_from_target(struct test_config_table *expected_table,
1104 				      enum intel_output_type output_type,
1105 				      const struct intel_dp_link_config *expected_target_config,
1106 				      const struct intel_dp_link_config *expected_fallback_config)
1107 {
1108 	struct kunit *test = expected_table->test;
1109 	struct test_ctx *ctx = test->priv;
1110 	struct intel_dp_link_caps *link_caps = ctx->dev.dig_port.dp.link.caps;
1111 	struct intel_dp_link_config iter_config;
1112 	const struct intel_dp_link_training_test_ops *lt_ops =
1113 		ctx->link_training_ops;
1114 	const struct intel_dp_link_caps_test_ops *lc_ops =
1115 		ctx->link_caps_ops;
1116 	/* Modify default order direction for max config lookup. */
1117 	struct intel_dp_link_caps_order fallback_order =
1118 		lc_ops->connector_fallback_order(ctx->dev.connector.mst.dp);
1119 	struct intel_dp_link_caps_iter iter;
1120 	int expected_fallback_err = 0;
1121 	int fallback_err;
1122 
1123 	/* Get the max connector config, optionally filtered to the max_rate limit. */
1124 	lc_ops->iter_start(&iter, link_caps, fallback_order, INTEL_DP_LINK_CAPS_FILTER_ALL);
1125 	for_each_dp_link_config(&iter, &iter_config)
1126 		break;
1127 	lc_ops->iter_end(&iter);
1128 
1129 	KUNIT_EXPECT_TRUE(test, link_configs_match(&iter_config,
1130 						   expected_target_config));
1131 	KUNIT_EXPECT_FALSE(test, link_configs_match(&iter_config,
1132 						    &INTEL_DP_LINK_CONFIG_NULL));
1133 
1134 	ctx->dev.crtc_state.output_types = BIT(output_type);
1135 	ctx->dev.crtc_state.port_clock = expected_target_config->rate;
1136 	ctx->dev.crtc_state.lane_count = expected_target_config->lane_count;
1137 
1138 	if (link_configs_match(expected_fallback_config, &INTEL_DP_LINK_CONFIG_NULL))
1139 		expected_fallback_err = -1;
1140 
1141 	fallback_err = lt_ops->get_fallback_values(&ctx->dev.dig_port.dp, &ctx->dev.crtc_state);
1142 	KUNIT_EXPECT_EQ(test, fallback_err, expected_fallback_err);
1143 
1144 	if (!fallback_err) {
1145 		/*
1146 		 * NOTE: This test does not verify any implied fallback
1147 		 * target selection.
1148 		 *
1149 		 * The current driver behavior may still select a fallback
1150 		 * configuration indirectly via max_limits, but that is an
1151 		 * implementation artifact rather than part of the intended
1152 		 * fallback API behavior, and is therefore not verified here.
1153 		 *
1154 		 * Instead, the effect of the fallback logic is verified by
1155 		 * checking that the failed target configuration is disabled.
1156 		 * Selecting the next target configuration from the remaining
1157 		 * allowed configurations belongs to the modeset link target
1158 		 * selection logic.
1159 		 */
1160 		add_config(test, &expected_table->disabled_configs,
1161 			   expected_target_config);
1162 	}
1163 
1164 	verify_link_caps(expected_table, link_caps);
1165 
1166 	return !fallback_err;
1167 }
1168 
1169 static const struct link_config_set *
1170 get_target_configs_for_output_type(struct kunit *test,
1171 				   enum intel_output_type output_type)
1172 {
1173 	switch (output_type) {
1174 	case INTEL_OUTPUT_DDI:
1175 	case INTEL_OUTPUT_DP:
1176 	case INTEL_OUTPUT_EDP:
1177 		return &standard_dp_link_configs[INTEL_DP_LINK_CAPS_ORDER_KEY_RATE_LANE];
1178 	case INTEL_OUTPUT_DP_MST:
1179 		return &standard_dp_link_configs[INTEL_DP_LINK_CAPS_ORDER_KEY_BW];
1180 	default:
1181 		KUNIT_FAIL_AND_ABORT(test, "Missing output type: %d", output_type);
1182 	}
1183 }
1184 
1185 static const struct link_config_set *
1186 get_fallback_configs_for_output_type(struct kunit *test,
1187 				     enum intel_output_type output_type)
1188 {
1189 	switch (output_type) {
1190 	case INTEL_OUTPUT_DDI:
1191 	case INTEL_OUTPUT_DP:
1192 	case INTEL_OUTPUT_EDP:
1193 		return &standard_dp_link_configs[INTEL_DP_LINK_CAPS_ORDER_KEY_LANE_RATE];
1194 	case INTEL_OUTPUT_DP_MST:
1195 		return &standard_dp_link_configs[INTEL_DP_LINK_CAPS_ORDER_KEY_BW];
1196 	default:
1197 		KUNIT_FAIL_AND_ABORT(test, "Missing output type: %d", output_type);
1198 	}
1199 }
1200 
1201 static void assert_config_is_supported(const struct test_config_table *expected_table,
1202 				       const struct intel_dp_link_config *config)
1203 {
1204 	struct kunit *test = expected_table->test;
1205 
1206 	KUNIT_ASSERT_TRUE(test, has_rate(&expected_table->rates, config->rate));
1207 	KUNIT_ASSERT_LE(test, config->lane_count, expected_table->max_lane_count);
1208 }
1209 
1210 static bool get_fallback_config(const struct test_config_table *expected_table,
1211 				enum intel_output_type output_type,
1212 				const struct intel_dp_link_config *target_config,
1213 				struct intel_dp_link_config *fallback_config)
1214 {
1215 	struct kunit *test = expected_table->test;
1216 	const struct link_config_set *config_set =
1217 		get_fallback_configs_for_output_type(test, output_type);
1218 	int i;
1219 
1220 	i = lookup_config(config_set, target_config);
1221 	KUNIT_ASSERT_GE(test, i, 0);
1222 
1223 	for (i--; i >= 0; i--) {
1224 		const struct intel_dp_link_config *config =
1225 			&config_set->entries[i];
1226 
1227 		assert_config_is_supported(expected_table, config);
1228 		*fallback_config = *config;
1229 
1230 		return true;
1231 	}
1232 
1233 	return false;
1234 }
1235 
1236 static bool get_target_config(const struct test_config_table *expected_table,
1237 			      enum intel_output_type output_type,
1238 			      struct intel_dp_link_config *target)
1239 {
1240 	struct kunit *test = expected_table->test;
1241 	const struct link_config_set *config_set =
1242 		get_target_configs_for_output_type(test, output_type);
1243 	int i;
1244 
1245 	for (i = config_set->size - 1; i >= 0; i--) {
1246 		const struct intel_dp_link_config *config =
1247 			&config_set->entries[i];
1248 
1249 		assert_config_is_supported(expected_table, config);
1250 		*target = *config;
1251 
1252 		return true;
1253 	}
1254 
1255 	return false;
1256 }
1257 
1258 static void test_fallback_seq(struct kunit *test,
1259 			      enum intel_output_type output_type)
1260 {
1261 	struct test_ctx *ctx = test->priv;
1262 	struct intel_dp_link_caps *link_caps = ctx->dev.dig_port.dp.link.caps;
1263 	struct test_config_table expected_table =
1264 		INIT_STANDARD_TABLE(test, LINK_TEST_NUM_STANDARD_RATES,
1265 					  LINK_TEST_MAX_LANE_COUNT);
1266 	struct intel_digital_port *dig_port = &ctx->dev.dig_port;
1267 	struct intel_dp_link_config fallback_config = {};
1268 	struct intel_dp_link_config target_config;
1269 	int fallback_count = 0;
1270 	bool target_found;
1271 
1272 	dig_port->base.type = output_type;
1273 	ctx->dev.dig_port.dp.use_max_params = false;
1274 
1275 	update_link_caps_and_verify(&expected_table, link_caps, true);
1276 
1277 	/* Get the initial target config. */
1278 	target_found = get_target_config(&expected_table, output_type,
1279 					 &target_config);
1280 	KUNIT_ASSERT_TRUE(test, target_found);
1281 
1282 	for (;;) {
1283 		/* Also test the case where no fallback is available. */
1284 		if (!get_fallback_config(&expected_table, output_type,
1285 					 &target_config, &fallback_config))
1286 			fallback_config = INTEL_DP_LINK_CONFIG_NULL;
1287 
1288 		if (!test_fallback_from_target(&expected_table, output_type,
1289 					       &target_config, &fallback_config))
1290 			break;
1291 
1292 		/* Simply select the fallback config as the next target. */
1293 		target_config = fallback_config;
1294 
1295 		fallback_count++;
1296 		KUNIT_ASSERT_LT(test, fallback_count, LINK_TEST_MAX_CONFIGS);
1297 	}
1298 }
1299 
1300 static void intel_dp_link_test_fallback_for_sst(struct kunit *test)
1301 {
1302 	test_fallback_seq(test, INTEL_OUTPUT_DP);
1303 }
1304 
1305 static void intel_dp_link_test_fallback_for_mst(struct kunit *test)
1306 {
1307 	struct test_ctx *ctx = test->priv;
1308 
1309 	ctx->dev.connector.mst.dp = &ctx->dev.dig_port.dp;
1310 
1311 	test_fallback_seq(test, INTEL_OUTPUT_DP_MST);
1312 }
1313 
1314 static struct kunit_case intel_dp_link_test_cases[] = {
1315 	KUNIT_CASE(intel_dp_link_caps_test_baseline),
1316 
1317 	KUNIT_CASE(intel_dp_link_caps_test_update_reset),
1318 
1319 	KUNIT_CASE(intel_dp_link_caps_test_update_rates_shrink),
1320 	KUNIT_CASE(intel_dp_link_caps_test_update_rates_shrink_disable),
1321 	KUNIT_CASE(intel_dp_link_caps_test_update_rates_expand),
1322 	KUNIT_CASE(intel_dp_link_caps_test_update_rates_expand_disable),
1323 	KUNIT_CASE(intel_dp_link_caps_test_update_lanes_shrink),
1324 	KUNIT_CASE(intel_dp_link_caps_test_update_lanes_shrink_disable),
1325 	KUNIT_CASE(intel_dp_link_caps_test_update_lanes_expand),
1326 	KUNIT_CASE(intel_dp_link_caps_test_update_lanes_expand_disable),
1327 	KUNIT_CASE(intel_dp_link_caps_test_update_params_shrink_random),
1328 	KUNIT_CASE(intel_dp_link_caps_test_update_params_shrink_disable_random),
1329 	KUNIT_CASE(intel_dp_link_caps_test_update_params_expand_random),
1330 	KUNIT_CASE(intel_dp_link_caps_test_update_params_expand_disable_random),
1331 
1332 	KUNIT_CASE(intel_dp_link_test_fallback_for_edp),
1333 	KUNIT_CASE(intel_dp_link_test_fallback_for_sst),
1334 	KUNIT_CASE(intel_dp_link_test_fallback_for_mst),
1335 
1336 	{}
1337 };
1338 
1339 static struct test_ctx test_ctx;
1340 
1341 static int intel_dp_link_test_init(struct kunit *test)
1342 {
1343 	struct intel_digital_port *dig_port;
1344 	struct intel_encoder *encoder;
1345 	struct intel_dp *intel_dp;
1346 
1347 	/* Reset the dev state for each test. */
1348 	memset(&test_ctx.dev, 0, sizeof(test_ctx.dev));
1349 
1350 	test_ctx.dev.generic_device.drm.dev = &test_ctx.dev.device;
1351 
1352 	test_ctx.dev.display.drm = &test_ctx.dev.generic_device.drm;
1353 	test_ctx.dev.generic_device.display = &test_ctx.dev.display;
1354 
1355 	encoder = &test_ctx.dev.dig_port.base;
1356 	encoder->base.dev = &test_ctx.dev.generic_device.drm;
1357 
1358 	dig_port = &test_ctx.dev.dig_port;
1359 	dig_port->base.type = INTEL_OUTPUT_DP;
1360 
1361 	test_ctx.dev.connector.encoder = encoder;
1362 
1363 	intel_dp = &dig_port->dp;
1364 	intel_dp->attached_connector = &test_ctx.dev.connector;
1365 
1366 	intel_dp->link.caps = test_ctx.link_caps_ops->init(intel_dp);
1367 
1368 	test->priv = &test_ctx;
1369 
1370 	return 0;
1371 }
1372 
1373 static void intel_dp_link_test_exit(struct kunit *test)
1374 {
1375 	struct test_ctx *ctx = test->priv;
1376 
1377 	ctx->link_caps_ops->cleanup(ctx->dev.dig_port.dp.link.caps);
1378 }
1379 
1380 static int intel_dp_link_test_suite_init(struct kunit_suite *test_suite)
1381 {
1382 #ifdef I915
1383 	test_ctx.link_caps_ops = &i915_display_dp_link_caps_test_ops;
1384 	test_ctx.link_training_ops = &i915_display_dp_link_training_test_ops;
1385 #else
1386 	test_ctx.link_caps_ops = &intel_display_dp_link_caps_test_ops;
1387 	test_ctx.link_training_ops = &intel_display_dp_link_training_test_ops;
1388 #endif
1389 	prandom_seed_state(&test_ctx.rnd, 0);
1390 
1391 	return 0;
1392 }
1393 
1394 static struct kunit_suite intel_dp_link_test_suite = {
1395 	.name = "intel_dp_link",
1396 	.suite_init = intel_dp_link_test_suite_init,
1397 	.init = intel_dp_link_test_init,
1398 	.exit = intel_dp_link_test_exit,
1399 	.test_cases = intel_dp_link_test_cases,
1400 };
1401 
1402 kunit_test_suites(&intel_dp_link_test_suite);
1403 
1404 MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
1405 
1406 MODULE_AUTHOR("Intel Corporation");
1407 MODULE_LICENSE("GPL and additional rights");
1408 MODULE_DESCRIPTION("Intel DP link KUnit tests");
1409