-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnpu-protodrv.c
More file actions
2854 lines (2508 loc) · 81.8 KB
/
Copy pathnpu-protodrv.c
File metadata and controls
2854 lines (2508 loc) · 81.8 KB
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
/*
* 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/atomic.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/atomic.h>
#include <linux/timekeeping.h>
#include "npu-log.h"
#include "npu-protodrv.h"
#include "npu-util-liststatemgr.h"
#include "npu-debug.h"
#include "npu-errno.h"
#include "npu-device.h"
#include "npu-queue.h"
#include "npu-if-protodrv-mbox2.h"
#include "npu-if-session-protodrv.h"
#define NPU_PROTO_DRV_SIZE 1024
#define NPU_PROTO_DRV_NAME "npu-proto_driver"
#define NPU_PROTO_DRV_FRAME_LSM_NAME "npu-proto_frame_lsm"
#define NPU_PROTO_DRV_NW_LSM_NAME "npu-proto_nw_lsm"
#define NPU_PROTO_DRV_AST_NAME "npu-proto_AST"
#ifdef NPU_LOG_TAG
#undef NPU_LOG_TAG
#endif
#define NPU_LOG_TAG "proto-drv"
#define UNLINK_CHECK_OWNERSHIP
#define TIME_STAT_BUF_LEN 256
const char *TYPE_NAME_FRAME = "frame";
const char *TYPE_NAME_NW = "Netwrok mgmt.";
/* Print log if driver is idle more than 10 seconds */
const s64 NPU_PROTO_DRV_IDLE_LOG_DELAY_NS = 10L * 1000 * 1000 * 1000;
#ifdef MBOX_MOCK_ENABLE
int deinit_for_mbox_mock(void);
int setup_for_mbox_mock(void);
#endif
/* State management */
typedef enum {
PROTO_DRV_STATE_UNLOADED = 0,
PROTO_DRV_STATE_PROBED,
PROTO_DRV_STATE_OPENED,
PROTO_DRV_STATE_INVALID,
} proto_drv_state_e;
const char *PROTO_DRV_STATE_NAME[PROTO_DRV_STATE_INVALID + 1]
= {"UNLOADED", "PROBED", "OPENED", "INVALID"};
static const u8 proto_drv_thread_state_transition[][PROTO_DRV_STATE_INVALID+1] = {
/* From - To UNLOADED PROBED OPENED INVALID*/
/* UNLOADED */ { 0, 1, 0, 0},
/* PROBED */ { 1, 0, 1, 0},
/* OPENED */ { 1, 1, 0, 0},
/* INVALID */ { 0, 0, 0, 0},
};
/* Declare List State Manager object for frame and nw*/
LSM_DECLARE(proto_frame_lsm, struct proto_req_frame, NPU_PROTO_DRV_SIZE, NPU_PROTO_DRV_FRAME_LSM_NAME);
LSM_DECLARE(proto_nw_lsm, struct proto_req_nw, NPU_PROTO_DRV_SIZE, NPU_PROTO_DRV_NW_LSM_NAME);
/* Definition of proto-drv singleton object */
struct npu_proto_drv npu_proto_drv = {
.magic_head = PROTO_DRV_MAGIC_HEAD,
.frame_lsm = &proto_frame_lsm,
.nw_lsm = &proto_nw_lsm,
.ast_param = {0},
.state = ATOMIC_INIT(PROTO_DRV_STATE_UNLOADED),
.req_id_gen = ATOMIC_INIT(NPU_REQ_ID_INITIAL - 1),
.if_session_ctx = NULL,
.npu_device = NULL,
.session_ref = {
.hash_table = {NULL},
.entry_list = LIST_HEAD_INIT(npu_proto_drv.session_ref.entry_list),
},
.magic_tail = PROTO_DRV_MAGIC_TAIL,
};
struct proto_drv_ops {
struct session_nw_ops session_nw_ops;
struct session_frame_ops session_frame_ops;
struct mbox_nw_ops mbox_nw_ops;
struct mbox_frame_ops mbox_frame_ops;
};
/*******************************************************************************
* State management functions
*
*/
static proto_drv_state_e state_transition(proto_drv_state_e new_state)
{
proto_drv_state_e old_state;
BUG_ON(new_state < 0);
BUG_ON(new_state >= PROTO_DRV_STATE_INVALID);
old_state = atomic_xchg(&npu_proto_drv.state, new_state);
/* Check after transition is made - To ensure atomicity */
if (!proto_drv_thread_state_transition[old_state][new_state]) {
npu_err("Invalid transition [%s] -> [%s]\n",
PROTO_DRV_STATE_NAME[old_state],
PROTO_DRV_STATE_NAME[new_state]);
BUG_ON(1);
}
return old_state;
}
static inline proto_drv_state_e get_state(void)
{
return atomic_read(&npu_proto_drv.state);
}
#define IS_TRANSITABLE(TARGET) \
({ \
proto_drv_state_e __curr_state = get_state(); \
proto_drv_state_e __target_state = (TARGET); \
u8 __ret; \
BUG_ON(__target_state < 0); \
BUG_ON(__target_state >= PROTO_DRV_STATE_INVALID); \
__ret = proto_drv_thread_state_transition[__curr_state][__target_state]; \
if (!__ret) \
npu_err("Invalid transition (%s) -> (%s)\n", \
PROTO_DRV_STATE_NAME[__curr_state], \
PROTO_DRV_STATE_NAME[__target_state]); \
__ret; \
})
#define EXPECT_STATE(STATE) \
({ \
proto_drv_state_e __curr_state = get_state(); \
proto_drv_state_e __expect_state = (STATE); \
BUG_ON(__expect_state < 0); \
BUG_ON(__expect_state >= PROTO_DRV_STATE_INVALID); \
if (__curr_state != __expect_state) \
npu_err("Requires state (%s), but current state is (%s)\n", \
PROTO_DRV_STATE_NAME[__expect_state], \
PROTO_DRV_STATE_NAME[__curr_state]); \
(__curr_state == __expect_state); \
})
/*******************************************************************************
* Utility functions
*
*/
static inline s64 get_time_ns(void)
{
return ktime_to_ns(ktime_get_boottime());
}
static inline const char *getTypeName(const proto_drv_req_type_e type)
{
switch (type) {
case PROTO_DRV_REQ_TYPE_FRAME:
return TYPE_NAME_FRAME;
case PROTO_DRV_REQ_TYPE_NW:
return TYPE_NAME_NW;
case PROTO_DRV_REQ_TYPE_INVALID:
default:
return "INVALID";
}
}
/* Look-up table for NW names */
static const char *NW_CMD_NAMES[NPU_NW_CMD_END - NPU_NW_CMD_BASE] = {
NULL, /* For NPU_NW_CMD_BASE */
"LOAD",
"UNLOAD",
"STREAM_ON",
"STREAM_OFF",
"POWER_DOWN",
"PROFILE_START",
"PROFILE_STOP",
"FW_TC_EXECUTE",
"CLEAR_CB",
};
/* Convinient function to get stringfy name of command */
static const char *__cmd_name(const u32 cmd)
{
int idx = cmd - NPU_NW_CMD_BASE;
BUG_ON(idx < 0);
BUG_ON(idx >= ARRAY_SIZE(NW_CMD_NAMES));
BUG_ON(!NW_CMD_NAMES[idx]);
return NW_CMD_NAMES[idx];
}
/*******************************************************************************
* Session reference management
*
*/
static void reset_session_ref(void)
{
struct session_ref *sess_ref = &(npu_proto_drv.session_ref);
/* Clear to Zero and initialize list head */
memset(sess_ref, 0, sizeof(*sess_ref));
INIT_LIST_HEAD(&sess_ref->entry_list);
}
static u32 __find_hash_id(const npu_uid_t uid)
{
u32 hash_id;
u32 add_val = 0;
struct session_ref *sess_ref = &(npu_proto_drv.session_ref);
struct session_ref_entry *sess_entry;
/* Retrive hash_id from session uid */
hash_id = uid % SESS_REF_HASH_TABLE_SIZE;
for (add_val = 0; add_val < SESS_REF_HASH_TABLE_SIZE; add_val += SESS_REF_HASH_MAGIC) {
hash_id = (hash_id + add_val) % SESS_REF_HASH_TABLE_SIZE;
sess_entry = sess_ref->hash_table[hash_id];
if (sess_entry != NULL) {
if (sess_entry->uid == uid) {
/* Match */
return hash_id;
}
}
}
/* Failed to find */
return SESS_REF_INVALID_HASH_ID;
}
/* Return 0 if the nw object is not belong to any session.
* Currently, POWER_DOWN and Profiling commands are not belong to session.
*/
static int is_belong_session(const struct npu_nw *nw)
{
BUG_ON(!nw);
switch (nw->cmd) {
case NPU_NW_CMD_POWER_DOWN:
case NPU_NW_CMD_PROFILE_START:
case NPU_NW_CMD_PROFILE_STOP:
case NPU_NW_CMD_FW_TC_EXECUTE:
return 0;
default:
return 1;
}
}
/*
* Return true for errcode whose proto_req_* object need to be
* kept on STUCKED state (To prepare out-of-order msgid arrival)
*/
static int is_stucked_result_code(const npu_errno_t result_code)
{
/* Determine by its result code */
switch (NPU_ERR_CODE(result_code)) {
case NPU_ERR_NPU_TIMEOUT:
case NPU_ERR_QUEUE_TIMEOUT:
return 1;
default:
return 0;
}
}
static int is_stucked_req_frame(const struct proto_req_frame *req_frame)
{
return is_stucked_result_code(req_frame->frame.result_code);
}
static int is_stucked_req_nw(const struct proto_req_nw *req_nw)
{
return is_stucked_result_code(req_nw->nw.result_code);
}
/* Dump the session reference to npu log */
static void log_session_ref(void)
{
struct session_ref *sess_ref = &(npu_proto_drv.session_ref);
struct session_ref_entry *sess_entry;
const struct npu_session *sess;
int session_cnt = 0;
u64 u_pid;
BUG_ON(!sess_ref);
npu_info("Session state list =============================\n");
list_for_each_entry(sess_entry, &sess_ref->entry_list, list) {
npu_info("Entry[%d] : UID[%u] state[%d] frame[%s] nw[%s]\n",
session_cnt, sess_entry->uid, sess_entry->s_state,
(list_empty(&sess_entry->frame_list)) ? "EMPTY" : "NOT_EMPTY",
(list_empty(&sess_entry->nw_list)) ? "EMPTY" : "NOT_EMPTY");
sess = sess_entry->session;
if (sess) {
u_pid = (u64)sess->pid;
npu_info("Entry[%d] : Session UID[%u] PID[%llu] frame_id[%u]\n",
session_cnt, sess->uid, u_pid, sess->frame_id);
} else {
npu_info("Entry[%d] : NULL Session\n", session_cnt);
}
session_cnt++;
}
npu_info("End of session state list [%d] entries =========\n", session_cnt);
}
static struct session_ref_entry *__find_session_ref(const npu_uid_t uid)
{
u32 hash_id;
struct session_ref *sess_ref = &(npu_proto_drv.session_ref);
struct session_ref_entry *sess_entry;
/* Try search on hash table */
hash_id = __find_hash_id(uid);
if (hash_id != SESS_REF_INVALID_HASH_ID) {
/* Find on hash table */
sess_entry = sess_ref->hash_table[hash_id];
goto find_entry;
}
/* No entry found on hash table -> Look up the linked list */
npu_warn("UID=%u cannot be found on hashtable. Lookup on linked list.\n", uid);
list_for_each_entry(sess_entry, &sess_ref->entry_list, list) {
if (sess_entry->uid == uid) {
/* Match */
goto find_entry;
}
}
npu_warn("UID=%u cannot be found on hash and linked list.\n", uid);
return NULL;
find_entry:
/* Now the sess_entry points valid session_ref_entry to session */
return sess_entry;
}
static struct session_ref_entry *find_session_ref_frame(const struct proto_req_frame *req_frame)
{
const struct npu_frame *frame;
BUG_ON(!req_frame);
frame = &req_frame->frame;
BUG_ON(!frame);
return __find_session_ref(frame->uid);
}
static struct session_ref_entry *find_session_ref_nw(const struct proto_req_nw *req_nw)
{
const struct npu_nw *nw;
BUG_ON(!req_nw);
nw = &req_nw->nw;
BUG_ON(!nw);
npu_trace("cmd:%u / nw->uid : %u\n", nw->cmd, nw->uid);
if (is_belong_session(nw)) {
return __find_session_ref(nw->uid);
} else {
/* REQ_NW not belong to session */
return NULL;
}
}
static enum protodrv_session_state_e get_session_ref_state_frame(const struct proto_req_frame *req_frame)
{
struct session_ref_entry *ref;
ref = find_session_ref_frame(req_frame);
if (ref == NULL) {
return SESSION_REF_STATE_INVALID;
} else {
return ref->s_state;
}
}
static enum protodrv_session_state_e get_session_ref_state_nw(const struct proto_req_nw *req_nw)
{
struct session_ref_entry *ref;
ref = find_session_ref_nw(req_nw);
if (ref == NULL) {
return SESSION_REF_STATE_INVALID;
} else {
return ref->s_state;
}
}
static struct session_ref_entry *alloc_session_ref_entry(const struct npu_session *sess)
{
struct session_ref_entry *entry;
BUG_ON(!sess);
/* TODO: Use slab allocator */
entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
if (entry == NULL) {
return NULL;
}
memset(entry, 0, sizeof(*entry));
entry->hash_id = SESS_REF_INVALID_HASH_ID;
entry->s_state = SESSION_REF_STATE_INVALID;
entry->uid = sess->uid;
entry->session = sess;
INIT_LIST_HEAD(&entry->list);
INIT_LIST_HEAD(&entry->frame_list);
INIT_LIST_HEAD(&entry->nw_list);
return entry;
}
static void free_session_ref_entry(struct session_ref_entry *sess_entry)
{
if (sess_entry == NULL) {
npu_warn("free request for null pointer.\n");
return;
}
kfree(sess_entry);
}
int register_session_ref(struct npu_session *sess)
{
u32 hash_id;
u32 add_val = 0;
struct session_ref *sess_ref = &(npu_proto_drv.session_ref);
struct session_ref_entry *sess_entry = NULL;
BUG_ON(!sess);
/* Create entry */
sess_entry = alloc_session_ref_entry(sess);
if (sess_entry == NULL) {
npu_uerr("fail in alloc_session_ref_entry\n", sess);
return -ENOMEM;
}
/* Find free entry in hash table */
hash_id = (sess->uid) % SESS_REF_HASH_TABLE_SIZE;
for (add_val = 0; add_val < SESS_REF_HASH_TABLE_SIZE; add_val += SESS_REF_HASH_MAGIC) {
hash_id = (hash_id + add_val) % SESS_REF_HASH_TABLE_SIZE;
if (sess_ref->hash_table[hash_id] == NULL) {
/* Find room */
npu_udbg("Register UID at hashtable[%u]\n", sess, hash_id);
sess_ref->hash_table[hash_id] = sess_entry;
sess_entry->hash_id = hash_id;
break;
}
}
if (add_val >= SESS_REF_HASH_TABLE_SIZE) {
/* Hash table is not available even thought it is registered */
npu_uwarn("UID cannot be stored on hash table.\n", sess);
}
list_add_tail(&sess_entry->list, &sess_ref->entry_list);
npu_udbg("session ref @%pK registered.\n", sess, sess_entry);
return 0;
}
int drop_session_ref(const npu_uid_t uid)
{
u32 hash_id;
struct session_ref *sess_ref = &(npu_proto_drv.session_ref);
struct session_ref_entry *sess_entry = NULL;
sess_entry = __find_session_ref(uid);
if (sess_entry == NULL) {
npu_err("cannot found session reference for UID [%u].\n", uid);
return -EINVAL;
}
/* Remove from hash table */
hash_id = __find_hash_id(uid);
if (hash_id != SESS_REF_INVALID_HASH_ID) {
/* Found on hash table */
BUG_ON(sess_entry != sess_ref->hash_table[hash_id]);
sess_ref->hash_table[hash_id] = NULL;
}
/* Check entries */
if (!list_empty(&sess_entry->frame_list)) {
npu_warn("frame list for UID [%u] is not empty. First is at %pK\n",
uid, list_first_entry_or_null(&sess_entry->frame_list, struct proto_req_frame, sess_ref_list));
}
if (!list_empty(&sess_entry->nw_list)) {
npu_warn("network mgmt. list for UID [%u] is not empty. First is at %pK\n",
uid, list_first_entry_or_null(&sess_entry->nw_list, struct proto_req_nw, sess_ref_list));
}
npu_info("[U%u]session ref @%pK dropped.\n", uid, sess_entry);
/* Remove from Linked list */
list_del_init(&sess_entry->list);
/* Free entry */
free_session_ref_entry(sess_entry);
return 0;
}
static int is_list_used(struct list_head *list)
{
if (!list_empty(list)) {
/* next is not self and next is not null -> Belong to other list */
if (list->next != NULL) {
return 1;
}
}
return 0;
}
int link_session_frame(struct proto_req_frame *req_frame)
{
const struct npu_frame *frame;
struct session_ref_entry *sess_entry = NULL;
BUG_ON(!req_frame);
frame = &req_frame->frame;
sess_entry = find_session_ref_frame(req_frame);
if (sess_entry == NULL) {
npu_uferr("cannot found session ref.\n", frame);
return -EINVAL;
}
if (is_list_used(&req_frame->sess_ref_list)) {
npu_uerr("frame seemed to belong other session. next pointer is %pK\n"
, frame, req_frame->sess_ref_list.next);
return -EFAULT;
}
INIT_LIST_HEAD(&req_frame->sess_ref_list);
list_add_tail(&req_frame->sess_ref_list, &sess_entry->frame_list);
npu_ufdbg("frame linked to ref@%pK.\n", frame, sess_entry);
return 0;
}
int link_session_nw(struct proto_req_nw *req_nw)
{
const struct npu_nw *nw;
struct session_ref_entry *sess_entry = NULL;
BUG_ON(!req_nw);
nw = &req_nw->nw;
if (!is_belong_session(nw)) {
/* No link necessary */
npu_uinfo("NW no need to linked to session reference.\n", nw);
return 0;
}
sess_entry = find_session_ref_nw(req_nw);
if (sess_entry == NULL) {
npu_uerr("cannot found session ref.\n", nw);
return -EINVAL;
}
if (is_list_used(&req_nw->sess_ref_list)) {
npu_uerr("network mgmt. seemed to belong other session. next pointer is %pK\n"
, nw, req_nw->sess_ref_list.next);
return -EFAULT;
}
INIT_LIST_HEAD(&req_nw->sess_ref_list);
list_add_tail(&req_nw->sess_ref_list, &sess_entry->nw_list);
npu_udbg("NW linked to ref@%pK.\n", nw, sess_entry);
return 0;
}
int unlink_session_frame(struct proto_req_frame *req_frame)
{
#ifdef UNLINK_CHECK_OWNERSHIP
/* This check is somewhat time consuming operation */
struct session_ref_entry *sess_entry = NULL;
struct proto_req_frame *iter_frame;
const struct npu_frame *frame;
BUG_ON(!req_frame);
frame = &req_frame->frame;
sess_entry = find_session_ref_frame(req_frame);
if (sess_entry == NULL) {
npu_uferr("cannot found session ref.\n", frame);
return -EINVAL;
}
list_for_each_entry(iter_frame, &sess_entry->frame_list, sess_ref_list) {
if (iter_frame == req_frame) {
goto found_match;
}
}
npu_uferr("frame does not belong to session ref@%pK.\n", frame, sess_entry);
return -EINVAL;
found_match:
#endif /* UNLINK_CHECK_OWNERSHIP */
list_del_init(&req_frame->sess_ref_list);
npu_ufdbg("frame unlinked from ref@%pK.\n", frame, sess_entry);
return 0;
}
int unlink_session_nw(struct proto_req_nw *req_nw)
{
#ifdef UNLINK_CHECK_OWNERSHIP
/* This check is somewhat time consuming operation */
struct session_ref_entry *sess_entry = NULL;
struct proto_req_nw *iter_nw;
const struct npu_nw *nw;
BUG_ON(!req_nw);
nw = &req_nw->nw;
if (!is_belong_session(nw)) {
/* No unlink necessary */
npu_uinfo("NW no need to be unlinked from session reference.\n", nw);
return 0;
}
sess_entry = find_session_ref_nw(req_nw);
if (sess_entry == NULL) {
npu_uerr("cannot found session ref.\n", nw);
return -EINVAL;
}
list_for_each_entry(iter_nw, &sess_entry->nw_list, sess_ref_list) {
if (iter_nw == req_nw) {
goto found_match;
}
}
npu_uerr("network mgmt. does not belong to session ref@%pK.\n", nw, sess_entry);
return -EINVAL;
found_match:
#endif /* UNLINK_CHECK_OWNERSHIP */
list_del_init(&req_nw->sess_ref_list);
npu_uinfo("NW unlinked from ref@%pK.\n", nw, sess_entry);
return 0;
}
/*
* Nullify src_queue(for frame) and notify_func(for nw)
* for all requests linked with session reference for specified nw
* (But do not clear notify_func for req_nw itself)
* to make no more notificatin sent for this session.
*/
static int force_clear_cb(struct proto_req_nw *req_nw)
{
struct session_ref_entry *sess_entry = NULL;
struct proto_req_nw *iter_nw;
struct proto_req_frame *iter_frame;
struct npu_nw *nw;
int cnt;
BUG_ON(!req_nw);
nw = &req_nw->nw;
sess_entry = find_session_ref_nw(req_nw);
if (sess_entry == NULL) {
npu_uerr("cannot found session ref.\n", nw);
return -ENOENT;
}
/* Clear callback from associated frame list */
cnt = 0;
list_for_each_entry(iter_frame, &sess_entry->frame_list, sess_ref_list) {
npu_ufdbg("Reset src_queue for frame in state [%d]\n",
&iter_frame->frame, iter_frame->state);
iter_frame->frame.src_queue = NULL;
cnt++;
}
npu_uinfo("Clear src_queue for frame: %d entries\n", nw, cnt);
cnt = 0;
list_for_each_entry(iter_nw, &sess_entry->nw_list, sess_ref_list) {
/* Do not clear CB for req_nw itself */
if (iter_nw != req_nw) {
npu_uinfo("Reset notify_func for nw in state [%d]\n",
&iter_nw->nw, iter_nw->state);
iter_nw->nw.notify_func = NULL;
cnt++;
}
}
npu_uinfo("Clear notify_func for nw: %d entries\n", nw, cnt);
return 0;
}
/*
* Returns 1 if the specified npu_nw object is the only linked to its associated session.
* Otherwise, returns 0.
*/
int is_last_session_ref(struct proto_req_nw *req_nw)
{
struct session_ref_entry *sess_entry = NULL;
struct proto_req_nw *iter_nw;
const struct npu_nw *nw;
int ret;
BUG_ON(!req_nw);
nw = &req_nw->nw;
sess_entry = find_session_ref_nw(req_nw);
if (sess_entry == NULL) {
npu_uwarn("cannot found session ref.\n", nw);
return 0;
}
/* No frame shall be associated */
if (!list_empty(&sess_entry->frame_list)) {
return 0; /* FALSE */
}
/* Only one frame shall be available, and it should be nw */
ret = 0;
list_for_each_entry(iter_nw, &sess_entry->nw_list, sess_ref_list) {
if (iter_nw == req_nw) {
ret = 1; /* TRUE, but need to check other entry */
} else {
return 0; /* Other entry except nw -> FALSE */
}
}
return ret;
}
/* Returns 0 if no session ref is exist.
* Otherwise, return 1
*/
static int is_session_ref_exist(void)
{
struct session_ref *sess_ref = &(npu_proto_drv.session_ref);
return !list_empty(&sess_ref->entry_list);
}
/* Iniitialize all entry's sess_ref_list on start up.
* This macro is called on npu_protodrv_open()
*/
#define NPU_PROTODRV_LSM_ENTRY_INIT(LSM_NAME, TYPE) \
do { \
TYPE *__entry; \
LSM_FOR_EACH_ENTRY_IN(LSM_NAME, FREE, __entry, \
memset(__entry, 0, sizeof(*__entry)); \
INIT_LIST_HEAD(&__entry->sess_ref_list); \
atomic_set(&__entry->ts.curr_entry, 0); \
) \
} while (0)
/*******************************************************************************
* NPU data handling functions
*/
static inline int check_time(const struct npu_timestamp *tstamp, const u64 timeout_ns, const u64 now_ns)
{
u64 diff;
const struct npu_timestamp_entry *ts_entry;
if (timeout_ns == 0) {
return 0; /* No timeout defined */
}
ts_entry = &(tstamp->hist[atomic_read(&tstamp->curr_entry)]);
diff = now_ns - ts_entry->enter;
return diff > timeout_ns;
}
/*
* Returns true if request's ts.curr exceedes specified timeout.
* (PROTO_REQ_PTR can be both proto_req_frame* or proto_req_nw*)
* Otherwise, return false.
*/
#define is_timedout(PROTO_REQ_PTR, timeout_ns, now_ns) \
({ \
typeof(PROTO_REQ_PTR) __p = (PROTO_REQ_PTR); \
BUG_ON(!__p); \
check_time(&__p->ts, (timeout_ns), (now_ns)); \
}) \
/*
* Generate a unique npu_req_id and return it.
* Current implementation implies the npu_req_id_gen is
* signed integer type.
*/
static npu_req_id_t get_next_npu_req_id(void)
{
/* handling roll over
The initial value is NPU_REQ_ID_INITIAL - 1 because
the return value is made by current counter + 1 */
atomic_cmpxchg(&npu_proto_drv.req_id_gen, NPU_REQ_ID_ROLLOVER, (NPU_REQ_ID_INITIAL - 1));
/* return next count */
return (npu_req_id_t)atomic_inc_return(&npu_proto_drv.req_id_gen);
}
/*******************************************************************************
* Timestamp handling functions
*/
static void __update_npu_timestamp(const lsm_list_type_e old_state, const lsm_list_type_e new_state, struct npu_timestamp *ts)
{
int ts_entry_idx_curr, ts_entry_idx_next;
s64 now = get_time_ns();
BUG_ON(!ts);
do {
ts_entry_idx_curr = atomic_read(&ts->curr_entry);
ts_entry_idx_next = (ts_entry_idx_curr + 1) % LSM_ELEM_STATUS_TRACK_LEN;
} while (atomic_cmpxchg(&ts->curr_entry, ts_entry_idx_curr, ts_entry_idx_next) != ts_entry_idx_curr);
if (ts->hist[ts_entry_idx_curr].state != old_state) {
npu_warn("history index(%d)'s state(%d) does not match with last state (%d).",
ts_entry_idx_curr, ts->hist[ts_entry_idx_curr].state, old_state);
}
ts->hist[ts_entry_idx_curr].leave = ts->hist[ts_entry_idx_next].enter = now;
ts->hist[ts_entry_idx_next].state = new_state;
}
/* Print timestamp history, until it gets FREE state */
static char *__print_npu_timestamp(struct npu_timestamp *ts, char *buf, const size_t len)
{
s64 now = get_time_ns();
int i, ret;
int idx, tmp_idx;
int idx_start;
int buf_idx = 0;
struct timespec ts_time;
BUG_ON(!ts);
idx_start = atomic_read(&ts->curr_entry);
if (idx_start < 0 || idx_start >= LSM_ELEM_STATUS_TRACK_LEN) {
npu_err("invalid curr_entry (%d)\n", idx_start);
ret = scnprintf(buf + buf_idx, len - buf_idx, "<Invalid curr_entry (%d)>", idx_start);
buf_idx += ret;
goto ret_exit;
}
/* Back track to start(FREE) state */
idx = idx_start;
while (ts->hist[idx].state != FREE) {
tmp_idx = idx - 1;
if (tmp_idx < 0)
tmp_idx = LSM_ELEM_STATUS_TRACK_LEN - 1;
if (tmp_idx == idx_start) {
/* Not enough history to FREE */
ret = scnprintf(buf + buf_idx, len - buf_idx, "<Not enough history>");
buf_idx += ret;
break;
}
idx = tmp_idx;
}
/* printout history */
i = idx;
while (1) {
ts_time = ns_to_timespec(ts->hist[i].enter);
ret = scnprintf(buf + buf_idx, len - buf_idx, "[%s](%ld.%09ld) ",
LSM_STATE_NAMES[ts->hist[i].state], ts_time.tv_sec, ts_time.tv_nsec);
if (!ret)
break;
buf_idx += ret;
if (i == idx_start)
break; /* This is normal termination condition */
i = (i + 1) % LSM_ELEM_STATUS_TRACK_LEN;
}
ts_time = ns_to_timespec(now);
scnprintf(buf + buf_idx, len - buf_idx, "[DONE](%ld.%09ld) ",
ts_time.tv_sec, ts_time.tv_nsec);
ret_exit:
buf[len - 1] = '\0';
return buf;
}
int session_fault_listener(void)
{
#define mn_state_f(x) #x
struct session_ref_entry *sess_entry = NULL;
struct session_ref *sess_ref = &(npu_proto_drv.session_ref);
struct proto_req_frame *iter_frame;
struct proto_req_nw *iter_nw;
struct npu_frame *frame;
struct npu_nw *nw;
struct npu_session *session;
struct av_info *IFM_info;
struct av_info *OFM_info;
struct addr_info *IFM_addr;
struct addr_info *OFM_addr;
struct addr_info *IMB_addr;
struct addr_info *WGT_addr;
int qbuf_IOFM_idx;
int dqbuf_IOFM_idx;
size_t IFM_cnt;
size_t OFM_cnt;
size_t IMB_cnt;
size_t WGT_cnt;
int session_processing_cnt = 0;
size_t idx0 = 0;
size_t idx1 = 0;
size_t idx2 = 0;
int session_chk[NPU_MAX_SESSION] = {0};
u32 session_uid;
u32 req_cnt = 0;
idx2 = 0;
list_for_each_entry(sess_entry, &sess_ref->entry_list, list) {
list_for_each_entry(iter_nw, &sess_entry->nw_list, sess_ref_list) {
if (iter_nw->state == PROCESSING)
req_cnt++;
}
list_for_each_entry(iter_frame, &sess_entry->frame_list, sess_ref_list) {
if (iter_frame->state == PROCESSING)
req_cnt++;
}
}
npu_info("<Outstanding requeset [%u]>\n", req_cnt);
npu_info("[Index] [Type] [KVA] [DVA] [Size]\n");
idx2 = 0;
list_for_each_entry(sess_entry, &sess_ref->entry_list, list) {
list_for_each_entry(iter_nw, &sess_entry->nw_list, sess_ref_list) {
if (iter_nw->state == PROCESSING) {
nw = &iter_nw->nw;
npu_info("[%zu] Type:NW UID:%u ReqID:%u FrameID:N/A CMD:%d MsgID:%d\n",
idx2, nw->uid, nw->npu_req_id, nw->cmd, nw->msgid);
idx2++;
}
}
list_for_each_entry(iter_frame, &sess_entry->frame_list, sess_ref_list) {
if (iter_frame->state == PROCESSING) {
frame = &iter_frame->frame;
npu_info("[%zu] Type:FRAME UID:%u ReqID:%u FrameID:%u CMD:%d MsgID:%d\n",
idx2, frame->uid, frame->npu_req_id, frame->frame_id, frame->cmd, frame->msgid);
IFM_info = &frame->IFM_info[0];
OFM_info = &frame->OFM_info[0];
IFM_addr = IFM_info->addr_info;
OFM_addr = OFM_info->addr_info;
IFM_cnt = IFM_info->address_vector_cnt;
OFM_cnt = OFM_info->address_vector_cnt;
for (idx1 = 0; idx1 < IFM_cnt; idx1++) {
npu_info("- %u %d %pK %pad %zu\n",
(IFM_addr + idx1)->av_index, MEMORY_TYPE_IN_FMAP,
(IFM_addr + idx1)->vaddr, &((IFM_addr + idx1)->daddr),
(IFM_addr + idx1)->size);
}
for (idx1 = 0; idx1 < OFM_cnt; idx1++) {
npu_info("- %u %d %pK %pad %zu\n",
(OFM_addr + idx1)->av_index, MEMORY_TYPE_OT_FMAP,
(OFM_addr + idx1)->vaddr, &((OFM_addr + idx1)->daddr),
(OFM_addr + idx1)->size);
}
idx2++;
}
}
}
idx2 = 0;
list_for_each_entry(sess_entry, &sess_ref->entry_list, list) {
list_for_each_entry(iter_frame, &sess_entry->frame_list, sess_ref_list) {
if (iter_frame->state == PROCESSING) {
frame = &iter_frame->frame;
session = frame->session;
if (!session)
continue;
session_uid = session->uid;
session_uid++;
for (idx2 = 0; idx2 < NPU_MAX_SESSION; idx2++) {
if (session_chk[idx2] != session_uid)
session_chk[idx2] = session_uid;
}
}
}
}
for (idx2 = 0; idx2 < NPU_MAX_SESSION; idx2++) {
if (session_chk[idx2] == 0) {
session_processing_cnt = idx2;
break;
}