Prevent %-by-0 in Assert_isTrue() expressions
[ipc/ipcdev.git] / packages / ti / sdo / ipc / transports / TransportShm.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  *  ======== 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(SharedRegion_getCacheLineSize(obj->regionId) == 0 ||
182                 ((UInt32)params->sharedAddr %
183                 SharedRegion_getCacheLineSize(obj->regionId) == 0),
184                 ti_sdo_ipc_Ipc_A_addrNotCacheAligned);
186         /* set object's cacheEnabled, type, self */
187         obj->cacheEnabled = SharedRegion_isCacheEnabled(obj->regionId);
188         obj->objType = ti_sdo_ipc_Ipc_ObjType_CREATEDYNAMIC;
189         obj->self = (TransportShm_Attrs *)params->sharedAddr;
190     }
192     /* determine the minimum alignment to align to */
193     minAlign = Memory_getMaxDefaultTypeAlign();
194     if (SharedRegion_getCacheLineSize(obj->regionId) > minAlign) {
195         minAlign = SharedRegion_getCacheLineSize(obj->regionId);
196     }
198     /*
199      *  Carve up the shared memory.
200      *  If cache is enabled, these need to be on separate cache lines.
201      *  This is done with minAlign and _Ipc_roundup function.
202      */
203     obj->other = (TransportShm_Attrs *)((UInt32)(obj->self) +
204         (_Ipc_roundup(sizeof(TransportShm_Attrs), minAlign)));
206     ListMP_Params_init(&(listMPParams[0]));
207     listMPParams[0].gate = (GateMP_Handle)obj->gate;
208     listMPParams[0].sharedAddr = (UInt32 *)((UInt32)(obj->other) +
209         (_Ipc_roundup(sizeof(TransportShm_Attrs), minAlign)));
211     ListMP_Params_init(&listMPParams[1]);
212     listMPParams[1].gate = (GateMP_Handle)obj->gate;
213     listMPParams[1].sharedAddr = (UInt32 *)((UInt32)(listMPParams[0].sharedAddr)
214         + ListMP_sharedMemReq(&listMPParams[0]));
216     obj->priority      = params->priority;
217     obj->remoteProcId  = procId;
219     Swi_Params_init(&swiParams);
220     swiParams.arg0 = (UArg)obj;
221     Swi_construct(Swi_struct(swiHandle),
222                  (Swi_FuncPtr)TransportShm_swiFxn,
223                  &swiParams, eb);
225     if (params->openFlag == FALSE) {
226         obj->localList = (ti_sdo_ipc_ListMP_Handle)
227                 ListMP_create(&(listMPParams[localIndex]));
228         if (obj->localList == NULL) {
229             Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
230             return (2);
231         }
233         obj->remoteList = (ti_sdo_ipc_ListMP_Handle)
234                 ListMP_create(&(listMPParams[remoteIndex]));
235         if (obj->localList == NULL) {
236             Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
237             return (2);
238         }
239     }
240     else {
241         /* Open the local ListMP instance */
242         status = ListMP_openByAddr(listMPParams[localIndex].sharedAddr,
243                                    (ListMP_Handle *)&(obj->localList));
245         if (status < 0) {
246             Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
247             return (2);
248         }
250         /* Open the remote ListMP instance */
251         status = ListMP_openByAddr(listMPParams[remoteIndex].sharedAddr,
252                                    (ListMP_Handle *)&(obj->remoteList));
254         if (status < 0) {
255             Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
256             return (2);
257         }
258     }
260     /* register the event with Notify */
261     status = Notify_registerEventSingle(
262                  procId,    /* remoteProcId */
263                  0,         /* lineId */
264                  TransportShm_notifyEventId,
265                  (Notify_FnNotifyCbck)TransportShm_notifyFxn,
266                  (UArg)swiHandle);
267     if (status < 0) {
268         Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
269         return (3);
270     }
272     /* Register the transport with MessageQ */
273     flag = ti_sdo_ipc_MessageQ_registerTransport(
274             TransportShm_Handle_upCast(obj), procId, params->priority);
275     if (flag == FALSE) {
276         Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
277         return (4);
278     }
280     if (params->openFlag == FALSE) {
281         obj->self->creatorProcId = MultiProc_self();
282         obj->self->notifyEventId = TransportShm_notifyEventId;
283         obj->self->priority = obj->priority;
285         /* Store the GateMP sharedAddr in the Attrs */
286         obj->self->gateMPAddr = ti_sdo_ipc_GateMP_getSharedAddr(obj->gate);
287         obj->self->flag = TransportShm_UP;
289         if (obj->cacheEnabled) {
290             Cache_wbInv(obj->self, sizeof(TransportShm_Attrs),
291                      Cache_Type_ALL, TRUE);
292         }
293     }
294     else {
295         obj->other->flag = TransportShm_UP;
296         if (obj->cacheEnabled) {
297             Cache_wbInv(&(obj->other->flag), minAlign, Cache_Type_ALL, TRUE);
298         }
299     }
301     obj->status = TransportShm_UP;
303     return (0);
306 /*
307  *  ======== TransportShm_Instance_finalize ========
308  */
309 Void TransportShm_Instance_finalize(TransportShm_Object* obj, Int status)
311     Swi_Handle     swiHandle;
313     if (obj->objType == ti_sdo_ipc_Ipc_ObjType_CREATEDYNAMIC) {
314         /* clear the self flag */
315         obj->self->flag = 0;
317         if (obj->cacheEnabled) {
318             Cache_wbInv(&(obj->self->flag),
319                 SharedRegion_getCacheLineSize(obj->regionId), Cache_Type_ALL,
320                     TRUE);
321         }
323         if (obj->localList != NULL) {
324             ListMP_delete((ListMP_Handle *)&(obj->localList));
325         }
327         if (obj->remoteList != NULL) {
328             ListMP_delete((ListMP_Handle *)&(obj->remoteList));
329         }
330     }
331     else {
332         /* other flag was set by remote proc */
333         obj->other->flag = 0;
335         if (obj->cacheEnabled) {
336             Cache_wbInv(&(obj->other->flag),
337                 SharedRegion_getCacheLineSize(obj->regionId), Cache_Type_ALL,
338                     TRUE);
339         }
341         if (obj->gate != NULL) {
342             GateMP_close((GateMP_Handle *)&(obj->gate));
343         }
345         if (obj->localList != NULL) {
346             ListMP_close((ListMP_Handle *)&(obj->localList));
347         }
349         if (obj->remoteList != NULL) {
350             ListMP_close((ListMP_Handle *)&(obj->remoteList));
351         }
352     }
354     switch(status) {
355         case 0:
356             /* MessageQ_registerTransport succeeded */
357             ti_sdo_ipc_MessageQ_unregisterTransport(obj->remoteProcId, obj->priority);
358             /* OK to fall through */
359         case 1: /* GateMP open failed */
360         case 2: /* ListMP create/open failed */
361         case 3: /* Notify_registerEventSingle failed */
362         case 4: /* MessageQ_registerTransport failed */
363             Notify_unregisterEventSingle(
364                 obj->remoteProcId,
365                 0,
366                 TransportShm_notifyEventId);
367             break;
368     }
370     /* Destruct the swi */
371     swiHandle = TransportShm_Instance_State_swiObj(obj);
372     if (swiHandle != NULL) {
373         Swi_destruct(Swi_struct(swiHandle));
374     }
377 /*
378  *  ======== TransportShm_put ========
379  *  Assuming MessageQ_put is making sure that the arguments are ok
380  */
381 Bool TransportShm_put(TransportShm_Object *obj, Ptr msg)
383     Int32 status;
384     Bool retval = TRUE;
385     IArg key;
386     UInt16 id = SharedRegion_getId(msg);
388     /* This transport only deals with messages allocated from SR's */
389     Assert_isTrue(id != SharedRegion_INVALIDREGIONID,
390             ti_sdo_ipc_SharedRegion_A_regionInvalid);
392     /* writeback invalidate the message */
393     if (SharedRegion_isCacheEnabled(id)) {
394         Cache_wbInv(msg, ((MessageQ_Msg)(msg))->msgSize, Cache_Type_ALL,
395             TRUE);
396     }
398     /* make sure ListMP_put and sendEvent are done before remote executes */
399     key = GateMP_enter((GateMP_Handle)obj->gate);
401     /* Put the message on the remoteList */
402     ListMP_putTail((ListMP_Handle)obj->remoteList, (ListMP_Elem *)msg);
404     /* Notify the remote processor */
405     status = Notify_sendEvent(obj->remoteProcId, 0, TransportShm_notifyEventId,
406         0, FALSE);
408     /* check the status of the sendEvent */
409     if (status < 0) {
410         /* remove the message from the List and return 'FALSE' */
411         ListMP_remove((ListMP_Handle)obj->remoteList, (ListMP_Elem *)msg);
412         retval = FALSE;
413     }
415     /* leave the gate */
416     GateMP_leave((GateMP_Handle)obj->gate, key);
418     return (retval);
421 /*
422  *  ======== TransportShm_control ========
423  */
424 Bool TransportShm_control(TransportShm_Object *obj, UInt cmd, UArg cmdArg)
426     return (FALSE);
429 /*
430  *  ======== TransportShm_getStatus ========
431  */
432 Int TransportShm_getStatus(TransportShm_Object *obj)
434     return (obj->status);
437 /*
438  *************************************************************************
439  *                      Module functions
440  *************************************************************************
441  */
443 /*
444  *  ======== TransportShm_close ========
445  */
446 Void TransportShm_close(TransportShm_Handle *handle)
448     TransportShm_delete(handle);
451 /*
452  *  ======== TransportShm_openByAddr ========
453  */
454 Int TransportShm_openByAddr(Ptr sharedAddr,
455                       TransportShm_Handle *handlePtr,
456                       Error_Block *eb)
458     TransportShm_Params params;
459     TransportShm_Attrs *attrs;
460     Int status;
461     UInt16 id;
463     if (sharedAddr == NULL) {
464         return (MessageQ_E_FAIL);
465     }
467     TransportShm_Params_init(&params);
469     /* Tell Instance_init() that we're opening */
470     params.openFlag = TRUE;
472     attrs = (TransportShm_Attrs *)sharedAddr;
473     id = SharedRegion_getId(sharedAddr);
475     /* Assert that the region is valid */
476     Assert_isTrue(id != SharedRegion_INVALIDREGIONID,
477             ti_sdo_ipc_Ipc_A_addrNotInSharedRegion);
479     /* invalidate the attrs before using it */
480     if (SharedRegion_isCacheEnabled(id)) {
481         Cache_inv(attrs, sizeof(TransportShm_Attrs), Cache_Type_ALL, TRUE);
482     }
484     /* set params field */
485     params.sharedAddr    = sharedAddr;
486     params.priority      = attrs->priority;
488     if (attrs->flag != TransportShm_UP) {
489         /* make sure transport is up */
490         *handlePtr = NULL;
491         status = MessageQ_E_NOTFOUND;
492     }
493     else {
494         /* Create the object */
495         *handlePtr = TransportShm_create(attrs->creatorProcId, &params, eb);
496         if (*handlePtr == NULL) {
497             status = MessageQ_E_FAIL;
498         }
499         else {
500             status = MessageQ_S_SUCCESS;
501         }
502     }
504     return (status);
507 /*
508  *  ======== TransportShm_sharedMemReq ========
509  */
510 SizeT TransportShm_sharedMemReq(const TransportShm_Params *params)
512     SizeT memReq, minAlign;
513     UInt16 regionId;
514     ListMP_Params listMPParams;
516     regionId = SharedRegion_getId(params->sharedAddr);
518     minAlign = Memory_getMaxDefaultTypeAlign();
519     if (SharedRegion_getCacheLineSize(regionId) > minAlign) {
520         minAlign = SharedRegion_getCacheLineSize(regionId);
521     }
523     /* for the Attrs structure */
524     memReq = _Ipc_roundup(sizeof(TransportShm_Attrs), minAlign);
526     /* for the second Attrs structure */
527     memReq += _Ipc_roundup(sizeof(TransportShm_Attrs), minAlign);
529     ListMP_Params_init(&listMPParams);
530     listMPParams.regionId = regionId;
532     /* for localListMP */
533     memReq += ListMP_sharedMemReq(&listMPParams);
535     /* for remoteListMP */
536     memReq += ListMP_sharedMemReq(&listMPParams);
538     return(memReq);
541 /*
542  *  ======== TransportShm_setErrFxn ========
543  */
544 Void TransportShm_setErrFxn(TransportShm_ErrFxn errFxn)
546     /* Ignore errFxn */