1 // SPDX-License-Identifier: GPL-2.0-only
3 * Test cases for the DRM DP MST helpers
8 #include <kunit/test.h>
10 #include <drm/display/drm_dp_mst_helper.h>
11 #include <drm/drm_print.h>
13 #include "../display/drm_dp_mst_topology_internal.h"
15 struct drm_dp_mst_calc_pbn_mode_test {
22 static const struct drm_dp_mst_calc_pbn_mode_test drm_dp_mst_calc_pbn_mode_cases[] = {
55 static void drm_test_dp_mst_calc_pbn_mode(struct kunit *test)
57 const struct drm_dp_mst_calc_pbn_mode_test *params = test->param_value;
59 KUNIT_EXPECT_EQ(test, drm_dp_calc_pbn_mode(params->clock, params->bpp << 4),
63 static void dp_mst_calc_pbn_mode_desc(const struct drm_dp_mst_calc_pbn_mode_test *t, char *desc)
65 sprintf(desc, "Clock %d BPP %d DSC %s", t->clock, t->bpp, t->dsc ? "enabled" : "disabled");
68 KUNIT_ARRAY_PARAM(drm_dp_mst_calc_pbn_mode, drm_dp_mst_calc_pbn_mode_cases,
69 dp_mst_calc_pbn_mode_desc);
71 struct drm_dp_mst_calc_pbn_div_test {
77 #define fp_init(__int, __frac) { \
78 .full = (__int) * (1 << 12) + \
79 (__frac) * (1 << 12) / 100000 \
82 static const struct drm_dp_mst_calc_pbn_div_test drm_dp_mst_calc_pbn_div_dp1_4_cases[] = {
84 * UHBR rates (DP Standard v2.1 2.7.6.3, specifying the rounded to
85 * closest value to 2 decimal places):
86 * .expected = .link_rate * .lane_count * 0.9671 / 8 / 54 / 100
87 * DP1.4 rates (DP Standard v2.1 2.6.4.2):
88 * .expected = .link_rate * .lane_count * 0.8000 / 8 / 54 / 100
90 * truncated to 5 decimal places.
95 .expected = fp_init(179, 9259), /* 179.09259 */
100 .expected = fp_init(89, 54629),
103 .link_rate = 2000000,
105 .expected = fp_init(44, 77314),
108 .link_rate = 1350000,
110 .expected = fp_init(120, 88750),
113 .link_rate = 1350000,
115 .expected = fp_init(60, 44375),
118 .link_rate = 1350000,
120 .expected = fp_init(30, 22187),
123 .link_rate = 1000000,
125 .expected = fp_init(89, 54629),
128 .link_rate = 1000000,
130 .expected = fp_init(44, 77314),
133 .link_rate = 1000000,
135 .expected = fp_init(22, 38657),
140 .expected = fp_init(60, 0),
145 .expected = fp_init(30, 0),
150 .expected = fp_init(15, 0),
155 .expected = fp_init(40, 0),
160 .expected = fp_init(20, 0),
165 .expected = fp_init(10, 0),
170 .expected = fp_init(20, 0),
175 .expected = fp_init(10, 0),
180 .expected = fp_init(5, 0),
185 .expected = fp_init(12, 0),
190 .expected = fp_init(6, 0),
195 .expected = fp_init(3, 0),
199 static void drm_test_dp_mst_calc_pbn_div(struct kunit *test)
201 const struct drm_dp_mst_calc_pbn_div_test *params = test->param_value;
203 KUNIT_EXPECT_EQ(test, drm_dp_get_vc_payload_bw(params->link_rate, params->lane_count).full,
204 params->expected.full);
207 static void dp_mst_calc_pbn_div_desc(const struct drm_dp_mst_calc_pbn_div_test *t, char *desc)
209 sprintf(desc, "Link rate %d lane count %d", t->link_rate, t->lane_count);
212 KUNIT_ARRAY_PARAM(drm_dp_mst_calc_pbn_div, drm_dp_mst_calc_pbn_div_dp1_4_cases,
213 dp_mst_calc_pbn_div_desc);
215 static u8 data[] = { 0xff, 0x00, 0xdd };
217 struct drm_dp_mst_sideband_msg_req_test {
219 const struct drm_dp_sideband_msg_req_body in;
222 static const struct drm_dp_mst_sideband_msg_req_test drm_dp_mst_sideband_msg_req_cases[] = {
224 .desc = "DP_ENUM_PATH_RESOURCES with port number",
226 .req_type = DP_ENUM_PATH_RESOURCES,
227 .u.port_num.port_number = 5,
231 .desc = "DP_POWER_UP_PHY with port number",
233 .req_type = DP_POWER_UP_PHY,
234 .u.port_num.port_number = 5,
238 .desc = "DP_POWER_DOWN_PHY with port number",
240 .req_type = DP_POWER_DOWN_PHY,
241 .u.port_num.port_number = 5,
245 .desc = "DP_ALLOCATE_PAYLOAD with SDP stream sinks",
247 .req_type = DP_ALLOCATE_PAYLOAD,
248 .u.allocate_payload.number_sdp_streams = 3,
249 .u.allocate_payload.sdp_stream_sink = { 1, 2, 3 },
253 .desc = "DP_ALLOCATE_PAYLOAD with port number",
255 .req_type = DP_ALLOCATE_PAYLOAD,
256 .u.allocate_payload.port_number = 0xf,
260 .desc = "DP_ALLOCATE_PAYLOAD with VCPI",
262 .req_type = DP_ALLOCATE_PAYLOAD,
263 .u.allocate_payload.vcpi = 0x7f,
267 .desc = "DP_ALLOCATE_PAYLOAD with PBN",
269 .req_type = DP_ALLOCATE_PAYLOAD,
270 .u.allocate_payload.pbn = U16_MAX,
274 .desc = "DP_QUERY_PAYLOAD with port number",
276 .req_type = DP_QUERY_PAYLOAD,
277 .u.query_payload.port_number = 0xf,
281 .desc = "DP_QUERY_PAYLOAD with VCPI",
283 .req_type = DP_QUERY_PAYLOAD,
284 .u.query_payload.vcpi = 0x7f,
288 .desc = "DP_REMOTE_DPCD_READ with port number",
290 .req_type = DP_REMOTE_DPCD_READ,
291 .u.dpcd_read.port_number = 0xf,
295 .desc = "DP_REMOTE_DPCD_READ with DPCD address",
297 .req_type = DP_REMOTE_DPCD_READ,
298 .u.dpcd_read.dpcd_address = 0xfedcb,
302 .desc = "DP_REMOTE_DPCD_READ with max number of bytes",
304 .req_type = DP_REMOTE_DPCD_READ,
305 .u.dpcd_read.num_bytes = U8_MAX,
309 .desc = "DP_REMOTE_DPCD_WRITE with port number",
311 .req_type = DP_REMOTE_DPCD_WRITE,
312 .u.dpcd_write.port_number = 0xf,
316 .desc = "DP_REMOTE_DPCD_WRITE with DPCD address",
318 .req_type = DP_REMOTE_DPCD_WRITE,
319 .u.dpcd_write.dpcd_address = 0xfedcb,
323 .desc = "DP_REMOTE_DPCD_WRITE with data array",
325 .req_type = DP_REMOTE_DPCD_WRITE,
326 .u.dpcd_write.num_bytes = ARRAY_SIZE(data),
327 .u.dpcd_write.bytes = data,
331 .desc = "DP_REMOTE_I2C_READ with port number",
333 .req_type = DP_REMOTE_I2C_READ,
334 .u.i2c_read.port_number = 0xf,
338 .desc = "DP_REMOTE_I2C_READ with I2C device ID",
340 .req_type = DP_REMOTE_I2C_READ,
341 .u.i2c_read.read_i2c_device_id = 0x7f,
345 .desc = "DP_REMOTE_I2C_READ with transactions array",
347 .req_type = DP_REMOTE_I2C_READ,
348 .u.i2c_read.num_transactions = 3,
349 .u.i2c_read.num_bytes_read = ARRAY_SIZE(data) * 3,
350 .u.i2c_read.transactions = {
351 { .bytes = data, .num_bytes = ARRAY_SIZE(data), .i2c_dev_id = 0x7f,
352 .i2c_transaction_delay = 0xf, },
353 { .bytes = data, .num_bytes = ARRAY_SIZE(data), .i2c_dev_id = 0x7e,
354 .i2c_transaction_delay = 0xe, },
355 { .bytes = data, .num_bytes = ARRAY_SIZE(data), .i2c_dev_id = 0x7d,
356 .i2c_transaction_delay = 0xd, },
361 .desc = "DP_REMOTE_I2C_WRITE with port number",
363 .req_type = DP_REMOTE_I2C_WRITE,
364 .u.i2c_write.port_number = 0xf,
368 .desc = "DP_REMOTE_I2C_WRITE with I2C device ID",
370 .req_type = DP_REMOTE_I2C_WRITE,
371 .u.i2c_write.write_i2c_device_id = 0x7f,
375 .desc = "DP_REMOTE_I2C_WRITE with data array",
377 .req_type = DP_REMOTE_I2C_WRITE,
378 .u.i2c_write.num_bytes = ARRAY_SIZE(data),
379 .u.i2c_write.bytes = data,
383 .desc = "DP_QUERY_STREAM_ENC_STATUS with stream ID",
385 .req_type = DP_QUERY_STREAM_ENC_STATUS,
386 .u.enc_status.stream_id = 1,
390 .desc = "DP_QUERY_STREAM_ENC_STATUS with client ID",
392 .req_type = DP_QUERY_STREAM_ENC_STATUS,
393 .u.enc_status.client_id = { 0x4f, 0x7f, 0xb4, 0x00, 0x8c, 0x0d, 0x67 },
397 .desc = "DP_QUERY_STREAM_ENC_STATUS with stream event",
399 .req_type = DP_QUERY_STREAM_ENC_STATUS,
400 .u.enc_status.stream_event = 3,
404 .desc = "DP_QUERY_STREAM_ENC_STATUS with valid stream event",
406 .req_type = DP_QUERY_STREAM_ENC_STATUS,
407 .u.enc_status.valid_stream_event = 0,
411 .desc = "DP_QUERY_STREAM_ENC_STATUS with stream behavior",
413 .req_type = DP_QUERY_STREAM_ENC_STATUS,
414 .u.enc_status.stream_behavior = 3,
418 .desc = "DP_QUERY_STREAM_ENC_STATUS with a valid stream behavior",
420 .req_type = DP_QUERY_STREAM_ENC_STATUS,
421 .u.enc_status.valid_stream_behavior = 1,
427 sideband_msg_req_equal(const struct drm_dp_sideband_msg_req_body *in,
428 const struct drm_dp_sideband_msg_req_body *out)
430 const struct drm_dp_remote_i2c_read_tx *txin, *txout;
433 if (in->req_type != out->req_type)
436 switch (in->req_type) {
438 * Compare struct members manually for request types which can't be
439 * compared simply using memcmp(). This is because said request types
440 * contain pointers to other allocated structs
442 case DP_REMOTE_I2C_READ:
443 #define IN in->u.i2c_read
444 #define OUT out->u.i2c_read
445 if (IN.num_bytes_read != OUT.num_bytes_read ||
446 IN.num_transactions != OUT.num_transactions ||
447 IN.port_number != OUT.port_number ||
448 IN.read_i2c_device_id != OUT.read_i2c_device_id)
451 for (i = 0; i < IN.num_transactions; i++) {
452 txin = &IN.transactions[i];
453 txout = &OUT.transactions[i];
455 if (txin->i2c_dev_id != txout->i2c_dev_id ||
456 txin->no_stop_bit != txout->no_stop_bit ||
457 txin->num_bytes != txout->num_bytes ||
458 txin->i2c_transaction_delay !=
459 txout->i2c_transaction_delay)
462 if (memcmp(txin->bytes, txout->bytes,
463 txin->num_bytes) != 0)
470 case DP_REMOTE_DPCD_WRITE:
471 #define IN in->u.dpcd_write
472 #define OUT out->u.dpcd_write
473 if (IN.dpcd_address != OUT.dpcd_address ||
474 IN.num_bytes != OUT.num_bytes ||
475 IN.port_number != OUT.port_number)
478 return memcmp(IN.bytes, OUT.bytes, IN.num_bytes) == 0;
482 case DP_REMOTE_I2C_WRITE:
483 #define IN in->u.i2c_write
484 #define OUT out->u.i2c_write
485 if (IN.port_number != OUT.port_number ||
486 IN.write_i2c_device_id != OUT.write_i2c_device_id ||
487 IN.num_bytes != OUT.num_bytes)
490 return memcmp(IN.bytes, OUT.bytes, IN.num_bytes) == 0;
495 return memcmp(in, out, sizeof(*in)) == 0;
501 static void drm_test_dp_mst_msg_printf(struct drm_printer *p, struct va_format *vaf)
503 struct kunit *test = p->arg;
505 kunit_err(test, "%pV", vaf);
508 static void drm_test_dp_mst_sideband_msg_req_decode(struct kunit *test)
510 const struct drm_dp_mst_sideband_msg_req_test *params = test->param_value;
511 const struct drm_dp_sideband_msg_req_body *in = ¶ms->in;
512 struct drm_dp_sideband_msg_req_body *out;
513 struct drm_dp_sideband_msg_tx *txmsg;
514 struct drm_printer p = {
515 .printfn = drm_test_dp_mst_msg_printf,
520 out = kunit_kzalloc(test, sizeof(*out), GFP_KERNEL);
521 KUNIT_ASSERT_NOT_NULL(test, out);
523 txmsg = kunit_kzalloc(test, sizeof(*txmsg), GFP_KERNEL);
524 KUNIT_ASSERT_NOT_NULL(test, txmsg);
526 drm_dp_encode_sideband_req(in, txmsg);
527 KUNIT_EXPECT_GE_MSG(test, drm_dp_decode_sideband_req(txmsg, out), 0,
528 "Failed to decode sideband request");
530 if (!sideband_msg_req_equal(in, out)) {
531 KUNIT_FAIL(test, "Encode/decode failed");
532 kunit_err(test, "Expected:");
533 drm_dp_dump_sideband_msg_req_body(in, 1, &p);
534 kunit_err(test, "Got:");
535 drm_dp_dump_sideband_msg_req_body(out, 1, &p);
538 switch (in->req_type) {
539 case DP_REMOTE_DPCD_WRITE:
540 kfree(out->u.dpcd_write.bytes);
542 case DP_REMOTE_I2C_READ:
543 for (i = 0; i < out->u.i2c_read.num_transactions; i++)
544 kfree(out->u.i2c_read.transactions[i].bytes);
546 case DP_REMOTE_I2C_WRITE:
547 kfree(out->u.i2c_write.bytes);
553 drm_dp_mst_sideband_msg_req_desc(const struct drm_dp_mst_sideband_msg_req_test *t, char *desc)
555 strcpy(desc, t->desc);
558 KUNIT_ARRAY_PARAM(drm_dp_mst_sideband_msg_req, drm_dp_mst_sideband_msg_req_cases,
559 drm_dp_mst_sideband_msg_req_desc);
561 static struct kunit_case drm_dp_mst_helper_tests[] = {
562 KUNIT_CASE_PARAM(drm_test_dp_mst_calc_pbn_mode, drm_dp_mst_calc_pbn_mode_gen_params),
563 KUNIT_CASE_PARAM(drm_test_dp_mst_calc_pbn_div, drm_dp_mst_calc_pbn_div_gen_params),
564 KUNIT_CASE_PARAM(drm_test_dp_mst_sideband_msg_req_decode,
565 drm_dp_mst_sideband_msg_req_gen_params),
569 static struct kunit_suite drm_dp_mst_helper_test_suite = {
570 .name = "drm_dp_mst_helper",
571 .test_cases = drm_dp_mst_helper_tests,
574 kunit_test_suite(drm_dp_mst_helper_test_suite);
576 MODULE_DESCRIPTION("Test cases for the DRM DP MST helpers");
577 MODULE_LICENSE("GPL");