]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - src/ti/sdo/ipc/transports/TransportShm.c
Rename: Renamed packages to src to allow application of ipc-j tree commits.
[ipc/ipcdev.git] / src / ti / sdo / ipc / transports / TransportShm.c
1 /*
2  * Copyright (c) 2012-2013, Texas Instruments Incorporated
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  *  ======== TransportShm.c ========
34  */
36 #include <xdc/std.h>
37 #include <xdc/runtime/Assert.h>
38 #include <xdc/runtime/Error.h>
39 #include <xdc/runtime/Gate.h>
40 #include <xdc/runtime/Memory.h>
42 #include <ti/sysbios/hal/Cache.h>
43 #include <ti/sysbios/knl/Swi.h>
45 #include "package/internal/TransportShm.xdc.h"
47 #include <ti/sdo/ipc/_Ipc.h>
48 #include <ti/sdo/utils/_MultiProc.h>
49 #include <ti/sdo/ipc/_SharedRegion.h>
50 #include <ti/sdo/ipc/_Notify.h>
51 #include <ti/sdo/ipc/_GateMP.h>
52 #include <ti/sdo/ipc/_ListMP.h>
53 #include <ti/sdo/ipc/_MessageQ.h>
55 /* Need to use reserved notify events */
56 #undef TransportShm_notifyEventId
57 #define TransportShm_notifyEventId \
58         ti_sdo_ipc_transports_TransportShm_notifyEventId + \
59                     (UInt32)((UInt32)Notify_SYSTEMKEY << 16)
61 /*
62  *************************************************************************
63  *                       Module functions
64  *************************************************************************
65  */
68 /*
69  *  ======== TransportShm_notifyFxn ========
70  */
71 Void TransportShm_notifyFxn(UInt16 procId,
72                             UInt16 lineId,
73                             UInt32 eventId,
74                             UArg arg,
75                             UInt32 payload)
76 {
77     Swi_Handle swiHandle;
79     /* Swi_Handle was passed as arg in register */
80     swiHandle = (Swi_Handle)arg;
82     /* post the Swi */
83     Swi_post(swiHandle);
84 }
86 /*
87  *  ======== TransportShm_swiFxn ========
88  */
89 Void TransportShm_swiFxn(UArg arg)
90 {
91     UInt32 queueId;
92     TransportShm_Object *obj = (TransportShm_Object *)arg;
93     MessageQ_Msg msg = NULL;
95     /*
96      *  While there are messages, get them out and send them to
97      *  their final destination.
98      */
99     msg = (MessageQ_Msg)ListMP_getHead((ListMP_Handle)obj->localList);
100     while (msg != NULL) {
101         /* Get the destination message queue Id */
102         queueId = MessageQ_getDstQueue(msg);
104         /* put the message to the destination queue */
105         MessageQ_put(queueId, msg);
107         /* check to see if there are more messages */
108         msg = (MessageQ_Msg)ListMP_getHead((ListMP_Handle)obj->localList);
109     }
112 /*
113  *************************************************************************
114  *                       Instance functions
115  *************************************************************************
116  */
118 /*
119  *  ======== TransportShm_Instance_init ========
120  */
121 Int TransportShm_Instance_init(TransportShm_Object *obj,
122         UInt16 procId, const TransportShm_Params *params,
123         Error_Block *eb)
125     Int            localIndex;
126     Int            remoteIndex;
127     Int            status;
128     Bool           flag;
129     UInt32         minAlign;
130     ListMP_Params  listMPParams[2];
131     Swi_Handle     swiHandle;
132     Swi_Params     swiParams;
133     Ptr            localAddr;
135     swiHandle = TransportShm_Instance_State_swiObj(obj);
137     /*
138      *  Determine who gets the '0' slot in shared memory and who gets
139      *  the '1' slot. The '0' slot is given to the lower MultiProc id.
140      */
141     if (MultiProc_self() < procId) {
142         localIndex  = 0;
143         remoteIndex = 1;
144     }
145     else {
146         localIndex  = 1;
147         remoteIndex = 0;
148     }
150     if (params->openFlag) {
151         /* Open by sharedAddr */
152         obj->objType = ti_sdo_ipc_Ipc_ObjType_OPENDYNAMIC;
153         obj->self = (TransportShm_Attrs *)params->sharedAddr;
154         obj->regionId = SharedRegion_getId(params->sharedAddr);
155         obj->cacheEnabled = SharedRegion_isCacheEnabled(obj->regionId);
157         localAddr = SharedRegion_getPtr(obj->self->gateMPAddr);
158         status = GateMP_openByAddr(localAddr, (GateMP_Handle *)&obj->gate);
159         if (status < 0) {
160             Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
161             return(1);
162         }
163     }
164     else {
165         /* init the gate for ListMP create below */
166         if (params->gate != NULL) {
167             obj->gate = params->gate;
168         }
169         else {
170             obj->gate = (ti_sdo_ipc_GateMP_Handle)GateMP_getDefaultRemote();
171         }
173         /* Creating using sharedAddr */
174         obj->regionId = SharedRegion_getId(params->sharedAddr);
176         /* Assert that the buffer is in a valid shared region */
177         Assert_isTrue(obj->regionId != SharedRegion_INVALIDREGIONID,
178                 ti_sdo_ipc_Ipc_A_addrNotInSharedRegion);
180         /* Assert that sharedAddr is cache aligned */
181         Assert_isTrue(((UInt32)params->sharedAddr %
182                 SharedRegion_getCacheLineSize(obj->regionId) == 0),
183                 ti_sdo_ipc_Ipc_A_addrNotCacheAligned);
185         /* set object's cacheEnabled, type, self */
186         obj->cacheEnabled = SharedRegion_isCacheEnabled(obj->regionId);
187         obj->objType = ti_sdo_ipc_Ipc_ObjType_CREATEDYNAMIC;
188         obj->self = (TransportShm_Attrs *)params->sharedAddr;
189     }
191     /* determine the minimum alignment to align to */
192     minAlign = Memory_getMaxDefaultTypeAlign();
193     if (SharedRegion_getCacheLineSize(obj->regionId) > minAlign) {
194         minAlign = SharedRegion_getCacheLineSize(obj->regionId);
195     }
197     /*
198      *  Carve up the shared memory.
199      *  If cache is enabled, these need to be on separate cache lines.
200      *  This is done with minAlign and _Ipc_roundup function.
201      */
202     obj->other = (TransportShm_Attrs *)((UInt32)(obj->self) +
203         (_Ipc_roundup(sizeof(TransportShm_Attrs), minAlign)));
205     ListMP_Params_init(&(listMPParams[0]));
206     listMPParams[0].gate = (GateMP_Handle)obj->gate;
207     listMPParams[0].sharedAddr = (UInt32 *)((UInt32)(obj->other) +
208         (_Ipc_roundup(sizeof(TransportShm_Attrs), minAlign)));
210     ListMP_Params_init(&listMPParams[1]);
211     listMPParams[1].gate = (GateMP_Handle)obj->gate;
212     listMPParams[1].sharedAddr = (UInt32 *)((UInt32)(listMPParams[0].sharedAddr)
213         + ListMP_sharedMemReq(&listMPParams[0]));
215     obj->priority      = params->priority;
216     obj->remoteProcId  = procId;
218     Swi_Params_init(&swiParams);
219     swiParams.arg0 = (UArg)obj;
220     Swi_construct(Swi_struct(swiHandle),
221                  (Swi_FuncPtr)TransportShm_swiFxn,
222                  &swiParams, eb);
224     if (params->openFlag == FALSE) {
225         obj->localList = (ti_sdo_ipc_ListMP_Handle)
226                 ListMP_create(&(listMPParams[localIndex]));
227         if (obj->localList == NULL) {
228             Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
229             return (2);
230         }
232         obj->remoteList = (ti_sdo_ipc_ListMP_Handle)
233                 ListMP_create(&(listMPParams[remoteIndex]));
234         if (obj->localList == NULL) {
235             Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
236             return (2);
237         }
238     }
239     else {
240         /* Open the local ListMP instance */
241         status = ListMP_openByAddr(listMPParams[localIndex].sharedAddr,
242                                    (ListMP_Handle *)&(obj->localList));
244         if (status < 0) {
245             Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
246             return (2);
247         }
249         /* Open the remote ListMP instance */
250         status = ListMP_openByAddr(listMPParams[remoteIndex].sharedAddr,
251                                    (ListMP_Handle *)&(obj->remoteList));
253         if (status < 0) {
254             Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
255             return (2);
256         }
257     }
259     /* register the event with Notify */
260     status = Notify_registerEventSingle(
261                  procId,    /* remoteProcId */
262                  0,         /* lineId */
263                  TransportShm_notifyEventId,
264                  (Notify_FnNotifyCbck)TransportShm_notifyFxn,
265                  (UArg)swiHandle);
266     if (status < 0) {
267         Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
268         return (3);
269     }
271     /* Register the transport with MessageQ */
272     flag = ti_sdo_ipc_MessageQ_registerTransport(
273             TransportShm_Handle_upCast(obj), procId, params->priority);
274     if (flag == FALSE) {
275         Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
276         return (4);
277     }
279     if (params->openFlag == FALSE) {
280         obj->self->creatorProcId = MultiProc_self();
281         obj->self->notifyEventId = TransportShm_notifyEventId;
282         obj->self->priority = obj->priority;
284         /* Store the GateMP sharedAddr in the Attrs */
285         obj->self->gateMPAddr = ti_sdo_ipc_GateMP_getSharedAddr(obj->gate);
286         obj->self->flag = TransportShm_UP;
288         if (obj->cacheEnabled) {
289             Cache_wbInv(obj->self, sizeof(TransportShm_Attrs),
290                      Cache_Type_ALL, TRUE);
291         }
292     }
293     else {
294         obj->other->flag = TransportShm_UP;
295         if (obj->cacheEnabled) {
296             Cache_wbInv(&(obj->other->flag), minAlign, Cache_Type_ALL, TRUE);
297         }
298     }
300     obj->status = TransportShm_UP;
302     return (0);
305 /*
306  *  ======== TransportShm_Instance_finalize ========
307  */
308 Void TransportShm_Instance_finalize(TransportShm_Object* obj, Int status)
310     Swi_Handle     swiHandle;
312     if (obj->objType == ti_sdo_ipc_Ipc_ObjType_CREATEDYNAMIC) {
313         /* clear the self flag */
314         obj->self->flag = 0;
316         if (obj->cacheEnabled) {
317             Cache_wbInv(&(obj->self->flag),
318                 SharedRegion_getCacheLineSize(obj->regionId), Cache_Type_ALL,
319                     TRUE);
320         }
322         if (obj->localList != NULL) {
323             ListMP_delete((ListMP_Handle *)&(obj->localList));
324         }
326         if (obj->remoteList != NULL) {
327             ListMP_delete((ListMP_Handle *)&(obj->remoteList));
328         }
329     }
330     else {
331         /* other flag was set by remote proc */
332         obj->other->flag = 0;
334         if (obj->cacheEnabled) {
335             Cache_wbInv(&(obj->other->flag),
336                 SharedRegion_getCacheLineSize(obj->regionId), Cache_Type_ALL,
337                     TRUE);
338         }
340         if (obj->gate != NULL) {
341             GateMP_close((GateMP_Handle *)&(obj->gate));
342         }
344         if (obj->localList != NULL) {
345             ListMP_close((ListMP_Handle *)&(obj->localList));
346         }
348         if (obj->remoteList != NULL) {
349             ListMP_close((ListMP_Handle *)&(obj->remoteList));
350         }
351     }
353     switch(status) {
354         case 0:
355             /* MessageQ_registerTransport succeeded */
356             ti_sdo_ipc_MessageQ_unregisterTransport(obj->remoteProcId, obj->priority);
357             /* OK to fall through */
358         case 1: /* GateMP open failed */
359         case 2: /* ListMP create/open failed */
360         case 3: /* Notify_registerEventSingle failed */
361         case 4: /* MessageQ_registerTransport failed */
362             Notify_unregisterEventSingle(
363                 obj->remoteProcId,
364                 0,
365                 TransportShm_notifyEventId);
366             break;
367     }
369     /* Destruct the swi */
370     swiHandle = TransportShm_Instance_State_swiObj(obj);
371     if (swiHandle != NULL) {
372         Swi_destruct(Swi_struct(swiHandle));
373     }
376 /*
377  *  ======== TransportShm_put ========
378  *  Assuming MessageQ_put is making sure that the arguments are ok
379  */
380 Bool TransportShm_put(TransportShm_Object *obj, Ptr msg)
382     Int32 status;
383     Bool retval = TRUE;
384     IArg key;
385     UInt16 id = SharedRegion_getId(msg);
387     /* This transport only deals with messages allocated from SR's */
388     Assert_isTrue(id != SharedRegion_INVALIDREGIONID,
389             ti_sdo_ipc_SharedRegion_A_regionInvalid);
391     /* writeback invalidate the message */
392     if (SharedRegion_isCacheEnabled(id)) {
393         Cache_wbInv(msg, ((MessageQ_Msg)(msg))->msgSize, Cache_Type_ALL,
394             TRUE);
395     }
397     /* make sure ListMP_put and sendEvent are done before remote executes */
398     key = GateMP_enter((GateMP_Handle)obj->gate);
400     /* Put the message on the remoteList */
401     ListMP_putTail((ListMP_Handle)obj->remoteList, (ListMP_Elem *)msg);
403     /* Notify the remote processor */
404     status = Notify_sendEvent(obj->remoteProcId, 0, TransportShm_notifyEventId,
405         0, FALSE);
407     /* check the status of the sendEvent */
408     if (status < 0) {
409         /* remove the message from the List and return 'FALSE' */
410         ListMP_remove((ListMP_Handle)obj->remoteList, (ListMP_Elem *)msg);
411         retval = FALSE;
412     }
414     /* leave the gate */
415     GateMP_leave((GateMP_Handle)obj->gate, key);
417     return (retval);
420 /*
421  *  ======== TransportShm_control ========
422  */
423 Bool TransportShm_control(TransportShm_Object *obj, UInt cmd, UArg cmdArg)
425     return (FALSE);
428 /*
429  *  ======== TransportShm_getStatus ========
430  */
431 Int TransportShm_getStatus(TransportShm_Object *obj)
433     return (obj->status);
436 /*
437  *************************************************************************
438  *                      Module functions
439  *************************************************************************
440  */
442 /*
443  *  ======== TransportShm_close ========
444  */
445 Void TransportShm_close(TransportShm_Handle *handle)
447     TransportShm_delete(handle);
450 /*
451  *  ======== TransportShm_openByAddr ========
452  */
453 Int TransportShm_openByAddr(Ptr sharedAddr,
454                       TransportShm_Handle *handlePtr,
455                       Error_Block *eb)
457     TransportShm_Params params;
458     TransportShm_Attrs *attrs;
459     Int status;
460     UInt16 id;
462     if (sharedAddr == NULL) {
463         return (MessageQ_E_FAIL);
464     }
466     TransportShm_Params_init(&params);
468     /* Tell Instance_init() that we're opening */
469     params.openFlag = TRUE;
471     attrs = (TransportShm_Attrs *)sharedAddr;
472     id = SharedRegion_getId(sharedAddr);
474     /* Assert that the region is valid */
475     Assert_isTrue(id != SharedRegion_INVALIDREGIONID,
476             ti_sdo_ipc_Ipc_A_addrNotInSharedRegion);
478     /* invalidate the attrs before using it */
479     if (SharedRegion_isCacheEnabled(id)) {
480         Cache_inv(attrs, sizeof(TransportShm_Attrs), Cache_Type_ALL, TRUE);
481     }
483     /* set params field */
484     params.sharedAddr    = sharedAddr;
485     params.priority      = attrs->priority;
487     if (attrs->flag != TransportShm_UP) {
488         /* make sure transport is up */
489         *handlePtr = NULL;
490         status = MessageQ_E_NOTFOUND;
491     }
492     else {
493         /* Create the object */
494         *handlePtr = TransportShm_create(attrs->creatorProcId, &params, eb);
495         if (*handlePtr == NULL) {
496             status = MessageQ_E_FAIL;
497         }
498         else {
499             status = MessageQ_S_SUCCESS;
500         }
501     }
503     return (status);
506 /*
507  *  ======== TransportShm_sharedMemReq ========
508  */
509 SizeT TransportShm_sharedMemReq(const TransportShm_Params *params)
511     SizeT memReq, minAlign;
512     UInt16 regionId;
513     ListMP_Params listMPParams;
515     regionId = SharedRegion_getId(params->sharedAddr);
517     minAlign = Memory_getMaxDefaultTypeAlign();
518     if (SharedRegion_getCacheLineSize(regionId) > minAlign) {
519         minAlign = SharedRegion_getCacheLineSize(regionId);
520     }
522     /* for the Attrs structure */
523     memReq = _Ipc_roundup(sizeof(TransportShm_Attrs), minAlign);
525     /* for the second Attrs structure */
526     memReq += _Ipc_roundup(sizeof(TransportShm_Attrs), minAlign);
528     ListMP_Params_init(&listMPParams);
529     listMPParams.regionId = regionId;
531     /* for localListMP */
532     memReq += ListMP_sharedMemReq(&listMPParams);
534     /* for remoteListMP */
535     memReq += ListMP_sharedMemReq(&listMPParams);
537     return(memReq);
540 /*
541  *  ======== TransportShm_setErrFxn ========
542  */
543 Void TransportShm_setErrFxn(TransportShm_ErrFxn errFxn)
545     /* Ignore errFxn */