/
user-vcsm.c
2416 lines (2069 loc) · 66.9 KB
/
user-vcsm.c
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
/*
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <vmcs_sm_ioctl.h>
#include "vc_sm_cma_ioctl.h"
#include "dma-buf.h"
#include "user-vcsm.h"
#include "interface/vcos/vcos.h"
typedef struct
{
VCSM_CACHE_TYPE_T cur; /* Current pattern. */
VCSM_CACHE_TYPE_T new; /* New pattern. */
VCSM_CACHE_TYPE_T res; /* End result. */
} VCSM_CACHE_MUTEX_LKUP_T;
// /dev/vcsm-cma is the new device that is using CMA and importing it to the VPU.
#define VCSM_CMA_DEVICE_NAME "/dev/vcsm-cma"
// /dev/vcsm is the older driver that maps gpu_mem into the ARM.
#define VCSM_DEVICE_NAME "/dev/vcsm"
#define VCSM_INVALID_HANDLE (-1)
static VCOS_LOG_CAT_T usrvcsm_log_category;
#define VCOS_LOG_CATEGORY (&usrvcsm_log_category)
static int using_vc_sm_cma = 0;
static int vcsm_handle = VCSM_INVALID_HANDLE;
static int vcsm_refcount;
static unsigned int vcsm_page_size = 0;
static VCOS_ONCE_T vcsm_once = VCOS_ONCE_INIT;
static VCOS_MUTEX_T vcsm_mutex;
#define VCSM_PAYLOAD_ELEM_MAX 512
typedef struct VCSM_PAYLOAD_ELEM_T
{
unsigned int handle; // User handle
int fd; // vcsm-cma / dmabuf fd (= user handle-1)
uint32_t vc_handle; // VPU reloc heap handle
uint8_t *mem; // mmap'ed address
unsigned int size; // size of mmap
uint32_t dma_addr; // VPU address for the buffer
int in_use;
} VCSM_PAYLOAD_ELEM_T;
typedef struct VCSM_PAYLOAD_LIST_T
{
VCSM_PAYLOAD_ELEM_T list[VCSM_PAYLOAD_ELEM_MAX];
VCOS_MUTEX_T lock;
struct VCSM_PAYLOAD_LIST_T *next;
} VCSM_PAYLOAD_LIST_T;
static VCSM_PAYLOAD_LIST_T vcsm_payload_list;
static void vcsm_payload_list_init(void)
{
vcos_mutex_create(&vcsm_payload_list.lock, "vcsm_payload_list");
}
static VCSM_PAYLOAD_ELEM_T *vcsm_payload_list_get()
{
VCSM_PAYLOAD_ELEM_T *elem = 0;
unsigned int i;
vcos_mutex_lock(&vcsm_payload_list.lock);
for (i = 0; i < VCSM_PAYLOAD_ELEM_MAX; i++)
{
if (vcsm_payload_list.list[i].in_use)
continue;
elem = &vcsm_payload_list.list[i];
elem->in_use = 1;
break;
}
vcos_mutex_unlock(&vcsm_payload_list.lock);
return elem;
}
static void vcsm_payload_list_release(VCSM_PAYLOAD_ELEM_T *elem)
{
vcos_mutex_lock(&vcsm_payload_list.lock);
elem->handle = elem->vc_handle = elem->fd = 0;
elem->mem = NULL;
elem->in_use = 0;
vcos_mutex_unlock(&vcsm_payload_list.lock);
}
static VCSM_PAYLOAD_ELEM_T *vcsm_payload_list_find_mem(void *mem)
{
VCSM_PAYLOAD_ELEM_T *elem = 0;
unsigned int i;
vcos_mutex_lock(&vcsm_payload_list.lock);
for (i = 0; i < VCSM_PAYLOAD_ELEM_MAX; i++)
{
if (!vcsm_payload_list.list[i].in_use)
continue;
if (vcsm_payload_list.list[i].mem != mem)
continue;
elem = &vcsm_payload_list.list[i];
break;
}
vcos_mutex_unlock(&vcsm_payload_list.lock);
return elem;
}
static VCSM_PAYLOAD_ELEM_T *vcsm_payload_list_find_handle(unsigned int handle)
{
VCSM_PAYLOAD_ELEM_T *elem = 0;
unsigned int i;
vcos_mutex_lock(&vcsm_payload_list.lock);
for (i = 0; i < VCSM_PAYLOAD_ELEM_MAX; i++)
{
if (!vcsm_payload_list.list[i].in_use)
continue;
if (vcsm_payload_list.list[i].handle != handle)
continue;
elem = &vcsm_payload_list.list[i];
break;
}
vcos_mutex_unlock(&vcsm_payload_list.lock);
return elem;
}
/*static VCSM_PAYLOAD_ELEM_T *vcsm_payload_list_find_vc_handle(uint32_t vc_handle)
{
VCSM_PAYLOAD_ELEM_T *elem = 0;
unsigned int i;
vcos_mutex_lock(&vcsm_payload_list.lock);
for (i = 0; i < VCSM_PAYLOAD_ELEM_MAX; i++)
{
if (!vcsm_payload_list.list[i].in_use)
continue;
if (vcsm_payload_list.list[i].vc_handle != vc_handle)
continue;
elem = &vcsm_payload_list.list[i];
break;
}
vcos_mutex_unlock(&vcsm_payload_list.lock);
return elem;
}*/
/* Cache [(current, new) -> outcome] mapping table, ignoring identity.
**
** Note: Videocore cache mode cannot be udpated 'lock' time.
*/
static VCSM_CACHE_MUTEX_LKUP_T vcsm_cache_mutex_table[] =
{
/* ------ CURRENT ------- *//* ---------- NEW --------- *//* --------- RESULT --------- */
{ VCSM_CACHE_TYPE_NONE, VCSM_CACHE_TYPE_HOST, VCSM_CACHE_TYPE_HOST },
{ VCSM_CACHE_TYPE_NONE, VCSM_CACHE_TYPE_VC, VCSM_CACHE_TYPE_NONE },
{ VCSM_CACHE_TYPE_NONE, VCSM_CACHE_TYPE_HOST_AND_VC, VCSM_CACHE_TYPE_HOST },
{ VCSM_CACHE_TYPE_HOST, VCSM_CACHE_TYPE_NONE, VCSM_CACHE_TYPE_NONE },
{ VCSM_CACHE_TYPE_HOST, VCSM_CACHE_TYPE_VC, VCSM_CACHE_TYPE_HOST },
{ VCSM_CACHE_TYPE_HOST, VCSM_CACHE_TYPE_HOST_AND_VC, VCSM_CACHE_TYPE_HOST },
{ VCSM_CACHE_TYPE_VC, VCSM_CACHE_TYPE_NONE, VCSM_CACHE_TYPE_NONE },
{ VCSM_CACHE_TYPE_VC, VCSM_CACHE_TYPE_HOST, VCSM_CACHE_TYPE_HOST_AND_VC },
{ VCSM_CACHE_TYPE_VC, VCSM_CACHE_TYPE_HOST_AND_VC, VCSM_CACHE_TYPE_HOST_AND_VC },
{ VCSM_CACHE_TYPE_HOST_AND_VC, VCSM_CACHE_TYPE_NONE, VCSM_CACHE_TYPE_VC },
{ VCSM_CACHE_TYPE_HOST_AND_VC, VCSM_CACHE_TYPE_HOST, VCSM_CACHE_TYPE_HOST_AND_VC },
{ VCSM_CACHE_TYPE_HOST_AND_VC, VCSM_CACHE_TYPE_VC, VCSM_CACHE_TYPE_VC },
/* Used for lookup termination. */
{ VCSM_CACHE_TYPE_NONE, VCSM_CACHE_TYPE_NONE, VCSM_CACHE_TYPE_NONE },
};
static VCSM_CACHE_TYPE_T vcsm_cache_table_lookup( VCSM_CACHE_TYPE_T current,
VCSM_CACHE_TYPE_T new )
{
VCSM_CACHE_MUTEX_LKUP_T *p_map = vcsm_cache_mutex_table;
while ( !( (p_map->cur == VCSM_CACHE_TYPE_NONE) &&
(p_map->new == VCSM_CACHE_TYPE_NONE) ) )
{
if ( (p_map->cur == current) && (p_map->new == new) )
{
return p_map->res;
}
p_map++;
};
vcos_log_error( "[%s]: [%d]: no mapping found for current %d - new %d",
__func__,
getpid(),
current,
new );
return current;
}
/* A one off vcsm initialization routine
*/
static void vcsm_init_once(void)
{
vcos_mutex_create(&vcsm_mutex, VCOS_FUNCTION);
vcos_log_set_level(&usrvcsm_log_category, VCOS_LOG_ERROR);
usrvcsm_log_category.flags.want_prefix = 0;
vcos_log_register( "usrvcsm", &usrvcsm_log_category );
vcsm_payload_list_init();
}
/* Initialize the vcsm processing.
**
** Must be called once before attempting to do anything else.
**
** Returns 0 on success, -1 on error.
*/
int vcsm_init_ex( int want_export, int fd )
{
int result = VCSM_INVALID_HANDLE;
vcos_once(&vcsm_once, vcsm_init_once);
/* Only open the VCSM device once per process.
*/
vcos_mutex_lock( &vcsm_mutex );
if ( vcsm_refcount != 0 )
{
// Note that in this case the external fd is ignored.
if (want_export && !using_vc_sm_cma)
{
vcos_log_trace( "[%s]: fail as already open and export not available",
__func__);
vcos_mutex_unlock( &vcsm_mutex );
return -1;
}
goto out; /* VCSM already opened. Nothing to do. */
}
if (fd != -1)
{
vcsm_handle = dup(fd);
// FIXME: Sanity check which device that the fd actually relates to.
// For now we have to guess based on whether export is requested.
// (the main use case is from Chromium which will be requesting export).
if (want_export)
using_vc_sm_cma = 1;
goto out;
}
if (want_export)
{
vcsm_handle = open( VCSM_CMA_DEVICE_NAME, O_RDWR, 0 );
if (vcsm_handle >= 0)
{
using_vc_sm_cma = 1;
vcos_log_trace( "[%s]: Using vc-sm-cma, handle %d",
__func__, vcsm_handle);
}
}
if (vcsm_handle < 0)
{
vcos_log_trace( "[%s]: NOT using vc-sm-cma as handle was %d",
__func__, vcsm_handle);
vcsm_handle = open( VCSM_DEVICE_NAME, O_RDWR, 0 );
}
if (vcsm_handle < 0 && !want_export)
{
// vcsm failed and not tried vcsm-cma yet.
vcsm_handle = open( VCSM_CMA_DEVICE_NAME, O_RDWR, 0 );
if (vcsm_handle >= 0)
{
using_vc_sm_cma = 1;
vcos_log_trace( "[%s]: Using vc-sm-cma, handle %d",
__func__, vcsm_handle);
}
}
out:
if ( vcsm_handle >= 0 )
{
vcsm_page_size = getpagesize();
result = 0;
vcsm_refcount++;
vcos_log_trace( "[%s]: [%d]: %d (align: %u) - ref-cnt %u",
__func__,
getpid(),
vcsm_handle,
vcsm_page_size,
vcsm_refcount );
}
vcos_mutex_unlock( &vcsm_mutex );
return result;
}
/* Initialize the vcsm processing.
**
** Must be called once before attempting to do anything else.
**
** Returns 0 on success, -1 on error.
*/
int vcsm_init( void )
{
return vcsm_init_ex(0, -1);
}
/* Terminates the vcsm processing.
**
** Must be called vcsm services are no longer needed, it will
** take care of removing any allocation under the current process
** control if deemed necessary.
*/
void vcsm_exit( void )
{
vcos_mutex_lock( &vcsm_mutex );
if ( vcsm_refcount == 0 )
{
goto out; /* Shouldn't really happen. */
}
if ( --vcsm_refcount != 0 )
{
vcos_log_trace( "[%s]: [%d]: %d - ref-cnt: %u",
__func__,
getpid(),
vcsm_handle,
vcsm_refcount );
goto out; /* We're done. */
}
close( vcsm_handle );
vcsm_handle = VCSM_INVALID_HANDLE;
out:
vcos_mutex_unlock( &vcsm_mutex );
}
/* Allocates a cached block of memory of size 'size' via the vcsm memory
** allocator, the type of caching requested is passed as argument of the
** function call.
**
** Returns: 0 on error
** a non-zero opaque handle on success.
**
** On success, the user must invoke vcsm_lock with the returned opaque
** handle to gain access to the memory associated with the opaque handle.
** When finished using the memory, the user calls vcsm_unlock_xx (see those
** function definition for more details on the one that can be used).
**
** A well behaved application should make every attempt to lock/unlock
** only for the duration it needs to access the memory data associated with
** the opaque handle.
*/
unsigned int vcsm_malloc_cache( unsigned int size, VCSM_CACHE_TYPE_T cache, const char *name )
{
unsigned int size_aligned = size;
void *usr_ptr = NULL;
int rc;
unsigned int handle = VCSM_INVALID_HANDLE;
if ( (size == 0) || (vcsm_handle == VCSM_INVALID_HANDLE) )
{
vcos_log_error( "[%s]: [%d] [%s]: NULL size or invalid device!",
__func__,
getpid(),
name );
return 0;
}
/* Ask for page aligned.
*/
size_aligned = (size + vcsm_page_size - 1) & ~(vcsm_page_size - 1);
if (using_vc_sm_cma)
{
struct vc_sm_cma_ioctl_alloc alloc;
VCSM_PAYLOAD_ELEM_T *payload;
memset( &alloc, 0, sizeof(alloc));
alloc.size = size_aligned;
alloc.num = 1;
alloc.cached = (enum vmcs_sm_cache_e) cache; /* Convenient one to one mapping. */
alloc.handle = 0;
if ( name != NULL )
{
memcpy ( alloc.name, name, 32 );
}
rc = ioctl( vcsm_handle,
VC_SM_CMA_IOCTL_MEM_ALLOC,
&alloc );
if ( rc < 0 || alloc.handle < 0 )
{
vcos_log_error( "[%s]: [%d] [%s]: ioctl mem-alloc FAILED [%d] (hdl: %x)",
__func__,
getpid(),
alloc.name,
rc,
alloc.handle );
return 0;
}
vcos_log_trace( "[%s]: [%d] [%s]: ioctl mem-alloc %d (hdl: %x)",
__func__,
getpid(),
alloc.name,
rc,
alloc.handle );
/* Map the buffer into user space.
*/
usr_ptr = mmap( 0,
alloc.size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
alloc.handle,
0 );
if ( usr_ptr == MAP_FAILED )
{
vcos_log_error( "[%s]: [%d]: mmap FAILED (hdl: %x)",
__func__,
getpid(),
alloc.handle );
vcsm_free( alloc.handle );
return 0;
}
// vc-sm-cma now hands out file handles (signed int), whilst libvcsm is
// handling unsigned int handles. Already checked the handle >=0, so
// add one to make it a usable handle.
handle = alloc.handle + 1;
vcos_log_trace( "[%s]: mmap to %p",
__func__,
usr_ptr
);
payload = vcsm_payload_list_get();
if (!payload)
{
vcos_log_error("[%s]: max number of allocations reached: %d", __func__, VCSM_PAYLOAD_ELEM_MAX);
munmap(usr_ptr, alloc.size);
vcsm_free(alloc.handle);
return 0;
}
payload->handle = handle;
payload->fd = alloc.handle;
payload->vc_handle = alloc.vc_handle;
payload->mem = usr_ptr;
payload->size = size_aligned;
if (alloc.dma_addr & 0xFFFFFFFF00000000ULL)
{
vcos_log_error("[%s]: dma address returned > 32bit 0x%llx", __func__, alloc.dma_addr);
payload->dma_addr = 0;
}
else
payload->dma_addr = (uint32_t)alloc.dma_addr;
}
else
{
struct vmcs_sm_ioctl_alloc alloc;
memset( &alloc, 0, sizeof(alloc));
/* Allocate the buffer on videocore via the VCSM (Videocore Shared Memory)
** interface.
*/
alloc.size = size_aligned;
alloc.num = 1;
alloc.cached = (enum vmcs_sm_cache_e) cache; /* Convenient one to one mapping. */
alloc.handle = 0;
if ( name != NULL )
{
memcpy ( alloc.name, name, 32 );
}
rc = ioctl( vcsm_handle,
VMCS_SM_IOCTL_MEM_ALLOC,
&alloc );
if ( rc < 0 || alloc.handle == 0 )
{
vcos_log_error( "[%s]: [%d] [%s]: ioctl mem-alloc FAILED [%d] (hdl: %x)",
__func__,
getpid(),
alloc.name,
rc,
alloc.handle );
return 0;
}
vcos_log_trace( "[%s]: [%d] [%s]: ioctl mem-alloc %d (hdl: %x)",
__func__,
getpid(),
alloc.name,
rc,
alloc.handle );
/* Map the buffer into user space.
*/
usr_ptr = mmap( 0,
alloc.size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
vcsm_handle,
alloc.handle );
if ( usr_ptr == NULL )
{
vcos_log_error( "[%s]: [%d]: mmap FAILED (hdl: %x)",
__func__,
getpid(),
alloc.handle );
vcsm_free( alloc.handle );
return 0;
}
handle = alloc.handle;
}
return handle;
}
/* Allocates a non-cached block of memory of size 'size' via the vcsm memory
** allocator.
**
** Returns: 0 on error
** a non-zero opaque handle on success.
**
** On success, the user must invoke vcsm_lock with the returned opaque
** handle to gain access to the memory associated with the opaque handle.
** When finished using the memory, the user calls vcsm_unlock_xx (see those
** function definition for more details on the one that can be used).
**
** A well behaved application should make every attempt to lock/unlock
** only for the duration it needs to access the memory data associated with
** the opaque handle.
*/
unsigned int vcsm_malloc( unsigned int size, const char *name )
{
return vcsm_malloc_cache( size, VCSM_CACHE_TYPE_NONE, name );
}
/* Shares an allocated block of memory.
**
** Returns: 0 on error
** a non-zero opaque handle on success.
**
** On success, the user must invoke vcsm_lock with the returned opaque
** handle to gain access to the memory associated with the opaque handle.
** When finished using the memory, the user calls vcsm_unlock_xx (see those
** function definition for more details on the one that can be used).
**
** A well behaved application should make every attempt to lock/unlock
** only for the duration it needs to access the memory data associated with
** the opaque handle.
*/
unsigned int vcsm_malloc_share( unsigned int handle )
{
struct vmcs_sm_ioctl_alloc_share alloc;
int rc;
if ( vcsm_handle == VCSM_INVALID_HANDLE )
{
vcos_log_error( "[%s]: [%d]: NULL size or invalid device!",
__func__,
getpid() );
return 0;
}
if (using_vc_sm_cma)
{
return 0;
}
memset( &alloc, 0, sizeof(alloc) );
/* Share the buffer on videocore via the VCSM (Videocore Shared Memory)
** interface.
*/
alloc.handle = handle;
rc = ioctl( vcsm_handle,
VMCS_SM_IOCTL_MEM_ALLOC_SHARE,
&alloc );
if ( rc < 0 || alloc.handle == 0 )
{
vcos_log_error( "[%s]: [%d]: ioctl mem-share FAILED [%d] (hdl: %x->%x)",
__func__,
getpid(),
rc,
handle,
alloc.handle );
goto error;
}
vcos_log_trace( "[%s]: [%d]: ioctl mem-share %d (hdl: %x->%x)",
__func__,
getpid(),
rc,
handle,
alloc.handle );
return alloc.handle;
error:
if ( alloc.handle )
{
vcsm_free( alloc.handle );
}
return 0;
}
/* Frees a block of memory that was successfully allocated by
** a prior call the vcms_alloc.
**
** The handle should be considered invalid upon return from this
** call.
**
** Whether any memory is actually freed up or not as the result of
** this call will depends on many factors, if all goes well it will
** be freed. If something goes wrong, the memory will likely end up
** being freed up as part of the vcsm_exit process. In the end the
** memory is guaranteed to be freed one way or another.
*/
void vcsm_free( unsigned int handle )
{
int rc;
void *usr_ptr = NULL;
if ( (vcsm_handle == VCSM_INVALID_HANDLE) || (handle == 0) )
{
vcos_log_error( "[%s]: [%d]: invalid device or handle!",
__func__,
getpid() );
goto out;
}
if (using_vc_sm_cma)
{
VCSM_PAYLOAD_ELEM_T *elem;
elem = vcsm_payload_list_find_handle(handle);
if (!elem)
{
vcos_log_trace( "[%s]: handle %u not tracked, or not mapped. elem %p\n",
__func__, handle, elem);
goto out;
}
rc = munmap( elem->mem, elem->size );
vcos_log_trace( "[%s]: ioctl unmap fd: %d, addr %p, size %u. rc %d",
__func__,
elem->fd,
elem->mem,
elem->size,
rc
);
close(elem->fd);
vcsm_payload_list_release(elem);
}
else
{
struct vmcs_sm_ioctl_free alloc_free;
struct vmcs_sm_ioctl_size sz;
struct vmcs_sm_ioctl_map map;
memset( &sz, 0, sizeof(sz) );
memset( &alloc_free, 0, sizeof(alloc_free) );
memset( &map, 0, sizeof(map) );
/* Verify what we want is valid.
*/
sz.handle = handle;
rc = ioctl( vcsm_handle,
VMCS_SM_IOCTL_SIZE_USR_HDL,
&sz );
vcos_log_trace( "[%s]: [%d]: ioctl size-usr-hdl %d (hdl: %x) - size %u",
__func__,
getpid(),
rc,
sz.handle,
sz.size );
/* We will not be able to free up the resource!
**
** However, the driver will take care of it eventually once the device is
** closed (or dies), so this is not such a dramatic event...
*/
if ( (rc < 0) || (sz.size == 0) )
{
goto out;
}
/* Un-map the buffer from user space, using the last known mapped
** address valid.
*/
usr_ptr = (void *) vcsm_usr_address( sz.handle );
if ( usr_ptr != NULL )
{
munmap( usr_ptr, sz.size );
vcos_log_trace( "[%s]: [%d]: ioctl unmap hdl: %x",
__func__,
getpid(),
sz.handle );
}
else
{
vcos_log_trace( "[%s]: [%d]: freeing unmapped area (hdl: %x)",
__func__,
getpid(),
map.handle );
}
/* Free the allocated buffer all the way through videocore.
*/
alloc_free.handle = sz.handle;
rc = ioctl( vcsm_handle,
VMCS_SM_IOCTL_MEM_FREE,
&alloc_free );
vcos_log_trace( "[%s]: [%d]: ioctl mem-free %d (hdl: %x)",
__func__,
getpid(),
rc,
alloc_free.handle );
}
out:
return;
}
/* Queries the status of the the vcsm.
**
** Triggers dump of various kind of information, see the
** different variants specified in VCSM_STATUS_T.
**
** Pid is optional.
*/
void vcsm_status( VCSM_STATUS_T status, int pid )
{
struct vmcs_sm_ioctl_walk walk;
if ( vcsm_handle == VCSM_INVALID_HANDLE )
{
vcos_log_error( "[%s]: [%d]: invalid device!",
__func__,
getpid() );
return;
}
if (using_vc_sm_cma)
{
return;
}
memset( &walk, 0, sizeof(walk) );
/* Allow user to specify the pid of interest if desired, otherwise
** assume the current one.
*/
walk.pid = (pid == VCSM_INVALID_HANDLE) ? getpid() : pid;
switch ( status )
{
case VCSM_STATUS_VC_WALK_ALLOC:
{
ioctl( vcsm_handle,
VMCS_SM_IOCTL_VC_WALK_ALLOC,
NULL );
}
break;
case VCSM_STATUS_HOST_WALK_MAP:
{
ioctl( vcsm_handle,
VMCS_SM_IOCTL_HOST_WALK_MAP,
NULL );
}
break;
case VCSM_STATUS_HOST_WALK_PID_MAP:
{
ioctl( vcsm_handle,
VMCS_SM_IOCTL_HOST_WALK_PID_MAP,
&walk );
}
break;
case VCSM_STATUS_HOST_WALK_PID_ALLOC:
{
ioctl( vcsm_handle,
VMCS_SM_IOCTL_HOST_WALK_PID_ALLOC,
&walk );
}
break;
case VCSM_STATUS_NONE:
default:
vcos_log_error( "[%s]: [%d]: invalid argument %d",
__func__,
getpid(),
status );
break;
}
}
/* Retrieves a videocore opaque handle from a mapped user address
** pointer. The videocore handle will correspond to the actual
** memory mapped in videocore.
**
** Returns: 0 on error
** a non-zero opaque handle on success.
**
** Note: the videocore opaque handle is distinct from the user
** opaque handle (allocated via vcsm_malloc) and it is only
** significant for such application which knows what to do
** with it, for the others it is just a number with little
** use since nothing can be done with it (in particular
** for safety reason it cannot be used to map anything).
*/
unsigned int vcsm_vc_hdl_from_ptr( void *usr_ptr )
{
int rc;
if ( (vcsm_handle == VCSM_INVALID_HANDLE) || (usr_ptr == NULL) )
{
vcos_log_error( "[%s]: [%d]: invalid device!",
__func__,
getpid() );
return 0;
}
if (using_vc_sm_cma)
{
VCSM_PAYLOAD_ELEM_T *elem;
elem = vcsm_payload_list_find_mem(usr_ptr);
if (!elem)
{
vcos_log_trace( "[%s]: addr %p not tracked, or not mapped. elem %p\n",
__func__, usr_ptr, elem);
return 0;
}
return elem->vc_handle;
}
else
{
struct vmcs_sm_ioctl_map map;
memset( &map, 0, sizeof(map) );
map.pid = getpid();
map.addr = (uintptr_t) usr_ptr;
rc = ioctl( vcsm_handle,
VMCS_SM_IOCTL_MAP_VC_HDL_FR_ADDR,
&map );
if ( rc < 0 )
{
vcos_log_error( "[%s]: [%d]: ioctl mapped-usr-hdl FAILED [%d] (pid: %d, addr: %x)",
__func__,
getpid(),
rc,
map.pid,
map.addr );
return 0;
}
else
{
vcos_log_trace( "[%s]: [%d]: ioctl mapped-usr-hdl %d (hdl: %x, addr: %x)",
__func__,
getpid(),
rc,
map.handle,
map.addr );
return map.handle;
}
}
}
/* Retrieves a videocore opaque handle from a opaque handle
** pointer. The videocore handle will correspond to the actual
** memory mapped in videocore.
**
** Returns: 0 on error
** a non-zero opaque handle on success.
**
** Note: the videocore opaque handle is distinct from the user
** opaque handle (allocated via vcsm_malloc) and it is only
** significant for such application which knows what to do
** with it, for the others it is just a number with little
** use since nothing can be done with it (in particular
** for safety reason it cannot be used to map anything).
*/
unsigned int vcsm_vc_hdl_from_hdl( unsigned int handle )
{
if ( (vcsm_handle == VCSM_INVALID_HANDLE) || (handle == 0) )
{
vcos_log_error( "[%s]: [%d]: invalid device or handle!",
__func__,
getpid() );
return 0;
}
if (using_vc_sm_cma)
{
VCSM_PAYLOAD_ELEM_T *elem;
elem = vcsm_payload_list_find_handle(handle);
if (!elem)
{
vcos_log_trace( "[%s]: handle %u not tracked, or not mapped. \n",
__func__, handle);
return 0;
}
return elem->vc_handle;
}
else
{
int rc;
struct vmcs_sm_ioctl_map map;
memset( &map, 0, sizeof(map) );
map.pid = getpid();
map.handle = handle;
rc = ioctl( vcsm_handle,
VMCS_SM_IOCTL_MAP_VC_HDL_FR_HDL,
&map );