xref: /utopia/UTPA2-700.0.x/modules/msos/msos/linux/drvIPAPool.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1*53ee8cc1Swenshuai.xi //<MStar Software>
2*53ee8cc1Swenshuai.xi //******************************************************************************
3*53ee8cc1Swenshuai.xi // MStar Software
4*53ee8cc1Swenshuai.xi // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5*53ee8cc1Swenshuai.xi // All software, firmware and related documentation herein ("MStar Software") are
6*53ee8cc1Swenshuai.xi // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7*53ee8cc1Swenshuai.xi // law, including, but not limited to, copyright law and international treaties.
8*53ee8cc1Swenshuai.xi // Any use, modification, reproduction, retransmission, or republication of all
9*53ee8cc1Swenshuai.xi // or part of MStar Software is expressly prohibited, unless prior written
10*53ee8cc1Swenshuai.xi // permission has been granted by MStar.
11*53ee8cc1Swenshuai.xi //
12*53ee8cc1Swenshuai.xi // By accessing, browsing and/or using MStar Software, you acknowledge that you
13*53ee8cc1Swenshuai.xi // have read, understood, and agree, to be bound by below terms ("Terms") and to
14*53ee8cc1Swenshuai.xi // comply with all applicable laws and regulations:
15*53ee8cc1Swenshuai.xi //
16*53ee8cc1Swenshuai.xi // 1. MStar shall retain any and all right, ownership and interest to MStar
17*53ee8cc1Swenshuai.xi //    Software and any modification/derivatives thereof.
18*53ee8cc1Swenshuai.xi //    No right, ownership, or interest to MStar Software and any
19*53ee8cc1Swenshuai.xi //    modification/derivatives thereof is transferred to you under Terms.
20*53ee8cc1Swenshuai.xi //
21*53ee8cc1Swenshuai.xi // 2. You understand that MStar Software might include, incorporate or be
22*53ee8cc1Swenshuai.xi //    supplied together with third party`s software and the use of MStar
23*53ee8cc1Swenshuai.xi //    Software may require additional licenses from third parties.
24*53ee8cc1Swenshuai.xi //    Therefore, you hereby agree it is your sole responsibility to separately
25*53ee8cc1Swenshuai.xi //    obtain any and all third party right and license necessary for your use of
26*53ee8cc1Swenshuai.xi //    such third party`s software.
27*53ee8cc1Swenshuai.xi //
28*53ee8cc1Swenshuai.xi // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29*53ee8cc1Swenshuai.xi //    MStar`s confidential information and you agree to keep MStar`s
30*53ee8cc1Swenshuai.xi //    confidential information in strictest confidence and not disclose to any
31*53ee8cc1Swenshuai.xi //    third party.
32*53ee8cc1Swenshuai.xi //
33*53ee8cc1Swenshuai.xi // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34*53ee8cc1Swenshuai.xi //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35*53ee8cc1Swenshuai.xi //    without limitation, any warranties of merchantability, non-infringement of
36*53ee8cc1Swenshuai.xi //    intellectual property rights, fitness for a particular purpose, error free
37*53ee8cc1Swenshuai.xi //    and in conformity with any international standard.  You agree to waive any
38*53ee8cc1Swenshuai.xi //    claim against MStar for any loss, damage, cost or expense that you may
39*53ee8cc1Swenshuai.xi //    incur related to your use of MStar Software.
40*53ee8cc1Swenshuai.xi //    In no event shall MStar be liable for any direct, indirect, incidental or
41*53ee8cc1Swenshuai.xi //    consequential damages, including without limitation, lost of profit or
42*53ee8cc1Swenshuai.xi //    revenues, lost or damage of data, and unauthorized system use.
43*53ee8cc1Swenshuai.xi //    You agree that this Section 4 shall still apply without being affected
44*53ee8cc1Swenshuai.xi //    even if MStar Software has been modified by MStar in accordance with your
45*53ee8cc1Swenshuai.xi //    request or instruction for your use, except otherwise agreed by both
46*53ee8cc1Swenshuai.xi //    parties in writing.
47*53ee8cc1Swenshuai.xi //
48*53ee8cc1Swenshuai.xi // 5. If requested, MStar may from time to time provide technical supports or
49*53ee8cc1Swenshuai.xi //    services in relation with MStar Software to you for your use of
50*53ee8cc1Swenshuai.xi //    MStar Software in conjunction with your or your customer`s product
51*53ee8cc1Swenshuai.xi //    ("Services").
52*53ee8cc1Swenshuai.xi //    You understand and agree that, except otherwise agreed by both parties in
53*53ee8cc1Swenshuai.xi //    writing, Services are provided on an "AS IS" basis and the warranty
54*53ee8cc1Swenshuai.xi //    disclaimer set forth in Section 4 above shall apply.
55*53ee8cc1Swenshuai.xi //
56*53ee8cc1Swenshuai.xi // 6. Nothing contained herein shall be construed as by implication, estoppels
57*53ee8cc1Swenshuai.xi //    or otherwise:
58*53ee8cc1Swenshuai.xi //    (a) conferring any license or right to use MStar name, trademark, service
59*53ee8cc1Swenshuai.xi //        mark, symbol or any other identification;
60*53ee8cc1Swenshuai.xi //    (b) obligating MStar or any of its affiliates to furnish any person,
61*53ee8cc1Swenshuai.xi //        including without limitation, you and your customers, any assistance
62*53ee8cc1Swenshuai.xi //        of any kind whatsoever, or any information; or
63*53ee8cc1Swenshuai.xi //    (c) conferring any license or right under any intellectual property right.
64*53ee8cc1Swenshuai.xi //
65*53ee8cc1Swenshuai.xi // 7. These terms shall be governed by and construed in accordance with the laws
66*53ee8cc1Swenshuai.xi //    of Taiwan, R.O.C., excluding its conflict of law rules.
67*53ee8cc1Swenshuai.xi //    Any and all dispute arising out hereof or related hereto shall be finally
68*53ee8cc1Swenshuai.xi //    settled by arbitration referred to the Chinese Arbitration Association,
69*53ee8cc1Swenshuai.xi //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70*53ee8cc1Swenshuai.xi //    Rules of the Association by three (3) arbitrators appointed in accordance
71*53ee8cc1Swenshuai.xi //    with the said Rules.
72*53ee8cc1Swenshuai.xi //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73*53ee8cc1Swenshuai.xi //    be English.
74*53ee8cc1Swenshuai.xi //    The arbitration award shall be final and binding to both parties.
75*53ee8cc1Swenshuai.xi //
76*53ee8cc1Swenshuai.xi //******************************************************************************
77*53ee8cc1Swenshuai.xi //<MStar Software>
78*53ee8cc1Swenshuai.xi ////////////////////////////////////////////////////////////////////////////////
79*53ee8cc1Swenshuai.xi //
80*53ee8cc1Swenshuai.xi // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81*53ee8cc1Swenshuai.xi // All rights reserved.
82*53ee8cc1Swenshuai.xi //
83*53ee8cc1Swenshuai.xi // Unless otherwise stipulated in writing, any and all information contained
84*53ee8cc1Swenshuai.xi // herein regardless in any format shall remain the sole proprietary of
85*53ee8cc1Swenshuai.xi // MStar Semiconductor Inc. and be kept in strict confidence
86*53ee8cc1Swenshuai.xi // ("MStar Confidential Information") by the recipient.
87*53ee8cc1Swenshuai.xi // Any unauthorized act including without limitation unauthorized disclosure,
88*53ee8cc1Swenshuai.xi // copying, use, reproduction, sale, distribution, modification, disassembling,
89*53ee8cc1Swenshuai.xi // reverse engineering and compiling of the contents of MStar Confidential
90*53ee8cc1Swenshuai.xi // Information is unlawful and strictly prohibited. MStar hereby reserves the
91*53ee8cc1Swenshuai.xi // rights to any and all damages, losses, costs and expenses resulting therefrom.
92*53ee8cc1Swenshuai.xi //
93*53ee8cc1Swenshuai.xi ////////////////////////////////////////////////////////////////////////////////
94*53ee8cc1Swenshuai.xi 
95*53ee8cc1Swenshuai.xi ///////////////////////////////////////////////////////////////////////////////////////////////////
96*53ee8cc1Swenshuai.xi ///
97*53ee8cc1Swenshuai.xi /// file    drvIPAPool.c
98*53ee8cc1Swenshuai.xi /// @brief  IPA Pool Driver
99*53ee8cc1Swenshuai.xi /// @author MStar Semiconductor Inc.
100*53ee8cc1Swenshuai.xi ///////////////////////////////////////////////////////////////////////////////////////////////////
101*53ee8cc1Swenshuai.xi 
102*53ee8cc1Swenshuai.xi 
103*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
104*53ee8cc1Swenshuai.xi //  Include Files
105*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
106*53ee8cc1Swenshuai.xi #if defined (MSOS_TYPE_LINUX)
107*53ee8cc1Swenshuai.xi #include<sys/types.h>
108*53ee8cc1Swenshuai.xi #include <sys/ioctl.h>
109*53ee8cc1Swenshuai.xi #include <sys/mman.h>
110*53ee8cc1Swenshuai.xi #include <poll.h>
111*53ee8cc1Swenshuai.xi #include <pthread.h>
112*53ee8cc1Swenshuai.xi #include<fcntl.h>
113*53ee8cc1Swenshuai.xi #include <unistd.h>
114*53ee8cc1Swenshuai.xi #include <string.h>
115*53ee8cc1Swenshuai.xi #include "MsCommon.h"
116*53ee8cc1Swenshuai.xi #include "halCHIP.h"
117*53ee8cc1Swenshuai.xi #include "halMPool.h"
118*53ee8cc1Swenshuai.xi #include "drvIPAPool.h"
119*53ee8cc1Swenshuai.xi #include "mdrv_ipa_pool_uapi.h"
120*53ee8cc1Swenshuai.xi 
121*53ee8cc1Swenshuai.xi #ifndef ANDROID
122*53ee8cc1Swenshuai.xi #define VPRINTF printf
123*53ee8cc1Swenshuai.xi #else
124*53ee8cc1Swenshuai.xi #include <sys/mman.h>
125*53ee8cc1Swenshuai.xi #include <cutils/ashmem.h>
126*53ee8cc1Swenshuai.xi #include <cutils/log.h>
127*53ee8cc1Swenshuai.xi #define VPRINTF ALOGD
128*53ee8cc1Swenshuai.xi #endif
129*53ee8cc1Swenshuai.xi 
130*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
131*53ee8cc1Swenshuai.xi //  Local Defines
132*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
133*53ee8cc1Swenshuai.xi #define MAX_IPAPOOLSIZE 16UL
134*53ee8cc1Swenshuai.xi 
135*53ee8cc1Swenshuai.xi 
136*53ee8cc1Swenshuai.xi //
137*53ee8cc1Swenshuai.xi //
138*53ee8cc1Swenshuai.xi //for example:
139*53ee8cc1Swenshuai.xi //area 1 map 2 times,area 2 map 2times,area 3 map 1 times,total map 2+2+1=5
140*53ee8cc1Swenshuai.xi //
141*53ee8cc1Swenshuai.xi //   map 2t  map 2t map 1t
142*53ee8cc1Swenshuai.xi //   ------   ----
143*53ee8cc1Swenshuai.xi //   ------   ----   ---
144*53ee8cc1Swenshuai.xi //     1        2    3
145*53ee8cc1Swenshuai.xi //   ------   ---- ------
146*53ee8cc1Swenshuai.xi //-----------------------------same client
147*53ee8cc1Swenshuai.xi #define MAX_CLIENT_MAP_NUM 8UL
148*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
149*53ee8cc1Swenshuai.xi //  Local Structurs
150*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
151*53ee8cc1Swenshuai.xi 
152*53ee8cc1Swenshuai.xi struct VIRT_MAP_INFO
153*53ee8cc1Swenshuai.xi {
154*53ee8cc1Swenshuai.xi     MS_U64 virt_addr;
155*53ee8cc1Swenshuai.xi     MS_U64 length;
156*53ee8cc1Swenshuai.xi     MS_BOOL bNonCache;
157*53ee8cc1Swenshuai.xi     MS_U64 Physaddr;
158*53ee8cc1Swenshuai.xi };
159*53ee8cc1Swenshuai.xi struct IPA_Pool_Init_Param_No_P
160*53ee8cc1Swenshuai.xi {
161*53ee8cc1Swenshuai.xi     MS_U32 heap_id;     //in: heap id the pool will be created in
162*53ee8cc1Swenshuai.xi     MS_U64 pool_name;//in: global identify name for pool to shared between multiple process
163*53ee8cc1Swenshuai.xi 
164*53ee8cc1Swenshuai.xi     MS_U64 offset_in_heap;    //in: pool location in heap
165*53ee8cc1Swenshuai.xi     MS_U64 len;       //in: pool length in  heap
166*53ee8cc1Swenshuai.xi 
167*53ee8cc1Swenshuai.xi     MS_U32 pool_handle_id; //out: generate pool id based on heap specified by heap_id
168*53ee8cc1Swenshuai.xi     MS_U32 miu;  //out: miu id this heap belongs, index from 0.
169*53ee8cc1Swenshuai.xi     enum IPA_SPACE_TYPE heap_type;//out: return heap type to application
170*53ee8cc1Swenshuai.xi     MS_S32 error_code; // error code when pool init failed
171*53ee8cc1Swenshuai.xi 
172*53ee8cc1Swenshuai.xi     MS_U64 heap_length; //out: heap leagth
173*53ee8cc1Swenshuai.xi     MS_U64 heap_miu_start_offset; //out: heap start offset in miu
174*53ee8cc1Swenshuai.xi };
175*53ee8cc1Swenshuai.xi 
176*53ee8cc1Swenshuai.xi typedef struct
177*53ee8cc1Swenshuai.xi {
178*53ee8cc1Swenshuai.xi     struct IPA_Pool_Init_Param_No_P Init_Param;
179*53ee8cc1Swenshuai.xi     MS_BOOL bIsUsed;
180*53ee8cc1Swenshuai.xi     struct VIRT_MAP_INFO map_info[MAX_CLIENT_MAP_NUM];
181*53ee8cc1Swenshuai.xi     volatile MS_U32 polling_thread_delete_task_flag;
182*53ee8cc1Swenshuai.xi     pthread_t pthIPAPollingId;
183*53ee8cc1Swenshuai.xi     void (*polling_callback)(MS_U32 pool_handle_id,MS_U64 start,MS_U64 length);
184*53ee8cc1Swenshuai.xi } IPAPOOL_INFO;
185*53ee8cc1Swenshuai.xi 
186*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
187*53ee8cc1Swenshuai.xi //  Global Variables
188*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
189*53ee8cc1Swenshuai.xi static MS_S32 _s32FdIPAPool = -1;
190*53ee8cc1Swenshuai.xi static pthread_mutex_t  _IPA_POOL_Mutex = PTHREAD_MUTEX_INITIALIZER;
191*53ee8cc1Swenshuai.xi static IPAPOOL_INFO IPAPool_Info[MAX_IPAPOOLSIZE];
192*53ee8cc1Swenshuai.xi 
193*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
194*53ee8cc1Swenshuai.xi //  Debug Functions
195*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
196*53ee8cc1Swenshuai.xi 
197*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
198*53ee8cc1Swenshuai.xi //  Local Functions
199*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
_findEmpty_IPA_Pool_Entry(MS_U32 * index)200*53ee8cc1Swenshuai.xi static MS_BOOL _findEmpty_IPA_Pool_Entry(MS_U32 *index)
201*53ee8cc1Swenshuai.xi {
202*53ee8cc1Swenshuai.xi     MS_BOOL find = FALSE;
203*53ee8cc1Swenshuai.xi     MS_U32 i;
204*53ee8cc1Swenshuai.xi 
205*53ee8cc1Swenshuai.xi     *index = 0;
206*53ee8cc1Swenshuai.xi     for (i = 0; i < MAX_IPAPOOLSIZE; i++)
207*53ee8cc1Swenshuai.xi     {
208*53ee8cc1Swenshuai.xi         if(IPAPool_Info[i].bIsUsed == FALSE)
209*53ee8cc1Swenshuai.xi         {
210*53ee8cc1Swenshuai.xi             find = TRUE;
211*53ee8cc1Swenshuai.xi             *index = i;
212*53ee8cc1Swenshuai.xi             break;
213*53ee8cc1Swenshuai.xi         }
214*53ee8cc1Swenshuai.xi     }
215*53ee8cc1Swenshuai.xi 
216*53ee8cc1Swenshuai.xi     if(find == FALSE)
217*53ee8cc1Swenshuai.xi         VPRINTF("Not enough IPAPool, must increase MAX_IPAPOOLSIZE!!\n");
218*53ee8cc1Swenshuai.xi 
219*53ee8cc1Swenshuai.xi     return find;
220*53ee8cc1Swenshuai.xi }
221*53ee8cc1Swenshuai.xi 
222*53ee8cc1Swenshuai.xi 
_findPoolHandleId_InIPA_Pool_Table(MS_U32 pool_handle_id,MS_U32 * index)223*53ee8cc1Swenshuai.xi static MS_BOOL _findPoolHandleId_InIPA_Pool_Table(MS_U32 pool_handle_id, MS_U32 *index)
224*53ee8cc1Swenshuai.xi {
225*53ee8cc1Swenshuai.xi     MS_BOOL find = FALSE;
226*53ee8cc1Swenshuai.xi     MS_U32 i;
227*53ee8cc1Swenshuai.xi 
228*53ee8cc1Swenshuai.xi     *index = 0;
229*53ee8cc1Swenshuai.xi     for (i = 0; i < MAX_IPAPOOLSIZE; i++)
230*53ee8cc1Swenshuai.xi     {
231*53ee8cc1Swenshuai.xi         if((IPAPool_Info[i].bIsUsed == TRUE) && (IPAPool_Info[i].Init_Param.pool_handle_id == pool_handle_id))
232*53ee8cc1Swenshuai.xi         {
233*53ee8cc1Swenshuai.xi             find = TRUE;
234*53ee8cc1Swenshuai.xi             *index = i;
235*53ee8cc1Swenshuai.xi             break;
236*53ee8cc1Swenshuai.xi         }
237*53ee8cc1Swenshuai.xi     }
238*53ee8cc1Swenshuai.xi 
239*53ee8cc1Swenshuai.xi     return find;
240*53ee8cc1Swenshuai.xi }
241*53ee8cc1Swenshuai.xi 
_findHeapId_InIPA_Pool_Table(struct IPA_Pool_Init_Param * Init_Param,MS_U32 * index)242*53ee8cc1Swenshuai.xi static MS_BOOL _findHeapId_InIPA_Pool_Table(struct IPA_Pool_Init_Param * Init_Param,MS_U32 *index)
243*53ee8cc1Swenshuai.xi {
244*53ee8cc1Swenshuai.xi     MS_BOOL find = FALSE;
245*53ee8cc1Swenshuai.xi     MS_U32 i;
246*53ee8cc1Swenshuai.xi 
247*53ee8cc1Swenshuai.xi     *index = 0;
248*53ee8cc1Swenshuai.xi     for (i = 0; i < MAX_IPAPOOLSIZE; i++)
249*53ee8cc1Swenshuai.xi     {
250*53ee8cc1Swenshuai.xi         if((IPAPool_Info[i].bIsUsed == TRUE)
251*53ee8cc1Swenshuai.xi 			&& (IPAPool_Info[i].Init_Param.heap_id == Init_Param->space_id)
252*53ee8cc1Swenshuai.xi 			&&(!strncmp((char *)(intptr_t)IPAPool_Info[i].Init_Param.pool_name, Init_Param->pool_name,strlen(Init_Param->pool_name)))
253*53ee8cc1Swenshuai.xi 			&& (IPAPool_Info[i].Init_Param.offset_in_heap == Init_Param->offset_in_heap)
254*53ee8cc1Swenshuai.xi 			&& (IPAPool_Info[i].Init_Param.len == Init_Param->len))
255*53ee8cc1Swenshuai.xi         {
256*53ee8cc1Swenshuai.xi             find = TRUE;
257*53ee8cc1Swenshuai.xi             *index = i;
258*53ee8cc1Swenshuai.xi             break;
259*53ee8cc1Swenshuai.xi         }
260*53ee8cc1Swenshuai.xi     }
261*53ee8cc1Swenshuai.xi 
262*53ee8cc1Swenshuai.xi     return find;
263*53ee8cc1Swenshuai.xi }
264*53ee8cc1Swenshuai.xi 
265*53ee8cc1Swenshuai.xi //N.B.  This API only for each module debug code use,in each module release code,please do not call it !!!
266*53ee8cc1Swenshuai.xi //in:pa value
267*53ee8cc1Swenshuai.xi //out:whether in miu/heap/pool,and info about miu/heap/pool,and if in pool whether allocated.
268*53ee8cc1Swenshuai.xi //return value:only allocated in pool will return TRUE,otherwise return FALSE.
PA_in_IPA_POOL_info(struct PA_In_IPA_Pool_Param * in_ipa_pool_info)269*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) PA_in_IPA_POOL_info(struct PA_In_IPA_Pool_Param * in_ipa_pool_info)
270*53ee8cc1Swenshuai.xi {
271*53ee8cc1Swenshuai.xi     MS_BOOL ret = FALSE;
272*53ee8cc1Swenshuai.xi     int res = 0;
273*53ee8cc1Swenshuai.xi     struct PA_In_IPA_Pool_Args in_ipa_pool_info_args;
274*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
275*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool <= 0)//may never open device
276*53ee8cc1Swenshuai.xi     {
277*53ee8cc1Swenshuai.xi         if ((_s32FdIPAPool = open("/dev/ipapool", O_RDWR)) < 0)
278*53ee8cc1Swenshuai.xi         {
279*53ee8cc1Swenshuai.xi             VPRINTF("open /dev/ipapool fail\n");
280*53ee8cc1Swenshuai.xi             ret =  FALSE;
281*53ee8cc1Swenshuai.xi             goto PA_in_IPA_POOL_info;
282*53ee8cc1Swenshuai.xi         }
283*53ee8cc1Swenshuai.xi 
284*53ee8cc1Swenshuai.xi         memset(IPAPool_Info, 0, sizeof(IPAPOOL_INFO)*MAX_IPAPOOLSIZE);
285*53ee8cc1Swenshuai.xi     }
286*53ee8cc1Swenshuai.xi     in_ipa_pool_info_args.PA = in_ipa_pool_info->PA;
287*53ee8cc1Swenshuai.xi     res = ioctl(_s32FdIPAPool, IPA_POOL_IOC_PA_INFO, &in_ipa_pool_info_args);
288*53ee8cc1Swenshuai.xi     if (res < 0 ||  in_ipa_pool_info_args.error_code < 0)
289*53ee8cc1Swenshuai.xi     {
290*53ee8cc1Swenshuai.xi         ret =  FALSE;//ioctl fail,return false.
291*53ee8cc1Swenshuai.xi         VPRINTF("ipa error: %s , PA 0x%lx,res=%d ,error_code=0x%x\n",__FUNCTION__, (unsigned long)in_ipa_pool_info_args.PA,res,in_ipa_pool_info_args.error_code);
292*53ee8cc1Swenshuai.xi         goto PA_in_IPA_POOL_info;
293*53ee8cc1Swenshuai.xi 
294*53ee8cc1Swenshuai.xi     }
295*53ee8cc1Swenshuai.xi     VPRINTF(" %s  PA:0x%lx   pa_state=%d\n",__FUNCTION__, (unsigned long)in_ipa_pool_info_args.PA,in_ipa_pool_info_args.pa_state);
296*53ee8cc1Swenshuai.xi 
297*53ee8cc1Swenshuai.xi      in_ipa_pool_info->pa_state= in_ipa_pool_info_args.pa_state;
298*53ee8cc1Swenshuai.xi      in_ipa_pool_info->miu= in_ipa_pool_info_args.miu;
299*53ee8cc1Swenshuai.xi      if(in_ipa_pool_info->miu < 0)
300*53ee8cc1Swenshuai.xi      {
301*53ee8cc1Swenshuai.xi         VPRINTF("ipa error: %s failed, PA 0x%lx,miu=%d\n",__FUNCTION__, (unsigned long)in_ipa_pool_info_args.PA,in_ipa_pool_info_args.miu);
302*53ee8cc1Swenshuai.xi         in_ipa_pool_info->in_heap = FALSE;
303*53ee8cc1Swenshuai.xi         in_ipa_pool_info->allocated = FALSE;
304*53ee8cc1Swenshuai.xi         ret =  TRUE;//ioctl success,but not in miu,return TRUE!!!
305*53ee8cc1Swenshuai.xi         goto PA_in_IPA_POOL_info;
306*53ee8cc1Swenshuai.xi      }
307*53ee8cc1Swenshuai.xi      in_ipa_pool_info->in_heap= in_ipa_pool_info_args.in_heap;
308*53ee8cc1Swenshuai.xi      if(FALSE == in_ipa_pool_info->in_heap)
309*53ee8cc1Swenshuai.xi      {
310*53ee8cc1Swenshuai.xi         VPRINTF("ipa error: %s failed, PA 0x%lx,miu=%d,in_heap is FALSE\n",__FUNCTION__, (unsigned long)in_ipa_pool_info_args.PA,in_ipa_pool_info_args.miu);
311*53ee8cc1Swenshuai.xi         in_ipa_pool_info->allocated = FALSE;
312*53ee8cc1Swenshuai.xi         ret =  TRUE;//ioctl success,but not in heap,return TRUE!!!
313*53ee8cc1Swenshuai.xi         goto PA_in_IPA_POOL_info;
314*53ee8cc1Swenshuai.xi      }
315*53ee8cc1Swenshuai.xi      else
316*53ee8cc1Swenshuai.xi      {
317*53ee8cc1Swenshuai.xi          in_ipa_pool_info->space_id= in_ipa_pool_info_args.heap_id;
318*53ee8cc1Swenshuai.xi          in_ipa_pool_info->space_type= in_ipa_pool_info_args.heap_type;
319*53ee8cc1Swenshuai.xi          in_ipa_pool_info->space_miu_start_offset= in_ipa_pool_info_args.heap_miu_start_offset;
320*53ee8cc1Swenshuai.xi          in_ipa_pool_info->space_length= in_ipa_pool_info_args.heap_length;
321*53ee8cc1Swenshuai.xi          in_ipa_pool_info->pa_offset_in_heap= in_ipa_pool_info_args.pa_offset_in_heap;
322*53ee8cc1Swenshuai.xi 
323*53ee8cc1Swenshuai.xi          in_ipa_pool_info->allocated = in_ipa_pool_info_args.allocated;
324*53ee8cc1Swenshuai.xi          if(FALSE == in_ipa_pool_info->allocated)
325*53ee8cc1Swenshuai.xi          {
326*53ee8cc1Swenshuai.xi              VPRINTF("ipa error: %s failed, PA 0x%lx,miu=%d,in_ipa_pool_info_args.heap_id=%d,allocated is FALSE\n",__FUNCTION__, (unsigned long)in_ipa_pool_info_args.PA,in_ipa_pool_info_args.miu,in_ipa_pool_info_args.heap_id);
327*53ee8cc1Swenshuai.xi              ret =  TRUE;//ioctl success,but not in pool(not allocated),return TRUE!!!
328*53ee8cc1Swenshuai.xi              goto PA_in_IPA_POOL_info;
329*53ee8cc1Swenshuai.xi          }
330*53ee8cc1Swenshuai.xi          else
331*53ee8cc1Swenshuai.xi          {
332*53ee8cc1Swenshuai.xi              //in_ipa_pool_info->pool_handle_id= in_ipa_pool_info_args.pool_handle_id;
333*53ee8cc1Swenshuai.xi #if defined (__aarch64__)
334*53ee8cc1Swenshuai.xi             strcpy((char *)in_ipa_pool_info->pool_name , (char *)in_ipa_pool_info_args.pool_name);
335*53ee8cc1Swenshuai.xi #else
336*53ee8cc1Swenshuai.xi             strcpy((char *)in_ipa_pool_info->pool_name ,(char *)in_ipa_pool_info_args.pool_name);
337*53ee8cc1Swenshuai.xi #endif
338*53ee8cc1Swenshuai.xi             in_ipa_pool_info->pool_len = in_ipa_pool_info_args.pool_len;
339*53ee8cc1Swenshuai.xi             in_ipa_pool_info->pool_offset_in_heap = in_ipa_pool_info_args.pool_offset_in_heap;
340*53ee8cc1Swenshuai.xi             in_ipa_pool_info->pa_offset_in_pool = in_ipa_pool_info_args.pa_offset_in_pool;
341*53ee8cc1Swenshuai.xi         }
342*53ee8cc1Swenshuai.xi     }
343*53ee8cc1Swenshuai.xi     ret =  TRUE;
344*53ee8cc1Swenshuai.xi 
345*53ee8cc1Swenshuai.xi PA_in_IPA_POOL_info:
346*53ee8cc1Swenshuai.xi 
347*53ee8cc1Swenshuai.xi    pthread_mutex_unlock(&_IPA_POOL_Mutex);
348*53ee8cc1Swenshuai.xi    return ret;
349*53ee8cc1Swenshuai.xi }
350*53ee8cc1Swenshuai.xi 
IN_IPA_POOL_To_PA(struct Pool_To_PA_Param * pool_to_pa_param)351*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) IN_IPA_POOL_To_PA(struct Pool_To_PA_Param * pool_to_pa_param)
352*53ee8cc1Swenshuai.xi {
353*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
354*53ee8cc1Swenshuai.xi     struct Pool_To_PA_Args pool_to_pa_args;
355*53ee8cc1Swenshuai.xi     int res = 0;
356*53ee8cc1Swenshuai.xi 
357*53ee8cc1Swenshuai.xi     VPRINTF("%s handle_id 0x%x offset_in_pool 0x%lx\n",
358*53ee8cc1Swenshuai.xi         __FUNCTION__,pool_to_pa_param->pool_handle_id, (unsigned long)pool_to_pa_param->offset_in_pool);
359*53ee8cc1Swenshuai.xi 
360*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
361*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool < 0)
362*53ee8cc1Swenshuai.xi     {
363*53ee8cc1Swenshuai.xi         ret = FALSE;
364*53ee8cc1Swenshuai.xi         goto POOL_To_PA_DONE;
365*53ee8cc1Swenshuai.xi     }
366*53ee8cc1Swenshuai.xi 
367*53ee8cc1Swenshuai.xi     //input
368*53ee8cc1Swenshuai.xi     pool_to_pa_args.pool_handle_id = pool_to_pa_param->pool_handle_id;
369*53ee8cc1Swenshuai.xi     pool_to_pa_args.offset_in_pool = pool_to_pa_param->offset_in_pool;
370*53ee8cc1Swenshuai.xi 
371*53ee8cc1Swenshuai.xi     res = ioctl(_s32FdIPAPool, IPA_POOL_IOC_POOL_TO_PA, &pool_to_pa_args);
372*53ee8cc1Swenshuai.xi     if (res < 0 || pool_to_pa_args.error_code != IPAERROR_OK)
373*53ee8cc1Swenshuai.xi     {
374*53ee8cc1Swenshuai.xi         VPRINTF("%s fail: pool_handle_id %u, offset 0x%lx, error_code=0x%x  res=%d\n",__FUNCTION__, pool_to_pa_param->pool_handle_id,
375*53ee8cc1Swenshuai.xi             (unsigned long)pool_to_pa_param->offset_in_pool,pool_to_pa_args.error_code,res);
376*53ee8cc1Swenshuai.xi         ret = FALSE;
377*53ee8cc1Swenshuai.xi         goto POOL_To_PA_DONE;
378*53ee8cc1Swenshuai.xi     }
379*53ee8cc1Swenshuai.xi 
380*53ee8cc1Swenshuai.xi     //output
381*53ee8cc1Swenshuai.xi     pool_to_pa_param->PA = pool_to_pa_args.PA;
382*53ee8cc1Swenshuai.xi     pool_to_pa_param->error_code = pool_to_pa_args.error_code;
383*53ee8cc1Swenshuai.xi     pool_to_pa_param->miu = pool_to_pa_args.miu;
384*53ee8cc1Swenshuai.xi     pool_to_pa_param->heap_id = pool_to_pa_args.heap_id;
385*53ee8cc1Swenshuai.xi 
386*53ee8cc1Swenshuai.xi POOL_To_PA_DONE:
387*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
388*53ee8cc1Swenshuai.xi 
389*53ee8cc1Swenshuai.xi     return ret;
390*53ee8cc1Swenshuai.xi }
391*53ee8cc1Swenshuai.xi 
392*53ee8cc1Swenshuai.xi 
393*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
394*53ee8cc1Swenshuai.xi //  Global Functions
395*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
396*53ee8cc1Swenshuai.xi 
397*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
398*53ee8cc1Swenshuai.xi /// System initialzation
399*53ee8cc1Swenshuai.xi /// @return TRUE(Success), FALSE(Failure)
400*53ee8cc1Swenshuai.xi //-------------------------------------------------------------------------------------------------
MApi_IPA_Pool_Init(struct IPA_Pool_Init_Param * Init_Param)401*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) MApi_IPA_Pool_Init(struct IPA_Pool_Init_Param * Init_Param)
402*53ee8cc1Swenshuai.xi {
403*53ee8cc1Swenshuai.xi     struct IPA_Pool_Init_Args ipa_init_args;
404*53ee8cc1Swenshuai.xi     MS_U64 u64PhyAddr = 0;
405*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
406*53ee8cc1Swenshuai.xi     MS_U32 idx = 0;
407*53ee8cc1Swenshuai.xi     int res = 0;
408*53ee8cc1Swenshuai.xi     //VPRINTF("%s start\n",__FUNCTION__);
409*53ee8cc1Swenshuai.xi     VPRINTF("%s heap_id %u\n",__FUNCTION__, Init_Param->space_id);
410*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
411*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool <= 0)
412*53ee8cc1Swenshuai.xi     {
413*53ee8cc1Swenshuai.xi         if ((_s32FdIPAPool = open("/dev/ipapool", O_RDWR)) < 0)
414*53ee8cc1Swenshuai.xi         {
415*53ee8cc1Swenshuai.xi             VPRINTF("open /dev/ipapool fail\n");
416*53ee8cc1Swenshuai.xi             ret =  FALSE;
417*53ee8cc1Swenshuai.xi             goto IPA_POOL_INIT_DONE;
418*53ee8cc1Swenshuai.xi         }
419*53ee8cc1Swenshuai.xi 
420*53ee8cc1Swenshuai.xi         memset(IPAPool_Info, 0, sizeof(IPAPOOL_INFO)*MAX_IPAPOOLSIZE);
421*53ee8cc1Swenshuai.xi     }
422*53ee8cc1Swenshuai.xi 
423*53ee8cc1Swenshuai.xi 
424*53ee8cc1Swenshuai.xi     //avoid mmap more than one time
425*53ee8cc1Swenshuai.xi     ret = _findHeapId_InIPA_Pool_Table(Init_Param, &idx);
426*53ee8cc1Swenshuai.xi     if(ret == TRUE)
427*53ee8cc1Swenshuai.xi     {
428*53ee8cc1Swenshuai.xi          //memcpy(Init_Param,IPAPool_Info[idx].Init_Param,sizeof(struct IPA_Pool_Init_Param));
429*53ee8cc1Swenshuai.xi          Init_Param->space_id = IPAPool_Info[idx].Init_Param.heap_id;
430*53ee8cc1Swenshuai.xi          Init_Param->pool_name = (char *)(intptr_t)IPAPool_Info[idx].Init_Param.pool_name;
431*53ee8cc1Swenshuai.xi          Init_Param->offset_in_heap = IPAPool_Info[idx].Init_Param.offset_in_heap;
432*53ee8cc1Swenshuai.xi          Init_Param->len = IPAPool_Info[idx].Init_Param.len;
433*53ee8cc1Swenshuai.xi          Init_Param->pool_handle_id = IPAPool_Info[idx].Init_Param.pool_handle_id;
434*53ee8cc1Swenshuai.xi          Init_Param->miu = IPAPool_Info[idx].Init_Param.miu;
435*53ee8cc1Swenshuai.xi          Init_Param->space_type = IPAPool_Info[idx].Init_Param.heap_type;
436*53ee8cc1Swenshuai.xi          Init_Param->error_code = IPAPool_Info[idx].Init_Param.error_code;
437*53ee8cc1Swenshuai.xi          Init_Param->space_length = IPAPool_Info[idx].Init_Param.heap_length;
438*53ee8cc1Swenshuai.xi          Init_Param->space_miu_start_offset = IPAPool_Info[idx].Init_Param.heap_miu_start_offset;
439*53ee8cc1Swenshuai.xi 
440*53ee8cc1Swenshuai.xi          VPRINTF("pool_handle_id %u already init!\n", IPAPool_Info[idx].Init_Param.pool_handle_id);
441*53ee8cc1Swenshuai.xi          goto IPA_POOL_INIT_DONE;
442*53ee8cc1Swenshuai.xi     }
443*53ee8cc1Swenshuai.xi 
444*53ee8cc1Swenshuai.xi     ipa_init_args.heap_id = Init_Param->space_id;
445*53ee8cc1Swenshuai.xi #if defined (__aarch64__)
446*53ee8cc1Swenshuai.xi     strcpy((char *)ipa_init_args.pool_name , Init_Param->pool_name);
447*53ee8cc1Swenshuai.xi #else
448*53ee8cc1Swenshuai.xi     strcpy((char *)ipa_init_args.pool_name ,Init_Param->pool_name);
449*53ee8cc1Swenshuai.xi #endif
450*53ee8cc1Swenshuai.xi     ipa_init_args.offset_in_heap = Init_Param->offset_in_heap;
451*53ee8cc1Swenshuai.xi     ipa_init_args.len = Init_Param->len;
452*53ee8cc1Swenshuai.xi     res = ioctl(_s32FdIPAPool, IPA_POOL_IOC_INIT, &ipa_init_args);
453*53ee8cc1Swenshuai.xi     if (res < 0 || ipa_init_args.error_code != IPAERROR_OK)
454*53ee8cc1Swenshuai.xi     {
455*53ee8cc1Swenshuai.xi         VPRINTF("ipa error: ipa init failed, heapid %u error_code %d\n", Init_Param->space_id, ipa_init_args.error_code);
456*53ee8cc1Swenshuai.xi         ret =  FALSE;
457*53ee8cc1Swenshuai.xi         goto IPA_POOL_INIT_CLOSE;
458*53ee8cc1Swenshuai.xi     }
459*53ee8cc1Swenshuai.xi 
460*53ee8cc1Swenshuai.xi     VPRINTF("%s heap_id %u, pool_handle_id %u heap_length %llu\n",
461*53ee8cc1Swenshuai.xi 		__FUNCTION__,Init_Param->space_id, ipa_init_args.pool_handle_id, (long long unsigned int)ipa_init_args.heap_length);
462*53ee8cc1Swenshuai.xi 
463*53ee8cc1Swenshuai.xi     Init_Param->pool_handle_id = ipa_init_args.pool_handle_id;
464*53ee8cc1Swenshuai.xi     Init_Param->miu = ipa_init_args.miu;
465*53ee8cc1Swenshuai.xi     Init_Param->space_type = ipa_init_args.heap_type;
466*53ee8cc1Swenshuai.xi     Init_Param->error_code = ipa_init_args.error_code;
467*53ee8cc1Swenshuai.xi     Init_Param->space_length = ipa_init_args.heap_length;
468*53ee8cc1Swenshuai.xi     Init_Param->space_miu_start_offset = ipa_init_args.heap_miu_start_offset;
469*53ee8cc1Swenshuai.xi     //VPRINTF("%s before _findEmpty_IPA_Pool_Entry\n",__FUNCTION__);
470*53ee8cc1Swenshuai.xi     ret = _findEmpty_IPA_Pool_Entry(&idx);
471*53ee8cc1Swenshuai.xi     //VPRINTF("%s after _findEmpty_IPA_Pool_Entry\n",__FUNCTION__);
472*53ee8cc1Swenshuai.xi     if(ret == FALSE)
473*53ee8cc1Swenshuai.xi     {
474*53ee8cc1Swenshuai.xi         VPRINTF("ipa error: pool_handle_id %u init failed!\n", ipa_init_args.pool_handle_id);
475*53ee8cc1Swenshuai.xi         goto IPA_POOL_INIT_DONE;
476*53ee8cc1Swenshuai.xi     }
477*53ee8cc1Swenshuai.xi     //VPRINTF("%s before _miu_offset_to_phy\n",__FUNCTION__);
478*53ee8cc1Swenshuai.xi     _miu_offset_to_phy(ipa_init_args.miu, ipa_init_args.heap_miu_start_offset, u64PhyAddr); // get miu base addr
479*53ee8cc1Swenshuai.xi     //VPRINTF("%s after _miu_offset_to_phy   idx=%d  ",__FUNCTION__,idx);
480*53ee8cc1Swenshuai.xi 
481*53ee8cc1Swenshuai.xi 
482*53ee8cc1Swenshuai.xi     #if defined (__aarch64__)//make sure when build no warning.
483*53ee8cc1Swenshuai.xi     VPRINTF("sizeof(struct IPA_Pool_Init_Param)=%lu\n",sizeof(struct IPA_Pool_Init_Param));
484*53ee8cc1Swenshuai.xi     #else
485*53ee8cc1Swenshuai.xi     VPRINTF("sizeof(struct IPA_Pool_Init_Param)=%u\n",sizeof(struct IPA_Pool_Init_Param));
486*53ee8cc1Swenshuai.xi     #endif
487*53ee8cc1Swenshuai.xi 
488*53ee8cc1Swenshuai.xi     /* IPA Pool setting*/
489*53ee8cc1Swenshuai.xi     //VPRINTF("%s before memcpy\n",__FUNCTION__);
490*53ee8cc1Swenshuai.xi     //memcpy(IPAPool_Info[idx].Init_Param,Init_Param,sizeof(struct IPA_Pool_Init_Param));
491*53ee8cc1Swenshuai.xi #if 1
492*53ee8cc1Swenshuai.xi     //VPRINTF("%s before heap_id\n",__FUNCTION__);
493*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].Init_Param.heap_id = Init_Param->space_id;
494*53ee8cc1Swenshuai.xi 
495*53ee8cc1Swenshuai.xi     //VPRINTF("%s before pool_name \n",__FUNCTION__);
496*53ee8cc1Swenshuai.xi #if defined (__aarch64__)
497*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].Init_Param.pool_name = (MS_U64)Init_Param->pool_name;
498*53ee8cc1Swenshuai.xi #else
499*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].Init_Param.pool_name = (MS_U32)Init_Param->pool_name;
500*53ee8cc1Swenshuai.xi #endif
501*53ee8cc1Swenshuai.xi     //VPRINTF("%s before offset \n",__FUNCTION__);
502*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].Init_Param.offset_in_heap =Init_Param->offset_in_heap;
503*53ee8cc1Swenshuai.xi     //VPRINTF("%s before len \n",__FUNCTION__);
504*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].Init_Param.len =Init_Param->len;
505*53ee8cc1Swenshuai.xi     //VPRINTF("%s before pool_handle_id \n",__FUNCTION__);
506*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].Init_Param.pool_handle_id =Init_Param->pool_handle_id;
507*53ee8cc1Swenshuai.xi     //VPRINTF("%s before miu \n",__FUNCTION__);
508*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].Init_Param.miu =Init_Param->miu;
509*53ee8cc1Swenshuai.xi     //VPRINTF("%s before heap_type \n",__FUNCTION__);
510*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].Init_Param.heap_type =Init_Param->space_type;
511*53ee8cc1Swenshuai.xi     //VPRINTF("%s before error_code \n",__FUNCTION__);
512*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].Init_Param.error_code =Init_Param->error_code;
513*53ee8cc1Swenshuai.xi     //VPRINTF("%s before heap_length \n",__FUNCTION__);
514*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].Init_Param.heap_length =Init_Param->space_length;
515*53ee8cc1Swenshuai.xi     //VPRINTF("%s before heap_miu_start_offset \n",__FUNCTION__);
516*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].Init_Param.heap_miu_start_offset =Init_Param->space_miu_start_offset;
517*53ee8cc1Swenshuai.xi #endif
518*53ee8cc1Swenshuai.xi 
519*53ee8cc1Swenshuai.xi     //VPRINTF("%s after memcpy\n",__FUNCTION__);
520*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].bIsUsed = TRUE;
521*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].pthIPAPollingId = -1;//in init,no polling id yet.
522*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].polling_thread_delete_task_flag = 0;
523*53ee8cc1Swenshuai.xi 
524*53ee8cc1Swenshuai.xi     VPRINTF("%s heap_id %u pool_handle_id %u miu %u offset 0x%lx len 0x%lx  idx=%u\n",__FUNCTION__,
525*53ee8cc1Swenshuai.xi 		Init_Param->space_id, Init_Param->pool_handle_id, Init_Param->miu, (unsigned long)Init_Param->offset_in_heap, (unsigned long)Init_Param->len,idx);
526*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
527*53ee8cc1Swenshuai.xi     //VPRINTF("%s  before return %d\n",__FUNCTION__,ret);
528*53ee8cc1Swenshuai.xi     return ret;
529*53ee8cc1Swenshuai.xi 
530*53ee8cc1Swenshuai.xi IPA_POOL_INIT_CLOSE:
531*53ee8cc1Swenshuai.xi     //VPRINTF("%s  before close\n",__FUNCTION__);
532*53ee8cc1Swenshuai.xi     close(_s32FdIPAPool);
533*53ee8cc1Swenshuai.xi     //VPRINTF("%s  after close\n",__FUNCTION__);
534*53ee8cc1Swenshuai.xi IPA_POOL_INIT_DONE:
535*53ee8cc1Swenshuai.xi     //VPRINTF("%s  after IPA_POOL_INIT_DONE  ret=%d\n",__FUNCTION__,ret);
536*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
537*53ee8cc1Swenshuai.xi 
538*53ee8cc1Swenshuai.xi     return ret;
539*53ee8cc1Swenshuai.xi }
540*53ee8cc1Swenshuai.xi 
541*53ee8cc1Swenshuai.xi //MS_U32 u32flag. Special Flag for customer mem allocation
MApi_IPA_Pool_GetMem(struct IPA_Pool_GetMem_Param * get_param)542*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) MApi_IPA_Pool_GetMem(struct IPA_Pool_GetMem_Param * get_param)
543*53ee8cc1Swenshuai.xi {
544*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
545*53ee8cc1Swenshuai.xi     struct IPA_Pool_Alloc_Args ipa_alloc_args;
546*53ee8cc1Swenshuai.xi     int res = 0;
547*53ee8cc1Swenshuai.xi 
548*53ee8cc1Swenshuai.xi     //VPRINTF("%s handle_id 0x%x length 0x%lx\n",__FUNCTION__,
549*53ee8cc1Swenshuai.xi 	//	get_param->pool_handle_id, (unsigned long)get_param->length);
550*53ee8cc1Swenshuai.xi 
551*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
552*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool < 0)
553*53ee8cc1Swenshuai.xi     {
554*53ee8cc1Swenshuai.xi         ret = FALSE;
555*53ee8cc1Swenshuai.xi         goto IPA_POOL_GETMEM_DONE;
556*53ee8cc1Swenshuai.xi     }
557*53ee8cc1Swenshuai.xi 
558*53ee8cc1Swenshuai.xi     ipa_alloc_args.pool_handle_id = get_param->pool_handle_id;
559*53ee8cc1Swenshuai.xi     ipa_alloc_args.offset_in_pool = get_param->offset_in_pool;
560*53ee8cc1Swenshuai.xi     ipa_alloc_args.length = get_param->length;
561*53ee8cc1Swenshuai.xi     ipa_alloc_args.timeout = 0;// 0 means if fail ,will not try again
562*53ee8cc1Swenshuai.xi     VPRINTF("%s %d pool_handle_id 0x%x [offset_in_pool ,length]=[0x%lx,0x%lx]\n",__FUNCTION__,__LINE__,
563*53ee8cc1Swenshuai.xi 		ipa_alloc_args.pool_handle_id,(unsigned long)ipa_alloc_args.offset_in_pool, (unsigned long)ipa_alloc_args.length);
564*53ee8cc1Swenshuai.xi     res = ioctl(_s32FdIPAPool, IPA_POOL_IOC_ALLOC, &ipa_alloc_args);
565*53ee8cc1Swenshuai.xi     if (res < 0 || ipa_alloc_args.error_code != IPAERROR_OK)
566*53ee8cc1Swenshuai.xi     {
567*53ee8cc1Swenshuai.xi         VPRINTF("%s fail: pool_handle_id %u, offset 0x%lx, len 0x%lx  ipa_alloc_args.error_code=0x%x  res=%d\n",__FUNCTION__, get_param->pool_handle_id,
568*53ee8cc1Swenshuai.xi 			(unsigned long)get_param->offset_in_pool, (unsigned long)get_param->length,ipa_alloc_args.error_code,res);
569*53ee8cc1Swenshuai.xi         ret = FALSE;
570*53ee8cc1Swenshuai.xi         goto IPA_POOL_GETMEM_DONE;
571*53ee8cc1Swenshuai.xi     }
572*53ee8cc1Swenshuai.xi 
573*53ee8cc1Swenshuai.xi     get_param->error_code = ipa_alloc_args.error_code;
574*53ee8cc1Swenshuai.xi 
575*53ee8cc1Swenshuai.xi IPA_POOL_GETMEM_DONE:
576*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
577*53ee8cc1Swenshuai.xi 
578*53ee8cc1Swenshuai.xi     return ret;
579*53ee8cc1Swenshuai.xi }
580*53ee8cc1Swenshuai.xi 
MApi_IPA_Pool_PutMem(struct IPA_Pool_PutMem_Param * put_param)581*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) MApi_IPA_Pool_PutMem(struct IPA_Pool_PutMem_Param * put_param)
582*53ee8cc1Swenshuai.xi {
583*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
584*53ee8cc1Swenshuai.xi     struct IPA_Pool_free_Args ipa_free_args;
585*53ee8cc1Swenshuai.xi 
586*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
587*53ee8cc1Swenshuai.xi     if(_s32FdIPAPool < 0)
588*53ee8cc1Swenshuai.xi     {
589*53ee8cc1Swenshuai.xi         ret = FALSE;
590*53ee8cc1Swenshuai.xi         goto IPA_POOL_PUTMEM_DONE;
591*53ee8cc1Swenshuai.xi     }
592*53ee8cc1Swenshuai.xi 
593*53ee8cc1Swenshuai.xi     ipa_free_args.pool_handle_id = put_param->pool_handle_id;
594*53ee8cc1Swenshuai.xi     ipa_free_args.offset_in_pool = put_param->offset_in_pool;
595*53ee8cc1Swenshuai.xi     ipa_free_args.length = put_param->length;
596*53ee8cc1Swenshuai.xi     VPRINTF("%s %d pool_handle_id 0x%x [offset_in_pool,length]=[0x%lx,0x%lx]\n", __FUNCTION__,__LINE__,
597*53ee8cc1Swenshuai.xi 		ipa_free_args.pool_handle_id, (unsigned long)ipa_free_args.offset_in_pool, (unsigned long)ipa_free_args.length);
598*53ee8cc1Swenshuai.xi 
599*53ee8cc1Swenshuai.xi     if(ioctl(_s32FdIPAPool, IPA_POOL_IOC_FREE, &ipa_free_args))
600*53ee8cc1Swenshuai.xi     {
601*53ee8cc1Swenshuai.xi         VPRINTF("%s fail: pool_handle_id 0x%x, offset 0x%lx, len 0x%lx\n",__FUNCTION__,
602*53ee8cc1Swenshuai.xi 			put_param->pool_handle_id, (unsigned long)put_param->offset_in_pool,(unsigned long)put_param->length);
603*53ee8cc1Swenshuai.xi         ret = FALSE;
604*53ee8cc1Swenshuai.xi         goto IPA_POOL_PUTMEM_DONE;
605*53ee8cc1Swenshuai.xi     }
606*53ee8cc1Swenshuai.xi 
607*53ee8cc1Swenshuai.xi IPA_POOL_PUTMEM_DONE:
608*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
609*53ee8cc1Swenshuai.xi 
610*53ee8cc1Swenshuai.xi     return ret;
611*53ee8cc1Swenshuai.xi }
612*53ee8cc1Swenshuai.xi 
MApi_IPA_Pool_Release(MS_U32 pool_handle_id)613*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) MApi_IPA_Pool_Release(MS_U32 pool_handle_id)
614*53ee8cc1Swenshuai.xi {
615*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
616*53ee8cc1Swenshuai.xi     struct IPA_Pool_Deinit_Args deinit_args;
617*53ee8cc1Swenshuai.xi     struct IPA_Pool_Unmap_Args unmap_args;
618*53ee8cc1Swenshuai.xi 
619*53ee8cc1Swenshuai.xi     MS_U32 idx = 0;
620*53ee8cc1Swenshuai.xi     int res = 0,i=0;
621*53ee8cc1Swenshuai.xi 
622*53ee8cc1Swenshuai.xi     VPRINTF("%s handle_id %u\n",__FUNCTION__, pool_handle_id);
623*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
624*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool < 0)
625*53ee8cc1Swenshuai.xi     {
626*53ee8cc1Swenshuai.xi         ret = FALSE;
627*53ee8cc1Swenshuai.xi         goto IPA_POOL_RELEASE_DONE;
628*53ee8cc1Swenshuai.xi     }
629*53ee8cc1Swenshuai.xi 
630*53ee8cc1Swenshuai.xi     ret = _findPoolHandleId_InIPA_Pool_Table(pool_handle_id, &idx);
631*53ee8cc1Swenshuai.xi     if(ret == FALSE)
632*53ee8cc1Swenshuai.xi     {
633*53ee8cc1Swenshuai.xi         VPRINTF("pool_handle_id %u _findPoolHandleId_InIPA_Pool_Table fail\n", pool_handle_id);
634*53ee8cc1Swenshuai.xi         goto IPA_POOL_RELEASE_DONE;
635*53ee8cc1Swenshuai.xi     }
636*53ee8cc1Swenshuai.xi 
637*53ee8cc1Swenshuai.xi     for(i=0;i<MAX_CLIENT_MAP_NUM;i++)
638*53ee8cc1Swenshuai.xi     {
639*53ee8cc1Swenshuai.xi         // tmp_map_info
640*53ee8cc1Swenshuai.xi         if((0 !=IPAPool_Info[idx].map_info[i].virt_addr)
641*53ee8cc1Swenshuai.xi 		    && (0 != IPAPool_Info[idx].map_info[i].length))
642*53ee8cc1Swenshuai.xi         {
643*53ee8cc1Swenshuai.xi             unmap_args.virt_addr =	IPAPool_Info[idx].map_info[i].virt_addr;
644*53ee8cc1Swenshuai.xi             unmap_args.length = IPAPool_Info[idx].map_info[i].length;
645*53ee8cc1Swenshuai.xi 
646*53ee8cc1Swenshuai.xi             MsOS_MPool_Remove_PA2VARange(IPAPool_Info[idx].map_info[i].Physaddr, IPAPool_Info[idx].map_info[i].virt_addr, IPAPool_Info[idx].map_info[i].length, IPAPool_Info[idx].map_info[i].bNonCache);
647*53ee8cc1Swenshuai.xi             res = ioctl(_s32FdIPAPool, IPA_POOL_IOC_UNMAP, &unmap_args);
648*53ee8cc1Swenshuai.xi             if (res < 0)
649*53ee8cc1Swenshuai.xi             {
650*53ee8cc1Swenshuai.xi                 //give printf,but not end.
651*53ee8cc1Swenshuai.xi                 VPRINTF("error: pool_handle_id %u unmap failed\n", pool_handle_id);
652*53ee8cc1Swenshuai.xi             }
653*53ee8cc1Swenshuai.xi 
654*53ee8cc1Swenshuai.xi             IPAPool_Info[idx].map_info[i].virt_addr = 0;
655*53ee8cc1Swenshuai.xi             IPAPool_Info[idx].map_info[i].length = 0;
656*53ee8cc1Swenshuai.xi         }
657*53ee8cc1Swenshuai.xi     }
658*53ee8cc1Swenshuai.xi 
659*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].bIsUsed = FALSE;
660*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].Init_Param.pool_handle_id = 0;
661*53ee8cc1Swenshuai.xi 
662*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].polling_thread_delete_task_flag = 2; //request thread to exit
663*53ee8cc1Swenshuai.xi     deinit_args.pool_handle_id = pool_handle_id;
664*53ee8cc1Swenshuai.xi 
665*53ee8cc1Swenshuai.xi     if (ioctl(_s32FdIPAPool, IPA_POOL_IOC_DEINIT, &deinit_args))
666*53ee8cc1Swenshuai.xi     {
667*53ee8cc1Swenshuai.xi         VPRINTF("pool_handle_id %u deinit fail\n", pool_handle_id);
668*53ee8cc1Swenshuai.xi         ret = FALSE;
669*53ee8cc1Swenshuai.xi         goto IPA_POOL_RELEASE_DONE;
670*53ee8cc1Swenshuai.xi     }
671*53ee8cc1Swenshuai.xi     ret = TRUE; //not find the pool hand id
672*53ee8cc1Swenshuai.xi 
673*53ee8cc1Swenshuai.xi     //after deinit, if have polling thread,should delete task
674*53ee8cc1Swenshuai.xi     if(IPAPool_Info[idx].pthIPAPollingId != -1)
675*53ee8cc1Swenshuai.xi     {
676*53ee8cc1Swenshuai.xi         #if 0//no need this while,for later pthread_join + pthread_cancel will wait for set delete_task_flag.
677*53ee8cc1Swenshuai.xi         while(1)
678*53ee8cc1Swenshuai.xi         {
679*53ee8cc1Swenshuai.xi             if(1 == IPAPool_Info[idx].polling_thread_delete_task_flag)
680*53ee8cc1Swenshuai.xi                 break;
681*53ee8cc1Swenshuai.xi         }
682*53ee8cc1Swenshuai.xi         #endif
683*53ee8cc1Swenshuai.xi 
684*53ee8cc1Swenshuai.xi         //N.B. here we do not directly use MsOS_DeleteTask but use pthread_join + pthread_cancel,
685*53ee8cc1Swenshuai.xi         //For in MsOS_DeleteTask,first use pthread_cancel and then use pthread_join and that will
686*53ee8cc1Swenshuai.xi         //cause hang.
687*53ee8cc1Swenshuai.xi         //MsOS_DeleteTask (IPAPool_Info[idx].pthIPAPollingId);//delete polling task
688*53ee8cc1Swenshuai.xi         pthread_join(IPAPool_Info[idx].pthIPAPollingId, NULL);
689*53ee8cc1Swenshuai.xi 
690*53ee8cc1Swenshuai.xi         #if 0
691*53ee8cc1Swenshuai.xi         //build SN lib can find pthread_cancel,
692*53ee8cc1Swenshuai.xi         //but build AN lib,can not find pthread_cancel,
693*53ee8cc1Swenshuai.xi         //in fact only need pthread_join,no need pthread_cancel.
694*53ee8cc1Swenshuai.xi         pthread_cancel(IPAPool_Info[idx].pthIPAPollingId);//delete polling task
695*53ee8cc1Swenshuai.xi         #endif
696*53ee8cc1Swenshuai.xi 
697*53ee8cc1Swenshuai.xi         if(1 != IPAPool_Info[idx].polling_thread_delete_task_flag)
698*53ee8cc1Swenshuai.xi         {
699*53ee8cc1Swenshuai.xi             VPRINTF("error !!!!!!   after pthread_join  polling_thread_delete_task_flag is not 1!!!  idx=%u  IPAPool_Info[%d].polling_thread_delete_task_flag=%u\n",idx,idx,IPAPool_Info[idx].polling_thread_delete_task_flag);
700*53ee8cc1Swenshuai.xi             ret = FALSE;
701*53ee8cc1Swenshuai.xi         }
702*53ee8cc1Swenshuai.xi 
703*53ee8cc1Swenshuai.xi     }
704*53ee8cc1Swenshuai.xi 
705*53ee8cc1Swenshuai.xi 
706*53ee8cc1Swenshuai.xi IPA_POOL_RELEASE_DONE:
707*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
708*53ee8cc1Swenshuai.xi     return ret;
709*53ee8cc1Swenshuai.xi }
710*53ee8cc1Swenshuai.xi 
711*53ee8cc1Swenshuai.xi //allow a same input [pool_handle_id, offset_in_pool, length, cache_type] map for more than once,
712*53ee8cc1Swenshuai.xi //and get different result output [virt_addr]
MApi_IPA_Pool_MapUserVA(struct IPA_Pool_Map_Param * map_param)713*53ee8cc1Swenshuai.xi static MS_BOOL MApi_IPA_Pool_MapUserVA(struct IPA_Pool_Map_Param * map_param)
714*53ee8cc1Swenshuai.xi {
715*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
716*53ee8cc1Swenshuai.xi     int res = 0;
717*53ee8cc1Swenshuai.xi     struct IPA_Pool_Map_Args map_args;
718*53ee8cc1Swenshuai.xi     MS_U32 idx = 0;
719*53ee8cc1Swenshuai.xi     VPRINTF("%s pool_handle_id %u, offset_in_pool 0x%lx, len 0x%lx,  cache_type %u\n",__FUNCTION__, map_param->pool_handle_id,
720*53ee8cc1Swenshuai.xi 		(unsigned long)map_param->offset_in_pool, (unsigned long)map_param->length,map_param->cache_type);
721*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
722*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool < 0)
723*53ee8cc1Swenshuai.xi     {
724*53ee8cc1Swenshuai.xi         ret = FALSE;
725*53ee8cc1Swenshuai.xi         goto IPA_POOL_MapUserVA_DONE;
726*53ee8cc1Swenshuai.xi     }
727*53ee8cc1Swenshuai.xi 
728*53ee8cc1Swenshuai.xi     if(0 == map_param->length)
729*53ee8cc1Swenshuai.xi     {
730*53ee8cc1Swenshuai.xi         VPRINTF("%s  error, len 0x%lx is invalid\n",__FUNCTION__,  (unsigned long)map_param->length);
731*53ee8cc1Swenshuai.xi         ret = FALSE;
732*53ee8cc1Swenshuai.xi         goto IPA_POOL_MapUserVA_DONE;
733*53ee8cc1Swenshuai.xi     }
734*53ee8cc1Swenshuai.xi 
735*53ee8cc1Swenshuai.xi     map_args.pool_handle_id = map_param->pool_handle_id;
736*53ee8cc1Swenshuai.xi     map_args.offset_in_pool = map_param->offset_in_pool;
737*53ee8cc1Swenshuai.xi     map_args.length = map_param->length;
738*53ee8cc1Swenshuai.xi     map_args.map_va_type = map_param->cache_type;
739*53ee8cc1Swenshuai.xi 
740*53ee8cc1Swenshuai.xi     res = ioctl(_s32FdIPAPool, IPA_POOL_IOC_MAP, &map_args);
741*53ee8cc1Swenshuai.xi     //VPRINTF("MApi_IPA_Pool_MapUserVA after ioctl\n");
742*53ee8cc1Swenshuai.xi     if (res < 0 || map_args.error_code != IPAERROR_OK)
743*53ee8cc1Swenshuai.xi     {
744*53ee8cc1Swenshuai.xi         VPRINTF("%s  fail: pool_handle_id %u, offset_in_pool 0x%lx, len 0x%lx,  cache_type %u\n",__FUNCTION__, map_param->pool_handle_id,
745*53ee8cc1Swenshuai.xi 			(unsigned long)map_param->offset_in_pool, (unsigned long)map_param->length,map_param->cache_type);
746*53ee8cc1Swenshuai.xi         ret = FALSE;
747*53ee8cc1Swenshuai.xi         goto IPA_POOL_MapUserVA_DONE;
748*53ee8cc1Swenshuai.xi     }
749*53ee8cc1Swenshuai.xi     //VPRINTF("MApi_IPA_Pool_MapUserVA before virt_addr\n");
750*53ee8cc1Swenshuai.xi     map_param->virt_addr = map_args.virt_addr;
751*53ee8cc1Swenshuai.xi     map_param->error_code = map_args.error_code;
752*53ee8cc1Swenshuai.xi     //VPRINTF("MApi_IPA_Pool_MapUserVA before _findPoolHandleId_InIPA_Pool_Table\n");
753*53ee8cc1Swenshuai.xi 
754*53ee8cc1Swenshuai.xi     ret = _findPoolHandleId_InIPA_Pool_Table(map_param->pool_handle_id, &idx);
755*53ee8cc1Swenshuai.xi     //VPRINTF("MApi_IPA_Pool_MapUserVA after _findPoolHandleId_InIPA_Pool_Table\n");
756*53ee8cc1Swenshuai.xi     if(ret == TRUE)
757*53ee8cc1Swenshuai.xi     {
758*53ee8cc1Swenshuai.xi         int i=0;
759*53ee8cc1Swenshuai.xi         //VPRINTF("MApi_IPA_Pool_MapUserVA before for\n");
760*53ee8cc1Swenshuai.xi         for(i=0;i<MAX_CLIENT_MAP_NUM;i++)
761*53ee8cc1Swenshuai.xi         {
762*53ee8cc1Swenshuai.xi             //VPRINTF("MApi_IPA_Pool_MapUserVA before if\n");
763*53ee8cc1Swenshuai.xi             if((0 == IPAPool_Info[idx].map_info[i].virt_addr)
764*53ee8cc1Swenshuai.xi                     || (0 == IPAPool_Info[idx].map_info[i].length))
765*53ee8cc1Swenshuai.xi             {
766*53ee8cc1Swenshuai.xi                 //VPRINTF("MApi_IPA_Pool_MapUserVA inside if\n");
767*53ee8cc1Swenshuai.xi                 IPAPool_Info[idx].map_info[i].virt_addr  = map_args.virt_addr;
768*53ee8cc1Swenshuai.xi                 IPAPool_Info[idx].map_info[i].length = map_args.length;
769*53ee8cc1Swenshuai.xi                 //VPRINTF("MApi_IPA_Pool_MapUserVA before _miu_offset_to_phy\n");
770*53ee8cc1Swenshuai.xi                 _miu_offset_to_phy(IPAPool_Info[idx].Init_Param.miu, IPAPool_Info[idx].Init_Param.offset_in_heap + map_param->offset_in_pool, IPAPool_Info[idx].map_info[i].Physaddr); // get miu base addr
771*53ee8cc1Swenshuai.xi                 if(IPA_VA_CACHE_NONE_CACHE_Param == map_param->cache_type)
772*53ee8cc1Swenshuai.xi                     IPAPool_Info[idx].map_info[i].bNonCache = TRUE;
773*53ee8cc1Swenshuai.xi                 else
774*53ee8cc1Swenshuai.xi                     IPAPool_Info[idx].map_info[i].bNonCache = FALSE;
775*53ee8cc1Swenshuai.xi                 //VPRINTF("MApi_IPA_Pool_MapUserVA before MsOS_MPool_Add_PA2VARange\n");
776*53ee8cc1Swenshuai.xi                 MsOS_MPool_Add_PA2VARange(IPAPool_Info[idx].map_info[i].Physaddr, IPAPool_Info[idx].map_info[i].virt_addr, IPAPool_Info[idx].map_info[i].length, IPAPool_Info[idx].map_info[i].bNonCache);
777*53ee8cc1Swenshuai.xi                 break;
778*53ee8cc1Swenshuai.xi             }
779*53ee8cc1Swenshuai.xi         }
780*53ee8cc1Swenshuai.xi         //VPRINTF("MApi_IPA_Pool_MapUserVA after for\n");
781*53ee8cc1Swenshuai.xi         if(MAX_CLIENT_MAP_NUM == i)
782*53ee8cc1Swenshuai.xi         {
783*53ee8cc1Swenshuai.xi             VPRINTF("print important log for 3 times :a same client mapped %lu times,unable  map once more!!!!\n", MAX_CLIENT_MAP_NUM);
784*53ee8cc1Swenshuai.xi             VPRINTF("print important log for 3 times :a same client mapped %lu times,unable  map once more!!!!\n", MAX_CLIENT_MAP_NUM);
785*53ee8cc1Swenshuai.xi             VPRINTF("print important log for 3 times :a same client mapped %lu times,unable  map once more!!!!\n", MAX_CLIENT_MAP_NUM);
786*53ee8cc1Swenshuai.xi             //here later can add code for dump map info
787*53ee8cc1Swenshuai.xi             ret = FALSE;
788*53ee8cc1Swenshuai.xi             goto IPA_POOL_MapUserVA_DONE;
789*53ee8cc1Swenshuai.xi         }
790*53ee8cc1Swenshuai.xi     }
791*53ee8cc1Swenshuai.xi     //VPRINTF("MApi_IPA_Pool_MapUserVA before IPA_POOL_MapUserVA_DONE\n");
792*53ee8cc1Swenshuai.xi 
793*53ee8cc1Swenshuai.xi IPA_POOL_MapUserVA_DONE:
794*53ee8cc1Swenshuai.xi     //VPRINTF("MApi_IPA_Pool_MapUserVA after IPA_POOL_MapUserVA_DONE\n");
795*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
796*53ee8cc1Swenshuai.xi     return ret;
797*53ee8cc1Swenshuai.xi }
798*53ee8cc1Swenshuai.xi 
MApi_IPA_Pool_MapVA(struct IPA_Pool_Map_Param * map_param)799*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) MApi_IPA_Pool_MapVA(struct IPA_Pool_Map_Param * map_param)
800*53ee8cc1Swenshuai.xi {
801*53ee8cc1Swenshuai.xi     return MApi_IPA_Pool_MapUserVA(map_param);
802*53ee8cc1Swenshuai.xi }
803*53ee8cc1Swenshuai.xi 
MApi_IPA_Pool_UnmapUserVA(struct IPA_Pool_Unmap_Param * unmap_param)804*53ee8cc1Swenshuai.xi static void MApi_IPA_Pool_UnmapUserVA(struct IPA_Pool_Unmap_Param * unmap_param)
805*53ee8cc1Swenshuai.xi {
806*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
807*53ee8cc1Swenshuai.xi     int res = 0;
808*53ee8cc1Swenshuai.xi     int i=0,idx=0,find_virt_info_i=-1,find_virt_info_idx=-1;
809*53ee8cc1Swenshuai.xi     MS_BOOL find_virt_info = FALSE;
810*53ee8cc1Swenshuai.xi     struct IPA_Pool_Unmap_Args unmap_args;
811*53ee8cc1Swenshuai.xi     VPRINTF("%s   virt_addr 0x%lx, len 0x%lx \n",__FUNCTION__,
812*53ee8cc1Swenshuai.xi 		(unsigned long)unmap_param->virt_addr, (unsigned long)unmap_param->length);
813*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
814*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool < 0)
815*53ee8cc1Swenshuai.xi     {
816*53ee8cc1Swenshuai.xi         ret = FALSE;
817*53ee8cc1Swenshuai.xi         goto IPA_POOL_UnmapUserVA_DONE;
818*53ee8cc1Swenshuai.xi     }
819*53ee8cc1Swenshuai.xi     for(idx=0;idx < MAX_IPAPOOLSIZE;idx++)
820*53ee8cc1Swenshuai.xi     {
821*53ee8cc1Swenshuai.xi         if(TRUE == IPAPool_Info[idx].bIsUsed)
822*53ee8cc1Swenshuai.xi         {
823*53ee8cc1Swenshuai.xi             for(i=0;i<MAX_CLIENT_MAP_NUM;i++)
824*53ee8cc1Swenshuai.xi             {
825*53ee8cc1Swenshuai.xi                 if((unmap_param->virt_addr == IPAPool_Info[idx].map_info[i].virt_addr)
826*53ee8cc1Swenshuai.xi 			  	 && (unmap_param->length == IPAPool_Info[idx].map_info[i].length))
827*53ee8cc1Swenshuai.xi                 {
828*53ee8cc1Swenshuai.xi                     find_virt_info = TRUE;
829*53ee8cc1Swenshuai.xi                     find_virt_info_idx = idx;
830*53ee8cc1Swenshuai.xi                     find_virt_info_i = i;
831*53ee8cc1Swenshuai.xi                 }
832*53ee8cc1Swenshuai.xi             }
833*53ee8cc1Swenshuai.xi         }
834*53ee8cc1Swenshuai.xi     }
835*53ee8cc1Swenshuai.xi     if(FALSE == find_virt_info)
836*53ee8cc1Swenshuai.xi     {
837*53ee8cc1Swenshuai.xi         VPRINTF("print important log for 3 times :map and unmap area should be strict equal !!!!  virt_addr=0x%lx,length=0x%lx\n", (unsigned long)unmap_param->virt_addr,(unsigned long)unmap_param->length);
838*53ee8cc1Swenshuai.xi         VPRINTF("print important log for 3 times :map and unmap area should be strict equal !!!!  virt_addr=0x%lx,length=0x%lx\n", (unsigned long)unmap_param->virt_addr,(unsigned long)unmap_param->length);
839*53ee8cc1Swenshuai.xi         VPRINTF("print important log for 3 times :map and unmap area should be strict equal !!!!  virt_addr=0x%lx,length=0x%lx\n", (unsigned long)unmap_param->virt_addr,(unsigned long)unmap_param->length);
840*53ee8cc1Swenshuai.xi         //later will add some debug code
841*53ee8cc1Swenshuai.xi         ret = FALSE;
842*53ee8cc1Swenshuai.xi         goto IPA_POOL_UnmapUserVA_DONE;
843*53ee8cc1Swenshuai.xi     }
844*53ee8cc1Swenshuai.xi     unmap_args.virt_addr = unmap_param->virt_addr;
845*53ee8cc1Swenshuai.xi     unmap_args.length = unmap_param->length;
846*53ee8cc1Swenshuai.xi     MsOS_MPool_Remove_PA2VARange(IPAPool_Info[find_virt_info_idx].map_info[find_virt_info_i].Physaddr, IPAPool_Info[find_virt_info_idx].map_info[find_virt_info_i].virt_addr, IPAPool_Info[find_virt_info_idx].map_info[find_virt_info_i].length, IPAPool_Info[find_virt_info_idx].map_info[find_virt_info_i].bNonCache);
847*53ee8cc1Swenshuai.xi     res = ioctl(_s32FdIPAPool,IPA_POOL_IOC_UNMAP,&unmap_args);
848*53ee8cc1Swenshuai.xi     if (res < 0)
849*53ee8cc1Swenshuai.xi     {
850*53ee8cc1Swenshuai.xi         VPRINTF("%s  fail:  virt_addr 0x%lx, len 0x%lx \n",__FUNCTION__,
851*53ee8cc1Swenshuai.xi 			(unsigned long)unmap_param->virt_addr, (unsigned long)unmap_param->length);
852*53ee8cc1Swenshuai.xi         ret = FALSE;
853*53ee8cc1Swenshuai.xi         goto IPA_POOL_UnmapUserVA_DONE;
854*53ee8cc1Swenshuai.xi     }
855*53ee8cc1Swenshuai.xi     IPAPool_Info[find_virt_info_idx].map_info[find_virt_info_i].virt_addr = 0;
856*53ee8cc1Swenshuai.xi     IPAPool_Info[find_virt_info_idx].map_info[find_virt_info_i].length = 0;
857*53ee8cc1Swenshuai.xi 
858*53ee8cc1Swenshuai.xi IPA_POOL_UnmapUserVA_DONE:
859*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
860*53ee8cc1Swenshuai.xi 
861*53ee8cc1Swenshuai.xi     return;
862*53ee8cc1Swenshuai.xi }
863*53ee8cc1Swenshuai.xi 
MApi_IPA_Pool_UnmapVA(struct IPA_Pool_Unmap_Param * unmap_param)864*53ee8cc1Swenshuai.xi void  __attribute__((weak)) MApi_IPA_Pool_UnmapVA(struct IPA_Pool_Unmap_Param * unmap_param)
865*53ee8cc1Swenshuai.xi {
866*53ee8cc1Swenshuai.xi     return MApi_IPA_Pool_UnmapUserVA(unmap_param);
867*53ee8cc1Swenshuai.xi }
868*53ee8cc1Swenshuai.xi 
MApi_IPA_Pool_DCacheFlush(struct IPA_Pool_DCacheFlush_Param * dcache_flush_param)869*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) MApi_IPA_Pool_DCacheFlush(struct IPA_Pool_DCacheFlush_Param* dcache_flush_param)
870*53ee8cc1Swenshuai.xi {
871*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
872*53ee8cc1Swenshuai.xi     int res = 0;
873*53ee8cc1Swenshuai.xi     struct IPA_Pool_DCacheFlush_Args dcache_flush_args;
874*53ee8cc1Swenshuai.xi     VPRINTF("%s  virt_addr 0x%lx, length 0x%lx flush_type 0x%x\n",__FUNCTION__,
875*53ee8cc1Swenshuai.xi 		(unsigned long)dcache_flush_param->virt_addr, (unsigned long)dcache_flush_param->length, dcache_flush_param->flush_type);
876*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
877*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool < 0)
878*53ee8cc1Swenshuai.xi     {
879*53ee8cc1Swenshuai.xi         ret = FALSE;
880*53ee8cc1Swenshuai.xi         goto IPA_POOL_DCacheFlush_DONE;
881*53ee8cc1Swenshuai.xi     }
882*53ee8cc1Swenshuai.xi 
883*53ee8cc1Swenshuai.xi     dcache_flush_args.virt_addr = dcache_flush_param->virt_addr;
884*53ee8cc1Swenshuai.xi     dcache_flush_args.length = dcache_flush_param->length;
885*53ee8cc1Swenshuai.xi     dcache_flush_args.flush_type = dcache_flush_param->flush_type;
886*53ee8cc1Swenshuai.xi     res = ioctl(_s32FdIPAPool,IPA_POOL_IOC_FLUSH,&dcache_flush_args);
887*53ee8cc1Swenshuai.xi     if (res < 0)
888*53ee8cc1Swenshuai.xi     {
889*53ee8cc1Swenshuai.xi         VPRINTF("%s  fail:  virt_addr 0x%lx, length 0x%lx flush_type 0x%x\n",__FUNCTION__,
890*53ee8cc1Swenshuai.xi 		(unsigned long)dcache_flush_param->virt_addr, (unsigned long)dcache_flush_param->length, dcache_flush_param->flush_type);
891*53ee8cc1Swenshuai.xi         ret = FALSE;
892*53ee8cc1Swenshuai.xi         goto IPA_POOL_DCacheFlush_DONE;
893*53ee8cc1Swenshuai.xi     }
894*53ee8cc1Swenshuai.xi 
895*53ee8cc1Swenshuai.xi IPA_POOL_DCacheFlush_DONE:
896*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
897*53ee8cc1Swenshuai.xi     return ret;
898*53ee8cc1Swenshuai.xi }
899*53ee8cc1Swenshuai.xi 
MApi_IPA_Pool_HEAP_ATTR(struct IPA_Pool_Heap_Attr_Param * heap_attr_param)900*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) MApi_IPA_Pool_HEAP_ATTR(struct IPA_Pool_Heap_Attr_Param* heap_attr_param)
901*53ee8cc1Swenshuai.xi {
902*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
903*53ee8cc1Swenshuai.xi     int res = 0;
904*53ee8cc1Swenshuai.xi     struct IPA_Pool_Heap_Attr heap_attr_args;
905*53ee8cc1Swenshuai.xi     VPRINTF("%s  heap_id 0x%x\n",__FUNCTION__, heap_attr_param->heap_id);
906*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
907*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool < 0)
908*53ee8cc1Swenshuai.xi     {
909*53ee8cc1Swenshuai.xi         ret = FALSE;
910*53ee8cc1Swenshuai.xi         goto IPA_POOL_HEAP_ATTR_DONE;
911*53ee8cc1Swenshuai.xi     }
912*53ee8cc1Swenshuai.xi 
913*53ee8cc1Swenshuai.xi     heap_attr_args.heap_id = heap_attr_param->heap_id;
914*53ee8cc1Swenshuai.xi     res = ioctl(_s32FdIPAPool,IPA_POOL_IOC_HEAP_ATTR,&heap_attr_args);
915*53ee8cc1Swenshuai.xi     if (res < 0 || heap_attr_args.error_code != IPAERROR_OK)
916*53ee8cc1Swenshuai.xi     {
917*53ee8cc1Swenshuai.xi         VPRINTF("%s  fail:  heap_id 0x%x\n",__FUNCTION__,heap_attr_param->heap_id);
918*53ee8cc1Swenshuai.xi         ret = FALSE;
919*53ee8cc1Swenshuai.xi         goto IPA_POOL_HEAP_ATTR_DONE;
920*53ee8cc1Swenshuai.xi     }
921*53ee8cc1Swenshuai.xi     memcpy(heap_attr_param->name,heap_attr_args.name,IPAPOOL_HEAP_NAME_MAX_LEN);
922*53ee8cc1Swenshuai.xi     heap_attr_param->heap_miu_start_offset = heap_attr_args.heap_miu_start_offset;
923*53ee8cc1Swenshuai.xi     heap_attr_param->heap_length = heap_attr_args.heap_length;
924*53ee8cc1Swenshuai.xi     heap_attr_param->miu = heap_attr_args.miu;
925*53ee8cc1Swenshuai.xi     heap_attr_param->heap_type = heap_attr_args.heap_type;
926*53ee8cc1Swenshuai.xi     heap_attr_param->error_code = heap_attr_args.error_code;
927*53ee8cc1Swenshuai.xi 
928*53ee8cc1Swenshuai.xi IPA_POOL_HEAP_ATTR_DONE:
929*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
930*53ee8cc1Swenshuai.xi     return ret;
931*53ee8cc1Swenshuai.xi }
932*53ee8cc1Swenshuai.xi 
MApi_IPA_Pool_GETIPCHANDLE(struct IPA_Pool_GetIpcHandle_Param * getipchandle_param)933*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) MApi_IPA_Pool_GETIPCHANDLE(struct IPA_Pool_GetIpcHandle_Param* getipchandle_param)
934*53ee8cc1Swenshuai.xi {
935*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
936*53ee8cc1Swenshuai.xi     int res = 0;
937*53ee8cc1Swenshuai.xi     struct IPA_Pool_GetIpcHandle_Args getipchandle_args;
938*53ee8cc1Swenshuai.xi     VPRINTF("%s  pool_handle_id 0x%x\n",__FUNCTION__, getipchandle_param->pool_handle_id);
939*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
940*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool < 0)
941*53ee8cc1Swenshuai.xi     {
942*53ee8cc1Swenshuai.xi         VPRINTF("MApi_IPA_Pool_GETIPCHANDLE  fail  pool_handle_id=0x%x,   _s32FdIPAPool=%d \n", getipchandle_param->pool_handle_id,_s32FdIPAPool);
943*53ee8cc1Swenshuai.xi         ret = FALSE;
944*53ee8cc1Swenshuai.xi         goto IPA_POOL_GETIPCHANDLE_DONE;
945*53ee8cc1Swenshuai.xi     }
946*53ee8cc1Swenshuai.xi 
947*53ee8cc1Swenshuai.xi     getipchandle_args.pool_handle_id = getipchandle_param->pool_handle_id;
948*53ee8cc1Swenshuai.xi     res = ioctl(_s32FdIPAPool,IPA_POOL_IOC_GETIPCHANDLE,&getipchandle_args);
949*53ee8cc1Swenshuai.xi     if (res < 0 || getipchandle_args.error_code != IPAERROR_OK)
950*53ee8cc1Swenshuai.xi     {
951*53ee8cc1Swenshuai.xi         VPRINTF("%s  fail:  pool_handle_id 0x%x\n",__FUNCTION__,getipchandle_param->pool_handle_id);
952*53ee8cc1Swenshuai.xi         ret = FALSE;
953*53ee8cc1Swenshuai.xi         goto IPA_POOL_GETIPCHANDLE_DONE;
954*53ee8cc1Swenshuai.xi     }
955*53ee8cc1Swenshuai.xi     getipchandle_param->ipc_handle_id = getipchandle_args.ipc_handle_id;
956*53ee8cc1Swenshuai.xi     getipchandle_param->error_code = getipchandle_args.error_code;
957*53ee8cc1Swenshuai.xi 
958*53ee8cc1Swenshuai.xi IPA_POOL_GETIPCHANDLE_DONE:
959*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
960*53ee8cc1Swenshuai.xi     return ret;
961*53ee8cc1Swenshuai.xi }
962*53ee8cc1Swenshuai.xi 
MApi_IPA_Pool_InstallIpcHandle(struct IPA_Pool_InstallIpcHandle_Param * installipchandle_param)963*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) MApi_IPA_Pool_InstallIpcHandle(struct IPA_Pool_InstallIpcHandle_Param* installipchandle_param)
964*53ee8cc1Swenshuai.xi {
965*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
966*53ee8cc1Swenshuai.xi     int res = 0;
967*53ee8cc1Swenshuai.xi     struct IPA_Pool_InstallIpcHandle_Args installipchandle_args;
968*53ee8cc1Swenshuai.xi     VPRINTF("%s  ipc_handle_id 0x%x\n",__FUNCTION__, installipchandle_param->ipc_handle_id);
969*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
970*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool < 0)
971*53ee8cc1Swenshuai.xi     {
972*53ee8cc1Swenshuai.xi         VPRINTF("%s ipc_handle_id=0x%x,   _s32FdIPAPool=%d \n",__FUNCTION__, installipchandle_param->ipc_handle_id,_s32FdIPAPool);
973*53ee8cc1Swenshuai.xi         //this process may not open ipapool yet,need we open for this process.
974*53ee8cc1Swenshuai.xi         if ((_s32FdIPAPool = open("/dev/ipapool", O_RDWR)) < 0)
975*53ee8cc1Swenshuai.xi         {
976*53ee8cc1Swenshuai.xi             VPRINTF("open /dev/ipapool fail   ipc_handle_id=0x%x,   _s32FdIPAPool=%d\n", installipchandle_param->ipc_handle_id,_s32FdIPAPool);
977*53ee8cc1Swenshuai.xi             ret =  FALSE;
978*53ee8cc1Swenshuai.xi             goto IPA_POOL_InstallIpcHandle_DONE;
979*53ee8cc1Swenshuai.xi         }
980*53ee8cc1Swenshuai.xi 
981*53ee8cc1Swenshuai.xi         memset(IPAPool_Info, 0, sizeof(IPAPOOL_INFO)*MAX_IPAPOOLSIZE);
982*53ee8cc1Swenshuai.xi 
983*53ee8cc1Swenshuai.xi     }
984*53ee8cc1Swenshuai.xi 
985*53ee8cc1Swenshuai.xi     installipchandle_args.ipc_handle_id = installipchandle_param->ipc_handle_id;
986*53ee8cc1Swenshuai.xi     // VPRINTF("MApi_IPA_Pool_InstallIpcHandle  ipc_handle_id=0x%x  debug will ioctl\n", installipchandle_param->ipc_handle_id);
987*53ee8cc1Swenshuai.xi     res = ioctl(_s32FdIPAPool,IPA_POOL_IOC_INSTALLIPCHANDLE,&installipchandle_args);
988*53ee8cc1Swenshuai.xi     // VPRINTF("MApi_IPA_Pool_InstallIpcHandle  ipc_handle_id=0x%x  debug after ioctl  res=%d\n", installipchandle_param->ipc_handle_id,res);
989*53ee8cc1Swenshuai.xi 
990*53ee8cc1Swenshuai.xi     if (res < 0 || installipchandle_args.error_code != IPAERROR_OK)
991*53ee8cc1Swenshuai.xi     {
992*53ee8cc1Swenshuai.xi         VPRINTF("%s  fail:  heap_id 0x%x\n",__FUNCTION__,installipchandle_param->ipc_handle_id);
993*53ee8cc1Swenshuai.xi         ret = FALSE;
994*53ee8cc1Swenshuai.xi         goto IPA_POOL_InstallIpcHandle_DONE;
995*53ee8cc1Swenshuai.xi     }
996*53ee8cc1Swenshuai.xi     installipchandle_param->pool_handle_id = installipchandle_args.pool_handle_id;
997*53ee8cc1Swenshuai.xi     installipchandle_param->error_code = installipchandle_args.error_code;
998*53ee8cc1Swenshuai.xi 
999*53ee8cc1Swenshuai.xi IPA_POOL_InstallIpcHandle_DONE:
1000*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
1001*53ee8cc1Swenshuai.xi     return ret;
1002*53ee8cc1Swenshuai.xi }
1003*53ee8cc1Swenshuai.xi 
1004*53ee8cc1Swenshuai.xi //unit of timeout is millisecond.
MApi_IPA_Pool_GetMem_Timeout(struct IPA_Pool_GetMem_Param * get_param,MS_U32 timeout_ms)1005*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) MApi_IPA_Pool_GetMem_Timeout(struct IPA_Pool_GetMem_Param* get_param,MS_U32 timeout_ms)
1006*53ee8cc1Swenshuai.xi {
1007*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
1008*53ee8cc1Swenshuai.xi     struct IPA_Pool_Alloc_Args ipa_alloc_args;
1009*53ee8cc1Swenshuai.xi     int res = 0;
1010*53ee8cc1Swenshuai.xi 
1011*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
1012*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool < 0)
1013*53ee8cc1Swenshuai.xi     {
1014*53ee8cc1Swenshuai.xi         ret = FALSE;
1015*53ee8cc1Swenshuai.xi         goto IPA_POOL_GETMEM_DONE;
1016*53ee8cc1Swenshuai.xi     }
1017*53ee8cc1Swenshuai.xi 
1018*53ee8cc1Swenshuai.xi     ipa_alloc_args.pool_handle_id = get_param->pool_handle_id;
1019*53ee8cc1Swenshuai.xi     ipa_alloc_args.offset_in_pool = get_param->offset_in_pool;
1020*53ee8cc1Swenshuai.xi     ipa_alloc_args.length = get_param->length;
1021*53ee8cc1Swenshuai.xi 
1022*53ee8cc1Swenshuai.xi     ipa_alloc_args.timeout = timeout_ms;
1023*53ee8cc1Swenshuai.xi 
1024*53ee8cc1Swenshuai.xi     VPRINTF("%s %d pool_handle_id 0x%x [offset_in_pool,length]= [0x%lx,0x%lx] timeout 0x%x\n", __FUNCTION__,__LINE__,
1025*53ee8cc1Swenshuai.xi 		ipa_alloc_args.pool_handle_id, (unsigned long)ipa_alloc_args.offset_in_pool,(unsigned long)ipa_alloc_args.length,timeout_ms);
1026*53ee8cc1Swenshuai.xi 
1027*53ee8cc1Swenshuai.xi     res = ioctl(_s32FdIPAPool, IPA_POOL_IOC_ALLOC, &ipa_alloc_args);
1028*53ee8cc1Swenshuai.xi     if (res < 0 || ipa_alloc_args.error_code != IPAERROR_OK)
1029*53ee8cc1Swenshuai.xi     {
1030*53ee8cc1Swenshuai.xi         MS_U32 each_delay_ms = 50;//empirical value in our test.
1031*53ee8cc1Swenshuai.xi         MS_U32 now_delay_ms=0;
1032*53ee8cc1Swenshuai.xi         while(1)
1033*53ee8cc1Swenshuai.xi         {
1034*53ee8cc1Swenshuai.xi             // delay and try again
1035*53ee8cc1Swenshuai.xi             pthread_mutex_unlock(&_IPA_POOL_Mutex);
1036*53ee8cc1Swenshuai.xi 
1037*53ee8cc1Swenshuai.xi             VPRINTF("%s:%d each_delay_ms=%u, delay and try alloc again now_delay_ms=%u\n",__FUNCTION__,__LINE__,each_delay_ms,now_delay_ms);
1038*53ee8cc1Swenshuai.xi 
1039*53ee8cc1Swenshuai.xi             ipa_alloc_args.timeout = 0;//into kernel will not consider timeout again
1040*53ee8cc1Swenshuai.xi             MsOS_DelayTask(each_delay_ms);
1041*53ee8cc1Swenshuai.xi             pthread_mutex_lock(&_IPA_POOL_Mutex);
1042*53ee8cc1Swenshuai.xi             res = ioctl(_s32FdIPAPool, IPA_POOL_IOC_ALLOC, &ipa_alloc_args);
1043*53ee8cc1Swenshuai.xi             if ((res == 0) && (ipa_alloc_args.error_code == IPAERROR_OK))
1044*53ee8cc1Swenshuai.xi                 break;
1045*53ee8cc1Swenshuai.xi             now_delay_ms += each_delay_ms;
1046*53ee8cc1Swenshuai.xi             if(now_delay_ms >= timeout_ms)
1047*53ee8cc1Swenshuai.xi                 break;
1048*53ee8cc1Swenshuai.xi         }
1049*53ee8cc1Swenshuai.xi         if (res < 0 || ipa_alloc_args.error_code != IPAERROR_OK)
1050*53ee8cc1Swenshuai.xi         {
1051*53ee8cc1Swenshuai.xi             VPRINTF("ipa pool get memory fail: pool_handle_id %u, offset 0x%lx, len 0x%lx  ipa_alloc_args.error_code=0x%x ipa_alloc_args.timeout=%u res=%d\n", get_param->pool_handle_id,
1052*53ee8cc1Swenshuai.xi 			(unsigned long)get_param->offset_in_pool, (unsigned long)get_param->length,ipa_alloc_args.error_code,ipa_alloc_args.timeout,res);
1053*53ee8cc1Swenshuai.xi             ret = FALSE;
1054*53ee8cc1Swenshuai.xi             goto IPA_POOL_GETMEM_DONE;
1055*53ee8cc1Swenshuai.xi         }
1056*53ee8cc1Swenshuai.xi     }
1057*53ee8cc1Swenshuai.xi 
1058*53ee8cc1Swenshuai.xi     get_param->error_code = ipa_alloc_args.error_code;
1059*53ee8cc1Swenshuai.xi 
1060*53ee8cc1Swenshuai.xi IPA_POOL_GETMEM_DONE:
1061*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
1062*53ee8cc1Swenshuai.xi 
1063*53ee8cc1Swenshuai.xi     return ret;
1064*53ee8cc1Swenshuai.xi 
1065*53ee8cc1Swenshuai.xi }
1066*53ee8cc1Swenshuai.xi 
IPA_Pool_Polling_Task(void * argc)1067*53ee8cc1Swenshuai.xi void *__attribute__((weak)) IPA_Pool_Polling_Task(void *argc)
1068*53ee8cc1Swenshuai.xi {
1069*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
1070*53ee8cc1Swenshuai.xi     MS_U32 idx = 0;
1071*53ee8cc1Swenshuai.xi     struct IPA_Pool_Event_Args pool_event_args;
1072*53ee8cc1Swenshuai.xi     MS_U32 pool_handle_id  = (MS_U32)(intptr_t)(argc);
1073*53ee8cc1Swenshuai.xi     VPRINTF("%s %d pool_handle_id=%u,argc=%p\n",__FUNCTION__,__LINE__,pool_handle_id,argc);
1074*53ee8cc1Swenshuai.xi 
1075*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
1076*53ee8cc1Swenshuai.xi     if (_s32FdIPAPool < 0)
1077*53ee8cc1Swenshuai.xi     {
1078*53ee8cc1Swenshuai.xi         VPRINTF("%s  fail  \n",__FUNCTION__);
1079*53ee8cc1Swenshuai.xi         ret = FALSE;
1080*53ee8cc1Swenshuai.xi         pthread_mutex_unlock(&_IPA_POOL_Mutex);
1081*53ee8cc1Swenshuai.xi         return NULL;
1082*53ee8cc1Swenshuai.xi     }
1083*53ee8cc1Swenshuai.xi     pool_event_args.pool_handle_id = pool_handle_id;
1084*53ee8cc1Swenshuai.xi     VPRINTF("%s %d pool_event_args.pool_handle_id=%u\n",__FUNCTION__,__LINE__,pool_event_args.pool_handle_id);
1085*53ee8cc1Swenshuai.xi 
1086*53ee8cc1Swenshuai.xi     ret = _findPoolHandleId_InIPA_Pool_Table(pool_event_args.pool_handle_id, &idx);
1087*53ee8cc1Swenshuai.xi 
1088*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
1089*53ee8cc1Swenshuai.xi 
1090*53ee8cc1Swenshuai.xi     if(ret == FALSE)
1091*53ee8cc1Swenshuai.xi     {
1092*53ee8cc1Swenshuai.xi         VPRINTF("%s  fail not find in pool table\n",__FUNCTION__);
1093*53ee8cc1Swenshuai.xi         return NULL;
1094*53ee8cc1Swenshuai.xi     }
1095*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].polling_thread_delete_task_flag = 0;
1096*53ee8cc1Swenshuai.xi     VPRINTF("%s %d set for idx=%u\n",__FUNCTION__,__LINE__,idx);
1097*53ee8cc1Swenshuai.xi 
1098*53ee8cc1Swenshuai.xi     while(1)
1099*53ee8cc1Swenshuai.xi     {
1100*53ee8cc1Swenshuai.xi         struct pollfd fds;
1101*53ee8cc1Swenshuai.xi         VPRINTF("%s %d\n",__FUNCTION__,__LINE__);
1102*53ee8cc1Swenshuai.xi         if(IPAPool_Info[idx].polling_thread_delete_task_flag)
1103*53ee8cc1Swenshuai.xi         {
1104*53ee8cc1Swenshuai.xi             VPRINTF("%s %d\n",__FUNCTION__,__LINE__);
1105*53ee8cc1Swenshuai.xi             break; //exit this thread
1106*53ee8cc1Swenshuai.xi         }
1107*53ee8cc1Swenshuai.xi         fds.fd = pool_handle_id;
1108*53ee8cc1Swenshuai.xi         fds.events = POLLIN;
1109*53ee8cc1Swenshuai.xi         VPRINTF("%s %d\n",__FUNCTION__,__LINE__);
1110*53ee8cc1Swenshuai.xi         if ((ret=poll(&fds, 1, -1)) < 0)
1111*53ee8cc1Swenshuai.xi         {
1112*53ee8cc1Swenshuai.xi             VPRINTF("%s %d fail\n",__FUNCTION__,__LINE__);
1113*53ee8cc1Swenshuai.xi             break;
1114*53ee8cc1Swenshuai.xi         }
1115*53ee8cc1Swenshuai.xi 
1116*53ee8cc1Swenshuai.xi         if(ret==0)
1117*53ee8cc1Swenshuai.xi         {
1118*53ee8cc1Swenshuai.xi             VPRINTF("%s %d\n",__FUNCTION__,__LINE__);
1119*53ee8cc1Swenshuai.xi             continue;
1120*53ee8cc1Swenshuai.xi         }
1121*53ee8cc1Swenshuai.xi 
1122*53ee8cc1Swenshuai.xi         if (fds.revents&(POLLHUP | POLLERR |POLLNVAL))
1123*53ee8cc1Swenshuai.xi         {
1124*53ee8cc1Swenshuai.xi             VPRINTF("%s %d\n",__FUNCTION__,__LINE__);
1125*53ee8cc1Swenshuai.xi             break;
1126*53ee8cc1Swenshuai.xi         }
1127*53ee8cc1Swenshuai.xi         if (fds.revents&POLLIN)
1128*53ee8cc1Swenshuai.xi         {
1129*53ee8cc1Swenshuai.xi             VPRINTF("%s %d\n",__FUNCTION__,__LINE__);
1130*53ee8cc1Swenshuai.xi             memset(&pool_event_args,0,sizeof(struct IPA_Pool_Event_Args));
1131*53ee8cc1Swenshuai.xi 			pool_event_args.pool_handle_id = pool_handle_id;
1132*53ee8cc1Swenshuai.xi             ret=read(fds.fd, &pool_event_args, sizeof(struct IPA_Pool_Event_Args));
1133*53ee8cc1Swenshuai.xi 			VPRINTF("%s %d\n",__FUNCTION__,__LINE__);
1134*53ee8cc1Swenshuai.xi             if (sizeof(struct IPA_Pool_Event_Args)!=ret)
1135*53ee8cc1Swenshuai.xi             {
1136*53ee8cc1Swenshuai.xi                 VPRINTF("%s %d\n",__FUNCTION__,__LINE__);
1137*53ee8cc1Swenshuai.xi                 continue;
1138*53ee8cc1Swenshuai.xi             }
1139*53ee8cc1Swenshuai.xi             if(IPA_EVENT_CONFLICT == pool_event_args.event)
1140*53ee8cc1Swenshuai.xi             {
1141*53ee8cc1Swenshuai.xi                 //invoke callback corresponding to that pool event
1142*53ee8cc1Swenshuai.xi                 VPRINTF("%s %d  pool_handle_id=%u  start=0x%lx,length=0x%lx\n",__FUNCTION__,__LINE__,pool_event_args.pool_handle_id,(unsigned long)pool_event_args.start,(unsigned long)pool_event_args.length);
1143*53ee8cc1Swenshuai.xi                 //after read,may change pool_handle_id,so set pool_handle_id again.
1144*53ee8cc1Swenshuai.xi 				pool_event_args.pool_handle_id = pool_handle_id;
1145*53ee8cc1Swenshuai.xi                 VPRINTF("%s %d  pool_handle_id=%u  start=0x%lx,length=0x%lx\n",__FUNCTION__,__LINE__,pool_event_args.pool_handle_id,(unsigned long)pool_event_args.start,(unsigned long)pool_event_args.length);
1146*53ee8cc1Swenshuai.xi                 IPAPool_Info[idx].polling_callback(pool_event_args.pool_handle_id,
1147*53ee8cc1Swenshuai.xi                                                                   pool_event_args.start,pool_event_args.length);
1148*53ee8cc1Swenshuai.xi                 VPRINTF("%s %d\n",__FUNCTION__,__LINE__);
1149*53ee8cc1Swenshuai.xi             }
1150*53ee8cc1Swenshuai.xi         }
1151*53ee8cc1Swenshuai.xi     }
1152*53ee8cc1Swenshuai.xi 
1153*53ee8cc1Swenshuai.xi     //before return ,set this flag.And this flag will be checked in MApi_IPA_Pool_Release.
1154*53ee8cc1Swenshuai.xi     IPAPool_Info[idx].polling_thread_delete_task_flag = 1;
1155*53ee8cc1Swenshuai.xi     VPRINTF("%s %d idx=%u\n",__FUNCTION__,__LINE__,idx);
1156*53ee8cc1Swenshuai.xi     return NULL;
1157*53ee8cc1Swenshuai.xi 
1158*53ee8cc1Swenshuai.xi }
1159*53ee8cc1Swenshuai.xi 
1160*53ee8cc1Swenshuai.xi //suggest this API be invoked after MApi_IPA_Pool_Init.
MApi_IPA_Pool_Notify(struct IPA_Pool_Polling_Param * polling_param)1161*53ee8cc1Swenshuai.xi MS_BOOL __attribute__((weak)) MApi_IPA_Pool_Notify(struct IPA_Pool_Polling_Param *polling_param)
1162*53ee8cc1Swenshuai.xi {
1163*53ee8cc1Swenshuai.xi     MS_BOOL ret = TRUE;
1164*53ee8cc1Swenshuai.xi     pthread_t pthIPAPollingId = -1;
1165*53ee8cc1Swenshuai.xi     MS_U32 idx = 0;
1166*53ee8cc1Swenshuai.xi     int pthread_res = -1;
1167*53ee8cc1Swenshuai.xi 
1168*53ee8cc1Swenshuai.xi     VPRINTF("%s\n",__FUNCTION__);
1169*53ee8cc1Swenshuai.xi     pthread_mutex_lock(&_IPA_POOL_Mutex);
1170*53ee8cc1Swenshuai.xi     if(!polling_param->polling_callback)
1171*53ee8cc1Swenshuai.xi     {
1172*53ee8cc1Swenshuai.xi         VPRINTF("%u have no polling_callback function , %s return directly\n",polling_param->pool_handle_id,__FUNCTION__);
1173*53ee8cc1Swenshuai.xi         pthread_mutex_unlock(&_IPA_POOL_Mutex);
1174*53ee8cc1Swenshuai.xi         return TRUE;
1175*53ee8cc1Swenshuai.xi     }
1176*53ee8cc1Swenshuai.xi 
1177*53ee8cc1Swenshuai.xi     ret = _findPoolHandleId_InIPA_Pool_Table(polling_param->pool_handle_id, &idx);
1178*53ee8cc1Swenshuai.xi 
1179*53ee8cc1Swenshuai.xi     if(ret == FALSE)//not find idx
1180*53ee8cc1Swenshuai.xi     {
1181*53ee8cc1Swenshuai.xi          VPRINTF(" %s ipa pool fail not find pool_handle_id %u in pool table\n",__FUNCTION__,polling_param->pool_handle_id);
1182*53ee8cc1Swenshuai.xi          pthread_mutex_unlock(&_IPA_POOL_Mutex);
1183*53ee8cc1Swenshuai.xi          return FALSE;
1184*53ee8cc1Swenshuai.xi     }
1185*53ee8cc1Swenshuai.xi     VPRINTF("%s  polling_param->pool_handle_id=0x%x\n",__FUNCTION__,polling_param->pool_handle_id);
1186*53ee8cc1Swenshuai.xi 
1187*53ee8cc1Swenshuai.xi     if(-1 == IPAPool_Info[idx].pthIPAPollingId) //no polling id refer to pool handle id
1188*53ee8cc1Swenshuai.xi     {
1189*53ee8cc1Swenshuai.xi 
1190*53ee8cc1Swenshuai.xi         //create polling thread
1191*53ee8cc1Swenshuai.xi         pthread_res = pthread_create(&pthIPAPollingId,
1192*53ee8cc1Swenshuai.xi                                                 NULL,
1193*53ee8cc1Swenshuai.xi                                                 (void *)IPA_Pool_Polling_Task,
1194*53ee8cc1Swenshuai.xi                                                 (void*)(intptr_t)polling_param->pool_handle_id);
1195*53ee8cc1Swenshuai.xi         //N.B. notice that first parameter of pthread_create is output parameter,which
1196*53ee8cc1Swenshuai.xi         //equal with pthread_self() in new pthread,and not equal with getpid().
1197*53ee8cc1Swenshuai.xi         //to judge whether pthread_create success,only need check  pthread_create return value.
1198*53ee8cc1Swenshuai.xi         if(pthread_res != 0)
1199*53ee8cc1Swenshuai.xi         {
1200*53ee8cc1Swenshuai.xi             printf("%s   can't create thread  pthread_res=%d 1111\n",__FUNCTION__,pthread_res);
1201*53ee8cc1Swenshuai.xi             ret= FALSE;
1202*53ee8cc1Swenshuai.xi             pthread_mutex_unlock(&_IPA_POOL_Mutex);
1203*53ee8cc1Swenshuai.xi             return ret;
1204*53ee8cc1Swenshuai.xi         }
1205*53ee8cc1Swenshuai.xi 
1206*53ee8cc1Swenshuai.xi         //corresponding pthIPAPollingId to pool_handle_id
1207*53ee8cc1Swenshuai.xi         IPAPool_Info[idx].pthIPAPollingId = pthIPAPollingId;
1208*53ee8cc1Swenshuai.xi         IPAPool_Info[idx].polling_callback = polling_param->polling_callback;
1209*53ee8cc1Swenshuai.xi     }
1210*53ee8cc1Swenshuai.xi     else//already have polling id refer to pool handle id.
1211*53ee8cc1Swenshuai.xi     {
1212*53ee8cc1Swenshuai.xi         ret=FALSE;
1213*53ee8cc1Swenshuai.xi         VPRINTF("pool handle id %u has its polling id %lu ,error,will not set callback again\n",polling_param->pool_handle_id,IPAPool_Info[idx].pthIPAPollingId);
1214*53ee8cc1Swenshuai.xi     }
1215*53ee8cc1Swenshuai.xi 
1216*53ee8cc1Swenshuai.xi     pthread_mutex_unlock(&_IPA_POOL_Mutex);
1217*53ee8cc1Swenshuai.xi     return ret;
1218*53ee8cc1Swenshuai.xi }
1219*53ee8cc1Swenshuai.xi #endif
1220