ipc: Remove compiler warnings
[ipc/ipcdev.git] / packages / ti / sdo / ipc / ListMP.c
index d0b9e7b0432f5ea0b543fc12f40146bf0683d046..d1a9bb44c3e955a6190799ee9511dd0d3817e1fc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012-2013, Texas Instruments Incorporated
+ * Copyright (c) 2012-2018 Texas Instruments Incorporated - http://www.ti.com
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  *                       Common Header Functions
  *************************************************************************
  */
+/*
+ *  ======== ListMP_barrier ========
+ *    Function ensures all the writeback to memory is complete
+ */
+static inline void ListMP_barrier(void)
+{
+#if defined(xdc_target__isaCompatible_64P)
+    /* Use _mfence to make sure memory transactions are complete */
+    _mfence();
+    /* Second mfence needed due to errata: See C6670 Advisory 32 */
+    _mfence();
+#endif
+}
 
 /*
  *  ======== ListMP_Params_init ========
@@ -159,9 +172,7 @@ Int ListMP_open(String name, ListMP_Handle *handlePtr)
 
     /* Search NameServer */
     status = NameServer_getUInt32((NameServer_Handle)ListMP_module->nameServer,
-                                  name,
-                                  &sharedShmBase,
-                                  ti_sdo_utils_MultiProc_procIdList);
+            name, &sharedShmBase, MultiProc_getClusterProcList());
 
     if (status < 0) {
         /* Name was not found */
@@ -170,6 +181,10 @@ Int ListMP_open(String name, ListMP_Handle *handlePtr)
     }
 
     sharedAddr = SharedRegion_getPtr(sharedShmBase);
+    if ( sharedAddr == NULL ) {
+        *handlePtr = NULL;
+        return (ListMP_E_NOTFOUND);
+    }
 
     status = ListMP_openByAddr(sharedAddr, handlePtr);
 
@@ -259,6 +274,7 @@ SizeT ListMP_sharedMemReq(const ListMP_Params *params)
 Bool ListMP_empty(ListMP_Handle handle)
 {
     ti_sdo_ipc_ListMP_Object *obj = (ti_sdo_ipc_ListMP_Object *)handle;
+    ti_sdo_ipc_ListMP_Attrs *attrs = obj->attrs;
     Bool flag = FALSE;
     IArg key;
     SharedRegion_SRPtr sharedHead;
@@ -266,25 +282,30 @@ Bool ListMP_empty(ListMP_Handle handle)
     /* prevent another thread or processor from modifying the ListMP */
     key = GateMP_enter((GateMP_Handle)obj->gate);
 
+#if defined(xdc_target__isaCompatible_v7A) || defined(xdc_target__isaCompatible_v8A)
+    /* ARM speculative execution might have pulled attrs into cache */
+    if (obj->cacheEnabled) {
+        Cache_inv(attrs, sizeof(ti_sdo_ipc_ListMP_Attrs), Cache_Type_ALL, TRUE);
+    }
+#endif
 
     if (ti_sdo_ipc_SharedRegion_translate == FALSE) {
         /* get the SRPtr for the head */
-        sharedHead = (SharedRegion_SRPtr)&(obj->attrs->head);
+        sharedHead = (SharedRegion_SRPtr)&(attrs->head);
     }
     else {
         /* get the SRPtr for the head */
-        sharedHead = SharedRegion_getSRPtr(&(obj->attrs->head), obj->regionId);
+        sharedHead = SharedRegion_getSRPtr(&(attrs->head), obj->regionId);
     }
 
     /* if 'next' is ourself, then the ListMP must be empty */
-    if (obj->attrs->head.next == sharedHead) {
+    if (attrs->head.next == sharedHead) {
         flag = TRUE;
     }
 
     if (obj->cacheEnabled) {
         /* invalidate the head to make sure we are not getting stale data */
-        Cache_inv(&(obj->attrs->head), sizeof(ListMP_Elem), Cache_Type_ALL,
-                  TRUE);
+        Cache_inv(&(attrs->head), sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
 
     /* leave the gate */
@@ -309,6 +330,7 @@ GateMP_Handle ListMP_getGate(ListMP_Handle handle)
 Ptr ListMP_getHead(ListMP_Handle handle)
 {
     ti_sdo_ipc_ListMP_Object *obj = (ti_sdo_ipc_ListMP_Object *)handle;
+    ti_sdo_ipc_ListMP_Attrs *attrs = obj->attrs;
     ListMP_Elem *elem;
     ListMP_Elem *localHeadNext;
     ListMP_Elem *localNext;
@@ -318,18 +340,25 @@ Ptr ListMP_getHead(ListMP_Handle handle)
     /* prevent another thread or processor from modifying the ListMP */
     key = GateMP_enter((GateMP_Handle)obj->gate);
 
+#if defined(xdc_target__isaCompatible_v7A) || defined(xdc_target__isaCompatible_v8A)
+    /* ARM speculative execution might have pulled attrs into cache */
+    if (obj->cacheEnabled) {
+        Cache_inv(attrs, sizeof(ti_sdo_ipc_ListMP_Attrs), Cache_Type_ALL, TRUE);
+    }
+#endif
+
     if (ti_sdo_ipc_SharedRegion_translate == FALSE) {
-        localHeadNext = (ListMP_Elem *)obj->attrs->head.next;
+        localHeadNext = (ListMP_Elem *)attrs->head.next;
     }
     else {
-        localHeadNext = SharedRegion_getPtr(obj->attrs->head.next);
+        localHeadNext = SharedRegion_getPtr(attrs->head.next);
     }
 
     /* Assert that pointer is not NULL */
     Assert_isTrue(localHeadNext != NULL, ti_sdo_ipc_Ipc_A_nullPointer);
 
     /* See if the ListMP was empty */
-    if (localHeadNext == (ListMP_Elem *)(&(obj->attrs->head))) {
+    if (localHeadNext == (ListMP_Elem *)(&(attrs->head))) {
         /* Empty, return NULL */
         elem = NULL;
     }
@@ -358,7 +387,7 @@ Ptr ListMP_getHead(ListMP_Handle handle)
         }
 
         /* Fix the head of the list next pointer */
-        obj->attrs->head.next = elem->next;
+        attrs->head.next = elem->next;
 
         /* Fix the prev pointer of the new first elem on the list */
         localNext->prev = localHeadNext->prev;
@@ -368,9 +397,10 @@ Ptr ListMP_getHead(ListMP_Handle handle)
     }
 
     if (obj->cacheEnabled) {
-        Cache_wbInv(&(obj->attrs->head), sizeof(ListMP_Elem),
-                    Cache_Type_ALL, TRUE);
+        Cache_wbInv(&(attrs->head), sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
+    else
+        ListMP_barrier();
 
     GateMP_leave((GateMP_Handle)obj->gate, key);
 
@@ -383,6 +413,7 @@ Ptr ListMP_getHead(ListMP_Handle handle)
 Ptr ListMP_getTail(ListMP_Handle handle)
 {
     ti_sdo_ipc_ListMP_Object *obj = (ti_sdo_ipc_ListMP_Object *)handle;
+    ti_sdo_ipc_ListMP_Attrs *attrs = obj->attrs;
     ListMP_Elem *elem;
     ListMP_Elem *localHeadPrev;
     ListMP_Elem *localPrev;
@@ -392,18 +423,25 @@ Ptr ListMP_getTail(ListMP_Handle handle)
     /* prevent another thread or processor from modifying the ListMP */
     key = GateMP_enter((GateMP_Handle)obj->gate);
 
+#if defined(xdc_target__isaCompatible_v7A) || defined(xdc_target__isaCompatible_v8A)
+    /* ARM speculative execution might have pulled attrs into cache */
+    if (obj->cacheEnabled) {
+        Cache_inv(attrs, sizeof(ti_sdo_ipc_ListMP_Attrs), Cache_Type_ALL, TRUE);
+    }
+#endif
+
     if (ti_sdo_ipc_SharedRegion_translate == FALSE) {
-        localHeadPrev = (ListMP_Elem *)obj->attrs->head.prev;
+        localHeadPrev = (ListMP_Elem *)attrs->head.prev;
     }
     else {
-        localHeadPrev = SharedRegion_getPtr(obj->attrs->head.prev);
+        localHeadPrev = SharedRegion_getPtr(attrs->head.prev);
     }
 
     /* Assert that pointer is not NULL */
     Assert_isTrue(localHeadPrev != NULL, ti_sdo_ipc_Ipc_A_nullPointer);
 
     /* See if the ListMP was empty */
-    if (localHeadPrev == (ListMP_Elem *)(&(obj->attrs->head))) {
+    if (localHeadPrev == (ListMP_Elem *)(&(attrs->head))) {
         /* Empty, return NULL */
         elem = NULL;
     }
@@ -432,7 +470,7 @@ Ptr ListMP_getTail(ListMP_Handle handle)
         }
 
         /* Fix the head of the list prev pointer */
-        obj->attrs->head.prev = elem->prev;
+        attrs->head.prev = elem->prev;
 
         /* Fix the next pointer of the new last elem on the list */
         localPrev->next = localHeadPrev->next;
@@ -442,9 +480,10 @@ Ptr ListMP_getTail(ListMP_Handle handle)
     }
 
     if (obj->cacheEnabled) {
-        Cache_wbInv(&(obj->attrs->head), sizeof(ListMP_Elem),
-                    Cache_Type_ALL, TRUE);
+        Cache_wbInv(&(attrs->head), sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
+    else
+        ListMP_barrier();
 
     GateMP_leave((GateMP_Handle)obj->gate, key);
 
@@ -471,7 +510,6 @@ Int ListMP_insert(ListMP_Handle handle, ListMP_Elem *newElem,
     if (ti_sdo_ipc_SharedRegion_translate == FALSE) {
         sharedNewElem = (SharedRegion_SRPtr)newElem;
         sharedCurElem = (SharedRegion_SRPtr)curElem;
-        localPrevElem = (ListMP_Elem *)(curElem->prev);
     }
     else {
         /* get SRPtr for newElem */
@@ -519,6 +557,8 @@ Int ListMP_insert(ListMP_Handle handle, ListMP_Elem *newElem,
         /* writeback invalidate new elem structure  */
         Cache_wbInv(newElem, sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
+    else
+        ListMP_barrier();
 
     GateMP_leave((GateMP_Handle)obj->gate, key);
 
@@ -539,7 +579,6 @@ Ptr ListMP_next(ListMP_Handle handle, ListMP_Elem *elem)
         /* Keep track of whether an extra Cache_inv is needed */
         elemIsCached = obj->cacheEnabled;
         elem = (ListMP_Elem *)&(obj->attrs->head);
-
     }
     else {
         elemIsCached = SharedRegion_isCacheEnabled(SharedRegion_getId(elem));
@@ -556,7 +595,7 @@ Ptr ListMP_next(ListMP_Handle handle, ListMP_Elem *elem)
     }
 
     if (elemIsCached) {
-        /* Invalidate because elem pulled into cache && elem != head. */
+        /* invalidate because elem pulled into cache */
         Cache_inv(elem, sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
 
@@ -592,7 +631,7 @@ Ptr ListMP_prev(ListMP_Handle handle, ListMP_Elem *elem)
     }
 
     if (elemIsCached) {
-        /* Invalidate because elem pulled into cache && elem != head. */
+        /* invalidate because elem pulled into cache */
         Cache_inv(elem, sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
 
@@ -605,6 +644,7 @@ Ptr ListMP_prev(ListMP_Handle handle, ListMP_Elem *elem)
 Int ListMP_putHead(ListMP_Handle handle, ListMP_Elem *elem)
 {
     ti_sdo_ipc_ListMP_Object *obj = (ti_sdo_ipc_ListMP_Object *)handle;
+    ti_sdo_ipc_ListMP_Attrs *attrs = obj->attrs;
     UInt key;
     UInt16 id;
     ListMP_Elem *localNextElem;
@@ -615,16 +655,23 @@ Int ListMP_putHead(ListMP_Handle handle, ListMP_Elem *elem)
     /* prevent another thread or processor from modifying the ListMP */
     key = GateMP_enter((GateMP_Handle)obj->gate);
 
+#if defined(xdc_target__isaCompatible_v7A) || defined(xdc_target__isaCompatible_v8A)
+    /* ARM speculative execution might have pulled attrs into cache */
+    if (obj->cacheEnabled) {
+        Cache_inv(attrs, sizeof(ti_sdo_ipc_ListMP_Attrs), Cache_Type_ALL, TRUE);
+    }
+#endif
+
     id = SharedRegion_getId(elem);
     if (ti_sdo_ipc_SharedRegion_translate == FALSE) {
         sharedElem = (SharedRegion_SRPtr)elem;
-        sharedHead = (SharedRegion_SRPtr)&(obj->attrs->head);
-        localNextElem = (ListMP_Elem *)obj->attrs->head.next;
+        sharedHead = (SharedRegion_SRPtr)&(attrs->head);
+        localNextElem = (ListMP_Elem *)attrs->head.next;
     }
     else {
         sharedElem = SharedRegion_getSRPtr(elem, id);
-        sharedHead = SharedRegion_getSRPtr(&(obj->attrs->head), obj->regionId);
-        localNextElem = SharedRegion_getPtr(obj->attrs->head.next);
+        sharedHead = SharedRegion_getSRPtr(&(attrs->head), obj->regionId);
+        localNextElem = SharedRegion_getPtr(attrs->head.next);
     }
 
     /* Assert that pointer is not NULL */
@@ -637,24 +684,25 @@ Int ListMP_putHead(ListMP_Handle handle, ListMP_Elem *elem)
     }
 
     /* add the new elem into the list */
-    elem->next = obj->attrs->head.next;
+    elem->next = attrs->head.next;
     elem->prev = sharedHead;
     localNextElem->prev = sharedElem;
-    obj->attrs->head.next = sharedElem;
+    attrs->head.next = sharedElem;
 
     if (localNextElemIsCached) {
         /* Write-back because localNextElem->prev changed */
         Cache_wbInv(localNextElem, sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
     if (obj->cacheEnabled) {
-        /* Write-back because obj->attrs->head.next changed */
-        Cache_wbInv(&(obj->attrs->head), sizeof(ListMP_Elem), Cache_Type_ALL,
-                    TRUE);
+        /* Write-back because attrs->head.next changed */
+        Cache_wbInv(&(attrs->head), sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
     if (SharedRegion_isCacheEnabled(id)) {
         /* Write-back because elem->next & elem->prev changed */
         Cache_wbInv(elem, sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
+    else
+        ListMP_barrier();
 
     GateMP_leave((GateMP_Handle)obj->gate, key);
 
@@ -667,6 +715,7 @@ Int ListMP_putHead(ListMP_Handle handle, ListMP_Elem *elem)
 Int ListMP_putTail(ListMP_Handle handle, ListMP_Elem *elem)
 {
     ti_sdo_ipc_ListMP_Object *obj = (ti_sdo_ipc_ListMP_Object *)handle;
+    ti_sdo_ipc_ListMP_Attrs *attrs = obj->attrs;
     UInt key;
     UInt16  id;
     ListMP_Elem *localPrevElem;
@@ -677,16 +726,23 @@ Int ListMP_putTail(ListMP_Handle handle, ListMP_Elem *elem)
     /* prevent another thread or processor from modifying the ListMP */
     key = GateMP_enter((GateMP_Handle)obj->gate);
 
+#if defined(xdc_target__isaCompatible_v7A) || defined(xdc_target__isaCompatible_v8A)
+    /* ARM speculative execution might have pulled attrs into cache */
+    if (obj->cacheEnabled) {
+        Cache_inv(attrs, sizeof(ti_sdo_ipc_ListMP_Attrs), Cache_Type_ALL, TRUE);
+    }
+#endif
+
     id = SharedRegion_getId(elem);
     if (ti_sdo_ipc_SharedRegion_translate == FALSE) {
         sharedElem = (SharedRegion_SRPtr)elem;
-        sharedHead = (SharedRegion_SRPtr)&(obj->attrs->head);
-        localPrevElem = (ListMP_Elem *)obj->attrs->head.prev;
+        sharedHead = (SharedRegion_SRPtr)&(attrs->head);
+        localPrevElem = (ListMP_Elem *)attrs->head.prev;
     }
     else {
         sharedElem = SharedRegion_getSRPtr(elem, id);
-        sharedHead = SharedRegion_getSRPtr(&(obj->attrs->head), obj->regionId);
-        localPrevElem = SharedRegion_getPtr(obj->attrs->head.prev);
+        sharedHead = SharedRegion_getSRPtr(&(attrs->head), obj->regionId);
+        localPrevElem = SharedRegion_getPtr(attrs->head.prev);
     }
 
     /* Assert that pointer is not NULL */
@@ -700,23 +756,24 @@ Int ListMP_putTail(ListMP_Handle handle, ListMP_Elem *elem)
 
     /* add the new elem into the list */
     elem->next = sharedHead;
-    elem->prev = obj->attrs->head.prev;
+    elem->prev = attrs->head.prev;
     localPrevElem->next = sharedElem;
-    obj->attrs->head.prev = sharedElem;
+    attrs->head.prev = sharedElem;
 
     if (localPrevElemIsCached) {
         /* Write-back because localPrevElem->next changed */
         Cache_wbInv(localPrevElem, sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
     if (obj->cacheEnabled) {
-        /* Write-back because obj->attrs->head.prev changed */
-        Cache_wbInv(&(obj->attrs->head), sizeof(ListMP_Elem), Cache_Type_ALL,
-                TRUE);
+        /* Write-back because attrs->head.prev changed */
+        Cache_wbInv(&(attrs->head), sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
     if (SharedRegion_isCacheEnabled(id)) {
         /* Write-back because elem->next & elem->prev changed */
         Cache_wbInv(elem, sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
+    else
+        ListMP_barrier();
 
     GateMP_leave((GateMP_Handle)obj->gate, key);
 
@@ -767,6 +824,8 @@ Int ListMP_remove(ListMP_Handle handle, ListMP_Elem *elem)
     if (localNextElemIsCached) {
         Cache_wbInv(localNextElem, sizeof(ListMP_Elem), Cache_Type_ALL, TRUE);
     }
+    else
+        ListMP_barrier();
 
     GateMP_leave((GateMP_Handle)obj->gate, key);
 
@@ -800,9 +859,19 @@ Int ti_sdo_ipc_ListMP_Instance_init(ti_sdo_ipc_ListMP_Object *obj,
         obj->cacheEnabled = SharedRegion_isCacheEnabled(obj->regionId);
         obj->cacheLineSize = SharedRegion_getCacheLineSize(obj->regionId);
 
+#if defined(xdc_target__isaCompatible_v7A) || defined(xdc_target__isaCompatible_v8A)
+        /* ARM speculative execution might have pulled attrs into cache */
+        if (obj->cacheEnabled) {
+            Cache_inv(obj->attrs, sizeof(ti_sdo_ipc_ListMP_Attrs), Cache_Type_ALL,
+                TRUE);
+        }
+#endif
         /* get the local address of the SRPtr */
         localAddr = SharedRegion_getPtr(obj->attrs->gateMPAddr);
 
+        /* Do NULL check on localAddr */
+        Assert_isTrue(localAddr != NULL, ti_sdo_ipc_Ipc_A_internal);
+
         status = GateMP_openByAddr(localAddr, (GateMP_Handle *)&(obj->gate));
         if (status != GateMP_S_SUCCESS) {
             Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
@@ -858,7 +927,7 @@ Int ti_sdo_ipc_ListMP_Instance_init(ti_sdo_ipc_ListMP_Object *obj,
 
         /* Assert that sharedAddr is cache aligned */
         Assert_isTrue((obj->cacheLineSize == 0) ||
-                      ((UInt32)params->sharedAddr % obj->cacheLineSize == 0),
+                      ((uintptr_t)params->sharedAddr % obj->cacheLineSize == 0),
                       ti_sdo_ipc_Ipc_A_addrNotCacheAligned);
     }