summaryrefslogtreecommitdiff
path: root/bridge/dsp_bridge.c
blob: e5affb35e30aeb1cbbe46884a20a188edfac7d53 (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
/*
 * Copyright (C) 2009-2010 Felipe Contreras
 * Copyright (C) 2009-2010 Nokia Corporation
 * Copyright (C) 2007 Texas Instruments, Incorporated
 *
 * Author: Felipe Contreras <felipe.contreras@gmail.com>
 *
 * This file may be used under the terms of the GNU Lesser General Public
 * License version 2.1, a copy of which is found in LICENSE included in the
 * packaging of this file.
 */

#include "dsp_bridge.h"

/* for open */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <unistd.h> /* for close */
#include <sys/ioctl.h> /* for ioctl */
#include <stdlib.h> /* for free */

#include <malloc.h> /* for memalign */

#define ALLOCATE_SM

#ifdef ALLOCATE_SM
#include <malloc.h> /* for memalign */
#include <sys/mman.h> /* for mmap */
#endif

#if DSP_API < 2
#include <errno.h>
#endif

/*
 * Dspbridge ioctl numbering scheme
 *
 *    7                           0
 *  ---------------------------------
 *  |  Module   |   ioctl Number    |
 *  ---------------------------------
 *  | x | x | x | 0 | 0 | 0 | 0 | 0 |
 *  ---------------------------------
 */

/* ioctl driver identifier */
#define DB 0xDB

/*
 * Following are used to distinguish between module ioctls, this is needed
 * in case new ioctls are introduced.
 */
#define DB_MODULE_MASK 0xE0
#define DB_IOC_MASK    0x1F

#if DSP_API >= 1

#include <linux/ioctl.h>

/* ioctl module masks */
#define DB_MGR  0x0
#define DB_PROC 0x20
#define DB_NODE 0x40
#define DB_STRM 0x60
#define DB_CMM  0x80

/* Used to calculate the ioctl per dspbridge module */
#define DB_IOC(module, num) \
	(((module) & DB_MODULE_MASK) | ((num) & DB_IOC_MASK))

#else

#define DB_MGR  1
#define DB_PROC 7
#define DB_NODE 24
#define DB_STRM 39
#define DB_CMM  50

#define DB_IOC(module, num)	((module) + (num))

#undef _IOR
#undef _IOW
#undef _IOWR

#define _IOR(type, nr, size)	(nr)
#define _IOW(type, nr, size)	(nr)
#define _IOWR(type, nr, size)	(nr)

#endif /* DSP_API */

/* MGR Module */
#define MGR_WAIT		_IOWR(DB, DB_IOC(DB_MGR, 4), unsigned long)
#define MGR_ENUMNODE_INFO	_IOWR(DB, DB_IOC(DB_MGR, 0), unsigned long)
#define MGR_REGISTEROBJECT	_IOWR(DB, DB_IOC(DB_MGR, 2), unsigned long)
#define MGR_UNREGISTEROBJECT	_IOWR(DB, DB_IOC(DB_MGR, 3), unsigned long)

/* PROC Module */
#define PROC_ATTACH		_IOWR(DB, DB_IOC(DB_PROC, 0), unsigned long)
/* PROC_DETACH Deprecated */
#define PROC_DETACH		_IOR(DB, DB_IOC(DB_PROC, 2), unsigned long)
#define PROC_REGISTERNOTIFY	_IOWR(DB, DB_IOC(DB_PROC, 8), unsigned long)
#define PROC_RSVMEM		_IOWR(DB, DB_IOC(DB_PROC, 10), unsigned long)
#define PROC_UNRSVMEM		_IOW(DB, DB_IOC(DB_PROC, 11), unsigned long)
#define PROC_MAPMEM		_IOWR(DB, DB_IOC(DB_PROC, 12), unsigned long)
#define PROC_UNMAPMEM		_IOR(DB, DB_IOC(DB_PROC, 13), unsigned long)
#define PROC_FLUSHMEMORY	_IOW(DB, DB_IOC(DB_PROC, 14), unsigned long)
#define PROC_INVALIDATEMEMORY	_IOW(DB, DB_IOC(DB_PROC, 16), unsigned long)
#define PROC_GET_STATE		_IOWR(DB, DB_IOC(DB_PROC, 5), unsigned long)
#define PROC_ENUMRESOURCES	_IOWR(DB, DB_IOC(DB_PROC, 4), unsigned long)
#define PROC_ENUMNODE		_IOWR(DB, DB_IOC(DB_PROC, 3), unsigned long)
#define PROC_STOP               _IOWR(DB, DB_IOC(DB_PROC, 15), unsigned long)
#define PROC_LOAD               _IOW(DB, DB_IOC(DB_PROC, 7), unsigned long)
#define PROC_START              _IOW(DB, DB_IOC(DB_PROC, 9), unsigned long)

/* NODE Module */
#define NODE_REGISTERNOTIFY	_IOWR(DB, DB_IOC(DB_NODE, 11), unsigned long)
#define NODE_CREATE		_IOW(DB, DB_IOC(DB_NODE, 4), unsigned long)
#define NODE_RUN		_IOW(DB, DB_IOC(DB_NODE, 12), unsigned long)
#define NODE_TERMINATE		_IOWR(DB, DB_IOC(DB_NODE, 13), unsigned long)
#define NODE_PUTMESSAGE		_IOW(DB, DB_IOC(DB_NODE, 10), unsigned long)
#define NODE_GETMESSAGE		_IOWR(DB, DB_IOC(DB_NODE, 8), unsigned long)
#define NODE_DELETE		_IOW(DB, DB_IOC(DB_NODE, 5), unsigned long)
#define NODE_GETATTR		_IOWR(DB, DB_IOC(DB_NODE, 7), unsigned long)
#define NODE_ALLOCMSGBUF	_IOWR(DB, DB_IOC(DB_NODE, 1), unsigned long)
#define NODE_GETUUIDPROPS	_IOWR(DB, DB_IOC(DB_NODE, 14), unsigned long)
#define NODE_ALLOCATE		_IOWR(DB, DB_IOC(DB_NODE, 0), unsigned long)
#define NODE_CONNECT		_IOW(DB, DB_IOC(DB_NODE, 3), unsigned long)

/* CMM Module */
#define CMM_GETHANDLE		_IOR(DB, DB_IOC(DB_CMM, 2), unsigned long)
#define CMM_GETINFO		_IOR(DB, DB_IOC(DB_CMM, 3), unsigned long)

/* STRM Module */
#define STRM_OPEN		_IOWR(DB, DB_IOC(DB_STRM, 7), unsigned long)
#define STRM_CLOSE		_IOW(DB, DB_IOC(DB_STRM, 1), unsigned long)
#define STRM_GETINFO		_IOWR(DB, DB_IOC(DB_STRM, 4), unsigned long)
#define STRM_ALLOCATEBUFFER	_IOWR(DB, DB_IOC(DB_STRM, 0), unsigned long)
#define STRM_IDLE		_IOW(DB, DB_IOC(DB_STRM, 5), unsigned long)
#define STRM_RECLAIM		_IOWR(DB, DB_IOC(DB_STRM, 8), unsigned long)
#define STRM_FREEBUFFER		_IOWR(DB, DB_IOC(DB_STRM, 2), unsigned long)
#define STRM_ISSUE		_IOW(DB, DB_IOC(DB_STRM, 6), unsigned long)

#if DSP_API < 2
static inline int real_ioctl(int fd, int r, void *arg)
{
	return ioctl(fd, r, arg);
}
#endif

/* will not be needed when tidspbridge uses proper error codes */
#define ioctl(...) (ioctl(__VA_ARGS__) < 0)

int dsp_open(void)
{
	return open("/dev/DspBridge", O_RDWR);
}

int dsp_close(int handle)
{
	return close(handle);
}

struct proc_attach {
	unsigned int num;
	const void *info; /* not used */
	void **ret_handle;
};

bool dsp_attach(int handle,
		unsigned int num,
		const void *info,
		void **ret_handle)
{
	struct proc_attach arg = {
		.num = num,
		.info = info,
		.ret_handle = ret_handle,
	};

	return !ioctl(handle, PROC_ATTACH, &arg);
}

struct proc_detach {
	void *proc_handle;
};

bool dsp_detach(int handle,
		void *proc_handle)
{
	struct proc_detach arg = {
		.proc_handle = proc_handle,
	};

	return !ioctl(handle, PROC_DETACH, &arg);
}

struct register_notify {
	void *proc_handle;
	unsigned int event_mask;
	unsigned int notify_type;
	struct dsp_notification *info;
};

bool dsp_register_notify(int handle,
		void *proc_handle,
		unsigned int event_mask,
		unsigned int notify_type,
		struct dsp_notification *info)
{
	struct register_notify arg = {
		.proc_handle = proc_handle,
		.event_mask = event_mask,
		.notify_type = notify_type,
		.info = info,
	};

	return !ioctl(handle, PROC_REGISTERNOTIFY, &arg);
}

struct proc_start {
	void *proc_handle;
};

bool dsp_start(int handle,
		void *proc_handle)
{
	struct proc_start arg = {
		.proc_handle = proc_handle,
	};

	return !ioctl(handle, PROC_START, &arg);
}

bool dsp_stop(int handle,
		void *proc_handle)
{
	struct proc_start arg = {
		.proc_handle = proc_handle,
	};

	return !ioctl(handle, PROC_STOP, &arg);
}

struct proc_load {
	void *proc_handle;
	int argc;
	char **argv;
	char **env;
};

bool dsp_load(int handle,
		void *proc_handle,
		int argc, char **argv,
		char **env)
{
	struct proc_load arg = {
		.proc_handle = proc_handle,
		.argc = argc,
		.argv = argv,
		.env = env,
	};

	return !ioctl(handle, PROC_LOAD, &arg);
}

struct node_register_notify {
	void *node_handle;
	unsigned int event_mask;
	unsigned int notify_type;
	struct dsp_notification *info;
};

bool dsp_node_register_notify(int handle,
		struct dsp_node *node,
		unsigned int event_mask,
		unsigned int notify_type,
		struct dsp_notification *info)
{
	struct node_register_notify arg = {
		.node_handle = node->handle,
		.event_mask = event_mask,
		.notify_type = notify_type,
		.info = info,
	};

	return !ioctl(handle, NODE_REGISTERNOTIFY, &arg);
}

struct wait_for_events {
	struct dsp_notification **notifications;
	unsigned int count;
	unsigned int *ret_index;
	unsigned int timeout;
};

bool dsp_wait_for_events(int handle,
		struct dsp_notification **notifications,
		unsigned int count,
		unsigned int *ret_index,
		unsigned int timeout)
{
	struct wait_for_events arg = {
		.notifications = notifications,
		.count = count,
		.ret_index = ret_index,
		.timeout = timeout,
	};

#if DSP_API >= 2
	return !ioctl(handle, MGR_WAIT, &arg);
#else
	/*
	 * Temporary hack since libc only saves errors -1 to -4095; 0x80008017
	 * is not stored.
	 */
	int r;
	r = real_ioctl(handle, MGR_WAIT, &arg);
	if (r == (int)0x80008017)
		errno = ETIME;
	return r >= 0;
#endif
}

struct enum_node {
	unsigned int num;
	struct dsp_ndb_props *info;
	unsigned int info_size;
	unsigned int *ret_num;
};

bool dsp_enum(int handle,
		unsigned int num,
		struct dsp_ndb_props *info,
		size_t info_size,
		unsigned int *ret_num)
{
	struct enum_node arg = {
		.num = num,
		.info = info,
		.info_size = info_size,
		.ret_num = ret_num,
	};

	return !ioctl(handle, MGR_ENUMNODE_INFO, &arg);
}

struct register_object {
	const struct dsp_uuid *uuid;
	enum dsp_dcd_object_type type;
	const char *path;
};

bool dsp_register(int handle,
		const struct dsp_uuid *uuid,
		enum dsp_dcd_object_type type,
		const char *path)
{
	struct register_object arg = {
		.uuid = uuid,
		.type = type,
		.path = path,
	};

	return !ioctl(handle, MGR_REGISTEROBJECT, &arg);
}

struct unregister_object {
	const struct dsp_uuid *uuid;
	enum dsp_dcd_object_type type;
};

bool dsp_unregister(int handle,
		const struct dsp_uuid *uuid,
		enum dsp_dcd_object_type type)
{
	struct unregister_object arg = {
		.uuid = uuid,
		.type = type,
	};

	return !ioctl(handle, MGR_UNREGISTEROBJECT, &arg);
}

struct node_create {
	void *node_handle;
};

bool dsp_node_create(int handle,
		struct dsp_node *node)
{
	struct node_create arg = {
		.node_handle = node->handle,
	};

	return !ioctl(handle, NODE_CREATE, &arg);
}

struct node_run {
	void *node_handle;
};

bool dsp_node_run(int handle,
		struct dsp_node *node)
{
	struct node_run arg = {
		.node_handle = node->handle,
	};

	return !ioctl(handle, NODE_RUN, &arg);
}

struct node_terminate {
	void *node_handle;
	unsigned long *status;
};

bool dsp_node_terminate(int handle,
		struct dsp_node *node,
		unsigned long *status)
{
	struct node_terminate arg = {
		.node_handle = node->handle,
		.status = status,
	};

	return !ioctl(handle, NODE_TERMINATE, &arg);
}

struct node_put_message {
	void *node_handle;
	const struct dsp_msg *message;
	unsigned int timeout;
};

bool dsp_node_put_message(int handle,
		struct dsp_node *node,
		const struct dsp_msg *message,
		unsigned int timeout)
{
	struct node_put_message arg = {
		.node_handle = node->handle,
		.message = message,
		.timeout = timeout,
	};

	return !ioctl(handle, NODE_PUTMESSAGE, &arg);
}

struct node_get_message {
	void *node_handle;
	struct dsp_msg *message;
	unsigned int timeout;
};

bool dsp_node_get_message(int handle,
		struct dsp_node *node,
		struct dsp_msg *message,
		unsigned int timeout)
{
	struct node_get_message arg = {
		.node_handle = node->handle,
		.message = message,
		.timeout = timeout,
	};

	return !ioctl(handle, NODE_GETMESSAGE, &arg);
}

struct node_delete {
	void *node_handle;
};

static inline bool dsp_node_delete(int handle,
		struct dsp_node *node)
{
	struct node_delete arg = {
		.node_handle = node->handle,
	};

	return !ioctl(handle, NODE_DELETE, &arg);
}

#ifdef ALLOCATE_SM
struct node_get_attr {
	void *node_handle;
	struct dsp_node_attr *attr;
	unsigned int attr_size;
};

bool dsp_node_get_attr(int handle,
		struct dsp_node *node,
		struct dsp_node_attr *attr,
		size_t attr_size)
{
	struct node_get_attr arg = {
		.node_handle = node->handle,
		.attr = attr,
		.attr_size = attr_size,
	};

	return !ioctl(handle, NODE_GETATTR, &arg);
}

struct dsp_buffer_attr {
	unsigned long cb;
	unsigned int segment;
	unsigned int alignment;
};

struct node_alloc_buf {
	void *node_handle;
	unsigned int size;
	struct dsp_buffer_attr *attr;
	void **buffer;
};

static inline bool dsp_node_alloc_buf(int handle,
		struct dsp_node *node,
		size_t size,
		struct dsp_buffer_attr *attr,
		void **buffer)
{
	struct node_alloc_buf arg = {
		.node_handle = node->handle,
		.size = size,
		.attr = attr,
		.buffer = buffer,
	};

	if (ioctl(handle, NODE_ALLOCMSGBUF, &arg)) {
		*buffer = NULL;
		return false;
	}

	return true;
}

struct dsp_cmm_seg_info {
	unsigned long base_pa;
	unsigned long size;
	unsigned long gpp_base_pa;
	unsigned long gpp_size;
	unsigned long dsp_base_va;
	unsigned long dsp_size;
	unsigned long use_count;
	unsigned long base_va;
};

struct dsp_cmm_info {
	unsigned long segments;
	unsigned long use_count;
	unsigned long min_block_size;
	struct dsp_cmm_seg_info info[1];
};

struct cmm_get_handle {
	void *proc_handle;
	struct cmm_object **cmm;
};

struct cmm_get_info {
	struct cmm_object *cmm;
	struct dsp_cmm_info *info;
};

static inline bool get_cmm_info(int handle,
		void *proc_handle,
		struct dsp_cmm_info *cmm_info)
{
	struct cmm_object *cmm;
	struct cmm_get_handle cmm_arg = {
		.proc_handle = proc_handle,
		.cmm = &cmm,
	};
	struct cmm_get_info cmm_info_arg = {
		.info = cmm_info,
	};

	if (ioctl(handle, CMM_GETHANDLE, &cmm_arg))
		return false;

	cmm_info_arg.cmm = cmm;
	if (ioctl(handle, CMM_GETINFO, &cmm_info_arg))
		return false;

	return true;
}

static inline bool allocate_segments(int handle,
		void *proc_handle,
		struct dsp_node *node)
{
	struct dsp_cmm_info cmm_info;
	struct dsp_node_attr attr;
	enum dsp_node_type node_type;

	if (!get_cmm_info(handle, proc_handle, &cmm_info))
		return false;

	if (!dsp_node_get_attr(handle, node, &attr, sizeof(attr)))
		return false;

	node_type = attr.info.props.ntype;

	if ((node_type != DSP_NODE_DEVICE) && (cmm_info.segments > 0)) {
		struct dsp_cmm_seg_info *seg;

		seg = &cmm_info.info[0];

		if (seg->base_pa != 0 && seg->size > 0) {
			void *base;
			struct dsp_buffer_attr buffer_attr;

			base = mmap(NULL, seg->size,
					PROT_READ | PROT_WRITE, MAP_SHARED | 0x2000 /* MAP_LOCKED */,
					handle, seg->base_pa);

			if (!base)
				return false;

			buffer_attr.alignment = 0;
			buffer_attr.segment = 1 | 0x10000000;
			buffer_attr.cb = 0;
			if (!dsp_node_alloc_buf(handle, node, seg->size,
						&buffer_attr, &base))
			{
				munmap(base, seg->size);
				return false;
			}

			node->msgbuf_addr = base;
			node->msgbuf_size = seg->size;
		}
	}

	return true;
}
#endif

#ifdef ALLOCATE_HEAP
struct get_uuid_props {
	void *proc_handle;
	const struct dsp_uuid *node_uuid;
	struct dsp_ndb_props *props;
};

static inline bool get_uuid_props(int handle,
		void *proc_handle,
		const struct dsp_uuid *node_uuid,
		struct dsp_ndb_props *props)
{
	struct get_uuid_props arg = {
		.proc_handle = proc_handle,
		.node_uuid = node_uuid,
		.props = props,
	};

	return !ioctl(handle, NODE_GETUUIDPROPS, &arg);
}

#define PG_SIZE_4K 4096
#define PG_MASK(pg_size) (~((pg_size)-1))
#define PG_ALIGN_LOW(addr, pg_size) ((addr) & PG_MASK(pg_size))
#define PG_ALIGN_HIGH(addr, pg_size) (((addr)+(pg_size)-1) & PG_MASK(pg_size))
#endif

struct node_allocate {
	void *proc_handle;
	const struct dsp_uuid *node_id;
	const void *cb_data;
	struct dsp_node_attr_in *attrs;
	void **ret_node;
};

bool dsp_node_allocate(int handle,
		void *proc_handle,
		const struct dsp_uuid *node_uuid,
		const void *cb_data,
		struct dsp_node_attr_in *attrs,
		struct dsp_node **ret_node)
{
	struct dsp_node *node;
	void *node_handle = NULL;
	struct node_allocate arg = {
		.proc_handle = proc_handle,
		.node_id = node_uuid,
		.cb_data = cb_data,
		.attrs = attrs,
		.ret_node = &node_handle,
	};

#ifdef ALLOCATE_HEAP
	if (attrs) {
		struct dsp_ndb_props props;

		if (!get_uuid_props(handle, proc_handle, node_uuid, &props)) {
			attrs->gpp_va = NULL;
			return false;
		}

		if (attrs->profile_id < props.count_profiles) {
			unsigned int heap_size = 0;

			heap_size = props.node_profiles[attrs->profile_id].heap_size;
			if (heap_size) {
				void *virtual = NULL;

				heap_size = PG_ALIGN_HIGH(heap_size, PG_SIZE_4K);
				virtual = memalign(128, heap_size);
				if (!virtual)
					return false;
				attrs->heap_size = heap_size;
				attrs->gpp_va = virtual;
			}
		}
	}
#endif

	if (ioctl(handle, NODE_ALLOCATE, &arg)) {
		if (attrs) {
			free(attrs->gpp_va);
			attrs->gpp_va = NULL;
		}
		return false;
	}

	node = calloc(1, sizeof(*node));
	node->handle = node_handle;
	if (attrs)
		node->heap = attrs->gpp_va;

#ifdef ALLOCATE_SM
	if (!allocate_segments(handle, proc_handle, node)) {
		dsp_node_delete(handle, node);
		free(node->heap);
		free(node);
		return false;
	}
#endif

	*ret_node = node;

	return true;
}

struct node_connect {
	void *node_handle;
	unsigned int stream;
	void *other_node_handle;
	unsigned int other_stream;
	struct dsp_stream_attr *attrs;
	void *params;
};

bool dsp_node_connect(int handle,
		struct dsp_node *node,
		unsigned int stream,
		struct dsp_node *other_node,
		unsigned int other_stream,
		struct dsp_stream_attr *attrs,
		void *params)
{
	struct node_connect arg = {
		.node_handle = node->handle,
		.stream = stream,
		.other_node_handle = other_node->handle,
		.other_stream = other_stream,
		.attrs = attrs,
		.params = params,
	};

	return !ioctl(handle, NODE_CONNECT, &arg);
}

bool dsp_node_free(int handle,
		struct dsp_node *node)
{
#ifdef ALLOCATE_SM
	munmap(node->msgbuf_addr, node->msgbuf_size);
#endif
	dsp_node_delete(handle, node);
	free(node->heap);
	free(node);

	return true;
}

struct reserve_mem {
	void *proc_handle;
	unsigned long size;
	void **addr;
};

bool dsp_reserve(int handle,
		void *proc_handle,
		unsigned long size,
		void **addr)
{
	struct reserve_mem arg = {
		.proc_handle = proc_handle,
		.size = size,
		.addr = addr,
	};

	return !ioctl(handle, PROC_RSVMEM, &arg);
}

struct unreserve_mem {
	void *proc_handle;
	unsigned long size;
	void *addr;
};

bool dsp_unreserve(int handle,
		void *proc_handle,
		void *addr)
{
	struct unreserve_mem arg = {
		.proc_handle = proc_handle,
		.addr = addr,
	};

	return !ioctl(handle, PROC_UNRSVMEM, &arg);
}

struct map_mem {
	void *proc_handle;
	void *mpu_addr;
	unsigned long size;
	void *req_addr;
	void **ret_map_addr;
	unsigned long attr;
};

bool dsp_map(int handle,
		void *proc_handle,
		void *mpu_addr,
		unsigned long size,
		void *req_addr,
		void *ret_map_addr,
		unsigned long attr)
{
	struct map_mem arg = {
		.proc_handle = proc_handle,
		.mpu_addr = mpu_addr,
		.size = size,
		.req_addr = req_addr,
		.ret_map_addr = ret_map_addr,
		.attr = attr,
	};

	return !ioctl(handle, PROC_MAPMEM, &arg);
}

struct unmap_mem {
	void *proc_handle;
	unsigned long size;
	void *map_addr;
};

bool dsp_unmap(int handle,
		void *proc_handle,
		void *map_addr)
{
	struct unmap_mem arg = {
		.proc_handle = proc_handle,
		.map_addr = map_addr,
	};

	return !ioctl(handle, PROC_UNMAPMEM, &arg);
}

struct flush_mem {
	void *proc_handle;
	void *mpu_addr;
	unsigned long size;
	unsigned long flags;
};

bool dsp_flush(int handle,
		void *proc_handle,
		void *mpu_addr,
		unsigned long size,
		unsigned long flags)
{
	struct flush_mem arg = {
		.proc_handle = proc_handle,
		.mpu_addr = mpu_addr,
		.size = size,
		.flags = flags,
	};

	return !ioctl(handle, PROC_FLUSHMEMORY, &arg);
}

struct invalidate_mem {
	void *proc_handle;
	void *mpu_addr;
	unsigned long size;
};

bool dsp_invalidate(int handle,
		void *proc_handle,
		void *mpu_addr,
		unsigned long size)
{
	struct invalidate_mem arg = {
		.proc_handle = proc_handle,
		.mpu_addr = mpu_addr,
		.size = size,
	};

	return !ioctl(handle, PROC_INVALIDATEMEMORY, &arg);
}

struct proc_get_info {
	void *proc_handle;
	unsigned type;
	struct dsp_info *info;
	unsigned size;
};

bool dsp_proc_get_info(int handle,
		void *proc_handle,
		unsigned type,
		struct dsp_info *info,
		unsigned size)
{
	struct proc_get_info arg = {
		.proc_handle = proc_handle,
		.type = type,
		.info = info,
		.size = size,
	};

	return !ioctl(handle, PROC_ENUMRESOURCES, &arg);
}

struct enum_nodes {
	void *proc_handle;
	void **node_table;
	unsigned node_table_size;
	unsigned *num_nodes;
	unsigned *allocated;
};

bool dsp_enum_nodes(int handle,
		void *proc_handle,
		void **node_table,
		unsigned node_table_size,
		unsigned *num_nodes,
		unsigned *allocated)
{
	struct enum_nodes arg = {
		.proc_handle = proc_handle,
		.node_table = node_table,
		.node_table_size = node_table_size,
		.num_nodes = num_nodes,
		.allocated = allocated,
	};

	return !ioctl(handle, PROC_ENUMNODE, &arg);
}

struct stream_attr {
	void *event;
	char *name;
	void *base;
	unsigned long size;
	struct dsp_stream_attr_in *attrin;
};

struct stream_open {
	void *node_handle;
	unsigned int direction;
	unsigned int index;
	struct stream_attr *attr;
	void *stream;
};

bool dsp_stream_open(int handle,
		struct dsp_node *node,
		unsigned int direction,
		unsigned int index,
		struct dsp_stream_attr_in *attrin,
		void *stream)
{
	struct stream_attr strm_attr = {
		.attrin = attrin,
	};
	struct stream_open stream_arg = {
		.node_handle = node->handle,
		.direction = direction,
		.index = index,
		.attr = &strm_attr,
		.stream = stream,
	};

	if (attrin && (attrin->mode == STRMMODE_ZEROCOPY ||
				attrin->mode == STRMMODE_RDMA)) {
		struct dsp_cmm_info cmm_info;

		if (!get_cmm_info(handle, NULL, &cmm_info))
			return false;

		if (cmm_info.segments > 0) {
			void *base;
			struct dsp_cmm_seg_info *seg;

			seg = &cmm_info.info[0];
			base = mmap(NULL, seg->size,
					PROT_READ | PROT_WRITE,
					MAP_SHARED | 0x2000 /* MAP_LOCKED */,
					handle, seg->base_pa);

			if (!base)
				return false;

			strm_attr.base = base;
			strm_attr.size = seg->size;
		}
	}

	return !ioctl(handle, STRM_OPEN, &stream_arg);
}

struct stream_info {
	enum dsp_stream_mode mode;
	unsigned int segment;
	void *base;
	struct dsp_stream_info *info;
};

struct stream_get_info {
	void *stream;
	struct stream_info *info;
	unsigned int size;
};

static inline bool get_stream_info(int handle,
		void *stream,
		struct stream_info *info,
		unsigned int size)
{
	struct stream_get_info arg = {
		.stream = stream,
		.info = info,
		.size = size,
	};

	return !ioctl(handle, STRM_GETINFO, &arg);
}

bool dsp_stream_close(int handle,
		void *stream)
{
	struct stream_info info;
	if (!get_stream_info(handle, stream, &info, sizeof(struct stream_info)))
		return false;

	if (info.base) {
		struct dsp_cmm_info cmm_info;

		if (!get_cmm_info(handle, NULL, &cmm_info))
			return false;

		if (cmm_info.segments > 0) {
			struct dsp_cmm_seg_info *seg;
			seg = &cmm_info.info[0];
			if (munmap(info.base, seg->size))
				return false;
		}
	}

	return !ioctl(handle, STRM_CLOSE, &stream);
}

struct stream_idle {
	void *stream;
	bool flush;
};

bool dsp_stream_idle(int handle,
		void *stream,
		bool flush)
{
	struct stream_idle arg = {
		.stream = stream,
		.flush = flush,
	};
	return !ioctl(handle, STRM_IDLE, &arg);
}

struct stream_reclaim {
	void *stream;
	unsigned char **buff;
	unsigned long *data_size;
	unsigned long *buff_size;
	unsigned long *flag;
};

bool dsp_stream_reclaim(int handle,
		void *stream,
		unsigned char **buff,
		unsigned long *data_size,
		unsigned long *buff_size,
		unsigned long *flag)
{
	struct stream_reclaim arg = {
		.stream = stream,
		.buff = buff,
		.data_size = data_size,
		.buff_size = buff_size,
		.flag = flag,
	};
	return !ioctl(handle, STRM_RECLAIM, &arg);
}

struct stream_issue {
	void *stream;
	unsigned char *buff;
	unsigned long data_size;
	unsigned long buff_size;
	unsigned long flag;
};

bool dsp_stream_issue(int handle,
		void *stream,
		unsigned char *buff,
		unsigned long data_size,
		unsigned long buff_size,
		unsigned long flag)
{
	struct stream_issue arg = {
		.stream = stream,
		.buff = buff,
		.data_size = data_size,
		.buff_size = buff_size,
		.flag = flag,
	};
	return !ioctl(handle, STRM_ISSUE, &arg);
}

bool dsp_stream_get_info(int handle,
		void *stream,
		struct dsp_stream_info *info,
		unsigned int size)
{
	struct stream_info stream_info = {
		.info = info
	};

	return get_stream_info(handle, stream, &stream_info, size);
}


struct stream_allocate_buffer {
	void *stream;
	unsigned int size;
	unsigned char **buff;
	unsigned int num_buf;
};

bool dsp_stream_allocate_buffers(int handle,
		void *stream,
		unsigned int size,
		unsigned char **buff,
		unsigned int num_buf)
{
	unsigned int i;
	struct stream_info info;
	if (!get_stream_info(handle, stream, &info, sizeof(struct stream_info)))
		return false;

	if (info.segment > 0) {
		struct stream_allocate_buffer arg = {
			.stream = stream,
			.size = size,
			.buff = buff,
			.num_buf = num_buf,
		};

		return !ioctl(handle, STRM_ALLOCATEBUFFER, &arg);
	}

	for (i = 0; i < num_buf; i++)
		buff[i] = (unsigned char *) malloc(size);

	return true;
}

struct stream_free_buffers {
	void *stream;
	unsigned char **buff;
	unsigned int num_buf;
};

bool dsp_stream_free_buffers(int handle,
		void *stream,
		unsigned char **buff,
		unsigned int num_buf)
{
	unsigned int i;
	struct stream_info info;
	if (!get_stream_info(handle, stream, &info, sizeof(struct stream_info)))
		return false;

	if (info.segment) {
		struct stream_free_buffers arg = {
			.stream = stream,
			.buff = buff,
			.num_buf = num_buf,
		};
		return !ioctl(handle, STRM_FREEBUFFER, &arg);
	}

	for (i = 0; i < num_buf; i++) {
		free(buff[i]);
		buff[i] = NULL;
	}

	return true;
}