diff --git a/src/rm.c b/src/rm.c
index c108bf6c58d4a978b52367f0802715634bd2fe63..3afc89ed2908db1c4a0706c88d809501e9433843 100644 (file)
--- a/src/rm.c
+++ b/src/rm.c
-/**\r
- * @file rm.c\r
- *\r
- * @brief \r
- * This is the Resource Manager source.\r
- *\r
- * \par\r
- * ============================================================================\r
- * @n (C) Copyright 2012, Texas Instruments, Inc.\r
- * \r
- * Redistribution and use in source and binary forms, with or without \r
- * modification, are permitted provided that the following conditions \r
- * are met:\r
- *\r
- * Redistributions of source code must retain the above copyright \r
- * notice, this list of conditions and the following disclaimer.\r
- *\r
- * Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the \r
- * documentation and/or other materials provided with the \r
- * distribution.\r
- *\r
- * Neither the name of Texas Instruments Incorporated nor the names of\r
- * its contributors may be used to endorse or promote products derived\r
- * from this software without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \r
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT \r
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT \r
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, \r
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT \r
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- *\r
- * \par\r
-*/\r
-\r
-/* RM Types */\r
-#include <ti/drv/rm/rm_types.h>\r
-\r
-/* RM external includes */\r
-#include <ti/drv/rm/rm.h>\r
-#include <ti/drv/rm/rm_services.h>\r
-#include <ti/drv/rm/rm_transport.h>\r
-#include <ti/drv/rm/rm_policy.h>\r
-\r
-/* RM internal includes */\r
-#include <ti/drv/rm/include/rm_loc.h>\r
-#include <ti/drv/rm/include/rm_transportloc.h>\r
-#include <ti/drv/rm/include/rm_servicesloc.h>\r
-#include <ti/drv/rm/include/rm_nameserverloc.h>\r
-#include <ti/drv/rm/include/rm_dtb_utilloc.h>\r
-#include <ti/drv/rm/include/rm_policyloc.h>\r
-\r
-/* RM LIBFDT includes */\r
-#include <ti/drv/rm/src/libfdt/libfdt.h>\r
-\r
-/* AVL BBST includes */\r
-#include <ti/drv/rm/include/tree.h>\r
-\r
-/* RM OSAL layer */\r
-#include <rm_osal.h>\r
-\r
-/**********************************************************************\r
- ************************** Globals ***********************************\r
- **********************************************************************/\r
-extern char rmDtbStartingNode[];\r
-\r
-/** @brief Global Variable which describes the RM Version Information */\r
-const char rmVersionStr[] = RM_VERSION_STR ":" __DATE__ ":" __TIME__;\r
-\r
-/**********************************************************************\r
- ************** Red-Black BBST Tree Allocator Functions ***************\r
- **********************************************************************/\r
-\r
-\r
-Rm_ResourceNode *Rm_newResourceNode(uint32_t resourceBase, uint32_t resourceLength)\r
-{\r
- Rm_ResourceNode *newNode = NULL;\r
-\r
- newNode = Rm_osalMalloc(sizeof(Rm_ResourceNode));\r
- memset((void *)newNode, 0, sizeof(Rm_ResourceNode));\r
-\r
- newNode->base = resourceBase;\r
- newNode->length = resourceLength;\r
- newNode->allocationCount = 0;\r
- newNode->allocatedTo = NULL;\r
-\r
- return(newNode);\r
-}\r
-\r
-void Rm_freeResourceNode(Rm_ResourceNode *node)\r
-{\r
- if (node->allocationCount == 0) {\r
- Rm_osalFree((void *)node, sizeof(Rm_ResourceNode));\r
- }\r
-}\r
-\r
-/* Prototype for tree node comparison function\r
- * element1 < element2 --> return < 0\r
- * element1 = element2 --> return 0\r
- * element1 > element2 --> return > 0 */\r
-int Rm_ResourceNodeCompare(Rm_ResourceNode *element1, Rm_ResourceNode *element2)\r
-{\r
- uint32_t element1End = element1->base + element1->length - 1;\r
- uint32_t element2End = element2->base + element2->length - 1;\r
-\r
- if (element1End < element2->base) {\r
- /* End of element1 range is less than the start of element2's range. Return a negative\r
- * value */\r
- return (-1);\r
- }\r
- else if (element1->base > element2End) {\r
- /* Start of element1 range is after end of element2's range. Return a positive value */\r
- return (1);\r
- }\r
- else {\r
- /* If neither of the latter conditions were satisfied there is some overlap between\r
- * element1 and element2. Return 0 since the application must handle this overlap. */\r
- return (0);\r
- }\r
-}\r
-\r
-/* Generate the red-black tree manipulation functions */\r
-RB_GENERATE(_Rm_ResourceTree, _Rm_ResourceNode, linkage, Rm_ResourceNodeCompare);\r
-\r
-/**********************************************************************\r
- ********************** Internal Functions ****************************\r
- **********************************************************************/\r
-\r
-Rm_Transaction *Rm_transactionQueueAdd(Rm_Inst *rmInst)\r
-{\r
- Rm_Transaction *transactionQueue = (Rm_Transaction *)rmInst->transactionQueue;\r
- Rm_Transaction *newTransaction = NULL;\r
- void *key;\r
-\r
- /* Lock access to the RM instance's transaction queue */\r
- key = Rm_osalMtCsEnter();\r
-\r
- /* Get memory for a new transaction from local memory */\r
- newTransaction = Rm_osalMalloc(sizeof(Rm_Transaction));\r
-\r
- /* Return if the memory allocated for the transaction entry is NULL */\r
- if (newTransaction != NULL) {\r
- /* Clear the transaction */\r
- memset((void *)newTransaction, 0, sizeof(Rm_Transaction));\r
-\r
- /* Create an ID for the new transaction. The ID will be used for two purposes:\r
- * 1) Matching responses from higher level RM agents to requests\r
- * 2) Provided to the component that requested the service so that it can match its\r
- * request with the response it receives via its callback function it provided */\r
- newTransaction->localId = Rm_transactionGetSequenceNum(rmInst);\r
- /* New transaction's nextTransaction pointer will always be NULL */\r
- newTransaction->nextTransaction = NULL; \r
-\r
- /* Check if there are any transactions in the transaction queue */\r
- if (transactionQueue) {\r
- /* At least one transaction in the transaction queue. Add the new entry to the \r
- * end of the transaction queue */\r
- while (transactionQueue->nextTransaction != NULL) {\r
- /* Traverse the list until arriving at the last transaction */\r
- transactionQueue = transactionQueue->nextTransaction;\r
- }\r
-\r
- /* Add the new transaction to the end of the queue */\r
- transactionQueue->nextTransaction = newTransaction;\r
- }\r
- else {\r
- /* The transaction queue does not currently exist. The new transaction is the \r
- * first transaction */\r
- rmInst->transactionQueue = newTransaction;\r
- }\r
- }\r
-\r
- Rm_osalMtCsExit(key);\r
- return (newTransaction);\r
-}\r
-\r
-Rm_Transaction *Rm_transactionQueueFind(Rm_Inst *rmInst, uint32_t transactionId)\r
-{\r
- Rm_Transaction *transaction = (Rm_Transaction *)rmInst->transactionQueue;\r
-\r
- if (transaction != NULL) {\r
- while (transaction != NULL) {\r
- if (transaction->localId == transactionId) {\r
- break; \r
- }\r
- transaction = transaction->nextTransaction;\r
- }\r
- }\r
- return (transaction);\r
-}\r
-\r
-int32_t Rm_transactionQueueDelete(Rm_Inst *rmInst, uint32_t transactionId)\r
-{\r
- Rm_Transaction *transaction = (Rm_Transaction *) rmInst->transactionQueue;\r
- Rm_Transaction *prevTransaction = NULL;\r
- int32_t retVal = RM_SERVICE_STATE_OKAY;\r
- void *key;\r
-\r
- /* Lock access to the instance */\r
- key = Rm_osalMtCsEnter();\r
-\r
- /* Find transaction ID within transaction queue. */\r
- while (transaction) {\r
- if (transaction->localId == transactionId) {\r
- break; \r
- }\r
-\r
- prevTransaction = transaction;\r
- transaction = transaction->nextTransaction;\r
- }\r
-\r
- if (transaction) {\r
- if (prevTransaction == NULL) {\r
- /* Transaction at start of queue. Map second transaction to start of queue \r
- * as long as more than one transactions. */\r
- rmInst->transactionQueue = transaction->nextTransaction;\r
- }\r
- else {\r
- /* Transaction in middle or end of queue. */\r
- prevTransaction->nextTransaction = transaction->nextTransaction;\r
- }\r
- Rm_osalFree((void *)transaction, sizeof(Rm_Transaction));\r
- }\r
- else {\r
- retVal = RM_SERVICE_ERROR_SERVICE_TRANSACTION_DOES_NOT_EXIST;\r
- } \r
-\r
- Rm_osalMtCsExit(key);\r
- return (retVal);\r
-}\r
-\r
-uint32_t Rm_transactionInitSequenceNum(void)\r
-{\r
- /* Sequence number can never have value of 0. Avoids conflicts\r
- * with non-response transactions that have remoteOriginatingId of 0 */\r
- return (1);\r
-}\r
-\r
-uint32_t Rm_transactionGetSequenceNum(Rm_Inst *rmInst)\r
-{\r
- uint32_t sequenceNum = 0;\r
-\r
- if (rmInst->transactionSeqNum + 1 < rmInst->transactionSeqNum) {\r
- /* Overflow */\r
- sequenceNum = rmInst->transactionSeqNum;\r
- rmInst->transactionSeqNum = Rm_transactionInitSequenceNum();\r
- }\r
- else {\r
- sequenceNum = rmInst->transactionSeqNum++;\r
- } \r
- return (sequenceNum);\r
-}\r
-\r
-Rm_Allocator *Rm_allocatorAdd(Rm_Inst *rmInst, const char *resourceName)\r
-{\r
- Rm_Allocator *allocators = (Rm_Allocator *)rmInst->allocators;\r
- Rm_Allocator *newAllocator = NULL;\r
- void *key;\r
-\r
- /* Lock access to the instance */\r
- key = Rm_osalMtCsEnter();\r
-\r
- newAllocator = Rm_osalMalloc(sizeof(Rm_Allocator));\r
-\r
- if (newAllocator) {\r
- memset((void *)newAllocator, 0, sizeof(Rm_Allocator));\r
- strcpy(newAllocator->resourceName, resourceName);\r
- newAllocator->allocatorRootEntry = NULL;\r
- newAllocator->nextAllocator = NULL; \r
-\r
- /* Add allocator to end of list */\r
- if (allocators) {\r
- while (allocators->nextAllocator != NULL) {\r
- allocators = allocators->nextAllocator;\r
- }\r
- allocators->nextAllocator = newAllocator;\r
- }\r
- else {\r
- rmInst->allocators = newAllocator;\r
- }\r
- }\r
-\r
- Rm_osalMtCsExit(key);\r
- return (newAllocator);\r
-}\r
-\r
-Rm_Allocator *Rm_allocatorFind(Rm_Inst *rmInst, char *resourceName)\r
-{\r
- Rm_Allocator *allocator = (Rm_Allocator *)rmInst->allocators;\r
-\r
- while (allocator) {\r
- if (strcmp(allocator->resourceName, resourceName) == 0) {\r
- break; \r
- }\r
- allocator = allocator->nextAllocator;\r
- }\r
-\r
- return (allocator);\r
-}\r
-\r
-int32_t Rm_allocatorDelete(Rm_Inst *rmInst, char *resourceName)\r
-{\r
- Rm_Allocator *allocator = (Rm_Allocator *) rmInst->allocators;\r
- Rm_Allocator *prevAllocator = NULL;\r
- int32_t retVal = RM_SERVICE_STATE_OKAY;\r
- void *key;\r
-\r
- /* Lock access to instance */\r
- key = Rm_osalMtCsEnter();\r
-\r
- while (allocator) {\r
- if (strcmp(allocator->resourceName, resourceName) == 0) {\r
- break; \r
- }\r
- prevAllocator = allocator;\r
- allocator = allocator->nextAllocator;\r
- }\r
-\r
- if (allocator) {\r
- if (prevAllocator == NULL) {\r
- /* Allocator at start of list. Map second allocator to start of list \r
- * as long as more than one allocators. */\r
- rmInst->allocators = allocator->nextAllocator;\r
- }\r
- else {\r
- /* Allocator in middle or end of list. */\r
- prevAllocator->nextAllocator = allocator->nextAllocator;\r
- }\r
- Rm_osalFree((void *)allocator, sizeof(Rm_Allocator));\r
- }\r
- else {\r
- retVal = -22; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
- }\r
-\r
- Rm_osalMtCsExit(key);\r
- return (retVal);\r
-}\r
-\r
-void Rm_addAllocatedTo(Rm_ResourceNode *node, void *serviceInstNode)\r
-{\r
- Rm_AllocatedTo *allocatedToList = node->allocatedTo;\r
- Rm_AllocatedTo *newAllocatedTo = NULL;\r
-\r
- newAllocatedTo = Rm_osalMalloc(sizeof(Rm_AllocatedTo));\r
-\r
- if (newAllocatedTo) {\r
- newAllocatedTo->instNameNode = serviceInstNode;\r
- newAllocatedTo->nextAllocatedTo = NULL; \r
-\r
- /* Add allocatedTo entry to end of list */\r
- if (allocatedToList) {\r
- while (allocatedToList->nextAllocatedTo) {\r
- allocatedToList = allocatedToList->nextAllocatedTo;\r
- }\r
- allocatedToList->nextAllocatedTo = newAllocatedTo;\r
- }\r
- else {\r
- node->allocatedTo = newAllocatedTo;\r
- }\r
-\r
- node->allocationCount++;\r
- Rm_policyIncrementValidInstAllocationCount(newAllocatedTo->instNameNode);\r
- }\r
-}\r
-\r
-bool Rm_isAllocatedTo(Rm_ResourceNode *node, void *serviceInstNode)\r
-{\r
- Rm_AllocatedTo *allocatedTo = node->allocatedTo;\r
-\r
- while (allocatedTo) {\r
- if (allocatedTo->instNameNode == serviceInstNode) {\r
- return(TRUE); \r
- }\r
- allocatedTo = allocatedTo->nextAllocatedTo;\r
- }\r
- return(FALSE);\r
-}\r
-\r
-bool Rm_compareResourceNodeAllocations(Rm_ResourceNode *nodeOne, Rm_ResourceNode *nodeTwo)\r
-{\r
- Rm_AllocatedTo *nodeOneAllocs = nodeOne->allocatedTo;\r
- Rm_AllocatedTo *nodeTwoAllocs = nodeTwo->allocatedTo;\r
- bool matchedInst;\r
-\r
- if (nodeOne->allocationCount == nodeTwo->allocationCount) {\r
- while (nodeOneAllocs) {\r
- matchedInst = FALSE;\r
- while (nodeTwoAllocs) {\r
- if (nodeOneAllocs->instNameNode == nodeTwoAllocs->instNameNode) {\r
- matchedInst = TRUE;\r
- break;\r
- }\r
- nodeTwoAllocs = nodeTwoAllocs->nextAllocatedTo;\r
- }\r
-\r
- if (matchedInst) {\r
- nodeTwoAllocs = nodeTwo->allocatedTo;\r
- nodeOneAllocs = nodeOneAllocs->nextAllocatedTo;\r
- }\r
- else {\r
- return(FALSE);\r
- } \r
- }\r
- }\r
- else {\r
- return(FALSE);\r
- } \r
- \r
- return(TRUE);\r
-}\r
-\r
-void Rm_deleteAllocatedTo(Rm_ResourceNode *node, void *serviceInstNode)\r
-{\r
- Rm_AllocatedTo *allocatedTo = node->allocatedTo;\r
- Rm_AllocatedTo *prevAllocatedTo = NULL;\r
-\r
- while (allocatedTo) {\r
- if (allocatedTo->instNameNode == serviceInstNode) {\r
- break; \r
- }\r
- prevAllocatedTo = allocatedTo;\r
- allocatedTo = allocatedTo->nextAllocatedTo;\r
- }\r
-\r
- if (prevAllocatedTo == NULL) {\r
- /* AllocatedTo entry at start of list. Map second allocatedTo entry to start of list \r
- * as long as more than one allocatedTo entries. */\r
- node->allocatedTo = allocatedTo->nextAllocatedTo;\r
- }\r
- else {\r
- /* AllocatedTo entry in middle or end of list. */\r
- prevAllocatedTo->nextAllocatedTo = allocatedTo->nextAllocatedTo;\r
- }\r
- \r
- node->allocationCount--;\r
- Rm_policyDecrementValidInstAllocationCount(allocatedTo->instNameNode);\r
- Rm_osalFree((void *)allocatedTo, sizeof(Rm_AllocatedTo));\r
-}\r
-\r
-void Rm_copyAllocatedTo(Rm_ResourceNode *dstNode, Rm_ResourceNode *srcNode)\r
-{\r
- Rm_AllocatedTo *srcAllocs = srcNode->allocatedTo;\r
- Rm_AllocatedTo *newDstAlloc;\r
- Rm_AllocatedTo *prevDstAlloc;\r
-\r
- dstNode->allocationCount = srcNode->allocationCount;\r
-\r
- while (srcAllocs) {\r
- newDstAlloc = Rm_osalMalloc(sizeof(Rm_AllocatedTo));\r
- newDstAlloc->instNameNode = srcAllocs->instNameNode;\r
- newDstAlloc->nextAllocatedTo = NULL;\r
-\r
- if (dstNode->allocatedTo == NULL) {\r
- dstNode->allocatedTo = newDstAlloc;\r
- }\r
- else {\r
- prevDstAlloc->nextAllocatedTo = newDstAlloc;\r
- }\r
- prevDstAlloc = newDstAlloc;\r
- srcAllocs = srcAllocs->nextAllocatedTo;\r
- }\r
-}\r
-\r
-void Rm_clearAllocatedTo(Rm_ResourceNode *node)\r
-{\r
- Rm_AllocatedTo *allocatedTo = node->allocatedTo;\r
- Rm_AllocatedTo *nextAllocatedTo;\r
-\r
- while (allocatedTo) {\r
- nextAllocatedTo = allocatedTo->nextAllocatedTo;\r
- node->allocationCount--;\r
- Rm_osalFree((void *)allocatedTo, sizeof(Rm_AllocatedTo));\r
- allocatedTo = nextAllocatedTo;\r
- }\r
-}\r
-\r
-int32_t Rm_createTreeAllocator(Rm_Inst *rmInst, const char *resourceName, Rm_ResourceRange *range)\r
-{\r
- Rm_Allocator *allocator = NULL;\r
- Rm_ResourceTree *treeRootEntry = NULL;\r
- Rm_ResourceNode *treeNode = NULL;\r
- Rm_ResourceNode *collidingNode = NULL;\r
-\r
- allocator = Rm_allocatorAdd(rmInst, resourceName);\r
- treeRootEntry = Rm_osalMalloc(sizeof(Rm_ResourceTree));\r
- RB_INIT(treeRootEntry);\r
-\r
- while (range != NULL) {\r
- treeNode = Rm_newResourceNode(range->base, range->length);\r
- collidingNode = RB_INSERT(_Rm_ResourceTree, treeRootEntry, treeNode);\r
-\r
- if (collidingNode) {\r
- Rm_ResourceNode *nextNode = NULL;\r
- \r
- /* Node that was inserted collides with existing node. Destroy tree and return error */\r
- for (treeNode = RB_MIN(_Rm_ResourceTree, treeRootEntry); treeNode != NULL; treeNode = nextNode) {\r
- nextNode = RB_NEXT(_Rm_ResourceTree, treeRootEntry, treeNode);\r
- RB_REMOVE(_Rm_ResourceTree, treeRootEntry, nextNode);\r
- Rm_freeResourceNode(treeNode);\r
- }\r
- Rm_osalFree((void *)treeRootEntry, sizeof(Rm_ResourceTree));\r
- Rm_allocatorDelete(rmInst, allocator->resourceName);\r
- return (-24); /* TODO FIX RETURN */\r
- }\r
- range = range->nextRange;\r
- }\r
- \r
- allocator->allocatorRootEntry = treeRootEntry;\r
- return(0); /* TODO: FIX THIS RETURN */\r
-}\r
-\r
-/* Called when an allocate request is made but the base is unspecified. RM must preallocate\r
- * resources which then must be checked against the RM policy for the instance. If the\r
- * policy does not agree another resource(s) must be preallocated and tested against the \r
- * policy. Policy will provide initialize the preallocate with the base that it allows\r
- * for the rm instance for the specified resource. */\r
-int32_t Rm_treePreAllocate(Rm_Inst *rmInst, Rm_Allocator *allocator, int32_t resourcePolicy,\r
- Rm_AllocatorOpInfo *opInfo)\r
-{\r
- Rm_ResourceNode findNode;\r
- Rm_ResourceNode *matchingNode = NULL;\r
- uint32_t matchingEnd;\r
- uint32_t rangeIndex;\r
- bool resourceFound = FALSE;\r
- Rm_PolicyCheckType policyCheckType;\r
- Rm_PolicyCheckCfg policyCheckCfg;\r
- bool nodePassesPolicy;\r
- int32_t retVal = RM_SERVICE_PROCESSING; \r
-\r
- opInfo->resourceInfo->base = Rm_policyGetResourceBase(rmInst->policy, opInfo->serviceSrcInstNode, \r
- resourcePolicy, opInfo->allocType, \r
- &retVal);\r
- if (retVal != RM_SERVICE_PROCESSING) {\r
- return (retVal);\r
- }\r
-\r
- if (opInfo->resourceInfo->alignment == RM_RESOURCE_ALIGNMENT_UNSPECIFIED) { \r
- /* Get alignment from policy */\r
- opInfo->resourceInfo->alignment = Rm_policyGetResourceAlignment(rmInst->policy, resourcePolicy);\r
- }\r
- \r
- if (opInfo->resourceInfo->alignment == 0) {\r
- opInfo->resourceInfo->alignment = 1;\r
- } \r
-\r
- memset((void *)&findNode, 0, sizeof(Rm_ResourceNode));\r
- findNode.base = opInfo->resourceInfo->base;\r
- findNode.length = opInfo->resourceInfo->length;\r
-\r
- /* Configure policy checking structure */\r
- memset((void *)&policyCheckCfg, 0, sizeof(Rm_PolicyCheckCfg));\r
- if (RM_POLICY_GET_PERM(opInfo->allocType, RM_POLICY_PERM_INIT_SHIFT)) {\r
- policyCheckType = Rm_policyCheck_INIT;\r
- }\r
- else if (RM_POLICY_GET_PERM(opInfo->allocType, RM_POLICY_PERM_USE_SHIFT)) {\r
- policyCheckType = Rm_policyCheck_USE;\r
- }\r
- policyCheckCfg.policyDtb = rmInst->policy;\r
- policyCheckCfg.resourcePolicy = resourcePolicy;\r
- \r
- do {\r
- matchingNode = RB_FIND(_Rm_ResourceTree, allocator->allocatorRootEntry, &findNode);\r
- \r
- if (matchingNode) {\r
- nodePassesPolicy = FALSE;\r
- policyCheckCfg.type = policyCheckType;\r
- policyCheckCfg.validInstNode = opInfo->serviceSrcInstNode;\r
- policyCheckCfg.resourceBase = findNode.base;\r
- policyCheckCfg.resourceLength = findNode.length;\r
- nodePassesPolicy = Rm_policyCheckPrivilege(&policyCheckCfg, &retVal); \r
- \r
- if (nodePassesPolicy && (matchingNode->allocationCount > 0)) {\r
- /* Check exclusive privileges of instance requesting resource. Requesting\r
- * instance with exclusive privileges can't reserve resource if already owned*/\r
- policyCheckCfg.type = Rm_policyCheck_EXCLUSIVE;\r
- policyCheckCfg.validInstNode = opInfo->serviceSrcInstNode;\r
- nodePassesPolicy = !Rm_policyCheckPrivilege(&policyCheckCfg, &retVal);\r
- }\r
- \r
- if (nodePassesPolicy && (matchingNode->allocationCount == 1)) {\r
- /* Check exclusive privileges of instance that currently owns resource */\r
- policyCheckCfg.type = Rm_policyCheck_EXCLUSIVE;\r
- policyCheckCfg.validInstNode = matchingNode->allocatedTo->instNameNode;\r
- nodePassesPolicy = !Rm_policyCheckPrivilege(&policyCheckCfg, &retVal);\r
- }\r
-\r
- if (retVal != RM_SERVICE_PROCESSING) {\r
- break;\r
- }\r
-\r
- if (nodePassesPolicy) {\r
- matchingEnd = matchingNode->base + matchingNode->length - 1;\r
- /* Initialize indexer to be first resource value that alignment */\r
- rangeIndex = findNode.base;\r
- if (rangeIndex % opInfo->resourceInfo->alignment) {\r
- rangeIndex += (opInfo->resourceInfo->alignment -\r
- (rangeIndex % opInfo->resourceInfo->alignment));\r
- }\r
- \r
- if ((rangeIndex + opInfo->resourceInfo->length - 1) <= matchingEnd) {\r
- /* Block of unallocated resources within matchingNode that satisfies\r
- * allocate requirements */\r
- opInfo->resourceInfo->base = rangeIndex;\r
- resourceFound = TRUE;\r
- } \r
- }\r
- \r
- if (!resourceFound) {\r
- /* Check next resource node for available resources */\r
- findNode.base = matchingNode->base + matchingNode->length;\r
- }\r
- }\r
- else {\r
- retVal = RM_SERVICE_DENIED_RESOURCE_ALLOCATION_REQUIREMENTS_COULD_NOT_BE_SATISFIED;\r
- }\r
- } while ((!resourceFound) && \r
- (retVal != RM_SERVICE_DENIED_RESOURCE_ALLOCATION_REQUIREMENTS_COULD_NOT_BE_SATISFIED));\r
-\r
- return(retVal); \r
-}\r
-\r
-int32_t Rm_treeAllocate(Rm_Inst *rmInst, Rm_Allocator *allocator, int32_t resourcePolicy,\r
- Rm_AllocatorOpInfo *opInfo)\r
-{\r
- Rm_ResourceNode findNode;\r
- Rm_ResourceNode *matchingNode = NULL;\r
- Rm_ResourceNode *leftNode = NULL;\r
- Rm_ResourceNode *rightNode = NULL;\r
- Rm_PolicyCheckType policyCheckType; \r
- Rm_PolicyCheckCfg policyCheckCfg;\r
- bool allocPassesPolicy;\r
- bool combineLeft = FALSE;\r
- bool combineRight = FALSE; \r
- uint32_t findEnd;\r
- uint32_t matchingEnd; \r
- int32_t retVal = RM_SERVICE_PROCESSING;\r
-\r
- memset((void *)&findNode, 0, sizeof(Rm_ResourceNode));\r
- findNode.base = opInfo->resourceInfo->base;\r
- findNode.length = opInfo->resourceInfo->length;\r
- matchingNode = RB_FIND(_Rm_ResourceTree, allocator->allocatorRootEntry, &findNode);\r
-\r
- /* Prepare privilege checks */\r
- memset((void *)&policyCheckCfg, 0, sizeof(Rm_PolicyCheckCfg));\r
- if (RM_POLICY_GET_PERM(opInfo->allocType, RM_POLICY_PERM_INIT_SHIFT)) {\r
- policyCheckType = Rm_policyCheck_INIT;\r
- }\r
- else if (RM_POLICY_GET_PERM(opInfo->allocType, RM_POLICY_PERM_USE_SHIFT)) {\r
- policyCheckType = Rm_policyCheck_USE;\r
- }\r
-\r
- if (matchingNode) {\r
- findEnd = findNode.base + findNode.length - 1;\r
- matchingEnd = matchingNode->base + matchingNode->length - 1;\r
- \r
- if ((findNode.base >= matchingNode->base) && (findEnd <= matchingEnd)) {\r
- if (opInfo->serviceSrcInstNode == Rm_policyGetLinuxInstNode(rmInst->validInstances)) {\r
- /* Bypass policy checks since Linux Kernel has full privileges */\r
- allocPassesPolicy = TRUE;\r
- }\r
- else {\r
- allocPassesPolicy = FALSE;\r
- policyCheckCfg.policyDtb = rmInst->policy;\r
- policyCheckCfg.resourcePolicy = resourcePolicy; \r
- policyCheckCfg.type = policyCheckType;\r
- policyCheckCfg.validInstNode = opInfo->serviceSrcInstNode;\r
- policyCheckCfg.resourceBase = findNode.base;\r
- policyCheckCfg.resourceLength = findNode.length;\r
- allocPassesPolicy = Rm_policyCheckPrivilege(&policyCheckCfg, &retVal);\r
- if (!allocPassesPolicy) {\r
- retVal = RM_SERVICE_DENIED_INIT_USE_PERMISSION_DENIED;\r
- }\r
- \r
- if (allocPassesPolicy && (matchingNode->allocationCount > 0)) {\r
- /* Check exclusive privileges of instance requesting resource. Requesting\r
- * instance with exclusive privileges can't reserve resource if already owned*/\r
- policyCheckCfg.type = Rm_policyCheck_EXCLUSIVE;\r
- policyCheckCfg.validInstNode = opInfo->serviceSrcInstNode;\r
- allocPassesPolicy = !Rm_policyCheckPrivilege(&policyCheckCfg, &retVal);\r
- if (!allocPassesPolicy) {\r
- retVal = RM_SERVICE_DENIED_REQUESTER_HAS_EXCLUSIVE_PRIV_BUT_RESOURCE_ALLOCATED;\r
- }\r
- }\r
- if (allocPassesPolicy && (matchingNode->allocationCount == 1)) {\r
- /* Check exclusive privileges of instance that currently owns resource */\r
- policyCheckCfg.type = Rm_policyCheck_EXCLUSIVE;\r
- policyCheckCfg.validInstNode = matchingNode->allocatedTo->instNameNode;\r
- allocPassesPolicy = !Rm_policyCheckPrivilege(&policyCheckCfg, &retVal);\r
- if (!allocPassesPolicy) {\r
- retVal = RM_SERVICE_DENIED_RESOURCE_ALLOCATED_TO_INSTANCE_WITH_EXCLUSIVE_PRIV;\r
- } \r
- } \r
- }\r
- \r
- if (allocPassesPolicy) { \r
- if ((findNode.base == matchingNode->base) && (findEnd == matchingEnd)) {\r
- /* findNode range matches matchingNode range\r
- *\r
- * |<--left node-->||<--matched node-->||<--right node-->| => existing node\r
- * |<--alloc request-->| => requested resources\r
- */ \r
- leftNode = RB_PREV(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- rightNode = RB_NEXT(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- Rm_addAllocatedTo(matchingNode, opInfo->serviceSrcInstNode);\r
-\r
- if (leftNode && Rm_compareResourceNodeAllocations(leftNode, matchingNode)) {\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, leftNode);\r
- combineLeft = TRUE;\r
- }\r
- if (rightNode && Rm_compareResourceNodeAllocations(rightNode, matchingNode)) {\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, rightNode);\r
- combineRight = TRUE;\r
- }\r
-\r
- if (combineLeft && combineRight) {\r
- /* Combine all three nodes into matchingNode */\r
- matchingNode->base = leftNode->base;\r
- matchingNode->length = leftNode->length + matchingNode->length + rightNode->length;\r
-\r
- Rm_clearAllocatedTo(leftNode);\r
- Rm_freeResourceNode(leftNode);\r
- Rm_clearAllocatedTo(rightNode);\r
- Rm_freeResourceNode(rightNode); \r
- }\r
- else if (combineLeft) {\r
- /* Combine left and matching nodes. Reinsert right. */\r
- matchingNode->base = leftNode->base;\r
- matchingNode->length += leftNode->length;\r
-\r
- Rm_clearAllocatedTo(leftNode);\r
- Rm_freeResourceNode(leftNode);\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, rightNode); \r
- }\r
- else if (combineRight) {\r
- /* Combine right and matching nodes. Reinsert left. */\r
- matchingNode->length += rightNode->length;\r
-\r
- Rm_clearAllocatedTo(rightNode);\r
- Rm_freeResourceNode(rightNode);\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, leftNode);\r
- }\r
- else {\r
- /* No combine. */\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, leftNode);\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, rightNode);\r
- }\r
-\r
- /* Always reinsert matchingNode */ \r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode); \r
- } \r
- else if ((findNode.base > matchingNode->base) && (findEnd < matchingEnd)) {\r
- /* findNode range is subset of matchingNode range and neither boundary is\r
- * equivalent.\r
- *\r
- * |<----------matched node---------->|\r
- * |<---alloc request--->|\r
- */ \r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- leftNode = Rm_newResourceNode(matchingNode->base, findNode.base - matchingNode->base);\r
- Rm_copyAllocatedTo(leftNode, matchingNode);\r
- rightNode = Rm_newResourceNode(findNode.base + findNode.length, matchingEnd - findEnd);\r
- Rm_copyAllocatedTo(rightNode, matchingNode);\r
-\r
- matchingNode->base = findNode.base; \r
- matchingNode->length = findNode.length;\r
- Rm_addAllocatedTo(matchingNode, opInfo->serviceSrcInstNode);\r
-\r
- /* Insert all the nodes */\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, leftNode);\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, rightNode);\r
- } \r
- else { \r
- if (findNode.base == matchingNode->base) {\r
- /* findNode base and matchingNode base are equivalent. May be combine\r
- * possibilities to the left\r
- *\r
- * |<---left node (alloc'd)--->||<----------matched node---------->|\r
- * |<---findNode (alloc req)--->|\r
- */ \r
- leftNode = RB_PREV(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- /* Add allocating instance to allocatedTo list for compare with leftNode */\r
- Rm_addAllocatedTo(matchingNode, opInfo->serviceSrcInstNode);\r
- \r
- if (leftNode && Rm_compareResourceNodeAllocations(leftNode, matchingNode)) {\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, leftNode);\r
- /* Combine leftNode and findNode */\r
- leftNode->length += findNode.length;\r
- }\r
- else {\r
- leftNode = Rm_newResourceNode(findNode.base, findNode.length);\r
- Rm_copyAllocatedTo(leftNode, matchingNode);\r
- }\r
-\r
- /* Account for leftNode in matchingNode */\r
- matchingNode->base = findNode.base + findNode.length;\r
- matchingNode->length = matchingEnd - findEnd; \r
-\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, leftNode);\r
- }\r
- else if (findEnd == matchingEnd) {\r
- /* findNode end and matchingNode end are equivalent. May be combine\r
- * possibilities to the right\r
- *\r
- * |<----------matched node---------->||<---right node (alloc'd)--->|\r
- * |<---findNode (alloc req)--->| \r
- */ \r
- rightNode = RB_NEXT(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- /* Add allocating instance to allocatedTo list for compare with rightNode */\r
- Rm_addAllocatedTo(matchingNode, opInfo->serviceSrcInstNode);\r
- \r
- if (rightNode && Rm_compareResourceNodeAllocations(rightNode, matchingNode)) {\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, rightNode);\r
- /* Combine rightNode and findNode */\r
- rightNode->base = findNode.base;\r
- rightNode->length += findNode.length;\r
- }\r
- else {\r
- rightNode = Rm_newResourceNode(findNode.base, findNode.length);\r
- Rm_copyAllocatedTo(rightNode, matchingNode);\r
- }\r
-\r
- /* Account for rightNode in matchingNode */\r
- matchingNode->length -= findNode.length; \r
-\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, rightNode);\r
- }\r
- /* Remove allocating instance from leftover matchingNode */\r
- Rm_deleteAllocatedTo(matchingNode, opInfo->serviceSrcInstNode);\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- }\r
- retVal = RM_SERVICE_APPROVED_AND_COMPLETED;\r
- }\r
- }\r
- else {\r
- retVal = RM_SERVICE_DENIED_RESOURCE_ALREADY_ALLOCATED;\r
- }\r
- }\r
- else {\r
- retVal = RM_SERVICE_DENIED_RESOURCE_VALUE_RANGE_DOES_NOT_EXIST;\r
- }\r
-\r
- return(retVal); \r
-}\r
-\r
-int32_t Rm_treeFree(Rm_Inst *rmInst, Rm_Allocator *allocator, int32_t resourcePolicy, Rm_AllocatorOpInfo *opInfo)\r
-{\r
- Rm_ResourceNode findNode;\r
- Rm_ResourceNode *matchingNode = NULL;\r
- Rm_ResourceNode *leftNode = NULL;\r
- Rm_ResourceNode *rightNode = NULL;\r
- bool combineLeft = FALSE;\r
- bool combineRight = FALSE;\r
- uint32_t findEnd;\r
- uint32_t matchingEnd;\r
- int32_t retVal;\r
-\r
- memset((void *)&findNode, 0, sizeof(Rm_ResourceNode));\r
- findNode.base = opInfo->resourceInfo->base;\r
- findNode.length = opInfo->resourceInfo->length;\r
- matchingNode = RB_FIND(_Rm_ResourceTree, allocator->allocatorRootEntry, &findNode);\r
-\r
- if (matchingNode) {\r
- findEnd = findNode.base + findNode.length - 1;\r
- matchingEnd = matchingNode->base + matchingNode->length - 1;\r
- \r
- if ((findNode.base >= matchingNode->base) && (findEnd <= matchingEnd)) { \r
- if (matchingNode->allocationCount) {\r
- if (Rm_isAllocatedTo(matchingNode, opInfo->serviceSrcInstNode)) {\r
- if ((findNode.base == matchingNode->base) && (findEnd == matchingEnd))\r
- {\r
- /* Case 1: Free range equals allocated matched node exactly. Attempt to combine \r
- * freed node with nodes to left and right.\r
- *\r
- * |<--left node-->||<---matched node--->||<--right node-->|\r
- * |<---free request--->|\r
- */ \r
- leftNode = RB_PREV(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- rightNode = RB_NEXT(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- Rm_deleteAllocatedTo(matchingNode, opInfo->serviceSrcInstNode);\r
-\r
- if (leftNode && Rm_compareResourceNodeAllocations(leftNode, matchingNode)) {\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, leftNode);\r
- combineLeft = TRUE;\r
- }\r
- if (rightNode && Rm_compareResourceNodeAllocations(rightNode, matchingNode)) {\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, rightNode);\r
- combineRight = TRUE;\r
- }\r
-\r
- if (combineLeft && combineRight) {\r
- /* Combine all three nodes into matchingNode */\r
- matchingNode->base = leftNode->base;\r
- matchingNode->length = leftNode->length + matchingNode->length + rightNode->length;\r
-\r
- Rm_clearAllocatedTo(leftNode);\r
- Rm_freeResourceNode(leftNode);\r
- Rm_clearAllocatedTo(rightNode);\r
- Rm_freeResourceNode(rightNode); \r
- }\r
- else if (combineLeft) {\r
- /* Combine left and matching nodes. Reinsert right. */\r
- matchingNode->base = leftNode->base;\r
- matchingNode->length += leftNode->length;\r
-\r
- Rm_clearAllocatedTo(leftNode);\r
- Rm_freeResourceNode(leftNode);\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, rightNode); \r
- }\r
- else if (combineRight) {\r
- /* Combine right and matching nodes. Reinsert left. */\r
- matchingNode->length += rightNode->length;\r
-\r
- Rm_clearAllocatedTo(rightNode);\r
- Rm_freeResourceNode(rightNode);\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, leftNode);\r
- }\r
- else {\r
- /* No combine. */\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, leftNode);\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, rightNode);\r
- }\r
-\r
- /* Always reinsert matchingNode */\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode); \r
- }\r
- else if ((findNode.base > matchingNode->base) && (findEnd < matchingEnd)) {\r
- /* Case 2: Free range is less than range in matched node. Split\r
- * matched node into three nodes.\r
- *\r
- * |<----------matched node---------->|\r
- * |<---free request--->|\r
- *\r
- * Remove instance from AllocatedTo list then add it back in for side nodes for\r
- * proper accounting of allocations in validInstance list\r
- */ \r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- Rm_deleteAllocatedTo(matchingNode, opInfo->serviceSrcInstNode);\r
- \r
- leftNode = Rm_newResourceNode(matchingNode->base, findNode.base - matchingNode->base);\r
- Rm_copyAllocatedTo(leftNode, matchingNode);\r
- Rm_addAllocatedTo(leftNode, opInfo->serviceSrcInstNode);\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, leftNode);\r
- \r
- rightNode = Rm_newResourceNode(findNode.base + findNode.length, matchingEnd - findEnd);\r
- Rm_copyAllocatedTo(rightNode, matchingNode);\r
- Rm_addAllocatedTo(rightNode, opInfo->serviceSrcInstNode);\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, rightNode);\r
-\r
- matchingNode->base = findNode.base; \r
- matchingNode->length = findNode.length;\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- }\r
- else { \r
- if (findNode.base == matchingNode->base) {\r
- /* Case 3: Free range is on left boundary of matched node. Try to \r
- * combine free range with left node.\r
- *\r
- * |<---left node (free)--->||<----------matched node---------->|\r
- * |<---findNode (free req)--->|\r
- */ \r
-\r
- leftNode = RB_PREV(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- /* Remove freeing instance from allocatedTo list for compare with leftNode */\r
- Rm_deleteAllocatedTo(matchingNode, opInfo->serviceSrcInstNode);\r
- \r
- if (leftNode && Rm_compareResourceNodeAllocations(leftNode, matchingNode)) {\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, leftNode);\r
- /* Combine leftNode and findNode */\r
- leftNode->length += findNode.length;\r
- }\r
- else {\r
- leftNode = Rm_newResourceNode(findNode.base, findNode.length);\r
- Rm_copyAllocatedTo(leftNode, matchingNode);\r
- }\r
-\r
- /* Remove leftNode range from matchingNode */\r
- matchingNode->base = findNode.base + findNode.length;\r
- matchingNode->length = matchingEnd - findEnd; \r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, leftNode);\r
- }\r
- else if (findEnd == matchingEnd) {\r
- /* Case 4: Free range is on right boundary of matched node. Try to \r
- * combine free range with right node.\r
- *\r
- * |<----------matched node---------->||<---right node (free)--->|\r
- * |<---findNode (free req)--->|\r
- */ \r
- \r
- rightNode = RB_NEXT(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode); \r
- /* Remove freeing instance from allocatedTo list for compare with rightNode */\r
- Rm_deleteAllocatedTo(matchingNode, opInfo->serviceSrcInstNode);\r
- \r
- if (rightNode && Rm_compareResourceNodeAllocations(rightNode, matchingNode)) {\r
- RB_REMOVE(_Rm_ResourceTree, allocator->allocatorRootEntry, rightNode);\r
- /* Combine rightNode and findNode */\r
- rightNode->base = findNode.base;\r
- rightNode->length += findNode.length;\r
- }\r
- else {\r
- rightNode = Rm_newResourceNode(findNode.base, findNode.length);\r
- Rm_copyAllocatedTo(rightNode, matchingNode);\r
- }\r
-\r
- /* Remove rightNode range from matchingNode */\r
- matchingNode->length -= findNode.length; \r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, rightNode);\r
- }\r
-\r
- /* Add freeing instance back into matchingNode allocations */\r
- Rm_addAllocatedTo(matchingNode, opInfo->serviceSrcInstNode);\r
- RB_INSERT(_Rm_ResourceTree, allocator->allocatorRootEntry, matchingNode);\r
- }\r
-\r
- retVal = RM_SERVICE_APPROVED_AND_COMPLETED;\r
- }\r
- else {\r
- retVal = RM_SERVICE_DENIED_RESOURCE_NOT_ALLOCATED_TO_INSTANCE_REQUESTING_THE_SERVICE;\r
- }\r
- }\r
- else {\r
- retVal = RM_SERVICE_DENIED_RESOURCE_ALREADY_FREE;\r
- }\r
- }\r
- else {\r
- retVal = RM_SERVICE_DENIED_INVALID_RESOURCE_RANGE;\r
- }\r
- }\r
- else {\r
- retVal = RM_SERVICE_DENIED_RESOURCE_VALUE_RANGE_DOES_NOT_EXIST;\r
- }\r
- return(retVal); \r
-}\r
-\r
-int32_t Rm_allocatorOperation(Rm_Inst *rmInst, Rm_AllocatorOpInfo *opInfo)\r
-{\r
- Rm_Allocator *allocator = NULL;\r
- int32_t resourcePolicy = NULL;\r
- int32_t retVal;\r
- void *key;\r
-\r
- /* Lock access to the RM instance's transaction queue */\r
- key = Rm_osalMtCsEnter();\r
- \r
- resourcePolicy = Rm_policyGetResourcePolicy(rmInst->policy, opInfo->resourceInfo->name);\r
- allocator = Rm_allocatorFind(rmInst, opInfo->resourceInfo->name);\r
- \r
- if ((resourcePolicy > 0) && allocator) {\r
- if (opInfo->operation == Rm_allocatorOp_PRE_ALLOCATE) {\r
- retVal = Rm_treePreAllocate(rmInst, allocator, resourcePolicy, opInfo);\r
- } \r
- else if (opInfo->operation == Rm_allocatorOp_ALLOCATE) {\r
- retVal = Rm_treeAllocate(rmInst, allocator, resourcePolicy, opInfo);\r
- }\r
- else if (opInfo->operation == Rm_allocatorOp_FREE) {\r
- retVal = Rm_treeFree(rmInst, allocator, resourcePolicy, opInfo);\r
- } \r
- }\r
- else {\r
- /* Resource could not be found in policy and/or allocator */\r
- retVal = RM_SERVICE_DENIED_RESOURCE_DOES_NOT_EXIST;\r
- }\r
-\r
- Rm_osalMtCsExit(key);\r
- return(retVal);\r
-}\r
-\r
-void Rm_allocationHandler (Rm_Inst *rmInst, Rm_Transaction *transaction, void *validInstNode,\r
- uint32_t allocType)\r
-{\r
- Rm_AllocatorOpInfo opInfo;\r
- int32_t retVal = transaction->state;\r
-\r
- memset((void *)&opInfo, 0, sizeof(Rm_AllocatorOpInfo));\r
- \r
- if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {\r
- /* TEMP: For now forward all allocations to the RM Server */\r
- Rm_transactionForwarder(rmInst, transaction);\r
- \r
- }\r
- else if (rmInst->instType == Rm_instType_SERVER) {\r
- opInfo.resourceInfo = &transaction->resourceInfo;\r
- opInfo.serviceSrcInstNode = validInstNode;\r
- opInfo.allocType = allocType;\r
-\r
- if (strlen(transaction->resourceInfo.nsName) > 0) {\r
- if (transaction->resourceInfo.base != 0) {\r
- /* Both NameServer name and static value cannot be specified for the request */\r
- retVal = RM_SERVICE_ERROR_NAMESERVER_NAME_AND_RESOURCE_RANGE_BOTH_DEFINED;\r
- }\r
- else {\r
- retVal = Rm_nsFindObject(rmInst, opInfo.resourceInfo);\r
- }\r
- }\r
-\r
- if (retVal == RM_SERVICE_PROCESSING) { \r
- if (transaction->resourceInfo.base == RM_RESOURCE_BASE_UNSPECIFIED) {\r
- opInfo.operation = Rm_allocatorOp_PRE_ALLOCATE;\r
- retVal = Rm_allocatorOperation(rmInst, &opInfo);\r
- }\r
- \r
- if (retVal == RM_SERVICE_PROCESSING) {\r
- opInfo.operation = Rm_allocatorOp_ALLOCATE;\r
- retVal = Rm_allocatorOperation(rmInst, &opInfo);\r
- } \r
- }\r
- \r
- transaction->state = retVal;\r
-\r
- if (strcmp(transaction->serviceSrcInstName, rmInst->name)) {\r
- /* Source of allocation was not the server instance, provide the transaction\r
- * to the transaction responder */\r
- Rm_transactionResponder(rmInst, transaction);\r
- }\r
- /* Otherwise let the return stack return the transaction to the serviceHandler */ \r
- } \r
-}\r
-\r
-void Rm_freeHandler (Rm_Inst *rmInst, Rm_Transaction *transaction, void *validInstNode)\r
-{\r
- Rm_AllocatorOpInfo opInfo; \r
- int32_t retVal = transaction->state;\r
-\r
- memset((void *)&opInfo, 0, sizeof(Rm_AllocatorOpInfo));\r
- \r
- if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {\r
- /* TEMP: Forward all free requests to the Server */\r
- Rm_transactionForwarder(rmInst, transaction); \r
- }\r
- else if (rmInst->instType == Rm_instType_SERVER) {\r
- opInfo.resourceInfo = &transaction->resourceInfo;\r
- opInfo.serviceSrcInstNode = validInstNode;\r
-\r
- if (strlen(transaction->resourceInfo.nsName) > 0) {\r
- if (transaction->resourceInfo.base != 0) {\r
- /* Both a name and a value cannot be specified for the request */\r
- retVal = RM_SERVICE_ERROR_NAMESERVER_NAME_AND_RESOURCE_RANGE_BOTH_DEFINED;\r
- }\r
- else {\r
- retVal = Rm_nsFindObject(rmInst, opInfo.resourceInfo);\r
- }\r
- }\r
- \r
- if(retVal == RM_SERVICE_PROCESSING) { \r
- opInfo.operation = Rm_allocatorOp_FREE;\r
- retVal = Rm_allocatorOperation(rmInst, &opInfo);\r
- } \r
-\r
- transaction->state = retVal;\r
-\r
- if (strcmp(transaction->serviceSrcInstName, rmInst->name)) {\r
- /* Source of allocation was not the server instance, provide the transaction\r
- * to the transaction responder */\r
- Rm_transactionResponder(rmInst, transaction);\r
- }\r
- /* Otherwise let the return stack return the transaction to the serviceHandler */ \r
- } \r
-}\r
-\r
-/* Function used to send RM response transactions to lower level agents */\r
-void Rm_transactionResponder (Rm_Inst *rmInst, Rm_Transaction *transaction)\r
-{\r
- Rm_TransportNode *dstTransportNode = NULL;\r
- Rm_Packet *rmPkt = NULL;\r
-\r
- /* Find the transport for the RM instance that sent the request. */\r
- dstTransportNode = Rm_transportNodeFindRemoteName(rmInst, transaction->pktSrcInstName);\r
-\r
- /* Create a RM packet using the service information */\r
- switch (transaction->type) {\r
- case Rm_service_RESOURCE_ALLOCATE_INIT:\r
- case Rm_service_RESOURCE_ALLOCATE_USE:\r
- case Rm_service_RESOURCE_FREE:\r
- case Rm_service_RESOURCE_GET_BY_NAME:\r
- rmPkt = Rm_transportCreateResourceResponsePkt(rmInst, dstTransportNode, \r
- transaction);\r
- break;\r
- case Rm_service_RESOURCE_MAP_TO_NAME:\r
- case Rm_service_RESOURCE_UNMAP_NAME:\r
- rmPkt = Rm_transportCreateNsResponsePkt(rmInst, dstTransportNode,\r
- transaction);\r
- break;\r
- default:\r
- /* Invalid service type. Flag the error and return */\r
- transaction->state = RM_SERVICE_ERROR_INVALID_SERVICE_TYPE;\r
- break;\r
- }\r
-\r
- if (transaction->state <= RM_SERVICE_ERROR_BASE) {\r
- /* Delete the transaction and return immediately because an error occurred \r
- * allocating the packet */\r
- Rm_transactionQueueDelete(rmInst, transaction->localId);\r
- return;\r
- }\r
-\r
- /* Send the RM packet to the application transport */\r
- if (rmInst->transport.rmSend((Rm_TransportHandle) dstTransportNode, rmPkt) < RM_TRANSPORT_SUCCESSFUL) {\r
- /* Negative value returned by transport send. An error occurred\r
- * in the transport while attempting to send the packet.*/\r
- transaction->state = RM_SERVICE_ERROR_TRANPSPORT_SEND_ERROR;\r
- /* Clean up the packet */\r
- if (rmInst->transport.rmFreePkt((Rm_TransportHandle) dstTransportNode, rmPkt)) {\r
- /* Non-NULL value returned by transport packet free. Flag the\r
- * error */\r
- transaction->state = RM_SERVICE_ERROR_TRANSPORT_FREE_PKT_ERROR;\r
- }\r
- return;\r
- }\r
-\r
- /* NEED TO DO SOMETHING IF GET AN ERROR IN THE transaction->state FIELD. CREATE\r
- * NEW TRANSACTION WITH DATA FROM ORIGINAL? THEN TRY TO SEND FAILED REQUEST BACK\r
- * TO REQUESTER??? KEEP RETRYING SEND OF RESPONSE??? */\r
-\r
- /* Delete the transaction */\r
- Rm_transactionQueueDelete(rmInst, transaction->localId);\r
-}\r
-\r
-/* Function used to forward RM transactions to higher level agents */\r
-void Rm_transactionForwarder (Rm_Inst *rmInst, Rm_Transaction *transaction)\r
-{\r
- Rm_TransportNode *dstTransportNode = NULL;\r
- Rm_Packet *rmPkt = NULL;\r
-\r
- /* Make sure the RM instance has a transport registered with a higher level agent */\r
- if (rmInst->registeredWithDelegateOrServer == false) {\r
- transaction->state = RM_SERVICE_ERROR_NOT_REGISTERED_WITH_DEL_OR_SERVER;\r
- return;\r
- }\r
-\r
- /* Find the transport for the higher level agent. Check for a connection to a Client Delegate\r
- * or a Server. Clients will be connected to either a Client Delegate or a Server. Client\r
- * Delegates will be connected to a Server. */\r
- if (rmInst->instType == Rm_instType_CLIENT) {\r
- dstTransportNode = Rm_transportNodeFindRemoteInstType(rmInst, Rm_instType_CLIENT_DELEGATE);\r
-\r
- if (!dstTransportNode) {\r
- /* No Client Delegate connection found. Check for a Server connection */\r
- dstTransportNode = Rm_transportNodeFindRemoteInstType(rmInst, Rm_instType_SERVER);\r
- }\r
- } \r
- else if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {\r
- dstTransportNode = Rm_transportNodeFindRemoteInstType(rmInst, Rm_instType_SERVER);\r
- }\r
-\r
- /* Create a RM packet using the service information */\r
- switch (transaction->type) {\r
- case Rm_service_RESOURCE_ALLOCATE_INIT:\r
- case Rm_service_RESOURCE_ALLOCATE_USE:\r
- case Rm_service_RESOURCE_FREE:\r
- case Rm_service_RESOURCE_GET_BY_NAME:\r
- rmPkt = Rm_transportCreateResourceReqPkt(rmInst, dstTransportNode, \r
- transaction);\r
- break;\r
- case Rm_service_RESOURCE_MAP_TO_NAME:\r
- case Rm_service_RESOURCE_UNMAP_NAME:\r
- rmPkt = Rm_transportCreateNsRequestPkt(rmInst, dstTransportNode,\r
- transaction);\r
- break;\r
- default:\r
- /* Invalid service type. Flag the error and return */\r
- transaction->state = RM_SERVICE_ERROR_INVALID_SERVICE_TYPE;\r
- break;\r
- }\r
-\r
- if (transaction->state <= RM_SERVICE_ERROR_BASE) {\r
- /* Return immediately because an error occurred allocating the packet */\r
- return;\r
- }\r
-\r
- /* Send the RM packet to the application transport */\r
- if (rmInst->transport.rmSend((Rm_TransportHandle) dstTransportNode, rmPkt) < RM_TRANSPORT_SUCCESSFUL) {\r
- /* Negative value returned by transport send. An error occurred\r
- * in the transport while attempting to send the packet.*/\r
- transaction->state = RM_SERVICE_ERROR_TRANPSPORT_SEND_ERROR;\r
- /* Clean up the packet */\r
- if (rmInst->transport.rmFreePkt((Rm_TransportHandle) dstTransportNode, rmPkt)) {\r
- /* Non-NULL value returned by transport packet free. Flag the\r
- * error */\r
- transaction->state = RM_SERVICE_ERROR_TRANSPORT_FREE_PKT_ERROR;\r
- }\r
- return;\r
- }\r
-\r
- /* Transaction is not deleted because it is awaiting a response from the higher level\r
- * RM instance */\r
-}\r
-\r
-void Rm_transactionProcessor (Rm_Inst *rmInst, Rm_Transaction *transaction)\r
-{\r
- void *validInstNode;\r
- uint32_t allocType = 0;\r
- \r
- /* Handle auto-forwarded transactions. These transactions include:\r
- * - All request transactions received on Clients are forwarded to the Client Delegate\r
- * - NameServer requests received on the Client Delegate are forwarded to the Server */\r
- if ((rmInst->instType == Rm_instType_CLIENT) ||\r
- ((rmInst->instType == Rm_instType_CLIENT_DELEGATE) &&\r
- ((transaction->type == Rm_service_RESOURCE_MAP_TO_NAME) ||\r
- (transaction->type == Rm_service_RESOURCE_GET_BY_NAME) ||\r
- (transaction->type == Rm_service_RESOURCE_UNMAP_NAME)))) {\r
- \r
- if (transaction->state != RM_SERVICE_PROCESSING) {\r
- if (strcmp(transaction->serviceSrcInstName, rmInst->name)) {\r
- /* Transaction did not originate on this instance */\r
- Rm_transactionResponder(rmInst, transaction);\r
- }\r
- else {\r
- /* Transaction originated on this instance */\r
- Rm_serviceResponder(rmInst, transaction);\r
- }\r
- }\r
- else {\r
- /* Forward new transaction */\r
- Rm_transactionForwarder(rmInst, transaction);\r
- }\r
- }\r
- else {\r
- /* Validate service's originating instance name */\r
- if (rmInst->instType == Rm_instType_SERVER) {\r
- validInstNode = Rm_policyGetValidInstNode(rmInst->validInstances, transaction->serviceSrcInstName);\r
- if (validInstNode == NULL) {\r
- transaction->state = RM_SERVICE_DENIED_ORIGINATING_INSTANCE_NAME_NOT_VALID;\r
-\r
- /* Send result via responder if transaction did not originate from this instance */\r
- if (strcmp(transaction->serviceSrcInstName, rmInst->name)) {\r
- Rm_transactionResponder(rmInst, transaction);\r
- }\r
- }\r
- }\r
-\r
- switch (transaction->type) {\r
- case Rm_service_RESOURCE_ALLOCATE_INIT:\r
- case Rm_service_RESOURCE_ALLOCATE_USE:\r
- case Rm_service_RESOURCE_FREE: \r
- if (transaction->state != RM_SERVICE_PROCESSING) {\r
- /* Transaction complete */\r
- if (strcmp(transaction->serviceSrcInstName, rmInst->name)) {\r
- /* Transaction result not destined for this instance */\r
- Rm_transactionResponder(rmInst, transaction);\r
- }\r
- else {\r
- /* Transaction result destined for this instance */\r
- Rm_serviceResponder(rmInst, transaction); \r
- }\r
- }\r
- else {\r
- /* Complete allocation/free request */\r
- if (transaction->type == Rm_service_RESOURCE_FREE) {\r
- Rm_freeHandler(rmInst, transaction, validInstNode);\r
- }\r
- else {\r
- switch (transaction->type) {\r
- case Rm_service_RESOURCE_ALLOCATE_INIT:\r
- RM_POLICY_SET_PERM(allocType, RM_POLICY_PERM_INIT_SHIFT, 1);\r
- break;\r
- case Rm_service_RESOURCE_ALLOCATE_USE:\r
- RM_POLICY_SET_PERM(allocType, RM_POLICY_PERM_USE_SHIFT, 1); \r
- break;\r
- }\r
- Rm_allocationHandler(rmInst, transaction, validInstNode, allocType);\r
- }\r
- }\r
- break;\r
- case Rm_service_RESOURCE_MAP_TO_NAME:\r
- case Rm_service_RESOURCE_GET_BY_NAME:\r
- case Rm_service_RESOURCE_UNMAP_NAME: \r
- /* NameServer resides on server */\r
- if (rmInst->instType == Rm_instType_SERVER) {\r
- if (transaction->type == Rm_service_RESOURCE_MAP_TO_NAME) {\r
- transaction->state = Rm_nsAddObject(rmInst, &transaction->resourceInfo);\r
- }\r
- else if (transaction->type == Rm_service_RESOURCE_GET_BY_NAME) {\r
- if (Rm_nsFindObject(rmInst, &transaction->resourceInfo) == RM_SERVICE_PROCESSING) {\r
- transaction->state = RM_SERVICE_APPROVED_AND_COMPLETED;\r
- }\r
- }\r
- else if (transaction->type == Rm_service_RESOURCE_UNMAP_NAME) {\r
- transaction->state = Rm_nsDeleteObject(rmInst, &transaction->resourceInfo);\r
- }\r
-\r
- /* Send result via responder if transaction did not originate from this instance */\r
- if (strcmp(transaction->serviceSrcInstName, rmInst->name)) {\r
- Rm_transactionResponder(rmInst, transaction);\r
- }\r
- }\r
- else {\r
- transaction->state = RM_SERVICE_ERROR_NAMESERVER_OBJECT_MOD_ON_INVALID_INSTANCE;\r
- }\r
- break;\r
- }\r
- }\r
-}\r
-\r
-int32_t Rm_reserveLinuxResource(Rm_Inst *rmInst, Rm_LinuxAlias *linuxAlias, \r
- Rm_LinuxValueRange *linuxValues, Rm_AllocatorOpInfo *opInfo)\r
-{\r
- int32_t retVal = RM_DTB_UTIL_RESULT_OKAY;\r
- bool baseFound = FALSE;\r
- bool lengthFound = FALSE;\r
- uint32_t valueIndex = 0;\r
-\r
- while ((linuxValues != NULL) && (!baseFound || !lengthFound)) {\r
- if (linuxAlias->baseOffset == valueIndex) {\r
- opInfo->resourceInfo->base = linuxValues->value;\r
- baseFound = TRUE;\r
-\r
- if (linuxAlias->lengthOffset == RM_DTB_LINUX_ALIAS_OFFSET_NOT_SET) {\r
- opInfo->resourceInfo->length = 1;\r
- lengthFound = TRUE;\r
- }\r
- }\r
- else if (linuxAlias->lengthOffset == valueIndex) {\r
- opInfo->resourceInfo->length = linuxValues->value;\r
- lengthFound = TRUE;\r
- }\r
-\r
- linuxValues = (Rm_LinuxValueRange *)linuxValues->nextValue;\r
- valueIndex++;\r
- }\r
-\r
- if (!baseFound || !lengthFound) {\r
- retVal = -33; /* TODO: ERROR BASE OR LENGTH OFFSET IN LINUX DTB WAS INCORRECT */\r
- }\r
- else {\r
- /* Allocate resource to Linux */\r
- retVal = Rm_allocatorOperation(rmInst, opInfo);\r
- }\r
- return (retVal);\r
-}\r
-\r
-int32_t Rm_findAndReserveLinuxResource(Rm_Inst *rmInst, const char *resourceName, void *linuxDtb, \r
- Rm_LinuxAlias *linuxAlias)\r
-{\r
- Rm_AllocatorOpInfo opInfo;\r
- Rm_ResourceInfo resourceInfo;\r
- uint32_t pathOffset;\r
- uint32_t pathSize;\r
- char *spacePtr;\r
- int32_t propOffset;\r
- int32_t nodeOffset = RM_DTB_UTIL_STARTING_NODE_OFFSET;\r
- int32_t prevDepth = RM_DTB_UTIL_STARTING_DEPTH;\r
- int32_t depth;\r
- int32_t propertyLen;\r
- const char *propertyName;\r
- const void *propertyData; \r
- Rm_LinuxValueRange *linuxValueRange;\r
- int32_t retVal = RM_DTB_UTIL_RESULT_OKAY; \r
-\r
- memset((void *) &opInfo, 0, sizeof(Rm_AllocatorOpInfo));\r
- memset((void *) &resourceInfo, 0, sizeof(Rm_ResourceInfo));\r
-\r
- strcpy(resourceInfo.name, resourceName);\r
- opInfo.serviceSrcInstNode = Rm_policyGetLinuxInstNode(rmInst->validInstances);\r
- opInfo.operation = Rm_allocatorOp_ALLOCATE;\r
- opInfo.resourceInfo = &resourceInfo; \r
-\r
- while(linuxAlias != NULL) {\r
- /* Reset parsing variables */\r
- pathOffset = 0;\r
- pathSize = strlen(linuxAlias->path) + 1;\r
- nodeOffset = RM_DTB_UTIL_STARTING_NODE_OFFSET;\r
- prevDepth = RM_DTB_UTIL_STARTING_DEPTH; \r
- resourceInfo.base = 0;\r
- resourceInfo.length = 0;\r
-\r
- spacePtr = strpbrk(linuxAlias->path, " ");\r
- if (spacePtr) {\r
- *spacePtr = '\0';\r
- } \r
- \r
- while(pathOffset < pathSize) {\r
- /* Move through DTB nodes until next alias path node found */\r
- if (strcmp(linuxAlias->path + pathOffset, fdt_get_name(linuxDtb, nodeOffset, NULL))) {\r
- nodeOffset = fdt_next_node(linuxDtb, nodeOffset, &depth);\r
-\r
- if ((depth < prevDepth) || (nodeOffset == -FDT_ERR_NOTFOUND)) {\r
- /* Returning from subnode that matched part of alias path without finding\r
- * resource values */\r
- retVal = (-31); /* TODO: COULD NOT FIND RESOURCE AT ALIAS PATH */\r
- break;\r
- }\r
- }\r
- else {\r
- /* Found next alias path node. Move to next node name in path string. */\r
- pathOffset += (strlen(linuxAlias->path + pathOffset) + 1);\r
- spacePtr = strpbrk(linuxAlias->path + pathOffset, " ");\r
- if (spacePtr) {\r
- *spacePtr = '\0';\r
- } \r
- \r
- prevDepth = fdt_node_depth(linuxDtb, nodeOffset);\r
- propOffset = fdt_first_property_offset(linuxDtb, nodeOffset);\r
- while ((propOffset >= RM_DTB_UTIL_STARTING_NODE_OFFSET) &&\r
- (pathOffset < pathSize)) {\r
- propertyData = fdt_getprop_by_offset(linuxDtb, propOffset, \r
- &propertyName, &propertyLen);\r
-\r
- if (strcmp(linuxAlias->path + pathOffset, propertyName) == 0) {\r
- /* Found resource at end of alias path */\r
- pathOffset += (strlen(linuxAlias->path + pathOffset) + 1);\r
- linuxValueRange = Rm_linuxExtractValues(propertyData, propertyLen);\r
- retVal = Rm_reserveLinuxResource(rmInst, linuxAlias, \r
- linuxValueRange, &opInfo);\r
- Rm_linuxFreeValues(linuxValueRange);\r
- }\r
- propOffset = fdt_next_property_offset(linuxDtb, propOffset);\r
- } \r
-\r
- if (propOffset < -FDT_ERR_NOTFOUND) {\r
- retVal = propOffset;\r
- break;\r
- }\r
- }\r
- }\r
-\r
- if (retVal < RM_DTB_UTIL_RESULT_OKAY) {\r
- break;\r
- }\r
- linuxAlias = (Rm_LinuxAlias *) linuxAlias->nextLinuxAlias;\r
- }\r
- return (retVal);\r
-}\r
-\r
-int32_t Rm_createAndInitAllocator(Rm_Inst *rmInst, const char *resourceName, \r
- Rm_ResourceProperties *resourceProperties, void *linuxDtb)\r
-{\r
- Rm_ResourceRange *range = NULL;\r
- Rm_ResourceRange *rangeBasePtr = NULL;\r
- Rm_NsAssignment *nsAssignments = NULL;\r
- Rm_NsAssignment *nsAssignmentBasePtr = NULL;\r
- Rm_LinuxAlias *linuxAlias = NULL;\r
- Rm_ResourceInfo resourceInfo;\r
- int32_t retVal = RM_DTB_UTIL_RESULT_OKAY;\r
-\r
- if (resourceProperties->rangeData && (resourceProperties->rangeLen > 0))\r
- {\r
- /* Extract the resource properties from the DTB */\r
- range = rangeBasePtr = Rm_resourceExtractRange(resourceProperties->rangeData, \r
- resourceProperties->rangeLen);\r
-\r
- /* Create a tree allocator using the resource properties */\r
- retVal = Rm_createTreeAllocator(rmInst, resourceName, range); \r
-\r
- if (retVal >= RM_DTB_UTIL_RESULT_OKAY)\r
- {\r
- if (resourceProperties->linuxAliasData && resourceProperties->linuxAliasLen)\r
- {\r
- /* Reserve the resources taken by the Linux kernel specified in the Linux DTB */\r
- linuxAlias = Rm_resourceExtractLinuxAlias(resourceProperties->linuxAliasData,\r
- resourceProperties->linuxAliasLen);\r
-\r
- retVal = Rm_findAndReserveLinuxResource(rmInst, resourceName, linuxDtb, linuxAlias); \r
- }\r
- }\r
- }\r
- \r
- if (retVal >= RM_DTB_UTIL_RESULT_OKAY)\r
- {\r
- /* Create entries in the NameServer if any NameServer assignments were specified */\r
- if (resourceProperties->nsAssignData && resourceProperties->nsAssignLen)\r
- {\r
- nsAssignments = Rm_resourceExtractNsAssignment(resourceProperties->nsAssignData, \r
- resourceProperties->nsAssignLen);\r
-\r
- /* Cycle through the list of assignments and add them to the NameServer */\r
- nsAssignmentBasePtr = nsAssignments;\r
- while (nsAssignments)\r
- {\r
- memset((void *)&resourceInfo, 0, sizeof(Rm_ResourceInfo));\r
-\r
- strcpy(resourceInfo.name, resourceName);\r
- resourceInfo.base = nsAssignments->resourceBase;\r
- resourceInfo.length = nsAssignments->resourceLength;\r
- strcpy(resourceInfo.nsName, nsAssignments->nsName);\r
- \r
- /* TODO: RETURN IF ANY OF THE ADDS FAIL??? */\r
- Rm_nsAddObject(rmInst, &resourceInfo);\r
- nsAssignments = nsAssignments->nextNsAssignment;\r
- }\r
- /* Free the memory allocated for the NameServer assignments */\r
- Rm_resourceFreeNsAssignmentList(nsAssignmentBasePtr);\r
- }\r
- }\r
-\r
- /* Free the memory allocated for the resource properties */\r
- Rm_resourceFreeRange(rangeBasePtr);\r
- Rm_resourceFreeLinuxAlias(linuxAlias);\r
-\r
- return(retVal);\r
-}\r
-\r
-int32_t Rm_parseResourceProperty(void *globalResourceDtb, int32_t offset, Rm_ResourceProperties *propertyInfo)\r
-{\r
- int32_t propertyLen;\r
- const char *propertyName;\r
- const void *propertyData;\r
- Rm_ResourcePropType propertyType;\r
- int32_t retVal = RM_DTB_UTIL_RESULT_OKAY;\r
-\r
- /* Get the property data and store it in the corresponding propertyInfo field */\r
- propertyData = fdt_getprop_by_offset(globalResourceDtb, offset, &propertyName, &propertyLen);\r
- if (propertyData)\r
- {\r
- propertyType = Rm_resourceGetPropertyType(propertyName);\r
- if (propertyType == Rm_resourcePropType_RESOURCE_RANGE)\r
- {\r
- if (propertyInfo->rangeData || propertyInfo->rangeLen)\r
- {\r
- /* The range fields have already been populated. Return an error.\r
- * The resource list has specified a property field more than once\r
- * for a resource node */\r
- retVal = -18; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
- }\r
- else\r
- {\r
- propertyInfo->rangeData = propertyData;\r
- propertyInfo->rangeLen = propertyLen;\r
- }\r
- }\r
- else if (propertyType == Rm_resourcePropType_NSASSIGNMENT)\r
- {\r
- if (propertyInfo->nsAssignData || propertyInfo->nsAssignLen)\r
- {\r
- /* The nsAssign fields have already been populated. Return an error.\r
- * The resource list has specified a property field more than once\r
- * for a resource node */\r
- retVal = -19; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
- }\r
- else\r
- {\r
- propertyInfo->nsAssignData = propertyData;\r
- propertyInfo->nsAssignLen = propertyLen;\r
- }\r
- }\r
- else if (propertyType == Rm_resourcePropType_RESOURCE_LINUX_ALIAS)\r
- {\r
- if (propertyInfo->linuxAliasData || propertyInfo->linuxAliasLen)\r
- {\r
- /* The linuxAlias fields have already been populated. Return an error.\r
- * The resource list has specified a property field more than once\r
- * for a resource node */\r
- retVal = -28; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
- }\r
- else\r
- {\r
- propertyInfo->linuxAliasData = propertyData;\r
- propertyInfo->linuxAliasLen = propertyLen;\r
- }\r
- } \r
- else\r
- {\r
- retVal = -20; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
- }\r
- }\r
- else\r
- {\r
- retVal = -16; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
- }\r
-\r
- /* Don't get anymore properties if error occurred */\r
- if (retVal == RM_DTB_UTIL_RESULT_OKAY)\r
- {\r
- offset = fdt_next_property_offset(globalResourceDtb, offset);\r
- if (offset >= 0)\r
- {\r
- retVal = Rm_parseResourceProperty(globalResourceDtb, offset, propertyInfo);\r
- }\r
- else if (offset != -FDT_ERR_NOTFOUND)\r
- {\r
- /* Error was returned by LIBFDT when parsing the properties */\r
- retVal = offset;\r
- }\r
- }\r
- \r
- return (retVal);\r
-}\r
-\r
-int32_t Rm_parseResourceNode(Rm_Inst *rmInst, void *globalResourceDtb, int32_t nodeOffset, int32_t depth,\r
- void *linuxDtb)\r
-{\r
- const char *resourceName = fdt_get_name(globalResourceDtb, nodeOffset, NULL);\r
- Rm_ResourceProperties resourceProperties;\r
- int32_t error = RM_DTB_UTIL_RESULT_OKAY;\r
- int32_t offset;\r
-\r
- /* Initialize the resource properties structure */\r
- memset((void *)&resourceProperties, 0, sizeof(Rm_ResourceProperties));\r
-\r
- /* Ignore properties of the base node */\r
- if (strcmp(resourceName, rmDtbStartingNode))\r
- {\r
- /* Get the properties for the resource node if any exist */\r
- offset = fdt_first_property_offset(globalResourceDtb, nodeOffset);\r
- if (offset >= RM_DTB_UTIL_STARTING_NODE_OFFSET)\r
- {\r
- /* Since at least one property exists attempt to parse the property nodes and \r
- * use them to create and initialize a resource allocator */\r
- error = Rm_parseResourceProperty(globalResourceDtb, offset, &resourceProperties);\r
- if (error < -FDT_ERR_NOTFOUND)\r
- {\r
- return (error);\r
- }\r
- \r
- /* Initialize an allocator with the resource properties if no error was returned */\r
- Rm_createAndInitAllocator(rmInst, resourceName, &resourceProperties, linuxDtb);\r
- }\r
- else if (offset != -FDT_ERR_NOTFOUND)\r
- {\r
- /* Error was returned by LIBFDT when parsing the properties */\r
- return (offset);\r
- }\r
- }\r
- \r
- /* Get the next resource node */\r
- offset = fdt_next_node(globalResourceDtb, nodeOffset, &depth);\r
- /* Check the offset and depth of the next node to make sure the current node\r
- * wasn't the last node in the Resource List. A depth less than the depth set\r
- * at the start of the recursion will signal the end of the resource list */\r
- if ((offset >= RM_DTB_UTIL_STARTING_NODE_OFFSET) && (depth >= RM_DTB_UTIL_STARTING_DEPTH))\r
- {\r
- error = Rm_parseResourceNode(rmInst, globalResourceDtb, offset, depth, linuxDtb);\r
- if (error < -FDT_ERR_NOTFOUND)\r
- {\r
- return (error);\r
- }\r
- }\r
- else if (offset != -FDT_ERR_NOTFOUND)\r
- {\r
- /* Error was returned by LIBFDT when parsing the nodes */\r
- return (offset);\r
- }\r
-\r
- return (RM_DTB_UTIL_RESULT_OKAY);\r
-}\r
-\r
-int32_t Rm_initializeAllocators(Rm_Inst *rmInst, void *globalResourceDtb, void *linuxDtb)\r
-{\r
- int32_t nodeOffset = RM_DTB_UTIL_STARTING_NODE_OFFSET;\r
- int32_t startDepth = RM_DTB_UTIL_STARTING_DEPTH;\r
- int32_t result = RM_DTB_UTIL_RESULT_OKAY;\r
-\r
- /* Recursively parse the Global Resource List, creating an allocator for\r
- * each resource as specified in the node */\r
- result = Rm_parseResourceNode(rmInst, globalResourceDtb, nodeOffset, startDepth, linuxDtb);\r
-\r
- return(result);\r
-}\r
- \r
-/**********************************************************************\r
- ********************** Application visible APIs **********************\r
- **********************************************************************/\r
-\r
-/* Server Only */\r
-void Rm_printResourceStatus(Rm_Handle *rmHandle)\r
-{\r
- Rm_Inst *rmInst = (Rm_Inst *) rmHandle;\r
- Rm_Allocator *allocator = rmInst->allocators;\r
- Rm_AllocatedTo *allocatedTo;\r
- Rm_ResourceTree *treeRoot;\r
- Rm_ResourceNode *treeNode;\r
-\r
- while (allocator != NULL) {\r
- Rm_osalLog("Resource: %s\n", allocator->resourceName);\r
-\r
- treeRoot = allocator->allocatorRootEntry;\r
-\r
- RB_FOREACH(treeNode, _Rm_ResourceTree, treeRoot) { \r
- Rm_osalLog(" %10d - %10d ", treeNode->base, \r
- treeNode->base + treeNode->length -1);\r
- \r
- if (treeNode->allocationCount == 0) {\r
- Rm_osalLog("NOT ALLOCATED\n");\r
- }\r
- else {\r
- allocatedTo = treeNode->allocatedTo;\r
- Rm_osalLog("allocated to ");\r
- while (allocatedTo) {\r
- Rm_osalLog(" %s", Rm_policyGetValidInstNodeName(allocatedTo->instNameNode));\r
- allocatedTo = allocatedTo->nextAllocatedTo;\r
- }\r
- Rm_osalLog("\n");\r
- }\r
- } \r
- allocator = allocator->nextAllocator;\r
- }\r
-\r
- Rm_nsPrintObjects(rmInst);\r
-}\r
-\r
-Rm_Handle Rm_init(Rm_InitCfg *initCfg, int32_t *result)\r
-{\r
- Rm_Inst *rmInst;\r
- void *globalResourceDtb = NULL;\r
- void *linuxResourceDtb = NULL;\r
- void *policyDtb = NULL;\r
-\r
- *result = RM_INIT_OK;\r
- \r
- if ((strlen(initCfg->instName) + 1) > RM_INSTANCE_NAME_MAX_CHARS) {\r
- *result = RM_INIT_ERROR_INSTANCE_NAME_TOO_BIG;\r
- return (NULL);\r
- }\r
-\r
- /* Create and initialize instance */\r
- rmInst = Rm_osalMalloc (sizeof(Rm_Inst));\r
- memset ((void *) rmInst, 0, sizeof(Rm_Inst));\r
- strcpy (&rmInst->name[0], initCfg->instName);\r
- rmInst->instType = initCfg->instType;\r
- rmInst->registeredWithDelegateOrServer = false;\r
- rmInst->routeMap = NULL;\r
- rmInst->allocators = NULL;\r
- rmInst->nameServer = NULL;\r
- rmInst->policy = NULL;\r
- rmInst->validInstances = NULL;\r
- rmInst->transactionSeqNum = Rm_transactionInitSequenceNum();\r
- rmInst->transactionQueue= NULL;\r
-\r
- if ((rmInst->instType != Rm_instType_CLIENT) && initCfg->policy) {\r
- policyDtb = initCfg->policy;\r
- fdt_open_into(policyDtb, policyDtb, fdt_totalsize(policyDtb)); \r
-\r
- /* Create valid instance list from policy. Must be done prior to parsing\r
- * GRL so that Linux resources can be reserved correctly */\r
- rmInst->validInstances = Rm_policyCreateValidInstTree(policyDtb, result);\r
- /* Validate policy assignment strings */\r
- *result = Rm_policyValidatePolicy(rmInst, policyDtb);\r
- rmInst->policy = policyDtb;\r
- }\r
-\r
- /* RM Server specific actions */\r
- if (rmInst->instType == Rm_instType_SERVER) {\r
- Rm_nsInit(rmInst);\r
-\r
- if (initCfg->globalResourceList) {\r
- globalResourceDtb = initCfg->globalResourceList;\r
- fdt_open_into(globalResourceDtb, globalResourceDtb, fdt_totalsize(globalResourceDtb));\r
-\r
- if (initCfg->linuxDtb) {\r
- linuxResourceDtb = initCfg->linuxDtb;\r
- fdt_open_into(linuxResourceDtb, linuxResourceDtb, fdt_totalsize(linuxResourceDtb)); \r
- }\r
- Rm_initializeAllocators(rmInst, globalResourceDtb, linuxResourceDtb);\r
- }\r
- }\r
-\r
- if ((rmInst->instType != Rm_instType_CLIENT) && initCfg->policy) {\r
- *result = Rm_policyValidatePolicyResourceNames(rmInst, policyDtb);\r
- } \r
-\r
- return ((Rm_Handle) rmInst);\r
-}\r
-\r
-uint32_t Rm_getVersion (void)\r
-{\r
- return RM_VERSION_ID;\r
-}\r
-\r
-\r
-const char* Rm_getVersionStr (void)\r
-{\r
- return rmVersionStr;\r
-}\r
-\r
-/**\r
-@}\r
-*/\r
+/**
+ * @file rm.c
+ *
+ * @brief
+ * This is the Resource Manager source.
+ *
+ * \par
+ * ============================================================================
+ * @n (C) Copyright 2012-2013, 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
+*/
+
+/* Standard includes */
+#include <stdint.h>
+#include <string.h>
+
+/* RM external includes */
+#include <ti/drv/rm/rm.h>
+#include <ti/drv/rm/rmver.h>
+#include <ti/drv/rm/rm_services.h>
+#include <ti/drv/rm/rm_transport.h>
+
+/* RM internal includes */
+#include <ti/drv/rm/include/rm_internal.h>
+#include <ti/drv/rm/include/rm_loc.h>
+#include <ti/drv/rm/include/rm_allocatorloc.h>
+#include <ti/drv/rm/include/rm_transportloc.h>
+#include <ti/drv/rm/include/rm_nameserverloc.h>
+#include <ti/drv/rm/include/rm_servicesloc.h>
+
+/* RM LIBFDT includes */
+#include <ti/drv/rm/util/libfdt/libfdt.h>
+
+/* RM OSAL layer */
+#include <rm_osal.h>
+
+/**********************************************************************
+ ************************** Globals ***********************************
+ **********************************************************************/
+
+/* Global Variable which describes the RM Version Information */
+const char rmVersionStr[] = RM_VERSION_STR ":" __DATE__ ":" __TIME__;
+
+/**********************************************************************
+ ************************ Local Functions *****************************
+ **********************************************************************/
+
+/* FUNCTION PURPOSE: Initializes a RM inst's transaction sequence number
+ ***********************************************************************
+ * DESCRIPTION: The RM instance transaction sequence number can never
+ * have a value of 0 to avoid conflicts with transactions
+ * that have a remoteOriginatingId of 0 (transaction ID
+ * will be used as the remoteOriginatingId for
+ * transactions that are responses to requests).
+ */
+static uint32_t transactionInitSequenceNum(void)
+{
+ return (1);
+}
+
+/* FUNCTION PURPOSE: Provides a sequence number for new transactions
+ ***********************************************************************
+ * DESCRIPTION: Returns a sequence number for a new transaction
+ * specific to a RM instance. Handles rollover of
+ * sequence number.
+ */
+static uint32_t transactionGetSequenceNum(Rm_Inst *rmInst)
+{
+ rmInst->transactionSeqNum++;
+ if (!rmInst->transactionSeqNum) {
+ rmInst->transactionSeqNum++;
+ }
+ return (rmInst->transactionSeqNum);
+}
+
+/* FUNCTION PURPOSE: Creates a resource request packet
+ ***********************************************************************
+ * DESCRIPTION: Returns a RM packet handle that points to a RM
+ * resource request packet that has been prepared
+ * for sending to another RM instance. The packet
+ * is allocated via the rmAllocPkt API using the
+ * appTransport handle provided by the application
+ */
+void createResourceReqPkt(Rm_Packet *rmPkt, char *localInstName, Rm_Transaction *transaction)
+{
+ Rm_ResourceRequestPkt *resourceReqPkt = NULL;
+
+ rmPkt->pktType = Rm_pktType_RESOURCE_REQUEST;
+ resourceReqPkt = (Rm_ResourceRequestPkt *) rmPkt->data;
+ resourceReqPkt->requestId = transaction->localId;
+ if (transaction->type == Rm_service_RESOURCE_ALLOCATE_INIT) {
+ resourceReqPkt->resourceReqType = Rm_resReqPktType_ALLOCATE_INIT;
+ }
+ else if (transaction->type == Rm_service_RESOURCE_ALLOCATE_USE) {
+ resourceReqPkt->resourceReqType = Rm_resReqPktType_ALLOCATE_USE;
+ }
+ else if (transaction->type == Rm_service_RESOURCE_FREE) {
+ resourceReqPkt->resourceReqType = Rm_resReqPktType_FREE;
+ }
+ else if (transaction->type == Rm_service_RESOURCE_GET_BY_NAME) {
+ resourceReqPkt->resourceReqType = Rm_resReqPktType_GET_NAMED;
+ }
+ strncpy(resourceReqPkt->pktSrcInstName, localInstName, RM_NAME_MAX_CHARS);
+ strncpy(resourceReqPkt->serviceSrcInstName, transaction->serviceSrcInstName, RM_NAME_MAX_CHARS);
+ memcpy ((void *)&(resourceReqPkt->resourceInfo), (void *)&(transaction->resourceInfo),
+ sizeof(Rm_ResourceInfo));
+}
+
+/* FUNCTION PURPOSE: Creates a resource response packet
+ ***********************************************************************
+ * DESCRIPTION: Returns a RM packet handle that points to a RM
+ * resource response packet that has been prepared
+ * for sending to another RM instance. The packet
+ * is allocated via the rmAllocPkt API using the
+ * appTransport handle provided by the application
+ */
+void createResourceResponsePkt(Rm_Packet *rmPkt, Rm_Transaction *transaction)
+{
+ Rm_ResourceResponsePkt *resourceRespPkt = NULL;
+
+ rmPkt->pktType = Rm_pktType_RESOURCE_RESPONSE;
+ resourceRespPkt = (Rm_ResourceResponsePkt *)rmPkt->data;
+ resourceRespPkt->responseId = transaction->remoteOriginatingId;
+ resourceRespPkt->requestState = transaction->state;
+ memcpy ((void *)&(resourceRespPkt->resourceInfo), (void *)&(transaction->resourceInfo),
+ sizeof(Rm_ResourceInfo));
+}
+
+/* FUNCTION PURPOSE: Creates a NameServer request packet
+ ***********************************************************************
+ * DESCRIPTION: Returns a RM packet handle that points to a RM
+ * NameServer request packet that has been prepared
+ * for sending to another RM instance. The packet
+ * is allocated via the rmAllocPkt API using the
+ * appTransport handle provided by the application
+ */
+void createNsRequestPkt(Rm_Packet *rmPkt, char *localInstName, Rm_Transaction *transaction)
+{
+ Rm_NsRequestPkt *nsReqPkt = NULL;
+
+ rmPkt->pktType = Rm_pktType_NAMESERVER_REQUEST;
+ nsReqPkt = (Rm_NsRequestPkt *)rmPkt->data;
+ nsReqPkt->requestId = transaction->localId;
+ if (transaction->type == Rm_service_RESOURCE_MAP_TO_NAME) {
+ nsReqPkt->nsRequestType = Rm_nsReqPktType_MAP_RESOURCE;
+ }
+ else if (transaction->type == Rm_service_RESOURCE_UNMAP_NAME) {
+ nsReqPkt->nsRequestType = Rm_nsReqPktType_UNMAP_RESOURCE;
+ }
+ strncpy(nsReqPkt->pktSrcInstName, localInstName, RM_NAME_MAX_CHARS);
+ strncpy(nsReqPkt->serviceSrcInstName, transaction->serviceSrcInstName, RM_NAME_MAX_CHARS);
+ memcpy ((void *)&(nsReqPkt->resourceInfo), (void *)&(transaction->resourceInfo),
+ sizeof(Rm_ResourceInfo));
+}
+
+/* FUNCTION PURPOSE: Creates a NameServer response packet
+ ***********************************************************************
+ * DESCRIPTION: Returns a RM packet handle that points to a RM
+ * NameServer response packet that has been prepared
+ * for sending to another RM instance. The packet
+ * is allocated via the rmAllocPkt API using the
+ * appTransport handle provided by the application
+ */
+void createNsResponsePkt(Rm_Packet *rmPkt, Rm_Transaction *transaction)
+{
+ Rm_NsResponsePkt *nsRespPkt = NULL;
+
+ rmPkt->pktType = Rm_pktType_NAMESERVER_RESPONSE;
+ nsRespPkt = (Rm_NsResponsePkt *)rmPkt->data;
+ nsRespPkt->responseId = transaction->remoteOriginatingId;
+ nsRespPkt->requestState = transaction->state;
+}
+
+/* FUNCTION PURPOSE: Issues a service response to application
+ ***********************************************************************
+ * DESCRIPTION: Provides a service response back to the application
+ * using the service callback function provided to
+ * the RM instance at the time of the service request.
+ */
+static void serviceResponder (Rm_Inst *rmInst, Rm_Transaction *transaction)
+{
+ Rm_ServiceRespInfo serviceResponse;
+
+ serviceResponse.rmHandle = (Rm_Handle)rmInst;
+ /* The responseTransaction will contain the resultant state details of
+ * the requestTransaction's service request */
+ serviceResponse.serviceState = transaction->state;
+ /* Pass back the ID that was provided to the component when it requested
+ * the service */
+ serviceResponse.serviceId = transaction->localId;
+
+ /* Service was approved and service was an allocate request. The resource
+ * data is passed back to the component */
+ if ((serviceResponse.serviceState == RM_SERVICE_APPROVED) &&
+ ((transaction->type == Rm_service_RESOURCE_ALLOCATE_INIT) ||
+ (transaction->type == Rm_service_RESOURCE_ALLOCATE_USE) ||
+ (transaction->type == Rm_service_RESOURCE_GET_BY_NAME)))
+ {
+ strncpy(serviceResponse.resourceName, transaction->resourceInfo.name, RM_NAME_MAX_CHARS);
+ serviceResponse.resourceBase = transaction->resourceInfo.base;
+ serviceResponse.resourceLength = transaction->resourceInfo.length;
+ }
+
+ if (transaction->callback.serviceCallback) {
+ /* Issue the callback to the requesting component with the response information */
+ transaction->callback.serviceCallback(&serviceResponse);
+ /* Delete the transaction from the transaction queue */
+ rmTransactionQueueDelete(rmInst, transaction->localId);
+ }
+ else {
+ rmServiceInternalCallback((Rm_Handle)rmInst);
+ }
+
+ return;
+}
+
+/* FUNCTION PURPOSE: Sends RM response packets
+ ***********************************************************************
+ * DESCRIPTION: Sends RM response packets to RM instance's that sent
+ * RM request packets to the RM instance. The response
+ * is sent via the RM transport API which is plugged
+ * with an application created transport path.
+ */
+static void transactionResponder (Rm_Inst *rmInst, Rm_Transaction *transaction)
+{
+ Rm_Transport *dstTransport = NULL;
+ Rm_Packet *rmPkt = NULL;
+ Rm_PacketHandle pktHandle = NULL;
+
+ if (dstTransport = rmTransportFindRemoteName(rmInst->transports, transaction->pktSrcInstName)) {
+ rmPkt = dstTransport->callouts.rmAllocPkt(dstTransport->appTransportHandle,
+ sizeof(Rm_Packet), &pktHandle);
+ if (!rmPkt || !pktHandle) {
+ transaction->state = RM_ERROR_TRANSPORT_ALLOC_PKT_ERROR;
+ goto errorExit;
+ }
+
+ switch (transaction->type) {
+ case Rm_service_RESOURCE_ALLOCATE_INIT:
+ case Rm_service_RESOURCE_ALLOCATE_USE:
+ case Rm_service_RESOURCE_FREE:
+ case Rm_service_RESOURCE_GET_BY_NAME:
+ createResourceResponsePkt(rmPkt, transaction);
+ break;
+ case Rm_service_RESOURCE_MAP_TO_NAME:
+ case Rm_service_RESOURCE_UNMAP_NAME:
+ createNsResponsePkt(rmPkt, transaction);
+ break;
+ }
+ if (dstTransport->callouts.rmSendPkt(dstTransport->appTransportHandle, pktHandle) < RM_OK) {
+ transaction->state = RM_ERROR_TRANSPORT_SEND_ERROR;
+ goto errorExit;
+ }
+
+ /* Response packet sent: Delete transaction from queue */
+ rmTransactionQueueDelete(rmInst, transaction->localId);
+ }
+ else {
+ transaction->state = RM_ERROR_TRANSPORT_REMOTE_HNDL_NOT_REGD;
+ }
+errorExit:
+ /* Do not delete transaction on transport error. Transport rrror transactions should be visible
+ * from Rm_printInstanceStatus() */
+ return;
+}
+
+/* FUNCTION PURPOSE: Sends RM request packets
+ ***********************************************************************
+ * DESCRIPTION: Sends RM request packets to RM instance's that are
+ * capable of forwarding or validating service requests.
+ * The request is sent via the RM transport API which is
+ * plugged with an application created transport path.
+ */
+static void transactionForwarder (Rm_Inst *rmInst, Rm_Transaction *transaction)
+{
+ Rm_Transport *dstTransport = NULL;
+ Rm_Packet *rmPkt = NULL;
+ Rm_PacketHandle pktHandle = NULL;
+
+ if (rmInst->instType == Rm_instType_CLIENT) {
+ dstTransport = rmTransportFindRemoteInstType(rmInst->transports, Rm_instType_CLIENT_DELEGATE);
+
+ if (!dstTransport) {
+ dstTransport = rmTransportFindRemoteInstType(rmInst->transports, Rm_instType_SERVER);
+ }
+ }
+ else if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {
+ dstTransport = rmTransportFindRemoteInstType(rmInst->transports, Rm_instType_SERVER);
+ }
+
+ /* Just queue transaction if transport hasn't been registered. Do not return error */
+ if (dstTransport) {
+ rmPkt = dstTransport->callouts.rmAllocPkt(dstTransport->appTransportHandle,
+ sizeof(Rm_Packet), &pktHandle);
+ if (!rmPkt || !pktHandle) {
+ transaction->state = RM_ERROR_TRANSPORT_ALLOC_PKT_ERROR;
+ goto errorExit;
+ }
+
+ switch (transaction->type) {
+ case Rm_service_RESOURCE_ALLOCATE_INIT:
+ case Rm_service_RESOURCE_ALLOCATE_USE:
+ case Rm_service_RESOURCE_FREE:
+ case Rm_service_RESOURCE_GET_BY_NAME:
+ createResourceReqPkt(rmPkt, rmInst->instName, transaction);
+ break;
+ case Rm_service_RESOURCE_MAP_TO_NAME:
+ case Rm_service_RESOURCE_UNMAP_NAME:
+ createNsRequestPkt(rmPkt, rmInst->instName, transaction);
+ break;
+ }
+
+ if (dstTransport->callouts.rmSendPkt(dstTransport->appTransportHandle, pktHandle) < RM_OK) {
+ transaction->state = RM_ERROR_TRANSPORT_SEND_ERROR;
+ goto errorExit;
+ }
+ transaction->hasBeenForwarded = RM_TRUE;
+ /* Transaction not deleted. Waiting for response from RM CD or Server */
+ }
+errorExit:
+ /* Do not delete transaction on transport error. Transport error transactions should be visible
+ * from Rm_printInstanceStatus() */
+ return;
+}
+
+/* FUNCTION PURPOSE: Handles static allocation requests
+ ***********************************************************************
+ * DESCRIPTION: Validates allocation requests received on CDs and
+ * Clients prior to the instance's registering
+ * with a Server. The allocation request is validated
+ * against a static policy.
+ */
+static void staticAllocationHandler (Rm_Handle rmHandle, Rm_Transaction *transaction)
+{
+ Rm_Inst *rmInst = (Rm_Inst *)rmHandle;
+ void *staticPolicy = rmPolicyGetPolicy(rmHandle);
+ Rm_PolicyCheckCfg privCheckCfg;
+ int32_t result;
+
+ if (staticPolicy) {
+ if ((transaction->type == Rm_service_RESOURCE_ALLOCATE_INIT) ||
+ (transaction->type == Rm_service_RESOURCE_ALLOCATE_USE)) {
+ /* Check request against static policy */
+ memset((void *)&privCheckCfg, 0, sizeof(Rm_PolicyCheckCfg));
+
+ if (transaction->type == Rm_service_RESOURCE_ALLOCATE_INIT) {
+ privCheckCfg.type = Rm_policyCheck_INIT;
+ }
+ else {
+ privCheckCfg.type = Rm_policyCheck_USE;
+ }
+ privCheckCfg.policyDtb = staticPolicy;
+ privCheckCfg.validInstNode = rmPolicyGetValidInstNode(rmHandle, rmInst->instName);
+ privCheckCfg.resourceOffset = rmPolicyGetResourceOffset(staticPolicy,
+ transaction->resourceInfo.name);
+ privCheckCfg.resourceBase = transaction->resourceInfo.base;
+ privCheckCfg.resourceLength = transaction->resourceInfo.length;
+
+ if (rmPolicyCheckPrivilege(&privCheckCfg, &result)) {
+ transaction->state = RM_SERVICE_APPROVED_STATIC;
+ }
+ else if (result == RM_OK) {
+ /* Privilege check returned false without error */
+ transaction->state = RM_SERVICE_DENIED_BY_STATIC_POLICY;
+ }
+ else {
+ /* Privilege check returned false with error */
+ transaction->state = result;
+ }
+ }
+ else {
+ transaction->state = RM_SERVICE_DENIED_INVALID_STATIC_REQUEST;
+ }
+ }
+ else {
+ transaction->state = RM_ERROR_REQ_FAILED_NO_STATIC_POLICY;
+ }
+}
+
+/* FUNCTION PURPOSE: Arbitrates allocation service requests
+ ***********************************************************************
+ * DESCRIPTION: Issues a set of allocator operations in order to
+ * handle a received allocation request. Allocation
+ * requests are always forwarded to the Server on Client
+ * CD instances. If a request is made with a NameServer
+ * name the resource base and length parameters are
+ * retrieved from the NameServer prior to the allocation
+ * attempt.
+ */
+static void allocationHandler (Rm_Inst *rmInst, Rm_Transaction *transaction)
+{
+ Rm_AllocatorOpInfo opInfo;
+
+ Rm_NameServerObjCfg nameServerObjCfg;
+ int32_t retVal = transaction->state;
+
+ if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {
+ /* Fill in allocation logic for CDs */
+ }
+ else if ((rmInst->instType == Rm_instType_SERVER)||
+ (rmInst->instType == Rm_instType_SHARED_SERVER)) {
+ memset((void *)&opInfo, 0, sizeof(Rm_AllocatorOpInfo));
+
+ opInfo.policy = rmInst->u.server.globalPolicy;
+ opInfo.resourceInfo = &transaction->resourceInfo;
+ opInfo.serviceSrcInstNode = rmPolicyGetValidInstNode((Rm_Handle)rmInst, transaction->serviceSrcInstName);
+ if (opInfo.serviceSrcInstNode) {
+ /* Populated NameServer name has precedence over base */
+ if (strlen(transaction->resourceInfo.nameServerName) > 0) {
+ if ((transaction->resourceInfo.base == 0) &&
+ (transaction->resourceInfo.length == 0) &&
+ (transaction->resourceInfo.alignment == 0)) {
+ if (rmInst->instType == Rm_instType_SHARED_SERVER) {
+ rmNameServerTreeInv(rmInst->u.server.nameServer);
+ }
+ memset((void *)&nameServerObjCfg, 0, sizeof(Rm_NameServerObjCfg));
+ nameServerObjCfg.nameServerTree = rmInst->u.server.nameServer;
+ nameServerObjCfg.nodeCfg.objName = transaction->resourceInfo.nameServerName;
+ if ((retVal = rmNameServerFindObject(&nameServerObjCfg)) == RM_SERVICE_PROCESSING) {
+ strncpy(transaction->resourceInfo.name, nameServerObjCfg.nodeCfg.resourceName, RM_NAME_MAX_CHARS);
+ transaction->resourceInfo.base = nameServerObjCfg.nodeCfg.resourceBase;
+ transaction->resourceInfo.length = nameServerObjCfg.nodeCfg.resourceLength;
+ }
+ }
+ else {
+ retVal = RM_ERROR_NS_NAME_AND_RES_VAL_CONFLICT;
+ goto errorExit;
+ }
+ }
+
+ if (transaction->resourceInfo.base == RM_RESOURCE_BASE_UNSPECIFIED) {
+ if (transaction->type == Rm_service_RESOURCE_ALLOCATE_INIT) {
+ opInfo.operation = Rm_allocatorOp_PRE_ALLOCATE_INIT;
+ }
+ else if (transaction->type == Rm_service_RESOURCE_ALLOCATE_USE) {
+ opInfo.operation = Rm_allocatorOp_PRE_ALLOCATE_USE;
+ }
+ else {
+ retVal = RM_ERROR_INVALID_SERVICE_TYPE;
+ goto errorExit;
+ }
+ retVal = rmAllocatorOperation((Rm_Handle)rmInst, &opInfo);
+ }
+
+ if (retVal == RM_SERVICE_PROCESSING) {
+ if (transaction->type == Rm_service_RESOURCE_ALLOCATE_INIT) {
+ opInfo.operation = Rm_allocatorOp_ALLOCATE_INIT;
+ }
+ else if (transaction->type == Rm_service_RESOURCE_ALLOCATE_USE) {
+ opInfo.operation = Rm_allocatorOp_ALLOCATE_USE;
+ }
+ else {
+ retVal = RM_ERROR_INVALID_SERVICE_TYPE;
+ goto errorExit;
+ }
+ retVal = rmAllocatorOperation((Rm_Handle)rmInst, &opInfo);
+ }
+ }
+ else {
+ retVal = RM_SERVICE_DENIED_INST_NAME_NOT_VALID;
+ }
+errorExit:
+ transaction->state = retVal;
+ }
+}
+
+/* FUNCTION PURPOSE: Arbitrates free service requests
+ ***********************************************************************
+ * DESCRIPTION: Issues a set of allocator operations in order to
+ * handle a received free request. Free
+ * requests are always forwarded to the Server on Client
+ * CD instances. If a request is made with a NameServer
+ * name the resource base and length parameters are
+ * retrieved from the NameServer prior to the free
+ * attempt.
+ */
+static void freeHandler (Rm_Inst *rmInst, Rm_Transaction *transaction)
+{
+ Rm_AllocatorOpInfo opInfo;
+ Rm_NameServerObjCfg nameServerObjCfg;
+ int32_t retVal = transaction->state;
+
+ if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {
+ /* Fill in free logic for CDs */
+ }
+ else if ((rmInst->instType == Rm_instType_SERVER) ||
+ (rmInst->instType == Rm_instType_SHARED_SERVER)) {
+ memset((void *)&opInfo, 0, sizeof(Rm_AllocatorOpInfo));
+
+ opInfo.policy = rmInst->u.server.globalPolicy;
+ opInfo.resourceInfo = &transaction->resourceInfo;
+ opInfo.serviceSrcInstNode = rmPolicyGetValidInstNode((Rm_Handle)rmInst, transaction->serviceSrcInstName);
+ if (opInfo.serviceSrcInstNode) {
+ /* Populated NameServer name has precedence over base */
+ if (strlen(transaction->resourceInfo.nameServerName) > 0) {
+ if ((transaction->resourceInfo.base == 0) &&
+ (transaction->resourceInfo.length == 0) &&
+ (transaction->resourceInfo.alignment == 0)) {
+ if (rmInst->instType == Rm_instType_SHARED_SERVER) {
+ rmNameServerTreeInv(rmInst->u.server.nameServer);
+ }
+ memset((void *)&nameServerObjCfg, 0, sizeof(Rm_NameServerObjCfg));
+ nameServerObjCfg.nameServerTree = rmInst->u.server.nameServer;
+ nameServerObjCfg.nodeCfg.objName = transaction->resourceInfo.nameServerName;
+ if ((retVal = rmNameServerFindObject(&nameServerObjCfg)) == RM_SERVICE_PROCESSING) {
+ strncpy(transaction->resourceInfo.name, nameServerObjCfg.nodeCfg.resourceName, RM_NAME_MAX_CHARS);
+ transaction->resourceInfo.base = nameServerObjCfg.nodeCfg.resourceBase;
+ transaction->resourceInfo.length = nameServerObjCfg.nodeCfg.resourceLength;
+ }
+ }
+ else {
+ retVal = RM_ERROR_NS_NAME_AND_RES_VAL_CONFLICT;
+ }
+ }
+
+ if(retVal == RM_SERVICE_PROCESSING) {
+ opInfo.operation = Rm_allocatorOp_FREE;
+ retVal = rmAllocatorOperation((Rm_Handle)rmInst, &opInfo);
+ }
+ }
+ else {
+ retVal = RM_SERVICE_DENIED_INST_NAME_NOT_VALID;
+ }
+
+ transaction->state = retVal;
+ }
+}
+
+/* FUNCTION PURPOSE: Client transaction handling process
+ ***********************************************************************
+ * DESCRIPTION: Client process for handling transactions created
+ * from services received via the service handle or the
+ * transport. The Client process:
+ * - Performs static allocations if no transport
+ * to CD or Server has been registered
+ * - Forwards all service requests to CD or Server
+ * once transport has been registered
+ */
+static void clientProcess (Rm_Inst *rmInst, Rm_Transaction *transaction)
+{
+ Rm_Transaction *transQ = rmInst->transactionQueue;
+
+ if (!rmInst->registeredWithDelegateOrServer) {
+ staticAllocationHandler((Rm_Handle)rmInst, transaction);
+ }
+ else {
+ if (transaction->state == RM_SERVICE_PROCESSING) {
+ /* Forward all new transactions to CD or Server */
+ transactionForwarder(rmInst, transaction);
+ }
+ else {
+ /* Transaction validated. Return result. */
+ serviceResponder(rmInst, transaction);
+ }
+
+ /* Forward any queued static requests that weren't forwarded */
+ while(transQ) {
+ if ((transQ->state == RM_SERVICE_APPROVED_STATIC) &&
+ (!transQ->hasBeenForwarded)) {
+ transactionForwarder(rmInst, transQ);
+ }
+ transQ = transQ->nextTransaction;
+ }
+ }
+ /* Let call stack return transaction result app via Rm_serviceHandler */
+}
+
+/* FUNCTION PURPOSE: Client Delegate transaction handling process
+ ***********************************************************************
+ * DESCRIPTION: Client Delegate process for handling transactions created
+ * from services received via the service handle or the
+ * transport. The Client Delegate process:
+ * - Performs static allocations if no transport
+ * to Server has been registered
+ * - Forwards all NameServer related service requests
+ * to Server once transport has been registered
+ * - Attempts to complete resource service requests
+ * received from registered Clients
+ */
+static void cdProcess (Rm_Inst *rmInst, Rm_Transaction *transaction)
+{
+ Rm_Transaction *transQ = rmInst->transactionQueue;
+
+ if (!rmInst->registeredWithDelegateOrServer) {
+ if ((transaction->state == RM_SERVICE_PROCESSING) &&
+ (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS) == 0)) {
+ /* Attempt static allocation of requests originating from CD inst */
+ staticAllocationHandler((Rm_Handle)rmInst, transaction);
+ }
+ /* Everything else left in transaction queue for forwarding once transport to
+ * Server is registered */
+ }
+ else {
+ if ((transaction->type == Rm_service_RESOURCE_MAP_TO_NAME) ||
+ (transaction->type == Rm_service_RESOURCE_GET_BY_NAME) ||
+ (transaction->type == Rm_service_RESOURCE_UNMAP_NAME)) {
+ if (transaction->state == RM_SERVICE_PROCESSING) {
+ /* Forward all NameServer requests. */
+ transactionForwarder(rmInst, transaction);
+ }
+ else {
+ /* NameServer transaction validated. Return result. */
+ if (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS)) {
+ /* Transaction did not originate on this instance */
+ transactionResponder(rmInst, transaction);
+ }
+ else {
+ /* Transaction originated on this instance */
+ serviceResponder(rmInst, transaction);
+ }
+ }
+ }
+ else if ((transaction->type == Rm_service_RESOURCE_ALLOCATE_INIT) ||
+ (transaction->type == Rm_service_RESOURCE_ALLOCATE_USE)) {
+ if ((transaction->state == RM_SERVICE_PROCESSING) ||
+ (transaction->state == RM_SERVICE_APPROVED_STATIC)) {
+ transactionForwarder(rmInst, transaction);
+ }
+ else {
+ /* Transaction validated. Return result. */
+ if (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS)) {
+ /* Transaction did not originate on this instance */
+ transactionResponder(rmInst, transaction);
+ }
+ else {
+ /* Transaction originated on this instance */
+ serviceResponder(rmInst, transaction);
+ }
+ }
+ }
+ else if (transaction->type == Rm_service_RESOURCE_FREE) {
+ if (transaction->state == RM_SERVICE_PROCESSING) {
+ freeHandler(rmInst, transaction);
+
+ if (transaction->state == RM_SERVICE_PROCESSING) {
+ /* CD could not handle free. Forward to Server */
+ transactionForwarder(rmInst, transaction);
+ }
+ else {
+ /* Free validated by CD. Return result */
+ if (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS)) {
+ /* Transaction did not originate on this instance */
+ transactionResponder(rmInst, transaction);
+ }
+ else {
+ /* Transaction originated on this instance */
+ serviceResponder(rmInst, transaction);
+ }
+ }
+ }
+ else {
+ /* Transaction validated. Return result. */
+ if (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS)) {
+ /* Transaction did not originate on this instance */
+ transactionResponder(rmInst, transaction);
+ }
+ else {
+ /* Transaction originated on this instance */
+ serviceResponder(rmInst, transaction);
+ }
+ }
+ }
+
+ /* Attempt allocation of any queued static requests:
+ * RM_SERVICE_APPROVED_STATIC - Originated locally
+ * RM_SERVICE_PROCESSING - Received from any registered Clients */
+ while(transQ) {
+ if (((transQ->state == RM_SERVICE_PROCESSING) ||
+ (transQ->state == RM_SERVICE_APPROVED_STATIC)) &&
+ (!transQ->hasBeenForwarded)) {
+ transactionForwarder(rmInst, transQ);
+ }
+ transQ = transQ->nextTransaction;
+ }
+ }
+}
+
+/* FUNCTION PURPOSE: Server transaction handling process
+ ***********************************************************************
+ * DESCRIPTION: Server process for handling transactions created
+ * from services received via the service handle or the
+ * transport. The Server process:
+ * - Validates all service requests received from
+ * the service handle and registered CDs and
+ * Clients
+ */
+static void serverProcess (Rm_Inst *rmInst, Rm_Transaction *transaction)
+{
+ Rm_NameServerObjCfg nameServerObjCfg;
+
+ switch (transaction->type) {
+ case Rm_service_RESOURCE_ALLOCATE_INIT:
+ case Rm_service_RESOURCE_ALLOCATE_USE:
+ allocationHandler(rmInst, transaction);
+ break;
+ case Rm_service_RESOURCE_FREE:
+ freeHandler(rmInst, transaction);
+ break;
+ case Rm_service_RESOURCE_MAP_TO_NAME:
+ case Rm_service_RESOURCE_GET_BY_NAME:
+ case Rm_service_RESOURCE_UNMAP_NAME:
+ if (rmInst->u.server.nameServer) {
+ if (rmInst->instType == Rm_instType_SHARED_SERVER) {
+ rmNameServerTreeInv(rmInst->u.server.nameServer);
+ }
+ memset((void *)&nameServerObjCfg, 0, sizeof(Rm_NameServerObjCfg));
+ nameServerObjCfg.nameServerTree = rmInst->u.server.nameServer;
+ nameServerObjCfg.nodeCfg.objName = transaction->resourceInfo.nameServerName;
+ if (transaction->type == Rm_service_RESOURCE_MAP_TO_NAME) {
+ nameServerObjCfg.nodeCfg.resourceName = transaction->resourceInfo.name;
+ nameServerObjCfg.nodeCfg.resourceBase= transaction->resourceInfo.base;
+ nameServerObjCfg.nodeCfg.resourceLength = transaction->resourceInfo.length;
+ transaction->state = rmNameServerAddObject(&nameServerObjCfg);
+ }
+ else if (transaction->type == Rm_service_RESOURCE_GET_BY_NAME) {
+ if ((transaction->state = rmNameServerFindObject(&nameServerObjCfg)) ==
+ RM_SERVICE_PROCESSING) {
+ strncpy(transaction->resourceInfo.name, nameServerObjCfg.nodeCfg.resourceName, RM_NAME_MAX_CHARS);
+ transaction->resourceInfo.base = nameServerObjCfg.nodeCfg.resourceBase;
+ transaction->resourceInfo.length = nameServerObjCfg.nodeCfg.resourceLength;
+ transaction->state = RM_SERVICE_APPROVED;
+ }
+ }
+ else if (transaction->type == Rm_service_RESOURCE_UNMAP_NAME) {
+ transaction->state = rmNameServerDeleteObject(&nameServerObjCfg);
+ }
+
+ if (rmInst->instType == Rm_instType_SHARED_SERVER) {
+ rmNameServerTreeWb(rmInst->u.server.nameServer);
+ }
+ }
+ else {
+ transaction->state = RM_ERROR_NAMESERVER_DOES_NOT_EXIST;
+ }
+ break;
+ }
+
+ /* Source of shared server transaction will always be local. */
+ if (rmInst->instType != Rm_instType_SHARED_SERVER) {
+ if (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS)) {
+ /* Source of transaction was not Server, return transaction via responder */
+ transactionResponder(rmInst, transaction);
+ }
+ }
+ /* Otherwise let call stack return transaction result app via Rm_serviceHandler */
+}
+
+/**********************************************************************
+ ********************** Internal Functions ****************************
+ **********************************************************************/
+
+/* FUNCTION PURPOSE: Adds a transaction
+ ***********************************************************************
+ * DESCRIPTION: Returns a pointer to a newly created transaction.
+ * The transaction is created based on a new service
+ * request received via the service API or the
+ * transport API (service forwarded from another instance)
+ */
+Rm_Transaction *rmTransactionQueueAdd(Rm_Inst *rmInst)
+{
+ Rm_Transaction *transactionQueue = rmInst->transactionQueue;
+ Rm_Transaction *newTransaction = NULL;
+
+ newTransaction = Rm_osalMalloc(sizeof(Rm_Transaction));
+ if (newTransaction) {
+ memset((void *)newTransaction, 0, sizeof(Rm_Transaction));
+
+ newTransaction->localId = transactionGetSequenceNum(rmInst);
+ newTransaction->nextTransaction = NULL;
+ if (transactionQueue) {
+ while (transactionQueue->nextTransaction) {
+ transactionQueue = transactionQueue->nextTransaction;
+ }
+ transactionQueue->nextTransaction = newTransaction;
+ }
+ else {
+ rmInst->transactionQueue = newTransaction;
+ }
+ }
+ return (newTransaction);
+}
+
+/* FUNCTION PURPOSE: Finds a transaction
+ ***********************************************************************
+ * DESCRIPTION: Returns a pointer to a transaction resident
+ * in the transaction queue that matches the provided
+ * transaction ID.
+ */
+Rm_Transaction *rmTransactionQueueFind(Rm_Inst *rmInst, uint32_t transactionId)
+{
+ Rm_Transaction *transaction = rmInst->transactionQueue;
+
+ while (transaction) {
+ if (transaction->localId == transactionId) {
+ break;
+ }
+ transaction = transaction->nextTransaction;
+ }
+
+ return (transaction);
+}
+
+/* FUNCTION PURPOSE: Deletes a transaction
+ ***********************************************************************
+ * DESCRIPTION: Deletes the transaction with the provided transaction
+ * ID from the instance's transaction queue.
+ */
+int32_t rmTransactionQueueDelete(Rm_Inst *rmInst, uint32_t transactionId)
+{
+ Rm_Transaction *transaction = rmInst->transactionQueue;
+ Rm_Transaction *prevTransaction = NULL;
+ int32_t retVal = RM_OK;
+
+ while (transaction) {
+ if (transaction->localId == transactionId) {
+ break;
+ }
+
+ prevTransaction = transaction;
+ transaction = transaction->nextTransaction;
+ }
+
+ if (transaction) {
+ if (prevTransaction == NULL) {
+ /* Transaction at start of queue. Map second transaction to start of queue
+ * as long as more than one transactions. */
+ rmInst->transactionQueue = transaction->nextTransaction;
+ }
+ else {
+ /* Transaction in middle or end of queue. */
+ prevTransaction->nextTransaction = transaction->nextTransaction;
+ }
+ Rm_osalFree((void *)transaction, sizeof(Rm_Transaction));
+ }
+ else {
+ retVal = RM_ERROR_SERVICE_TRANS_DOES_NOT_EXIST;
+ }
+ return (retVal);
+}
+
+/* FUNCTION PURPOSE: Routes a transaction for processing
+ ***********************************************************************
+ * DESCRIPTION: Routes a received transaction to the appropriate
+ * instance processing routine
+ */
+void rmProcessRouter (Rm_Inst *rmInst, Rm_Transaction *transaction)
+{
+ if (rmInst->instType == Rm_instType_CLIENT) {
+ clientProcess(rmInst, transaction);
+ }
+ else if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {
+ cdProcess(rmInst, transaction);
+ }
+ else if ((rmInst->instType == Rm_instType_SERVER) ||
+ (rmInst->instType == Rm_instType_SHARED_SERVER)) {
+ serverProcess(rmInst, transaction);
+ }
+}
+
+/**********************************************************************
+ ********************** Application visible APIs **********************
+ **********************************************************************/
+
+/* FUNCTION PURPOSE: Display status of managed resources
+ ***********************************************************************
+ * DESCRIPTION: Prints the status (allocate/free status, as well as
+ * owners) for all resources managed by the RM
+ * instance network. Also, prints the NameServer name
+ * entries. The number of resource range owners is
+ * returned as well. This function is only available on
+ * server instances.
+ */
+int32_t Rm_resourceStatus(Rm_Handle rmServerHandle, int printResources)
+{
+ Rm_Inst *rmInst = (Rm_Inst *)rmServerHandle;
+ Rm_Allocator *allocator = NULL;
+ Rm_Owner *owners;
+ Rm_ResourceTree *treeRoot;
+ Rm_ResourceNode *treeNode;
+ int32_t totalResOwners = 0;
+ void *key;
+
+ RM_SS_INST_INV_ENTER_CS(key);
+ RM_SC_INST_INV_ENTER_CS(key);
+
+ if (rmInst->instType == Rm_instType_SHARED_CLIENT) {
+ /* Transfer control to shared server instance */
+ rmInst = rmInst->u.sharedClient.sharedServerHandle;
+ }
+
+ if ((rmInst->instType == Rm_instType_SERVER) ||
+ (rmInst->instType == Rm_instType_SHARED_SERVER)) {
+ allocator = rmInst->u.server.allocators;
+ while (allocator) {
+ RM_SS_OBJ_INV(allocator, Rm_Allocator);
+ if (printResources) {
+ Rm_osalLog("Resource: %s\n", allocator->resourceName);
+ }
+
+ treeRoot = allocator->allocatorRootEntry;
+ if (rmInst->instType == Rm_instType_SHARED_SERVER) {
+ rmResourceTreeInv(treeRoot);
+ }
+ RB_FOREACH(treeNode, _Rm_AllocatorResourceTree, treeRoot) {
+ if (printResources) {
+ if ((treeNode->base >= 65536) ||
+ ((treeNode->base + treeNode->length - 1) >= 65536)) {
+ /* Print in hex if number is very large */
+ Rm_osalLog(" 0x%08x - 0x%08x ", treeNode->base,
+ treeNode->base + treeNode->length - 1);
+ }
+ else {
+ Rm_osalLog(" %10d - %10d ", treeNode->base,
+ treeNode->base + treeNode->length - 1);
+ }
+ }
+
+ if (treeNode->allocationCount == 0) {
+ if (printResources) {
+ Rm_osalLog("FREE\n");
+ }
+ }
+ else {
+ owners = treeNode->ownerList;
+ while (owners) {
+ RM_SS_OBJ_INV(owners, Rm_Owner);
+ if (printResources) {
+ Rm_osalLog("%s ", owners->instNameNode->name);
+ }
+ totalResOwners++;
+ owners = owners->nextOwner;
+ }
+ if (printResources) {
+ Rm_osalLog("\n");
+ }
+ }
+ }
+ allocator = allocator->nextAllocator;
+ }
+ if (printResources) {
+ rmNameServerPrintObjects((Rm_Handle)rmInst);
+ }
+ }
+ else {
+ totalResOwners = RM_ERROR_INVALID_RES_STATUS_INSTANCE;
+ }
+
+ RM_SS_INST_WB_EXIT_CS(key);
+ return(totalResOwners);
+}
+
+/* FUNCTION PURPOSE: Display status of a RM instance
+ ***********************************************************************
+ * DESCRIPTION: Prints the current status of various RM instance
+ * properties such as the state of all transactions
+ * in the transaction queue and registered transports
+ */
+void Rm_instanceStatus(Rm_Handle rmHandle)
+{
+ Rm_Inst *rmInst = (Rm_Inst *)rmHandle;
+ Rm_Transport *transportList = NULL;
+ Rm_Transaction *transactionQ = NULL;
+ void *key;
+
+ RM_SS_INST_INV_ENTER_CS(key);
+ RM_SC_INST_INV_ENTER_CS(key);
+
+ Rm_osalLog("Instance name: %s\n", rmInst->instName);
+ Rm_osalLog("Handle: 0x%08x\n", rmHandle);
+ if (rmInst->instType == Rm_instType_SERVER) {
+ Rm_osalLog("Type: Server\n");
+ }
+ else if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {
+ Rm_osalLog("Type: Client Delegate\n");
+ }
+ else if (rmInst->instType == Rm_instType_CLIENT) {
+ Rm_osalLog("Type: Client\n");
+ }
+ else if (rmInst->instType == Rm_instType_SHARED_SERVER) {
+ Rm_osalLog("Type: Shared Server\n");
+ }
+ else if (rmInst->instType == Rm_instType_SHARED_CLIENT) {
+ Rm_osalLog("Type: Shared Client\n");
+
+ Rm_osalLog("\nShared Server Properties:\n");
+ /* Transfer to Shared Server instance to print out transport and
+ * transaction status */
+ rmInst = rmInst->u.sharedClient.sharedServerHandle;
+ Rm_osalLog("Instance name: %s\n", rmInst->instName);
+ Rm_osalLog("Handle: 0x%08x\n", rmHandle);
+ }
+
+ transportList = rmInst->transports;
+ if (transportList) {
+ Rm_osalLog("\nRegistered Transports:\n");
+ while (transportList) {
+ RM_SS_OBJ_INV(transportList, Rm_Transport);
+ Rm_osalLog(" Remote instName: %s\n", transportList->remoteInstName);
+ if (transportList->remoteInstType == Rm_instType_SERVER) {
+ Rm_osalLog(" Remote instType: Server\n");
+ }
+ else if (transportList->remoteInstType == Rm_instType_CLIENT_DELEGATE) {
+ Rm_osalLog(" Remote instType: Client Delegate\n");
+ }
+ else {
+ Rm_osalLog(" Remote instType: Client\n");
+ }
+ Rm_osalLog(" appTransportHandle: 0x%08x\n", transportList->appTransportHandle);
+ Rm_osalLog("\n");
+ transportList = transportList->nextTransport;
+ }
+ }
+
+ transactionQ = rmInst->transactionQueue;
+ if (transactionQ) {
+ Rm_osalLog("\nQueued Service Transactions:\n");
+ while (transactionQ) {
+ RM_SS_OBJ_INV(transactionQ, Rm_Transaction);
+ Rm_osalLog(" Service type: %d\n", transactionQ->type);
+ Rm_osalLog(" Service ID: %d\n", transactionQ->localId);
+ Rm_osalLog(" Service srcInstName %s\n", transactionQ->serviceSrcInstName);
+ Rm_osalLog(" Service state: %d\n", transactionQ->state);
+ Rm_osalLog(" Resource name: %s\n", transactionQ->resourceInfo.name);
+ Rm_osalLog(" Resource base: %d\n", transactionQ->resourceInfo.base);
+ Rm_osalLog(" Resource length: %d\n", transactionQ->resourceInfo.length);
+ Rm_osalLog(" Resource alignment: %d\n", transactionQ->resourceInfo.alignment);
+ Rm_osalLog(" Resource NS name: %s\n", transactionQ->resourceInfo.nameServerName);
+ Rm_osalLog("\n");
+ transactionQ = transactionQ->nextTransaction;
+ }
+ }
+ RM_SS_INST_WB_EXIT_CS(key);
+}
+
+/* FUNCTION PURPOSE: RM instance creation and initialization
+ ***********************************************************************
+ * DESCRIPTION: Returns a new RM instance created and initialized
+ * using the parameters provided via the initCfg
+ * structure.
+ */
+Rm_Handle Rm_init(const Rm_InitCfg *initCfg, int32_t *result)
+{
+ Rm_Inst *rmInst = NULL;
+ Rm_Inst *sharedServerInst = NULL;
+ uint32_t policySize;
+ void *globalResourceDtb = NULL;
+ void *linuxResourceDtb = NULL;
+ int addLinux = RM_FALSE;
+ void *key;
+
+ *result = RM_OK;
+
+ if ((initCfg->instName == NULL) ||
+ ((strlen(initCfg->instName) + 1) > RM_NAME_MAX_CHARS)) {
+ *result = RM_ERROR_INVALID_INST_NAME;
+ goto errorExit;
+ }
+
+ if (initCfg->instType >= Rm_instType_LAST) {
+ *result = RM_ERROR_INVALID_INST_TYPE;
+ goto errorExit;
+ }
+
+ /* Create and initialize instance */
+ rmInst = Rm_osalMalloc(sizeof(*rmInst));
+ memset ((void *)rmInst, 0, sizeof(*rmInst));
+ rmInst->isLocked = RM_FALSE;
+ rmInst->registeredWithDelegateOrServer = RM_FALSE;
+ rmInst->transactionSeqNum = transactionInitSequenceNum();
+
+ rmInst->instType = initCfg->instType;
+ strncpy (rmInst->instName, initCfg->instName, RM_NAME_MAX_CHARS);
+
+ if ((rmInst->instType == Rm_instType_SERVER) ||
+ (rmInst->instType == Rm_instType_SHARED_SERVER)) {
+ if (!initCfg->instCfg.serverCfg.globalResourceList ||
+ !initCfg->instCfg.serverCfg.globalPolicy) {
+ *result = RM_ERROR_INVALID_SERVER_CONFIGURATION;
+ goto errorExit;
+ }
+
+ if (rmInst->instType == Rm_instType_SHARED_SERVER) {
+ /* Shared Server makes copy of policy in shared memory for Shared Clients
+ * on other cores */
+ policySize = fdt_totalsize(initCfg->instCfg.serverCfg.globalPolicy);
+ /* Align policy size to cache boundary */
+ if (policySize % RM_MAX_CACHE_ALIGN) {
+ policySize += (RM_MAX_CACHE_ALIGN - (policySize % RM_MAX_CACHE_ALIGN));
+ }
+ rmInst->u.server.policySize = policySize;
+ rmInst->u.server.globalPolicy = Rm_osalMalloc(rmInst->u.server.policySize);
+ memcpy(rmInst->u.server.globalPolicy, initCfg->instCfg.serverCfg.globalPolicy, rmInst->u.server.policySize);
+ }
+ else {
+ rmInst->u.server.globalPolicy = initCfg->instCfg.serverCfg.globalPolicy;
+ }
+
+ if (initCfg->instCfg.serverCfg.linuxDtb) {
+ linuxResourceDtb = initCfg->instCfg.serverCfg.linuxDtb;
+ addLinux = RM_TRUE;
+ }
+
+ /* Create valid instance list from policy. Must be done prior to parsing
+ * GRL so that Linux resources can be reserved correctly */
+ rmInst->u.server.globalValidInstTree = rmPolicyCreateValidInstTree((Rm_Handle)rmInst, addLinux, result);
+ if (*result == RM_OK) {
+ *result = rmPolicyValidatePolicy((Rm_Handle)rmInst);
+ }
+
+ if (*result != RM_OK) {
+ if (rmInst->u.server.globalValidInstTree) {
+ rmPolicyFreeValidInstTree((Rm_Handle)rmInst);
+ }
+ goto errorExit;
+ }
+ else {
+ rmNameServerInit((Rm_Handle)rmInst);
+
+ globalResourceDtb = initCfg->instCfg.serverCfg.globalResourceList;
+
+ if ((*result = rmAllocatorInitializeResources((Rm_Handle) rmInst, globalResourceDtb, linuxResourceDtb)) == RM_OK) {
+ *result = rmPolicyValidatePolicyResourceNames((Rm_Handle)rmInst);
+ }
+
+ if (*result != RM_OK) {
+ rmAllocatorDeleteResources((Rm_Handle)rmInst);
+ rmNameServerDelete((Rm_Handle)rmInst);
+ goto errorExit;
+ }
+ }
+ }
+ else if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {
+ if (initCfg->instCfg.cdCfg.cdPolicy) {
+ rmInst->u.cd.cdPolicy = initCfg->instCfg.cdCfg.cdPolicy;
+ rmInst->u.cd.cdValidInstTree = rmPolicyCreateValidInstTree((Rm_Handle)rmInst, addLinux, result);
+ if (*result == RM_OK) {
+ *result = rmPolicyValidatePolicy((Rm_Handle)rmInst);
+ }
+
+ if (*result != RM_OK) {
+ if (rmInst->u.cd.cdValidInstTree) {
+ rmPolicyFreeValidInstTree((Rm_Handle)rmInst);
+ }
+ goto errorExit;
+ }
+ }
+
+ rmInst->u.cd.allocators = NULL;
+ }
+ else if (rmInst->instType == Rm_instType_CLIENT) {
+ if (initCfg->instCfg.clientCfg.staticPolicy) {
+ rmInst->u.client.staticPolicy = initCfg->instCfg.clientCfg.staticPolicy;
+ rmInst->u.client.staticValidInstTree = rmPolicyCreateValidInstTree((Rm_Handle)rmInst, addLinux, result);
+ if (*result == RM_OK) {
+ *result = rmPolicyValidatePolicy((Rm_Handle)rmInst);
+ }
+
+ if (*result != RM_OK) {
+ if (rmInst->u.client.staticValidInstTree) {
+ rmPolicyFreeValidInstTree((Rm_Handle)rmInst);
+ }
+ goto errorExit;
+ }
+ }
+ }
+ else if (rmInst->instType == Rm_instType_SHARED_CLIENT) {
+ if (initCfg->instCfg.sharedClientCfg.sharedServerHandle) {
+ rmInst->u.sharedClient.sharedServerHandle = initCfg->instCfg.sharedClientCfg.sharedServerHandle;
+ /* Invalidate the Shared server instance structure on this core to get the latest
+ * instance data. */
+ key = Rm_osalCsEnter();
+ Rm_osalBeginMemAccess((void *)rmInst->u.sharedClient.sharedServerHandle, sizeof(Rm_Inst));
+ sharedServerInst = rmInst->u.sharedClient.sharedServerHandle;
+ if (sharedServerInst->instType != Rm_instType_SHARED_SERVER) {
+ *result = RM_ERROR_INVALID_SHARED_SERVER_HANDLE;
+ Rm_osalCsExit(key);
+ goto errorExit;
+ }
+ else {
+ /* Invalidate the policy */
+ Rm_osalBeginMemAccess((void *)sharedServerInst->u.server.globalPolicy,
+ sharedServerInst->u.server.policySize);
+ }
+ Rm_osalCsExit(key);
+ }
+ else {
+ *result = RM_ERROR_INVALID_SHARED_SERVER_HANDLE;
+ goto errorExit;
+ }
+ }
+
+ if (initCfg->instType == Rm_instType_SHARED_SERVER) {
+ /* Writeback the instance and policy for other cores */
+ Rm_osalEndMemAccess ((void *)rmInst, sizeof(Rm_Inst));
+ Rm_osalEndMemAccess ((void *)rmInst->u.server.globalPolicy, rmInst->u.server.policySize);
+ }
+ else if (rmInst->instType != Rm_instType_SHARED_CLIENT) {
+ /* Create the instance's task blocking mechanism */
+ rmInst->blockHandle = Rm_osalTaskBlockCreate();
+ }
+
+ return ((Rm_Handle) rmInst);
+errorExit:
+ if (rmInst) {
+ Rm_osalFree((void *)rmInst, sizeof(Rm_Inst));
+ }
+ return (NULL);
+}
+
+/* FUNCTION PURPOSE: Deletes an RM instance
+ ***********************************************************************
+ * DESCRIPTION: Frees all memory associated with an RM instance
+ * as long as all transports have been unregistered
+ * and the service handle has been closed
+ */
+int32_t Rm_delete(Rm_Handle rmHandle, int ignorePendingServices)
+{
+ Rm_Inst *rmInst = (Rm_Inst *)rmHandle;
+ void *key;
+
+ key = Rm_osalCsEnter();
+ if (rmInst->instType == Rm_instType_SHARED_SERVER) {
+ Rm_osalBeginMemAccess((void *)rmInst, sizeof(Rm_Inst));
+ }
+
+ if (rmInst->serviceHandle) {
+ return (RM_ERROR_CANT_DELETE_WITH_OPEN_SERV_HNDL);
+ }
+ else if (rmInst->transports) {
+ return (RM_ERROR_CANT_DELETE_WITH_REGD_TRANSPORT);
+ }
+ else if (rmInst->transactionQueue && !ignorePendingServices) {
+ return (RM_ERROR_CANT_DELETE_PENDING_TRANSACTIONS);
+ }
+
+ if ((rmInst->instType == Rm_instType_SERVER) ||
+ (rmInst->instType == Rm_instType_SHARED_SERVER)) {
+ rmAllocatorDeleteResources(rmHandle);
+ rmNameServerDelete(rmHandle);
+ rmInst->u.server.allocators = NULL;
+
+ if (rmInst->instType == Rm_instType_SHARED_SERVER) {
+ Rm_osalFree((void *)rmInst->u.server.globalPolicy, rmInst->u.server.policySize);
+ }
+ }
+ else if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {
+ rmAllocatorDeleteResources(rmHandle);
+ rmInst->u.cd.allocators = NULL;
+ }
+
+ if (rmInst->instType != Rm_instType_SHARED_CLIENT) {
+ /* Delete valid instance tree */
+ rmPolicyFreeValidInstTree(rmHandle);
+
+ /* Delete any transactions */
+ while(rmInst->transactionQueue) {
+ rmTransactionQueueDelete(rmInst, rmInst->transactionQueue->localId);
+ }
+
+ if (rmInst->instType != Rm_instType_SHARED_SERVER) {
+ /* Delete the instance's task blocking mechanism */
+ Rm_osalTaskBlockDelete(rmInst->blockHandle);
+ }
+ else {
+ Rm_osalEndMemAccess((void *)rmInst, sizeof(Rm_Inst));
+ }
+ }
+
+ Rm_osalFree((void *)rmInst, sizeof(Rm_Inst));
+
+ Rm_osalCsExit(key);
+ return (RM_OK);
+}
+
+/* FUNCTION PURPOSE: Returns RM version information
+ ***********************************************************************
+ */
+uint32_t Rm_getVersion(void)
+{
+ return RM_VERSION_ID;
+}
+
+/* FUNCTION PURPOSE: Returns RM version string
+ ***********************************************************************
+ */
+const char* Rm_getVersionStr(void)
+{
+ return rmVersionStr;
+}
+