summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_dmub_test.c
blob: ead43eeb38c773532f824fac747b23a0440c88f4 (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
// SPDX-License-Identifier: GPL-2.0 OR MIT
/*
 * KUnit tests for amdgpu_dm_dmub.c
 *
 * Copyright 2026 Advanced Micro Devices, Inc.
 */

#include <kunit/test.h>
#include <linux/firmware.h>

#include "dc.h"
#include "dc/inc/core_types.h"
#include "dc/inc/hw/dmcu.h"
#include "dc/inc/hw/abm.h"
#include "amdgpu_mode.h"
#include "amdgpu_dm.h"
#include "dm_services.h"
#include "dmub/dmub_srv.h"
#include "amdgpu_dm_dmub.h"

#define DM_TEST_FW_SIZE	512

/* Tests for dm_register_dmub_notify_callback() */

static void dummy_callback(struct amdgpu_device *adev,
			   struct dmub_notification *notify)
{
}

static bool dm_test_dmub_supported(struct dmub_srv *dmub)
{
	return true;
}

static bool dm_test_dmub_unsupported(struct dmub_srv *dmub)
{
	return false;
}

static bool dm_test_dmub_hw_initialized(struct dmub_srv *dmub)
{
	return true;
}

static union dmub_fw_boot_status dm_test_dmub_fw_ready(struct dmub_srv *dmub)
{
	union dmub_fw_boot_status status = { 0 };

	status.bits.dal_fw = 1;
	status.bits.mailbox_rdy = 1;
	return status;
}

static union dmub_fw_boot_status dm_test_dmub_fw_not_ready(struct dmub_srv *dmub)
{
	union dmub_fw_boot_status status = { 0 };

	return status;
}

static void dm_test_dmub_init_reg_offsets(struct dmub_srv *dmub,
					  struct dc_context *ctx)
{
}

static bool dm_test_dmcu_init(struct dmcu *dmcu)
{
	return true;
}

static bool dm_test_dmcu_is_initialized(struct dmcu *dmcu)
{
	return true;
}

static const struct dmcu_funcs dm_test_dmcu_funcs = {
	.dmcu_init = dm_test_dmcu_init,
	.is_dmcu_initialized = dm_test_dmcu_is_initialized,
};

static struct dmub_srv *dm_test_alloc_dmub_srv(struct kunit *test)
{
	struct dmub_srv *dmub_srv;

	dmub_srv = kunit_kzalloc(test, sizeof(*dmub_srv), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmub_srv);

	dmub_srv->sw_init = true;
	dmub_srv->hw_init = true;
	dmub_srv->power_state = DMUB_POWER_STATE_D0;
	dmub_srv->hw_funcs.is_supported = dm_test_dmub_supported;
	dmub_srv->hw_funcs.is_hw_init = dm_test_dmub_hw_initialized;
	dmub_srv->hw_funcs.get_fw_status = dm_test_dmub_fw_ready;
	dmub_srv->hw_funcs.init_reg_offsets = dm_test_dmub_init_reg_offsets;

	return dmub_srv;
}

static const struct firmware *dm_test_alloc_dmub_fw(struct kunit *test)
{
	struct dmcub_firmware_header_v1_0 *hdr;
	struct firmware *fw;
	u8 *data;

	fw = kunit_kzalloc(test, sizeof(*fw), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fw);

	data = kunit_kzalloc(test, DM_TEST_FW_SIZE, GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, data);

	hdr = (struct dmcub_firmware_header_v1_0 *)data;
	hdr->header.ucode_array_offset_bytes = cpu_to_le32(0);
	hdr->header.ucode_version = cpu_to_le32(DMUB_FW_VERSION(9, 9, 9));
	hdr->inst_const_bytes = cpu_to_le32(PSP_HEADER_BYTES_256);
	hdr->bss_data_bytes = cpu_to_le32(0);

	fw->size = DM_TEST_FW_SIZE;
	fw->data = data;

	return fw;
}

/**
 * dm_test_register_dmub_notify_callback_null_callback - Test null callback is rejected
 * @test: The KUnit test context
 */
static void dm_test_register_dmub_notify_callback_null_callback(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	KUNIT_EXPECT_FALSE(test, dm_register_dmub_notify_callback(adev,
		DMUB_NOTIFICATION_AUX_REPLY, NULL, false));
}

/**
 * dm_test_register_dmub_notify_callback_type_out_of_range - Test out-of-range type is rejected
 * @test: The KUnit test context
 */
static void dm_test_register_dmub_notify_callback_type_out_of_range(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	KUNIT_EXPECT_FALSE(test, dm_register_dmub_notify_callback(adev,
		AMDGPU_DMUB_NOTIFICATION_MAX, dummy_callback, false));
}

/**
 * dm_test_register_dmub_notify_callback_valid - Test Register dmub notify callback valid
 * @test: The KUnit test context
 */
static void dm_test_register_dmub_notify_callback_valid(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	KUNIT_EXPECT_TRUE(test, dm_register_dmub_notify_callback(adev,
		DMUB_NOTIFICATION_AUX_REPLY, dummy_callback, true));

	KUNIT_EXPECT_TRUE(test,
		adev->dm.dmub_callback[DMUB_NOTIFICATION_AUX_REPLY] == dummy_callback);
	KUNIT_EXPECT_TRUE(test,
		adev->dm.dmub_thread_offload[DMUB_NOTIFICATION_AUX_REPLY]);
}

/**
 * dm_test_register_dmub_notify_callback_offload_false - Test registration with offload disabled
 * @test: The KUnit test context
 */
static void dm_test_register_dmub_notify_callback_offload_false(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	KUNIT_EXPECT_TRUE(test, dm_register_dmub_notify_callback(adev,
		DMUB_NOTIFICATION_HPD, dummy_callback, false));

	KUNIT_EXPECT_TRUE(test,
		adev->dm.dmub_callback[DMUB_NOTIFICATION_HPD] == dummy_callback);
	KUNIT_EXPECT_FALSE(test,
		adev->dm.dmub_thread_offload[DMUB_NOTIFICATION_HPD]);
}

/* Tests for dm_dmub_aux_setconfig_callback() */

/**
 * dm_test_dmub_aux_setconfig_callback_copies_and_completes - Test copy and complete on AUX reply
 * @test: The KUnit test context
 */
static void dm_test_dmub_aux_setconfig_callback_copies_and_completes(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dmub_notification *dm_notify;
	struct dmub_notification notify = {};

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	dm_notify = kunit_kzalloc(test, sizeof(*dm_notify), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dm_notify);

	init_completion(&adev->dm.dmub_aux_transfer_done);
	adev->dm.dmub_notify = dm_notify;

	notify.type = DMUB_NOTIFICATION_AUX_REPLY;
	notify.result = AUX_RET_SUCCESS;
	notify.aux_reply.command = 0xA5;
	notify.aux_reply.length = 3;
	notify.aux_reply.data[0] = 0x11;
	notify.aux_reply.data[1] = 0x22;
	notify.aux_reply.data[2] = 0x33;

	dm_dmub_aux_setconfig_callback(adev, &notify);

	KUNIT_EXPECT_EQ(test, dm_notify->type, notify.type);
	KUNIT_EXPECT_EQ(test, dm_notify->result, notify.result);
	KUNIT_EXPECT_EQ(test, dm_notify->aux_reply.command, notify.aux_reply.command);
	KUNIT_EXPECT_EQ(test, dm_notify->aux_reply.length, notify.aux_reply.length);
	KUNIT_EXPECT_EQ(test, dm_notify->aux_reply.data[0], notify.aux_reply.data[0]);
	KUNIT_EXPECT_EQ(test, dm_notify->aux_reply.data[1], notify.aux_reply.data[1]);
	KUNIT_EXPECT_EQ(test, dm_notify->aux_reply.data[2], notify.aux_reply.data[2]);
	KUNIT_EXPECT_TRUE(test, completion_done(&adev->dm.dmub_aux_transfer_done));
}

/**
 * dm_test_dmub_aux_setconfig_callback_non_aux_no_complete - Test non-AUX type skips completion
 * @test: The KUnit test context
 */
static void dm_test_dmub_aux_setconfig_callback_non_aux_no_complete(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dmub_notification *dm_notify;
	struct dmub_notification notify = {};

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	dm_notify = kunit_kzalloc(test, sizeof(*dm_notify), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dm_notify);

	init_completion(&adev->dm.dmub_aux_transfer_done);
	adev->dm.dmub_notify = dm_notify;

	notify.type = DMUB_NOTIFICATION_HPD;
	notify.result = AUX_RET_ERROR_TIMEOUT;

	dm_dmub_aux_setconfig_callback(adev, &notify);

	KUNIT_EXPECT_EQ(test, dm_notify->type, notify.type);
	KUNIT_EXPECT_FALSE(test, completion_done(&adev->dm.dmub_aux_transfer_done));
}

/**
 * dm_test_dmub_aux_setconfig_callback_aux_with_null_dm_notify - Test AUX with NULL dm_notify
 * @test: The KUnit test context
 */
static void dm_test_dmub_aux_setconfig_callback_aux_with_null_dm_notify(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dmub_notification notify = {};

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	init_completion(&adev->dm.dmub_aux_transfer_done);
	adev->dm.dmub_notify = NULL;

	notify.type = DMUB_NOTIFICATION_AUX_REPLY;

	dm_dmub_aux_setconfig_callback(adev, &notify);

	KUNIT_EXPECT_TRUE(test, completion_done(&adev->dm.dmub_aux_transfer_done));
}

/**
 * dm_test_dmub_aux_setconfig_callback_set_config_reply - Test SET_CONFIG reply copies status
 * @test: The KUnit test context
 */
static void dm_test_dmub_aux_setconfig_callback_set_config_reply(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dmub_notification *dm_notify;
	struct dmub_notification notify = {};

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	dm_notify = kunit_kzalloc(test, sizeof(*dm_notify), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dm_notify);

	init_completion(&adev->dm.dmub_aux_transfer_done);
	adev->dm.dmub_notify = dm_notify;

	notify.type = DMUB_NOTIFICATION_SET_CONFIG_REPLY;
	notify.sc_status = SET_CONFIG_RX_TIMEOUT;

	dm_dmub_aux_setconfig_callback(adev, &notify);

	KUNIT_EXPECT_EQ(test, dm_notify->type, notify.type);
	KUNIT_EXPECT_EQ(test, dm_notify->sc_status, notify.sc_status);
	KUNIT_EXPECT_FALSE(test, completion_done(&adev->dm.dmub_aux_transfer_done));
}

/* Tests for dm_dmub_aux_fused_io_callback() */

/**
 * dm_test_dmub_aux_fused_io_callback_copies_reply_and_completes - Test copy and complete
 * @test: The KUnit test context
 */
static void dm_test_dmub_aux_fused_io_callback_copies_reply_and_completes(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dmub_notification notify = {};
	struct dmub_cmd_fused_request *reply;
	u32 reply_ddc_line;
	u32 notify_ddc_line;
	u32 reply_address;
	u32 notify_address;
	u32 reply_length;
	u32 notify_length;
	uint8_t ddc_line = 2;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	init_completion(&adev->dm.fused_io[ddc_line].replied);

	notify.fused_request.identifier = 0x34;
	notify.fused_request.status = FUSED_REQUEST_STATUS_SUCCESS;
	notify.fused_request.u.aux.ddc_line = ddc_line;
	notify.fused_request.u.aux.address = 0x50;
	notify.fused_request.u.aux.length = 4;

	dm_dmub_aux_fused_io_callback(adev, &notify);

	KUNIT_EXPECT_TRUE(test, completion_done(&adev->dm.fused_io[ddc_line].replied));

	reply = (struct dmub_cmd_fused_request *)adev->dm.fused_io[ddc_line].reply_data;
	reply_ddc_line = reply->u.aux.ddc_line;
	notify_ddc_line = notify.fused_request.u.aux.ddc_line;
	reply_address = reply->u.aux.address;
	notify_address = notify.fused_request.u.aux.address;
	reply_length = reply->u.aux.length;
	notify_length = notify.fused_request.u.aux.length;

	KUNIT_EXPECT_EQ(test, reply->identifier, notify.fused_request.identifier);
	KUNIT_EXPECT_EQ(test, reply->status, notify.fused_request.status);
	KUNIT_EXPECT_EQ(test, reply_ddc_line, notify_ddc_line);
	KUNIT_EXPECT_EQ(test, reply_address, notify_address);
	KUNIT_EXPECT_EQ(test, reply_length, notify_length);
}

/**
 * dm_test_dmub_aux_fused_io_callback_max_ddc_line - Test Dmub aux fused io callback max ddc line
 * @test: The KUnit test context
 */
static void dm_test_dmub_aux_fused_io_callback_max_ddc_line(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dmub_notification notify = {};
	struct dmub_cmd_fused_request *reply;
	u32 reply_ddc_line;
	u32 notify_ddc_line;
	uint8_t ddc_line;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	ddc_line = ARRAY_SIZE(adev->dm.fused_io) - 1;
	init_completion(&adev->dm.fused_io[ddc_line].replied);

	notify.fused_request.identifier = 0x56;
	notify.fused_request.status = FUSED_REQUEST_STATUS_SUCCESS;
	notify.fused_request.u.aux.ddc_line = ddc_line;
	notify.fused_request.u.aux.address = 0x50;
	notify.fused_request.u.aux.length = 1;

	dm_dmub_aux_fused_io_callback(adev, &notify);

	KUNIT_EXPECT_TRUE(test, completion_done(&adev->dm.fused_io[ddc_line].replied));

	reply = (struct dmub_cmd_fused_request *)adev->dm.fused_io[ddc_line].reply_data;
	reply_ddc_line = reply->u.aux.ddc_line;
	notify_ddc_line = notify.fused_request.u.aux.ddc_line;

	KUNIT_EXPECT_EQ(test, reply->identifier, notify.fused_request.identifier);
	KUNIT_EXPECT_EQ(test, reply_ddc_line, notify_ddc_line);
}

/**
 * dm_test_dmub_aux_fused_io_callback_null_args - Test the NULL-argument guard
 * @test: The KUnit test context
 *
 * Passing a NULL device triggers the defensive guard (an ASSERT that maps to
 * WARN_ON_ONCE in this build) and returns early without dereferencing the
 * arguments. The call must not crash.
 */
static void dm_test_dmub_aux_fused_io_callback_null_args(struct kunit *test)
{
	struct dmub_notification notify = {};

	/* Must not crash; guard hits ASSERT (WARN_ON_ONCE) and returns. */
	dm_dmub_aux_fused_io_callback(NULL, &notify);
}

/* Tests for dm_get_default_ips_mode() */

/**
 * dm_test_get_default_ips_mode_dcn35 - Test Get default ips mode dcn35
 * @test: The KUnit test context
 */
static void dm_test_get_default_ips_mode_dcn35(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 5, 0);

	KUNIT_EXPECT_EQ(test, dm_get_default_ips_mode(adev),
			DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF);
}

/**
 * dm_test_get_default_ips_mode_dcn351 - Test Get default ips mode dcn351
 * @test: The KUnit test context
 */
static void dm_test_get_default_ips_mode_dcn351(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 5, 1);

	KUNIT_EXPECT_EQ(test, dm_get_default_ips_mode(adev),
			DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF);
}

/**
 * dm_test_get_default_ips_mode_dcn36 - Test Get default ips mode dcn36
 * @test: The KUnit test context
 */
static void dm_test_get_default_ips_mode_dcn36(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 6, 0);

	KUNIT_EXPECT_EQ(test, dm_get_default_ips_mode(adev),
			DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF);
}

/**
 * dm_test_get_default_ips_mode_older_than_dcn35 - Test Get default ips mode older than dcn35
 * @test: The KUnit test context
 */
static void dm_test_get_default_ips_mode_older_than_dcn35(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 2, 0);

	KUNIT_EXPECT_EQ(test, dm_get_default_ips_mode(adev),
			DMUB_IPS_DISABLE_ALL);
}

/**
 * dm_test_get_default_ips_mode_newer_default - Test Get default ips mode newer default
 * @test: The KUnit test context
 */
static void dm_test_get_default_ips_mode_newer_default(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	/* DCN 4.0.1 is >= 3.5 but has no explicit case, returns ENABLE */
	adev->ip_versions[DCE_HWIP][0] = IP_VERSION(4, 0, 1);

	KUNIT_EXPECT_EQ(test, dm_get_default_ips_mode(adev),
			DMUB_IPS_ENABLE);
}

/* Tests for dm_dmub_hw_init() */

/*
 * Build an amdgpu_device with the minimal dc/res_pool pointers that
 * dm_dmub_hw_init() and dm_dmub_hw_resume() dereference before their
 * early-return checks.
 */
static struct amdgpu_device *dm_test_alloc_adev_with_dc(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc *dc;
	struct resource_pool *res_pool;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);

	res_pool = kunit_kzalloc(test, sizeof(*res_pool), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res_pool);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);

	dc->res_pool = res_pool;
	dc->ctx = ctx;
	ctx->dc = dc;
	ctx->driver_context = adev;
	adev->dm.dc = dc;

	return adev;
}

static struct amdgpu_device *dm_test_alloc_adev_with_dmub(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dmub_srv_fb_info *fb_info;
	int i;

	adev = dm_test_alloc_adev_with_dc(test);

	fb_info = kunit_kzalloc(test, sizeof(*fb_info), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fb_info);

	fb_info->num_fb = DMUB_WINDOW_TOTAL;
	for (i = 0; i < DMUB_WINDOW_TOTAL; i++) {
		fb_info->fb[i].size = PAGE_SIZE;
		fb_info->fb[i].cpu_addr = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL);
		KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fb_info->fb[i].cpu_addr);
	}

	adev->dm.dmub_srv = dm_test_alloc_dmub_srv(test);
	adev->dm.dmub_fb_info = fb_info;
	adev->dm.dmub_fw = dm_test_alloc_dmub_fw(test);
	adev->bios = kunit_kzalloc(test, 4, GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev->bios);
	adev->bios_size = 4;
	adev->dm.fw_inst_size = 0;

	return adev;
}

/**
 * dm_test_dmub_hw_init_no_dmub_srv - Test hw init returns 0 when DMUB unsupported
 * @test: The KUnit test context
 *
 * When adev->dm.dmub_srv is NULL the ASIC does not support DMUB and
 * dm_dmub_hw_init() should return 0 without touching the hardware.
 */
static void dm_test_dmub_hw_init_no_dmub_srv(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);

	adev->dm.dmub_srv = NULL;

	KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
}

/**
 * dm_test_dmub_hw_init_no_fb_info - Test hw init fails without framebuffer info
 * @test: The KUnit test context
 *
 * With a DMUB service present but no framebuffer info, dm_dmub_hw_init()
 * should return -EINVAL.
 */
static void dm_test_dmub_hw_init_no_fb_info(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);
	struct dmub_srv *dmub_srv;

	dmub_srv = kunit_kzalloc(test, sizeof(*dmub_srv), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmub_srv);

	adev->dm.dmub_srv = dmub_srv;
	adev->dm.dmub_fb_info = NULL;

	KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), -EINVAL);
}

/**
 * dm_test_dmub_hw_init_no_firmware - Test hw init fails without firmware
 * @test: The KUnit test context
 *
 * With a DMUB service and framebuffer info present but no firmware,
 * dm_dmub_hw_init() should return -EINVAL.
 */
static void dm_test_dmub_hw_init_no_firmware(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);
	struct dmub_srv *dmub_srv;
	struct dmub_srv_fb_info *fb_info;

	dmub_srv = kunit_kzalloc(test, sizeof(*dmub_srv), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmub_srv);

	fb_info = kunit_kzalloc(test, sizeof(*fb_info), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fb_info);

	adev->dm.dmub_srv = dmub_srv;
	adev->dm.dmub_fb_info = fb_info;
	adev->dm.dmub_fw = NULL;

	KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), -EINVAL);
}

/**
 * dm_test_dmub_hw_init_success_fake_dmub - Test hw init with a fake DMUB service
 * @test: The KUnit test context
 *
 * With fake DMUB callbacks and preallocated framebuffer windows, the init path
 * should reach DMUB service initialization without real register access.
 */
static void dm_test_dmub_hw_init_success_fake_dmub(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);

	KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
	KUNIT_EXPECT_TRUE(test, adev->dm.dmub_srv->hw_init);
	KUNIT_EXPECT_NOT_NULL(test, adev->dm.dc->ctx->dmub_srv);
}

/**
 * dm_test_dmub_hw_init_no_hw_support - Test hw init returns 0 when HW is unsupported
 * @test: The KUnit test context
 *
 * When the DMUB service reports no hardware support, dm_dmub_hw_init() should
 * log and return 0 without initializing the DMUB hardware.
 */
static void dm_test_dmub_hw_init_no_hw_support(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);

	adev->dm.dmub_srv->hw_funcs.is_supported = dm_test_dmub_unsupported;

	KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
	KUNIT_EXPECT_NULL(test, adev->dm.dc->ctx->dmub_srv);
}

/**
 * dm_test_dmub_hw_init_bss_data - Test hw init copies BSS data into FB memory
 * @test: The KUnit test context
 *
 * When the DMUB firmware declares a non-zero BSS data size, dm_dmub_hw_init()
 * should copy that region into the BSS framebuffer window.
 */
static void dm_test_dmub_hw_init_bss_data(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
	struct dmcub_firmware_header_v1_0 *hdr;

	hdr = (struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data;
	hdr->bss_data_bytes = cpu_to_le32(16);

	KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
	KUNIT_EXPECT_TRUE(test, adev->dm.dmub_srv->hw_init);
}

/**
 * dm_test_dmub_hw_init_hw_init_fails - Test hw init returns -EINVAL on DMUB init failure
 * @test: The KUnit test context
 *
 * A framebuffer-info window count below the required total makes
 * dmub_srv_hw_init() reject the request, so dm_dmub_hw_init() logs and
 * returns -EINVAL. (The rejection path emits a one-time WARN via ASSERT.)
 */
static void dm_test_dmub_hw_init_hw_init_fails(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);

	adev->dm.dmub_fb_info->num_fb = 0;

	KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), -EINVAL);
}

/**
 * dm_test_dmub_hw_init_auto_load_timeout - Test hw init tolerates an auto-load timeout
 * @test: The KUnit test context
 *
 * When the DMUB firmware never reports ready, dmub_srv_wait_for_auto_load()
 * times out; dm_dmub_hw_init() only warns and still completes successfully.
 */
static void dm_test_dmub_hw_init_auto_load_timeout(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);

	adev->dm.dmub_srv->hw_funcs.get_fw_status = dm_test_dmub_fw_not_ready;

	KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
	KUNIT_EXPECT_NOT_NULL(test, adev->dm.dc->ctx->dmub_srv);
}

/**
 * dm_test_dmub_hw_init_apu_dpia_dcn35 - Test hw init APU DPIA and DCN35 hw params
 * @test: The KUnit test context
 *
 * On a DCN3.5 APU with a USB4 DPIA link, dm_dmub_hw_init() should populate the
 * DPIA hw params and the DCN3.5 IPS-sequential hw params before initializing
 * the fake DMUB service.
 */
static void dm_test_dmub_hw_init_apu_dpia_dcn35(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);

	adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 5, 0);
	adev->dm.dc->caps.is_apu = true;
	adev->dm.dc->res_pool->usb4_dpia_count = 1;

	KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
	KUNIT_EXPECT_TRUE(test, adev->dm.dmub_srv->hw_init);
}

/**
 * dm_test_dmub_hw_init_sanity_checks_dcn31 - Test hw init enables DCN31 sanity checks
 * @test: The KUnit test context
 *
 * On DCN3.1.2 with a DMCUB firmware version in the affected range,
 * dm_dmub_hw_init() should enable the DC sanity-check debug flag.
 */
static void dm_test_dmub_hw_init_sanity_checks_dcn31(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);

	adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 1, 2);
	adev->dm.dmcub_fw_version = DMUB_FW_VERSION(4, 0, 10);

	KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
	KUNIT_EXPECT_TRUE(test, adev->dm.dc->debug.sanity_checks);
}

/**
 * dm_test_dmub_hw_init_sanity_checks_dcn314 - Test hw init enables DCN314 sanity checks
 * @test: The KUnit test context
 *
 * On DCN3.1.4 with a DMCUB firmware version in the affected range,
 * dm_dmub_hw_init() should enable the DC sanity-check debug flag.
 */
static void dm_test_dmub_hw_init_sanity_checks_dcn314(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);

	adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 1, 4);
	adev->dm.dmcub_fw_version = DMUB_FW_VERSION(4, 0, 10);

	KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
	KUNIT_EXPECT_TRUE(test, adev->dm.dc->debug.sanity_checks);
}

/**
 * dm_test_dmub_hw_init_dmcu_abm - Test hw init initializes DMCU and ABM when present
 * @test: The KUnit test context
 *
 * When the resource pool exposes a DMCU and ABM, dm_dmub_hw_init() should
 * program the PSP version, invoke the DMCU init callback, and record the
 * running state reported by the DMCU.
 */
static void dm_test_dmub_hw_init_dmcu_abm(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
	struct dmcu *dmcu;
	struct abm *abm;

	dmcu = kunit_kzalloc(test, sizeof(*dmcu), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmcu);

	abm = kunit_kzalloc(test, sizeof(*abm), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, abm);

	dmcu->funcs = &dm_test_dmcu_funcs;
	dmcu->psp_version = 0x12345678;
	adev->dm.dc->res_pool->dmcu = dmcu;
	adev->dm.dc->res_pool->abm = abm;

	KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
	KUNIT_EXPECT_TRUE(test, abm->dmcu_is_running);
}

/* Tests for dm_dmub_hw_resume() */

/**
 * dm_test_dmub_hw_resume_no_dmub_srv - Test hw resume is a no-op when DMUB unsupported
 * @test: The KUnit test context
 *
 * When adev->dm.dmub_srv is NULL, dm_dmub_hw_resume() should return early
 * without dereferencing the (absent) DMUB service.
 */
static void dm_test_dmub_hw_resume_no_dmub_srv(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);

	adev->dm.dmub_srv = NULL;

	/* Must not crash. */
	dm_dmub_hw_resume(adev);
}

/**
 * dm_test_dmub_hw_resume_initialized_dmub - Test resume waits for initialized DMUB
 * @test: The KUnit test context
 *
 * When the fake DMUB service reports hardware already initialized, resume
 * should only wait for firmware readiness and skip full reinitialization.
 */
static void dm_test_dmub_hw_resume_initialized_dmub(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);

	adev->dm.dmub_srv = dm_test_alloc_dmub_srv(test);

	/* Must not crash. */
	dm_dmub_hw_resume(adev);
}

/**
 * dm_test_dmub_hw_resume_full_init - Test resume performs full init when uninitialized
 * @test: The KUnit test context
 *
 * When the fake DMUB service reports hardware not yet initialized, resume
 * should continue into a full dm_dmub_hw_init() and create the DC DMUB
 * server.
 */
static void dm_test_dmub_hw_resume_full_init(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);

	adev->dm.dmub_srv->hw_init = false;

	dm_dmub_hw_resume(adev);

	KUNIT_EXPECT_NOT_NULL(test, adev->dm.dc->ctx->dmub_srv);
}

/**
 * dm_test_dmub_hw_resume_init_check_failed - Test resume handles a failed init check
 * @test: The KUnit test context
 *
 * When the DMUB service is not software-initialized, the init-state query
 * fails and resume continues into dm_dmub_hw_init(), which also fails; the
 * call must warn and return without crashing.
 */
static void dm_test_dmub_hw_resume_init_check_failed(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);

	adev->dm.dmub_srv->sw_init = false;

	/* Must not crash. */
	dm_dmub_hw_resume(adev);
}

/**
 * dm_test_dmub_hw_resume_auto_load_timeout - Test resume tolerates an auto-load timeout
 * @test: The KUnit test context
 *
 * When the DMUB reports hardware already initialized but the firmware never
 * signals ready, resume's auto-load wait times out and only warns; the call
 * must not crash.
 */
static void dm_test_dmub_hw_resume_auto_load_timeout(struct kunit *test)
{
	struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);

	adev->dm.dmub_srv = dm_test_alloc_dmub_srv(test);
	adev->dm.dmub_srv->hw_funcs.get_fw_status = dm_test_dmub_fw_not_ready;

	/* Must not crash; auto-load times out and only warns. */
	dm_dmub_hw_resume(adev);
}

/* Tests for dm_dmub_sw_init() */

/**
 * dm_test_dmub_sw_init_unsupported_asic - Test sw init returns 0 for unsupported ASIC
 * @test: The KUnit test context
 *
 * For an IP version with no DMUB support, dm_dmub_sw_init() should return 0
 * before attempting to access the firmware.
 */
static void dm_test_dmub_sw_init_unsupported_asic(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	adev->ip_versions[DCE_HWIP][0] = IP_VERSION(1, 0, 0);

	KUNIT_EXPECT_EQ(test, dm_dmub_sw_init(adev), 0);
}

/* Tests for dm_init_microcode() */

/**
 * dm_test_init_microcode_unsupported_asic - Test microcode init returns 0 for unsupported ASIC
 * @test: The KUnit test context
 *
 * For an IP version with no DMUB support, dm_init_microcode() should return 0
 * without requesting any firmware.
 */
static void dm_test_init_microcode_unsupported_asic(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	adev->ip_versions[DCE_HWIP][0] = IP_VERSION(1, 0, 0);

	KUNIT_EXPECT_EQ(test, dm_init_microcode(adev), 0);
}

/* Tests for dm_dmub_get_vbios_bounding_box() */

/**
 * dm_test_dmub_get_vbios_bounding_box_default_null - Test default IP version returns NULL
 * @test: The KUnit test context
 *
 * For an IP version without a bounding-box size mapping, the switch falls
 * through to the default case and dm_dmub_get_vbios_bounding_box() returns
 * NULL without allocating GPU memory or issuing GPINT commands.
 */
static void dm_test_dmub_get_vbios_bounding_box_default_null(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 5, 0);

	KUNIT_EXPECT_NULL(test, dm_dmub_get_vbios_bounding_box(adev));
}

/* Tests for dm_execute_dmub_cmd() */

/**
 * dm_test_execute_dmub_cmd_null_dmub_srv - Test command execution fails without DMUB service
 * @test: The KUnit test context
 *
 * With no DC DMUB service on the context, dc_dmub_srv_cmd_run() returns false
 * and dm_execute_dmub_cmd() propagates that failure.
 */
static void dm_test_execute_dmub_cmd_null_dmub_srv(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	union dmub_rb_cmd *cmd;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);

	cmd = kunit_kzalloc(test, sizeof(*cmd), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cmd);

	spin_lock_init(&adev->dm.dmub_lock);
	ctx->driver_context = adev;
	ctx->dmub_srv = NULL;

	KUNIT_EXPECT_FALSE(test,
			   dm_execute_dmub_cmd(ctx, cmd, DM_DMUB_WAIT_TYPE_NO_WAIT));
}

/* Tests for amdgpu_dm_process_dmub_aux_transfer_sync() */

/**
 * dm_test_process_dmub_aux_transfer_sync_engine_acquire - Test AUX transfer engine-acquire failure
 * @test: The KUnit test context
 *
 * With dc->link_count == 0, dc_process_dmub_aux_transfer_async() rejects the
 * link index and amdgpu_dm_process_dmub_aux_transfer_sync() reports an
 * engine-acquire error and returns -1.
 */
static void dm_test_process_dmub_aux_transfer_sync_engine_acquire(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc *dc;
	struct aux_payload *payload;
	struct dmub_notification *notify;
	enum aux_return_code_type result = AUX_RET_SUCCESS;
	int ret;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);

	dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);

	payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, payload);

	notify = kunit_kzalloc(test, sizeof(*notify), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, notify);

	dc->link_count = 0;
	ctx->dc = dc;
	ctx->driver_context = adev;
	adev->dm.dmub_notify = notify;
	mutex_init(&adev->dm.dpia_aux_lock);
	init_completion(&adev->dm.dmub_aux_transfer_done);

	ret = amdgpu_dm_process_dmub_aux_transfer_sync(ctx, 0, payload, &result);

	KUNIT_EXPECT_EQ(test, ret, -1);
	KUNIT_EXPECT_EQ(test, result, AUX_RET_ERROR_ENGINE_ACQUIRE);
}

/**
 * dm_test_process_dmub_aux_transfer_sync_protocol_error - Test AUX protocol error result
 * @test: The KUnit test context
 *
 * With the completion pre-signaled and a fake DC DMUB service that rejects the
 * command after construction, the sync helper should propagate the notification
 * result without waiting for real firmware.
 */
static void dm_test_process_dmub_aux_transfer_sync_protocol_error(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc_context *dc_ctx;
	struct dc *dc;
	struct dc_link *link;
	struct ddc_service *ddc;
	struct aux_payload *payload;
	struct dmub_notification *notify;
	enum aux_return_code_type result = AUX_RET_SUCCESS;
	int ret;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);

	dc_ctx = kunit_kzalloc(test, sizeof(*dc_ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc_ctx);

	dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, link);

	ddc = kunit_kzalloc(test, sizeof(*ddc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ddc);

	payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, payload);

	notify = kunit_kzalloc(test, sizeof(*notify), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, notify);

	link->ddc = ddc;
	dc->ctx = dc_ctx;
	dc->link_count = 1;
	dc->links[0] = link;
	dc_ctx->dc = dc;
	dc_ctx->driver_context = adev;
	dc_ctx->dmub_srv = NULL;
	ctx->dc = dc;
	ctx->driver_context = adev;
	spin_lock_init(&adev->dm.dmub_lock);
	adev->dm.dmub_notify = notify;
	mutex_init(&adev->dm.dpia_aux_lock);
	init_completion(&adev->dm.dmub_aux_transfer_done);
	complete(&adev->dm.dmub_aux_transfer_done);
	notify->result = AUX_RET_ERROR_PROTOCOL_ERROR;

	ret = amdgpu_dm_process_dmub_aux_transfer_sync(ctx, 0, payload, &result);

	KUNIT_EXPECT_EQ(test, ret, -1);
	KUNIT_EXPECT_EQ(test, result, AUX_RET_ERROR_PROTOCOL_ERROR);
}

/**
 * dm_test_process_dmub_aux_transfer_sync_copies_data - Test AUX reply data copy
 * @test: The KUnit test context
 *
 * On a successful notification, the sync helper should copy the bounded reply
 * data and report the high-nibble command reply when present.
 */
static void dm_test_process_dmub_aux_transfer_sync_copies_data(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc_context *dc_ctx;
	struct dc *dc;
	struct dc_link *link;
	struct ddc_service *ddc;
	struct aux_payload *payload;
	struct dmub_notification *notify;
	enum aux_return_code_type result = AUX_RET_ERROR_UNKNOWN;
	u8 data[4] = { 0 };
	u8 reply = 0;
	int ret;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);

	dc_ctx = kunit_kzalloc(test, sizeof(*dc_ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc_ctx);

	dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, link);

	ddc = kunit_kzalloc(test, sizeof(*ddc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ddc);

	payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, payload);

	notify = kunit_kzalloc(test, sizeof(*notify), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, notify);

	link->ddc = ddc;
	dc->ctx = dc_ctx;
	dc->link_count = 1;
	dc->links[0] = link;
	dc_ctx->dc = dc;
	dc_ctx->driver_context = adev;
	dc_ctx->dmub_srv = NULL;
	ctx->dc = dc;
	ctx->driver_context = adev;
	spin_lock_init(&adev->dm.dmub_lock);
	adev->dm.dmub_notify = notify;
	mutex_init(&adev->dm.dpia_aux_lock);
	init_completion(&adev->dm.dmub_aux_transfer_done);
	complete(&adev->dm.dmub_aux_transfer_done);
	payload->data = data;
	payload->reply = &reply;
	payload->length = sizeof(data);
	notify->result = AUX_RET_SUCCESS;
	notify->aux_reply.command = 0xA4;
	notify->aux_reply.length = 3;
	notify->aux_reply.data[0] = 0x11;
	notify->aux_reply.data[1] = 0x22;
	notify->aux_reply.data[2] = 0x33;

	ret = amdgpu_dm_process_dmub_aux_transfer_sync(ctx, 0, payload, &result);

	KUNIT_EXPECT_EQ(test, ret, 3);
	KUNIT_EXPECT_EQ(test, result, AUX_RET_SUCCESS);
	KUNIT_EXPECT_EQ(test, reply, 0xA);
	KUNIT_EXPECT_EQ(test, data[0], 0x11);
	KUNIT_EXPECT_EQ(test, data[1], 0x22);
	KUNIT_EXPECT_EQ(test, data[2], 0x33);
}

/**
 * dm_test_process_dmub_aux_transfer_sync_zero_length - Test AUX reply with no data
 * @test: The KUnit test context
 *
 * On a successful notification whose reply carries no data, the sync helper
 * takes the zero-length branch and returns the reply length (0) without
 * copying any payload data.
 */
static void dm_test_process_dmub_aux_transfer_sync_zero_length(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc_context *dc_ctx;
	struct dc *dc;
	struct dc_link *link;
	struct ddc_service *ddc;
	struct aux_payload *payload;
	struct dmub_notification *notify;
	enum aux_return_code_type result = AUX_RET_ERROR_UNKNOWN;
	u8 data[4] = { 0 };
	u8 reply = 0;
	int ret;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);

	dc_ctx = kunit_kzalloc(test, sizeof(*dc_ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc_ctx);

	dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, link);

	ddc = kunit_kzalloc(test, sizeof(*ddc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ddc);

	payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, payload);

	notify = kunit_kzalloc(test, sizeof(*notify), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, notify);

	link->ddc = ddc;
	dc->ctx = dc_ctx;
	dc->link_count = 1;
	dc->links[0] = link;
	dc_ctx->dc = dc;
	dc_ctx->driver_context = adev;
	dc_ctx->dmub_srv = NULL;
	ctx->dc = dc;
	ctx->driver_context = adev;
	spin_lock_init(&adev->dm.dmub_lock);
	adev->dm.dmub_notify = notify;
	mutex_init(&adev->dm.dpia_aux_lock);
	init_completion(&adev->dm.dmub_aux_transfer_done);
	complete(&adev->dm.dmub_aux_transfer_done);
	payload->data = data;
	payload->reply = &reply;
	payload->length = sizeof(data);
	notify->result = AUX_RET_SUCCESS;
	notify->aux_reply.command = 0x03;
	notify->aux_reply.length = 0;

	ret = amdgpu_dm_process_dmub_aux_transfer_sync(ctx, 0, payload, &result);

	KUNIT_EXPECT_EQ(test, ret, 0);
	KUNIT_EXPECT_EQ(test, result, AUX_RET_SUCCESS);
	KUNIT_EXPECT_EQ(test, reply, 0x3);
}

/* Tests for amdgpu_dm_process_dmub_set_config_sync() */

/**
 * dm_test_process_dmub_set_config_sync_unknown_error - Test SET_CONFIG completes with unknown error
 * @test: The KUnit test context
 *
 * With no DC DMUB service, dc_process_dmub_set_config_async() cannot reach the
 * firmware and reports the command as completed with SET_CONFIG_UNKNOWN_ERROR,
 * so amdgpu_dm_process_dmub_set_config_sync() returns 0 with that status.
 */
static void dm_test_process_dmub_set_config_sync_unknown_error(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc_context *dc_ctx;
	struct dc *dc;
	struct dc_link *link;
	struct set_config_cmd_payload *payload;
	struct dmub_notification *notify;
	enum set_config_status result = SET_CONFIG_PENDING;
	int ret;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);

	dc_ctx = kunit_kzalloc(test, sizeof(*dc_ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc_ctx);

	dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, link);

	payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, payload);

	notify = kunit_kzalloc(test, sizeof(*notify), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, notify);

	dc->ctx = dc_ctx;
	dc_ctx->dmub_srv = NULL;
	dc->links[0] = link;
	ctx->dc = dc;
	ctx->driver_context = adev;
	adev->dm.dmub_notify = notify;
	mutex_init(&adev->dm.dpia_aux_lock);
	init_completion(&adev->dm.dmub_aux_transfer_done);

	ret = amdgpu_dm_process_dmub_set_config_sync(ctx, 0, payload, &result);

	KUNIT_EXPECT_EQ(test, ret, 0);
	KUNIT_EXPECT_EQ(test, result, SET_CONFIG_UNKNOWN_ERROR);
}

/* Tests for abort_fused_io() */

/**
 * dm_test_abort_fused_io_no_dmub_srv - Test fused IO abort is a safe no-op without DMUB service
 * @test: The KUnit test context
 *
 * abort_fused_io() builds an abort command and submits it via
 * dm_execute_dmub_cmd(); with no DC DMUB service the submission fails
 * silently and the call must not crash.
 */
static void dm_test_abort_fused_io_no_dmub_srv(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dmub_cmd_fused_request *req;

	adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);

	req = kunit_kzalloc(test, sizeof(*req), GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, req);

	spin_lock_init(&adev->dm.dmub_lock);
	ctx->driver_context = adev;
	ctx->dmub_srv = NULL;

	/* Must not crash. */
	abort_fused_io(ctx, req);
}

static struct kunit_case amdgpu_dm_dmub_tests[] = {
	/* dm_register_dmub_notify_callback() */
	KUNIT_CASE(dm_test_register_dmub_notify_callback_null_callback),
	KUNIT_CASE(dm_test_register_dmub_notify_callback_type_out_of_range),
	KUNIT_CASE(dm_test_register_dmub_notify_callback_valid),
	KUNIT_CASE(dm_test_register_dmub_notify_callback_offload_false),
	/* dm_dmub_aux_setconfig_callback() */
	KUNIT_CASE(dm_test_dmub_aux_setconfig_callback_copies_and_completes),
	KUNIT_CASE(dm_test_dmub_aux_setconfig_callback_non_aux_no_complete),
	KUNIT_CASE(dm_test_dmub_aux_setconfig_callback_aux_with_null_dm_notify),
	KUNIT_CASE(dm_test_dmub_aux_setconfig_callback_set_config_reply),
	/* dm_dmub_aux_fused_io_callback() */
	KUNIT_CASE(dm_test_dmub_aux_fused_io_callback_copies_reply_and_completes),
	KUNIT_CASE(dm_test_dmub_aux_fused_io_callback_max_ddc_line),
	KUNIT_CASE(dm_test_dmub_aux_fused_io_callback_null_args),
	/* dm_get_default_ips_mode() */
	KUNIT_CASE(dm_test_get_default_ips_mode_dcn35),
	KUNIT_CASE(dm_test_get_default_ips_mode_dcn351),
	KUNIT_CASE(dm_test_get_default_ips_mode_dcn36),
	KUNIT_CASE(dm_test_get_default_ips_mode_older_than_dcn35),
	KUNIT_CASE(dm_test_get_default_ips_mode_newer_default),
	/* dm_dmub_hw_init() */
	KUNIT_CASE(dm_test_dmub_hw_init_no_dmub_srv),
	KUNIT_CASE(dm_test_dmub_hw_init_no_fb_info),
	KUNIT_CASE(dm_test_dmub_hw_init_no_firmware),
	KUNIT_CASE(dm_test_dmub_hw_init_success_fake_dmub),
	KUNIT_CASE(dm_test_dmub_hw_init_no_hw_support),
	KUNIT_CASE(dm_test_dmub_hw_init_bss_data),
	KUNIT_CASE(dm_test_dmub_hw_init_hw_init_fails),
	KUNIT_CASE(dm_test_dmub_hw_init_auto_load_timeout),
	KUNIT_CASE(dm_test_dmub_hw_init_apu_dpia_dcn35),
	KUNIT_CASE(dm_test_dmub_hw_init_sanity_checks_dcn31),
	KUNIT_CASE(dm_test_dmub_hw_init_sanity_checks_dcn314),
	KUNIT_CASE(dm_test_dmub_hw_init_dmcu_abm),
	/* dm_dmub_hw_resume() */
	KUNIT_CASE(dm_test_dmub_hw_resume_no_dmub_srv),
	KUNIT_CASE(dm_test_dmub_hw_resume_initialized_dmub),
	KUNIT_CASE(dm_test_dmub_hw_resume_full_init),
	KUNIT_CASE(dm_test_dmub_hw_resume_init_check_failed),
	KUNIT_CASE(dm_test_dmub_hw_resume_auto_load_timeout),
	/* dm_dmub_sw_init() */
	KUNIT_CASE(dm_test_dmub_sw_init_unsupported_asic),
	/* dm_init_microcode() */
	KUNIT_CASE(dm_test_init_microcode_unsupported_asic),
	/* dm_dmub_get_vbios_bounding_box() */
	KUNIT_CASE(dm_test_dmub_get_vbios_bounding_box_default_null),
	/* dm_execute_dmub_cmd() */
	KUNIT_CASE(dm_test_execute_dmub_cmd_null_dmub_srv),
	/* amdgpu_dm_process_dmub_aux_transfer_sync() */
	KUNIT_CASE(dm_test_process_dmub_aux_transfer_sync_engine_acquire),
	KUNIT_CASE(dm_test_process_dmub_aux_transfer_sync_protocol_error),
	KUNIT_CASE(dm_test_process_dmub_aux_transfer_sync_copies_data),
	KUNIT_CASE(dm_test_process_dmub_aux_transfer_sync_zero_length),
	/* amdgpu_dm_process_dmub_set_config_sync() */
	KUNIT_CASE(dm_test_process_dmub_set_config_sync_unknown_error),
	/* abort_fused_io() */
	KUNIT_CASE(dm_test_abort_fused_io_no_dmub_srv),
	{}
};

static struct kunit_suite amdgpu_dm_dmub_test_suite = {
	.name = "amdgpu_dm_dmub",
	.test_cases = amdgpu_dm_dmub_tests,
};

kunit_test_suite(amdgpu_dm_dmub_test_suite);

MODULE_AUTHOR("AMD");
MODULE_DESCRIPTION("KUnit tests for amdgpu_dm_dmub");
MODULE_LICENSE("Dual MIT/GPL");