/** * @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 #include #include #include /* RM OSAL layer */ #include /********************************************************************** ************************** Globals *********************************** **********************************************************************/ #if 0 /* 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)]; #endif /** @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) { Rm_Transaction *transactionQueue = (Rm_Transaction *)rmInst->transactionQueue; Rm_Transaction *newTransaction = NULL; void *key; /* Lock access to the RM instance's transaction queue */ key = Rm_osalMtCsEnter(); /* 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_osalMtCsExit(key); return(newTransaction); } /* Clear the transaction */ memset((void *)newTransaction, 0, sizeof(Rm_Transaction)); /* Create an ID for the new transaction. The ID will be used for two purposes: * 1) Matching responses from higher level RM agents to requests * 2) Provided to the component that requested the service so that it can match its * request with the response it receives via its callback function it provided */ newTransaction->localId = Rm_transactionGetSequenceNum(rmInst); /* 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_osalMtCsExit(key); return (newTransaction); } Rm_Transaction *Rm_transactionQueueFind(Rm_Inst *rmInst, uint32_t transactionId) { Rm_Transaction *transaction = (Rm_Transaction *)rmInst->transactionQueue; /* 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->localId == 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; int32_t retVal = RM_SERVICE_STATE_OKAY; void *key; /* Lock access to the RM instance's transaction queue */ key = Rm_osalMtCsEnter(); /* Find the transaction ID within the specified RM instance's transaction queue. */ while (transaction != NULL) { if (transaction->localId == transactionId) { /* Match: break out of loop and delete the transaction */ break; } prevTransaction = transaction; transaction = transaction->nextTransaction; } /* Traversed entire queue but did not find transaction */ if (transaction == NULL) { retVal = RM_SERVICE_ERROR_SERVICE_TRANSACTION_DOES_NOT_EXIST; } else { /* Delete the transaction */ if (prevTransaction == NULL) { /* Transaction to be deleted exists at start of transaction queue. Map second * transaction to be start of transaction queue. This covers case where there is * only one transaction in the queue since the nextTransaction will be NULL */ 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_osalMtCsExit(key); return (retVal); } uint32_t Rm_transactionInitSequenceNum(void) { /* Sequence number can never have a value of zero so that there are no conflicts * with transactions that have a remoteOriginatingId of zero */ return (1); } uint32_t Rm_transactionGetSequenceNum(Rm_Inst *rmInst) { uint32_t sequenceNum = 0; /* Get the next sequence number and then increment. If there's an overflow * assign the initial value instead of incrementing. */ if (rmInst->transactionSeqNum + 1 < rmInst->transactionSeqNum) { /* Overflow */ sequenceNum = rmInst->transactionSeqNum; rmInst->transactionSeqNum = Rm_transactionInitSequenceNum(); } else { sequenceNum = rmInst->transactionSeqNum++; } return (sequenceNum); } /* Function used to send RM response transactions to lower level agents */ void Rm_transactionResponder (Rm_Inst *rmInst, Rm_Transaction *transaction) { 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); break; case Rm_service_RESOURCE_MAP_TO_NAME: case Rm_service_RESOURCE_UNMAP_NAME: rmPkt = Rm_transportCreateNsResponsePkt(rmInst, dstTransportNode, transaction); break; default: /* Invalid service type. Flag the error and return */ transaction->state = RM_SERVICE_ERROR_INVALID_SERVICE_TYPE; break; } if (transaction->state <= RM_SERVICE_ERROR_BASE) { /* Delete the transaction and return immediately because an error occurred * allocating the packet */ Rm_transactionQueueDelete(rmInst, transaction->localId); return; } /* Send the RM packet to the application transport */ if (rmInst->transport.rmSend((Rm_TransportHandle) dstTransportNode, rmPkt) < RM_TRANSPORT_SUCCESSFUL) { /* Negative value returned by transport send. An error occurred * in the transport while attempting to send the packet.*/ transaction->state = 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 */ transaction->state = RM_SERVICE_ERROR_TRANSPORT_FREE_PKT_ERROR; } return; } /* NEED TO DO SOMETHING IF GET AN ERROR IN THE transaction->state FIELD. CREATE * NEW TRANSACTION WITH DATA FROM ORIGINAL? THEN TRY TO SEND FAILED REQUEST BACK * TO REQUESTER??? KEEP RETRYING SEND OF RESPONSE??? */ /* Delete the transaction */ Rm_transactionQueueDelete(rmInst, transaction->localId); } void Rm_allocationHandler (Rm_Inst *rmInst, Rm_Transaction *transaction) { if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) { #if 0 /* Check local policy to see if the request can be satisfied with the * resources stored locally */ Rm_policy...API() if (policy check approves the resource) { /* call the allocator to allocate the resource */ if (allocator returns resource) { /* Populate the transaction with the allocated resources and the result */ transaction->state = approve reason; return ... } else { /* allocator ran out of resources, need to contact Server for more * resources */ Rm_resourcePoolModRequest(...); } } else if (policy check denies resource) { /* Policy check denied resource. */ transaction->state= deny reason; return ... } else if (policy check says forward to Server for validation) { /* Forward the transaction to the Server */ Rm_transactionForwarder(rmInst, transaction); } #endif } else if (rmInst->instType == Rm_instType_SERVER) { #if 0 /* Check global policy to see if resource can be allocated. return result * no matter what */ Rm_policy...API() if (policy approves) { /* call allocator to allocate resource */ } transaction->state = approve or deny reason; transaction->resourceInfo.base = ...; transaction->resourceInfo.range = ...; /* If source instance name does not match the current instance * name the allocation request came from a Client. The result * must be sent back to the Client */ if (strcmp(transaction->sourceInstName, rmInst->name)) { /* Names don't match. Send the transaction back to the Client */ Rm_transactionResponder(rmInst, transaction); } else { /* Resource allocation request originated locally on the active * instance. Send the response via the service responder. */ Rm_serviceResponder(rmInst, transaction); } #endif } } void Rm_freeHandler (Rm_Inst *rmInst, Rm_Transaction *transaction) { if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) { #if 0 /* Check local policy to see if the request can be satisfied with the * resources stored locally */ Rm_policy...API() if (policy check approves the free) { /* call the allocator to free the resource */ /* Run a resource pool check to see if the free combined a resource block * that can be returned to the server */ if (resource block has been combined) { /* allocator ran out of resources, need to contact Server for more * resources */ Rm_resourcePoolModRequest(free pool block to server...); } else { /* Populate the receipt with the freed resources and the result */ transaction->state = approve reason; return ... } } else if (policy check denies resource free) { /* Policy check denied resource. */ transaction->state = deny reason; return ... } else if (policy check says forward to Server for validation) { /* Forward the transaction to the Server */ Rm_transactionForwarder(rmInst, transaction); } #endif } else if (rmInst->instType == Rm_instType_SERVER) { #if 0 /* Check global policy to see if resource can be freed. return result * no matter what */ Rm_policy...API() if (policy approves) { /* call allocator to free resources */ } transaction->state = approve or deny reason; transaction->resourceInfo.base = ...; transaction->resourceInfo.range = ...; /* If source instance name does not match the current instance * name the allocation request came from a client. The result * must be sent back to the Client */ if (strcmp(transaction->sourceInstName, rmInst->name)) { /* Names don't match. Send the transaction back to the Client Delegate or Client */ Rm_transactionResponder(rmInst, transaction); } else { /* Resource allocation request originated locally on the active * instance. Send the response via the service responder. */ Rm_serviceResponder(rmInst, transaction); } #endif } } /* Function used to forward RM transactions to higher level agents */ void Rm_transactionForwarder (Rm_Inst *rmInst, Rm_Transaction *transaction) { 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) { transaction->state = RM_SERVICE_ERROR_NOT_REGISTERED_WITH_DEL_OR_SERVER; return; } /* Find the transport for the higher level agent. Check for a connection to a Client Delegate * or a Server. Clients will be connected to either a Client Delegate or a Server. Client * Delegates will be connected to a Server. */ if (rmInst->instType == Rm_instType_CLIENT) { dstTransportNode = Rm_transportNodeFindRemoteInstType(rmInst, Rm_instType_CLIENT_DELEGATE); } else if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) { 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); break; case Rm_service_RESOURCE_MAP_TO_NAME: case Rm_service_RESOURCE_UNMAP_NAME: rmPkt = Rm_transportCreateNsRequestPkt(rmInst, dstTransportNode, transaction); break; default: /* Invalid service type. Flag the error and return */ transaction->state = RM_SERVICE_ERROR_INVALID_SERVICE_TYPE; break; } if (transaction->state <= RM_SERVICE_ERROR_BASE) { /* Return immediately because an error occurred allocating the packet */ return; } /* Send the RM packet to the application transport */ if (rmInst->transport.rmSend((Rm_TransportHandle) dstTransportNode, rmPkt) < RM_TRANSPORT_SUCCESSFUL) { /* Negative value returned by transport send. An error occurred * in the transport while attempting to send the packet.*/ transaction->state = 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 */ transaction->state = RM_SERVICE_ERROR_TRANSPORT_FREE_PKT_ERROR; } return; } /* Transaction is not deleted because it is awaiting a response from the higher level * RM instance */ } void Rm_transactionProcessor (Rm_Inst *rmInst, Rm_Transaction *transaction) { /* Handle auto-forwarded transactions. These transactions include: * - All request transactions received on Clients are forwarded to the Client Delegate * - NameServer requests received on the Client Delegate are forwarded to the Server */ if ((rmInst->instType == Rm_instType_CLIENT) || ((rmInst->instType == Rm_instType_CLIENT_DELEGATE) && (transaction->type == Rm_service_RESOURCE_MAP_TO_NAME) || (transaction->type == Rm_service_RESOURCE_UNMAP_NAME))) { /* Check if the transaction is a transaction that received a response to its * request. */ if (transaction->state != RM_SERVICE_PROCESSING) { /* A transaction has received a response. Send the response to either the * transaction or service responder based on the source instance */ if (strcmp(transaction->sourceInstName, rmInst->name)) { /* Transaction originated from another instance. Use the * transaction responder to send the result to the source instance. This * is not possible on RM Clients since they can't forward RM services */ Rm_transactionResponder(rmInst, transaction); } else { /* Transaction originated on this instance. Send to the * service responder */ Rm_serviceResponder(rmInst, transaction); } } else { /* This is a new transaction that must be forwarded to a higher level RM instance. */ Rm_transactionForwarder(rmInst, transaction); } } else { /* Client Delegate and Server transaction processors. */ 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: /* Check if the transaction is fulfilled request */ if (transaction->state != RM_SERVICE_PROCESSING) { /* If source instance name does not match the current instance * name the allocation request came from a client. The result * must be sent back to the Client */ if (strcmp(transaction->sourceInstName, rmInst->name)) { Rm_transactionResponder(rmInst, transaction); } else { /* Resource allocation request originated locally. Send the response * via the service responder. */ Rm_serviceResponder(rmInst, transaction); /* WHAT IF TRANSACITON CAME FROM CD AND WAS SOLVED IMMEDIATELY. NEED TO RETURN * THE TRANSACTION RATHER THAN GO THROUGH THE SERVICERESPONDER */ } } else { /* This is a new transaction request originating from an RM instance with fewer * allocate/free privileges. Run the allocation or free handler to see if the resource * request can be handled locally or if it needs to be forwarded to a higher level * agent */ if ((transaction->type == Rm_service_RESOURCE_ALLOCATE) || (transaction->type == Rm_service_RESOURCE_BLOCK_ALLOCATE) || (transaction->type == Rm_service_RESOURCE_ALLOCATE_BY_NAME)) { Rm_allocationHandler(rmInst, transaction); } else { Rm_freeHandler(rmInst, transaction); } } break; case Rm_service_RESOURCE_MAP_TO_NAME: case Rm_service_RESOURCE_UNMAP_NAME: /* Server is the only RM instance capable of adding NameServer objects */ if (rmInst->instType == Rm_instType_SERVER) { if (transaction->type == Rm_service_RESOURCE_MAP_TO_NAME) { /* Create a new NameServer object with the request transaction information. * Transaction will contain the state result of the NameServer addition. */ Rm_nsAddObject(rmInst, transaction); } else { /* Delete an existing NameServer object with the request transaction information * Transaction will contain the state result of the NameServer addition. */ Rm_nsDeleteObject(rmInst, transaction); } /* If source instance name does not match the local instance * name the NameServer request came from a Client or Client Delegate. The * result must be sent back to the Client or Client Delegate. Just return if it does * match since the NameServer transaction result can be returned immediately. */ if (strcmp(transaction->sourceInstName, rmInst->name)) { Rm_transactionResponder(rmInst, transaction); } } else { transaction->state = RM_SERVICE_ERROR_NAMESERVER_OBJECT_MOD_ON_INVALID_INSTANCE; } 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->registeredWithDelegateOrServer = false; /* 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 elements. */ rmInst->transactionSeqNum = Rm_transactionInitSequenceNum(); 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; } /** @} */