Coverity bug fixes
[keystone-rtos/rm-lld.git] / src / rm_services.c
1 /**
2  *   @file  rm_services.c
3  *
4  *   @brief   
5  *      This is the Resource Manager services source.
6  *
7  *  \par
8  *  ============================================================================
9  *  @n   (C) Copyright 2012-2013, Texas Instruments, Inc.
10  * 
11  *  Redistribution and use in source and binary forms, with or without 
12  *  modification, are permitted provided that the following conditions 
13  *  are met:
14  *
15  *    Redistributions of source code must retain the above copyright 
16  *    notice, this list of conditions and the following disclaimer.
17  *
18  *    Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the 
20  *    documentation and/or other materials provided with the   
21  *    distribution.
22  *
23  *    Neither the name of Texas Instruments Incorporated nor the names of
24  *    its contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
28  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
29  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
31  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
32  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
33  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
36  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
37  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  *
39  *  \par
40 */
42 /* RM external API includes */
43 #include <ti/drv/rm/rm.h>
44 #include <ti/drv/rm/rm_services.h>
46 /* RM internal API includes */
47 #include <ti/drv/rm/include/rm_loc.h>
48 #include <ti/drv/rm/include/rm_internal.h>
50 /* RM OSAL layer */
51 #include <rm_osal.h>
53 /**********************************************************************
54  ********************** Internal Functions ****************************
55  **********************************************************************/
57 /* FUNCTION PURPOSE: Internal Callback to unblock RM instance
58  ***********************************************************************
59  * DESCRIPTION: Internal callback function executed when the result
60  *              of a service request has been received from a remote
61  *              instance.  The original service request did not specify
62  *              a callback function so the Rm_serviceHandler is blocked
63  *              waiting for the response.  This function unblocks the
64  *              Rm_serviceHandler to return the response to the 
65  *              application.
66  */
67 void rmServiceInternalCallback(Rm_Handle rmHandle)
68 {
69     Rm_Inst *rmInst = (Rm_Inst *)rmHandle;
71     /* Unblock so Rm_serviceHandler can provide response to application */
72     Rm_osalTaskUnblock(rmInst->blockHandle);
73 }
75 /**********************************************************************
76  ********************** Application visible APIs **********************
77  **********************************************************************/
79 /* FUNCTION PURPOSE: Handles application component service requests
80  ***********************************************************************
81  * DESCRIPTION: Receives service requests from application components
82  *              and routes them to the transaction processor.  If
83  *              the service can be handled immediately the response
84  *              will be provided in the service response.  If the
85  *              service requires a blocking operation the handler
86  *              will provide a service ID back to the application.
87  *              The response will be sent at a later time via the
88  *              application supplied callback function.
89  */
90 void Rm_serviceHandler (void *rmHandle, const Rm_ServiceReqInfo *serviceRequest,
91                         Rm_ServiceRespInfo *serviceResponse)
92 {
93     Rm_Inst        *rmInst = (Rm_Inst *)rmHandle;
94     char           *instanceName;
95     Rm_Transaction *transaction;
96     void           *key;     
98     RM_SS_INST_INV_ENTER_CS(key);
99     RM_SC_INST_INV_ENTER_CS(key);
101     if (rmInst->isLocked) {
102         serviceResponse->serviceState = RM_SERVICE_DENIED_RM_INSTANCE_LOCKED;
103         return;
104     }
106     if (serviceRequest->type >= Rm_service_LAST) {
107         serviceResponse->serviceState = RM_ERROR_INVALID_SERVICE_TYPE;
108         return;
109     }
111     /* Copy location of instance name to local variable in case Shared Client needs to
112      * transfer control to a shared server */
113     instanceName = rmInst->instName;
114     if (rmInst->instType == Rm_instType_SHARED_CLIENT) {
115         /* Transfer control to shared server instance */
116         rmInst = rmInst->u.sharedClient.sharedServerHandle;
117     }       
118     
119     transaction = rmTransactionQueueAdd(rmInst);
120     if (transaction) {
121         transaction->type = serviceRequest->type;
122         rm_strncpy(transaction->serviceSrcInstName, instanceName, RM_NAME_MAX_CHARS);
123         transaction->u.callback.serviceCallback = serviceRequest->callback.serviceCallback;
124         transaction->state = RM_SERVICE_PROCESSING;
125         if (serviceRequest->resourceName) {
126             rm_strncpy(transaction->resourceInfo.name, serviceRequest->resourceName, RM_NAME_MAX_CHARS);
127         }
128         transaction->resourceInfo.base = serviceRequest->resourceBase;
129         transaction->resourceInfo.length = serviceRequest->resourceLength;
130         transaction->resourceInfo.alignment = serviceRequest->resourceAlignment;
131         transaction->resourceInfo.ownerCount = RM_RESOURCE_NUM_OWNERS_INVALID;
132         transaction->resourceInfo.instAllocCount = RM_INST_ALLOC_COUNT_INVALID;
133         if (serviceRequest->resourceNsName) {
134             rm_strncpy(transaction->resourceInfo.nameServerName, serviceRequest->resourceNsName, RM_NAME_MAX_CHARS);
135         }
137         /* Process received transaction */
138         rmProcessRouter(rmInst, transaction);
139             
140         memset((void *)serviceResponse, 0, sizeof(*serviceResponse));
142         if ((rmInst->instType == Rm_instType_SHARED_SERVER) && 
143             (transaction->state == RM_SERVICE_PROCESSING)) {
144             /* Shared Server should always return a fully processed transaction */
145             serviceResponse->serviceState = RM_ERROR_SHARED_INSTANCE_UNFINISHED_REQ;
146             rmTransactionQueueDelete(rmInst, transaction->localId);
147         }
148         else {
149             if ((transaction->state == RM_SERVICE_PROCESSING) && 
150                 (transaction->u.callback.serviceCallback == NULL)) {
151                 /* Block until response is received.  Response will be received in transaction. */
152                 Rm_osalTaskBlock(rmInst->blockHandle);
153             }
155             serviceResponse->rmHandle = rmHandle;
156             serviceResponse->serviceState = transaction->state;
157             /* Owner and instance allocation count will only be set within RM under certain circumstances. */
158             serviceResponse->resourceNumOwners = transaction->resourceInfo.ownerCount;
159             serviceResponse->instAllocCount = transaction->resourceInfo.instAllocCount;
160             if ((serviceResponse->serviceState == RM_SERVICE_PROCESSING) ||
161                 (serviceResponse->serviceState == RM_SERVICE_APPROVED_STATIC) ||
162                 (serviceResponse->serviceState == RM_SERVICE_PENDING_SERVER_RESPONSE)) {
163                 /* Service still being processed.  Static requests will have their validation responses sent once
164                  * all transports have been established.  Provide transaction ID back to component so it can sort 
165                  * service responses received via callback function */
166                 serviceResponse->serviceId = transaction->localId;
167             }
169             if ((serviceResponse->serviceState == RM_SERVICE_APPROVED) ||
170                 (serviceResponse->serviceState == RM_SERVICE_APPROVED_STATIC)) {
171                 rm_strncpy(serviceResponse->resourceName, transaction->resourceInfo.name, RM_NAME_MAX_CHARS);
172                 serviceResponse->resourceBase = transaction->resourceInfo.base;
173                 serviceResponse->resourceLength = transaction->resourceInfo.length;
174             }
176             /* Transactions still processing not deleted from queue including static transactions which will be 
177              * verified once all transports are up */
178             if ((serviceResponse->serviceState != RM_SERVICE_PROCESSING) &&
179                 (serviceResponse->serviceState != RM_SERVICE_APPROVED_STATIC) &&
180                 (serviceResponse->serviceState != RM_SERVICE_PENDING_SERVER_RESPONSE)) {
181                 rmTransactionQueueDelete(rmInst, transaction->localId);
182             }
183         }
184     }
185     else {
186         serviceResponse->serviceState = RM_ERROR_SERVICE_TRANS_NOT_CREATED;    
187     }
188     RM_SS_INST_WB_EXIT_CS(key);
189     return;
192 /* FUNCTION PURPOSE: Opens the RM instance service handle
193  ***********************************************************************
194  * DESCRIPTION: Returns the service handle for an RM instance.  Only
195  *              one service handle is opened per instance.
196  */
197 Rm_ServiceHandle *Rm_serviceOpenHandle(Rm_Handle rmHandle, int32_t *result)
199     Rm_Inst          *rmInst = (Rm_Inst *)rmHandle;
200     Rm_ServiceHandle *serviceHandle = NULL;
201     void             *key;
202     
203     RM_SS_INST_INV_ENTER_CS(key);
205     *result = RM_OK;
207     serviceHandle = rmInst->serviceHandle;
208     if (serviceHandle == NULL) {
209         serviceHandle = Rm_osalMalloc(sizeof(*serviceHandle));
210         if (serviceHandle) {
211             serviceHandle->rmHandle = rmHandle;
212             serviceHandle->Rm_serviceHandler = Rm_serviceHandler;
213             RM_SS_OBJ_WB(serviceHandle, Rm_ServiceHandle);
214             rmInst->serviceHandle = serviceHandle;
215         }
216         else {
217             *result = RM_ERROR_SERVICE_HANDLE_MEM_ALLOC_FAILED;
218         }
219     }
220     RM_SS_INST_WB_EXIT_CS(key);
221     return (serviceHandle);
224 /* FUNCTION PURPOSE: Closes the RM instance service handle
225  ***********************************************************************
226  * DESCRIPTION: Closes the service handle for an RM instance.
227  */
228 int32_t Rm_serviceCloseHandle(Rm_ServiceHandle *rmServiceHandle)
230     Rm_Inst *rmInst = (Rm_Inst *)rmServiceHandle->rmHandle;
231     int32_t  retVal = RM_OK;
232     void    *key;
234     RM_SS_INST_INV_ENTER_CS(key);
236     if (rmInst->serviceHandle) {
237         Rm_osalFree((void *)rmServiceHandle, sizeof(*rmServiceHandle));
238         rmInst->serviceHandle = NULL;
239     }
240     else {
241         retVal = RM_ERROR_SERVICE_HANDLE_ALREADY_CLOSED;
242     }
243     RM_SS_INST_WB_EXIT_CS(key);
244     return(retVal);