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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Samsung Exynos SoC series dsp driver
*
* Copyright (c) 2019 Samsung Electronics Co., Ltd.
* http://www.samsung.com/
*/
#include "dsp-log.h"
#include "dsp-util.h"
#include "hardware/dsp-system.h"
#include "dsp-task.h"
static void __dsp_task_set_ready(struct dsp_task_manager *tmgr,
struct dsp_task *task)
{
dsp_enter();
task->state = DSP_TASK_STATE_READY;
list_add_tail(&task->state_list, &tmgr->ready_list);
tmgr->ready_count++;
dsp_leave();
}
static int __dsp_task_del_ready(struct dsp_task_manager *tmgr,
struct dsp_task *task)
{
dsp_enter();
if (task->state != DSP_TASK_STATE_READY) {
dsp_warn("task state(%u) is not ready(%u)\n",
task->state, task->id);
return -EINVAL;
}
if (!tmgr->ready_count) {
dsp_warn("task manager doesn't have ready task(%u)\n",
task->id);
return -EINVAL;
}
list_del(&task->state_list);
tmgr->ready_count--;
dsp_leave();
return 0;
}
static void __dsp_task_set_process(struct dsp_task_manager *tmgr,
struct dsp_task *task)
{
dsp_enter();
task->state = DSP_TASK_STATE_PROCESS;
list_add_tail(&task->state_list, &tmgr->process_list);
tmgr->process_count++;
dsp_leave();
}
static int __dsp_task_del_process(struct dsp_task_manager *tmgr,
struct dsp_task *task)
{
dsp_enter();
if (task->state != DSP_TASK_STATE_PROCESS) {
dsp_warn("task state(%u) is not process(%u)\n",
task->state, task->id);
return -EINVAL;
}
if (!tmgr->process_count) {
dsp_warn("task manager doesn't have process task(%u)\n",
task->id);
return -EINVAL;
}
list_del(&task->state_list);
tmgr->process_count--;
dsp_leave();
return 0;
}
static void __dsp_task_set_complete(struct dsp_task_manager *tmgr,
struct dsp_task *task)
{
dsp_enter();
task->state = DSP_TASK_STATE_COMPLETE;
list_add_tail(&task->state_list, &tmgr->complete_list);
tmgr->complete_count++;
dsp_leave();
}
static int __dsp_task_del_complete(struct dsp_task_manager *tmgr,
struct dsp_task *task)
{
dsp_enter();
if (task->state != DSP_TASK_STATE_COMPLETE) {
dsp_warn("task state(%u) is not complete(%u)\n",
task->state, task->id);
return -EINVAL;
}
if (!tmgr->complete_count) {
dsp_warn("task manager doesn't have complete task(%u)\n",
task->id);
return -EINVAL;
}
list_del(&task->state_list);
tmgr->complete_count--;
dsp_leave();
return 0;
}
int dsp_task_trans_ready_to_process(struct dsp_task *task)
{
int ret;
struct dsp_task_manager *tmgr = task->owner;
dsp_enter();
if (tmgr->block && !task->force) {
ret = -ENOSTR;
dsp_warn("task manager is blocked\n");
goto p_err;
}
ret = __dsp_task_del_ready(tmgr, task);
if (ret)
goto p_err;
__dsp_task_set_process(tmgr, task);
dsp_leave();
return 0;
p_err:
return ret;
}
int dsp_task_trans_ready_to_complete(struct dsp_task *task)
{
int ret;
struct dsp_task_manager *tmgr = task->owner;
dsp_enter();
ret = __dsp_task_del_ready(tmgr, task);
if (ret)
goto p_err;
__dsp_task_set_complete(tmgr, task);
dsp_leave();
return 0;
p_err:
return ret;
}
int dsp_task_trans_process_to_complete(struct dsp_task *task)
{
int ret;
struct dsp_task_manager *tmgr = task->owner;
dsp_enter();
ret = __dsp_task_del_process(tmgr, task);
if (ret)
goto p_err;
__dsp_task_set_complete(tmgr, task);
dsp_leave();
return 0;
p_err:
return ret;
}
static int __dsp_task_destroy_state(struct dsp_task_manager *tmgr,
struct dsp_task *task)
{
int ret;
dsp_enter();
switch (task->state) {
case DSP_TASK_STATE_READY:
ret = __dsp_task_del_ready(tmgr, task);
break;
case DSP_TASK_STATE_PROCESS:
ret = __dsp_task_del_process(tmgr, task);
break;
case DSP_TASK_STATE_COMPLETE:
ret = __dsp_task_del_complete(tmgr, task);
break;
default:
ret = -EINVAL;
dsp_warn("task state(%u) is invalid(%u)\n",
task->state, task->id);
}
dsp_leave();
return ret;
}
int dsp_task_trans_any_to_complete(struct dsp_task *task)
{
int ret;
struct dsp_task_manager *tmgr = task->owner;
dsp_enter();
if (task->state != DSP_TASK_STATE_COMPLETE) {
ret = __dsp_task_destroy_state(tmgr, task);
__dsp_task_set_complete(tmgr, task);
} else {
ret = 0;
}
dsp_leave();
return ret;
}
struct dsp_task *dsp_task_get_process_by_id(struct dsp_task_manager *tmgr,
unsigned int id)
{
struct dsp_task *task, *temp;
dsp_enter();
list_for_each_entry_safe(task, temp, &tmgr->process_list, state_list) {
if (task->id == id)
return task;
}
dsp_leave();
return NULL;
}
struct dsp_task *dsp_task_get_any_by_id(struct dsp_task_manager *tmgr,
unsigned int id)
{
struct dsp_task *task, *temp;
dsp_enter();
list_for_each_entry_safe(task, temp, &tmgr->total_list, total_list) {
if (task->id == id)
return task;
}
dsp_leave();
return NULL;
}
struct dsp_task *dsp_task_create(struct dsp_task_manager *tmgr, bool force)
{
int ret;
unsigned long flags;
struct dsp_task *task;
dsp_enter();
task = kzalloc(sizeof(*task), GFP_KERNEL);
if (!task) {
ret = -ENOMEM;
dsp_err("Failed to allocate task\n");
return ERR_PTR(ret);
}
task->owner = tmgr;
task->force = force;
spin_lock_irqsave(&tmgr->slock, flags);
if (tmgr->block && !force) {
ret = -ENOSTR;
dsp_warn("task manager is blocked\n");
goto p_err;
}
task->id = dsp_util_bitmap_set_region(&tmgr->task_map, 1);
if (task->id < 0) {
ret = task->id;
dsp_err("Failed to allocate task bitmap(%d)\n", ret);
goto p_err;
}
list_add_tail(&task->total_list, &tmgr->total_list);
tmgr->total_count++;
__dsp_task_set_ready(tmgr, task);
spin_unlock_irqrestore(&tmgr->slock, flags);
dsp_leave();
return task;
p_err:
spin_unlock_irqrestore(&tmgr->slock, flags);
kfree(task);
return ERR_PTR(ret);
}
static void __dsp_task_destroy(struct dsp_task_manager *tmgr,
struct dsp_task *task)
{
unsigned long flags;
dsp_enter();
spin_lock_irqsave(&tmgr->slock, flags);
__dsp_task_destroy_state(tmgr, task);
tmgr->total_count--;
list_del(&task->total_list);
dsp_util_bitmap_clear_region(&tmgr->task_map, task->id, 1);
spin_unlock_irqrestore(&tmgr->slock, flags);
kfree(task);
dsp_leave();
}
void dsp_task_destroy(struct dsp_task *task)
{
__dsp_task_destroy(task->owner, task);
}
void dsp_task_manager_set_block_mode(struct dsp_task_manager *tmgr, bool block)
{
dsp_enter();
tmgr->block = block;
dsp_leave();
}
int dsp_task_manager_flush_process(struct dsp_task_manager *tmgr, int result)
{
struct dsp_task *task, *temp;
dsp_enter();
list_for_each_entry_safe(task, temp, &tmgr->process_list, state_list) {
dsp_warn("process task[%u] is flushed(count:%u)\n",
task->id, tmgr->process_count);
task->result = result;
dsp_task_trans_process_to_complete(task);
}
dsp_leave();
return 0;
}
void dsp_task_manager_dump_count(struct dsp_task_manager *tmgr)
{
dsp_enter();
dsp_notice("[DSP_TASK_DUMP_COUNT] %u/%u/%u/%u/%u/%u\n",
tmgr->total_count, tmgr->ready_count,
tmgr->process_count, tmgr->complete_count,
tmgr->normal_count, tmgr->error_count);
dsp_leave();
}
int dsp_task_manager_probe(struct dsp_system *sys)
{
int ret;
struct dsp_task_manager *tmgr;
dsp_enter();
tmgr = &sys->task_manager;
INIT_LIST_HEAD(&tmgr->total_list);
tmgr->total_count = 0;
INIT_LIST_HEAD(&tmgr->ready_list);
tmgr->ready_count = 0;
INIT_LIST_HEAD(&tmgr->process_list);
tmgr->process_count = 0;
INIT_LIST_HEAD(&tmgr->complete_list);
tmgr->complete_count = 0;
tmgr->normal_count = 0;
tmgr->error_count = 0;
spin_lock_init(&tmgr->slock);
init_waitqueue_head(&tmgr->done_wq);
tmgr->block = false;
ret = dsp_util_bitmap_init(&tmgr->task_map, "task_bitmap",
DSP_TASK_MAX_COUNT);
if (ret)
goto p_err;
dsp_leave();
return 0;
p_err:
return ret;
}
void dsp_task_manager_remove(struct dsp_task_manager *tmgr)
{
struct dsp_task *task, *temp;
dsp_enter();
list_for_each_entry_safe(task, temp, &tmgr->total_list,
total_list) {
dsp_warn("task[%u] is destroyed(count:%u)\n",
task->id, tmgr->total_count);
__dsp_task_destroy(tmgr, task);
}
dsp_util_bitmap_deinit(&tmgr->task_map);
dsp_leave();
}
|