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
|
/*
* 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/list.h>
#include <linux/kthread.h>
#include <linux/atomic.h>
#include <linux/device.h>
#include <linux/slab.h>
#include "npu-log.h"
#include "npu-util-liststatemgr.h"
#include "npu-util-autosleepthr.h"
const char *default_name = "Thr??";
#define NL_NAME(name) ((name == NULL) ? (default_name) : (name))
const char *LSM_STATE_NAMES[LSM_LIST_TYPE_INVALID + 1]
= {"FREE", "REQUESTED", "PROCESSING", "COMPLETED", "STUCKED", "INVALID"};
/* Helper function: Initialize entry lists */
void __lsm_reset_list(struct lsm_internal_data *lsm)
{
int i;
BUG_ON(!lsm);
/* Initialization of INVALID list is not really necessary,
but doing so for safety */
for (i = 0; i < LSM_LIST_TYPE_INVALID; i++) {
INIT_LIST_HEAD(&(lsm->list_for_state[i]));
}
}
void __lsm_destroy(struct lsm_internal_data *lsm)
{
int ret = 0;
npu_info("start in __lsm_destroy[%s]\n", NL_NAME(lsm->name));
BUG_ON(!lsm);
if (lsm->is_ast_enabled) {
/* Terminate the worker thread */
ret = auto_sleep_thread_terminate(&(lsm->ast_worker));
/* ret is usually the return value of thread function.
so non-zero value does not mean error condition */
npu_dbg("auto_sleep_thread_terminate on LSM [%s]. returns [%d]\n",
NL_NAME(lsm->name), ret);
}
npu_info("complete in __lsm_destroy(%s)\n", NL_NAME(lsm->name));
return;
}
struct lsm_base_entry *__lsm_get_entry(struct lsm_internal_data *lsm,
lsm_list_type_e type)
{
/* The list for specified type */
struct lsm_base_entry *ret_entry;
struct list_head *target_list_head;
BUG_ON(!lsm);
BUG_ON(type < 0);
BUG_ON(type >= LSM_LIST_TYPE_INVALID);
target_list_head = &(lsm->list_for_state[type]);
/* Check whether the list is empty */
if (list_empty(target_list_head)) {
npu_warn("LSM[%s]. empty on type[%d].\n",
NL_NAME(lsm->name), type);
return NULL;
} else {
/* Return the first(Oldest) element */
ret_entry = list_first_entry(target_list_head,
struct lsm_base_entry, list);
list_del_init(&(ret_entry->list));
return ret_entry;
}
}
/* Helper function - Put entry in sorted list */
static int __put_entry_sorted(struct list_head *target_list_head,
struct lsm_base_entry *new_entry,
lsm_comparator_t compare)
{
struct list_head *iter;
struct list_head *prev = target_list_head;
struct lsm_base_entry *cmp_entry;
int index = 0;
list_for_each(iter, target_list_head)
{
cmp_entry = list_entry(iter, struct lsm_base_entry, list);
if (compare(&(new_entry->data_place), &(cmp_entry->data_place))) {
/* The entry should be added before iter */
list_add(&(new_entry->list), prev);
return index;
}
/* Update prev */
prev = iter;
index++;
}
/* Reach at the end of list - Add it to tail */
list_add_tail(&(new_entry->list), target_list_head);
return index;
}
/* Helper function - Put entry in unsorted list */
static int __put_entry_unsorted(struct list_head *target_list_head,
struct lsm_base_entry *new_entry)
{
list_add_tail(&(new_entry->list), target_list_head);
return 0;
}
int __lsm_put_entry(struct lsm_internal_data *lsm, lsm_list_type_e type,
struct lsm_base_entry *new_entry)
{
int ret = 0;
struct list_head *target_list_head;
BUG_ON(!new_entry);
BUG_ON(!lsm);
BUG_ON(type < 0);
BUG_ON(type >= LSM_LIST_TYPE_INVALID);
target_list_head = &(lsm->list_for_state[type]);
new_entry->state = type;
/* Check whether the entry is properly detached from list */
BUG_ON(!list_empty(&(new_entry->list)));
/* Call helper, based on compare function availability */
if (lsm->compare != NULL) {
ret = __put_entry_sorted(target_list_head, new_entry,
(lsm_comparator_t)lsm->compare);
} else {
ret = __put_entry_unsorted(target_list_head, new_entry);
}
/* Call hook on success */
if (ret == 0)
if (lsm->put_hook) {
lsm->put_hook(type, &new_entry->data_place);
}
return ret;
}
int __lsm_move_entry(struct lsm_internal_data *lsm, lsm_list_type_e to,
struct lsm_base_entry *entry)
{
BUG_ON(!lsm);
BUG_ON(to < 0);
BUG_ON(to >= LSM_LIST_TYPE_INVALID);
BUG_ON(!entry);
/* Remove from the current list */
list_del_init(&(entry->list));
/* Put it to target list */
return __lsm_put_entry(lsm, to, entry);
}
void __lsm_send_signal(struct lsm_internal_data *lsm)
{
BUG_ON(!lsm);
auto_sleep_thread_signal(&lsm->ast_worker);
}
void __lsm_set_hook(struct lsm_internal_data *lsm, lsm_hook_t put_hook)
{
BUG_ON(!lsm);
lsm->put_hook = put_hook;
}
/* Unit test */
#ifdef CONFIG_VISION_UNITTEST
#define IDIOT_TESTCASE_IMPL "../npu/npu-util-liststatemgr.idiot"
#include "idiot-def.h"
#endif
|