summaryrefstree
path: root/kernel/drivers/vision/npu/npu-golden.c
blob: 4d44c822496e85d7e29804d558ab34cac5bd2ad4 (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
/*
 * Samsung Exynos SoC series NPU driver
 *
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *              http://www.samsung.com/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/file.h>
#include <linux/list.h>
#include <linux/uaccess.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <asm/cacheflush.h>
#include "ncp_header.h"
#include "npu-golden.h"
#include "npu-log.h"
#include "npu-debug.h"
#include "npu-util-comparator.h"
#include "npu-common.h"
#include "npu-session.h"

#define GOLDEN_FILENAME_LEN	2048

typedef enum {
	NPU_GOLDEN_STATE_NOT_INITIALIZED,
	NPU_GOLDEN_STATE_OPENED,
	NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES,
	NPU_GOLDEN_STATE_CONFIGURED,
	NPU_GOLDEN_STATE_COMPARING,
	NPU_GOLDEN_STATE_INVALID,
} npu_golden_state_e;

static const char *state_names[NPU_GOLDEN_STATE_INVALID + 1]
	= {"NOT INITIALZED", "OPENED", "ADDING_GOLDEN_FILES", "CONFIGURED", "COMPARING"};

static const u8 transition_map[][STATE_KEEPER_MAX_STATE] = {
/* From - To			NOT INITIALZED	OPENED	ADDING_GOLDEN_FILES	CONFIGURED	COMPARING	INVALID */
/* NOT INITIALZED      */  {	1,		1,	0,			0,		0,		0},
/* OPENED              */  {	1,		0,	1,			0,		0,		0},
/* ADDING_GOLDEN_FILES */  {	1,		0,	1,			1,		0,		0},
/* CONFIGURED          */  {	1,		0,	0,			0,		1,		0},
/* COMPARING           */  {	1,		0,	0,			1,		1,		0},
/* INVALID             */  {	0,		0,	0,			0,		0,		0},
};

/*
 * Trailing message to notify the result is truncated
 * due to the limitation of buffer size
 */
static const char *TRUNCATE_MSG = "\nWarning:Result is truncated.\n";

struct golden_file {
	u32	av_index;
	size_t	size;
	struct  list_head	list;
	u8 *img_vbuf;
	char	file_name[GOLDEN_FILENAME_LEN+1];
};

struct parse_buf {
	size_t	idx;
	int	(*value_setter)(struct parse_buf *parse_buf);
	char	token_char[GOLDEN_FILENAME_LEN+1];
};

typedef int (*action_func)(const char ch, struct parse_buf *parse_buf);

typedef enum {
	TOKEN_ALPHA,	/* Alphabet and some special character */
	TOKEN_NUMBER,	/* Number */
	TOKEN_WS,	/* White space */
	TOKEN_NL,	/* New line */
	TOKEN_SHARP,	/* '#' sign */
	TOKEN_INVALID,
} npu_golden_set_token;

typedef enum {
	PS_BEGIN = 0,	/* Initial */
	PS_HDR,		/* Header directive */
	PS_HDR_SEP,
	PS_HDR_PARAM,
	PS_COMMENT,
	PS_GOLDEN_IDX,
	PS_GOLDEN_SEP,
	PS_GOLDEN_FILE,
	PS_INVALID,
} npu_golden_set_parsing_state;

struct npu_golden_data {
	struct {
		u8	frame_id_matching;
		u8	net_id_matching;
		u8	valid;
		u8	updated;
	} flags;
	struct {
		u32	frame_id;
		u32	net_id;
	} match;
	golden_comparator		comparator;
	golden_is_av_valid		is_av_valid;

	npu_golden_set_parsing_state	parse_state;
	struct parse_buf		parse_buf;
	struct {
		u32			line;
		u32			col;
	} parse_pos;

	struct list_head	file_list;
};

struct npu_golden_ctx {
	/* Flag to check golden is available or not, without acquiring lock */
	atomic_t				loaded;

	struct npu_statekeeper			statekeeper;
	struct mutex				lock;
	struct npu_golden_data			data;
	struct npu_golden_compare_result	result;
	struct device *dev;
};

struct parsing_action {
	npu_golden_set_parsing_state	next_state;
	action_func			func;
};

/* Comparator function configuration map */
struct {
	const char *name;
	golden_comparator	comparator;
	golden_is_av_valid	is_av_valid;
} COMPARATOR_TABLE[] = {
	{	/* SIMPLE: Default */
		.name = "SIMPLE",
		.comparator = simple_compare,
		.is_av_valid = is_av_valid_simple,
	},
	{	/* REORDERED */
		.name = "REORDERED",
		.comparator = reordered_compare,
		.is_av_valid = is_av_valid_reordered,
	},
	{	/* Terminator */
		.name = NULL,
		.comparator = NULL,
		.is_av_valid = NULL,
	}
};

/* Global storage for golden reference data */
static struct npu_golden_ctx	npu_golden_ctx;

/*********************************************************
 * Golden file structure manipulation
 */
static struct golden_file *new_golden_file(void)
{
	struct golden_file *obj;

	BUG_ON(!npu_golden_ctx.dev);

	obj = devm_kzalloc(npu_golden_ctx.dev, sizeof(*obj), GFP_KERNEL);
	if (obj == NULL) {
		npu_err("fail in golden_file memory allocattion");
		return NULL;
	}
	/* new golden_file object is zero-cleared, no memset required */
	return obj;
}

static void destroy_golden_file(struct golden_file *obj)
{
	BUG_ON(!obj);
	BUG_ON(!npu_golden_ctx.dev);

	if (obj->img_vbuf) {
		vfree(obj->img_vbuf);
	}
	devm_kfree(npu_golden_ctx.dev, obj);
}

static int load_golden_file(struct golden_file *obj)
{

	int ret;
	mm_segment_t old_fs = 0;
	struct file *fp = NULL;
	struct kstat st;
	int nread;

	loff_t file_offset = 0;

	BUG_ON(!obj);
	BUG_ON(!npu_golden_ctx.dev);

	obj->img_vbuf = NULL;

	if (obj->file_name[0] == '\0') {
		npu_err("no file name\n");
		ret = -EINVAL;
		goto err_exit;
	}

	npu_info("start in load_golden_file %s\n", obj->file_name);

	/* Switch to Kernel data segmenet
	   Ref: https://wiki.kldp.org/wiki.php/%C4%BF%B3%CE%B3%BB%BF%A1%BC%ADReadWrite%BB%E7%BF%EB%C7%CF%B1%E2
	 */
	old_fs = get_fs();
	set_fs(KERNEL_DS);

	/* Step 1. Determine file size */
	fp = filp_open(obj->file_name, O_RDONLY, 0);
	if (!fp) {
		npu_err("fail in filp_open %s.\n", obj->file_name);
		ret = -EBADFD;
		goto err_exit;
	} else if (IS_ERR(fp)) {
		npu_err("fail in filp_open(%s): errno = %ld\n",
			obj->file_name, PTR_ERR(fp));
		fp = NULL;	/* No need to close fp on err_exit */
		ret = -ENOENT;
		goto err_exit;
	}

	/* Get file attributes */
	//[MOD]
	ret = vfs_getattr(&fp->f_path, &st, STATX_MODE | STATX_TYPE, AT_STATX_SYNC_AS_STAT);
	if (ret) {
		npu_err("error(%d) in vfs_getattr\n", ret);
		goto err_exit;
	}
	/* Checke whether it is regular file */
	if (!S_ISREG(st.mode))	{
		npu_err("fail in regular file.\n");
		ret = -ENOENT;
		goto err_exit;
	}

	obj->size = st.size;
	if (obj->size <= 0) {
		npu_err("fail in size check: %s, size=%lu.\n",
			obj->file_name, obj->size);
		ret = -ENOENT;
		goto err_exit;
	}

	/* Step 2. Allocate memory */
	obj->img_vbuf = vzalloc(obj->size);
	if (!obj->img_vbuf) {
		npu_err("fail in memory allocation: %s, size=%lu.\n",
			obj->file_name, obj->size);
		ret = -ENOMEM;
		goto err_exit;
	}

	/* Step 3. Read file contents and save to the allocated buffer */
	nread = kernel_read(fp, obj->img_vbuf, obj->size, &file_offset);
	if (nread != obj->size) {
		npu_err("fail(%d) in kernel_read", nread);
		ret = -EFAULT;
		goto err_exit;
	}

	ret = 0;
	goto ok_exit;

err_exit:
	/* Step 4. Clean-up */

	/* if an error occurs, image_buf buffer should be de-allocated */
	if (obj->img_vbuf)
		vfree(obj->img_vbuf);

	obj->img_vbuf = NULL;
	obj->size = 0;

ok_exit:
	if (fp)
		filp_close(fp, NULL);

	/* Second parameter of filp_close is NULL
	   Ref: https://stackoverflow.com/questions/32442521/
		why-can-the-posix-thread-id-be-null-in-linux-kernel-function-filp-close
	*/
	set_fs(old_fs);

	switch (ret) {
	case 0:
		npu_info("success in load_golden_file: (%s, %luB length)\n",
			 obj->file_name, obj->size);
		npu_dbg("loaded file on (%pK)\n", obj->img_vbuf);
		break;
	default:
		npu_err("fail(%d) in load_golden_file %s\n",
			ret, obj->file_name);
		break;
	}
	return ret;
}


static void destroy_npu_golden(void)
{
	struct golden_file *cur;
	struct golden_file *tmp;
	int cnt = 0;

	npu_statekeeper_transition(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_NOT_INITIALIZED);

	if (npu_golden_ctx.data.flags.valid == 0) {
		npu_warn("not initialized in npu_golden.data\n");
		return;
	}
	list_for_each_entry_safe(cur, tmp, &npu_golden_ctx.data.file_list, list) {
		list_del_init(&cur->list);
		destroy_golden_file(cur);
		cnt++;
	}
	npu_dbg("%s: removed %d file entries.\n", __func__, cnt);

	atomic_set(&npu_golden_ctx.loaded, 0);
	npu_golden_ctx.data.flags.valid = 0;
}
/*
 * Open :
 *  Create a new golden data
 */
static int npu_golden_set_open(struct inode *inode, struct file *file)
{
	BUG_ON(!file);
	mutex_lock(&npu_golden_ctx.lock);

	npu_info("start in npu_golden_set_open, npu_golden_ctx = @%pK\n", &npu_golden_ctx);

	/* Destroy last golden setting */
	if (npu_golden_ctx.data.flags.valid) {
		npu_dbg("open for npu_golden_set: Purge existing golden data....\n");
		destroy_npu_golden();
	}
	/* Clear old data */
	memset(&npu_golden_ctx.data, 0, sizeof(npu_golden_ctx.data));

	/* Set initial value for non-zero value */
	npu_golden_ctx.data.parse_state = PS_BEGIN;
	npu_golden_ctx.data.parse_pos.line = 1;
	npu_golden_ctx.data.parse_pos.col = 1;
	npu_golden_ctx.data.flags.valid = 1;
	npu_golden_ctx.data.flags.updated = 0;
	npu_golden_ctx.data.comparator = COMPARATOR_TABLE[0].comparator;	/* Default comparator */
	npu_golden_ctx.data.is_av_valid = COMPARATOR_TABLE[0].is_av_valid;	/* Default comparator */
	INIT_LIST_HEAD(&npu_golden_ctx.data.file_list);

	npu_statekeeper_transition(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_OPENED);

	npu_info("complete in npu_golden_set_open\n");
	mutex_unlock(&npu_golden_ctx.lock);
	return 0;
}

/*
 * Close : Wrap up the golden data and transition to
 *         comparing ready state
 */
static int npu_golden_set_close(struct inode *inode, struct file *file)
{
	int ret;
	struct golden_file *f;

	mutex_lock(&npu_golden_ctx.lock);

	npu_info("start in npu_golden_set_close\n");
	BUG_ON(!file);
	if (npu_golden_ctx.data.flags.valid == 0) {
		npu_err("not initialized in npu_golden_ctx.data\n");
		ret = -EBADFD;
		goto err_exit;
	}
	if (npu_golden_ctx.data.flags.updated == 0) {
		// No update after open
		npu_err("no golden description\n");
		ret = -EINVAL;
		goto err_exit;
	}
	if (npu_golden_ctx.data.parse_state != PS_BEGIN) {
		/* Improper parsing */
		npu_err("unexpected EOF in description.\n");
		ret = -EINVAL;
		goto err_exit;
	}

	/* Load golden files on memory */
	list_for_each_entry(f, &npu_golden_ctx.data.file_list, list) {
		ret = load_golden_file(f);
		if (ret) {
			npu_err("fail in load_golden_file on entry(%pK).\n", f);
			ret = -EFAULT;
			goto err_exit;
		}
	}

	/* Set flag to mark the golden is valid */
	atomic_set(&npu_golden_ctx.loaded, 1);

	/* Test data is all prepared */
	npu_statekeeper_transition(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_CONFIGURED);

	npu_info("complete in npu_golden_set_close\n");
	ret = 0;
	goto ok_exit;

err_exit:
	npu_err("error(%d) in npu_golden_set_close]\n", ret);

	destroy_npu_golden();
	npu_golden_ctx.data.flags.valid = 0;
	npu_statekeeper_transition(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_NOT_INITIALIZED);

ok_exit:
	mutex_unlock(&npu_golden_ctx.lock);
	return ret;
}

static npu_golden_set_token get_token_type(const char ch)
{
	switch (ch) {
	case '\n':
		return TOKEN_NL;
	case ' ':
	case '\t':
	case '\r':
		return TOKEN_WS;
	case '#':
		return TOKEN_SHARP;
	case '_':
	case '-':
	case '/':
	case '.':
		/* Special character allowed for alphabet */
		return TOKEN_ALPHA;
	}

	/* Range compare */
	if (('a' <= ch && ch <= 'z')
	    || ('A' <= ch && ch <= 'Z')) {
		return TOKEN_ALPHA;
	}

	if ('0' <= ch && ch <= '9')	{
		return TOKEN_NUMBER;
	}

	return TOKEN_INVALID;
}

/* Remove trailing whitespcae from parse_buf */
static void trim_and_term_parse_buf(struct parse_buf *parse_buf)
{
	while (parse_buf->idx > 0) {
		if (get_token_type(parse_buf->token_char[parse_buf->idx - 1]) != TOKEN_WS) {
			break;
		}
		parse_buf->idx--;
	}
	parse_buf->token_char[parse_buf->idx] = '\0';
}

static void to_upper_parse_buf(struct parse_buf *parse_buf)
{
	size_t i;

	for (i = 0; i < parse_buf->idx; ++i) {
		parse_buf->token_char[i] = toupper(parse_buf->token_char[i]);
	}
}

static inline void reset_parse_buf(struct parse_buf *parse_buf)
{
	parse_buf->idx = 0;
	parse_buf->token_char[0] = '\0';
}

/***************************************************************************
 * Value setter funtion
 */
static int set_frame_id(struct parse_buf *parse_buf)
{
	int ret;
	long long frame_id;

	BUG_ON(!parse_buf);
	SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES);
	trim_and_term_parse_buf(parse_buf);
	if (parse_buf->idx <= 0)	{
		return -1;
	}

	ret = kstrtoll(parse_buf->token_char, 10, &frame_id);
	if (ret) {
		npu_err("invalid frame id (%s) specified on line (%d)\n",
			parse_buf->token_char,
			npu_golden_ctx.data.parse_pos.line);
		return ret;
	}

	npu_golden_ctx.data.flags.frame_id_matching = 1;
	npu_golden_ctx.data.match.frame_id = frame_id;

	npu_info("frame ID matching set: Frame ID = (%u)\n", npu_golden_ctx.data.match.frame_id);
	return 0;
}
static int set_net_id(struct parse_buf *parse_buf)
{
	int ret;
	long long net_id;

	BUG_ON(!parse_buf);
	SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES);
	trim_and_term_parse_buf(parse_buf);
	if (parse_buf->idx <= 0)	{
		return -1;
	}

	ret = kstrtoll(parse_buf->token_char, 10, &net_id);
	if (ret) {
		npu_err("invalid net id [%s] specified on line %d\n",
			parse_buf->token_char,
			npu_golden_ctx.data.parse_pos.line);
		return ret;
	}

	npu_golden_ctx.data.flags.net_id_matching = 1;
	npu_golden_ctx.data.match.net_id = net_id;

	npu_info("net ID matching set: Net ID = (%u)\n", npu_golden_ctx.data.match.net_id);
	return 0;
}

static int set_comparator(struct parse_buf *parse_buf)
{
	int i;

	BUG_ON(!parse_buf);
	SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES);
	trim_and_term_parse_buf(parse_buf);
	to_upper_parse_buf(parse_buf);
	if (parse_buf->idx <= 0)	{
		return -1;
	}

	/* Find header value */
	for (i = 0; COMPARATOR_TABLE[i].name != NULL; i++) {
		if (strncmp(parse_buf->token_char, COMPARATOR_TABLE[i].name, parse_buf->idx) == 0) {
			npu_info("setting comparator to (%s)\n", COMPARATOR_TABLE[i].name);
			npu_golden_ctx.data.comparator = COMPARATOR_TABLE[i].comparator;
			npu_golden_ctx.data.is_av_valid = COMPARATOR_TABLE[i].is_av_valid;
			goto find_header;
		}
	}
	/* Could not find appropirate header */
	npu_err("invalid comparator (%s) found on line (%d)\n", parse_buf->token_char, npu_golden_ctx.data.parse_pos.line);
	return -1;
find_header:
	return 0;
}

struct {
	const char *header;
	int (*value_setter)(struct parse_buf *);
} HDR_PARSE_TABLE[] = {
	{.header = "FRAME_ID",		.value_setter = set_frame_id},
	{.header = "NET_ID",		.value_setter = set_net_id},
	{.header = "COMPARATOR",	.value_setter = set_comparator},
	{.header = NULL,		.value_setter = NULL},
};

/***************************************************************************
 * Parse action funtion
 */
/* Do nothing but consume the input character */
static int skip(const char ch, struct parse_buf *parse_buf)
{
	BUG_ON(!parse_buf);
	SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES);
	return 0;
}
/* Keep the character in parse_buf */
static int append(const char ch, struct parse_buf *parse_buf)
{
	BUG_ON(!parse_buf);
	SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES);
	if (parse_buf->idx + 1 >= GOLDEN_FILENAME_LEN) {
		return -1;
	} else {
		parse_buf->token_char[parse_buf->idx] = ch;
		parse_buf->idx++;
	}
	return 0;
}
static int set_hdr_name(const char ch, struct parse_buf *parse_buf)
{
	int i;

	BUG_ON(!parse_buf);
	SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES);
	trim_and_term_parse_buf(parse_buf);
	to_upper_parse_buf(parse_buf);
	if (parse_buf->idx <= 0)	{
		return -1;
	}

	/* Find header value */
	for (i = 0; HDR_PARSE_TABLE[i].header != NULL; i++) {
		if (strncmp(parse_buf->token_char, HDR_PARSE_TABLE[i].header, parse_buf->idx) == 0) {
			parse_buf->value_setter = HDR_PARSE_TABLE[i].value_setter;
			goto find_header;
		}
	}
	/* Could not find appropirate header */
	npu_err("invalid header (%s) found on line (%d)\n", parse_buf->token_char, npu_golden_ctx.data.parse_pos.line);
	return -1;
find_header:
	/* reset parse_buf */
	reset_parse_buf(parse_buf);

	return 0;
}
static int set_hdr_param(const char ch, struct parse_buf *parse_buf)
{
	int ret;

	BUG_ON(!parse_buf);
	SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES);
	trim_and_term_parse_buf(parse_buf);

	if (parse_buf->value_setter == NULL) {
		npu_err("no value setter on line (%d)\n", npu_golden_ctx.data.parse_pos.line);
		return -1;
	}
	ret = parse_buf->value_setter(parse_buf);
	if (ret) {
		npu_err("value setter failed on line (%d)\n", npu_golden_ctx.data.parse_pos.line);
		return ret;
	}
	parse_buf->value_setter = NULL;

	/* reset parse_buf */
	reset_parse_buf(parse_buf);

	return 0;
}
static int set_golden_idx(const char ch, struct parse_buf *parse_buf)
{
	int ret;
	long long golden_idx;
	struct golden_file *golden_file;

	BUG_ON(!parse_buf);
	SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES);
	trim_and_term_parse_buf(parse_buf);
	if (parse_buf->idx <= 0)	{
		return -1;
	}

	ret = kstrtoll(parse_buf->token_char, 10, &golden_idx);
	if (ret) {
		npu_err("invalid vector index (%s) specified on line %d\n",
			parse_buf->token_char,
			npu_golden_ctx.data.parse_pos.line);
		return ret;
	}

	/* Create a new file entry */
	golden_file = new_golden_file();
	if (!golden_file) {
		npu_err("fail in new_golden_file\n");
		return -ENOMEM;
	}
	golden_file->av_index = golden_idx;
	list_add_tail(&golden_file->list, &npu_golden_ctx.data.file_list);

	npu_dbg("adding new_golden_file entry: IDX(%d), Ptr(%pK)\n",
		golden_file->av_index, golden_file);

	/* reset parse_buf */
	reset_parse_buf(parse_buf);

	return 0;
}
static int set_golden_file(const char ch, struct parse_buf *parse_buf)
{
	struct golden_file *golden_file;

	BUG_ON(!parse_buf);
	SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES);
	trim_and_term_parse_buf(parse_buf);

	/* Retrive last entry from the list -> Added on last set_golden_idx() */
	if (list_empty(&npu_golden_ctx.data.file_list)) {
		/* Somethings wrong */
		npu_err("no file list entry on line (%d)\n", npu_golden_ctx.data.parse_pos.line);
		return -1;
	}
	golden_file = list_last_entry(&npu_golden_ctx.data.file_list, struct golden_file, list);
	BUG_ON(!golden_file);

	strncpy(golden_file->file_name, parse_buf->token_char, parse_buf->idx);
	golden_file->file_name[parse_buf->idx] = '\0';

	npu_dbg("golden file entry set: IDX(%d), filename(%s), Ptr(%pK)\n",
		golden_file->av_index, golden_file->file_name, golden_file);

	/* reset parse_buf */
	reset_parse_buf(parse_buf);

	return 0;
}
static int reset(const char ch, struct parse_buf *parse_buf)
{
	BUG_ON(!parse_buf);
	SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES);
	/* reset parse_buf */
	reset_parse_buf(parse_buf);

	return 0;
}


static const struct parsing_action PARSE_TABLE[][TOKEN_INVALID+1] = {
/*PS_BEGIN*/ {
	/*TOKEN_ALPHA,*/	{PS_HDR, append},
	/*TOKEN_NUMBER,*/	{PS_GOLDEN_IDX, append},
	/*TOKEN_WS,*/		{PS_BEGIN, skip},
	/*TOKEN_NL,*/		{PS_BEGIN, skip},
	/*TOKEN_SHARP,*/	{PS_COMMENT, skip},
	/*TOKEN_INVALID,*/	{PS_INVALID, skip},
},
/*PS_HDR,*/ {
	/*TOKEN_ALPHA,*/	{PS_HDR, append},
	/*TOKEN_NUMBER,*/	{PS_HDR, append},
	/*TOKEN_WS,*/		{PS_HDR_SEP, set_hdr_name},
	/*TOKEN_NL,*/		{PS_INVALID, skip},
	/*TOKEN_SHARP,*/	{PS_INVALID, skip},
	/*TOKEN_INVALID,*/	{PS_INVALID, skip},
},
/*PS_HDR_SEP,*/ {
	/*TOKEN_ALPHA,*/	{PS_HDR_PARAM, append},
	/*TOKEN_NUMBER,*/	{PS_HDR_PARAM, append},
	/*TOKEN_WS,*/		{PS_HDR_SEP, skip},
	/*TOKEN_NL,*/		{PS_INVALID, skip},
	/*TOKEN_SHARP,*/	{PS_INVALID, skip},
	/*TOKEN_INVALID,*/	{PS_INVALID, skip},
},
/*PS_HDR_PARAM,*/ {
	/*TOKEN_ALPHA,*/	{PS_HDR_PARAM, append},
	/*TOKEN_NUMBER,*/	{PS_HDR_PARAM, append},
	/*TOKEN_WS,*/		{PS_HDR_PARAM, append},
	/*TOKEN_NL,*/		{PS_BEGIN, set_hdr_param},
	/*TOKEN_SHARP,*/	{PS_INVALID, skip},
	/*TOKEN_INVALID,*/	{PS_INVALID, skip},
},
/*PS_COMMENT,*/ {
	/*TOKEN_ALPHA,*/	{PS_COMMENT, skip},
	/*TOKEN_NUMBER,*/	{PS_COMMENT, skip},
	/*TOKEN_WS,*/		{PS_COMMENT, skip},
	/*TOKEN_NL,*/		{PS_BEGIN, reset},
	/*TOKEN_SHARP,*/	{PS_COMMENT, skip},
	/*TOKEN_INVALID,*/	{PS_COMMENT, skip},
},
/*PS_GOLDEN_IDX,*/ {
	/*TOKEN_ALPHA,*/	{PS_INVALID, skip},
	/*TOKEN_NUMBER,*/	{PS_GOLDEN_IDX, append},
	/*TOKEN_WS,*/		{PS_GOLDEN_SEP, set_golden_idx},
	/*TOKEN_NL,*/		{PS_INVALID, skip},
	/*TOKEN_SHARP,*/	{PS_INVALID, skip},
	/*TOKEN_INVALID,*/	{PS_INVALID, skip},
},
/*PS_GOLDEN_SEP,*/  {
	/*TOKEN_ALPHA,*/	{PS_GOLDEN_FILE, append},
	/*TOKEN_NUMBER,*/	{PS_GOLDEN_FILE, append},
	/*TOKEN_WS,*/		{PS_GOLDEN_SEP, skip},
	/*TOKEN_NL,*/		{PS_INVALID, skip},
	/*TOKEN_SHARP,*/	{PS_INVALID, skip},
	/*TOKEN_INVALID,*/	{PS_INVALID, skip},
},
/*PS_GOLDEN_FILE*/  {
	/*TOKEN_ALPHA,*/	{PS_GOLDEN_FILE, append},
	/*TOKEN_NUMBER,*/	{PS_GOLDEN_FILE, append},
	/*TOKEN_WS,*/		{PS_GOLDEN_FILE, append},
	/*TOKEN_NL,*/		{PS_BEGIN, set_golden_file},
	/*TOKEN_SHARP,*/	{PS_INVALID, skip},
	/*TOKEN_INVALID,*/	{PS_INVALID, skip},
},
};

int parse_char(const char ch)
{
	npu_golden_set_token token_type;
	npu_golden_set_parsing_state cur_state = npu_golden_ctx.data.parse_state;
	const struct parsing_action *cur_action = NULL;
	struct parse_buf *parse_buf = &npu_golden_ctx.data.parse_buf;

	token_type = get_token_type(ch);
	if (token_type == TOKEN_INVALID) {
		/* Invalid character */
		npu_err("invalid token at line:%u, col:%u\n",
			npu_golden_ctx.data.parse_pos.line,
			npu_golden_ctx.data.parse_pos.col);
		return -EINVAL;
	}
	cur_action = &(PARSE_TABLE[cur_state][token_type]);

	/* Do action and state transition */
	if (cur_action->func) {
		if (cur_action->func(ch, parse_buf) != 0) {
			/* Invalid character */
			npu_err("parsing error at line:%u, col:%u\n",
				npu_golden_ctx.data.parse_pos.line,
				npu_golden_ctx.data.parse_pos.col);
			return -EINVAL;
		}
	}
	npu_golden_ctx.data.parse_state = cur_action->next_state;
	if (cur_action->next_state >= PS_INVALID) {
		npu_err("parsing error at line:%u, col:%u\n",
			npu_golden_ctx.data.parse_pos.line,
			npu_golden_ctx.data.parse_pos.col);
		return -EINVAL;
	}

	/* Adjust col and line */
	if (token_type == TOKEN_NL) {
		npu_golden_ctx.data.parse_pos.line++;
		npu_golden_ctx.data.parse_pos.col = 0;
	} else {
		npu_golden_ctx.data.parse_pos.col++;
	}
	return 0;
}

/*
 * Write : Set golden definition
 */
static ssize_t npu_golden_set_write(struct file *file, const char * __user userbuf, size_t count, loff_t *offp)
{
	char ch;
	size_t i;
	int ret;

	mutex_lock(&npu_golden_ctx.lock);
	npu_statekeeper_transition(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES);

	for (i = 0; i < count; i++) {
		get_user(ch, userbuf+i);
		ret = parse_char(ch);
		if (ret) {
			goto err_exit;
		}
	}

	ret = i;
	npu_golden_ctx.data.flags.updated = 1;

err_exit:
	mutex_unlock(&npu_golden_ctx.lock);
	return ret;
}

/* Same implementation but buf is kernel buffer - For IDIOT testing */
__attribute__((unused)) static ssize_t npu_golden_set_write_kern(struct file *file, const char *buf, size_t count, loff_t *offp)
{
	char ch;
	size_t i;
	int ret;

	mutex_lock(&npu_golden_ctx.lock);
	npu_statekeeper_transition(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_ADDING_GOLDEN_FILES);

	for (i = 0; i < count; i++) {
		ch = buf[i];
		ret = parse_char(ch);
		if (ret) {
			goto err_exit;
		}
	}

	ret = i;
	npu_golden_ctx.data.flags.updated = 1;

err_exit:
	mutex_unlock(&npu_golden_ctx.lock);
	return ret;
}

static const struct file_operations npu_golden_set_debug_fops = {
	.owner = THIS_MODULE,
	.open = npu_golden_set_open,
	.release = npu_golden_set_close,
	.write = npu_golden_set_write,
};

/*
 * Open: Check readiness of compare
 */
static int npu_golden_match_open(struct inode *inode, struct file *file)
{
	int	ret;
	BUG_ON(!file);
	BUG_ON(!npu_golden_ctx.dev);

	mutex_lock(&npu_golden_ctx.lock);
	npu_info("start in npu_golden_match_open\n");
	if (!SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_CONFIGURED)
	   && !SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_COMPARING)) {
		npu_err("not configured in npu_golden_ctx\n");
		ret = -EBADFD;
		goto err_exit;
	}

	if (!atomic_read(&npu_golden_ctx.loaded)) {
		npu_err("not loaded in npu_golden_ctx\n");
		ret = -EBADFD;
		goto err_exit;
	}
	ret = 0;

err_exit:
	mutex_unlock(&npu_golden_ctx.lock);
	npu_info("complete in npu_golden_match_open\n");
	return ret;
}

/*
 * Close : Reset the copied_len to get same result on next read.
 */
static int npu_golden_match_close(struct inode *inode, struct file *file)
{
	mutex_lock(&npu_golden_ctx.lock);
	npu_golden_ctx.result.copied_len = 0;
	mutex_unlock(&npu_golden_ctx.lock);
	return 0;
}

/*
 * Provide contents of golden matching
 */
static ssize_t npu_golden_match_read(struct file *file, char __user *outbuf, size_t outlen, loff_t *loff)
{
	struct npu_golden_compare_result *result = &npu_golden_ctx.result;
	size_t copy_len;
	int ret;

	mutex_lock(&npu_golden_ctx.lock);
	if (result->out_buf_cur == 0) {
		/* No data available */
		npu_dbg("No golden matching result available.");
		copy_len = 0;
		goto err_exit;
	}
	if (result->copied_len >= result->out_buf_cur) {
		/* Otherwise, EOF */
		copy_len = 0;
		goto err_exit;
	}

	copy_len = min(outlen, result->out_buf_cur - result->copied_len);
	ret = copy_to_user(outbuf, result->out_buf + result->copied_len, copy_len);
	if (ret) {
		npu_err("fail in copy_to_user: %d bytes are not copied\n", ret);
	}
	copy_len -= ret;
	result->copied_len += copy_len;

err_exit:
	mutex_unlock(&npu_golden_ctx.lock);
	return copy_len;
}

static void reset_compare_result(struct npu_golden_compare_result *result)
{
	BUG_ON(!result);
	result->copied_len = 0;
	result->out_buf_cur = 0;
	result->out_buf_len = NPU_GOLDEN_RESULT_BUF_LEN;
	result->out_buf[0] = '\0';
}



/*
 * Compare Golden data with address vector
 */
static int __npu_golden_compare(const struct npu_frame *frame)
{
	struct npu_golden_compare_result *result;
	struct npu_session *session;
	struct golden_file *golden_entry;
	struct addr_info *target_entry;
	struct ncp_header *ncp_header;
	struct memory_vector *memory_vector;

	struct npu_fmap_dim	dim;
	u32			av_len;
	int			compare_result = 0;
	int			total_result = 0;
	int			ret;
	int			tr_msg_len;
	size_t			byte_size, dim_size;

	BUG_ON(!frame);
	BUG_ON(!frame->session);

	if (!SKEEPER_EXPECT_STATE(&npu_golden_ctx.statekeeper, NPU_GOLDEN_STATE_CONFIGURED)) {
		npu_dbg("invalid state.");
		return -EINVAL;
	}

	result = &npu_golden_ctx.result;
	session = frame->session;
	ncp_header = (struct ncp_header *)(session->ncp_info.ncp_addr.vaddr);

	npu_dbg("golden compare: starting with frame @ %pK, result at %pK\n [cur=%zu len=%zu cop=%zu]",
		 frame, result->out_buf, result->out_buf_cur, result->out_buf_len, result->copied_len);

	if (!ncp_header) {
		npu_uferr("not initialized in NCP header\n", frame);
		return -EINVAL;
	}
	if (ncp_header->magic_number1 != NCP_MAGIC1) {
		npu_uferr("NCP header magic mismatch: expected(0x%08x), actual(0x%08x).\n",
			frame, NCP_MAGIC1, ncp_header->magic_number1);
		return -EINVAL;
	}

	/* Check comparing condition */
	if (npu_golden_ctx.data.flags.frame_id_matching) {
		if (frame->frame_id != npu_golden_ctx.data.match.frame_id) {
			npu_ufdbg("frame ID [frame:%u != Golden:%u] mismatch. skipping.\n",
				frame, frame->frame_id, npu_golden_ctx.data.match.frame_id);
			return 0;
		}
	}
	if (npu_golden_ctx.data.flags.net_id_matching) {
		if (ncp_header->net_id != npu_golden_ctx.data.match.net_id) {
			npu_ufdbg("net ID [fFrame:%u != golden:%u] mismatch. skipping.\n",
				frame, ncp_header->net_id, npu_golden_ctx.data.match.net_id);
			return 0;
		}
	}

	/* Reset comparing buffer */
	reset_compare_result(&npu_golden_ctx.result);

	/* Retrieve address vector information */
	av_len = session->ncp_info.address_vector_cnt;

	if (av_len <= 0) {
		npu_uferr("Address vector table length [%u] is not properly initialized.\n", frame, av_len);
		return -EINVAL;
	}

	/* Compare each vector */
	list_for_each_entry(golden_entry, &npu_golden_ctx.data.file_list, list) {
		compare_result = 0;

		if (golden_entry->av_index >= av_len) {
			add_compare_msg(&npu_golden_ctx.result,
					"Golden idx[%u] exceedes total number of address vector[%u]. ",
					golden_entry->av_index, av_len);
			compare_result++;
			goto end_compare;
		}

		target_entry = find_addr_info(session, golden_entry->av_index, OFM_TYPE, frame->input->index);
		if (!target_entry) {
			target_entry = find_addr_info(session, golden_entry->av_index, IMB_TYPE, frame->input->index);
		}
		if (!target_entry) {
			add_compare_msg(&npu_golden_ctx.result,
					"No matching address vector found for idx[%u]. ",
					golden_entry->av_index);
			compare_result++;
			goto end_compare;
		}

		/* Size compare */
		if (!npu_golden_ctx.data.is_av_valid(golden_entry->size, target_entry->size, result)) {
			compare_result++;
			goto end_compare;
		}

		/* Setting featuremap dimension */
		memory_vector = (struct memory_vector *)((char *) ncp_header + ncp_header->memory_vector_offset);

		dim.axis1 = (size_t)((memory_vector + golden_entry->av_index)->channels);
		dim.axis2 = (size_t)((memory_vector + golden_entry->av_index)->height);
		dim.axis3 = (size_t)((memory_vector + golden_entry->av_index)->width);
		byte_size = (golden_entry->size < target_entry->size) ? golden_entry->size : target_entry->size;
		dim_size = dim.axis1 * dim.axis2 * dim.axis3;

		if (dim_size == 0 || dim_size != byte_size) {
			npu_ufwarn("Axis on MV(%lu x %lu x %lu) does not match with size[%lu] -> Use 1D comparison\n",
				frame, dim.axis1, dim.axis2, dim.axis3, byte_size);
			dim.axis1 = dim.axis2 = 1;
			dim.axis3 = byte_size;
		}

		npu_ufdbg("av_index(%u), dim.axis1(%zu), dim.axis2(%zu), dim.axis3(%zu)\n",
			frame, golden_entry->av_index, dim.axis1, dim.axis2, dim.axis3);

		add_compare_msg(&npu_golden_ctx.result, "%u: [%lu x %lu x %lu]:",
				golden_entry->av_index,
				dim.axis1, dim.axis2, dim.axis3);

		/* Buffer pointer validation */
		if (!target_entry->vaddr) {
			add_compare_msg(&npu_golden_ctx.result,
					"KV addr of address vector golden idx[%u] is not valid. ",
					golden_entry->av_index);
			compare_result++;
			goto end_compare;
		}
		if (!golden_entry->img_vbuf) {
			add_compare_msg(&npu_golden_ctx.result,
					"Image data of golden idx[%u] is not valid. ",
					golden_entry->av_index);
			compare_result++;
			goto end_compare;
		}
		/* Cache invalidation */
		__dma_map_area(target_entry->vaddr, target_entry->size, DMA_FROM_DEVICE);

		npu_ufdbg("golden compare Golden[%pK:%zuB] / Target[%pK:%zuB]\n",
			frame, golden_entry->img_vbuf, golden_entry->size,
			target_entry->vaddr, target_entry->size);
		/* Finally, comparing contents */
		if (npu_golden_ctx.data.comparator) {
			ret = npu_golden_ctx.data.comparator(0,
					golden_entry->img_vbuf, target_entry->vaddr,
					dim, result);
			compare_result += ret;
			npu_ufdbg("result of comparator for golden idx(%u): (%d)\n",
				frame, golden_entry->av_index, ret);

		} else {
			npu_ufdbg("no comparator defined for golden idx(%u). skipping data match.\n",
				frame, golden_entry->av_index);
		}

end_compare:
		if (compare_result > 0) {
			add_compare_msg(&npu_golden_ctx.result,
					" MISMATCH(%d)\n", compare_result);
		} else {
			add_compare_msg(&npu_golden_ctx.result, " OK\n");
		}

		total_result += compare_result;
	}

	/* If the output buffer is full, left an message at the end of the buffer */
	if (result->out_buf_cur == (result->out_buf_len - 1)) {
		npu_ufdbg("full in output buffer, truncate the result.\n", frame);
		/* Notify that the buffer is truncated */
		tr_msg_len = strlen(TRUNCATE_MSG);
		strncpy(&result->out_buf[NPU_GOLDEN_RESULT_BUF_LEN - tr_msg_len],
			TRUNCATE_MSG, tr_msg_len);
	}
	return total_result;
}

/*
 * Wrapper to npu_golden_compare for locking
 */
int npu_golden_compare(const struct npu_frame *frame)
{
	int ret;

	if (!atomic_read(&npu_golden_ctx.loaded)) {
		npu_uftrace("Golden is not configured.\n", frame);
		return 0;
	}

	mutex_lock(&npu_golden_ctx.lock);
	ret = __npu_golden_compare(frame);
	mutex_unlock(&npu_golden_ctx.lock);

	return ret;
}

static const struct file_operations npu_golden_match_debug_fops = {
	.owner = THIS_MODULE,
	.open = npu_golden_match_open,
	.release = npu_golden_match_close,
	.read = npu_golden_match_read,
};

#define SET_GOLDEN_DESC_DEBUGFS_NAME		"set-golden-desc"
#define RESULT_GOLDEN_MATCH_DEBUGFS_NAME	"result-golden-match"

int register_golden_matcher(struct device *dev)
{
	int ret;

	npu_info("start in register_golden_matcher\n");

	BUG_ON(!dev);

	npu_dbg("initialize statekeeper in register_golden_matcher\n");
	npu_statekeeper_initialize(&npu_golden_ctx.statekeeper,
				   NPU_GOLDEN_STATE_INVALID,
				   state_names, transition_map);

	npu_info("Registeration of golden matcher : Register " SET_GOLDEN_DESC_DEBUGFS_NAME " to debugfs.\n");
	ret = npu_debug_register(SET_GOLDEN_DESC_DEBUGFS_NAME, &npu_golden_set_debug_fops);
	if (ret) {
		npu_err("fail(%d) in npu_debug_register for " SET_GOLDEN_DESC_DEBUGFS_NAME "\n", ret);
		goto err_exit;
	}

	npu_info("Registeration of golden matcher: Register " RESULT_GOLDEN_MATCH_DEBUGFS_NAME " to debugfs.\n");
	ret = npu_debug_register(RESULT_GOLDEN_MATCH_DEBUGFS_NAME, &npu_golden_match_debug_fops);
	if (ret) {
		npu_err("fail(%d) in npu_debug_register for " RESULT_GOLDEN_MATCH_DEBUGFS_NAME "\n", ret);
		goto err_exit;
	}

	/* Initialize lock */
	atomic_set(&npu_golden_ctx.loaded, 0);
	mutex_init(&npu_golden_ctx.lock);

	npu_golden_ctx.dev = dev;
	npu_info("complete in register_golden_matcher\n");
	return 0;

err_exit:
	npu_err("fail(%d) in register_golden_matcher\n", ret);
	return ret;
}

/* Unit test */
#ifdef CONFIG_VISION_UNITTEST
#define IDIOT_TESTCASE_IMPL "npu-golden.idiot"
#include "idiot-def.h"
#endif