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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Samsung Exynos SoC series dsp driver
*
* Copyright (c) 2019 Samsung Electronics Co., Ltd.
* http://www.samsung.com/
*/
#include <linux/slab.h>
#include <linux/uaccess.h>
#include "dsp-log.h"
#include "dsp-util.h"
#include "dsp-device.h"
#include "hardware/dsp-mailbox.h"
#include "dsp-common-type.h"
#include "dsp-control.h"
#include "dsp-context.h"
static int dsp_context_boot(struct dsp_context *dctx, struct dsp_ioc_boot *args)
{
int ret;
struct dsp_core *core;
dsp_enter();
dsp_dbg("boot start\n");
core = dctx->core;
ret = dsp_graph_manager_open(&core->graph_manager);
if (ret)
goto p_err_graph;
mutex_lock(&dctx->lock);
if (dctx->boot_count + 1 < dctx->boot_count) {
ret = -EINVAL;
dsp_err("boot count is overflowed\n");
goto p_err_count;
}
ret = dsp_device_start(core->dspdev, args->pm_level);
if (ret)
goto p_err_device;
dctx->boot_count++;
mutex_unlock(&dctx->lock);
dsp_dbg("boot end\n");
dsp_leave();
return 0;
p_err_device:
p_err_count:
mutex_unlock(&dctx->lock);
dsp_graph_manager_close(&core->graph_manager, 1);
p_err_graph:
return ret;
}
static int __dsp_context_get_graph_info(struct dsp_context *dctx,
struct dsp_ioc_load_graph *load, void *ginfo, void *kernel_name)
{
int ret;
dsp_enter();
if (copy_from_user(ginfo, (void __user *)load->param_addr,
load->param_size)) {
ret = -EFAULT;
dsp_err("Failed to copy from user param_addr(%d)\n", ret);
goto p_err_copy;
}
if (copy_from_user(kernel_name, (void __user *)load->kernel_addr,
load->kernel_size)) {
ret = -EFAULT;
dsp_err("Failed to copy from user kernel_addr(%d)\n", ret);
goto p_err_copy;
}
dsp_leave();
return 0;
p_err_copy:
return ret;
}
static void __dsp_context_put_graph_info(struct dsp_context *dctx, void *ginfo)
{
dsp_enter();
dsp_leave();
}
static int dsp_context_load_graph(struct dsp_context *dctx,
struct dsp_ioc_load_graph *args)
{
int ret;
unsigned int booted;
struct dsp_system *sys;
void *kernel_name;
struct dsp_mailbox_pool *pool;
struct dsp_common_graph_info_v1 *ginfo1;
struct dsp_common_graph_info_v2 *ginfo2;
struct dsp_common_graph_info_v3 *ginfo3;
struct dsp_graph *graph;
unsigned int version;
dsp_enter();
dsp_dbg("load start\n");
mutex_lock(&dctx->lock);
booted = dctx->boot_count;
mutex_unlock(&dctx->lock);
if (!booted) {
ret = -EINVAL;
dsp_err("device is not booted\n");
goto p_err;
}
sys = &dctx->core->dspdev->system;
version = args->version;
if (!args->kernel_size) {
ret = -EINVAL;
dsp_err("size for kernel_name is invalid\n");
goto p_err_name;
}
kernel_name = kmalloc(args->kernel_size, GFP_KERNEL);
if (!kernel_name) {
ret = -ENOMEM;
dsp_err("Failed to allocate kernel_name(%u)\n",
args->kernel_size);
goto p_err_name;
}
pool = dsp_mailbox_alloc_pool(&sys->mailbox, args->param_size);
if (IS_ERR(pool)) {
ret = PTR_ERR(pool);
goto p_err_pool;
}
pool->pm_qos = args->request_qos;
if (version == DSP_IOC_V1) {
ginfo1 = pool->kva;
ret = __dsp_context_get_graph_info(dctx, args, ginfo1,
kernel_name);
if (ret)
goto p_err_info;
SET_COMMON_CONTEXT_ID(&ginfo1->global_id, dctx->id);
} else if (version == DSP_IOC_V2) {
ginfo2 = pool->kva;
ret = __dsp_context_get_graph_info(dctx, args, ginfo2,
kernel_name);
if (ret)
goto p_err_info;
SET_COMMON_CONTEXT_ID(&ginfo2->global_id, dctx->id);
} else if (version == DSP_IOC_V3) {
ginfo3 = pool->kva;
ret = __dsp_context_get_graph_info(dctx, args, ginfo3,
kernel_name);
if (ret)
goto p_err_info;
SET_COMMON_CONTEXT_ID(&ginfo3->global_id, dctx->id);
} else {
ret = -EINVAL;
dsp_err("Failed to load graph due to invalid version(%u)\n",
version);
goto p_err_info;
}
graph = dsp_graph_load(&dctx->core->graph_manager, pool,
kernel_name, version);
if (IS_ERR(graph)) {
ret = PTR_ERR(graph);
goto p_err_graph;
}
args->timestamp[0] = pool->time.start;
args->timestamp[1] = pool->time.end;
if (version == DSP_IOC_V1)
__dsp_context_put_graph_info(dctx, ginfo1);
else if (version == DSP_IOC_V2)
__dsp_context_put_graph_info(dctx, ginfo2);
else if (version == DSP_IOC_V3)
__dsp_context_put_graph_info(dctx, ginfo3);
dsp_dbg("load end\n");
dsp_leave();
return 0;
p_err_graph:
if (version == DSP_IOC_V1)
__dsp_context_put_graph_info(dctx, ginfo1);
else if (version == DSP_IOC_V2)
__dsp_context_put_graph_info(dctx, ginfo2);
else if (version == DSP_IOC_V3)
__dsp_context_put_graph_info(dctx, ginfo3);
p_err_info:
dsp_mailbox_free_pool(pool);
p_err_pool:
kfree(kernel_name);
p_err_name:
p_err:
return ret;
}
static int dsp_context_unload_graph(struct dsp_context *dctx,
struct dsp_ioc_unload_graph *args)
{
int ret;
unsigned int booted;
struct dsp_system *sys;
struct dsp_graph *graph;
struct dsp_mailbox_pool *pool;
unsigned int *global_id;
dsp_enter();
dsp_dbg("unload start\n");
mutex_lock(&dctx->lock);
booted = dctx->boot_count;
mutex_unlock(&dctx->lock);
if (!booted) {
ret = -EINVAL;
dsp_err("device is not booted\n");
goto p_err;
}
sys = &dctx->core->dspdev->system;
SET_COMMON_CONTEXT_ID(&args->global_id, dctx->id);
graph = dsp_graph_get(&dctx->core->graph_manager, args->global_id);
if (!graph) {
ret = -EINVAL;
dsp_err("graph is not loaded(%x)\n", args->global_id);
goto p_err;
}
pool = dsp_mailbox_alloc_pool(&sys->mailbox, sizeof(args->global_id));
if (IS_ERR(pool)) {
ret = PTR_ERR(pool);
goto p_err;
}
pool->pm_qos = args->request_qos;
global_id = pool->kva;
*global_id = args->global_id;
dsp_graph_unload(graph, pool);
args->timestamp[0] = pool->time.start;
args->timestamp[1] = pool->time.end;
dsp_mailbox_free_pool(pool);
dsp_dbg("unload end\n");
dsp_leave();
return 0;
p_err:
return ret;
}
static int __dsp_context_get_execute_info(struct dsp_context *dctx,
struct dsp_ioc_execute_msg *execute, void *einfo)
{
int ret;
unsigned int size;
dsp_enter();
size = execute->size;
if (!size) {
ret = -EINVAL;
dsp_err("size for execute_msg is invalid(%u)\n", size);
goto p_err;
}
if (copy_from_user(einfo, (void __user *)execute->addr, size)) {
ret = -EFAULT;
dsp_err("Failed to copy from user addr(%d)\n", ret);
goto p_err_copy;
}
dsp_leave();
return 0;
p_err_copy:
p_err:
return ret;
}
static void __dsp_context_put_execute_info(struct dsp_context *dctx, void *info)
{
dsp_enter();
dsp_leave();
}
static int dsp_context_execute_msg(struct dsp_context *dctx,
struct dsp_ioc_execute_msg *args)
{
int ret;
unsigned int booted;
struct dsp_system *sys;
struct dsp_mailbox_pool *pool;
struct dsp_common_execute_info_v1 *einfo1;
struct dsp_common_execute_info_v2 *einfo2;
struct dsp_common_execute_info_v3 *einfo3;
struct dsp_graph *graph;
unsigned int version;
unsigned int einfo_global_id;
dsp_enter();
dsp_dbg("execute start\n");
mutex_lock(&dctx->lock);
booted = dctx->boot_count;
mutex_unlock(&dctx->lock);
if (!booted) {
ret = -EINVAL;
dsp_err("device is not booted\n");
goto p_err;
}
sys = &dctx->core->dspdev->system;
version = args->version;
pool = dsp_mailbox_alloc_pool(&sys->mailbox, args->size);
if (IS_ERR(pool)) {
ret = PTR_ERR(pool);
goto p_err_pool;
}
pool->pm_qos = args->request_qos;
if (version == DSP_IOC_V1) {
einfo1 = pool->kva;
ret = __dsp_context_get_execute_info(dctx, args, einfo1);
if (ret)
goto p_err_info;
SET_COMMON_CONTEXT_ID(&einfo1->global_id, dctx->id);
einfo_global_id = einfo1->global_id;
} else if (version == DSP_IOC_V2) {
einfo2 = pool->kva;
ret = __dsp_context_get_execute_info(dctx, args, einfo2);
if (ret)
goto p_err_info;
SET_COMMON_CONTEXT_ID(&einfo2->global_id, dctx->id);
einfo_global_id = einfo2->global_id;
} else if (version == DSP_IOC_V3) {
einfo3 = pool->kva;
ret = __dsp_context_get_execute_info(dctx, args, einfo3);
if (ret)
goto p_err_info;
SET_COMMON_CONTEXT_ID(&einfo3->global_id, dctx->id);
einfo_global_id = einfo3->global_id;
} else {
ret = -EINVAL;
dsp_err("Failed to execute msg due to invalid version(%u)\n",
version);
goto p_err_info;
}
graph = dsp_graph_get(&dctx->core->graph_manager, einfo_global_id);
if (!graph) {
ret = -EINVAL;
dsp_err("graph is not loaded(%x)\n", einfo_global_id);
goto p_err_graph_get;
}
if (graph->version != version) {
ret = -EINVAL;
dsp_err("graph has different message_version(%u/%u)\n",
graph->version, version);
goto p_err_graph_version;
}
ret = dsp_graph_execute(graph, pool);
if (ret)
goto p_err_graph_execute;
args->timestamp[0] = pool->time.start;
args->timestamp[1] = pool->time.end;
if (version == DSP_IOC_V1)
__dsp_context_put_execute_info(dctx, einfo1);
else if (version == DSP_IOC_V2)
__dsp_context_put_execute_info(dctx, einfo2);
else if (version == DSP_IOC_V3)
__dsp_context_put_execute_info(dctx, einfo3);
dsp_mailbox_free_pool(pool);
dsp_dbg("execute end\n");
dsp_leave();
return 0;
p_err_graph_execute:
p_err_graph_version:
p_err_graph_get:
if (version == DSP_IOC_V1)
__dsp_context_put_execute_info(dctx, einfo1);
else if (version == DSP_IOC_V2)
__dsp_context_put_execute_info(dctx, einfo2);
else if (version == DSP_IOC_V3)
__dsp_context_put_execute_info(dctx, einfo3);
p_err_info:
dsp_mailbox_free_pool(pool);
p_err_pool:
p_err:
return ret;
}
static int __dsp_context_get_control(struct dsp_context *dctx,
struct dsp_ioc_control *control, union dsp_control *cmd)
{
int ret;
dsp_enter();
dsp_dbg("control(%u/%u/%#lx)\n",
control->control_id, control->size, control->addr);
switch (control->control_id) {
case DSP_CONTROL_ENABLE_DVFS:
case DSP_CONTROL_DISABLE_DVFS:
if (control->size != sizeof(cmd->dvfs.pm_qos)) {
ret = -EINVAL;
dsp_err("user cmd size is invalid(%u/%zu)\n",
control->size,
sizeof(cmd->dvfs.pm_qos));
goto p_err;
}
if (copy_from_user(&cmd->dvfs.pm_qos,
(void __user *)control->addr,
control->size)) {
ret = -EFAULT;
dsp_err("Failed to copy from user cmd(%u/%d)\n",
control->control_id, ret);
goto p_err;
}
break;
case DSP_CONTROL_ENABLE_BOOST:
case DSP_CONTROL_DISABLE_BOOST:
break;
case DSP_CONTROL_REQUEST_MO:
case DSP_CONTROL_RELEASE_MO:
if (!control->size || control->size >= SCENARIO_NAME_MAX) {
ret = -EINVAL;
dsp_err("user cmd size is invalid(%u/%u)\n",
control->size, SCENARIO_NAME_MAX);
goto p_err;
}
if (copy_from_user(cmd->mo.scenario_name,
(void __user *)control->addr,
control->size)) {
ret = -EFAULT;
dsp_err("Failed to copy from user cmd(%u/%d)\n",
control->control_id, ret);
goto p_err;
}
cmd->mo.scenario_name[control->size] = '\0';
break;
default:
ret = -EINVAL;
dsp_err("control id is invalid(%u)\n", control->control_id);
goto p_err;
}
dsp_leave();
return 0;
p_err:
return ret;
}
static int dsp_context_control(struct dsp_context *dctx,
struct dsp_ioc_control *args)
{
int ret;
unsigned int booted;
struct dsp_system *sys;
union dsp_control cmd;
dsp_enter();
dsp_dbg("control start\n");
mutex_lock(&dctx->lock);
booted = dctx->boot_count;
mutex_unlock(&dctx->lock);
if (!booted) {
ret = -EINVAL;
dsp_err("device is not booted\n");
goto p_err;
}
sys = &dctx->core->dspdev->system;
ret = __dsp_context_get_control(dctx, args, &cmd);
if (ret)
goto p_err;
ret = dsp_system_request_control(sys, args->control_id, &cmd);
if (ret)
goto p_err;
dsp_dbg("control end\n");
dsp_leave();
return 0;
p_err:
return ret;
}
const struct dsp_ioctl_ops dsp_ioctl_ops = {
.boot = dsp_context_boot,
.load_graph = dsp_context_load_graph,
.unload_graph = dsp_context_unload_graph,
.execute_msg = dsp_context_execute_msg,
.control = dsp_context_control,
};
struct dsp_context *dsp_context_create(struct dsp_core *core)
{
int ret;
struct dsp_context *dctx;
unsigned long flags;
dsp_enter();
dctx = kzalloc(sizeof(*dctx), GFP_KERNEL);
if (!dctx) {
ret = -ENOMEM;
dsp_err("Failed to alloc context\n");
goto p_err;
}
dctx->core = core;
spin_lock_irqsave(&core->dctx_slock, flags);
dctx->id = dsp_util_bitmap_set_region(&core->context_map, 1);
if (dctx->id < 0) {
spin_unlock_irqrestore(&core->dctx_slock, flags);
ret = dctx->id;
dsp_err("Failed to allocate context bitmap(%d)\n", ret);
goto p_err_alloc_id;
}
list_add_tail(&dctx->list, &core->dctx_list);
core->dctx_count++;
spin_unlock_irqrestore(&core->dctx_slock, flags);
mutex_init(&dctx->lock);
dsp_leave();
return dctx;
p_err_alloc_id:
kfree(dctx);
p_err:
return ERR_PTR(ret);
}
void dsp_context_destroy(struct dsp_context *dctx)
{
struct dsp_core *core;
unsigned long flags;
dsp_enter();
core = dctx->core;
mutex_destroy(&dctx->lock);
spin_lock_irqsave(&core->dctx_slock, flags);
core->dctx_count--;
list_del(&dctx->list);
dsp_util_bitmap_clear_region(&core->context_map, dctx->id, 1);
spin_unlock_irqrestore(&core->dctx_slock, flags);
kfree(dctx);
dsp_leave();
}
const struct dsp_ioctl_ops *dsp_context_get_ops(void)
{
dsp_check();
return &dsp_ioctl_ops;
}
|