/** * @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 #include /* RM LIBFDT includes */ #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 char rmIntegerAllocator[] = "integer"; char rmTreeAllocator[] = "tree"; extern char rmDtbStartingNode[]; /** @brief Global Variable which describes the RM Version Information */ const char rmVersionStr[] = RM_VERSION_STR ":" __DATE__ ":" __TIME__; /********************************************************************** ********************** Internal Functions **************************** **********************************************************************/ 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)); /* Return if the memory allocated for the transaction entry is NULL */ if (newTransaction == NULL) { /* 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->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)); } 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); } Rm_Allocator *Rm_allocatorAdd(Rm_Inst *rmInst, const char *resourceName, Rm_AllocatorType type) { Rm_Allocator *allocators = (Rm_Allocator *)rmInst->allocators; Rm_Allocator *newAllocator = NULL; void *key; /* Lock access to the RM instance's allocator list */ key = Rm_osalMtCsEnter(); /* Get memory for a new allocator from local memory */ newAllocator = Rm_osalMalloc(sizeof(Rm_Allocator)); /* Return if the memory allocated for the allocator is NULL */ if (newAllocator != NULL) { /* Clear the allocator */ memset((void *)newAllocator, 0, sizeof(Rm_Allocator)); /* Populate the allocator */ newAllocator->type = type; strcpy(newAllocator->resourceName, resourceName); /* allocator's root entry will be created by the invoking function */ newAllocator->allocatorRootEntry = NULL; /* New allocator's nextAllocator pointer will always be NULL */ newAllocator->nextAllocator = NULL; /* Check if there are any allocators in the allocator list */ if (allocators) { /* At least one allocator in the allocator list. Add the new allocator to the * end of the allocator list */ while (allocators->nextAllocator != NULL) { /* Traverse the list until arriving at the last allocator */ allocators = allocators->nextAllocator; } /* Add the new allocator to the end of the list */ allocators->nextAllocator = newAllocator; } else { /* The allocator list does not currently exist. The new allocator is the * first allocator */ rmInst->allocators = newAllocator; } } Rm_osalMtCsExit(key); return (newAllocator); } int32_t Rm_allocatorDelete(Rm_Inst *rmInst, char *resourceName) { Rm_Allocator *allocator = (Rm_Allocator *) rmInst->allocators; Rm_Allocator *prevAllocator = NULL; int32_t retVal = RM_SERVICE_STATE_OKAY; void *key; /* Lock access to the RM instance's allocator list */ key = Rm_osalMtCsEnter(); /* Find the resource within the specified RM instance's allocator list. */ while (allocator != NULL) { if (strcmp(allocator->resourceName, resourceName) == 0) { /* Match: break out of loop and delete the transaction */ break; } prevAllocator = allocator; allocator = allocator->nextAllocator; } /* Traversed entire list but did not find allocator. */ if (allocator == NULL) { retVal = -22; /* TEMP ERROR: Can't conflict with LIBFDT errors */ } else { /* Delete the allocator */ if ((prevAllocator == NULL) && allocator->nextAllocator) { /* Allocator to be deleted exists at start of allocator list. Map second * allocator to be start of allocator list as long as there are more than * one allocators. */ rmInst->allocators = allocator->nextAllocator; } else { /* Allocator to be deleted is in the middle or at end of the list. Adjust * adjacent allocator pointers. This covers the case where the allocator to be * removed is at the end of the list. */ prevAllocator->nextAllocator = allocator->nextAllocator; } /* Free the memory associated with the allocator. */ Rm_osalFree((void *)allocator, sizeof(Rm_Allocator)); } Rm_osalMtCsExit(key); return (retVal); } int32_t Rm_createIntegerAllocator(Rm_Inst *rmInst, const char *resourceName, Rm_ResourceRange *range) { Rm_Allocator *allocator = NULL; Rm_ResourceRange *rangeBasePtr = range; Rm_IntegerAllocatorRootEntry *intRootEntry = NULL; uint16_t i, entryIndex; /* Create the new base integer allocator */ allocator = Rm_allocatorAdd(rmInst, resourceName, Rm_allocatorType_INTEGER); /* Construct the integer allocator root entry */ intRootEntry = Rm_osalMalloc(sizeof(Rm_IntegerAllocatorRootEntry)); intRootEntry->numResourceElements = 0; /* Get the number of entries to allocate based on the lengths in the ranges */ while (range != NULL) { intRootEntry->numResourceElements += range->length; range = range->nextRange; } /* Initialize the entries using the range information */ if (intRootEntry->numResourceElements) { intRootEntry->resourceArrayBase = Rm_osalMalloc(sizeof(Rm_IntegerEntry) * intRootEntry->numResourceElements); memset((void *)intRootEntry->resourceArrayBase, 0, sizeof(Rm_IntegerEntry) * intRootEntry->numResourceElements); /* Reset the range pointer */ range = rangeBasePtr; entryIndex = 0; while (range != NULL) { /* Initialize each entry */ for (i = range->base; i < (range->base + range->length); i++, entryIndex++) { intRootEntry->resourceArrayBase[entryIndex].value = i; } range = range->nextRange; } allocator->allocatorRootEntry = intRootEntry; } else { /* No resource entries were created. Free the memory associated with the * allocator and the root entry */ Rm_osalFree((void *)intRootEntry, sizeof(Rm_IntegerAllocatorRootEntry)); Rm_allocatorDelete(rmInst, allocator->resourceName); } return(0); /* TODO: FIX THIS RETURN */ } int32_t Rm_createAndInitAllocator(Rm_Inst *rmInst, const char *resourceName, Rm_ResourceProperties *resourceProperties) { char *allocatorType = NULL; Rm_ResourceRange *range = NULL; Rm_ResourceRange *rangeBasePtr = NULL; Rm_NsAssignment *nsAssignments = NULL; Rm_NsAssignment *nsAssignmentBasePtr = NULL; int32_t retVal = RM_DTB_UTIL_RESULT_OKAY; /* TODO: NEED CHECKS FOR VALIDITY OF ALL THE resourceProperties FIELDS */ /* Extract the resource properties from the DTB */ allocatorType = Rm_resourceExtractResourceAllocator(resourceProperties->allocatorData, resourceProperties->allocatorLen); range = rangeBasePtr = Rm_resourceExtractResourceRange(resourceProperties->rangeData, resourceProperties->rangeLen); /* Create an allocator based on the allocator type specified */ if (strcmp(allocatorType, &rmIntegerAllocator[0]) == 0) { /* Create an integer allocator using the resource properties */ retVal = Rm_createIntegerAllocator(rmInst, resourceName, range); } else if (strcmp(allocatorType, &rmTreeAllocator[0]) == 0) { /* Create a tree allocator using the resource properties */ } else { /* Allocator type not recognized. Free the resource properties and return */ retVal = -21; /* TEMP ERROR: Can't conflict with LIBFDT errors */ } if (retVal >= RM_DTB_UTIL_RESULT_OKAY) { /* Create entries in the NameServer if any NameServer assignments were specified */ if (resourceProperties->nsAssignData && resourceProperties->nsAssignLen) { nsAssignments = Rm_resourceExtractNsAssignment(resourceProperties->nsAssignData, resourceProperties->nsAssignLen); /* Cycle through the list of assignments and add them to the NameServer */ nsAssignmentBasePtr = nsAssignments; while (nsAssignments) { /* TODO: RETURN IF ANY OF THE ADDS FAIL??? */ Rm_nsAddObject(rmInst, nsAssignments->nsName, nsAssignments->resourceValue); nsAssignments = nsAssignments->nextNsAssignment; } /* Free the memory allocated for the NameServer assignments */ Rm_resourceFreeNsAssignmentList(nsAssignmentBasePtr); } } /* Free the memory allocated for the resource properties */ Rm_resourceFreeResourceAllocator(allocatorType); Rm_resourceFreeResourceRange(rangeBasePtr); return(retVal); } int32_t Rm_parseResourceProperty(void *globalResourceDtb, int32_t offset, Rm_ResourceProperties *propertyInfo) { int32_t propertyLen; const char *propertyName; const void *propertyData; Rm_ResourcePropType propertyType; int32_t retVal = RM_DTB_UTIL_RESULT_OKAY; /* Get the property data and store it in the corresponding propertyInfo field */ propertyData = fdt_getprop_by_offset(globalResourceDtb, offset, &propertyName, &propertyLen); if (propertyData) { propertyType = Rm_resourceGetPropertyType(propertyName); if (propertyType == Rm_resourcePropType_RESOURCE_ALLOCATOR) { if (propertyInfo->allocatorData || propertyInfo->allocatorLen) { /* The allocator fields have already been populated. Return an error. * The resource list has specified a property field more than once * for a resource node */ retVal = -17; /* TEMP ERROR: Can't conflict with LIBFDT errors */ } else { propertyInfo->allocatorData = propertyData; propertyInfo->allocatorLen = propertyLen; } } else if (propertyType == Rm_resourcePropType_RESOURCE_RANGE) { if (propertyInfo->rangeData || propertyInfo->rangeLen) { /* The range fields have already been populated. Return an error. * The resource list has specified a property field more than once * for a resource node */ retVal = -18; /* TEMP ERROR: Can't conflict with LIBFDT errors */ } else { propertyInfo->rangeData = propertyData; propertyInfo->rangeLen = propertyLen; } } else if (propertyType == Rm_resourcePropType_NSASSIGNMENT) { if (propertyInfo->nsAssignData || propertyInfo->nsAssignLen) { /* The nsAssign fields have already been populated. Return an error. * The resource list has specified a property field more than once * for a resource node */ retVal = -19; /* TEMP ERROR: Can't conflict with LIBFDT errors */ } else { propertyInfo->nsAssignData = propertyData; propertyInfo->nsAssignLen = propertyLen; } } else { retVal = -20; /* TEMP ERROR: Can't conflict with LIBFDT errors */ } } else { retVal = -16; /* TEMP ERROR: Can't conflict with LIBFDT errors */ } /* Don't get anymore properties if error occurred */ if (retVal == RM_DTB_UTIL_RESULT_OKAY) { offset = fdt_next_property_offset(globalResourceDtb, offset); if (offset >= 0) { retVal = Rm_parseResourceProperty(globalResourceDtb, offset, propertyInfo); } else if (offset != -FDT_ERR_NOTFOUND) { /* Error was returned by LIBFDT when parsing the properties */ retVal = offset; } } return (retVal); } int32_t Rm_parseResourceNode(Rm_Inst *rmInst, void *globalResourceDtb, int32_t nodeOffset, int32_t depth) { const char *resourceName = fdt_get_name(globalResourceDtb, nodeOffset, NULL); Rm_ResourceProperties resourceProperties; int32_t error = RM_DTB_UTIL_RESULT_OKAY; int32_t offset; /* Initialize the resource properties structure */ memset((void *)&resourceProperties, 0, sizeof(Rm_ResourceProperties)); /* Ignore properties of the base node */ if (strcmp(resourceName, rmDtbStartingNode)) { /* Get the properties for the resource node if any exist */ offset = fdt_first_property_offset(globalResourceDtb, nodeOffset); if (offset >= RM_DTB_UTIL_STARTING_NODE_OFFSET) { /* Since at least one property exists attempt to parse the property nodes and * use them to create and initialize a resource allocator */ error = Rm_parseResourceProperty(globalResourceDtb, offset, &resourceProperties); if (error < -FDT_ERR_NOTFOUND) { return (error); } /* Initialize an allocator with the resource properties if no error was returned */ Rm_createAndInitAllocator(rmInst, resourceName, &resourceProperties); } else if (offset != -FDT_ERR_NOTFOUND) { /* Error was returned by LIBFDT when parsing the properties */ return (offset); } } /* Get the next resource node */ offset = fdt_next_node(globalResourceDtb, nodeOffset, &depth); /* Check the offset and depth of the next node to make sure the current node * wasn't the last node in the Resource List. A depth less than the depth set * at the start of the recursion will signal the end of the resource list */ if ((offset >= RM_DTB_UTIL_STARTING_NODE_OFFSET) && (depth >= RM_DTB_UTIL_STARTING_DEPTH)) { error = Rm_parseResourceNode(rmInst, globalResourceDtb, offset, depth); if (error < -FDT_ERR_NOTFOUND) { return (error); } } else if (offset != -FDT_ERR_NOTFOUND) { /* Error was returned by LIBFDT when parsing the nodes */ return (offset); } return (RM_DTB_UTIL_RESULT_OKAY); } int32_t Rm_initializeAllocators(Rm_Inst *rmInst, void *globalResourceDtb) { int32_t nodeOffset = RM_DTB_UTIL_STARTING_NODE_OFFSET; int32_t startDepth = RM_DTB_UTIL_STARTING_DEPTH; int32_t result = RM_DTB_UTIL_RESULT_OKAY; /* Recursively parse the Global Resource List, creating an allocator for * each resource as specified in the node */ result = Rm_parseResourceNode(rmInst, globalResourceDtb, nodeOffset, startDepth); return(result); } int32_t Rm_reserveLinuxResources(void *linuxResourceDtb) { return(0); } 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); } } 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. */ if (Rm_nsAddObject(rmInst, transaction->resourceInfo.nsName, transaction->resourceInfo.base) == RM_NS_ACTION_APPROVED) { transaction->state = RM_SERVICE_APPROVED; } else { /* TEMP: UPDATE THIS STATE VALUE */ transaction->state = RM_SERVICE_DENIED_BEGIN; } } else { /* Delete an existing NameServer object with the request transaction information * Transaction will contain the state result of the NameServer addition. */ if (Rm_nsDeleteObject(rmInst, transaction->resourceInfo.nsName) == RM_NS_ACTION_APPROVED) { transaction->state = RM_SERVICE_APPROVED; } else { /* TEMP: UPDATE THIS STATE VALUE */ transaction->state = RM_SERVICE_DENIED_BEGIN; } } /* 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 by the * Rm_serviceHandler. */ 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; void *globalResourceDtb = NULL; void *linuxResourceDtb = NULL; /* 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)); /* Populate instance based on input parameters */ strcpy (&rmInst->name[0], initCfg->instName); rmInst->instType = initCfg->instType; rmInst->registeredWithDelegateOrServer = false; rmInst->policyDtb = NULL; /* 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 allocators linked list pointer to NULL. The linked list nodes will * be created on the Server instance when the application reads in the resource list. * Nodes will also be created on Client Delegates when blocks of resources are requested * for allocation to clients. */ rmInst->allocators = NULL; /* Initialize the transaction queue elements. */ rmInst->transactionSeqNum = Rm_transactionInitSequenceNum(); rmInst->transactionQueue= NULL; /* RM Server specific actions */ if (rmInst->instType == Rm_instType_SERVER) { /* Open the ResourceList file and provide it to the resource initializer. */ if (initCfg->globalResourceList) { globalResourceDtb = initCfg->globalResourceList; fdt_open_into(globalResourceDtb, globalResourceDtb, fdt_totalsize(globalResourceDtb)); Rm_initializeAllocators(rmInst, globalResourceDtb); } /* Parse the Linux DTB for the resources reserved by the Linux kernel. These resources * will be marked as used in the resource allocators. */ if (initCfg->linuxDtb) { linuxResourceDtb = initCfg->linuxDtb; fdt_open_into(linuxResourceDtb, linuxResourceDtb, fdt_totalsize(linuxResourceDtb)); Rm_reserveLinuxResources(linuxResourceDtb); } } /* Instance startup policies are only used for Servers and Client Delegates */ if (rmInst->instType != Rm_instType_CLIENT) { /* Open the instance's policy and store it */ if (initCfg->startupPolicy) { rmInst->policyDtb = initCfg->startupPolicy; fdt_open_into(rmInst->policyDtb, rmInst->policyDtb, fdt_totalsize(rmInst->policyDtb)); } /* 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; } /** @} */