]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - packages/ti/sdo/ipc/GateMP.c
GateMP: Fix for hostSupport with GateMPSupportNull
[ipc/ipcdev.git] / packages / ti / sdo / ipc / GateMP.c
1 /*
2  * Copyright (c) 2012-2015 Texas Instruments Incorporated - http://www.ti.com
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  *  ======== GateMP.c ========
34  */
36 #include <xdc/std.h>
37 #include <string.h>
38 #include <xdc/runtime/Error.h>
39 #include <xdc/runtime/Assert.h>
40 #include <xdc/runtime/System.h>
41 #include <xdc/runtime/IGateProvider.h>
42 #include <xdc/runtime/IHeap.h>
43 #include <xdc/runtime/Memory.h>
44 #include <xdc/runtime/Log.h>
46 #include <ti/sdo/ipc/interfaces/IGateMPSupport.h>
48 #include <ti/sysbios/gates/GateMutexPri.h>
49 #include <ti/sysbios/gates/GateSwi.h>
50 #include <ti/sysbios/gates/GateAll.h>
51 #include <ti/sysbios/hal/Cache.h>
52 #include <ti/sysbios/hal/Hwi.h>
54 #include <ti/sdo/ipc/_Ipc.h>
55 #include <ti/sdo/utils/_MultiProc.h>
56 #include <ti/sdo/ipc/_GateMP.h>
57 #include <ti/sdo/utils/_NameServer.h>
58 #include <ti/sdo/ipc/_SharedRegion.h>
60 #include "package/internal/GateMP.xdc.h"
62 /* Helper macros */
63 #define GETREMOTE(mask) ((ti_sdo_ipc_GateMP_RemoteProtect)((mask) >> 8))
64 #define GETLOCAL(mask)  ((ti_sdo_ipc_GateMP_LocalProtect)((mask) & 0xFF))
65 #define SETMASK(remoteProtect, localProtect) \
66                         ((Bits32)((remoteProtect) << 8 | (localProtect)))
68 /* Values used to populate the resource 'inUse' arrays */
69 #define UNUSED          ((UInt8)0)
70 #define USED            ((UInt8)1)
71 #define RESERVED        ((UInt8)-1)
73 #ifdef __ti__
74     #pragma FUNC_EXT_CALLED(GateMP_Params_init);
75     #pragma FUNC_EXT_CALLED(GateMP_create);
76     #pragma FUNC_EXT_CALLED(GateMP_close);
77     #pragma FUNC_EXT_CALLED(GateMP_delete);
78     #pragma FUNC_EXT_CALLED(GateMP_enter);
79     #pragma FUNC_EXT_CALLED(GateMP_getDefaultRemote);
80     #pragma FUNC_EXT_CALLED(GateMP_getLocalProtect);
81     #pragma FUNC_EXT_CALLED(GateMP_getRemoteProtect);
82     #pragma FUNC_EXT_CALLED(GateMP_leave);
83     #pragma FUNC_EXT_CALLED(GateMP_open);
84     #pragma FUNC_EXT_CALLED(GateMP_openByAddr);
85     #pragma FUNC_EXT_CALLED(GateMP_sharedMemReq);
86 #endif
88 /*
89  *  ======== GateMP_getSharedParams ========
90  */
91 static Void GateMP_getSharedParams(GateMP_Params *sparams,
92         const ti_sdo_ipc_GateMP_Params *params)
93 {
94     sparams->name             = params->name;
95     sparams->regionId         = params->regionId;
96     sparams->sharedAddr       = params->sharedAddr;
97     sparams->localProtect     = (GateMP_LocalProtect)
98         params->localProtect;
99     sparams->remoteProtect    = (GateMP_RemoteProtect)
100         params->remoteProtect;
103 /*
104  *  ======== GateMP_getRTSCParams ========
105  */
106 static Void GateMP_getRTSCParams(ti_sdo_ipc_GateMP_Params *params,
107         const GateMP_Params *sparams)
110     ti_sdo_ipc_GateMP_Params_init(params);
112     params->name             = sparams->name;
113     params->regionId         = sparams->regionId;
114     params->sharedAddr       = sparams->sharedAddr;
115     params->localProtect     = (ti_sdo_ipc_GateMP_LocalProtect)
116         sparams->localProtect;
117     params->remoteProtect    = (ti_sdo_ipc_GateMP_RemoteProtect)
118         sparams->remoteProtect;
121 /*
122  *************************************************************************
123  *                       Common Header Functions
124  *************************************************************************
125  */
127 /*
128  *  ======== GateMP_Params_init ========
129  */
130 Void GateMP_Params_init(GateMP_Params *sparams)
132     ti_sdo_ipc_GateMP_Params params;
134     ti_sdo_ipc_GateMP_Params_init(&params);
135     GateMP_getSharedParams(sparams, &params);
138 /*
139  *  ======== GateMP_create ========
140  */
141 GateMP_Handle GateMP_create(const GateMP_Params *sparams)
143     ti_sdo_ipc_GateMP_Params params;
144     ti_sdo_ipc_GateMP_Object *obj;
145     Error_Block eb;
147     GateMP_getRTSCParams(&params, (Ptr)sparams);
149     Error_init(&eb);
151     /* call the module create */
152     obj = ti_sdo_ipc_GateMP_create(&params, &eb);
154     return ((GateMP_Handle)obj);
157 /*
158  *  ======== GateMP_close ========
159  */
160 Int GateMP_close(GateMP_Handle *handlePtr)
162     ti_sdo_ipc_GateMP_Object *obj = (ti_sdo_ipc_GateMP_Object *)*handlePtr;
163     UInt key;
164     Int count;
166     /*
167      *  Cannot call with the numOpens equal to zero.  This is either
168      *  a created handle or been closed already.
169      */
170     Assert_isTrue((obj->numOpens != 0), ti_sdo_ipc_GateMP_A_invalidClose);
172     key = Hwi_disable();
173     count = --obj->numOpens;
174     Hwi_restore(key);
176     /*
177      *  if the count is zero and the gate is opened, then this
178      *  object was created in the open (i.e. the create happened
179      *  on a remote processor.
180      */
181     if ((count == 0) && (obj->objType == ti_sdo_ipc_Ipc_ObjType_OPENDYNAMIC)) {
182         GateMP_delete(handlePtr);
183     }
185     *handlePtr = NULL;
187     return (GateMP_S_SUCCESS);
190 /*
191  *  ======== GateMP_delete ========
192  */
193 Int GateMP_delete(GateMP_Handle *handlePtr)
195     ti_sdo_ipc_GateMP_delete((ti_sdo_ipc_GateMP_Handle *)handlePtr);
197     return (GateMP_S_SUCCESS);
200 /*
201  *  ======== GateMP_enter ========
202  */
203 IArg GateMP_enter(GateMP_Handle handle)
205     IArg key;
206     ti_sdo_ipc_GateMP_Object *obj = (ti_sdo_ipc_GateMP_Object *)handle;
208     Assert_isTrue(obj != NULL, ti_sdo_ipc_Ipc_A_nullArgument);
210     key = IGateProvider_enter(obj->gateHandle);
212     Log_write3(ti_sdo_ipc_GateMP_LM_enter,(UArg)obj->remoteProtect,
213             (UArg)obj->resourceId, key);
215     return (key);
218 /*
219  *  ======== GateMP_leave ========
220  */
221 Void GateMP_leave(GateMP_Handle handle, IArg key)
223     ti_sdo_ipc_GateMP_Object *obj = (ti_sdo_ipc_GateMP_Object *)handle;
225     Assert_isTrue(obj != NULL, ti_sdo_ipc_Ipc_A_nullArgument);
227     IGateProvider_leave(obj->gateHandle, key);
229     Log_write3(ti_sdo_ipc_GateMP_LM_leave, (UArg)obj->remoteProtect,
230         (UArg)obj->resourceId, key);
233 /*
234  *  ======== GateMP_open ========
235  */
236 Int GateMP_open(String name, GateMP_Handle *handlePtr)
238     SharedRegion_SRPtr sharedShmBase;
239     Int status;
240     ti_sdo_ipc_GateMP_Object *obj;
241     ti_sdo_ipc_GateMP_Params params;
242     Error_Block eb;
243     UInt key;
244     UInt32 len;
245     UInt32 mask;
246     UInt32 resourceId;
247     Ptr sharedAddr;
248     UInt32 nsValue[4];
250     Error_init(&eb);
252     /* Assert that a pointer has been supplied */
253     Assert_isTrue(handlePtr != NULL, ti_sdo_ipc_Ipc_A_nullArgument);
255     len = sizeof(nsValue);
257     /*
258      *  Get the Attrs out of the NameServer instance.
259      *  Search all processors.
260      */
261     status = NameServer_get((NameServer_Handle)GateMP_module->nameServer, name,
262             &nsValue, &len, MultiProc_getClusterProcList());
264     if (status < 0) {
265         *handlePtr = NULL;
266         return (GateMP_E_NOTFOUND);
267     }
269     /*
270      * The least significant bit of nsValue[1] == 0 means its a
271      * local GateMP, otherwise its a remote GateMP.
272      */
273     if (((nsValue[1] & 0x1) == 0) &&
274         ((nsValue[1] >> 16) != MultiProc_self())) {
275         /* Trying to open a local GateMP remotely */
276         *handlePtr = NULL;
277         return (GateMP_E_FAIL);
278     }
280     if ((nsValue[1] & 0x1) == 0) {
281         /*
282          * Opening a local GateMP locally. The GateMP is created
283          * from a local heap so don't do SharedRegion Ptr conversion.
284          */
285         sharedAddr = (Ptr)nsValue[0];
286         status = GateMP_openByAddr(sharedAddr, handlePtr);
287     }
288     else if (GateMP_module->hostSupport == FALSE) {
289         /* Opening a remote GateMP. Need to do SharedRegion Ptr conversion. */
290         sharedShmBase = (SharedRegion_SRPtr)nsValue[0];
291         sharedAddr = SharedRegion_getPtr(sharedShmBase);
292         status = GateMP_openByAddr(sharedAddr, handlePtr);
293     }
294     else {
295         /*  need to track number of opens atomically */
296         key = Hwi_disable();
297         resourceId = nsValue[2];
298         mask = nsValue[3];
300         /* Remote case */
301         switch (GETREMOTE(mask)) {
302             case GateMP_RemoteProtect_SYSTEM:
303                 obj = GateMP_module->remoteSystemGates[resourceId];
304                 break;
306             case GateMP_RemoteProtect_CUSTOM1:
307                 obj = GateMP_module->remoteCustom1Gates[resourceId];
308                 break;
310             case GateMP_RemoteProtect_CUSTOM2:
311                 obj = GateMP_module->remoteCustom2Gates[resourceId];
312                 break;
314             default:
315                 obj = NULL;
316                 status = GateMP_E_FAIL;
317                 break;
318         }
320         if (status == GateMP_S_SUCCESS) {
321             /*
322              *  If the object is NULL, then it must have been created on a
323              *  remote processor. Need to create a local object. This is
324              *  accomplished by setting the openFlag to TRUE.
325              */
326             if (obj == NULL) {
327                 /* Create a GateMP object with the openFlag set to true */
328                 ti_sdo_ipc_GateMP_Params_init(&params);
329                 params.openFlag = TRUE;
330                 params.sharedAddr = NULL;
331                 params.resourceId = resourceId;
332                 params.localProtect = GETLOCAL(mask);
333                 params.remoteProtect = GETREMOTE(mask);
335                 obj = ti_sdo_ipc_GateMP_create(&params, &eb);
336                 if (obj == NULL) {
337                     status = GateMP_E_FAIL;
338                 }
339             }
340             else {
341                 obj->numOpens++;
342             }
343         }
345         /* Return the GateMP instance  */
346         *handlePtr = (GateMP_Handle)obj;
348         /* restore hwi mask */
349         Hwi_restore(key);
350     }
352     return (status);
355 /*
356  *  ======== GateMP_openByAddr ========
357  */
358 Int GateMP_openByAddr(Ptr sharedAddr, GateMP_Handle *handlePtr)
360     Int status = GateMP_S_SUCCESS;
361     UInt key;
362     ti_sdo_ipc_GateMP_Object *obj;
363     ti_sdo_ipc_GateMP_Params params;
364     ti_sdo_ipc_GateMP_Attrs *attrs;
365     UInt16 regionId;
366     Error_Block eb;
368     Error_init(&eb);
370     attrs = (ti_sdo_ipc_GateMP_Attrs *)sharedAddr;
372     /* get the region id and invalidate attrs is needed */
373     regionId = SharedRegion_getId(sharedAddr);
374     if (regionId != SharedRegion_INVALIDREGIONID) {
375         if (SharedRegion_isCacheEnabled(regionId)) {
376             Cache_inv(attrs, sizeof(ti_sdo_ipc_GateMP_Attrs), Cache_Type_ALL,
377                     TRUE);
378         }
379     }
381     if (attrs->status != ti_sdo_ipc_GateMP_CREATED) {
382         *handlePtr = NULL;
383         status = GateMP_E_NOTFOUND;
384     }
385     else {
386         /* Local gate */
387         if (GETREMOTE(attrs->mask) == GateMP_RemoteProtect_NONE) {
388             if (attrs->creatorProcId != MultiProc_self()) {
389                 Error_raise(&eb, ti_sdo_ipc_GateMP_E_localGate, 0, 0);
390                 return (GateMP_E_FAIL);
391             }
393             /* need to atomically increment number of opens */
394             key = Hwi_disable();
396             obj = (ti_sdo_ipc_GateMP_Object *)attrs->arg;
397             *handlePtr = (GateMP_Handle)obj;
398             obj->numOpens++;
400             /* restore hwi mask */
401             Hwi_restore(key);
403             return (GateMP_S_SUCCESS);
404         }
406         /*  need to track number of opens atomically */
407         key = Hwi_disable();
409         /* Remote case */
410         switch (GETREMOTE(attrs->mask)) {
411             case GateMP_RemoteProtect_SYSTEM:
412                 obj = GateMP_module->remoteSystemGates[attrs->arg];
413                 break;
415             case GateMP_RemoteProtect_CUSTOM1:
416                 obj = GateMP_module->remoteCustom1Gates[attrs->arg];
417                 break;
419             case GateMP_RemoteProtect_CUSTOM2:
420                 obj = GateMP_module->remoteCustom2Gates[attrs->arg];
421                 break;
423             default:
424                 obj = NULL;
425                 status = GateMP_E_FAIL;
426                 break;
427         }
429         if (status == GateMP_S_SUCCESS) {
430             /*
431              *  If the object is NULL, then it must have been created on a
432              *  remote processor. Need to create a local object. This is
433              *  accomplished by setting the openFlag to TRUE.
434              */
435             if (obj == NULL) {
436                 /* Create a GateMP object with the openFlag set to true */
437                 ti_sdo_ipc_GateMP_Params_init(&params);
438                 params.openFlag = TRUE;
439                 params.sharedAddr = sharedAddr;
440                 params.resourceId = attrs->arg;
441                 params.localProtect = GETLOCAL(attrs->mask);
442                 params.remoteProtect = GETREMOTE(attrs->mask);
444                 obj = ti_sdo_ipc_GateMP_create(&params, &eb);
445                 if (obj == NULL) {
446                     status = GateMP_E_FAIL;
447                 }
448             }
449             else {
450                 obj->numOpens++;
451             }
452         }
454         /* Return the GateMP instance  */
455         *handlePtr = (GateMP_Handle)obj;
457         /* restore hwi mask */
458         Hwi_restore(key);
459     }
461     return (status);
464 /*
465  *  ======== GateMP_sharedMemReq ========
466  */
467 SizeT GateMP_sharedMemReq(const GateMP_Params *params)
469     SizeT memReq, minAlign;
470     UInt16 regionId;
471     ti_sdo_ipc_GateMP_RemoteSystemProxy_Params systemParams;
472     ti_sdo_ipc_GateMP_RemoteCustom1Proxy_Params custom1Params;
473     ti_sdo_ipc_GateMP_RemoteCustom2Proxy_Params custom2Params;
475     if (params->sharedAddr) {
476         regionId = SharedRegion_getId(params->sharedAddr);
477     }
478     else {
479         regionId = params->regionId;
480     }
482     Assert_isTrue(regionId != SharedRegion_INVALIDREGIONID,
483             ti_sdo_ipc_Ipc_A_internal);
485     minAlign = Memory_getMaxDefaultTypeAlign();
486     if (SharedRegion_getCacheLineSize(regionId) > minAlign) {
487         minAlign = SharedRegion_getCacheLineSize(regionId);
488     }
490     memReq = _Ipc_roundup(sizeof(ti_sdo_ipc_GateMP_Attrs), minAlign);
492     /* add the amount of shared memory required by proxy */
493     if (params->remoteProtect == GateMP_RemoteProtect_SYSTEM) {
494         ti_sdo_ipc_GateMP_RemoteSystemProxy_Params_init(&systemParams);
495         systemParams.regionId = regionId;
496         memReq += ti_sdo_ipc_GateMP_RemoteSystemProxy_sharedMemReq(
497                 (IGateMPSupport_Params *)&systemParams);
498     }
499     else if (params->remoteProtect == GateMP_RemoteProtect_CUSTOM1) {
500         ti_sdo_ipc_GateMP_RemoteCustom1Proxy_Params_init(&custom1Params);
501         custom1Params.regionId = regionId;
502         memReq += ti_sdo_ipc_GateMP_RemoteCustom1Proxy_sharedMemReq(
503                 (IGateMPSupport_Params *)&custom1Params);
504     }
505     else if (params->remoteProtect == GateMP_RemoteProtect_CUSTOM2) {
506         ti_sdo_ipc_GateMP_RemoteCustom2Proxy_Params_init(&custom2Params);
507         custom2Params.regionId = regionId;
508         memReq += ti_sdo_ipc_GateMP_RemoteCustom2Proxy_sharedMemReq(
509                 (IGateMPSupport_Params *)&custom2Params);
510     }
512     return (memReq);
515 /*
516  *************************************************************************
517  *                       Module functions
518  *************************************************************************
519  */
521 /*
522  *  ======== ti_sdo_ipc_GateMP_createLocal ========
523  */
524 IGateProvider_Handle ti_sdo_ipc_GateMP_createLocal(
525     ti_sdo_ipc_GateMP_LocalProtect localProtect)
527     IGateProvider_Handle gateHandle;
529     /* Create the local gate. */
530     switch (localProtect) {
531         case GateMP_LocalProtect_NONE:
532             /* Plug with the GateNull singleton */
533             gateHandle = GateMP_module->gateNull;
534             break;
536         case GateMP_LocalProtect_INTERRUPT:
537             /* Plug with the GateAll singleton */
538             gateHandle = GateMP_module->gateAll;
539             break;
541         case GateMP_LocalProtect_TASKLET:
542             /* Plug with the GateSwi singleton */
543             gateHandle = GateMP_module->gateSwi;
544             break;
546         case GateMP_LocalProtect_THREAD:
547         case GateMP_LocalProtect_PROCESS:
548             /* Plug with the GateMutexPri singleton */
549             gateHandle = GateMP_module->gateMutexPri;
550             break;
552         default:
553             /* Invalid local protection level encountered */
554             Assert_isTrue(FALSE, ti_sdo_ipc_Ipc_A_internal);
555             break;
556     }
558     return (gateHandle);
561 /*
562  *  ======== GateMP_getDefaultRemote ========
563  */
564 GateMP_Handle GateMP_getDefaultRemote()
566     return ((GateMP_Handle)GateMP_module->defaultGate);
569 /*
570  *  ======== GateMP_getLocalProtect ========
571  */
572 GateMP_LocalProtect GateMP_getLocalProtect(GateMP_Handle handle)
574     ti_sdo_ipc_GateMP_Object *obj = (ti_sdo_ipc_GateMP_Object *)handle;
576     Assert_isTrue(obj != NULL, ti_sdo_ipc_Ipc_A_nullArgument);
578     return ((GateMP_LocalProtect)obj->localProtect);
581 /*
582  *  ======== GateMP_getRemoteProtect ========
583  */
584 GateMP_RemoteProtect GateMP_getRemoteProtect(GateMP_Handle handle)
586     ti_sdo_ipc_GateMP_Object *obj = (ti_sdo_ipc_GateMP_Object *)handle;
588     Assert_isTrue(obj != NULL, ti_sdo_ipc_Ipc_A_nullArgument);
590     return ((GateMP_RemoteProtect)obj->remoteProtect);
593 /*
594  *  ======== ti_sdo_ipc_GateMP_getRegion0ReservedSize ========
595  */
596 SizeT ti_sdo_ipc_GateMP_getRegion0ReservedSize(Void)
598     SizeT reserved, minAlign;
600     minAlign = Memory_getMaxDefaultTypeAlign();
602     if (SharedRegion_getCacheLineSize(0) > minAlign) {
603         minAlign = SharedRegion_getCacheLineSize(0);
604     }
606     reserved = _Ipc_roundup(sizeof(ti_sdo_ipc_GateMP_Reserved), minAlign);
608     reserved += _Ipc_roundup(GateMP_module->numRemoteSystem, minAlign);
610     if (GateMP_module->proxyMap[ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM1] ==
611         ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM1) {
612             reserved += _Ipc_roundup(GateMP_module->numRemoteCustom1, minAlign);
613     }
615     if (GateMP_module->proxyMap[ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM2] ==
616         ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM2) {
617             reserved += _Ipc_roundup(GateMP_module->numRemoteCustom2, minAlign);
618     }
620     return (reserved);
623 /*
624  *  ======== ti_sdo_ipc_GateMP_setRegion0Reserved ========
625  */
626 Void ti_sdo_ipc_GateMP_setRegion0Reserved(Ptr sharedAddr)
628     ti_sdo_ipc_GateMP_Reserved *reserve;
629     SizeT minAlign, offset;
630     UInt i;
631     Bits32 *delegateReservedMask;
632     UInt32 nsValue[6];
633     Int ret;
635     minAlign = Memory_getMaxDefaultTypeAlign();
636     if (SharedRegion_getCacheLineSize(0) > minAlign) {
637         minAlign = SharedRegion_getCacheLineSize(0);
638     }
640     /* setup ti_sdo_ipc_GateMP_Reserved fields */
641     reserve = (ti_sdo_ipc_GateMP_Reserved *)sharedAddr;
642     reserve->version = ti_sdo_ipc_GateMP_VERSION;
644     if (SharedRegion_isCacheEnabled(0)) {
645         Cache_wbInv(sharedAddr, sizeof(ti_sdo_ipc_GateMP_Reserved),
646             Cache_Type_ALL, TRUE);
647     }
649     /*
650      *  initialize the in-use array in shared memory for the system gates.
651      */
652     offset = _Ipc_roundup(sizeof(ti_sdo_ipc_GateMP_Reserved), minAlign);
653     GateMP_module->remoteSystemInUse =
654         (Ptr)((UInt32)sharedAddr + offset);
656     if (GateMP_module->numRemoteSystem != 0) {
657         memset(GateMP_module->remoteSystemInUse, 0,
658             GateMP_module->numRemoteSystem * sizeof(UInt8));
659         delegateReservedMask =
660                 ti_sdo_ipc_GateMP_RemoteSystemProxy_getReservedMask();
661         if (delegateReservedMask != NULL) {
662             for (i = 0; i < GateMP_module->numRemoteSystem; i++) {
663                 if (delegateReservedMask[i >> 5] & (1 << (i % 32))) {
664                     GateMP_module->remoteSystemInUse[i] = RESERVED;
665                 }
666             }
667         }
669         if (SharedRegion_isCacheEnabled(0)) {
670             Cache_wbInv(GateMP_module->remoteSystemInUse,
671                 GateMP_module->numRemoteSystem * sizeof(UInt8),
672                 Cache_Type_ALL,
673                 TRUE);
674         }
675     }
677     /*
678      *  initialize the in-use array in shared memory for the custom1 gates.
679      *  Need to check if this proxy is the same as system
680      */
681     offset = _Ipc_roundup(GateMP_module->numRemoteSystem, minAlign);
682     if (GateMP_module->proxyMap[ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM1] ==
683         ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM1) {
684         if (GateMP_module->numRemoteCustom1 != 0) {
685             GateMP_module->remoteCustom1InUse =
686                 GateMP_module->remoteSystemInUse + offset;
688             memset(GateMP_module->remoteCustom1InUse, 0,
689                 GateMP_module->numRemoteCustom1 * sizeof(UInt8));
690             delegateReservedMask =
691                     ti_sdo_ipc_GateMP_RemoteCustom1Proxy_getReservedMask();
692             if (delegateReservedMask != NULL) {
693                 for (i = 0; i < GateMP_module->numRemoteCustom1; i++) {
694                     if (delegateReservedMask[i >> 5] & (1 << (i % 32))) {
695                         GateMP_module->remoteCustom1InUse[i] = RESERVED;
696                     }
697                 }
698             }
699             if (SharedRegion_isCacheEnabled(0)) {
700                 Cache_wbInv(GateMP_module->remoteCustom1InUse,
701                      GateMP_module->numRemoteCustom1 * sizeof(UInt8),
702                      Cache_Type_ALL,
703                      TRUE);
704             }
705         }
706     }
707     else {
708         GateMP_module->remoteCustom1InUse = GateMP_module->remoteSystemInUse;
709         GateMP_module->remoteCustom1Gates = GateMP_module->remoteSystemGates;
710     }
712     /*
713      *  initialize the in-use array in shared memory for the custom2 gates.
714      *  Need to check if this proxy is the same as system or custom1
715      */
716     offset = _Ipc_roundup(GateMP_module->numRemoteCustom1, minAlign);
717     if (GateMP_module->proxyMap[ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM2] ==
718         ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM2) {
719         if (GateMP_module->numRemoteCustom2 != 0) {
720                 GateMP_module->remoteCustom2InUse =
721                     GateMP_module->remoteCustom1InUse + offset;
723             memset(GateMP_module->remoteCustom2InUse, 0,
724                 GateMP_module->numRemoteCustom2 * sizeof(UInt8));
725             delegateReservedMask =
726                     ti_sdo_ipc_GateMP_RemoteCustom2Proxy_getReservedMask();
727             if (delegateReservedMask != NULL) {
728                 for (i = 0; i < GateMP_module->numRemoteCustom2; i++) {
729                     if (delegateReservedMask[i >> 5] & (1 << (i % 32))) {
730                         GateMP_module->remoteCustom2InUse[i] = RESERVED;
731                     }
732                 }
733             }
735             if (SharedRegion_isCacheEnabled(0)) {
736                 Cache_wbInv(GateMP_module->remoteCustom2InUse,
737                      GateMP_module->numRemoteCustom2 * sizeof(UInt8),
738                      Cache_Type_ALL,
739                      TRUE);
740             }
741         }
742     }
743     else if (GateMP_module->proxyMap[ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM2] ==
744              ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM1) {
745         GateMP_module->remoteCustom2InUse =
746             GateMP_module->remoteCustom1InUse;
747         GateMP_module->remoteCustom2Gates =
748             GateMP_module->remoteCustom1Gates;
749     }
750     else {
751         GateMP_module->remoteCustom2InUse = GateMP_module->remoteSystemInUse;
752         GateMP_module->remoteCustom2Gates = GateMP_module->remoteSystemGates;
753     }
755     if (GateMP_module->hostSupport == TRUE) {
756         /* Add special entry to store inuse arrays' location and size */
757         ret = _GateMP_virtToPhys(
758             (UInt32)GateMP_module->remoteSystemInUse, &nsValue[0]);
759         Assert_isTrue(ret == GateMP_S_SUCCESS, (Assert_Id)NULL);
760         (void)ret;   /* silence unused var warning when asserts disabled */
761         if (GateMP_module->numRemoteCustom1 != 0) {
762             ret = _GateMP_virtToPhys(
763                 (UInt32)GateMP_module->remoteCustom1InUse, &nsValue[1]);
764             Assert_isTrue(ret == GateMP_S_SUCCESS, (Assert_Id)NULL);
765         }
766         else {
767             nsValue[1] = 0;
768         }
769         if (GateMP_module->numRemoteCustom2 != 0) {
770             ret = _GateMP_virtToPhys(
771                 (UInt32)GateMP_module->remoteCustom2InUse, &nsValue[2]);
772             Assert_isTrue(ret == GateMP_S_SUCCESS, (Assert_Id)NULL);
773         }
774         else {
775             nsValue[2] = 0;
776         }
777         nsValue[3] = GateMP_module->numRemoteSystem;
778         nsValue[4] = GateMP_module->numRemoteCustom1;
779         nsValue[5] = GateMP_module->numRemoteCustom2;
780         GateMP_module->nsKey = NameServer_add((NameServer_Handle)
781             GateMP_module->nameServer, "_GateMP_TI_info", &nsValue,
782             sizeof(nsValue));
783     }
786 /*
787  *  ======== ti_sdo_ipc_GateMP_openRegion0Reserved ========
788  */
789 Void ti_sdo_ipc_GateMP_openRegion0Reserved(Ptr sharedAddr)
791     ti_sdo_ipc_GateMP_Reserved *reserve;
792     SizeT minAlign, offset;
794     minAlign = Memory_getMaxDefaultTypeAlign();
795     if (SharedRegion_getCacheLineSize(0) > minAlign) {
796         minAlign = SharedRegion_getCacheLineSize(0);
797     }
800     /* setup ti_sdo_ipc_GateMP_Reserved fields */
801     reserve = (ti_sdo_ipc_GateMP_Reserved *)sharedAddr;
803     if (reserve->version != ti_sdo_ipc_GateMP_VERSION) {
804         /* Version mismatch: return an error */
805         Error_raise(NULL, ti_sdo_ipc_Ipc_E_versionMismatch, reserve->version,
806                     ti_sdo_ipc_GateMP_VERSION);
807         return;
808     }
810     offset = _Ipc_roundup(sizeof(ti_sdo_ipc_GateMP_Reserved), minAlign);
811     GateMP_module->remoteSystemInUse =
812         (Ptr)((UInt32)sharedAddr + offset);
814     /*
815      *  initialize the in-use array in shared memory for the custom1 gates.
816      *  Need to check if this proxy is the same as system
817      */
818     offset = _Ipc_roundup(GateMP_module->numRemoteSystem, minAlign);
819     if (GateMP_module->proxyMap[ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM1] ==
820         ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM1) {
821         if (GateMP_module->numRemoteCustom1 != 0) {
822             GateMP_module->remoteCustom1InUse =
823                 GateMP_module->remoteSystemInUse + offset;
824         }
825     }
826     else {
827         GateMP_module->remoteCustom1InUse = GateMP_module->remoteSystemInUse;
828         GateMP_module->remoteCustom1Gates = GateMP_module->remoteSystemGates;
829     }
831     offset = _Ipc_roundup(GateMP_module->numRemoteCustom1, minAlign);
832     if (GateMP_module->proxyMap[ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM2] ==
833         ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM2) {
834         if (GateMP_module->numRemoteCustom2 != 0) {
835             GateMP_module->remoteCustom2InUse =
836                 GateMP_module->remoteCustom1InUse + offset;
837         }
838     }
839     else if (GateMP_module->proxyMap[ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM2] ==
840              ti_sdo_ipc_GateMP_ProxyOrder_CUSTOM1) {
841         GateMP_module->remoteCustom2InUse =
842             GateMP_module->remoteCustom1InUse;
843         GateMP_module->remoteCustom2Gates =
844             GateMP_module->remoteCustom1Gates;
845     }
846     else {
847         GateMP_module->remoteCustom2InUse = GateMP_module->remoteSystemInUse;
848         GateMP_module->remoteCustom2Gates = GateMP_module->remoteSystemGates;
849     }
852 /*
853  *  ======== ti_sdo_ipc_GateMP_attach ========
854  */
855 Int ti_sdo_ipc_GateMP_attach(UInt16 remoteProcId, Ptr sharedAddr)
857     Ptr gateMPsharedAddr;
858     SharedRegion_Entry entry;
859     ti_sdo_ipc_GateMP_Handle defaultGate;
860     Int status = GateMP_S_SUCCESS;
862     /* If default gate is not NULL return since its already set */
863     if (GateMP_getDefaultRemote() != NULL) {
864         return(GateMP_S_ALREADYSETUP);
865     }
867     /* get region 0 information */
868     SharedRegion_getEntry(0, &entry);
870     gateMPsharedAddr = (Ptr)((UInt32)sharedAddr +
871                        ti_sdo_ipc_GateMP_getRegion0ReservedSize());
873     if ((entry.ownerProcId != MultiProc_self()) &&
874         (entry.ownerProcId != MultiProc_INVALIDID)) {
875         /* if not the owner of the SharedRegion */
876         ti_sdo_ipc_GateMP_openRegion0Reserved(sharedAddr);
878         /* open the gate by address */
879         status = GateMP_openByAddr(gateMPsharedAddr,
880                 (GateMP_Handle *)&defaultGate);
882         /* openByAddr should always succeed */
883         Assert_isTrue(status >= 0, ti_sdo_ipc_Ipc_A_internal);
885         /* set the default GateMP for opener */
886         ti_sdo_ipc_GateMP_setDefaultRemote(defaultGate);
887     }
889     return (status);
892 /*
893  *  ======== ti_sdo_ipc_GateMP_detach ========
894  */
895 Int ti_sdo_ipc_GateMP_detach(UInt16 remoteProcId)
897     SharedRegion_Entry entry;
898     GateMP_Handle gate;
899     Int status = GateMP_S_SUCCESS;
901     /* get region 0 information */
902     SharedRegion_getEntry(0, &entry);
904     if ((entry.isValid) &&
905         (entry.ownerProcId == remoteProcId)) {
906         /* get the default gate */
907         gate = GateMP_getDefaultRemote();
909         if (gate != NULL) {
910             /* close the gate */
911             status = GateMP_close(&gate);
913             /* set the default remote gate to NULL */
914             ti_sdo_ipc_GateMP_setDefaultRemote(NULL);
915         }
916     }
918     return (status);
921 /*
922  *  ======== ti_sdo_ipc_GateMP_start ========
923  */
924 Int ti_sdo_ipc_GateMP_start(Ptr sharedAddr)
926     SharedRegion_Entry       entry;
927     ti_sdo_ipc_GateMP_Params gateMPParams;
928     ti_sdo_ipc_GateMP_Handle defaultGate;
929     Int status = GateMP_S_SUCCESS;
930     Error_Block eb;
932     /* get region 0 information */
933     SharedRegion_getEntry(0, &entry);
935     /* if entry owner proc is not specified return */
936     if (entry.ownerProcId == MultiProc_INVALIDID) {
937         return (status);
938     }
940     if (entry.ownerProcId == MultiProc_self()) {
941         /* if owner of the SharedRegion */
942         ti_sdo_ipc_GateMP_setRegion0Reserved(sharedAddr);
944         /* create default GateMP */
945         ti_sdo_ipc_GateMP_Params_init(&gateMPParams);
946         gateMPParams.sharedAddr = (Ptr)((UInt32)sharedAddr +
947                 ti_sdo_ipc_GateMP_getRegion0ReservedSize());
948         gateMPParams.localProtect  = ti_sdo_ipc_GateMP_LocalProtect_TASKLET;
949         gateMPParams.name = "_GateMP_TI_dGate";
951         if (ti_sdo_utils_MultiProc_numProcessors > 1) {
952             gateMPParams.remoteProtect = ti_sdo_ipc_GateMP_RemoteProtect_SYSTEM;
953         }
954         else {
955             gateMPParams.remoteProtect = ti_sdo_ipc_GateMP_RemoteProtect_NONE;
956         }
958         Error_init(&eb);
959         defaultGate = ti_sdo_ipc_GateMP_create(&gateMPParams, &eb);
960         if (defaultGate != NULL) {
961             /* set the default GateMP for creator */
962             ti_sdo_ipc_GateMP_setDefaultRemote(defaultGate);
963         }
964         else {
965             status = GateMP_E_FAIL;
966         }
967     }
969     return (status);
972 /*
973  *  ======== ti_sdo_ipc_GateMP_stop ========
974  */
975 Int ti_sdo_ipc_GateMP_stop()
977     SharedRegion_Entry entry;
978     GateMP_Handle gate;
979     Int status = GateMP_S_SUCCESS;
981     /* get region 0 information */
982     SharedRegion_getEntry(0, &entry);
984     if ((entry.isValid) &&
985         (entry.createHeap) &&
986         (entry.ownerProcId == MultiProc_self())) {
987         /* get the default GateMP */
988         gate = GateMP_getDefaultRemote();
990         if (gate != NULL) {
991             /* set the default GateMP to NULL */
992             ti_sdo_ipc_GateMP_setDefaultRemote(NULL);
994             /* delete the default GateMP */
995             status = GateMP_delete(&gate);
996         }
998         /* Remove global info entry from NameServer */
999         if ((GateMP_module->hostSupport == TRUE) &&
1000             (GateMP_module->nsKey != 0)) {
1001             NameServer_removeEntry((NameServer_Handle)GateMP_module->nameServer,
1002                 GateMP_module->nsKey);
1003         }
1004     }
1006     return (status);
1009 /*
1010  *************************************************************************
1011  *                       Instance functions
1012  *************************************************************************
1013  */
1015 /*
1016  *  ======== ti_sdo_ipc_GateMP_Instance_init ========
1017  */
1018 Int ti_sdo_ipc_GateMP_Instance_init(ti_sdo_ipc_GateMP_Object *obj,
1019         const ti_sdo_ipc_GateMP_Params *params,
1020         Error_Block *eb)
1022     UInt key;
1023     IGateMPSupport_Handle remoteHandle;
1024     IGateProvider_Handle localHandle;
1025     ti_sdo_ipc_GateMP_RemoteSystemProxy_Params systemParams;
1026     ti_sdo_ipc_GateMP_RemoteCustom1Proxy_Params custom1Params;
1027     ti_sdo_ipc_GateMP_RemoteCustom2Proxy_Params custom2Params;
1028     SizeT minAlign, offset;
1029     SharedRegion_SRPtr sharedShmBase;
1030     GateMP_Params sparams;
1031     UInt32 nsValue[4];
1032     UInt32 sizeNsValue;
1033     IHeap_Handle regionHeap;
1035     /* Initialize resourceId to an invalid value */
1036     obj->resourceId = (UInt)-1;
1038     localHandle = ti_sdo_ipc_GateMP_createLocal(params->localProtect);
1040     /* Open GateMP instance */
1041     if (params->openFlag == TRUE) {
1042         /* all open work done here except for remote gateHandle */
1043         obj->localProtect   = params->localProtect;
1044         obj->remoteProtect  = params->remoteProtect;
1045         obj->nsKey          = 0;
1046         obj->numOpens       = 0; /* Will be set to 1 after init() complete */
1047         obj->attrs          = (ti_sdo_ipc_GateMP_Attrs *)params->sharedAddr;
1048         if (obj->attrs != NULL) {
1049             obj->regionId       = SharedRegion_getId((Ptr)obj->attrs);
1050             obj->cacheEnabled   = SharedRegion_isCacheEnabled(obj->regionId);
1051             /* Assert that the buffer is in a valid shared region */
1052             Assert_isTrue(obj->regionId != SharedRegion_INVALIDREGIONID,
1053                       ti_sdo_ipc_Ipc_A_addrNotInSharedRegion);
1054         }
1056         obj->allocSize      = 0;
1057         obj->objType        = ti_sdo_ipc_Ipc_ObjType_OPENDYNAMIC;
1059         minAlign = Memory_getMaxDefaultTypeAlign();
1061         if ((obj->attrs != NULL) &&
1062             (SharedRegion_getCacheLineSize(obj->regionId) > minAlign)) {
1063             minAlign = SharedRegion_getCacheLineSize(obj->regionId);
1064         }
1066         offset = _Ipc_roundup(sizeof(ti_sdo_ipc_GateMP_Attrs), minAlign);
1068         /* TODO: host side created gates cannot have proxy memory */
1069         if (obj->attrs != NULL) {
1070             obj->proxyAttrs = (Ptr)((UInt32)obj->attrs + offset);
1071         }
1072         else {
1073             obj->proxyAttrs = NULL;
1074         }
1075     }
1076     /* Create GateMP instance */
1077     else {
1078         obj->localProtect  = params->localProtect;
1079         obj->remoteProtect = params->remoteProtect;
1080         obj->nsKey         = 0;
1081         obj->numOpens      = 0;
1083         if (obj->remoteProtect == GateMP_RemoteProtect_NONE) {
1084             obj->gateHandle = ti_sdo_ipc_GateMP_createLocal(obj->localProtect);
1085             if (params->sharedAddr != NULL) {
1086                 /* Create a local gate using shared memory */
1087                 obj->objType = ti_sdo_ipc_Ipc_ObjType_CREATEDYNAMIC;
1088                 obj->attrs = params->sharedAddr;
1089                 obj->regionId = SharedRegion_getId((Ptr)obj->attrs);
1090                 obj->cacheEnabled = SharedRegion_isCacheEnabled(obj->regionId);
1091             }
1092             else {
1093                 /* Create a local gate allocating from the local heap */
1094                 obj->objType = ti_sdo_ipc_Ipc_ObjType_LOCAL;
1095                 obj->regionId = ti_sdo_ipc_SharedRegion_INVALIDREGIONID;
1096                 obj->cacheEnabled  = FALSE; /* local */
1097                 obj->attrs = Memory_alloc(NULL,
1098                         sizeof(ti_sdo_ipc_GateMP_Attrs), 0, eb);
1099                 if (obj->attrs == NULL) {
1100                     return (2);
1101                 }
1103             }
1105             obj->attrs->arg = (Bits32)obj;
1106             obj->attrs->mask = SETMASK(obj->remoteProtect, obj->localProtect);
1107             obj->attrs->creatorProcId = MultiProc_self();
1108             obj->attrs->status = ti_sdo_ipc_GateMP_CREATED;
1109             if (obj->cacheEnabled) {
1110                 /*
1111                  *  Need to write back memory if cache is enabled because cache
1112                  *  will be invalidated during openByAddr
1113                  */
1114                 Cache_wbInv(obj->attrs, sizeof(ti_sdo_ipc_GateMP_Attrs),
1115                         Cache_Type_ALL, TRUE);
1116             }
1118             if (params->name) {
1119                 nsValue[0] = (UInt32)obj->attrs;
1120                 /*
1121                  *  Top 16 bits = procId of creator
1122                  *  Bottom 16 bits = '0' if local, '1' otherwise
1123                  */
1124                 nsValue[1] = ((UInt32)MultiProc_self()) << 16;
1126                 if (GateMP_module->hostSupport == TRUE) {
1127                     nsValue[2] = obj->attrs->arg;
1128                     nsValue[3] = obj->attrs->mask;
1129                     sizeNsValue = sizeof(nsValue);
1130                 }
1131                 else {
1132                     sizeNsValue = 2 * sizeof(UInt32);
1133                 }
1135                 obj->nsKey = NameServer_add((NameServer_Handle)
1136                         GateMP_module->nameServer, params->name, &nsValue,
1137                         sizeNsValue);
1139                 if (obj->nsKey == NULL) {
1140                     Error_raise(eb, ti_sdo_ipc_Ipc_E_nameFailed, params->name,
1141                         0);
1142                     return (3);
1143                 }
1144             }
1146             /* Nothing else to do for local gates */
1147             return (0);
1148         }
1150         if (params->sharedAddr == NULL) {
1151             /* Need to allocate from heap */
1152             obj->objType        = ti_sdo_ipc_Ipc_ObjType_CREATEDYNAMIC_REGION;
1153             obj->regionId       = params->regionId;
1154             GateMP_getSharedParams(&sparams, params);
1155             obj->allocSize      = GateMP_sharedMemReq(&sparams);
1156             obj->cacheEnabled   = SharedRegion_isCacheEnabled(obj->regionId);
1158             /* The region heap will do the alignment */
1159             regionHeap = SharedRegion_getHeap(obj->regionId);
1160             Assert_isTrue(regionHeap != NULL, ti_sdo_ipc_SharedRegion_A_noHeap);
1161             obj->attrs = Memory_alloc(regionHeap, obj->allocSize, 0, eb);
1162             if (obj->attrs == NULL) {
1163                 return (3);
1164             }
1166             minAlign = Memory_getMaxDefaultTypeAlign();
1168             if (SharedRegion_getCacheLineSize(obj->regionId) > minAlign) {
1169                 minAlign = SharedRegion_getCacheLineSize(obj->regionId);
1170             }
1172             offset = _Ipc_roundup(sizeof(ti_sdo_ipc_GateMP_Attrs), minAlign);
1174             obj->proxyAttrs = (Ptr)((UInt32)obj->attrs + offset);
1175         }
1176         else {
1177             /* creating using sharedAddr */
1178             obj->regionId = SharedRegion_getId(params->sharedAddr);
1179             /* Assert that the buffer is in a valid shared region */
1180             Assert_isTrue(obj->regionId != SharedRegion_INVALIDREGIONID,
1181                           ti_sdo_ipc_Ipc_A_addrNotInSharedRegion);
1183             obj->attrs = (ti_sdo_ipc_GateMP_Attrs *)params->sharedAddr;
1184             obj->cacheEnabled  = SharedRegion_isCacheEnabled(obj->regionId);
1185             obj->objType = ti_sdo_ipc_Ipc_ObjType_CREATEDYNAMIC;
1187             minAlign = Memory_getMaxDefaultTypeAlign();
1189             if (SharedRegion_getCacheLineSize(obj->regionId) > minAlign) {
1190                 minAlign = SharedRegion_getCacheLineSize(obj->regionId);
1191             }
1193             /* Assert that sharedAddr has correct alignment */
1194             Assert_isTrue(minAlign == 0 ||
1195                           ((UInt32)params->sharedAddr % minAlign) == 0,
1196                           ti_sdo_ipc_Ipc_A_addrNotCacheAligned);
1198             offset = _Ipc_roundup(sizeof(ti_sdo_ipc_GateMP_Attrs), minAlign);
1200             obj->proxyAttrs = (Ptr)((UInt32)obj->attrs + offset);
1201         }
1202     }
1204     /* Proxy work for open and create done here */
1205     switch (obj->remoteProtect) {
1206         case GateMP_RemoteProtect_SYSTEM:
1207             if (obj->objType != ti_sdo_ipc_Ipc_ObjType_OPENDYNAMIC) {
1208                 /* Created Instance */
1209                 obj->resourceId = GateMP_getFreeResource(
1210                     GateMP_module->remoteSystemInUse,
1211                              GateMP_module->numRemoteSystem, eb);
1212                 if (Error_check(eb) == TRUE) {
1213                     return (4);
1214                 }
1215             }
1216             else {
1217                 /* resourceId set by open call */
1218                 obj->resourceId = params->resourceId;
1219             }
1221             /* Create the proxy object */
1222             ti_sdo_ipc_GateMP_RemoteSystemProxy_Params_init(&systemParams);
1223             systemParams.resourceId = obj->resourceId;
1224             systemParams.openFlag =
1225                     (obj->objType == ti_sdo_ipc_Ipc_ObjType_OPENDYNAMIC);
1226             systemParams.sharedAddr = obj->proxyAttrs;
1227             systemParams.regionId = obj->regionId;
1228             remoteHandle = ti_sdo_ipc_GateMP_RemoteSystemProxy_create(
1229                     localHandle, &systemParams, eb);
1231             if (remoteHandle == NULL) {
1232                 return (5);
1233             }
1235             /* Fill in the local array because it is cooked */
1236             key = Hwi_disable();
1237             GateMP_module->remoteSystemGates[obj->resourceId] = obj;
1238             Hwi_restore(key);
1239             break;
1241         case GateMP_RemoteProtect_CUSTOM1:
1242             if (obj->objType != ti_sdo_ipc_Ipc_ObjType_OPENDYNAMIC) {
1243                 obj->resourceId = GateMP_getFreeResource(
1244                              GateMP_module->remoteCustom1InUse,
1245                              GateMP_module->numRemoteCustom1, eb);
1246                 if (Error_check(eb) == TRUE) {
1247                     return (4);
1248                 }
1249             }
1250             else {
1251                 /* resourceId set by open call */
1252                 obj->resourceId = params->resourceId;
1253             }
1255             /* Create the proxy object */
1256             ti_sdo_ipc_GateMP_RemoteCustom1Proxy_Params_init(&custom1Params);
1257             custom1Params.resourceId = obj->resourceId;
1258             custom1Params.openFlag =
1259                     (obj->objType == ti_sdo_ipc_Ipc_ObjType_OPENDYNAMIC);
1260             custom1Params.sharedAddr = obj->proxyAttrs;
1261             custom1Params.regionId = obj->regionId;
1262             remoteHandle = ti_sdo_ipc_GateMP_RemoteCustom1Proxy_create(
1263                     localHandle, &custom1Params, eb);
1264             if (remoteHandle == NULL) {
1265                 return (5);
1266             }
1268             /* Fill in the local array because it is cooked */
1269             key = Hwi_disable();
1270             GateMP_module->remoteCustom1Gates[obj->resourceId] = obj;
1271             Hwi_restore(key);
1272             break;
1274         case GateMP_RemoteProtect_CUSTOM2:
1275             if (obj->objType != ti_sdo_ipc_Ipc_ObjType_OPENDYNAMIC) {
1276                 obj->resourceId = GateMP_getFreeResource(
1277                              GateMP_module->remoteCustom2InUse,
1278                              GateMP_module->numRemoteCustom2, eb);
1279                 if (Error_check(eb) == TRUE) {
1280                     return (4);
1281                 }
1282             }
1283             else {
1284                 /* resourceId set by open call */
1285                 obj->resourceId = params->resourceId;
1286             }
1288             /* Create the proxy object */
1289             ti_sdo_ipc_GateMP_RemoteCustom2Proxy_Params_init(&custom2Params);
1290             custom2Params.resourceId = obj->resourceId;
1291             custom2Params.openFlag =
1292                     (obj->objType == ti_sdo_ipc_Ipc_ObjType_OPENDYNAMIC);
1293             custom2Params.sharedAddr = obj->proxyAttrs;
1294             custom2Params.regionId = obj->regionId;
1295             remoteHandle = ti_sdo_ipc_GateMP_RemoteCustom2Proxy_create(
1296                     localHandle, &custom2Params, eb);
1297             if (remoteHandle == NULL) {
1298                 return (5);
1299             }
1301             /* Fill in the local array because it is cooked */
1302             key = Hwi_disable();
1303             GateMP_module->remoteCustom2Gates[obj->resourceId] = obj;
1304             Hwi_restore(key);
1305             break;
1307         default:
1308             /* Should never be here */
1309             Assert_isTrue(FALSE, ti_sdo_ipc_Ipc_A_internal);
1310             return (5);  /* keep Coverity happy */
1311     }
1313     obj->gateHandle = IGateMPSupport_Handle_upCast(remoteHandle);
1315     /* Place Name/Attrs into NameServer table */
1316     if (obj->objType != ti_sdo_ipc_Ipc_ObjType_OPENDYNAMIC) {
1317         /* Fill in the attrs */
1318         obj->attrs->arg = obj->resourceId;
1319         obj->attrs->mask = SETMASK(obj->remoteProtect, obj->localProtect);
1320         obj->attrs->creatorProcId = MultiProc_self();
1321         obj->attrs->status = ti_sdo_ipc_GateMP_CREATED;
1323         if (obj->cacheEnabled) {
1324             Cache_wbInv(obj->attrs, sizeof(ti_sdo_ipc_GateMP_Attrs),
1325                     Cache_Type_ALL, TRUE);
1326         }
1328         if (params->name != NULL) {
1329             sharedShmBase = SharedRegion_getSRPtr(obj->attrs, obj->regionId);
1330             nsValue[0] = (UInt32)sharedShmBase;
1331             /*
1332              *  Top 16 bits = procId of creator
1333              *  Bottom 16 bits = '0' if local, '1' otherwise
1334              */
1335             nsValue[1] = ((UInt32)MultiProc_self() << 16) | 1;
1337             if (GateMP_module->hostSupport == TRUE) {
1338                 /* Making a copy of these for host processor */
1339                 nsValue[2] = obj->attrs->arg;
1340                 nsValue[3] = obj->attrs->mask;
1341                 sizeNsValue = sizeof(nsValue);
1342             }
1343             else {
1344                 sizeNsValue = 2 * sizeof(UInt32);
1345             }
1347             obj->nsKey = NameServer_add((NameServer_Handle)
1348                     GateMP_module->nameServer, params->name, &nsValue,
1349                     sizeNsValue);
1351             if (obj->nsKey == NULL) {
1352                 Error_raise(eb, ti_sdo_ipc_Ipc_E_nameFailed, params->name, 0);
1353                 return (6);
1354             }
1355         }
1357         Log_write2(ti_sdo_ipc_GateMP_LM_create,
1358                 (UArg)obj->remoteProtect, (UArg)obj->resourceId);
1359     }
1360     else {
1361         obj->numOpens = 1;
1363         Log_write2(ti_sdo_ipc_GateMP_LM_open,
1364             (UArg)obj->remoteProtect, (UArg)obj->resourceId);
1365     }
1367     return (0);
1370 /*
1371  *  ======== ti_sdo_ipc_GateMP_Instance_finalize ========
1372  */
1373 Void ti_sdo_ipc_GateMP_Instance_finalize(
1374         ti_sdo_ipc_GateMP_Object *obj, Int status)
1376     UInt systemKey;
1377     ti_sdo_ipc_GateMP_RemoteSystemProxy_Handle systemHandle;
1378     ti_sdo_ipc_GateMP_RemoteCustom1Proxy_Handle custom1Handle;
1379     ti_sdo_ipc_GateMP_RemoteCustom2Proxy_Handle custom2Handle;
1381     ti_sdo_ipc_GateMP_Handle *remoteHandles;
1382     UInt8 *inUseArray;
1383     UInt size;
1385     /* Cannot call when numOpen is non-zero. */
1386     Assert_isTrue(obj->numOpens == 0, ti_sdo_ipc_GateMP_A_invalidDelete);
1388     /* Removed from NameServer */
1389     if (obj->nsKey != 0) {
1390         NameServer_removeEntry((NameServer_Handle)GateMP_module->nameServer,
1391                 obj->nsKey);
1392     }
1394     /* Set the status to 0 */
1395     if (obj->objType != ti_sdo_ipc_Ipc_ObjType_OPENDYNAMIC) {
1396         obj->attrs->status = 0;
1397         if (obj->cacheEnabled) {
1398             Cache_wbInv(obj->attrs, sizeof(ti_sdo_ipc_GateMP_Attrs),
1399                     Cache_Type_ALL, TRUE);
1400         }
1401     }
1403     /*
1404      *  If ObjType_LOCAL, memory was allocated from the local system heap.
1405      *  obj->attrs might be NULL if the Memory_alloc failed in Instance_init
1406      */
1407     if (obj->objType == ti_sdo_ipc_Ipc_ObjType_LOCAL && obj->attrs != NULL) {
1408         Memory_free(NULL, obj->attrs, sizeof(ti_sdo_ipc_GateMP_Attrs));
1409     }
1411     switch (obj->remoteProtect) {
1412         case GateMP_RemoteProtect_SYSTEM:
1413             if (obj->gateHandle) {
1414                 systemHandle =
1415                 ti_sdo_ipc_GateMP_RemoteSystemProxy_Handle_downCast2(
1416                         obj->gateHandle);
1417                 ti_sdo_ipc_GateMP_RemoteSystemProxy_delete(&systemHandle);
1418             }
1420             inUseArray = GateMP_module->remoteSystemInUse;
1421             remoteHandles = GateMP_module->remoteSystemGates;
1422             size = GateMP_module->numRemoteSystem * sizeof(UInt8);
1423             break;
1425         case GateMP_RemoteProtect_CUSTOM1:
1426             if (obj->gateHandle) {
1427                 custom1Handle =
1428                         ti_sdo_ipc_GateMP_RemoteCustom1Proxy_Handle_downCast2(
1429                             obj->gateHandle);
1430                 ti_sdo_ipc_GateMP_RemoteCustom1Proxy_delete(&custom1Handle);
1431             }
1433             inUseArray = GateMP_module->remoteCustom1InUse;
1434             remoteHandles = GateMP_module->remoteCustom1Gates;
1435             size = GateMP_module->numRemoteCustom1 * sizeof(UInt8);
1436             break;
1438         case GateMP_RemoteProtect_CUSTOM2:
1439             if (obj->gateHandle) {
1440                 custom2Handle =
1441                         ti_sdo_ipc_GateMP_RemoteCustom2Proxy_Handle_downCast2(
1442                             obj->gateHandle);
1443                 ti_sdo_ipc_GateMP_RemoteCustom2Proxy_delete(&custom2Handle);
1444             }
1446             inUseArray = GateMP_module->remoteCustom2InUse;
1447             remoteHandles = GateMP_module->remoteCustom2Gates;
1448             size = GateMP_module->numRemoteCustom2 * sizeof(UInt8);
1449             break;
1450         case GateMP_RemoteProtect_NONE:
1451             /*
1452              *  Nothing else to finalize. Any alloc'ed memory has already been
1453              *  freed
1454              */
1455             Log_write2(ti_sdo_ipc_GateMP_LM_delete, (UArg)obj->remoteProtect,
1456                 (UArg)obj->resourceId);
1457             return;
1458         default:
1459             /* Should never be here */
1460             Assert_isTrue(FALSE, ti_sdo_ipc_Ipc_A_internal);
1461             break;
1462     }
1464     /* Clear the handle array entry in local memory */
1465     if (obj->resourceId != (UInt)-1) {
1466         remoteHandles[obj->resourceId] = NULL;
1467     }
1469     if ((obj->objType != ti_sdo_ipc_Ipc_ObjType_OPENDYNAMIC) &&
1470         (obj->resourceId != (UInt)-1)) {
1471         /* Clear the resource used flag in shared memory */
1472         if (GateMP_module->defaultGate != NULL) {
1473             systemKey = GateMP_enter((GateMP_Handle)GateMP_module->defaultGate);
1474         }
1475         else {
1476             /* this should only be done when deleting the very last GateMP */
1477             systemKey = Hwi_disable();
1478         }
1480 #ifdef xdc_target__isaCompatible_v7A
1481         /* ARM speculative execution might have pulled array into cache */
1482         if (obj->cacheEnabled) {
1483             Cache_inv(inUseArray, size, Cache_Type_ALL, TRUE);
1484         }
1485 #endif
1486         /* update the GateMP resource tracker */
1487         inUseArray[obj->resourceId] = UNUSED;
1488         if (obj->cacheEnabled) {
1489             Cache_wbInv(inUseArray, size, Cache_Type_ALL, TRUE);
1490         }
1492         if (GateMP_module->defaultGate != NULL) {
1493             GateMP_leave((GateMP_Handle)GateMP_module->defaultGate, systemKey);
1494         }
1495         else {
1496             Hwi_restore(systemKey);
1497         }
1498     }
1500     if (obj->objType == ti_sdo_ipc_Ipc_ObjType_CREATEDYNAMIC_REGION) {
1501         /* Free memory allocated from the region heap */
1502         if (obj->attrs) {
1503             Memory_free(SharedRegion_getHeap(obj->regionId), obj->attrs,
1504                 obj->allocSize);
1505         }
1506     }
1508     Log_write2(ti_sdo_ipc_GateMP_LM_delete, (UArg)obj->remoteProtect,
1509             (UArg)obj->resourceId);
1512 /*
1513  *  ======== ti_sdo_ipc_GateMP_getSharedAddr ========
1514  */
1515 SharedRegion_SRPtr ti_sdo_ipc_GateMP_getSharedAddr(
1516         ti_sdo_ipc_GateMP_Object *obj)
1518     SharedRegion_SRPtr srPtr;
1520     srPtr = SharedRegion_getSRPtr(obj->attrs, obj->regionId);
1522     return (srPtr);
1525 /*
1526  *  ======== ti_sdo_ipc_GateMP_setDefaultRemote ========
1527  */
1528 Void ti_sdo_ipc_GateMP_setDefaultRemote(ti_sdo_ipc_GateMP_Object *obj)
1530     GateMP_module->defaultGate = obj;
1533 /*
1534  *************************************************************************
1535  *                       Internal functions
1536  *************************************************************************
1537  */
1539 /*
1540  *  ======== ti_sdo_ipc_GateMP_getFreeResource ========
1541  */
1542 UInt ti_sdo_ipc_GateMP_getFreeResource(UInt8 *inUse, Int num, Error_Block *eb)
1544     UInt key;
1545     Bool flag = FALSE;
1546     UInt resourceId;
1547     GateMP_Handle defaultGate;
1549     /* Need to look at shared memory. Enter default gate */
1550     defaultGate = GateMP_getDefaultRemote();
1552     if (defaultGate){
1553         key = GateMP_enter(defaultGate);
1554     }
1556     /* Invalidate cache before looking at the in-use flags */
1557     if (SharedRegion_isCacheEnabled(0)) {
1558         Cache_inv(inUse, num * sizeof(UInt8), Cache_Type_ALL, TRUE);
1559     }
1561     /*
1562      *  Find a free resource id. Note: zero is reserved on the
1563      *  system proxy for the default gate.
1564      */
1565     for (resourceId = 0; resourceId < num; resourceId++) {
1566         /*
1567          *  If not in-use, set the inUse to TRUE to prevent other
1568          *  creates from getting this one.
1569          */
1570         if (inUse[resourceId] == UNUSED) {
1571            flag = TRUE;
1573            /* Denote in shared memory that the resource is used */
1574            inUse[resourceId] = USED;
1575            break;
1576        }
1577     }
1579     /* Write-back if a in-use flag was changed */
1580     if (flag == TRUE && SharedRegion_isCacheEnabled(0)) {
1581         Cache_wbInv(inUse, num * sizeof(UInt8), Cache_Type_ALL, TRUE);
1582     }
1584     /* Done with the critical section */
1585     if (defaultGate) {
1586         GateMP_leave(defaultGate, key);
1587     }
1589     if (flag == FALSE) {
1590         resourceId = (UInt)-1;
1591         Error_raise(eb, ti_sdo_ipc_GateMP_E_gateUnavailable, 0, 0);
1592     }
1594     return (resourceId);