/** * @file rm.c * * @brief * This is the Resource Manager source. * * \par * ============================================================================ * @n (C) Copyright 2012, Texas Instruments, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution. * * Neither the name of Texas Instruments Incorporated nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * \par */ /* RM Types */ #include /* RM external includes */ #include #include #include #include /* RM internal includes */ #include /* RM OSAL layer */ #include /********************************************************************** ************************** Globals *********************************** **********************************************************************/ /* Place QMSS PDSP permissions array */ #pragma DATA_SECTION (rmQmssPdspFirmwarePerms, ".rm"); #pragma DATA_ALIGN (rmQmssPdspFirmwarePerms, 128) Rm_Perms rmQmssPdspFirmwarePerms[RM_ALIGN_PERMISSIONS_ARRAY(RM_QMSS_FIRMWARE_PDSPS, Rm_Perms)]; /** @brief Global Variable which describes the RM Version Information */ const char rmVersionStr[] = RM_VERSION_STR ":" __DATE__ ":" __TIME__; /********************************************************************** ********************** Internal Functions **************************** **********************************************************************/ /* At the very least the transaction ID needs to be provided to create a transaction */ Rm_Transaction *Rm_transactionQueueAdd(Rm_Inst *rmInst, uint32_t transactionId) { Rm_Transaction *transactionQueue = (Rm_Transaction *)rmInst->transactionQueue; Rm_Transaction *newTransaction = NULL; void *key; /* Lock access to the RM instance's transaction queue */ key = Rm_osalLocalCsEnter(); /* Get memory for a new transaction from local memory */ newTransaction = Rm_osalMalloc(sizeof(Rm_Transaction), false); /* Return if the memory allocated for the transaction entry is NULL */ if (newTransaction == NULL) { Rm_osalLocalCsExit(key); return(newTransaction); } /* Clear the transaction */ memset((void *)newTransaction, 0, sizeof(Rm_Transaction)); /* Populate transaction with the provided ID */ newTransaction->id = transactionId; /* New transaction's nextTransaction pointer will always be NULL */ newTransaction->nextTransaction = NULL; /* Check if there are any transactions in the transaction queue */ if (transactionQueue) { /* At least one transaction in the transaction queue. Add the new entry to the * end of the transaction queue */ while (transactionQueue->nextTransaction != NULL) { /* Traverse the list until arriving at the last transaction */ transactionQueue = transactionQueue->nextTransaction; } /* Add the new transaction to the end of the queue */ transactionQueue->nextTransaction = newTransaction; } else { /* The transaction queue does not currently exist. The new transaction is the * first transaction */ rmInst->transactionQueue = newTransaction; } Rm_osalLocalCsExit(key); return (newTransaction); } Rm_Transaction *Rm_transactionQueueFind(Rm_Inst *rmInst, uint32_t transactionId) { Rm_Transaction *transaction = (Rm_Transaction *)rmInst->transactionQueue; void *key; /* Make sure there is at least one transaction in the transaction queue */ if (transaction != NULL) { /* Find the transaction ID within the specified RM instance's transaction queue. * If the end of the transaction queue is reached without finding the transaction the * transaction pointer will be NULL */ while (transaction != NULL) { if (transaction->transactionId == transactionId) { /* Match: break out of loop and return the transaction */ break; } transaction = transaction->nextTransaction; } } return (transaction); } int32_t Rm_transactionQueueDelete(Rm_Inst *rmInst, uint32_t transactionId) { Rm_Transaction *transaction = (Rm_Transaction *) rmInst->transactionQueue; Rm_Transaction *prevTransaction = NULL; void *key; /* Lock access to the RM instance's transaction queue */ key = Rm_osalLocalCsEnter(); /* Make sure there is at least one entry in the transaction queue */ if (transaction == NULL) { Rm_osalLocalCsExit(key); return (RM_SERVICE_ERROR_NO_TRANSACTIONS_IN_QUEUE); } /* Find the transaction ID within the specified RM instance's transaction queue. */ while (transaction != NULL) { if (transaction->transactionId == transactionId) { /* Match: break out of loop and delete the transaction */ break; } prevTransaction = transaction; transaction = transaction->nextEntry; } /* Traversed entire queue but did not find transaction */ if (transaction == NULL) { Rm_osalLocalCsExit(key); return (RM_SERVICE_ERROR_SERVICE_TRANSACTION_DOES_NOT_EXIST); } else { /* Delete the transaction */ if ((prevTransaction == NULL) && transaction->nextTransaction) { /* Transaction to be deleted exists at start of transaction queue. Map second * transaction to be start of transaction queue as long as there are more than * one transactions */ rmInst->transactionQueue = transaction->nextTransaction; } else { /* Transaction to be deleted is in the middle or at end of the queue. Adjust * adjacent transaction pointers. This covers the case where the transaction to be * removed is at the end of the queue. */ prevTransaction->nextTransaction = transaction->nextTransaction; } /* Free the memory associated with the transaction. */ Rm_osalFree((void *)transaction, sizeof(Rm_Transaction), false); } Rm_osalLocalCsExit(key); return (RM_SERVICE_ACTION_OKAY); } /* Function used to send RM response transactions to lower level agents */ void Rm_transactionResponder (Rm_Inst *rmInst, Rm_Transaction *transaction, Rm_TransactionReceipt *receipt) { Rm_TransportNode *dstTransportNode = NULL; Rm_Packet *rmPkt = NULL; /* Find the transport for the RM instance that sent the request. */ dstTransportNode = Rm_transportNodeFindRemoteName(rmInst, transaction->sourceInstName); /* Create a RM packet using the service information */ switch (transaction->type) { case Rm_service_RESOURCE_ALLOCATE: case Rm_service_RESOURCE_BLOCK_ALLOCATE: case Rm_service_RESOURCE_ALLOCATE_BY_NAME: case Rm_service_RESOURCE_FREE: case Rm_service_RESOURCE_BLOCK_FREE: case Rm_service_RESOURCE_FREE_BY_NAME: rmPkt = Rm_transportCreateResourceResponsePkt(rmInst, dstTransportNode, transaction, receipt); break; case Rm_service_RESOURCE_MAP_TO_NAME: case Rm_service_RESOURCE_UNMAP_NAME: rmPkt = Rm_transportCreateNsResponsePkt(rmInst, dstTransportNode, transaction, receipt); break; default: /* Invalid service type. Flag the error and return */ receipt->serviceResult = RM_SERVICE_ERROR_INVALID_SERVICE_TYPE; break; } if (receipt->serviceResult <= RM_SERVICE_ERROR_BASE) { /* Delete the transaction and return immediately because an error occurred * allocating the packet */ Rm_transactionQueueDelete(rmInst, transaction->id); return; } /* Send the RM packet to the application transport */ if (rmInst->transport.rmSend((Rm_TransportHandle) dstTransportNode, rmPkt)) { /* Non-NULL value returned by transport send. An error occurred * in the transport while attempting to send the packet.*/ receipt->serviceResult = RM_SERVICE_ERROR_TRANPSPORT_SEND_ERROR; /* Clean up the packet */ if (rmInst->transport.rmFreePkt((Rm_TransportHandle) dstTransportNode, rmPkt)) { /* Non-NULL value returned by transport packet free. Flag the * error */ receipt->serviceResult = RM_SERVICE_ERROR_TRANSPORT_FREE_PKT_ERROR; } return; } /* Fill out the receipt information and delete the transaction */ receipt->serviceResult = transaction->details; Rm_transactionQueueDelete(rmInst, transaction->id); } /* Function used to forward RM transactions to higher level agents */ void Rm_transactionForwarder (Rm_Inst *rmInst, Rm_Transaction *transaction, Rm_TransactionReceipt *receipt) { Rm_TransportNode *dstTransportNode = NULL; Rm_Packet *rmPkt = NULL; /* Make sure the RM instance has a transport registered with a higher level agent */ if (rmInst->registeredWithDelegateOrServer == false) { receipt->serviceResult = RM_SERVICE_ERROR_NOT_REGISTERED_WITH_DEL_OR_SERVER; return; } /* Find the transport for the higher level agent. Check for a remote Client Delegate first. * If a connection does not exist check for a connection to a Server */ dstTransportNode = Rm_transportNodeFindRemoteInstType(rmInst, Rm_instType_CLIENT_DELEGATE); if (dstTransportNode == NULL) { dstTransportNode = Rm_transportNodeFindRemoteInstType(rmInst, Rm_instType_SERVER); } /* Create a RM packet using the service information */ switch (transaction->type) { case Rm_service_RESOURCE_ALLOCATE: case Rm_service_RESOURCE_BLOCK_ALLOCATE: case Rm_service_RESOURCE_ALLOCATE_BY_NAME: case Rm_service_RESOURCE_FREE: case Rm_service_RESOURCE_BLOCK_FREE: case Rm_service_RESOURCE_FREE_BY_NAME: rmPkt = Rm_transportCreateResourceReqPkt(rmInst, dstTransportNode, transaction, receipt); break; case Rm_service_RESOURCE_MAP_TO_NAME: case Rm_service_RESOURCE_UNMAP_NAME: rmPkt = Rm_transportCreateNsRequestPkt(rmInst, dstTransportNode, transaction, receipt); break; default: /* Invalid service type. Flag the error and return */ receipt->serviceResult = RM_SERVICE_ERROR_INVALID_SERVICE_TYPE; break; } if (receipt->serviceResult <= RM_SERVICE_ERROR_BASE) { /* Return immediately because an error occurred allocating the packet */ return; } /* Switch the queued transaction to the awaiting response state */ transaction->state = Rm_transactionState_AWAITING_RESPONSE; /* Send the RM packet to the application transport */ if (rmInst->transport.rmSend((Rm_TransportHandle) dstTransportNode, rmPkt)) { /* Non-NULL value returned by transport send. An error occurred * in the transport while attempting to send the packet.*/ receipt->serviceResult = RM_SERVICE_ERROR_TRANPSPORT_SEND_ERROR; /* Clean up the packet */ if (rmInst->transport.rmFreePkt((Rm_TransportHandle) dstTransportNode, rmPkt)) { /* Non-NULL value returned by transport packet free. Flag the * error */ receipt->serviceResult = RM_SERVICE_ERROR_TRANSPORT_FREE_PKT_ERROR; } return; } /* Inform requesting component that the service is being forwarded to a higher lever * RM agent for processing. The result of the service will be provided to the * component via the specified callback function */ receipt->serviceResult = RM_SERVICE_PROCESSING; receipt->serviceId = transaction->id; } void Rm_transactionProcessor (Rm_Inst *rmInst, Rm_Transaction *transaction, Rm_TransactionReceipt *receipt) { Rm_Transaction *queuedTransaction = NULL; if (rmInst->instType == Rm_instType_CLIENT) { /* Check if the new transaction's ID matches any transactions waiting for * responses. A transaction received as a response will have an ID that * matches the transaction that originated the request packet */ if (queuedTransaction = Rm_transactionQueueFind(rmInst, transaction->id)) { if (queuedTransaction->state == Rm_transactionState_AWAITING_RESPONSE) { /* Found a transaction awaiting a response. Pass both transactions * to the service responder for response processing */ Rm_serviceResponder(rmInst, transaction, queuedTransaction, receipt); } else { /* Request transaction was not in the awaiting response state. Flag the * error in the receipt and return */ receipt->serviceResult = RM_SERVICE_ERROR_INVALID_REQUEST_TRANSACTION_STATE_UPON_RESPONSE; } } else { /* This is a new transaction. Make sure the transaction is not a * response transaction sent to the wrong RM instance */ if ((transaction->state == Rm_transactionState_RESOURCE_APPROVED) || (transaction->state == Rm_transactionState_RESOURCE_DENIED)) { /* No matching request transaction. This transaction result was sent to the * wrong RM instance. Flag the error in the receipt and return */ receipt->serviceResult = RM_SERVICE_ERROR_INVALID_TRANSACTION_RECEIVED_ON_CLIENT; } else { /* All service requests on Clients are forwarded to the higher level RM agent * either a Client Delegate or Server, based on the RM system architecture */ Rm_transactionForwarder(rmInst, transaction, receipt); } } } else { /* Execute a command processor based on the command type */ switch (transaction->transCommand) { case Rm_command_ALLOCATE: case Rm_command_BLOCK_ALLOCATE: case Rm_command_ALLOCATE_NAMED: Rm_allocate(rmInst, transaction, receipt); break; case Rm_command_FREE: case Rm_command_BLOCK_FREE: case Rm_command_FREE_NAMED: Rm_free(rmInst, transaction, receipt); break; case Rm_command_MAP_NAME: Rm_nsAddObject(rmInst, transaction, receipt); break; case Rm_command_UNMAP_NAME: Rm_nsDeleteObject(rmInst, transaction, receipt); break; case Rm_command_RESOURCE_STATUS: Rm_getResourceStatus(rmInst, transaction, receipt); break; case Rm_command_RESOURCE_RESPONSE: break; case Rm_command_POLICY_REQUEST: break; case Rm_command_POLICY_RESPONSE: break; } } } /********************************************************************** ********************** Application visible APIs ********************** **********************************************************************/ Rm_Handle Rm_init(Rm_InitCfg *initCfg) { Rm_Inst *rmInst; /* Instance creation checks. Add one to strlen calculation for null character */ if ((strlen(initCfg->instName) + 1) > RM_INSTANCE_NAME_MAX_CHARS) { /* Failure: Instance name is too big */ return (NULL); } /* Get memory for RM instance from local memory */ rmInst = Rm_osalMalloc (sizeof(Rm_Inst), false); /* Populate instance based on input parameters */ strcpy (&rmInst->name[0], initCfg->instName); rmInst->instType = initCfg->instType; rmInst->instState = RM_state_IDLE; rmInst->registeredWithDelegateOrServer = false; rmInst->serviceCallback = NULL; /* The transport APIs must be provided */ if ((initCfg->rmAllocPktFuncPtr == NULL) || (initCfg->rmFreePktFuncPtr == NULL) || (initCfg->rmSendFuncPtr == NULL) || (initCfg->rmReceiveFuncPtr == NULL) || (initCfg->rmNumPktsReceivedFuncPtr == NULL)) { return (NULL); } /* Populate the instance transport callouts */ rmInst->transport.rmAllocPkt = initCfg->rmAllocPktFuncPtr; rmInst->transport.rmFreePkt = initCfg->rmFreePktFuncPtr; rmInst->transport.rmSend = initCfg->rmSendFuncPtr; rmInst->transport.rmReceive = initCfg->rmReceiveFuncPtr; rmInst->transport.rmNumPktsReceived = initCfg->rmNumPktsReceivedFuncPtr; /* Initialize the transport routing map linked list pointer to NULL. The linked list * nodes will be created when the application registers transports */ rmInst->routeMap = NULL; /* Initialize the transaction queue linked list pointer to NULL. The linked list * nodes will be created when the transactions are forwarded to higher level RM * agents. */ rmInst->transactionQueue= NULL; /* RM Server specific actions */ if (rmInst->instType == Rm_instType_SERVER) { /* parse DTB, etc */ } /* Instance startup policies are only used for Servers and Client Delegates */ if (rmInst->instType != Rm_instType_CLIENT) { rmInst->instPolicy = initCfg->startupPolicy; /* Store policy via policy APIs ... */ } /* Return the RM Handle */ return ((Rm_Handle) rmInst); } uint32_t Rm_getVersion (void) { return RM_VERSION_ID; } const char* Rm_getVersionStr (void) { return rmVersionStr; } /** @} */