Fix PRSDK-442
[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-2016, 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;
97     void           *mtKey;
99     if (serviceRequest->type >= Rm_service_LAST) {
100         serviceResponse->serviceState = RM_ERROR_INVALID_SERVICE_TYPE;
101         return;
102     }
104     if (serviceRequest->resourceName) {
105         if ((strlen(serviceRequest->resourceName) + 1) > RM_NAME_MAX_CHARS) {
106             serviceResponse->serviceState = RM_ERROR_RESOURCE_NAME_TOO_LONG;
107             return;
108         }
109     }
111     if (serviceRequest->resourceNsName) {
112         if ((strlen(serviceRequest->resourceNsName) + 1) > RM_NAME_MAX_CHARS) {
113             serviceResponse->serviceState = RM_ERROR_NAMESERVER_NAME_TOO_LONG;
114             return;
115         }
116     }
117     
118     if (rmInst->isLocked) {
119         serviceResponse->serviceState = RM_SERVICE_DENIED_RM_INSTANCE_LOCKED;
120         return;
121     }
123     RM_SS_INST_INV_ENTER_CS(rmInst, key);
124     RM_SC_INST_INV_ENTER_CS(rmInst, key);
126     if (rmInst->mtSemObj) {
127         mtKey = Rm_osalMtCsEnter(rmInst->mtSemObj);
128     }
130     /* Copy location of instance name to local variable in case Shared Client
131      * needs to transfer control to a shared server */
132     instanceName = rmInst->instName;
133     if (rmInst->instType == Rm_instType_SHARED_CLIENT) {
134         /* Transfer control to shared server instance */
135         rmInst = rmInst->u.sharedClient.sharedServerHandle;
136     }
138     if ((transaction = rmTransactionQueueAdd(rmInst))) {
139         transaction->type = serviceRequest->type;
140         rm_strncpy(transaction->serviceSrcInstName, instanceName,
141                    RM_NAME_MAX_CHARS);
142         transaction->u.callback.serviceCallback = serviceRequest->callback.serviceCallback;
143         transaction->state = RM_SERVICE_PROCESSING;
144         if (serviceRequest->resourceName) {
145             rm_strncpy(transaction->resourceInfo.name,
146                        serviceRequest->resourceName, RM_NAME_MAX_CHARS);
147         }
148         transaction->resourceInfo.base = serviceRequest->resourceBase;
149         transaction->resourceInfo.length = serviceRequest->resourceLength;
150         transaction->resourceInfo.alignment = serviceRequest->resourceAlignment;
151         transaction->resourceInfo.ownerCount = RM_RESOURCE_NUM_OWNERS_INVALID;
152         transaction->resourceInfo.instAllocCount = RM_INST_ALLOC_COUNT_INVALID;
153         if (serviceRequest->resourceNsName) {
154             rm_strncpy(transaction->resourceInfo.nameServerName,
155                        serviceRequest->resourceNsName, RM_NAME_MAX_CHARS);
156         }
158         /* Process received transaction */
159         rmProcessRouter(rmInst, transaction);
161         memset((void *)serviceResponse, 0, sizeof(*serviceResponse));
163         if ((rmInst->instType == Rm_instType_SHARED_SERVER) && 
164             (transaction->state == RM_SERVICE_PROCESSING)) {
165             /* Shared Server should always return a fully processed
166              * transaction */
167             serviceResponse->serviceState = RM_ERROR_SHARED_INSTANCE_UNFINISHED_REQ;
168             rmTransactionQueueDelete(rmInst, transaction->localId);
169         } else {
170             if ((transaction->state == RM_SERVICE_PROCESSING) &&
171                 (transaction->u.callback.serviceCallback == NULL)) {
172                 /* Block until response is received.  Response will be
173                  * received in transaction. */
174                 Rm_osalTaskBlock(rmInst->blockHandle);
175             }
177             serviceResponse->rmHandle = rmHandle;
178             serviceResponse->serviceState = transaction->state;
179             /* Owner and instance allocation count will only be set within RM
180              * under certain circumstances. */
181             serviceResponse->resourceNumOwners = transaction->resourceInfo.ownerCount;
182             serviceResponse->instAllocCount = transaction->resourceInfo.instAllocCount;
183             if ((serviceResponse->serviceState == RM_SERVICE_PROCESSING) ||
184                 (serviceResponse->serviceState == RM_SERVICE_APPROVED_STATIC) ||
185                 (serviceResponse->serviceState ==
186                  RM_SERVICE_PENDING_SERVER_RESPONSE)) {
187                 /* Service still being processed.  Static requests will have
188                  * their validation responses sent once all transports have
189                  * been established.  Provide transaction ID back to component
190                  * so it can sort service responses received via callback
191                  * function */
192                 serviceResponse->serviceId = transaction->localId;
193             }
195             if ((serviceResponse->serviceState == RM_SERVICE_APPROVED) ||
196                 (serviceResponse->serviceState == RM_SERVICE_APPROVED_STATIC)) {
197                 rm_strncpy(serviceResponse->resourceName,
198                            transaction->resourceInfo.name, RM_NAME_MAX_CHARS);
199                 serviceResponse->resourceBase = transaction->resourceInfo.base;
200                 serviceResponse->resourceLength = transaction->resourceInfo.length;
201             }
203             /* Transactions still processing not deleted from queue including
204              * static transactions which will be verified once all transports
205              * are up */
206             if ((serviceResponse->serviceState != RM_SERVICE_PROCESSING) &&
207                 (serviceResponse->serviceState != RM_SERVICE_APPROVED_STATIC) &&
208                 (serviceResponse->serviceState !=
209                  RM_SERVICE_PENDING_SERVER_RESPONSE)) {
210                 rmTransactionQueueDelete(rmInst, transaction->localId);
211             }
212         }
213     } else {
214         serviceResponse->serviceState = RM_ERROR_SERVICE_TRANS_NOT_CREATED;
215     }
217     /* Free sem object using originating instance in case the Shared Client to
218      * Shared Server instance switch took place */
219     if (((Rm_Inst *)rmHandle)->mtSemObj) {
220         Rm_osalMtCsExit(((Rm_Inst *)rmHandle)->mtSemObj, mtKey);
221     }
222     /* Shared Client switches to Shared Server instance so only need
223      * SS_EXIT_CS macro */
224     RM_SS_INST_WB_EXIT_CS(rmInst, key);
225     return;
228 /* FUNCTION PURPOSE: Opens the RM instance service handle
229  ***********************************************************************
230  * DESCRIPTION: Returns the service handle for an RM instance.  Only
231  *              one service handle is opened per instance.
232  */
233 Rm_ServiceHandle *Rm_serviceOpenHandle(Rm_Handle rmHandle, int32_t *result)
235     Rm_Inst          *rmInst = (Rm_Inst *)rmHandle;
236     Rm_ServiceHandle *serviceHandle = NULL;
237     void             *key;
238     void             *mtKey;
239     
240     RM_SS_INST_INV_ENTER_CS(rmInst, key);
241     if (rmInst->mtSemObj) {
242         mtKey = Rm_osalMtCsEnter(rmInst->mtSemObj);
243     }
245     *result = RM_OK;
247     serviceHandle = rmInst->serviceHandle;
248     if (serviceHandle == NULL) {
249         serviceHandle = Rm_osalMalloc(sizeof(*serviceHandle));
250         if (serviceHandle) {
251             serviceHandle->rmHandle = rmHandle;
252             serviceHandle->Rm_serviceHandler = Rm_serviceHandler;
253             RM_SS_OBJ_WB(rmInst, serviceHandle, Rm_ServiceHandle);
254             rmInst->serviceHandle = serviceHandle;
255         } else {
256             *result = RM_ERROR_SERVICE_HANDLE_MEM_ALLOC_FAILED;
257         }
258     }
260     if (rmInst->mtSemObj) {
261         Rm_osalMtCsExit(rmInst->mtSemObj, mtKey);
262     }
263     RM_SS_INST_WB_EXIT_CS(rmInst, key);
264     return (serviceHandle);
267 /* FUNCTION PURPOSE: Closes the RM instance service handle
268  ***********************************************************************
269  * DESCRIPTION: Closes the service handle for an RM instance.
270  */
271 int32_t Rm_serviceCloseHandle(Rm_ServiceHandle *rmServiceHandle)
273     Rm_Inst *rmInst = (Rm_Inst *)rmServiceHandle->rmHandle;
274     int32_t  retVal = RM_OK;
275     void    *key;
276     void    *mtKey;
278     RM_SS_INST_INV_ENTER_CS(rmInst, key);
279     if (rmInst->mtSemObj) {
280         mtKey = Rm_osalMtCsEnter(rmInst->mtSemObj);
281     }
283     if (rmInst->serviceHandle) {
284         Rm_osalFree((void *)rmServiceHandle, sizeof(*rmServiceHandle));
285         rmInst->serviceHandle = NULL;
286     } else {
287         retVal = RM_ERROR_SERVICE_HANDLE_ALREADY_CLOSED;
288     }
290     if (rmInst->mtSemObj) {
291         Rm_osalMtCsExit(rmInst->mtSemObj, mtKey);
292     }
293     RM_SS_INST_WB_EXIT_CS(rmInst, key);
294     return(retVal);