7e30a2eba5522a8433bc97157c7c1b799aece496
[keystone-rtos/rm-lld.git] / src / rm.c
1 /**\r
2  *   @file  rm.c\r
3  *\r
4  *   @brief   \r
5  *      This is the Resource Manager source.\r
6  *\r
7  *  \par\r
8  *  ============================================================================\r
9  *  @n   (C) Copyright 2012-2013, Texas Instruments, Inc.\r
10  * \r
11  *  Redistribution and use in source and binary forms, with or without \r
12  *  modification, are permitted provided that the following conditions \r
13  *  are met:\r
14  *\r
15  *    Redistributions of source code must retain the above copyright \r
16  *    notice, this list of conditions and the following disclaimer.\r
17  *\r
18  *    Redistributions in binary form must reproduce the above copyright\r
19  *    notice, this list of conditions and the following disclaimer in the \r
20  *    documentation and/or other materials provided with the   \r
21  *    distribution.\r
22  *\r
23  *    Neither the name of Texas Instruments Incorporated nor the names of\r
24  *    its contributors may be used to endorse or promote products derived\r
25  *    from this software without specific prior written permission.\r
26  *\r
27  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \r
28  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT \r
29  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
30  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT \r
31  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, \r
32  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT \r
33  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
34  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
35  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
36  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
37  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
38  *\r
39  *  \par\r
40 */\r
41 \r
42 /* Standard includes */\r
43 #include <stdint.h>\r
44 #include <string.h>\r
45 #include <stdbool.h>\r
46 \r
47 /* RM external includes */\r
48 #include <ti/drv/rm/rm.h>\r
49 #include <ti/drv/rm/rm_services.h>\r
50 #include <ti/drv/rm/rm_transport.h>\r
51 \r
52 /* RM internal includes */\r
53 #include <ti/drv/rm/include/rm_loc.h>\r
54 #include <ti/drv/rm/include/rm_transportloc.h>\r
55 #include <ti/drv/rm/include/rm_nameserverloc.h>\r
56 #include <ti/drv/rm/include/rm_dtb_utilloc.h>\r
57 #include <ti/drv/rm/include/rm_policyloc.h>\r
58 #include <ti/drv/rm/include/rm_treeloc.h>\r
59 \r
60 /* RM LIBFDT includes */\r
61 #include <ti/drv/rm/util/libfdt/libfdt.h>\r
62 \r
63 /* Tree algorithm includes */\r
64 #include <ti/drv/rm/util/tree.h>\r
65 \r
66 /* RM OSAL layer */\r
67 #include <rm_osal.h>\r
68 \r
69 /**********************************************************************\r
70  ************************** Globals ***********************************\r
71  **********************************************************************/\r
72 \r
73 /** @brief Global Variable which describes the RM Version Information */\r
74 const char  rmVersionStr[] = RM_VERSION_STR ":" __DATE__  ":" __TIME__;\r
75 \r
76 /**********************************************************************\r
77  ************************ Local Functions *****************************\r
78  **********************************************************************/\r
79 \r
80 /* FUNCTION PURPOSE: Initializes a RM inst's transaction sequence number\r
81  ***********************************************************************\r
82  * DESCRIPTION: The RM instance transaction sequence number can never\r
83  *              have a value of 0 to avoid conflicts with transactions\r
84  *              that have a remoteOriginatingId of 0 (transaction ID\r
85  *              will be used as the remoteOriginatingId for\r
86  *              transactions that are responses to requests).\r
87  */\r
88 uint32_t transactionInitSequenceNum(void)\r
89 {\r
90     return (1);\r
91 }\r
92 \r
93 /* FUNCTION PURPOSE: Provides a sequence number for new transactions\r
94  ***********************************************************************\r
95  * DESCRIPTION: Returns a sequence number for a new transaction\r
96  *              specific to a RM instance.  Handles rollover of\r
97  *              sequence number.\r
98  */\r
99 uint32_t transactionGetSequenceNum(Rm_Inst *rmInst)\r
100 {\r
101     uint32_t sequenceNum = 0;\r
102 \r
103     if (rmInst->transactionSeqNum + 1 < rmInst->transactionSeqNum) {\r
104         /* Rollover */\r
105         sequenceNum = rmInst->transactionSeqNum;\r
106         rmInst->transactionSeqNum = transactionInitSequenceNum();\r
107     }\r
108     else {\r
109         sequenceNum = rmInst->transactionSeqNum++;\r
110     }    \r
111     return (sequenceNum);\r
112 }\r
113 \r
114 /* FUNCTION PURPOSE: Creates a resource allocator\r
115  ***********************************************************************\r
116  * DESCRIPTION: Returns a newly Created and initialized resource\r
117  *              The allocator is also stored in the RM instance\r
118  *              allocator list.            \r
119  */\r
120 Rm_Allocator *allocatorAdd(Rm_Inst *rmInst, const char *resourceName)\r
121 {\r
122     Rm_Allocator *allocators   = rmInst->allocators;\r
123     Rm_Allocator *newAllocator = NULL;\r
124 \r
125     newAllocator = Rm_osalMalloc(sizeof(Rm_Allocator));\r
126 \r
127     if (newAllocator) {\r
128         memset((void *)newAllocator, 0, sizeof(Rm_Allocator));\r
129         strncpy(newAllocator->resourceName, resourceName, RM_NAME_MAX_CHARS);\r
130         newAllocator->allocatorRootEntry = NULL;\r
131         newAllocator->nextAllocator = NULL;  \r
132 \r
133         /* Add allocator to end of list */\r
134         if (allocators) {\r
135             while (allocators->nextAllocator) {\r
136                 allocators = allocators->nextAllocator;\r
137             }\r
138             allocators->nextAllocator = newAllocator;\r
139         }\r
140         else {\r
141             rmInst->allocators = newAllocator;\r
142         }\r
143     }\r
144     return (newAllocator);\r
145 }\r
146 \r
147 /* FUNCTION PURPOSE: Deletes a resource allocator\r
148  ***********************************************************************\r
149  * DESCRIPTION: Deletes a resource allocator based on the given\r
150  *              resource name.  The resource allocator will be\r
151  *              removed from the RM instance allocator list.\r
152  */\r
153 int32_t allocatorDelete(Rm_Inst *rmInst, char *resourceName)\r
154 {\r
155     Rm_Allocator *allocator = rmInst->allocators;\r
156     Rm_Allocator *prevAllocator = NULL;\r
157     int32_t       retVal = RM_OK;\r
158 \r
159     while (allocator) {\r
160         if (strncmp(allocator->resourceName, resourceName, RM_NAME_MAX_CHARS) == 0) {\r
161             break;             \r
162         }\r
163         prevAllocator = allocator;\r
164         allocator = allocator->nextAllocator;\r
165     }\r
166 \r
167     if (allocator) {\r
168         if (prevAllocator == NULL) {\r
169             rmInst->allocators = allocator->nextAllocator;\r
170         }\r
171         else {\r
172             prevAllocator->nextAllocator = allocator->nextAllocator;\r
173         }\r
174         Rm_osalFree((void *)allocator, sizeof(Rm_Allocator));\r
175     }\r
176     else {\r
177         retVal = RM_ERROR_RES_ALLOCATOR_DOES_NOT_EXIST;\r
178     }\r
179     return (retVal);\r
180 }\r
181 \r
182 /* FUNCTION PURPOSE: Adds an owner to an allocator resource\r
183  ***********************************************************************\r
184  * DESCRIPTION: Adds a RM instance node to a resource node's\r
185  *              list of owners.\r
186  */\r
187 void addOwner(Rm_ResourceNode *node, void *serviceInstNode)\r
188 {\r
189     Rm_Owner *ownerList = node->ownerList;\r
190     Rm_Owner *newOwner = NULL;\r
191 \r
192     newOwner = Rm_osalMalloc(sizeof(Rm_Owner));\r
193 \r
194     if (newOwner) {\r
195         newOwner->instNameNode = serviceInstNode;\r
196         newOwner->nextOwner = NULL;  \r
197 \r
198         /* Add owner entry to end of list */\r
199         if (ownerList) {\r
200             while (ownerList->nextOwner) {\r
201                 ownerList = ownerList->nextOwner;\r
202             }\r
203             ownerList->nextOwner = newOwner;\r
204         }\r
205         else {\r
206             node->ownerList = newOwner;\r
207         }\r
208 \r
209         node->allocationCount++;\r
210         newOwner->instNameNode->allocRefCount++;\r
211     }\r
212 }\r
213 \r
214 /* FUNCTION PURPOSE: Checks a resource node's ownership\r
215  ***********************************************************************\r
216  * DESCRIPTION: Returns TRUE if the provided instance node is\r
217  *              in the list of resource node owners.  Otherwise,\r
218  *              returns FALSE.\r
219  */\r
220 bool isOwnedBy(Rm_ResourceNode *node, void *serviceInstNode)\r
221 {\r
222     Rm_Owner *owner = node->ownerList;\r
223 \r
224     while (owner) {\r
225         if (owner->instNameNode == serviceInstNode) {\r
226             return(true);           \r
227         }\r
228         owner = owner->nextOwner;\r
229     }\r
230     return(false);\r
231 }\r
232 \r
233 /* FUNCTION PURPOSE: Compares two resource node's owners\r
234  ***********************************************************************\r
235  * DESCRIPTION: Returns TRUE if the owners of two resource nodes \r
236  *              are equivalent.  Otherwise, returns FALSE.\r
237  */\r
238 bool compareResourceNodeOwners(Rm_ResourceNode *node1, Rm_ResourceNode *node2)\r
239 {\r
240     Rm_Owner *node1Owners = node1->ownerList;\r
241     Rm_Owner *node2Owners = node2->ownerList;\r
242     bool      matchedInst;\r
243 \r
244     if (node1->allocationCount == node2->allocationCount) {\r
245         while (node1Owners) {\r
246             matchedInst = false;\r
247             while (node2Owners) {\r
248                 if (node1Owners->instNameNode == node2Owners->instNameNode) {\r
249                     matchedInst = true;\r
250                     break;\r
251                 }\r
252                 node2Owners = node2Owners->nextOwner;\r
253             }\r
254 \r
255             if (matchedInst) {\r
256                 node2Owners = node2->ownerList;\r
257                 node1Owners = node1Owners->nextOwner;\r
258             }\r
259             else {\r
260                 return(false);\r
261             }                \r
262         }\r
263     }\r
264     else {\r
265         return(false);\r
266     }  \r
267     \r
268     return(true);\r
269 }\r
270 \r
271 /* FUNCTION PURPOSE: Deletes an owner from an allocator resource\r
272  ***********************************************************************\r
273  * DESCRIPTION: Removes a RM instance node from a resource node's\r
274  *              list of owners.\r
275  */\r
276 void deleteOwner(Rm_ResourceNode *node, void *serviceInstNode)\r
277 {\r
278     Rm_Owner *owner = node->ownerList;\r
279     Rm_Owner *prevOwner = NULL;\r
280 \r
281     while (owner) {\r
282         if (owner->instNameNode == serviceInstNode) {\r
283             break;             \r
284         }\r
285         prevOwner = owner;\r
286         owner = owner->nextOwner;\r
287     }\r
288 \r
289     if (prevOwner == NULL) {\r
290         node->ownerList = owner->nextOwner;\r
291     }\r
292     else {\r
293         prevOwner->nextOwner = owner->nextOwner;\r
294     }\r
295     \r
296     node->allocationCount--;\r
297     owner->instNameNode->allocRefCount--;\r
298     Rm_osalFree((void *)owner, sizeof(Rm_Owner));\r
299 }\r
300 \r
301 /* FUNCTION PURPOSE: Copies the owners of a resource node\r
302  ***********************************************************************\r
303  * DESCRIPTION: Creates a list of resource owners for the destination\r
304  *              resource node that is equivalent to the the \r
305  *              source resource node's owners\r
306  */\r
307 void copyOwners(Rm_ResourceNode *dstNode, Rm_ResourceNode *srcNode)\r
308 {\r
309     Rm_Owner *srcOwnerList = srcNode->ownerList;\r
310     Rm_Owner *dstNewOwner;\r
311     Rm_Owner *dstPrevOwner;\r
312 \r
313     dstNode->allocationCount = srcNode->allocationCount;\r
314 \r
315     while (srcOwnerList) {\r
316         dstNewOwner = Rm_osalMalloc(sizeof(Rm_Owner));\r
317         dstNewOwner->instNameNode = srcOwnerList->instNameNode;\r
318         dstNewOwner->nextOwner = NULL;\r
319 \r
320         if (dstNode->ownerList == NULL) {\r
321             dstNode->ownerList = dstNewOwner;\r
322         }\r
323         else {\r
324             dstPrevOwner->nextOwner = dstNewOwner;\r
325         }\r
326         dstPrevOwner = dstNewOwner;\r
327         srcOwnerList = srcOwnerList->nextOwner;\r
328     }\r
329 }\r
330 \r
331 /* FUNCTION PURPOSE: Clears a resource node's owners\r
332  ***********************************************************************\r
333  * DESCRIPTION: Deletes all owners from the owners list of a \r
334  *              resource node.\r
335  */\r
336 void clearOwners(Rm_ResourceNode *node)\r
337 {\r
338     Rm_Owner *owner = node->ownerList;\r
339     Rm_Owner *nextOwner;\r
340 \r
341     while (owner) {\r
342         nextOwner = owner->nextOwner;\r
343         node->allocationCount--;\r
344         Rm_osalFree((void *)owner, sizeof(Rm_Owner));\r
345         owner = nextOwner;\r
346     }\r
347 }\r
348 \r
349 /* FUNCTION PURPOSE: Creates a new resource tree\r
350  ***********************************************************************\r
351  * DESCRIPTION: Creates a new resource tree using the provided\r
352  *              resource name and value range.  The name and value\r
353  *              typically originate from the GRL.\r
354  */\r
355 int32_t createResourceTree(Rm_Inst *rmInst, const char *resourceName, Rm_ResourceRange *range)\r
356 {\r
357     Rm_Allocator    *allocator = NULL;\r
358     Rm_ResourceTree *treeRootEntry = NULL;\r
359     Rm_ResourceNode *treeNode = NULL;\r
360     Rm_ResourceNode *collidingNode = NULL;\r
361 \r
362     allocator = allocatorAdd(rmInst, resourceName);\r
363     treeRootEntry = Rm_osalMalloc(sizeof(Rm_ResourceTree));\r
364     RB_INIT(treeRootEntry);\r
365 \r
366     while (range != NULL) {\r
367         treeNode = rmResourceNodeNew(range->base, range->length);\r
368         collidingNode = RB_INSERT(_Rm_AllocatorResourceTree, treeRootEntry, treeNode);\r
369 \r
370         if (collidingNode) {\r
371             Rm_ResourceNode *nextNode = NULL;\r
372             \r
373             /* Node that was inserted collides with existing node.  Destroy tree and return error */\r
374             for (treeNode = RB_MIN(_Rm_AllocatorResourceTree, treeRootEntry); treeNode != NULL; treeNode = nextNode) {\r
375                         nextNode = RB_NEXT(_Rm_AllocatorResourceTree, treeRootEntry, treeNode);\r
376                         RB_REMOVE(_Rm_AllocatorResourceTree, treeRootEntry, nextNode);\r
377                 rmResourceNodeFree(treeNode);\r
378                 }\r
379             Rm_osalFree((void *)treeRootEntry, sizeof(Rm_ResourceTree));\r
380             allocatorDelete(rmInst, allocator->resourceName);\r
381             return (RM_ERROR_GRL_RES_SPECIFIED_MORE_THAN_ONCE);\r
382         }\r
383         range = range->nextRange;\r
384     }\r
385     \r
386     allocator->allocatorRootEntry = treeRootEntry;\r
387     return(RM_OK);\r
388 }\r
389 \r
390 /* FUNCTION PURPOSE: Preallocates a resource\r
391  ***********************************************************************\r
392  * DESCRIPTION: Called when an allocate request is made but the base \r
393  *              is unspecified.  The preallocation algorithm looks at \r
394  *              available resources as well as policy permissions to \r
395  *              determine a resource range that satisfies the request.\r
396  *              If a valid range is found it will be returned for the \r
397  *              treeAllocate algorithm to handle.\r
398  */\r
399 int32_t treePreAllocate(Rm_Inst *rmInst, Rm_Allocator *allocator, int32_t resourcePolicy,\r
400                         Rm_AllocatorOpInfo *opInfo)\r
401 {\r
402     Rm_ResourceNode    findNode;\r
403     Rm_ResourceNode   *matchingNode = NULL;\r
404     uint32_t           matchingEnd;\r
405     uint32_t           rangeIndex;\r
406     bool               resourceFound = false;\r
407     Rm_PolicyCheckType policyCheckType;\r
408     Rm_PolicyCheckCfg  policyCheckCfg;\r
409     bool               nodePassesPolicy;\r
410     int32_t            retVal = RM_SERVICE_PROCESSING;    \r
411 \r
412     opInfo->resourceInfo->base = rmPolicyGetResourceBase(rmInst->policy, opInfo->serviceSrcInstNode, \r
413                                                           resourcePolicy, opInfo->allocType, \r
414                                                           &retVal);\r
415     if (retVal != RM_SERVICE_PROCESSING) {\r
416         return (retVal);\r
417     }\r
418 \r
419     if (opInfo->resourceInfo->alignment == RM_RESOURCE_ALIGNMENT_UNSPECIFIED) {  \r
420         /* Get alignment from policy */\r
421         opInfo->resourceInfo->alignment = rmPolicyGetResourceAlignment(rmInst->policy, resourcePolicy);\r
422     }\r
423     \r
424     if (opInfo->resourceInfo->alignment == 0) {\r
425         opInfo->resourceInfo->alignment = 1;\r
426     }    \r
427 \r
428     memset((void *)&findNode, 0, sizeof(Rm_ResourceNode));\r
429     findNode.base = opInfo->resourceInfo->base;\r
430     findNode.length = opInfo->resourceInfo->length;\r
431 \r
432     /* Configure policy checking structure */\r
433     memset((void *)&policyCheckCfg, 0, sizeof(Rm_PolicyCheckCfg));\r
434     if (RM_policy_GET_PERM(opInfo->allocType, RM_POLICY_PERM_INIT_SHIFT)) {\r
435         policyCheckType = Rm_policyCheck_INIT;\r
436     }\r
437     else if (RM_policy_GET_PERM(opInfo->allocType, RM_POLICY_PERM_USE_SHIFT)) {\r
438         policyCheckType = Rm_policyCheck_USE;\r
439     }\r
440     policyCheckCfg.policyDtb = rmInst->policy;\r
441     policyCheckCfg.resourceOffset = resourcePolicy;\r
442     \r
443     do {\r
444         matchingNode = RB_FIND(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, &findNode);\r
445         \r
446         if (matchingNode) {\r
447             nodePassesPolicy = false;\r
448             policyCheckCfg.type = policyCheckType;\r
449             policyCheckCfg.validInstNode = opInfo->serviceSrcInstNode;\r
450             policyCheckCfg.resourceBase = findNode.base;\r
451             policyCheckCfg.resourceLength = findNode.length;\r
452             nodePassesPolicy = rmPolicyCheckPrivilege(&policyCheckCfg, &retVal);    \r
453             \r
454             if (nodePassesPolicy && (matchingNode->allocationCount > 0)) {\r
455                 /* Check exclusive privileges of instance requesting resource.  Requesting\r
456                  * instance with exclusive privileges can't reserve resource if already owned*/\r
457                 policyCheckCfg.type = Rm_policyCheck_EXCLUSIVE;\r
458                 policyCheckCfg.validInstNode = opInfo->serviceSrcInstNode;\r
459                 nodePassesPolicy = !rmPolicyCheckPrivilege(&policyCheckCfg, &retVal);\r
460             }\r
461             \r
462             if (nodePassesPolicy && (matchingNode->allocationCount == 1)) {\r
463                 /* Check exclusive privileges of instance that currently owns resource */\r
464                 policyCheckCfg.type = Rm_policyCheck_EXCLUSIVE;\r
465                 policyCheckCfg.validInstNode = matchingNode->ownerList->instNameNode;\r
466                 nodePassesPolicy = !rmPolicyCheckPrivilege(&policyCheckCfg, &retVal);\r
467             }\r
468 \r
469             if (retVal != RM_SERVICE_PROCESSING) {\r
470                 break;\r
471             }\r
472 \r
473             if (nodePassesPolicy) {\r
474                 matchingEnd = matchingNode->base + matchingNode->length - 1;\r
475                 /* Initialize indexer to be first resource value that alignment */\r
476                 rangeIndex = findNode.base;\r
477                 if (rangeIndex % opInfo->resourceInfo->alignment) {\r
478                     rangeIndex += (opInfo->resourceInfo->alignment -\r
479                                   (rangeIndex % opInfo->resourceInfo->alignment));\r
480                 }\r
481                 \r
482                 if ((rangeIndex + opInfo->resourceInfo->length - 1) <= matchingEnd) {\r
483                     /* Block of unallocated resources within matchingNode that satisfies\r
484                      * allocate requirements */\r
485                     opInfo->resourceInfo->base = rangeIndex;\r
486                     resourceFound = true;\r
487                 }     \r
488             }\r
489             \r
490             if (!resourceFound) {\r
491                 /* Check next resource node for available resources */\r
492                 findNode.base = matchingNode->base + matchingNode->length;\r
493             }\r
494         }\r
495         else {\r
496             retVal = RM_SERVICE_DENIED_RES_ALLOC_REQS_NOT_MET;\r
497         }\r
498     } while ((!resourceFound) && \r
499              (retVal != RM_SERVICE_DENIED_RES_ALLOC_REQS_NOT_MET));\r
500 \r
501     return(retVal); \r
502 }\r
503 \r
504 /* FUNCTION PURPOSE: Allocates a resource\r
505  ***********************************************************************\r
506  * DESCRIPTION: Will attempt to allocate the resource with specified\r
507  *              base and length from the resource's allocator.  The\r
508  *              allocation algorithm will verify the allocation against\r
509  *              the policy permissions for the instance requesting the\r
510  *              allocation.  If the policy allows the allocation the \r
511  *              algorithm will allocate the resource then combine any\r
512  *              resource nodes that may have become equivalent (in terms\r
513  *              of ownership) after the allocation.\r
514  */\r
515 int32_t treeAllocate(Rm_Inst *rmInst, Rm_Allocator *allocator, int32_t resourcePolicy,\r
516                      Rm_AllocatorOpInfo *opInfo)\r
517 {\r
518     Rm_ResourceNode     findNode;\r
519     Rm_ResourceNode    *matchingNode = NULL;\r
520     Rm_ResourceNode    *leftNode = NULL;\r
521     Rm_ResourceNode    *rightNode = NULL;\r
522     Rm_PolicyCheckType  policyCheckType;    \r
523     Rm_PolicyCheckCfg   policyCheckCfg;\r
524     bool                allocPassesPolicy;\r
525     bool                combineLeft = false;\r
526     bool                combineRight = false;    \r
527     uint32_t            findEnd;\r
528     uint32_t            matchingEnd;  \r
529     int32_t             retVal = RM_SERVICE_PROCESSING;\r
530 \r
531     memset((void *)&findNode, 0, sizeof(Rm_ResourceNode));\r
532     findNode.base = opInfo->resourceInfo->base;\r
533     findNode.length = opInfo->resourceInfo->length;\r
534     matchingNode = RB_FIND(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, &findNode);\r
535 \r
536     /* Prepare privilege checks */\r
537     memset((void *)&policyCheckCfg, 0, sizeof(Rm_PolicyCheckCfg));\r
538     if (RM_policy_GET_PERM(opInfo->allocType, RM_POLICY_PERM_INIT_SHIFT)) {\r
539         policyCheckType = Rm_policyCheck_INIT;\r
540     }\r
541     else if (RM_policy_GET_PERM(opInfo->allocType, RM_POLICY_PERM_USE_SHIFT)) {\r
542         policyCheckType = Rm_policyCheck_USE;\r
543     }\r
544 \r
545     if (matchingNode) {\r
546         findEnd = findNode.base + findNode.length - 1;\r
547         matchingEnd = matchingNode->base + matchingNode->length - 1;\r
548         \r
549         if ((findNode.base >= matchingNode->base) && (findEnd <= matchingEnd)) {\r
550             if (opInfo->serviceSrcInstNode == rmPolicyGetLinuxInstNode(rmInst->validInstances)) {\r
551                 /* Bypass policy checks since Linux Kernel has full privileges */\r
552                 allocPassesPolicy = true;\r
553             }\r
554             else {\r
555                 policyCheckCfg.policyDtb = rmInst->policy;\r
556                 policyCheckCfg.resourceOffset = resourcePolicy;    \r
557                 policyCheckCfg.type = policyCheckType;\r
558                 policyCheckCfg.validInstNode = opInfo->serviceSrcInstNode;\r
559                 policyCheckCfg.resourceBase = findNode.base;\r
560                 policyCheckCfg.resourceLength = findNode.length;\r
561                 allocPassesPolicy = rmPolicyCheckPrivilege(&policyCheckCfg, &retVal);\r
562                 if (!allocPassesPolicy) {\r
563                     if (policyCheckType == Rm_policyCheck_INIT) {\r
564                         retVal = RM_SERVICE_DENIED_INIT_PERM_NOT_GIVEN;\r
565                     }\r
566                     else {\r
567                         retVal = RM_SERVICE_DENIED_USE_PERM_NOT_GIVEN;\r
568                     }\r
569                 }\r
570                 \r
571                 if (allocPassesPolicy && (matchingNode->allocationCount > 0)) {\r
572                     /* Check exclusive privileges of instance requesting resource.  Requesting\r
573                      * instance with exclusive privileges can't reserve resource if already owned*/\r
574                     policyCheckCfg.type = Rm_policyCheck_EXCLUSIVE;\r
575                     policyCheckCfg.validInstNode = opInfo->serviceSrcInstNode;\r
576                     allocPassesPolicy = !rmPolicyCheckPrivilege(&policyCheckCfg, &retVal);\r
577                     if (!allocPassesPolicy) {\r
578                         retVal = RM_SERVICE_DENIED_EXCLUSIVE_RES_ALLOCD;\r
579                     }\r
580                 }\r
581                 if (allocPassesPolicy && (matchingNode->allocationCount == 1)) {\r
582                     /* Check exclusive privileges of instance that currently owns resource */\r
583                     policyCheckCfg.type = Rm_policyCheck_EXCLUSIVE;\r
584                     policyCheckCfg.validInstNode = matchingNode->ownerList->instNameNode;\r
585                     allocPassesPolicy = !rmPolicyCheckPrivilege(&policyCheckCfg, &retVal);\r
586                     if (!allocPassesPolicy) {\r
587                         retVal = RM_SERVICE_DENIED_ALLOCD_TO_EXCLUSIVE_INST;\r
588                     }                \r
589                 }  \r
590             }\r
591             \r
592             if (allocPassesPolicy) {   \r
593                 if ((findNode.base == matchingNode->base) && (findEnd == matchingEnd)) {\r
594                     /* findNode range matches matchingNode range\r
595                      *\r
596                      *   |<--left node-->||<--matched  node-->||<--right node-->| => existing node\r
597                      *                    |<--alloc request-->|  => requested resources\r
598                      */                     \r
599                     leftNode = RB_PREV(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
600                     rightNode = RB_NEXT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
601                     RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
602                     addOwner(matchingNode, opInfo->serviceSrcInstNode);\r
603 \r
604                     if (leftNode && compareResourceNodeOwners(leftNode, matchingNode)) {\r
605                         RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);\r
606                         combineLeft = true;\r
607                     }\r
608                     if (rightNode && compareResourceNodeOwners(rightNode, matchingNode)) {\r
609                         RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);\r
610                         combineRight = true;\r
611                     }\r
612 \r
613                     if (combineLeft && combineRight) {\r
614                         /* Combine all three nodes into matchingNode */\r
615                         matchingNode->base = leftNode->base;\r
616                         matchingNode->length = leftNode->length + matchingNode->length + rightNode->length;\r
617 \r
618                         clearOwners(leftNode);\r
619                         rmResourceNodeFree(leftNode);\r
620                         clearOwners(rightNode);\r
621                         rmResourceNodeFree(rightNode);                        \r
622                     }\r
623                     else if (combineLeft) {\r
624                         /* Combine left and matching nodes.  Reinsert right. */\r
625                         matchingNode->base = leftNode->base;\r
626                         matchingNode->length += leftNode->length;\r
627 \r
628                         clearOwners(leftNode);\r
629                         rmResourceNodeFree(leftNode);\r
630                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);                        \r
631                     }\r
632                     else if (combineRight) {\r
633                         /* Combine right and matching nodes.  Reinsert left. */\r
634                         matchingNode->length += rightNode->length;\r
635 \r
636                         clearOwners(rightNode);\r
637                         rmResourceNodeFree(rightNode);\r
638                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);\r
639                     }\r
640                     else {\r
641                         /* No combine. */\r
642                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);\r
643                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);\r
644                     }\r
645 \r
646                     /* Always reinsert matchingNode */                \r
647                     RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);   \r
648                 }   \r
649                 else if ((findNode.base > matchingNode->base) && (findEnd < matchingEnd)) {\r
650                     /* findNode range is subset of matchingNode range and neither boundary is\r
651                      * equivalent.\r
652                      *\r
653                      * |<----------matched node---------->|\r
654                      *        |<---alloc request--->|\r
655                      */ \r
656                     RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
657                     leftNode = rmResourceNodeNew(matchingNode->base, findNode.base - matchingNode->base);\r
658                     copyOwners(leftNode, matchingNode);\r
659                     rightNode = rmResourceNodeNew(findNode.base + findNode.length, matchingEnd - findEnd);\r
660                     copyOwners(rightNode, matchingNode);\r
661 \r
662                     matchingNode->base = findNode.base;                                    \r
663                     matchingNode->length = findNode.length;\r
664                     addOwner(matchingNode, opInfo->serviceSrcInstNode);\r
665 \r
666                     /* Insert all the nodes */\r
667                     RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
668                     RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);\r
669                     RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);\r
670                 }  \r
671                 else {    \r
672                     if (findNode.base == matchingNode->base) {\r
673                         /* findNode base and matchingNode base are equivalent.  May be combine\r
674                          * possibilities to the left\r
675                          *\r
676                          * |<---left node (alloc'd)--->||<----------matched node---------->|\r
677                          *                              |<---findNode (alloc req)--->|\r
678                          */                         \r
679                         leftNode = RB_PREV(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
680                         RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
681                         /* Add allocating instance to owner list for compare with leftNode */\r
682                         addOwner(matchingNode, opInfo->serviceSrcInstNode);\r
683                         \r
684                         if (leftNode && compareResourceNodeOwners(leftNode, matchingNode)) {\r
685                             RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);\r
686                             /* Combine leftNode and findNode */\r
687                             leftNode->length += findNode.length;\r
688                         }\r
689                         else {\r
690                             leftNode = rmResourceNodeNew(findNode.base, findNode.length);\r
691                             copyOwners(leftNode, matchingNode);\r
692                         }\r
693 \r
694                         /* Account for leftNode in matchingNode */\r
695                         matchingNode->base = findNode.base + findNode.length;\r
696                         matchingNode->length = matchingEnd - findEnd;  \r
697 \r
698                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);\r
699                     }\r
700                     else if (findEnd == matchingEnd) {\r
701                         /* findNode end and matchingNode end are equivalent.  May be combine\r
702                          * possibilities to the right\r
703                          *\r
704                          * |<----------matched node---------->||<---right node (alloc'd)--->|\r
705                          *       |<---findNode (alloc req)--->| \r
706                          */                        \r
707                         rightNode = RB_NEXT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
708                         RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
709                         /* Add allocating instance to owner list for compare with rightNode */\r
710                         addOwner(matchingNode, opInfo->serviceSrcInstNode);\r
711                         \r
712                         if (rightNode && compareResourceNodeOwners(rightNode, matchingNode)) {\r
713                             RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);\r
714                             /* Combine rightNode and findNode */\r
715                             rightNode->base = findNode.base;\r
716                             rightNode->length += findNode.length;\r
717                         }\r
718                         else {\r
719                             rightNode = rmResourceNodeNew(findNode.base, findNode.length);\r
720                             copyOwners(rightNode, matchingNode);\r
721                         }\r
722 \r
723                         /* Account for rightNode in matchingNode */\r
724                         matchingNode->length -= findNode.length;  \r
725 \r
726                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);\r
727                     }\r
728                     /* Remove allocating instance from leftover matchingNode */\r
729                     deleteOwner(matchingNode, opInfo->serviceSrcInstNode);\r
730                     RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
731                 }\r
732                 retVal = RM_SERVICE_APPROVED;\r
733             }\r
734         }\r
735         else {\r
736             retVal = RM_SERVICE_DENIED_PARTIAL_ALLOCATION;\r
737         }\r
738     }\r
739     else {\r
740         retVal = RM_SERVICE_DENIED_RES_RANGE_DOES_NOT_EXIST;\r
741     }\r
742 \r
743     return(retVal);        \r
744 }\r
745 \r
746 /* FUNCTION PURPOSE: Frees a resource\r
747  ***********************************************************************\r
748  * DESCRIPTION: Will attempt to free the resource with specified\r
749  *              base and length from the resource's allocator.  The\r
750  *              free algorithm will verify the free request parameters\r
751  *              match an allocated range for the resource and that the\r
752  *              range is owned by the instance requesting the free. If\r
753  *              the free is validated the algorithm will free the \r
754  *              resource then combine any resource nodes that may have\r
755  *              become equivalent (in terms of ownership) after the\r
756  *              allocation.\r
757  */\r
758 int32_t treeFree(Rm_Allocator *allocator, Rm_AllocatorOpInfo *opInfo)\r
759 {\r
760     Rm_ResourceNode  findNode;\r
761     Rm_ResourceNode *matchingNode = NULL;\r
762     Rm_ResourceNode *leftNode = NULL;\r
763     Rm_ResourceNode *rightNode = NULL;\r
764     bool             combineLeft = false;\r
765     bool             combineRight = false;\r
766     uint32_t         findEnd;\r
767     uint32_t         matchingEnd;\r
768     int32_t          retVal;\r
769 \r
770     memset((void *)&findNode, 0, sizeof(Rm_ResourceNode));\r
771     findNode.base = opInfo->resourceInfo->base;\r
772     findNode.length = opInfo->resourceInfo->length;\r
773     matchingNode = RB_FIND(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, &findNode);\r
774 \r
775     if (matchingNode) {\r
776         findEnd = findNode.base + findNode.length - 1;\r
777         matchingEnd = matchingNode->base + matchingNode->length - 1;\r
778         \r
779         if ((findNode.base >= matchingNode->base) && (findEnd <= matchingEnd)) {  \r
780             if (matchingNode->allocationCount) {\r
781                 if (isOwnedBy(matchingNode, opInfo->serviceSrcInstNode)) {\r
782                     if ((findNode.base == matchingNode->base) && (findEnd == matchingEnd))\r
783                     {\r
784                         /* Case 1: Free range equals allocated matched node exactly. Attempt to combine \r
785                          *         freed node with nodes to left and right.\r
786                          *\r
787                          * |<--left node-->||<---matched node--->||<--right node-->|\r
788                          *                  |<---free request--->|\r
789                          */ \r
790                         leftNode = RB_PREV(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
791                         rightNode = RB_NEXT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
792                         RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
793                         deleteOwner(matchingNode, opInfo->serviceSrcInstNode);\r
794 \r
795                         if (leftNode && compareResourceNodeOwners(leftNode, matchingNode)) {\r
796                             RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);\r
797                             combineLeft = true;\r
798                         }\r
799                         if (rightNode && compareResourceNodeOwners(rightNode, matchingNode)) {\r
800                             RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);\r
801                             combineRight = true;\r
802                         }\r
803 \r
804                         if (combineLeft && combineRight) {\r
805                             /* Combine all three nodes into matchingNode */\r
806                             matchingNode->base = leftNode->base;\r
807                             matchingNode->length = leftNode->length + matchingNode->length + rightNode->length;\r
808 \r
809                             clearOwners(leftNode);\r
810                             rmResourceNodeFree(leftNode);\r
811                             clearOwners(rightNode);\r
812                             rmResourceNodeFree(rightNode);                        \r
813                         }\r
814                         else if (combineLeft) {\r
815                             /* Combine left and matching nodes.  Reinsert right. */\r
816                             matchingNode->base = leftNode->base;\r
817                             matchingNode->length += leftNode->length;\r
818 \r
819                             clearOwners(leftNode);\r
820                             rmResourceNodeFree(leftNode);\r
821                             RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);                        \r
822                         }\r
823                         else if (combineRight) {\r
824                             /* Combine right and matching nodes.  Reinsert left. */\r
825                             matchingNode->length += rightNode->length;\r
826 \r
827                             clearOwners(rightNode);\r
828                             rmResourceNodeFree(rightNode);\r
829                             RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);\r
830                         }\r
831                         else {\r
832                             /* No combine. */\r
833                             RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);\r
834                             RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);\r
835                         }\r
836 \r
837                         /* Always reinsert matchingNode */\r
838                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);                    \r
839                     }\r
840                     else if ((findNode.base > matchingNode->base) && (findEnd < matchingEnd)) {\r
841                         /* Case 2: Free range is less than range in matched node. Split\r
842                          *         matched node into three nodes.\r
843                          *\r
844                          * |<----------matched node---------->|\r
845                          *        |<---free request--->|\r
846                          *\r
847                          * Remove instance from AllocatedTo list then add it back in for side nodes for\r
848                          * proper accounting of allocations in validInstance list\r
849                          */ \r
850                         RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
851                         deleteOwner(matchingNode, opInfo->serviceSrcInstNode);\r
852                         \r
853                         leftNode = rmResourceNodeNew(matchingNode->base, findNode.base - matchingNode->base);\r
854                         copyOwners(leftNode, matchingNode);\r
855                         addOwner(leftNode, opInfo->serviceSrcInstNode);\r
856                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);\r
857                         \r
858                         rightNode = rmResourceNodeNew(findNode.base + findNode.length, matchingEnd - findEnd);\r
859                         copyOwners(rightNode, matchingNode);\r
860                         addOwner(rightNode, opInfo->serviceSrcInstNode);\r
861                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);\r
862 \r
863                         matchingNode->base = findNode.base;                                    \r
864                         matchingNode->length = findNode.length;\r
865                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
866                     }\r
867                     else {                        \r
868                         if (findNode.base == matchingNode->base) {\r
869                             /* Case 3: Free range is on left boundary of matched node. Try to \r
870                              *         combine free range with left node.\r
871                              *\r
872                              * |<---left node (free)--->||<----------matched node---------->|\r
873                              *                           |<---findNode (free req)--->|\r
874                              */ \r
875 \r
876                             leftNode = RB_PREV(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
877                             RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
878                             /* Remove freeing instance from owner list for compare with leftNode */\r
879                             deleteOwner(matchingNode, opInfo->serviceSrcInstNode);\r
880                             \r
881                             if (leftNode && compareResourceNodeOwners(leftNode, matchingNode)) {\r
882                                 RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);\r
883                                 /* Combine leftNode and findNode */\r
884                                 leftNode->length += findNode.length;\r
885                             }\r
886                             else {\r
887                                 leftNode = rmResourceNodeNew(findNode.base, findNode.length);\r
888                                 copyOwners(leftNode, matchingNode);\r
889                             }\r
890 \r
891                             /* Remove leftNode range from matchingNode */\r
892                             matchingNode->base = findNode.base + findNode.length;\r
893                             matchingNode->length = matchingEnd - findEnd;  \r
894                             RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);\r
895                         }\r
896                         else if (findEnd == matchingEnd) {\r
897                             /* Case 4: Free range is on right boundary of matched node. Try to \r
898                              *         combine free range with right node.\r
899                              *\r
900                              * |<----------matched node---------->||<---right node (free)--->|\r
901                              *        |<---findNode (free req)--->|\r
902                              */ \r
903                             \r
904                             rightNode = RB_NEXT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
905                             RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode); \r
906                             /* Remove freeing instance from owner list for compare with rightNode */\r
907                             deleteOwner(matchingNode, opInfo->serviceSrcInstNode);\r
908                             \r
909                             if (rightNode && compareResourceNodeOwners(rightNode, matchingNode)) {\r
910                                 RB_REMOVE(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);\r
911                                 /* Combine rightNode and findNode */\r
912                                 rightNode->base = findNode.base;\r
913                                 rightNode->length += findNode.length;\r
914                             }\r
915                             else {\r
916                                 rightNode = rmResourceNodeNew(findNode.base, findNode.length);\r
917                                 copyOwners(rightNode, matchingNode);\r
918                             }\r
919 \r
920                             /* Remove rightNode range from matchingNode */\r
921                             matchingNode->length -= findNode.length;  \r
922                             RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);\r
923                         }\r
924 \r
925                         /* Add freeing instance back into matchingNode allocations */\r
926                         addOwner(matchingNode, opInfo->serviceSrcInstNode);\r
927                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);\r
928                     }\r
929 \r
930                     retVal = RM_SERVICE_APPROVED;\r
931                 }\r
932                 else {\r
933                     retVal = RM_SERVICE_DENIED_RES_NOT_ALLOCD_TO_INST;\r
934                 }\r
935             }\r
936             else {\r
937                 retVal = RM_SERVICE_DENIED_RES_ALREADY_FREE;\r
938             }\r
939         }\r
940         else {\r
941             retVal = RM_SERVICE_DENIED_PARTIAL_FREE;\r
942         }\r
943     }\r
944     else {\r
945         retVal = RM_SERVICE_DENIED_RES_RANGE_DOES_NOT_EXIST;\r
946     }\r
947     return(retVal);  \r
948 }\r
949 \r
950 /* FUNCTION PURPOSE: Issues a service response to application\r
951  ***********************************************************************\r
952  * DESCRIPTION: Provides a service response back to the application\r
953  *              using the service callback function provided to\r
954  *              the RM instance at the time of the service request.\r
955  */\r
956 void serviceResponder (Rm_Inst *rmInst, Rm_Transaction *transaction)\r
957 {\r
958     Rm_ServiceRespInfo serviceResponse;\r
959 \r
960     /* The responseTransaction will contain the resultant state details of\r
961      * the requestTransaction's service request */\r
962     serviceResponse.serviceState = transaction->state;\r
963     /* Pass back the ID that was provided to the component when it requested\r
964      * the service */\r
965     serviceResponse.serviceId = transaction->localId;\r
966 \r
967     /* Service was approved and service was an allocate request.  The resource\r
968      * data is passed back to the component */\r
969     if ((serviceResponse.serviceState == RM_SERVICE_APPROVED) &&\r
970         ((transaction->type == Rm_service_RESOURCE_ALLOCATE_INIT) ||\r
971          (transaction->type == Rm_service_RESOURCE_ALLOCATE_USE) ||\r
972          (transaction->type == Rm_service_RESOURCE_GET_BY_NAME)))\r
973     {\r
974         strncpy(serviceResponse.resourceName, transaction->resourceInfo.name, RM_NAME_MAX_CHARS);\r
975         serviceResponse.resourceBase = transaction->resourceInfo.base;\r
976         serviceResponse.resourceLength = transaction->resourceInfo.length;\r
977     }\r
978 \r
979     /* Issue the callback to the requesting component with the response information */\r
980     transaction->callback.serviceCallback(&serviceResponse);\r
981 \r
982     /* Delete the transaction from the transaction queue */\r
983     rmTransactionQueueDelete(rmInst, transaction->localId);\r
984     return;\r
985 }\r
986 \r
987 /* FUNCTION PURPOSE: Sends RM response packets\r
988  ***********************************************************************\r
989  * DESCRIPTION: Sends RM response packets to RM instance's that sent\r
990  *              RM request packets to the RM instance.  The response\r
991  *              is sent via the RM transport API which is plugged\r
992  *              with an application created transport path.\r
993  */\r
994 void transactionResponder (Rm_Inst *rmInst, Rm_Transaction *transaction)\r
995 {\r
996     Rm_Transport    *dstTransport = NULL;\r
997     Rm_PacketHandle  pktHandle = NULL;\r
998 \r
999     dstTransport = rmTransportFindRemoteName((Rm_Transport *) rmInst->transports, transaction->pktSrcInstName);\r
1000 \r
1001     switch (transaction->type) {\r
1002         case Rm_service_RESOURCE_ALLOCATE_INIT:\r
1003         case Rm_service_RESOURCE_ALLOCATE_USE:\r
1004         case Rm_service_RESOURCE_FREE:\r
1005         case Rm_service_RESOURCE_GET_BY_NAME:\r
1006             pktHandle = rmTransportCreateResourceResponsePkt(rmInst, dstTransport->appTransportHandle, \r
1007                                                              transaction);\r
1008             break;\r
1009         case Rm_service_RESOURCE_MAP_TO_NAME:\r
1010         case Rm_service_RESOURCE_UNMAP_NAME:\r
1011             pktHandle = rmTransportCreateNsResponsePkt(rmInst, dstTransport->appTransportHandle,\r
1012                                                        transaction);\r
1013             break;\r
1014     }\r
1015 \r
1016     if (pktHandle) {\r
1017         if (rmInst->transportCallouts.rmSendPkt(dstTransport->appTransportHandle, pktHandle) < RM_OK) {\r
1018             transaction->state = RM_ERROR_TRANSPORT_SEND_ERROR;\r
1019         }\r
1020     }\r
1021     rmTransactionQueueDelete(rmInst, transaction->localId);\r
1022 }\r
1023 \r
1024 /* FUNCTION PURPOSE: Sends RM request packets\r
1025  ***********************************************************************\r
1026  * DESCRIPTION: Sends RM request packets to RM instance's that are\r
1027  *              capable of forwarding or validating service requests.\r
1028  *              The request is sent via the RM transport API which is\r
1029  *              plugged with an application created transport path.\r
1030  */\r
1031 void transactionForwarder (Rm_Inst *rmInst, Rm_Transaction *transaction)\r
1032 {\r
1033     Rm_Transport    *dstTransport = NULL;\r
1034     Rm_PacketHandle  pktHandle = NULL;\r
1035 \r
1036     if (rmInst->registeredWithDelegateOrServer) {\r
1037         if (rmInst->instType == Rm_instType_CLIENT) {\r
1038             dstTransport = rmTransportFindRemoteInstType((Rm_Transport *) rmInst->transports, Rm_instType_CLIENT_DELEGATE);\r
1039 \r
1040             if (!dstTransport) {\r
1041                 dstTransport = rmTransportFindRemoteInstType((Rm_Transport *) rmInst->transports, Rm_instType_SERVER);\r
1042             }\r
1043         } \r
1044         else if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {\r
1045             dstTransport = rmTransportFindRemoteInstType((Rm_Transport *) rmInst->transports, Rm_instType_SERVER);\r
1046         }\r
1047 \r
1048         switch (transaction->type) {\r
1049             case Rm_service_RESOURCE_ALLOCATE_INIT:\r
1050             case Rm_service_RESOURCE_ALLOCATE_USE:\r
1051             case Rm_service_RESOURCE_FREE:\r
1052             case Rm_service_RESOURCE_GET_BY_NAME:\r
1053                 pktHandle = rmTransportCreateResourceReqPkt(rmInst, dstTransport->appTransportHandle, transaction);\r
1054                 break;\r
1055             case Rm_service_RESOURCE_MAP_TO_NAME:\r
1056             case Rm_service_RESOURCE_UNMAP_NAME:\r
1057                 pktHandle = rmTransportCreateNsRequestPkt(rmInst, dstTransport->appTransportHandle, transaction);\r
1058                 break;\r
1059         }\r
1060 \r
1061         if (pktHandle) {\r
1062             if (rmInst->transportCallouts.rmSendPkt(dstTransport->appTransportHandle, pktHandle) < RM_OK) {\r
1063                 transaction->state = RM_ERROR_TRANSPORT_SEND_ERROR;\r
1064             }\r
1065         }\r
1066 \r
1067         transaction->hasBeenForwarded = true;\r
1068         /* Transaction not deleted.  Waiting for response from RM CD or Server */\r
1069     }\r
1070 }\r
1071 \r
1072 /* FUNCTION PURPOSE: Issues an allocator operation\r
1073  ***********************************************************************\r
1074  * DESCRIPTION: Issues an allocator preallocate, allocate, or free\r
1075  *              for an RM resource.\r
1076  */\r
1077 int32_t allocatorOperation(Rm_Inst *rmInst, Rm_AllocatorOpInfo *opInfo)\r
1078 {\r
1079     Rm_Allocator *allocator = NULL;\r
1080     int32_t       resourceOffsetInPolicy;\r
1081     int32_t       retVal;\r
1082     \r
1083     resourceOffsetInPolicy = rmPolicyGetResourceOffset(rmInst->policy, opInfo->resourceInfo->name);\r
1084     allocator = rmAllocatorFind(rmInst->allocators, opInfo->resourceInfo->name);\r
1085     \r
1086     if ((resourceOffsetInPolicy > 0) && allocator) {\r
1087         if (opInfo->operation == Rm_allocatorOp_PRE_ALLOCATE) {\r
1088             retVal = treePreAllocate(rmInst, allocator, resourceOffsetInPolicy, opInfo);\r
1089         }               \r
1090         else if (opInfo->operation == Rm_allocatorOp_ALLOCATE) {\r
1091             retVal = treeAllocate(rmInst, allocator, resourceOffsetInPolicy, opInfo);\r
1092         }\r
1093         else if (opInfo->operation == Rm_allocatorOp_FREE) {\r
1094             retVal = treeFree(allocator, opInfo);\r
1095         }         \r
1096     }\r
1097     else {\r
1098         /* Resource could not be found in policy and/or allocator */\r
1099         retVal = RM_SERVICE_DENIED_RES_DOES_NOT_EXIST;\r
1100     }\r
1101     return(retVal);\r
1102 }\r
1103 \r
1104 /* FUNCTION PURPOSE: Arbitrates allocation service requests\r
1105  ***********************************************************************\r
1106  * DESCRIPTION: Issues a set of allocator operations in order to\r
1107  *              handle a received allocation request.  Allocation\r
1108  *              requests are always forwarded to the Server on Client\r
1109  *              CD instances.  If a request is made with a NameServer\r
1110  *              name the resource base and length parameters are\r
1111  *              retrieved from the NameServer prior to the allocation\r
1112  *              attempt.\r
1113  */\r
1114 void allocationHandler (Rm_Inst *rmInst, Rm_Transaction *transaction, void *validInstNode,\r
1115                         uint32_t allocType)\r
1116 {\r
1117     Rm_AllocatorOpInfo  opInfo;\r
1118     Rm_NameServerObjCfg nameServerObjCfg;\r
1119     int32_t             retVal = transaction->state;\r
1120 \r
1121     memset((void *)&opInfo, 0, sizeof(Rm_AllocatorOpInfo));\r
1122     \r
1123     if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {\r
1124         /* Forward all allocation requests to Server if transport is up.  Otherwise, just queue. */\r
1125         if (rmInst->registeredWithDelegateOrServer) {\r
1126             transactionForwarder(rmInst, transaction);   \r
1127         }  \r
1128     }\r
1129     else if (rmInst->instType == Rm_instType_SERVER) {\r
1130         opInfo.resourceInfo = &transaction->resourceInfo;\r
1131         opInfo.serviceSrcInstNode = validInstNode;\r
1132         opInfo.allocType = allocType;\r
1133 \r
1134         /* Populated NameServer name has precedence over base */\r
1135         if (strlen(transaction->resourceInfo.nameServerName) > 0) {\r
1136             if ((transaction->resourceInfo.base == 0) &&\r
1137                 (transaction->resourceInfo.length == 0) &&\r
1138                 (transaction->resourceInfo.alignment == 0)) {\r
1139                 memset((void *)&nameServerObjCfg, 0, sizeof(Rm_NameServerObjCfg));\r
1140                 nameServerObjCfg.nameServerTree = rmInst->nameServer;\r
1141                 nameServerObjCfg.nodeCfg.objName = transaction->resourceInfo.nameServerName;\r
1142                 if ((retVal = rmNameServerFindObject(&nameServerObjCfg)) == RM_SERVICE_PROCESSING) {\r
1143                     strncpy(transaction->resourceInfo.name, nameServerObjCfg.nodeCfg.resourceName, RM_NAME_MAX_CHARS);\r
1144                     transaction->resourceInfo.base = nameServerObjCfg.nodeCfg.resourceBase;\r
1145                     transaction->resourceInfo.length = nameServerObjCfg.nodeCfg.resourceLength;\r
1146                 }                \r
1147             }\r
1148             else {\r
1149                 retVal = RM_ERROR_NS_NAME_AND_RES_VAL_CONFLICT;\r
1150             }\r
1151         }\r
1152 \r
1153         if (retVal == RM_SERVICE_PROCESSING) {      \r
1154             if (transaction->resourceInfo.base == RM_RESOURCE_BASE_UNSPECIFIED) {\r
1155                 opInfo.operation = Rm_allocatorOp_PRE_ALLOCATE;\r
1156                 retVal = allocatorOperation(rmInst, &opInfo);\r
1157             }\r
1158         \r
1159             if (retVal == RM_SERVICE_PROCESSING) {\r
1160                 opInfo.operation = Rm_allocatorOp_ALLOCATE;\r
1161                 retVal = allocatorOperation(rmInst, &opInfo);\r
1162             }      \r
1163         }\r
1164         \r
1165         transaction->state = retVal;\r
1166 \r
1167         if (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS)) {\r
1168             /* Source of allocation was not the server instance, provide the transaction\r
1169              * to the transaction responder */\r
1170             transactionResponder(rmInst, transaction);\r
1171         }\r
1172         /* Otherwise let the return stack return the transaction to the serviceHandler */                   \r
1173     }   \r
1174 }\r
1175 \r
1176 /* FUNCTION PURPOSE: Arbitrates free service requests\r
1177  ***********************************************************************\r
1178  * DESCRIPTION: Issues a set of allocator operations in order to\r
1179  *              handle a received free request.  Free\r
1180  *              requests are always forwarded to the Server on Client\r
1181  *              CD instances.  If a request is made with a NameServer\r
1182  *              name the resource base and length parameters are\r
1183  *              retrieved from the NameServer prior to the free\r
1184  *              attempt.\r
1185  */\r
1186 void freeHandler (Rm_Inst *rmInst, Rm_Transaction *transaction, void *validInstNode)\r
1187 {\r
1188     Rm_AllocatorOpInfo  opInfo; \r
1189     Rm_NameServerObjCfg nameServerObjCfg;    \r
1190     int32_t             retVal = transaction->state;\r
1191 \r
1192     memset((void *)&opInfo, 0, sizeof(Rm_AllocatorOpInfo));\r
1193     \r
1194     if (rmInst->instType == Rm_instType_CLIENT_DELEGATE) {\r
1195         /* Forward all free requests to Server if transport is up.  Otherwise, just queue. */\r
1196         if (rmInst->registeredWithDelegateOrServer) {\r
1197             transactionForwarder(rmInst, transaction);   \r
1198         }\r
1199     }\r
1200     else if (rmInst->instType == Rm_instType_SERVER) {\r
1201         opInfo.resourceInfo = &transaction->resourceInfo;\r
1202         opInfo.serviceSrcInstNode = validInstNode;\r
1203 \r
1204         /* Populated NameServer name has precedence over base */\r
1205         if (strlen(transaction->resourceInfo.nameServerName) > 0) {\r
1206             if ((transaction->resourceInfo.base == 0) &&\r
1207                 (transaction->resourceInfo.length == 0) &&\r
1208                 (transaction->resourceInfo.alignment == 0)) {\r
1209                 memset((void *)&nameServerObjCfg, 0, sizeof(Rm_NameServerObjCfg));\r
1210                 nameServerObjCfg.nameServerTree = rmInst->nameServer;\r
1211                 nameServerObjCfg.nodeCfg.objName = transaction->resourceInfo.nameServerName;\r
1212                 if ((retVal = rmNameServerFindObject(&nameServerObjCfg)) == RM_SERVICE_PROCESSING) {\r
1213                     strncpy(transaction->resourceInfo.name, nameServerObjCfg.nodeCfg.resourceName, RM_NAME_MAX_CHARS);\r
1214                     transaction->resourceInfo.base = nameServerObjCfg.nodeCfg.resourceBase;\r
1215                     transaction->resourceInfo.length = nameServerObjCfg.nodeCfg.resourceLength;\r
1216                 } \r
1217             }\r
1218             else {\r
1219                 retVal = RM_ERROR_NS_NAME_AND_RES_VAL_CONFLICT;\r
1220             }                \r
1221         }\r
1222         \r
1223         if(retVal == RM_SERVICE_PROCESSING) {        \r
1224             opInfo.operation = Rm_allocatorOp_FREE;\r
1225             retVal = allocatorOperation(rmInst, &opInfo);\r
1226         }       \r
1227 \r
1228         transaction->state = retVal;\r
1229 \r
1230         if (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS)) {\r
1231             /* Source of allocation was not the server instance, provide the transaction\r
1232              * to the transaction responder */\r
1233             transactionResponder(rmInst, transaction);\r
1234         }\r
1235         /* Otherwise let the return stack return the transaction to the serviceHandler */        \r
1236     }   \r
1237 }\r
1238 \r
1239 /* FUNCTION PURPOSE: Reserves a Linux resource\r
1240  ***********************************************************************\r
1241  * DESCRIPTION: Reserves resources for Linux using the base and length\r
1242  *              values retrieved from the Linux DTB via the\r
1243  *              "linux-dtb-alias" properties within the GRL.\r
1244  */\r
1245 int32_t reserveLinuxResource(Rm_Inst *rmInst, Rm_LinuxAlias *linuxAlias, \r
1246                              Rm_LinuxValueRange *linuxValues, Rm_AllocatorOpInfo *opInfo)\r
1247 {\r
1248     int32_t  retVal = RM_OK;\r
1249     bool     baseFound = false;\r
1250     bool     lengthFound = false;\r
1251     uint32_t valueIndex = 0;\r
1252 \r
1253     while ((linuxValues) && (!baseFound || !lengthFound)) {\r
1254         if (linuxAlias->baseOffset == valueIndex) {\r
1255             opInfo->resourceInfo->base = linuxValues->value;\r
1256             baseFound = true;\r
1257 \r
1258             if (linuxAlias->lengthOffset == RM_DTB_LINUX_ALIAS_OFFSET_NOT_SET) {\r
1259                 opInfo->resourceInfo->length = 1;\r
1260                 lengthFound = true;\r
1261             }\r
1262         }\r
1263         else if (linuxAlias->lengthOffset == valueIndex) {\r
1264             opInfo->resourceInfo->length = linuxValues->value;\r
1265             lengthFound = true;\r
1266         }\r
1267 \r
1268         linuxValues = (Rm_LinuxValueRange *)linuxValues->nextValue;\r
1269         valueIndex++;\r
1270     }\r
1271 \r
1272     if (!baseFound || !lengthFound) {\r
1273         retVal = RM_ERROR_DATA_NOT_FOUND_AT_LINUX_ALIAS;\r
1274     }\r
1275     else {\r
1276         /* Allocate resource to Linux */\r
1277         retVal = allocatorOperation(rmInst, opInfo);\r
1278     }\r
1279     return (retVal);\r
1280 }\r
1281 \r
1282 /* FUNCTION PURPOSE: Finds and reserves Linux resources\r
1283  ***********************************************************************\r
1284  * DESCRIPTION: Parses the Linux DTB for resources consumed by the\r
1285  *              Linux kernel.  If the resource is found via the\r
1286  *              "linux-dtb-alias" property defined in the GRL it is \r
1287  *              reserved.\r
1288  */\r
1289 int32_t findAndReserveLinuxResource(Rm_Inst *rmInst, const char *resourceName, void *linuxDtb, \r
1290                                     Rm_LinuxAlias *linuxAlias)\r
1291 {\r
1292     Rm_AllocatorOpInfo  opInfo;\r
1293     Rm_ResourceInfo     resourceInfo;\r
1294     uint32_t            pathOffset;\r
1295     uint32_t            pathSize;\r
1296     char               *spacePtr;\r
1297     int32_t             propOffset;\r
1298     int32_t             nodeOffset = RM_DTB_UTIL_STARTING_NODE_OFFSET;\r
1299     int32_t             prevDepth = RM_DTB_UTIL_STARTING_DEPTH;\r
1300     int32_t             depth;\r
1301     int32_t             propertyLen;\r
1302     const char         *propertyName;\r
1303     const void         *propertyData; \r
1304     Rm_LinuxValueRange *linuxValueRange;\r
1305     int32_t             retVal = RM_OK; \r
1306 \r
1307     memset((void *) &opInfo, 0, sizeof(Rm_AllocatorOpInfo));\r
1308     memset((void *) &resourceInfo, 0, sizeof(Rm_ResourceInfo));\r
1309 \r
1310     strncpy(resourceInfo.name, resourceName, RM_NAME_MAX_CHARS);\r
1311     opInfo.serviceSrcInstNode = rmPolicyGetLinuxInstNode(rmInst->validInstances);\r
1312     opInfo.operation = Rm_allocatorOp_ALLOCATE;\r
1313     opInfo.resourceInfo = &resourceInfo;    \r
1314 \r
1315     while(linuxAlias) {\r
1316         /* Reset parsing variables */\r
1317         pathOffset = 0;\r
1318         pathSize = strlen(linuxAlias->path) + 1;\r
1319         nodeOffset = RM_DTB_UTIL_STARTING_NODE_OFFSET;\r
1320         prevDepth = RM_DTB_UTIL_STARTING_DEPTH;   \r
1321         resourceInfo.base = 0;\r
1322         resourceInfo.length = 0;\r
1323 \r
1324         spacePtr = strpbrk(linuxAlias->path, " ");\r
1325         if (spacePtr) {\r
1326             *spacePtr = '\0';\r
1327         }       \r
1328         \r
1329         while(pathOffset < pathSize) {\r
1330             /* Move through DTB nodes until next alias path node found */\r
1331             if (strcmp(linuxAlias->path + pathOffset, fdt_get_name(linuxDtb, nodeOffset, NULL))) {\r
1332                 nodeOffset = fdt_next_node(linuxDtb, nodeOffset, &depth);\r
1333 \r
1334                 if ((depth < prevDepth) || (nodeOffset == -FDT_ERR_NOTFOUND)) {\r
1335                     /* Returning from subnode that matched part of alias path without finding\r
1336                      * resource values */\r
1337                     retVal = RM_ERROR_DATA_NOT_FOUND_AT_LINUX_ALIAS;\r
1338                     break;\r
1339                 }\r
1340             }\r
1341             else {\r
1342                 /* Found next alias path node.  Move to next node name in path string. */\r
1343                 pathOffset += (strlen(linuxAlias->path + pathOffset) + 1);\r
1344                 spacePtr = strpbrk(linuxAlias->path + pathOffset, " ");\r
1345                 if (spacePtr) {\r
1346                     *spacePtr = '\0';\r
1347                 }       \r
1348                 \r
1349                 prevDepth = fdt_node_depth(linuxDtb, nodeOffset);\r
1350                 propOffset = fdt_first_property_offset(linuxDtb, nodeOffset);\r
1351                 while ((propOffset >= RM_DTB_UTIL_STARTING_NODE_OFFSET) &&\r
1352                        (pathOffset < pathSize)) {\r
1353                     propertyData = fdt_getprop_by_offset(linuxDtb, propOffset, \r
1354                                                          &propertyName, &propertyLen);\r
1355 \r
1356                     if (strcmp(linuxAlias->path + pathOffset, propertyName) == 0) {\r
1357                         /* Found resource at end of alias path */\r
1358                         pathOffset += (strlen(linuxAlias->path + pathOffset) + 1);\r
1359                         linuxValueRange = rmDtbUtilLinuxExtractValues(propertyData, propertyLen);\r
1360                         retVal = reserveLinuxResource(rmInst, linuxAlias, \r
1361                                                          linuxValueRange, &opInfo);\r
1362                         rmDtbUtilLinuxFreeValues(linuxValueRange);\r
1363                     }\r
1364                     propOffset = fdt_next_property_offset(linuxDtb, propOffset);\r
1365                 } \r
1366 \r
1367                 if (propOffset < -FDT_ERR_NOTFOUND) {\r
1368                     retVal = propOffset;\r
1369                     break;\r
1370                 }\r
1371             }\r
1372         }\r
1373 \r
1374         if (retVal < RM_OK) {\r
1375             break;\r
1376         }\r
1377         linuxAlias = linuxAlias->nextLinuxAlias;\r
1378     }\r
1379     return (retVal);\r
1380 }\r
1381 \r
1382 /* FUNCTION PURPOSE: Creates and initializes a resource allocator\r
1383  ***********************************************************************\r
1384  * DESCRIPTION: Creates a resource allocator for the provided\r
1385  *              resource name and resource properties retrieved\r
1386  *              from the GRL.  Resources will be reserved for \r
1387  *              the Linux kernel if the Linux DTB is provided\r
1388  *              and there are "linux-dtb-alias" properties\r
1389  *              specified in the GRL.\r
1390  */\r
1391 int32_t createAndInitAllocator(Rm_Inst *rmInst, const char *resourceName, \r
1392                                Rm_ResourceProperties *resourceProperties, void *linuxDtb)\r
1393 {\r
1394     Rm_ResourceRange    *range = NULL;\r
1395     Rm_ResourceRange    *rangeBasePtr = NULL;\r
1396     Rm_NsAssignment     *nsAssignments = NULL;\r
1397     Rm_NsAssignment     *nsAssignmentBasePtr = NULL;\r
1398     Rm_LinuxAlias       *linuxAlias = NULL;\r
1399     Rm_NameServerObjCfg  nameServerObjCfg;      \r
1400     int32_t              retVal = RM_OK;\r
1401 \r
1402     if (resourceProperties->rangeData && (resourceProperties->rangeLen > 0)) {\r
1403         range = rangeBasePtr = rmDtbUtilResExtractRange(resourceProperties->rangeData, \r
1404                                                         resourceProperties->rangeLen);\r
1405         \r
1406         if ((retVal = createResourceTree(rmInst, resourceName, range)) >= RM_OK) {\r
1407             if (resourceProperties->linuxAliasData && resourceProperties->linuxAliasLen) {\r
1408                 if (linuxDtb) {\r
1409                     linuxAlias = rmDtbUtilResExtractLinuxAlias(resourceProperties->linuxAliasData,\r
1410                                                                resourceProperties->linuxAliasLen, &retVal);\r
1411                     if (linuxAlias) {\r
1412                         retVal = findAndReserveLinuxResource(rmInst, resourceName, linuxDtb, linuxAlias);            \r
1413                     }\r
1414                 }\r
1415                 else {\r
1416                     retVal = RM_ERROR_GRL_LINUX_ALIAS_BUT_NO_DTB;\r
1417                 }\r
1418             }\r
1419         }\r
1420     }\r
1421     \r
1422     if (retVal >= RM_OK) {\r
1423         if (resourceProperties->nsAssignData && resourceProperties->nsAssignLen) {\r
1424             nsAssignments = rmDtbUtilResExtractNsAssignment(resourceProperties->nsAssignData, \r
1425                                                             resourceProperties->nsAssignLen, &retVal);\r
1426             if (nsAssignments) {\r
1427                 nsAssignmentBasePtr = nsAssignments;\r
1428                 while (nsAssignments) {\r
1429                     memset((void *)&nameServerObjCfg, 0, sizeof(Rm_NameServerObjCfg));\r
1430                     nameServerObjCfg.nameServerTree = rmInst->nameServer;\r
1431                     nameServerObjCfg.nodeCfg.objName = nsAssignments->nsName;\r
1432                     nameServerObjCfg.nodeCfg.resourceName = (char *)resourceName;\r
1433                     nameServerObjCfg.nodeCfg.resourceBase= nsAssignments->resourceBase;\r
1434                     nameServerObjCfg.nodeCfg.resourceLength = nsAssignments->resourceLength;                \r
1435                     rmNameServerAddObject(&nameServerObjCfg);\r
1436                     nsAssignments = nsAssignments->nextNsAssignment;\r
1437                 }\r
1438                 rmDtbUtilResFreeNsAssignmentList(nsAssignmentBasePtr);\r
1439             }\r
1440         }\r
1441     }\r
1442 \r
1443     rmDtbUtilResFreeRange(rangeBasePtr);\r
1444     if (linuxAlias) {\r
1445         rmDtbUtilResFreeLinuxAlias(linuxAlias);\r
1446     }\r
1447     return(retVal);\r
1448 }\r
1449 \r
1450 /* FUNCTION PURPOSE: Recursively parses GRL resource properties\r
1451  ***********************************************************************\r
1452  * DESCRIPTION: Recursively parses and stores GRL resource node \r
1453  *              properties using the LIBFDT APIs\r
1454  */\r
1455 int32_t parseResourceProperty(void *globalResourceDtb, int32_t offset, Rm_ResourceProperties *propertyInfo)\r
1456 {\r
1457         int32_t              propertyLen;\r
1458         const char          *propertyName;\r
1459         const void          *propertyData;\r
1460     Rm_ResourcePropType  propertyType;\r
1461     int32_t              retVal = RM_OK;\r
1462 \r
1463         propertyData = fdt_getprop_by_offset(globalResourceDtb, offset, &propertyName, &propertyLen);\r
1464     propertyType = rmDtbUtilResGetPropertyType(propertyName);\r
1465     if (propertyType == Rm_resourcePropType_RESOURCE_RANGE) {\r
1466         propertyInfo->rangeData = propertyData;\r
1467         propertyInfo->rangeLen = propertyLen;\r
1468     }\r
1469     else if (propertyType == Rm_resourcePropType_NSASSIGNMENT) {\r
1470         propertyInfo->nsAssignData = propertyData;\r
1471         propertyInfo->nsAssignLen = propertyLen;\r
1472     }\r
1473     else if (propertyType == Rm_resourcePropType_RESOURCE_LINUX_ALIAS) {\r
1474         propertyInfo->linuxAliasData = propertyData;\r
1475         propertyInfo->linuxAliasLen = propertyLen;\r
1476     }        \r
1477     else {\r
1478         retVal = RM_ERROR_GRL_UNKNOWN_RESOURCE_PROPERTY;\r
1479     }\r
1480 \r
1481     if (retVal == RM_OK) {\r
1482         offset = fdt_next_property_offset(globalResourceDtb, offset);\r
1483         if (offset >= 0) {\r
1484             retVal = parseResourceProperty(globalResourceDtb, offset, propertyInfo);\r
1485         }\r
1486         else if (offset != -FDT_ERR_NOTFOUND) {\r
1487             /* Error returned by LIBFDT */\r
1488             retVal = offset;\r
1489         }\r
1490     }\r
1491     return (retVal);\r
1492 }\r
1493 \r
1494 /* FUNCTION PURPOSE: Recursively parses GRL resource nodes\r
1495  ***********************************************************************\r
1496  * DESCRIPTION: Recursively parses GRL resource nodes looking for\r
1497  *              resource properties to create the resource allocators.\r
1498  *              The LIBFDT APIs are used to parse the GRL.\r
1499  */\r
1500 int32_t parseResourceNode(Rm_Inst *rmInst, void *globalResourceDtb, int32_t nodeOffset, int32_t depth,\r
1501                           void *linuxDtb)\r
1502 {\r
1503         const char            *resourceName = fdt_get_name(globalResourceDtb, nodeOffset, NULL);\r
1504     Rm_ResourceProperties  resourceProperties;\r
1505         int32_t                retVal = RM_OK;\r
1506         int32_t                offset;\r
1507 \r
1508     memset((void *)&resourceProperties, 0, sizeof(Rm_ResourceProperties));\r
1509     /* Get properties of resource node */\r
1510         offset = fdt_first_property_offset(globalResourceDtb, nodeOffset);\r
1511         if (offset >= RM_DTB_UTIL_STARTING_NODE_OFFSET) {\r
1512                 retVal =  parseResourceProperty(globalResourceDtb, offset, &resourceProperties);\r
1513         if (retVal < -FDT_ERR_NOTFOUND) {\r
1514             return (retVal);\r
1515         }\r
1516         if (retVal = createAndInitAllocator(rmInst, resourceName, &resourceProperties, linuxDtb) < RM_OK) {\r
1517             return (retVal);\r
1518         }\r
1519         }\r
1520     else if (offset != -FDT_ERR_NOTFOUND) {\r
1521                 /* Error returned by LIBFDT */\r
1522         return (offset);\r
1523     }\r
1524     \r
1525         offset = fdt_next_node(globalResourceDtb, nodeOffset, &depth);\r
1526     if ((offset >= RM_DTB_UTIL_STARTING_NODE_OFFSET) && (depth >= RM_DTB_UTIL_STARTING_DEPTH)) {\r
1527         retVal = parseResourceNode(rmInst, globalResourceDtb, offset, depth, linuxDtb);\r
1528         if (retVal < -FDT_ERR_NOTFOUND) {\r
1529             return (retVal);\r
1530         }\r
1531     }\r
1532     else if (offset != -FDT_ERR_NOTFOUND) {\r
1533         /* Error returned by LIBFDT */\r
1534         return (offset);\r
1535     }\r
1536     return (retVal);\r
1537 }\r
1538 \r
1539 /* FUNCTION PURPOSE: Initializes server allocators\r
1540  ***********************************************************************\r
1541  * DESCRIPTION: Creates and initializes a server instance's\r
1542  *              resource allocators using the GRL and, if\r
1543  *              provided, Linux DTB.\r
1544  */\r
1545 int32_t initializeAllocators(Rm_Inst *rmInst, void *globalResourceDtb, void *linuxDtb)\r
1546 {\r
1547     int32_t nodeOffset = RM_DTB_UTIL_STARTING_NODE_OFFSET;\r
1548     int32_t startDepth = RM_DTB_UTIL_STARTING_DEPTH;\r
1549     int32_t result = RM_OK;\r
1550 \r
1551     /* Recursively parse the Global Resource List, creating an allocator for\r
1552      * each resource as specified in the node */\r
1553     result = parseResourceNode(rmInst, globalResourceDtb, nodeOffset, startDepth, linuxDtb);\r
1554 \r
1555     return(result);\r
1556 }\r
1557 \r
1558 /**********************************************************************\r
1559  ********************** Internal Functions ****************************\r
1560  **********************************************************************/\r
1561 \r
1562 /* FUNCTION PURPOSE: Adds a transaction\r
1563  ***********************************************************************\r
1564  * DESCRIPTION: Returns a pointer to a newly created transaction.\r
1565  *              The transaction is created based on a new service\r
1566  *              request received via the service API or the\r
1567  *              transport API (service forwarded from another instance)\r
1568  */\r
1569 Rm_Transaction *rmTransactionQueueAdd(Rm_Inst *rmInst)\r
1570 {\r
1571     Rm_Transaction *transactionQueue = rmInst->transactionQueue;\r
1572     Rm_Transaction *newTransaction   = NULL;\r
1573 \r
1574     newTransaction = Rm_osalMalloc(sizeof(Rm_Transaction));\r
1575     if (newTransaction) {\r
1576         memset((void *)newTransaction, 0, sizeof(Rm_Transaction));\r
1577 \r
1578         newTransaction->localId = transactionGetSequenceNum(rmInst);\r
1579         newTransaction->nextTransaction = NULL;  \r
1580         if (transactionQueue) {\r
1581             while (transactionQueue->nextTransaction) {\r
1582                 transactionQueue = transactionQueue->nextTransaction;\r
1583             }\r
1584             transactionQueue->nextTransaction = newTransaction;\r
1585         }\r
1586         else {\r
1587             rmInst->transactionQueue = newTransaction;\r
1588         }\r
1589     }\r
1590     return (newTransaction);\r
1591 }\r
1592 \r
1593 /* FUNCTION PURPOSE: Finds a transaction\r
1594  ***********************************************************************\r
1595  * DESCRIPTION: Returns a pointer to a transaction resident\r
1596  *              in the transaction queue that matches the provided\r
1597  *              transaction ID.\r
1598  */\r
1599 Rm_Transaction *rmTransactionQueueFind(Rm_Inst *rmInst, uint32_t transactionId)\r
1600 {\r
1601     Rm_Transaction *transaction = rmInst->transactionQueue;\r
1602 \r
1603     while (transaction) {\r
1604         if (transaction->localId == transactionId) {\r
1605             break;             \r
1606         }\r
1607         transaction = transaction->nextTransaction;\r
1608     }\r
1609 \r
1610     return (transaction);\r
1611 }\r
1612 \r
1613 /* FUNCTION PURPOSE: Deletes a transaction\r
1614  ***********************************************************************\r
1615  * DESCRIPTION: Deletes the transaction with the provided transaction\r
1616  *              ID from the instance's transaction queue.\r
1617  */\r
1618 int32_t rmTransactionQueueDelete(Rm_Inst *rmInst, uint32_t transactionId)\r
1619 {\r
1620     Rm_Transaction *transaction     = rmInst->transactionQueue;\r
1621     Rm_Transaction *prevTransaction = NULL;\r
1622     int32_t         retVal          = RM_OK;\r
1623 \r
1624     while (transaction) {\r
1625         if (transaction->localId == transactionId) {\r
1626             break;             \r
1627         }\r
1628 \r
1629         prevTransaction = transaction;\r
1630         transaction = transaction->nextTransaction;\r
1631     }\r
1632 \r
1633     if (transaction) {\r
1634         if (prevTransaction == NULL) {\r
1635             /* Transaction at start of queue. Map second transaction to start of queue \r
1636              * as long as more than one transactions. */\r
1637             rmInst->transactionQueue = transaction->nextTransaction;\r
1638         }\r
1639         else {\r
1640             /* Transaction in middle or end of queue. */\r
1641             prevTransaction->nextTransaction = transaction->nextTransaction;\r
1642         }\r
1643         Rm_osalFree((void *)transaction, sizeof(Rm_Transaction));\r
1644     }\r
1645     else {\r
1646         retVal = RM_ERROR_SERVICE_TRANS_DOES_NOT_EXIST;\r
1647     }    \r
1648     return (retVal);\r
1649 }\r
1650 \r
1651 /* FUNCTION PURPOSE: Finds an allocator\r
1652  ***********************************************************************\r
1653  * DESCRIPTION: Returns a pointer to an allocator that matches the \r
1654  *              provided resource name.\r
1655  */\r
1656 Rm_Allocator *rmAllocatorFind(Rm_Allocator *allocatorList, char *resourceName)\r
1657 {\r
1658     while (allocatorList) {\r
1659         if (strncmp(allocatorList->resourceName, resourceName, RM_NAME_MAX_CHARS) == 0) {\r
1660             break;             \r
1661         }\r
1662         allocatorList = allocatorList->nextAllocator;\r
1663     }\r
1664 \r
1665     return (allocatorList);\r
1666 }\r
1667 \r
1668 /* FUNCTION PURPOSE: Processes a transaction\r
1669  ***********************************************************************\r
1670  * DESCRIPTION: Processes transactions created from services\r
1671  *              received via the service handle or the transport.\r
1672  *              Transactions will be routed within the RM system\r
1673  *              based on the RM instance type and the type of\r
1674  *              the transaction.\r
1675  */\r
1676 void rmTransactionProcessor (Rm_Inst *rmInst, Rm_Transaction *transaction)\r
1677 {\r
1678     void                *validInstNode;\r
1679     Rm_PolicyCheckCfg    privCheckCfg;\r
1680     Rm_NameServerObjCfg  nameServerObjCfg;      \r
1681     uint32_t             allocType = 0;    \r
1682 \r
1683     /* Handle static transactions originating on this instance.  Any other static transactions will be\r
1684      * stored in transaction queue until all transports are up. */\r
1685     if (((rmInst->instType == Rm_instType_CLIENT) || (rmInst->instType == Rm_instType_CLIENT_DELEGATE)) &&\r
1686         (!rmInst->registeredWithDelegateOrServer) && \r
1687         (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS) == 0)) {\r
1688         if (rmInst->staticInfo.staticPolicy) {\r
1689             if ((transaction->type == Rm_service_RESOURCE_ALLOCATE_INIT) ||\r
1690                 (transaction->type == Rm_service_RESOURCE_ALLOCATE_USE)) {\r
1691                 /* Check request against startup policy */\r
1692                 memset((void *)&privCheckCfg, 0, sizeof(Rm_PolicyCheckCfg));\r
1693 \r
1694                 if (transaction->type == Rm_service_RESOURCE_ALLOCATE_INIT) {\r
1695                     privCheckCfg.type = Rm_policyCheck_INIT;\r
1696                 }\r
1697                 else {\r
1698                     privCheckCfg.type = Rm_policyCheck_USE;\r
1699                 }\r
1700                 privCheckCfg.policyDtb = rmInst->staticInfo.staticPolicy;\r
1701                 privCheckCfg.validInstNode = rmPolicyGetValidInstNode(rmInst->staticInfo.staticValidInstTree, \r
1702                                                                       rmInst->instName);\r
1703                 privCheckCfg.resourceOffset = rmPolicyGetResourceOffset(rmInst->staticInfo.staticPolicy,\r
1704                                                                         transaction->resourceInfo.name);\r
1705                 privCheckCfg.resourceBase = transaction->resourceInfo.base;\r
1706                 privCheckCfg.resourceLength = transaction->resourceInfo.length;\r
1707 \r
1708                 if (rmPolicyCheckPrivilege(&privCheckCfg, &transaction->state)) {\r
1709                     transaction->state = RM_SERVICE_APPROVED_STATIC;\r
1710                 }\r
1711                 else if (transaction->state == RM_SERVICE_PROCESSING) {\r
1712                     /* Privilege check returned false without error */\r
1713                     transaction->state = RM_SERVICE_DENIED_BY_STATIC_POLICY;\r
1714                 }\r
1715             }\r
1716             else {\r
1717                 transaction->state = RM_SERVICE_DENIED_INVALID_STATIC_REQUEST;\r
1718             }\r
1719         }\r
1720         else {\r
1721             transaction->state = RM_ERROR_REQ_FAILED_NO_STATIC_POLICY;\r
1722         }        \r
1723     }\r
1724     else {\r
1725         /* Handle auto-forwarded transactions.  These transactions include:\r
1726          * - All request transactions received on Clients are forwarded to the Client Delegate\r
1727          * - NameServer requests received on the Client Delegate are forwarded to the Server */\r
1728         if ((rmInst->instType == Rm_instType_CLIENT) ||\r
1729             ((rmInst->instType == Rm_instType_CLIENT_DELEGATE) &&\r
1730              ((transaction->type == Rm_service_RESOURCE_MAP_TO_NAME) ||\r
1731               (transaction->type == Rm_service_RESOURCE_GET_BY_NAME) ||\r
1732               (transaction->type == Rm_service_RESOURCE_UNMAP_NAME)))) {\r
1733               \r
1734             if ((transaction->state != RM_SERVICE_PROCESSING) &&\r
1735                 (transaction->state != RM_SERVICE_APPROVED_STATIC)) {\r
1736                 if (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS)) {\r
1737                     /* Transaction did not originate on this instance */\r
1738                     transactionResponder(rmInst, transaction);\r
1739                 }\r
1740                 else {\r
1741                     /* Transaction originated on this instance */\r
1742                     serviceResponder(rmInst, transaction);\r
1743                 }\r
1744             }\r
1745             else {\r
1746                 /* Forward request if transport is up.  Otherwise, just queue. */\r
1747                 if (rmInst->registeredWithDelegateOrServer) {\r
1748                     transactionForwarder(rmInst, transaction);   \r
1749                 } \r
1750             }\r
1751         }\r
1752         else {\r
1753             /* Validate service's originating instance name */\r
1754             if (rmInst->instType == Rm_instType_SERVER) {\r
1755                 validInstNode = rmPolicyGetValidInstNode(rmInst->validInstances, transaction->serviceSrcInstName);\r
1756                 if (validInstNode == NULL) {\r
1757                     transaction->state = RM_SERVICE_DENIED_INST_NAME_NOT_VALID;\r
1758 \r
1759                     /* Send result via responder if transaction did not originate from this instance */\r
1760                     if (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS)) {\r
1761                         transactionResponder(rmInst, transaction);\r
1762                     }\r
1763                 }\r
1764             }\r
1765 \r
1766             switch (transaction->type) {\r
1767                 case Rm_service_RESOURCE_ALLOCATE_INIT:\r
1768                 case Rm_service_RESOURCE_ALLOCATE_USE:\r
1769                 case Rm_service_RESOURCE_FREE:               \r
1770                     if ((transaction->state != RM_SERVICE_PROCESSING) &&\r
1771                         (transaction->state != RM_SERVICE_APPROVED_STATIC)) {\r
1772                         /* Transaction complete */\r
1773                         if (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS)) {\r
1774                             /* Transaction result not destined for this instance */\r
1775                             transactionResponder(rmInst, transaction);\r
1776                         }\r
1777                         else {\r
1778                             /* Transaction result destined for this instance */\r
1779                             serviceResponder(rmInst, transaction);      \r
1780                         }\r
1781                     }\r
1782                     else {\r
1783                         /* Complete allocation/free request */\r
1784                         if (transaction->type == Rm_service_RESOURCE_FREE) {\r
1785                             freeHandler(rmInst, transaction, validInstNode);\r
1786                         }\r
1787                         else {\r
1788                             switch (transaction->type) {\r
1789                                 case Rm_service_RESOURCE_ALLOCATE_INIT:\r
1790                                     RM_policy_SET_PERM(allocType, RM_POLICY_PERM_INIT_SHIFT, 1);\r
1791                                     break;\r
1792                                 case Rm_service_RESOURCE_ALLOCATE_USE:\r
1793                                     RM_policy_SET_PERM(allocType, RM_POLICY_PERM_USE_SHIFT, 1);    \r
1794                                     break;\r
1795                             }\r
1796                             allocationHandler(rmInst, transaction, validInstNode, allocType);\r
1797                         }\r
1798                     }\r
1799                     break;\r
1800                 case Rm_service_RESOURCE_MAP_TO_NAME:\r
1801                 case Rm_service_RESOURCE_GET_BY_NAME:\r
1802                 case Rm_service_RESOURCE_UNMAP_NAME:                \r
1803                     /* NameServer resides on server */\r
1804                     memset((void *)&nameServerObjCfg, 0, sizeof(Rm_NameServerObjCfg));\r
1805                     if (rmInst->nameServer) {\r
1806                         nameServerObjCfg.nameServerTree = rmInst->nameServer;\r
1807                         nameServerObjCfg.nodeCfg.objName = transaction->resourceInfo.nameServerName;\r
1808                         if (transaction->type == Rm_service_RESOURCE_MAP_TO_NAME) {\r
1809                             nameServerObjCfg.nodeCfg.resourceName = transaction->resourceInfo.name;\r
1810                             nameServerObjCfg.nodeCfg.resourceBase= transaction->resourceInfo.base;\r
1811                             nameServerObjCfg.nodeCfg.resourceLength = transaction->resourceInfo.length;\r
1812                             transaction->state = rmNameServerAddObject(&nameServerObjCfg);\r
1813                         }\r
1814                         else if (transaction->type == Rm_service_RESOURCE_GET_BY_NAME) {\r
1815                             if ((transaction->state = rmNameServerFindObject(&nameServerObjCfg)) ==\r
1816                                 RM_SERVICE_PROCESSING) {\r
1817                                 strncpy(transaction->resourceInfo.name, nameServerObjCfg.nodeCfg.resourceName, RM_NAME_MAX_CHARS);\r
1818                                 transaction->resourceInfo.base = nameServerObjCfg.nodeCfg.resourceBase;\r
1819                                 transaction->resourceInfo.length = nameServerObjCfg.nodeCfg.resourceLength;\r
1820                                 transaction->state = RM_SERVICE_APPROVED;\r
1821                             } \r
1822                         }\r
1823                         else if (transaction->type == Rm_service_RESOURCE_UNMAP_NAME) {\r
1824                             transaction->state = rmNameServerDeleteObject(&nameServerObjCfg);\r
1825                         }\r
1826                     }\r
1827                     else {\r
1828                         transaction->state = RM_ERROR_NAMESERVER_DOES_NOT_EXIST;\r
1829                     }\r
1830 \r
1831                     /* Send result via responder if transaction did not originate from this instance */\r
1832                     if (strncmp(transaction->serviceSrcInstName, rmInst->instName, RM_NAME_MAX_CHARS)) {\r
1833                         transactionResponder(rmInst, transaction);\r
1834                     }\r
1835                     break;\r
1836             }\r
1837         }\r
1838     }\r
1839 \r
1840     /* Forward any queued requests that weren't forwarded yet */\r
1841     if (rmInst->registeredWithDelegateOrServer) {\r
1842         transaction = rmInst->transactionQueue;\r
1843         while(transaction) {\r
1844             if (((transaction->state == RM_SERVICE_PROCESSING) ||\r
1845                  (transaction->state == RM_SERVICE_APPROVED_STATIC)) &&\r
1846                 !transaction->hasBeenForwarded) {\r
1847                 transactionForwarder(rmInst, transaction);\r
1848             }\r
1849             transaction = transaction->nextTransaction;\r
1850         }\r
1851     }\r
1852 }\r
1853      \r
1854 /**********************************************************************\r
1855  ********************** Application visible APIs **********************\r
1856  **********************************************************************/\r
1857 \r
1858 /* FUNCTION PURPOSE: Display status of managed resources\r
1859  ***********************************************************************\r
1860  * DESCRIPTION: Prints the status (allocate/free status, as well as\r
1861  *              owners) for all resources managed by the RM \r
1862  *              instance network.  Also, prints the NameServer name\r
1863  *              entries.  This function is only available on server\r
1864  *              instances.\r
1865  */\r
1866 void Rm_printResourceStatus(Rm_Handle rmServerHandle)\r
1867 {\r
1868     Rm_Inst         *rmInst = (Rm_Inst *)rmServerHandle;\r
1869     Rm_Allocator    *allocator = rmInst->allocators;\r
1870     Rm_Owner        *owners;\r
1871     Rm_ResourceTree *treeRoot;\r
1872     Rm_ResourceNode *treeNode;\r
1873 \r
1874     if (rmInst->instType == Rm_instType_SERVER) {\r
1875         while (allocator != NULL) {\r
1876             Rm_osalLog("Resource: %s\n", allocator->resourceName);\r
1877 \r
1878             treeRoot = allocator->allocatorRootEntry;\r
1879 \r
1880             RB_FOREACH(treeNode, _Rm_AllocatorResourceTree, treeRoot) {               \r
1881                 Rm_osalLog("          %10d - %10d ", treeNode->base, \r
1882                                                      treeNode->base + treeNode->length -1);\r
1883                 \r
1884                 if (treeNode->allocationCount == 0) {\r
1885                     Rm_osalLog("NOT ALLOCATED\n");\r
1886                 }\r
1887                 else {\r
1888                     owners = treeNode->ownerList;\r
1889                     Rm_osalLog("allocated to");\r
1890                     while (owners) {\r
1891                         Rm_osalLog(" %s", owners->instNameNode->name);\r
1892                         owners = owners->nextOwner;\r
1893                     }\r
1894                     Rm_osalLog("\n");\r
1895                 }\r
1896             }        \r
1897             allocator = allocator->nextAllocator;\r
1898         }\r
1899         rmNameServerPrintObjects(rmInst->nameServer);\r
1900     }\r
1901 }\r
1902 \r
1903 /* FUNCTION PURPOSE: RM instance creation and initialization\r
1904  ***********************************************************************\r
1905  * DESCRIPTION: Returns a new RM instance created and initialized\r
1906  *              using the parameters provided via the initCfg\r
1907  *              structure.\r
1908  */\r
1909 Rm_Handle Rm_init(Rm_InitCfg *initCfg, int32_t *result)\r
1910 {\r
1911     Rm_Inst *rmInst;\r
1912     void    *globalResourceDtb = NULL;\r
1913     void    *linuxResourceDtb = NULL;\r
1914     bool     addLinux = false;\r
1915 \r
1916     *result = RM_OK;\r
1917     \r
1918     if ((initCfg->instName == NULL) ||\r
1919         ((strlen(initCfg->instName) + 1) > RM_NAME_MAX_CHARS)) {\r
1920         *result = RM_ERROR_INVALID_INST_NAME;\r
1921         return (NULL);\r
1922     }\r
1923 \r
1924     if (initCfg->instType >= Rm_instType_LAST) {\r
1925         *result = RM_ERROR_INVALID_INST_TYPE;\r
1926     }\r
1927 \r
1928     /* Create and initialize instance */\r
1929     rmInst = Rm_osalMalloc (sizeof(Rm_Inst));\r
1930     memset ((void *) rmInst, 0, sizeof(Rm_Inst));\r
1931     rmInst->isLocked = false;\r
1932     rmInst->registeredWithDelegateOrServer = false;\r
1933     rmInst->transactionSeqNum = transactionInitSequenceNum();\r
1934 \r
1935     rmInst->instType = initCfg->instType;    \r
1936     strncpy (rmInst->instName, initCfg->instName, RM_NAME_MAX_CHARS);\r
1937 \r
1938     if (rmInst->instType == Rm_instType_SERVER) {\r
1939         if (!initCfg->instCfg.serverCfg.globalResourceList ||\r
1940             !initCfg->instCfg.serverCfg.globalPolicy) {\r
1941             *result = RM_ERROR_INVALID_SERVER_CONFIGURATION;\r
1942             Rm_osalFree((void *)rmInst, sizeof(Rm_Inst));\r
1943             return(NULL);\r
1944         }\r
1945 \r
1946         rmInst->policy = initCfg->instCfg.serverCfg.globalPolicy;\r
1947         fdt_open_into(rmInst->policy, rmInst->policy, fdt_totalsize(rmInst->policy)); \r
1948 \r
1949         if (initCfg->instCfg.serverCfg.linuxDtb) {\r
1950             linuxResourceDtb = initCfg->instCfg.serverCfg.linuxDtb;\r
1951             fdt_open_into(linuxResourceDtb, linuxResourceDtb, fdt_totalsize(linuxResourceDtb));\r
1952             addLinux = true;\r
1953         }\r
1954 \r
1955         /* Create valid instance list from policy.  Must be done prior to parsing\r
1956          * GRL so that Linux resources can be reserved correctly */\r
1957         rmInst->validInstances = rmPolicyCreateValidInstTree(rmInst->policy, addLinux, result);\r
1958         /* Validate policy assignment strings */\r
1959         *result = rmPolicyValidatePolicy(rmInst->policy, rmInst->validInstances);  \r
1960 \r
1961         rmInst->nameServer = rmNameServerInit();\r
1962 \r
1963         globalResourceDtb = initCfg->instCfg.serverCfg.globalResourceList;\r
1964         fdt_open_into(globalResourceDtb, globalResourceDtb, fdt_totalsize(globalResourceDtb));\r
1965 \r
1966         if ((*result = initializeAllocators(rmInst, globalResourceDtb, linuxResourceDtb)) == RM_OK) {  \r
1967             *result = rmPolicyValidatePolicyResourceNames(rmInst->policy, (void *)rmInst->allocators);\r
1968         }\r
1969         if (*result < RM_OK) {\r
1970             Rm_osalFree((void *)rmInst, sizeof(Rm_Inst));\r
1971             return(NULL);\r
1972         }\r
1973     }\r
1974 \r
1975     if ((rmInst->instType == Rm_instType_CLIENT) && \r
1976         (initCfg->instCfg.clientCfg.staticPolicy)) {\r
1977         rmInst->staticInfo.staticPolicy = initCfg->instCfg.clientCfg.staticPolicy; \r
1978     }\r
1979     else if ((rmInst->instType == Rm_instType_CLIENT_DELEGATE) &&\r
1980              (initCfg->instCfg.cdCfg.staticPolicy)) { \r
1981         rmInst->staticInfo.staticPolicy = initCfg->instCfg.cdCfg.staticPolicy; \r
1982     }\r
1983     if (rmInst->staticInfo.staticPolicy) {\r
1984         fdt_open_into(rmInst->staticInfo.staticPolicy, rmInst->staticInfo.staticPolicy, \r
1985                       fdt_totalsize(rmInst->staticInfo.staticPolicy));       \r
1986         rmInst->staticInfo.staticValidInstTree = rmPolicyCreateValidInstTree(rmInst->staticInfo.staticPolicy, \r
1987                                                                              addLinux, result);        \r
1988         if (*result == RM_OK) {\r
1989             /* Validate policy assignment strings */\r
1990             *result = rmPolicyValidatePolicy(rmInst->staticInfo.staticPolicy, rmInst->staticInfo.staticValidInstTree);\r
1991         }\r
1992         if (*result != RM_OK) {\r
1993             if (rmInst->staticInfo.staticValidInstTree) {\r
1994                 rmPolicyFreeValidInstTree(rmInst->staticInfo.staticValidInstTree);\r
1995             }\r
1996             Rm_osalFree((void *)rmInst, sizeof(Rm_Inst));\r
1997             rmInst = NULL;\r
1998         }\r
1999     }\r
2000     return ((Rm_Handle) rmInst);\r
2001 }\r
2002 \r
2003 /* FUNCTION PURPOSE: Returns RM version information\r
2004  ***********************************************************************\r
2005  */\r
2006 uint32_t Rm_getVersion (void)\r
2007 {\r
2008     return RM_VERSION_ID;\r
2009 }\r
2010 \r
2011 /* FUNCTION PURPOSE: Returns RM version string\r
2012  ***********************************************************************\r
2013  */\r
2014 const char* Rm_getVersionStr (void)\r
2015 {\r
2016     return rmVersionStr;\r
2017 }\r
2018 \r