xref: /utopia/UTPA2-700.0.x/modules/msos/msos/linux/drvCMAPool.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file    drvCMAPool.c
98 /// @brief  CMA Pool Driver
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 
103 //-------------------------------------------------------------------------------------------------
104 //  Include Files
105 //-------------------------------------------------------------------------------------------------
106 #if defined (MSOS_TYPE_LINUX)
107 #include<sys/types.h>
108 #include <sys/ioctl.h>
109 #include <sys/mman.h>
110 #include <pthread.h>
111 #include<fcntl.h>
112 #include <unistd.h>
113 #include <string.h>
114 
115 #include "MsCommon.h"
116 #include "halCHIP.h"
117 #include "halMPool.h"
118 #include "mdrv_cma_pool_io.h"
119 #include "mdrv_cma_pool_st.h"
120 #include "mdrv_cma_pool_private.h"
121 
122 #include "drvCMAPool.h"
123 
124 #ifndef ANDROID
125 #define VPRINTF printf
126 #else
127 #include <sys/mman.h>
128 #include <cutils/ashmem.h>
129 #include <cutils/log.h>
130 #define VPRINTF ALOGD
131 #endif
132 //-------------------------------------------------------------------------------------------------
133 //  Local Defines
134 //-------------------------------------------------------------------------------------------------
135 #define MAX_CMAPOOLSIZE 10UL
136 //-------------------------------------------------------------------------------------------------
137 //  Local Structurs
138 //-------------------------------------------------------------------------------------------------
139 typedef struct
140 {
141     MS_U32 pool_handle_id;
142     MS_VIRT VirtStart;
143     MS_U64 Physaddr;
144     MS_SIZE CMAPoolSize;
145     MS_BOOL bNonCache;
146     MS_BOOL bIsUsed;
147 } CMAPOOL_INFO;
148 
149 //-------------------------------------------------------------------------------------------------
150 //  Global Variables
151 //-------------------------------------------------------------------------------------------------
152 static MS_S32              _s32FdCMAPool = -1;
153 static MS_S32 _s32FdCMAPool_pid = -1;
154 static pthread_mutex_t  _CMA_POOL_Mutex = PTHREAD_MUTEX_INITIALIZER;
155 static CMAPOOL_INFO cpool_info[MAX_CMAPOOLSIZE];
156 
157 //-------------------------------------------------------------------------------------------------
158 //  Debug Functions
159 //-------------------------------------------------------------------------------------------------
160 
161 
162 //-------------------------------------------------------------------------------------------------
163 //  Local Functions
164 //-------------------------------------------------------------------------------------------------
_findEmpty_CMA_Pool_Entry(MS_U32 * index)165 static MS_BOOL _findEmpty_CMA_Pool_Entry(MS_U32 *index)
166 {
167     MS_BOOL find = FALSE;
168     MS_U32 i;
169 
170     *index = 0;
171     for (i = 0; i < MAX_CMAPOOLSIZE; i++)
172     {
173         if(cpool_info[i].bIsUsed == FALSE)
174         {
175             find = TRUE;
176             *index = i;
177             break;
178         }
179     }
180 
181     if(find == FALSE)
182         VPRINTF("Not enough CMAPool, must increase MAX_CMAPOOLSIZE!!\n");
183 
184     return find;
185 }
186 
187 
_findPoolHandleId_InCMA_Pool_Table(MS_U32 pool_handle_id,MS_U32 * index)188 static MS_BOOL _findPoolHandleId_InCMA_Pool_Table(MS_U32 pool_handle_id, MS_U32 *index)
189 {
190     MS_BOOL find = FALSE;
191     MS_U32 i;
192 
193     *index = 0;
194     for (i = 0; i < MAX_CMAPOOLSIZE; i++)
195     {
196         if((cpool_info[i].bIsUsed == TRUE) && (cpool_info[i].pool_handle_id == pool_handle_id))
197         {
198             find = TRUE;
199             *index = i;
200             break;
201         }
202     }
203 
204     return find;
205 }
206 
207 //-------------------------------------------------------------------------------------------------
208 //  Global Functions
209 //-------------------------------------------------------------------------------------------------
210 
211 //-------------------------------------------------------------------------------------------------
212 /// System initialzation
213 /// @return TRUE(Success), FALSE(Failure)
214 //-------------------------------------------------------------------------------------------------
MApi_CMA_Pool_Init(struct CMA_Pool_Init_Param * Init_Param)215 MS_BOOL __attribute__((weak)) MApi_CMA_Pool_Init(struct CMA_Pool_Init_Param *Init_Param)
216 {
217     struct cma_heap_info heap_info;
218     MS_U64 u64PhyAddr = 0, u64AddrOffset = 0;
219     MS_U8 u8MiuSel = 0;
220     MS_BOOL bNonCache, ret = TRUE;
221     MS_U32 idx = 0;
222     char * ptr = NULL;
223     VPRINTF("[user mode %s:%d]debug    Init_Param->heap_id=%u\n",__FUNCTION__,__LINE__,Init_Param->heap_id);
224 
225 	memset(&heap_info, 0, sizeof(struct cma_heap_info));
226 
227     pthread_mutex_lock(&_CMA_POOL_Mutex);
228     if(_s32FdCMAPool > 0 && _s32FdCMAPool_pid != getpid())
229     {
230         close(_s32FdCMAPool);//need close and later open again.
231         _s32FdCMAPool = -1;
232         _s32FdCMAPool_pid = -1;
233     }
234     if (_s32FdCMAPool <= 0)
235     {
236         if ((_s32FdCMAPool = open("/dev/cmapool", O_RDWR)) < 0)
237         {
238             VPRINTF("Open /dev/cmapool fail\n");
239             ret =  FALSE;
240             goto CMA_POOL_INIT_DONE;
241         }
242 
243         _s32FdCMAPool_pid = getpid();
244         memset(cpool_info, 0, sizeof(CMAPOOL_INFO)*MAX_CMAPOOLSIZE);
245     }
246 
247     heap_info.heap_id = Init_Param->heap_id;
248     heap_info.flags = Init_Param->flags;
249 
250     if (ioctl(_s32FdCMAPool, CMA_POOL_IOC_INIT, &heap_info))
251     {
252         VPRINTF("init heap_id %tu fail\n", (ptrdiff_t)heap_info.heap_id);
253         ret =  FALSE;
254         goto CMA_POOL_INIT_DONE;
255     }
256 
257     Init_Param->pool_handle_id = heap_info.heap_id;
258     Init_Param->miu = heap_info.miu;											// the miu this heap located at
259     Init_Param->heap_miu_start_offset = heap_info.heap_miu_start_offset;		// heap_start_offset(pa), with respect to the miu
260     Init_Param->heap_length = heap_info.heap_length;
261 
262     //not need virtual address for this pool
263     if(!(Init_Param->flags & CMA_FLAG_MAP_VMA))
264     {
265          ret = TRUE;
266          goto CMA_POOL_INIT_DONE;
267     }
268 
269     //avoid mmap more than one time
270     ret = _findPoolHandleId_InCMA_Pool_Table(Init_Param->pool_handle_id, &idx);
271     if(ret == TRUE)
272     {
273          VPRINTF("heap_id %tu already init!\n", (ptrdiff_t)Init_Param->pool_handle_id);
274          goto CMA_POOL_INIT_DONE;
275     }
276 
277     ret = _findEmpty_CMA_Pool_Entry(&idx);
278     if(ret == FALSE)
279     {
280         VPRINTF("heap_id %tu init failed!\n", (ptrdiff_t)Init_Param->pool_handle_id);
281         goto CMA_POOL_INIT_DONE;
282     }
283 
284 	/* we reserve all heap_length in va at beginning(keep vma, if CMA_FLAG_MAP_VMA)!!
285 	 * really mapping will be done while MApi_CMA_Pool_GetMem
286 	 * also, we will defined this reserved va as cached/noncached here
287 	 * and then, you can see the reserved vma @ /proc/pid/maps
288 	 */
289     ptr = mmap(NULL, Init_Param->heap_length, PROT_READ|PROT_WRITE, MAP_SHARED, _s32FdCMAPool, 0);
290 
291 	if(ptr == (char*)(-1))
292     {
293 		VPRINTF("pool id %tu, size %lu mmap failed\n", (ptrdiff_t)Init_Param->pool_handle_id, (unsigned long)Init_Param->heap_length);
294 		ret =  FALSE;
295 		goto CMA_POOL_INIT_DONE;
296     }
297 
298     u64PhyAddr = HAL_MsOS_MPool_BA2PA(heap_info.bus_addr);   	// get pa
299     _phy_to_miu_offset(u8MiuSel, u64AddrOffset, u64PhyAddr);   	// get miu
300 
301     if(Init_Param->flags & CMA_FLAG_CACHED)
302         bNonCache = FALSE;
303     else
304         bNonCache = TRUE;
305 
306     ret = MsOS_MPool_Add_PA2VARange(u64PhyAddr, (MS_VIRT)ptr, Init_Param->heap_length, bNonCache);
307 
308     if(ret)
309     {
310         /* CMA Pool setting*/
311         cpool_info[idx].pool_handle_id = Init_Param->pool_handle_id;
312         cpool_info[idx].VirtStart = (MS_VIRT)ptr;
313         cpool_info[idx].Physaddr = u64PhyAddr;
314         cpool_info[idx].CMAPoolSize = Init_Param->heap_length;
315         cpool_info[idx].bNonCache = bNonCache;
316         cpool_info[idx].bIsUsed = TRUE;
317     }
318     else
319     {
320         VPRINTF("[%s:%d] ret==FALSE   ret=%d\n",__FUNCTION__,__LINE__,ret);
321     }
322 
323 CMA_POOL_INIT_DONE:
324     pthread_mutex_unlock(&_CMA_POOL_Mutex);
325     return ret;
326 }
327 
MApi_CMA_Pool_GetMem(struct CMA_Pool_Alloc_Param * alloc_param)328 MS_BOOL __attribute__((weak)) MApi_CMA_Pool_GetMem(struct CMA_Pool_Alloc_Param *alloc_param)
329 {
330     struct cma_alloc_args alloc_args;
331     MS_BOOL ret = TRUE;
332 
333 	memset(&alloc_args, 0, sizeof(struct cma_alloc_args));
334 
335     pthread_mutex_lock(&_CMA_POOL_Mutex);
336     if (_s32FdCMAPool < 0)
337     {
338         ret = FALSE;
339         goto CMA_POOL_GETMEM_DONE;
340     }
341 
342     alloc_args.heap_id = alloc_param->pool_handle_id;
343     alloc_args.align = 0x1000ULL;		// 4096
344     alloc_args.offset_in_heap = alloc_param->offset_in_pool;
345     alloc_args.length = alloc_param->length;
346     alloc_args.flags = alloc_param->flags;
347 
348     if (ioctl(_s32FdCMAPool, CMA_POOL_IOC_ALLOC, &alloc_args))
349     {
350         VPRINTF("cma pool get memory fail: heap id %tu, offset %llu, size %lu, flags %tX\n", (ptrdiff_t)alloc_args.heap_id,
351             (unsigned long long)alloc_args.offset_in_heap, (unsigned long)alloc_args.length, (ptrdiff_t)alloc_args.flags);
352         ret = FALSE;
353         goto CMA_POOL_GETMEM_DONE;
354     }
355 
356     if(alloc_args.flags & CMA_FLAG_VIRT_ADDR)
357     {
358         alloc_param->virt_addr = alloc_args.cpu_addr;
359 		VPRINTF("\033[35mFunction = %s, Line = %d, alloc_param->virt_addr is 0x%lX\033[m\n", __PRETTY_FUNCTION__, __LINE__, (unsigned long)alloc_param->virt_addr);
360 	}
361 
362 CMA_POOL_GETMEM_DONE:
363 	pthread_mutex_unlock(&_CMA_POOL_Mutex);
364 
365     return ret;
366 }
367 
MApi_CMA_Pool_PutMem(struct CMA_Pool_Free_Param * free_param)368 MS_BOOL __attribute__((weak)) MApi_CMA_Pool_PutMem(struct CMA_Pool_Free_Param * free_param)
369 {
370     struct cma_free_args free_args;
371     MS_BOOL ret = TRUE;
372 
373 	memset(&free_args, 0, sizeof(struct cma_free_args));
374 
375     pthread_mutex_lock(&_CMA_POOL_Mutex);
376     if(_s32FdCMAPool < 0)
377     {
378         ret = FALSE;
379         goto CMA_POOL_PUTMEM_DONE;
380     }
381 
382     free_args.heap_id = free_param->pool_handle_id;
383     free_args.offset_in_heap = free_param->offset_in_pool;
384     free_args.length = free_param->length;
385     if(ioctl(_s32FdCMAPool, CMA_POOL_IOC_FREE, &free_args))
386     {
387         VPRINTF("cma pool put memory fail: heap id %tu, offset 0x%lX, size 0x%lX\n", (ptrdiff_t)free_args.heap_id,
388             (unsigned long)free_args.offset_in_heap, (unsigned long)free_args.length);
389         ret = FALSE;
390         goto CMA_POOL_PUTMEM_DONE;
391     }
392 
393 CMA_POOL_PUTMEM_DONE:
394     pthread_mutex_unlock(&_CMA_POOL_Mutex);
395     return ret;
396 }
397 
MApi_CMA_Pool_Release(MS_U32 pool_handle_id)398 MS_BOOL __attribute__((weak)) MApi_CMA_Pool_Release(MS_U32 pool_handle_id)
399 {
400     MS_U32  heap_id = pool_handle_id;
401     MS_BOOL ret = TRUE;
402     MS_U32 idx = 0;
403 
404     pthread_mutex_lock(&_CMA_POOL_Mutex);
405     if (_s32FdCMAPool < 0)
406     {
407         ret = FALSE;
408         goto CMA_POOL_RELEASE_DONE;
409     }
410 
411     if (ioctl(_s32FdCMAPool, CMA_POOL_IOC_RELEASE, &heap_id))
412     {
413         VPRINTF("cma pool release fail: pool id %tu\n", (ptrdiff_t)pool_handle_id);
414         ret = FALSE;
415         goto CMA_POOL_RELEASE_DONE;
416     }
417 
418     ret = _findPoolHandleId_InCMA_Pool_Table(pool_handle_id, &idx);
419     if(ret == TRUE)
420     {
421         MsOS_MPool_Remove_PA2VARange(cpool_info[idx].Physaddr, cpool_info[idx].VirtStart,
422                 cpool_info[idx].CMAPoolSize, cpool_info[idx].bNonCache);
423         munmap((void *)(cpool_info[idx].VirtStart), cpool_info[idx].CMAPoolSize);
424         cpool_info[idx].bIsUsed = FALSE;
425         cpool_info[idx].pool_handle_id = 0;
426     }
427     ret = TRUE; //not find the pool hand id
428 
429 CMA_POOL_RELEASE_DONE:
430     pthread_mutex_unlock(&_CMA_POOL_Mutex);
431     return ret;
432 }
433 
MApi_CMA_Pool_PA_in_pool_info(struct PA_In_CMA_Pool_Param * pa_in_cma_pool_param)434 MS_BOOL __attribute__((weak)) MApi_CMA_Pool_PA_in_pool_info(struct PA_In_CMA_Pool_Param * pa_in_cma_pool_param)
435 {
436     struct pa_in_cma_pool_args pa_in_args;
437     pa_in_args.PA = pa_in_cma_pool_param->PA;//input
438     VPRINTF("%s:%d to do...\n",__FUNCTION__,__LINE__);
439     return false;
440 }
441 
442 //only kernel mode will use this function
CMA_Pool_Get_Info_From_PA(MS_S32 fd,struct CMA_Pool_Get_Info_From_Pa_Param * get_info_param)443 MS_BOOL CMA_Pool_Get_Info_From_PA(MS_S32 fd,struct CMA_Pool_Get_Info_From_Pa_Param *get_info_param)
444 {
445     MS_BOOL ret = TRUE;
446     struct cma_get_info_from_pa get_info_args;
447     memset(&get_info_args,0,sizeof(struct cma_get_info_from_pa));
448     //VPRINTF("%s:%d  \n",__FUNCTION__,__LINE__);
449     if (fd <= 0)
450     {
451        VPRINTF("%s:%d  fd=%d is false\n",__FUNCTION__,__LINE__,fd);
452        return false;
453     }
454 
455     get_info_args.PA=get_info_param->PA;
456     //VPRINTF("%s:%d get_info_args.PA=0x%lx\n",__FUNCTION__,__LINE__,(unsigned long)get_info_args.PA);
457     if(ioctl(fd,CMA_POOL_IOC_GET_HEAP_INFO_FROM_PA,&get_info_args))
458     {
459         //VPRINTF("cma pool get heap info from pa fail: PA=0x%lx\n", (unsigned long)get_info_args.PA);
460         ret = FALSE;
461     }
462     get_info_param->heap_id = get_info_args.heap_id;
463     get_info_param->miu = get_info_args.miu;
464     get_info_param->heap_miu_start_offset = get_info_args.heap_miu_start_offset;
465     get_info_param->heap_length = get_info_args.heap_length;
466     get_info_param->pa_offset_in_heap = get_info_args.pa_offset_in_heap;
467     //VPRINTF("%s:%d  ret=%d\n",__FUNCTION__,__LINE__,ret);
468     return ret;
469 }
CMA_Pool_CMA_Heap_Get_User_VA(MS_S32 fd,struct CMA_Pool_Heap_Get_User_Va_Param * vma_valid_Param)470 MS_BOOL CMA_Pool_CMA_Heap_Get_User_VA(MS_S32 fd,struct CMA_Pool_Heap_Get_User_Va_Param *vma_valid_Param)
471 {
472     struct cma_heap_get_user_va vma_valid_args;
473     MS_BOOL ret = TRUE;
474     memset(&vma_valid_args,0,sizeof(struct cma_heap_get_user_va));
475     if (fd <= 0)
476     {
477         VPRINTF("%s:%d fd=%d is false \n",__FUNCTION__,__LINE__,fd);
478         return FALSE;
479     }
480 
481     vma_valid_args.heap_id = vma_valid_Param->heap_id;
482     vma_valid_args.flags = vma_valid_Param->flags;
483 
484     if(ioctl(fd,CMA_POOL_IOC_KERNEL_MODE_GET_USER_VA,&vma_valid_args))
485     {
486         VPRINTF("cma pool kernel mode get user va fail: heap_id %u,flags %u\n", vma_valid_args.heap_id,vma_valid_args.flags);
487 		ret = FALSE;
488     }
489     vma_valid_Param->user_va_valid_flag = vma_valid_args.user_va_valid_flag;
490     vma_valid_Param->heap_start_user_space_virt_addr = vma_valid_args.heap_start_user_space_virt_addr;
491     VPRINTF("%s:%d  ret=%d  heap_start_user_space_virt_addr=0x%lx\n",__FUNCTION__,__LINE__,ret,(unsigned long)vma_valid_Param->heap_start_user_space_virt_addr);
492     return ret;
493 }
494 
CMA_Pool_CMA_Mmap_User_VA_Page(MS_S32 fd,struct CMA_Pool_Mmap_User_Va_Page_Param * mmap_user_va_page)495 MS_BOOL CMA_Pool_CMA_Mmap_User_VA_Page(MS_S32 fd,struct CMA_Pool_Mmap_User_Va_Page_Param *mmap_user_va_page)
496 {
497     MS_BOOL ret = TRUE;
498     struct cma_mmap_user_va_page mmap_user_va_page_args;
499     memset(&mmap_user_va_page_args,0,sizeof(struct cma_mmap_user_va_page));
500     VPRINTF("%s:%d  \n",__FUNCTION__,__LINE__);
501     if (fd <= 0)
502     {
503         VPRINTF("%s:%d  fd=%d is false\n",__FUNCTION__,__LINE__,fd);
504 		return false;
505     }
506 
507     mmap_user_va_page_args.heap_id = mmap_user_va_page->heap_id;
508     mmap_user_va_page_args.flags = mmap_user_va_page->flags;
509     if(ioctl(fd,CMA_POOL_IOC_KERNEL_MODE_MAPPING_USER_VA_PAGE,&mmap_user_va_page_args))
510     {
511         VPRINTF("cma pool kernel mode mapping user va page fail: heap_id %u,flags %u\n", mmap_user_va_page_args.heap_id,mmap_user_va_page_args.flags);
512 		ret = FALSE;
513     }
514     VPRINTF("%s:%d  ret=%d\n",__FUNCTION__,__LINE__,ret);
515     return ret;
516 }
517 
MApi_CMA_Pool_GetKernelCMAPooLUserVA(MS_PHY pAddrPhys,MS_BOOL un_cache_flags)518 MS_VIRT MApi_CMA_Pool_GetKernelCMAPooLUserVA(MS_PHY pAddrPhys,MS_BOOL un_cache_flags)
519 {
520     MS_VIRT pAddrVirt=NULL;
521     struct CMA_Pool_Get_Info_From_Pa_Param get_info_param;
522     struct CMA_Pool_Heap_Get_User_Va_Param vma_valid_Param;
523     struct CMA_Pool_Mmap_User_Va_Page_Param mmap_user_va_page;
524     MS_BOOL ret = TRUE;
525 
526     memset(&get_info_param,0,sizeof(struct CMA_Pool_Get_Info_From_Pa_Param));
527     memset(&vma_valid_Param,0,sizeof(struct CMA_Pool_Heap_Get_User_Va_Param));
528     memset(&mmap_user_va_page,0,sizeof(struct CMA_Pool_Mmap_User_Va_Page_Param));
529 
530     //VPRINTF("%s:%d un_cache_flags=%d  sizeof(MS_VIRT)=%d,sizeof(MS_PHY)=%d,sizeof(unsigned long)=%d ",__FUNCTION__,__LINE__,un_cache_flags,sizeof(MS_VIRT),sizeof(MS_PHY),sizeof(unsigned long));
531 /*
532 #if defined (__aarch64__)
533     {
534         VPRINTF("lx pAddrPhys=0x%lx  \n",pAddrPhys);
535     }
536 #else
537     {
538         VPRINTF("llx pAddrPhys=0x%llx  \n",pAddrPhys);
539     }
540 #endif
541 */
542     pthread_mutex_lock(&_CMA_POOL_Mutex);
543     if (_s32FdCMAPool <= 0)
544     {
545         if ((_s32FdCMAPool = open("/dev/cmapool", O_RDWR)) < 0)
546         {
547             VPRINTF("open /dev/cmapool fail\n");
548             pAddrVirt=NULL;
549             goto CMA_POOL_USER_VA_DONE;
550         }
551 
552         memset(cpool_info, 0, sizeof(CMAPOOL_INFO)*MAX_CMAPOOLSIZE);
553     }
554     get_info_param.PA = pAddrPhys;
555     //VPRINTF("%s:%d get_info_param.PA=0x%lx\n",__FUNCTION__,__LINE__,(unsigned long)get_info_param.PA);
556     ret = CMA_Pool_Get_Info_From_PA(_s32FdCMAPool,&get_info_param);
557     if(ret == FALSE)
558     {
559         //VPRINTF("%s:%d  GET_INFO_FROM_PA fail  PA=0x%lx\n",__FUNCTION__,__LINE__,(unsigned long)get_info_param.PA);
560         pAddrVirt=NULL;
561         goto CMA_POOL_USER_VA_DONE;
562     }
563     VPRINTF("%s:%d get_info_param.heap_id=%u\n",__FUNCTION__,__LINE__,get_info_param.heap_id);
564     vma_valid_Param.heap_id = get_info_param.heap_id;
565     if(un_cache_flags)
566         vma_valid_Param.flags &= ~CMA_FLAG_KERNEL_MODE_USER_SPACE_MAP_CACHED;
567     else
568         vma_valid_Param.flags |= CMA_FLAG_KERNEL_MODE_USER_SPACE_MAP_CACHED;
569     VPRINTF("%s:%d vma_valid_Param.heap_id=%u   vma_valid_Param.flags=%u\n",__FUNCTION__,__LINE__,vma_valid_Param.heap_id ,vma_valid_Param.flags);
570     ret = CMA_Pool_CMA_Heap_Get_User_VA(_s32FdCMAPool,&vma_valid_Param);
571     if(ret == FALSE)
572     {
573         VPRINTF("%s:%d  CMA_Pool_CMA_Heap_Get_User_VA fail  heap_id=%u,flags=%u\n",__FUNCTION__,__LINE__,vma_valid_Param.heap_id,vma_valid_Param.flags);
574         pAddrVirt=NULL;
575         goto CMA_POOL_USER_VA_DONE;
576     }
577     VPRINTF("%s:%d vma_valid_Param.user_va_valid_flag=%d\n",__FUNCTION__,__LINE__,vma_valid_Param.user_va_valid_flag);
578     if(USER_VA_VALID == (int32_t)vma_valid_Param.user_va_valid_flag)
579     {
580         pAddrVirt = vma_valid_Param.heap_start_user_space_virt_addr + get_info_param.pa_offset_in_heap;
581         VPRINTF("%s:%d  USER_VA_VALID case   pAddrPhys=0x%lx pAddrVirt=0x%lx   get_info_param.pa_offset_in_heap=0x%lx\n",__FUNCTION__,__LINE__,(unsigned long)pAddrPhys,(unsigned long)pAddrVirt,(unsigned long)get_info_param.pa_offset_in_heap);
582     }
583     else if(NO_USER_VA == (int32_t)vma_valid_Param.user_va_valid_flag)
584     {
585         char * ptr = NULL;
586         VPRINTF("%s:%d  NO_USER_VA case\n",__FUNCTION__,__LINE__);
587         ptr = mmap(NULL, get_info_param.heap_length, PROT_READ|PROT_WRITE, MAP_SHARED, _s32FdCMAPool, 0);
588         VPRINTF("%s:%d  \n",__FUNCTION__,__LINE__);
589         if(ptr == (char*)(-1))
590         {
591             VPRINTF("%s:%d  mmap fail heap_length=0x%lx\n",__FUNCTION__,__LINE__,(unsigned long)get_info_param.heap_length);
592             pAddrVirt = NULL;
593             goto CMA_POOL_USER_VA_DONE;
594         }
595         VPRINTF("%s:%d get_info_param.heap_id=%u \n",__FUNCTION__,__LINE__,get_info_param.heap_id);
596         mmap_user_va_page.heap_id = get_info_param.heap_id;
597         if(un_cache_flags)
598 			mmap_user_va_page.flags &= ~CMA_FLAG_KERNEL_MODE_USER_SPACE_MAP_CACHED;
599         else
600 			mmap_user_va_page.flags |= CMA_FLAG_KERNEL_MODE_USER_SPACE_MAP_CACHED;
601         VPRINTF("%s:%d mmap_user_va_page.heap_id=%u \n",__FUNCTION__,__LINE__,mmap_user_va_page.heap_id);
602         ret = CMA_Pool_CMA_Mmap_User_VA_Page(_s32FdCMAPool,&mmap_user_va_page);
603         if(ret == FALSE)
604         {
605             VPRINTF("%s:%d  CMA_Pool_CMA_Mmap_User_VA_Page fail heap_id=%u,flags=%u\n",__FUNCTION__,__LINE__,
606                                              mmap_user_va_page.heap_id,mmap_user_va_page.flags);
607             pAddrVirt=NULL;
608 			munmap((MS_VIRT *)ptr, get_info_param.heap_length);
609             goto CMA_POOL_USER_VA_DONE;
610         }
611         VPRINTF("%s:%d  \n",__FUNCTION__,__LINE__);
612         pAddrVirt  = (intptr_t)ptr  + get_info_param.pa_offset_in_heap;
613         VPRINTF("%s:%d pAddrPhys=0x%lx pAddrVirt=0x%lx  get_info_param.pa_offset_in_heap=0x%lx\n",__FUNCTION__,__LINE__,(unsigned long)pAddrPhys,(unsigned long)pAddrVirt,(unsigned long)get_info_param.pa_offset_in_heap);
614     }
615     else
616     {
617         VPRINTF("%s:%d  fail  vma_valid_Param.user_va_valid_flag=%d  \n",__FUNCTION__,__LINE__,vma_valid_Param.user_va_valid_flag);
618         pAddrVirt = NULL;
619         goto CMA_POOL_USER_VA_DONE;
620     }
621 CMA_POOL_USER_VA_DONE:
622 
623     pthread_mutex_unlock(&_CMA_POOL_Mutex);
624     return pAddrVirt;
625 }
626 
627 #endif
628