]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/rm-lld.git/blob - src/rm.c
Integrated red-black tree algorithm for tree allocators
[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, 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 /* RM Types */\r
43 #include <ti/drv/rm/rm_types.h>\r
44 \r
45 /* RM external includes */\r
46 #include <ti/drv/rm/rm.h>\r
47 #include <ti/drv/rm/rm_services.h>\r
48 #include <ti/drv/rm/rm_transport.h>\r
49 #include <ti/drv/rm/rm_policy.h>\r
50 \r
51 /* RM internal includes */\r
52 #include <ti/drv/rm/include/rm_loc.h>\r
53 #include <ti/drv/rm/include/rm_transportloc.h>\r
54 #include <ti/drv/rm/include/rm_servicesloc.h>\r
55 #include <ti/drv/rm/include/rm_nameserverloc.h>\r
56 #include <ti/drv/rm/include/rm_dtb_utilloc.h>\r
57 \r
58 /* RM LIBFDT includes */\r
59 #include <ti/drv/rm/src/libfdt/libfdt.h>\r
60 \r
61 /* AVL BBST includes */\r
62 #include <ti/drv/rm/include/tree.h>\r
63 \r
64 /* RM OSAL layer */\r
65 #include <rm_osal.h>\r
66 \r
67 /**********************************************************************\r
68  ************************** Globals ***********************************\r
69  **********************************************************************/\r
70 char rmIntegerAllocator[] = "integer";\r
71 char rmTreeAllocator[] = "tree";\r
72 \r
73 extern char rmDtbStartingNode[];\r
74 \r
75 /** @brief Global Variable which describes the RM Version Information */\r
76 const char   rmVersionStr[] = RM_VERSION_STR ":" __DATE__  ":" __TIME__;\r
77 \r
78 /**********************************************************************\r
79  ************** Red-Black BBST Tree Allocator Functions ***************\r
80  **********************************************************************/\r
81 \r
82 /* TODO: This needed? */\r
83 /* Prototype for function that allocates new tree nodes */\r
84 Rm_ResourceTreeNode *Rm_newResourceTreeNode(uint32_t resourceBase, uint32_t resourceLength)\r
85 {\r
86     Rm_ResourceTreeNode *newNode = NULL;\r
87 \r
88     newNode = Rm_osalMalloc(sizeof(Rm_ResourceTreeNode));\r
89 \r
90     /* Populate the RM relevant fields */\r
91     newNode->base = resourceBase;\r
92     newNode->length = resourceLength;\r
93 \r
94     return(newNode);\r
95 }\r
96 \r
97 /* TODO: This needed? */\r
98 /* Prototype for function that frees new tree nodes */\r
99 void Rm_freeResourceTreeNode(Rm_ResourceTreeNode *treeNode)\r
100 {\r
101     /* Free the memory associated with the tree node. */\r
102     Rm_osalFree((void *)treeNode, sizeof(Rm_ResourceTreeNode));\r
103 }\r
104 \r
105 /* Prototype for tree node comparison function\r
106  * element1 < element2 --> return < 0\r
107  * element1 = element2 --> return 0\r
108  * element1 > element2 --> return > 0 */\r
109 int Rm_ResourceTreeNodeCompare(Rm_ResourceTreeNode *element1, Rm_ResourceTreeNode *element2)\r
110 {\r
111     uint32_t element1End = element1->base + element1->length - 1;\r
112     uint32_t element2End = element2->base + element2->length - 1;\r
113 \r
114     if (element1End < element2->base)\r
115     {\r
116         /* End of element1 range is less than the start of element2's range.  Return a negative\r
117          * value */\r
118         return (-1);\r
119     }\r
120     else if (element1->base > element2End)\r
121     {\r
122         /* Start of element1 range is after end of element2's range.  Return a positive value */\r
123         return (1);\r
124     }\r
125     else\r
126     {\r
127         /* If neither of the latter conditions were satisfied there is some overlap between\r
128          * element1 and element2.  Return 0 since the application must handle this overlap. */\r
129         return (0);\r
130     }\r
131 }\r
132 \r
133 /* Generate the red-black tree manipulation functions */\r
134 RB_GENERATE(_Rm_ResourceTree, _Rm_ResourceTreeNode, linkage, Rm_ResourceTreeNodeCompare);\r
135 \r
136 /**********************************************************************\r
137  ********************** Internal Functions ****************************\r
138  **********************************************************************/\r
139 \r
140 Rm_Transaction *Rm_transactionQueueAdd(Rm_Inst *rmInst)\r
141 {\r
142     Rm_Transaction *transactionQueue = (Rm_Transaction *)rmInst->transactionQueue;\r
143     Rm_Transaction *newTransaction = NULL;\r
144     void *key;\r
145 \r
146     /* Lock access to the RM instance's transaction queue */\r
147     key = Rm_osalMtCsEnter();\r
148 \r
149     /* Get memory for a new transaction from local memory */\r
150     newTransaction = Rm_osalMalloc(sizeof(Rm_Transaction));\r
151 \r
152     /* Return if the memory allocated for the transaction entry is NULL */\r
153     if (newTransaction == NULL)\r
154     {\r
155         /* Clear the transaction */\r
156         memset((void *)newTransaction, 0, sizeof(Rm_Transaction));\r
157 \r
158         /* Create an ID for the new transaction.  The ID will be used for two purposes:\r
159          * 1) Matching responses from higher level RM agents to requests\r
160          * 2) Provided to the component that requested the service so that it can match its\r
161          *    request with the response it receives via its callback function it provided */\r
162         newTransaction->localId = Rm_transactionGetSequenceNum(rmInst);\r
163         /* New transaction's nextTransaction pointer will always be NULL */\r
164         newTransaction->nextTransaction = NULL;  \r
165 \r
166         /* Check if there are any transactions in the transaction queue */\r
167         if (transactionQueue)\r
168         {\r
169             /* At least one transaction in the transaction queue.  Add the new entry to the \r
170              * end of the transaction queue */\r
171             while (transactionQueue->nextTransaction != NULL)\r
172             {\r
173                 /* Traverse the list until arriving at the last transaction */\r
174                 transactionQueue = transactionQueue->nextTransaction;\r
175             }\r
176 \r
177             /* Add the new transaction to the end of the queue */\r
178             transactionQueue->nextTransaction = newTransaction;\r
179         }\r
180         else\r
181         {\r
182             /* The transaction queue does not currently exist.  The new transaction is the \r
183              * first transaction */\r
184             rmInst->transactionQueue = newTransaction;\r
185         }\r
186     }\r
187 \r
188     Rm_osalMtCsExit(key);\r
189     return (newTransaction);\r
190 }\r
191 \r
192 Rm_Transaction *Rm_transactionQueueFind(Rm_Inst *rmInst, uint32_t transactionId)\r
193 {\r
194     Rm_Transaction *transaction = (Rm_Transaction *)rmInst->transactionQueue;\r
195 \r
196     /* Make sure there is at least one transaction in the transaction queue */\r
197     if (transaction != NULL)\r
198     {\r
199         /* Find the transaction ID within the specified RM instance's transaction queue.\r
200          * If the end of the transaction queue is reached without finding the transaction the \r
201          * transaction pointer will be NULL */\r
202         while (transaction != NULL)\r
203         {\r
204             if (transaction->localId == transactionId)\r
205             {\r
206                 /* Match: break out of loop and return the transaction */\r
207                 break;             \r
208             }\r
209             transaction = transaction->nextTransaction;\r
210         }\r
211     }\r
212 \r
213     return (transaction);\r
214 }\r
215 \r
216 int32_t Rm_transactionQueueDelete(Rm_Inst *rmInst, uint32_t transactionId)\r
217 {\r
218     Rm_Transaction *transaction = (Rm_Transaction *) rmInst->transactionQueue;\r
219     Rm_Transaction *prevTransaction = NULL;\r
220     int32_t retVal = RM_SERVICE_STATE_OKAY;\r
221     void *key;\r
222 \r
223     /* Lock access to the RM instance's transaction queue */\r
224     key = Rm_osalMtCsEnter();\r
225 \r
226     /* Find the transaction ID within the specified RM instance's transaction queue. */\r
227     while (transaction != NULL)\r
228     {\r
229         if (transaction->localId == transactionId)\r
230         {\r
231             /* Match: break out of loop and delete the transaction */\r
232             break;             \r
233         }\r
234 \r
235         prevTransaction = transaction;\r
236         transaction = transaction->nextTransaction;\r
237     }\r
238 \r
239     /* Traversed entire queue but did not find transaction */\r
240     if (transaction == NULL)\r
241     {\r
242         retVal = RM_SERVICE_ERROR_SERVICE_TRANSACTION_DOES_NOT_EXIST;\r
243     }\r
244     else\r
245     {\r
246         /* Delete the transaction */\r
247         if ((prevTransaction == NULL) && transaction->nextTransaction)\r
248         {\r
249             /* Transaction to be deleted exists at start of transaction queue.  Map second\r
250              * transaction to be start of transaction queue as long as there are more than\r
251              * one transactions. */\r
252             rmInst->transactionQueue = transaction->nextTransaction;\r
253         }\r
254         else\r
255         {\r
256             /* Transaction to be deleted is in the middle or at end of the queue.  Adjust \r
257              * adjacent transaction pointers.  This covers the case where the transaction to be \r
258              * removed is at the end of the queue. */\r
259             prevTransaction->nextTransaction = transaction->nextTransaction;\r
260         }\r
261 \r
262         /* Free the memory associated with the transaction. */\r
263         Rm_osalFree((void *)transaction, sizeof(Rm_Transaction));\r
264     }\r
265 \r
266     Rm_osalMtCsExit(key);\r
267     return (retVal);\r
268 }\r
269 \r
270 uint32_t Rm_transactionInitSequenceNum(void)\r
271 {\r
272     /* Sequence number can never have a value of zero so that there are no conflicts\r
273      * with transactions that have a remoteOriginatingId of zero */\r
274     return (1);\r
275 }\r
276 \r
277 uint32_t Rm_transactionGetSequenceNum(Rm_Inst *rmInst)\r
278 {\r
279     uint32_t sequenceNum = 0;\r
280 \r
281     /* Get the next sequence number and then increment.  If there's an overflow\r
282      * assign the initial value instead of incrementing. */\r
283     if (rmInst->transactionSeqNum + 1 < rmInst->transactionSeqNum)\r
284     {\r
285         /* Overflow */\r
286         sequenceNum = rmInst->transactionSeqNum;\r
287         rmInst->transactionSeqNum = Rm_transactionInitSequenceNum();\r
288     }\r
289     else\r
290     {\r
291         sequenceNum = rmInst->transactionSeqNum++;\r
292     }    \r
293 \r
294     return (sequenceNum);\r
295 }\r
296 \r
297 /* Function used to send RM response transactions to lower level agents */\r
298 void Rm_transactionResponder (Rm_Inst *rmInst, Rm_Transaction *transaction)\r
299 {\r
300     Rm_TransportNode *dstTransportNode = NULL;\r
301     Rm_Packet *rmPkt = NULL;\r
302 \r
303     /* Find the transport for the RM instance that sent the request. */\r
304     dstTransportNode = Rm_transportNodeFindRemoteName(rmInst, transaction->sourceInstName);\r
305 \r
306     /* Create a RM packet using the service information */\r
307     switch (transaction->type)\r
308     {\r
309         case Rm_service_RESOURCE_ALLOCATE:\r
310         case Rm_service_RESOURCE_BLOCK_ALLOCATE:\r
311         case Rm_service_RESOURCE_ALLOCATE_BY_NAME:\r
312         case Rm_service_RESOURCE_FREE:\r
313         case Rm_service_RESOURCE_BLOCK_FREE:\r
314         case Rm_service_RESOURCE_FREE_BY_NAME:\r
315             rmPkt = Rm_transportCreateResourceResponsePkt(rmInst, dstTransportNode, \r
316                                                           transaction);\r
317             break;\r
318         case Rm_service_RESOURCE_MAP_TO_NAME:\r
319         case Rm_service_RESOURCE_UNMAP_NAME:\r
320             rmPkt = Rm_transportCreateNsResponsePkt(rmInst, dstTransportNode,\r
321                                                     transaction);\r
322             break;\r
323         default:\r
324             /* Invalid service type.  Flag the error and return */\r
325             transaction->state = RM_SERVICE_ERROR_INVALID_SERVICE_TYPE;\r
326             break;\r
327     }\r
328 \r
329     if (transaction->state <= RM_SERVICE_ERROR_BASE)\r
330     {\r
331         /* Delete the transaction and return immediately because an error occurred \r
332          * allocating the packet */\r
333         Rm_transactionQueueDelete(rmInst, transaction->localId);\r
334         return;\r
335     }\r
336 \r
337     /* Send the RM packet to the application transport */\r
338     if (rmInst->transport.rmSend((Rm_TransportHandle) dstTransportNode, rmPkt) < RM_TRANSPORT_SUCCESSFUL)\r
339     {\r
340         /* Negative value returned by transport send.  An error occurred\r
341          * in the transport while attempting to send the packet.*/\r
342         transaction->state = RM_SERVICE_ERROR_TRANPSPORT_SEND_ERROR;\r
343         /* Clean up the packet */\r
344         if (rmInst->transport.rmFreePkt((Rm_TransportHandle) dstTransportNode, rmPkt))\r
345         {\r
346             /* Non-NULL value returned by transport packet free. Flag the\r
347              * error */\r
348             transaction->state = RM_SERVICE_ERROR_TRANSPORT_FREE_PKT_ERROR;\r
349         }\r
350         return;\r
351     }\r
352 \r
353     /* NEED TO DO SOMETHING IF GET AN ERROR IN THE transaction->state FIELD.  CREATE\r
354      * NEW TRANSACTION WITH DATA FROM ORIGINAL?  THEN TRY TO SEND FAILED REQUEST BACK\r
355      * TO REQUESTER???  KEEP RETRYING SEND OF RESPONSE??? */\r
356 \r
357     /* Delete the transaction */\r
358     Rm_transactionQueueDelete(rmInst, transaction->localId);\r
359 }\r
360 \r
361 Rm_Allocator *Rm_allocatorAdd(Rm_Inst *rmInst, const char *resourceName, Rm_AllocatorType type)\r
362 {\r
363     Rm_Allocator *allocators = (Rm_Allocator *)rmInst->allocators;\r
364     Rm_Allocator *newAllocator = NULL;\r
365     void *key;\r
366 \r
367     /* Lock access to the RM instance's allocator list */\r
368     key = Rm_osalMtCsEnter();\r
369 \r
370     /* Get memory for a new allocator from local memory */\r
371     newAllocator = Rm_osalMalloc(sizeof(Rm_Allocator));\r
372 \r
373     /* Return if the memory allocated for the allocator is NULL */\r
374     if (newAllocator != NULL)\r
375     {\r
376         /* Clear the allocator */\r
377         memset((void *)newAllocator, 0, sizeof(Rm_Allocator));\r
378 \r
379         /* Populate the allocator */\r
380         newAllocator->type = type;\r
381         strcpy(newAllocator->resourceName, resourceName);\r
382         /* allocator's root entry will be created by the invoking function */\r
383         newAllocator->allocatorRootEntry = NULL;\r
384         /* New allocator's nextAllocator pointer will always be NULL */\r
385         newAllocator->nextAllocator = NULL;  \r
386 \r
387         /* Check if there are any allocators in the allocator list */\r
388         if (allocators)\r
389         {\r
390             /* At least one allocator in the allocator list.  Add the new allocator to the \r
391              * end of the allocator list */\r
392             while (allocators->nextAllocator != NULL)\r
393             {\r
394                 /* Traverse the list until arriving at the last allocator */\r
395                 allocators = allocators->nextAllocator;\r
396             }\r
397 \r
398             /* Add the new allocator to the end of the list */\r
399             allocators->nextAllocator = newAllocator;\r
400         }\r
401         else\r
402         {\r
403             /* The allocator list does not currently exist.  The new allocator is the \r
404              * first allocator */\r
405             rmInst->allocators = newAllocator;\r
406         }\r
407     }\r
408 \r
409     Rm_osalMtCsExit(key);\r
410     return (newAllocator);\r
411 }\r
412 \r
413 Rm_Allocator *Rm_allocatorFind(Rm_Inst *rmInst, char *resourceName)\r
414 {\r
415     Rm_Allocator *allocator = (Rm_Allocator *)rmInst->allocators;\r
416 \r
417     /* Make sure there is at least one allocator in the allocator list */\r
418     if (allocator != NULL)\r
419     {\r
420         /* Find the resource name within the allocator list.  If the end of the\r
421          * allocator list is reached without finding the resource name the \r
422          * allocator pointer will be NULL */\r
423         while (allocator != NULL)\r
424         {\r
425             if (strcmp(allocator->resourceName, resourceName) == 0)\r
426             {\r
427                 /* Match: break out of loop and return the allocator */\r
428                 break;             \r
429             }\r
430             allocator = allocator->nextAllocator;\r
431         }\r
432     }\r
433 \r
434     return (allocator);\r
435 }\r
436 \r
437 int32_t Rm_allocatorDelete(Rm_Inst *rmInst, char *resourceName)\r
438 {\r
439     Rm_Allocator *allocator = (Rm_Allocator *) rmInst->allocators;\r
440     Rm_Allocator *prevAllocator = NULL;\r
441     int32_t retVal = RM_SERVICE_STATE_OKAY;\r
442     void *key;\r
443 \r
444     /* Lock access to the RM instance's allocator list */\r
445     key = Rm_osalMtCsEnter();\r
446 \r
447     /* Find the resource within the specified RM instance's allocator list. */\r
448     while (allocator != NULL)\r
449     {\r
450         if (strcmp(allocator->resourceName, resourceName) == 0)\r
451         {\r
452             /* Match: break out of loop and delete the transaction */\r
453             break;             \r
454         }\r
455 \r
456         prevAllocator = allocator;\r
457         allocator = allocator->nextAllocator;\r
458     }\r
459 \r
460     /* Traversed entire list but did not find allocator. */\r
461     if (allocator == NULL)\r
462     {\r
463         retVal = -22; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
464     }\r
465     else\r
466     {\r
467         /* Delete the allocator */\r
468         if ((prevAllocator == NULL) && allocator->nextAllocator)\r
469         {\r
470             /* Allocator to be deleted exists at start of allocator list.  Map second\r
471              * allocator to be start of allocator list as long as there are more than\r
472              * one allocators. */\r
473             rmInst->allocators = allocator->nextAllocator;\r
474         }\r
475         else\r
476         {\r
477             /* Allocator to be deleted is in the middle or at end of the list.  Adjust \r
478              * adjacent allocator pointers.  This covers the case where the allocator to be \r
479              * removed is at the end of the list. */\r
480             prevAllocator->nextAllocator = allocator->nextAllocator;\r
481         }\r
482 \r
483         /* Free the memory associated with the allocator. */\r
484         Rm_osalFree((void *)allocator, sizeof(Rm_Allocator));\r
485     }\r
486 \r
487     Rm_osalMtCsExit(key);\r
488     return (retVal);\r
489 }\r
490 \r
491 int32_t Rm_createIntegerAllocator(Rm_Inst *rmInst, const char *resourceName, Rm_ResourceRange *range)\r
492 {\r
493     Rm_Allocator *allocator = NULL;\r
494     Rm_ResourceRange *rangeBasePtr = range;\r
495     Rm_IntegerAllocatorRootEntry *intRootEntry = NULL;\r
496     uint16_t i, entryIndex;\r
497 \r
498     /* Create the new base integer allocator */\r
499     allocator = Rm_allocatorAdd(rmInst, resourceName, Rm_allocatorType_INTEGER);\r
500 \r
501     /* Construct the integer allocator root entry */\r
502     intRootEntry = Rm_osalMalloc(sizeof(Rm_IntegerAllocatorRootEntry));\r
503     intRootEntry->numResourceElements = 0;\r
504 \r
505     /* Get the number of entries to allocate based on the lengths in the ranges */\r
506     while (range != NULL)\r
507     {\r
508         intRootEntry->numResourceElements += range->length;\r
509         range = range->nextRange;\r
510     }\r
511 \r
512     /* Initialize the entries using the range information */\r
513     if (intRootEntry->numResourceElements)\r
514     {\r
515         intRootEntry->resourceArrayBase = Rm_osalMalloc(sizeof(Rm_IntegerEntry) * intRootEntry->numResourceElements);\r
516         memset((void *)intRootEntry->resourceArrayBase, 0, sizeof(Rm_IntegerEntry) * intRootEntry->numResourceElements);\r
517 \r
518         /* Reset the range pointer */\r
519         range = rangeBasePtr;\r
520         entryIndex = 0;\r
521 \r
522         while (range != NULL)\r
523         {\r
524             /* Initialize each entry */\r
525             for (i = range->base; i < (range->base + range->length); i++, entryIndex++)\r
526             {\r
527                 intRootEntry->resourceArrayBase[entryIndex].value = i;\r
528                 /* Initialize the allocatedTo field to the NOT_ALLOCATED string */\r
529                 strcpy(intRootEntry->resourceArrayBase[entryIndex].allocatedTo, RM_NOT_ALLOCATED_STRING);\r
530             }\r
531             \r
532             range = range->nextRange;\r
533         }\r
534         \r
535         allocator->allocatorRootEntry = intRootEntry;\r
536     }\r
537     else\r
538     {\r
539         /* No resource entries were created.  Free the memory associated with the\r
540          * allocator and the root entry */\r
541         Rm_osalFree((void *)intRootEntry, sizeof(Rm_IntegerAllocatorRootEntry));\r
542         Rm_allocatorDelete(rmInst, allocator->resourceName);\r
543     }\r
544 \r
545     return(0);  /* TODO: FIX THIS RETURN */\r
546 }\r
547 \r
548 int32_t Rm_createTreeAllocator(Rm_Inst *rmInst, const char *resourceName, Rm_ResourceRange *range)\r
549 {\r
550     Rm_Allocator *allocator = NULL;\r
551     Rm_ResourceTree *treeRootEntry = NULL;\r
552     Rm_ResourceTreeNode *treeNode = NULL;\r
553     Rm_ResourceTreeNode *collidingNode = NULL;\r
554 \r
555     /* Create the new base integer allocator */\r
556     allocator = Rm_allocatorAdd(rmInst, resourceName, Rm_allocatorType_TREE);\r
557 \r
558     /* Create the tree root entry and initialize it */\r
559     treeRootEntry = Rm_osalMalloc(sizeof(Rm_ResourceTree));\r
560     RB_INIT(treeRootEntry);\r
561 \r
562     /* Create a node in the tree for resource range and insert them into the tree. */\r
563     while (range != NULL)\r
564     {\r
565         treeNode = Rm_osalMalloc(sizeof(Rm_ResourceTreeNode));\r
566         treeNode->base = range->base;\r
567         treeNode->length = range->length;\r
568         /* Initialize the allocatedTo field to the NOT_ALLOCATED string */\r
569         strcpy(treeNode->allocatedTo, RM_NOT_ALLOCATED_STRING);\r
570 \r
571         /* Insert the node into the tree */\r
572         collidingNode = RB_INSERT(_Rm_ResourceTree, treeRootEntry, treeNode);\r
573 \r
574         if (collidingNode)\r
575         {\r
576             Rm_ResourceTreeNode *nextNode = NULL;\r
577             \r
578             /* Node that was inserted colliding with an existing node.  Clean up the tree\r
579              * that's been allocated thus far and return an error since there should be no\r
580              * collisions */\r
581             for (treeNode = RB_MIN(_Rm_ResourceTree, treeRootEntry); treeNode != NULL; treeNode = nextNode)\r
582             {\r
583                         nextNode = RB_NEXT(_Rm_ResourceTree, treeRootEntry, treeNode);\r
584                         RB_REMOVE(_Rm_ResourceTree, treeRootEntry, nextNode);\r
585                         Rm_osalFree((void *)treeNode, sizeof(Rm_ResourceTreeNode));\r
586                 }\r
587             /* Delete the tree root entry and the allocator */\r
588             Rm_osalFree((void *)treeRootEntry, sizeof(Rm_ResourceTree));\r
589             Rm_allocatorDelete(rmInst, allocator->resourceName);\r
590             return (-24); /* TODO FIX RETURN */\r
591         }\r
592 \r
593         range = range->nextRange;\r
594     }\r
595 \r
596     /* Assign the tree's root to the allocator */\r
597     allocator->allocatorRootEntry = treeRootEntry;\r
598 \r
599     /* Print the base values as a test */\r
600     RB_FOREACH(treeNode, _Rm_ResourceTree, (Rm_ResourceTree *) allocator->allocatorRootEntry)\r
601     {\r
602         Rm_osalLog("Tree node base: %d length: %d and allocated to: %s\n", treeNode->base, treeNode->length, treeNode->allocatedTo);\r
603     }\r
604 \r
605     return(0);   /* TODO: FIX THIS RETURN */\r
606 }\r
607 \r
608 int32_t Rm_createAndInitAllocator(Rm_Inst *rmInst, const char *resourceName, \r
609                                   Rm_ResourceProperties *resourceProperties)\r
610 {\r
611     char *allocatorType = NULL;\r
612     Rm_ResourceRange *range = NULL;\r
613     Rm_ResourceRange *rangeBasePtr = NULL;\r
614     Rm_NsAssignment *nsAssignments = NULL;\r
615     Rm_NsAssignment *nsAssignmentBasePtr = NULL;\r
616     int32_t retVal = RM_DTB_UTIL_RESULT_OKAY;\r
617 \r
618     /* TODO: NEED CHECKS FOR VALIDITY OF ALL THE resourceProperties FIELDS */\r
619 \r
620     /* Extract the resource properties from the DTB */\r
621     allocatorType = Rm_resourceExtractResourceAllocator(resourceProperties->allocatorData, \r
622                                                         resourceProperties->allocatorLen);\r
623     range = rangeBasePtr = Rm_resourceExtractResourceRange(resourceProperties->rangeData, \r
624                                                            resourceProperties->rangeLen);\r
625 \r
626     /* Create an allocator based on the allocator type specified */\r
627     if (strcmp(allocatorType, &rmIntegerAllocator[0]) == 0)\r
628     {\r
629         /* Create an integer allocator using the resource properties */\r
630         retVal = Rm_createIntegerAllocator(rmInst, resourceName, range);        \r
631     }\r
632     else if (strcmp(allocatorType, &rmTreeAllocator[0]) == 0)\r
633     {\r
634         /* Create a tree allocator using the resource properties */\r
635         retVal = Rm_createTreeAllocator(rmInst, resourceName, range); \r
636     }\r
637     else\r
638     {\r
639         /* Allocator type not recognized.  Free the resource properties and return */\r
640         retVal = -21; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
641     }\r
642     \r
643     if (retVal >= RM_DTB_UTIL_RESULT_OKAY)\r
644     {\r
645         /* Create entries in the NameServer if any NameServer assignments were specified */\r
646         if (resourceProperties->nsAssignData && resourceProperties->nsAssignLen)\r
647         {\r
648             nsAssignments = Rm_resourceExtractNsAssignment(resourceProperties->nsAssignData, \r
649                                                            resourceProperties->nsAssignLen);\r
650 \r
651             /* Cycle through the list of assignments and add them to the NameServer */\r
652             nsAssignmentBasePtr = nsAssignments;\r
653             while (nsAssignments)\r
654             {\r
655                 /* TODO: RETURN IF ANY OF THE ADDS FAIL??? */\r
656                 Rm_nsAddObject(rmInst, nsAssignments->nsName, nsAssignments->resourceValue);\r
657                 nsAssignments = nsAssignments->nextNsAssignment;\r
658             }\r
659             /* Free the memory allocated for the NameServer assignments */\r
660             Rm_resourceFreeNsAssignmentList(nsAssignmentBasePtr);\r
661         }\r
662     }\r
663 \r
664     /* Free the memory allocated for the resource properties */\r
665     Rm_resourceFreeResourceAllocator(allocatorType);\r
666     Rm_resourceFreeResourceRange(rangeBasePtr);\r
667 \r
668     return(retVal);\r
669 }\r
670 \r
671 int32_t Rm_parseResourceProperty(void *globalResourceDtb, int32_t offset, Rm_ResourceProperties *propertyInfo)\r
672 {\r
673         int32_t propertyLen;\r
674         const char *propertyName;\r
675         const void *propertyData;\r
676     Rm_ResourcePropType propertyType;\r
677     int32_t retVal = RM_DTB_UTIL_RESULT_OKAY;\r
678 \r
679     /* Get the property data and store it in the corresponding propertyInfo field */\r
680         propertyData = fdt_getprop_by_offset(globalResourceDtb, offset, &propertyName, &propertyLen);\r
681     if (propertyData)\r
682     {\r
683         propertyType = Rm_resourceGetPropertyType(propertyName);\r
684         if (propertyType == Rm_resourcePropType_RESOURCE_ALLOCATOR)\r
685         {\r
686             if (propertyInfo->allocatorData || propertyInfo->allocatorLen)\r
687             {\r
688                 /* The allocator fields have already been populated.  Return an error.\r
689                  * The resource list has specified a property field more than once\r
690                  * for a resource node */\r
691                 retVal = -17; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
692             }\r
693             else\r
694             {\r
695                 propertyInfo->allocatorData = propertyData;\r
696                 propertyInfo->allocatorLen = propertyLen;\r
697             }\r
698         }\r
699         else if (propertyType == Rm_resourcePropType_RESOURCE_RANGE)\r
700         {\r
701             if (propertyInfo->rangeData || propertyInfo->rangeLen)\r
702             {\r
703                 /* The range fields have already been populated.  Return an error.\r
704                  * The resource list has specified a property field more than once\r
705                  * for a resource node */\r
706                 retVal = -18; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
707             }\r
708             else\r
709             {\r
710                 propertyInfo->rangeData = propertyData;\r
711                 propertyInfo->rangeLen = propertyLen;\r
712             }\r
713         }\r
714         else if (propertyType == Rm_resourcePropType_NSASSIGNMENT)\r
715         {\r
716             if (propertyInfo->nsAssignData || propertyInfo->nsAssignLen)\r
717             {\r
718                 /* The nsAssign fields have already been populated.  Return an error.\r
719                  * The resource list has specified a property field more than once\r
720                  * for a resource node */\r
721                 retVal = -19; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
722             }\r
723             else\r
724             {\r
725                 propertyInfo->nsAssignData = propertyData;\r
726                 propertyInfo->nsAssignLen = propertyLen;\r
727             }\r
728         }\r
729         else\r
730         {\r
731             retVal = -20; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
732         }\r
733     }\r
734     else\r
735     {\r
736         retVal = -16; /* TEMP ERROR: Can't conflict with LIBFDT errors */\r
737     }\r
738 \r
739     /* Don't get anymore properties if error occurred */\r
740     if (retVal == RM_DTB_UTIL_RESULT_OKAY)\r
741     {\r
742         offset = fdt_next_property_offset(globalResourceDtb, offset);\r
743         if (offset >= 0)\r
744         {\r
745             retVal = Rm_parseResourceProperty(globalResourceDtb, offset, propertyInfo);\r
746         }\r
747         else if (offset != -FDT_ERR_NOTFOUND)\r
748         {\r
749             /* Error was returned by LIBFDT when parsing the properties */\r
750             retVal = offset;\r
751         }\r
752     }\r
753     \r
754     return (retVal);\r
755 }\r
756 \r
757 int32_t Rm_parseResourceNode(Rm_Inst *rmInst, void *globalResourceDtb, int32_t nodeOffset, int32_t depth)\r
758 {\r
759         const char *resourceName = fdt_get_name(globalResourceDtb, nodeOffset, NULL);\r
760     Rm_ResourceProperties resourceProperties;\r
761         int32_t error = RM_DTB_UTIL_RESULT_OKAY;\r
762         int32_t offset;\r
763 \r
764     /* Initialize the resource properties structure */\r
765     memset((void *)&resourceProperties, 0, sizeof(Rm_ResourceProperties));\r
766 \r
767     /* Ignore properties of the base node */\r
768     if (strcmp(resourceName, rmDtbStartingNode))\r
769     {\r
770         /* Get the properties for the resource node if any exist */\r
771         offset = fdt_first_property_offset(globalResourceDtb, nodeOffset);\r
772         if (offset >= RM_DTB_UTIL_STARTING_NODE_OFFSET)\r
773         {\r
774             /* Since at least one property exists attempt to parse the property nodes and \r
775              * use them to create and initialize a resource allocator */\r
776                 error =  Rm_parseResourceProperty(globalResourceDtb, offset, &resourceProperties);\r
777             if (error < -FDT_ERR_NOTFOUND)\r
778             {\r
779                 return (error);\r
780             }\r
781             \r
782             /* Initialize an allocator with the resource properties if no error was returned */\r
783             Rm_createAndInitAllocator(rmInst, resourceName, &resourceProperties);\r
784         }\r
785         else if (offset != -FDT_ERR_NOTFOUND)\r
786         {\r
787                 /* Error was returned by LIBFDT when parsing the properties */\r
788             return (offset);\r
789         }\r
790     }\r
791     \r
792     /* Get the next resource node */\r
793         offset = fdt_next_node(globalResourceDtb, nodeOffset, &depth);\r
794     /* Check the offset and depth of the next node to make sure the current node\r
795      * wasn't the last node in the Resource List.  A depth less than the depth set\r
796      * at the start of the recursion will signal the end of the resource list */\r
797     if ((offset >= RM_DTB_UTIL_STARTING_NODE_OFFSET) && (depth >= RM_DTB_UTIL_STARTING_DEPTH))\r
798     {\r
799         error = Rm_parseResourceNode(rmInst, globalResourceDtb, offset, depth);\r
800         if (error < -FDT_ERR_NOTFOUND)\r
801         {\r
802             return (error);\r
803         }\r
804     }\r
805     else if (offset != -FDT_ERR_NOTFOUND)\r
806     {\r
807         /* Error was returned by LIBFDT when parsing the nodes */\r
808         return (offset);\r
809     }\r
810 \r
811     return (RM_DTB_UTIL_RESULT_OKAY);\r
812 }\r
813 \r
814 /* Called when an allocate request is made but the base is unspecified.  RM must preallocate\r
815  * resources which then must be checked against the RM policy for the instance.  If the\r
816  * policy does not agree another resource(s) must be preallocated and tested against the \r
817  * policy.  Policy will provide initialize the preallocate with the base that it allows\r
818  * for the rm instance for the specified resource. */\r
819 int32_t Rm_integerPreAllocate(Rm_Allocator *allocator, Rm_AllocatorOpInfo *opInfo)\r
820 {\r
821     \r
822 }\r
823 \r
824 int32_t Rm_integerAllocate(Rm_Allocator *allocator, Rm_AllocatorOpInfo *opInfo)\r
825 {\r
826     Rm_IntegerAllocatorRootEntry *root = allocator->allocatorRootEntry;\r
827     uint16_t resourceIndex, i, j;\r
828     bool resourcesValidated = TRUE;\r
829     int32_t retVal;\r
830 \r
831     /* Find the specified resource base within the allocator */\r
832     for (resourceIndex = 0; resourceIndex < root->numResourceElements; resourceIndex++)\r
833     {\r
834         if (root->resourceArrayBase[resourceIndex].value == opInfo->resourceInfo->base)\r
835         {\r
836             /* Found the resource base in the allocator.  Break from the loop */\r
837             break;\r
838         }\r
839     }\r
840 \r
841     /* Only execute the allocate operation if the resource base was found in the allocator\r
842      * and the base+range does not exceed the number of entries in the allocator */\r
843     if (resourceIndex + opInfo->resourceInfo->range <= root->numResourceElements)\r
844     {\r
845         /* Verify all resource values from base to base+range exist in the allocator and\r
846          * are not allocated to another instance. */\r
847         for (i = resourceIndex, j = opInfo->resourceInfo->base; \r
848              i < (resourceIndex + opInfo->resourceInfo->range);\r
849              i++, j++)\r
850         {\r
851             if (root->resourceArrayBase[i].value != j)\r
852             {\r
853                 /* A value in the range did not match. */\r
854                 retVal = RM_SERVICE_DENIED_RESOURCE_VALUE_IN_RANGE_DOES_NOT_EXIST;\r
855                 resourcesValidated = FALSE;\r
856                 break;\r
857             }\r
858             else if (strcmp(root->resourceArrayBase[i].allocatedTo, RM_NOT_ALLOCATED_STRING) != 0)\r
859             {\r
860                 /* A value in the range is already allocated. */\r
861                 retVal = RM_SERVICE_DENIED_RESOURCE_ALREADY_ALLOCATED;\r
862                 resourcesValidated = FALSE;\r
863                 break;            \r
864             }\r
865         }\r
866 \r
867         if (resourcesValidated)\r
868         {\r
869             /* Allocate all resources from base to base+range */\r
870             for (i = resourceIndex; i < (resourceIndex + opInfo->resourceInfo->range); i++)\r
871             {\r
872                 strcpy(root->resourceArrayBase[i].allocatedTo, opInfo->srcInstName);\r
873             }\r
874             retVal = RM_SERVICE_APPROVED_AND_COMPLETED;\r
875         }\r
876     }\r
877     else\r
878     {\r
879         retVal = RM_SERVICE_DENIED_RESOURCE_VALUE_IN_RANGE_DOES_NOT_EXIST;\r
880     }\r
881 \r
882     return(retVal); \r
883 }\r
884 \r
885 int32_t Rm_integerFree(Rm_Allocator *allocator, Rm_AllocatorOpInfo *opInfo)\r
886 {\r
887     Rm_IntegerAllocatorRootEntry *root = allocator->allocatorRootEntry;\r
888     uint16_t resourceIndex, i, j;\r
889     bool resourcesValidated = TRUE;\r
890     int32_t retVal;\r
891 \r
892     /* Find the specified resource base within the allocator */\r
893     for (resourceIndex = 0; resourceIndex < root->numResourceElements; resourceIndex++)\r
894     {\r
895         if (root->resourceArrayBase[resourceIndex].value == opInfo->resourceInfo->base)\r
896         {\r
897             /* Found the resource base in the allocator.  Break from the loop */\r
898             break;\r
899         }\r
900     }\r
901 \r
902     /* Only execute the free operation if the resource base was found in the allocator\r
903      * and the base+range does not exceed the number of entries in the allocator */\r
904     if (resourceIndex + opInfo->resourceInfo->range <= root->numResourceElements)\r
905     {\r
906         /* Verify all resource values from base to base+range exist in the allocator,\r
907          * were not already free and were allocated to the instance that is the source\r
908          * of the free request. */\r
909         for (i = resourceIndex, j = opInfo->resourceInfo->base; \r
910              i < (resourceIndex + opInfo->resourceInfo->range);\r
911              i++, j++)\r
912         {\r
913             if (root->resourceArrayBase[i].value != j)\r
914             {\r
915                 /* A value in the range did not match. */\r
916                 retVal = RM_SERVICE_DENIED_RESOURCE_VALUE_IN_RANGE_DOES_NOT_EXIST;\r
917                 resourcesValidated = FALSE;\r
918                 break;\r
919             }\r
920             else if (strcmp(root->resourceArrayBase[i].allocatedTo, RM_NOT_ALLOCATED_STRING) == 0)\r
921             {\r
922                 /* A value in the range is already free. */\r
923                 retVal = RM_SERVICE_DENIED_RESOURCE_ALREADY_FREE;\r
924                 resourcesValidated = FALSE;\r
925                 break;            \r
926             }            \r
927             else if (strcmp(root->resourceArrayBase[i].allocatedTo, opInfo->srcInstName) != 0)\r
928             {\r
929                 /* A value in the range was not allocated to the source of\r
930                  * the free request */\r
931                 retVal = RM_SERVICE_DENIED_RESOURCE_NOT_ALLOCATED_TO_INSTANCE_REQUESTING_THE_SERVICE;\r
932                 resourcesValidated = FALSE;\r
933                 break;\r
934             }\r
935         }\r
936 \r
937         if (resourcesValidated)\r
938         {\r
939             /* Free all resources from base to base+range */\r
940             for (i = resourceIndex; i < (resourceIndex + opInfo->resourceInfo->range); i++)\r
941             {\r
942                 strcpy(root->resourceArrayBase[i].allocatedTo, RM_NOT_ALLOCATED_STRING);\r
943             }\r
944             retVal = RM_SERVICE_APPROVED_AND_COMPLETED;\r
945         }\r
946     }\r
947     else\r
948     {\r
949         retVal = RM_SERVICE_DENIED_RESOURCE_VALUE_IN_RANGE_DOES_NOT_EXIST;\r
950     }\r
951 \r
952     return(retVal);\r
953 }\r
954 \r
955 /* Called when an allocate request is made but the base is unspecified.  RM must preallocate\r
956  * resources which then must be checked against the RM policy for the instance.  If the\r
957  * policy does not agree another resource(s) must be preallocated and tested against the \r
958  * policy */\r
959 int32_t Rm_treePreAllocate(Rm_Allocator *allocator, Rm_AllocatorOpInfo *opInfo)\r
960 {\r
961 \r
962 }\r
963 \r
964 int32_t Rm_treeAllocate(Rm_Allocator *allocator, Rm_AllocatorOpInfo *opInfo)\r
965 {\r
966     \r
967 }\r
968 \r
969 int32_t Rm_treeFree(Rm_Allocator *allocator, Rm_AllocatorOpInfo *opInfo)\r
970 {\r
971 \r
972 }\r
973 \r
974 int32_t Rm_allocatorOperation(Rm_Inst *rmInst, Rm_AllocatorOpInfo *opInfo)\r
975 {\r
976     Rm_Allocator *allocator = NULL;\r
977     int32_t retVal;\r
978     void *key;\r
979 \r
980     /* Lock access to the RM instance's transaction queue */\r
981     key = Rm_osalMtCsEnter();\r
982 \r
983     /* Get the specified resource's allocator */\r
984     allocator = Rm_allocatorFind(rmInst, opInfo->resourceInfo->name);\r
985 \r
986     if (allocator)\r
987     {\r
988         /* Call the allocator's type-based allocation function */\r
989         if(allocator->type == Rm_allocatorType_INTEGER)\r
990         {\r
991             if (opInfo->operation == Rm_allocatorOp_PRE_ALLOCATE)\r
992             {\r
993                 retVal = Rm_integerPreAllocateUnspecified(allocator, opInfo);\r
994             }            \r
995             else if (opInfo->operation == Rm_allocatorOp_ALLOCATE)\r
996             {\r
997                 retVal = Rm_integerAllocate(allocator, opInfo);\r
998             }\r
999             else if (opInfo->operation == Rm_allocatorOp_FREE)\r
1000             {\r
1001                 retVal = Rm_integerFree(allocator, opInfo);\r
1002             }\r
1003         }\r
1004         else if (allocator->type == Rm_allocatorType_TREE)\r
1005         {\r
1006             if (opInfo->operation == Rm_allocatorOp_PRE_ALLOCATE)\r
1007             {\r
1008                 retVal = Rm_treePreAllocateUnspecified(allocator, opInfo);\r
1009             }               \r
1010             else if (opInfo->operation == Rm_allocatorOp_ALLOCATE)\r
1011             {\r
1012                 retVal = Rm_treeAllocate(allocator, opInfo);\r
1013             }\r
1014             else if (opInfo->operation == Rm_allocatorOp_FREE)\r
1015             {\r
1016                 retVal = Rm_treeFree(allocator, opInfo);\r
1017             }  \r
1018         }        \r
1019     }\r
1020     else\r
1021     {\r
1022         /* Allocator could not be found for resource */\r
1023         retVal = RM_SERVICE_DENIED_RESOURCE_DOES_NOT_EXIST;\r
1024     }\r
1025 \r
1026     Rm_osalMtCsExit(key);\r
1027     return(retVal);\r
1028 }\r
1029 \r
1030 void Rm_allocationHandler (Rm_Inst *rmInst, Rm_Transaction *transaction)\r
1031 {\r
1032     if (rmInst->instType == Rm_instType_CLIENT_DELEGATE)\r
1033     {\r
1034 #if 0        \r
1035         if (resourceBase is unspecified)\r
1036         {\r
1037            while (policy does not approve)\r
1038            {\r
1039                Rm_policy check get allowed base as starting point for prealloc\r
1040                preallocate resource based on the range and alignment\r
1041                Rm_policy...check\r
1042            }\r
1043         }\r
1044         else\r
1045         {\r
1046             /* Check local policy to see if the request can be satisfied with the\r
1047              * resources stored locally */\r
1048             Rm_policy...API()\r
1049 \r
1050             if (policy check approves the resource)\r
1051             {\r
1052                 /* call the allocator to allocate the resource */\r
1053                 if (allocator returns resource)\r
1054                 {\r
1055                     /* Populate the transaction with the allocated resources and the result */\r
1056                     transaction->state = approve reason;\r
1057                     return ...\r
1058                 }\r
1059                 else\r
1060                 {\r
1061                     /* allocator ran out of resources, need to contact Server for more\r
1062                      * resources */\r
1063                     Rm_resourcePoolModRequest(...);\r
1064                 }\r
1065             }\r
1066             else if (policy check denies resource)\r
1067             {\r
1068                 /* Policy check denied resource. */\r
1069                 transaction->state= deny reason;\r
1070                 return ...\r
1071             }\r
1072             else if (policy check says forward to Server for validation)\r
1073             {\r
1074                 /* Forward the transaction to the Server */\r
1075                 Rm_transactionForwarder(rmInst, transaction);\r
1076             }\r
1077         }\r
1078 #endif         \r
1079     }\r
1080     else if (rmInst->instType == Rm_instType_SERVER)\r
1081     {\r
1082 #if 0       \r
1083         if (resourceBase is unspecified)\r
1084         {\r
1085            while (policy does not approve)\r
1086            {\r
1087                Rm_policy check get allowed base as starting point for prealloc\r
1088                preallocate resource based on the range and alignment\r
1089                Rm_policy...check\r
1090            }\r
1091         }\r
1092         else\r
1093         {\r
1094             /* Check global policy to see if resource can be allocated. return result\r
1095              * no matter what */\r
1096             Rm_policy...API()\r
1097 \r
1098             if (policy approves)\r
1099             {\r
1100                 /* call allocator to allocate resource */\r
1101             }\r
1102 \r
1103             transaction->state = approve or deny reason;\r
1104             transaction->resourceInfo.base = ...;\r
1105             transaction->resourceInfo.range = ...;\r
1106 \r
1107             /* If source instance name does not match the current instance\r
1108              * name the allocation request came from a Client.  The result\r
1109              * must be sent back to the Client */\r
1110             if (strcmp(transaction->sourceInstName, rmInst->name))\r
1111             {\r
1112                 /* Names don't match.  Send the transaction back to the Client */\r
1113                 Rm_transactionResponder(rmInst, transaction);\r
1114             }\r
1115             else\r
1116             {\r
1117                 /* Resource allocation request originated locally on the active\r
1118                  * instance. Send the response via the service responder. */          \r
1119                 Rm_serviceResponder(rmInst, transaction);                            \r
1120             }\r
1121         }\r
1122 #endif        \r
1123     }   \r
1124 }\r
1125 \r
1126 void Rm_freeHandler (Rm_Inst *rmInst, Rm_Transaction *transaction)\r
1127 {\r
1128     if (rmInst->instType == Rm_instType_CLIENT_DELEGATE)\r
1129     {\r
1130 #if 0        \r
1131         /* Check local policy to see if the request can be satisfied with the\r
1132          * resources stored locally */\r
1133         Rm_policy...API()\r
1134 \r
1135         if (policy check approves the free)\r
1136         {\r
1137             /* call the allocator to free the resource */\r
1138             /* Run a resource pool check to see if the free combined a resource block\r
1139              * that can be returned to the server */\r
1140             if (resource block has been combined)\r
1141             {\r
1142                   /* allocator ran out of resources, need to contact Server for more\r
1143                  * resources */\r
1144                 Rm_resourcePoolModRequest(free pool block to server...);\r
1145             }\r
1146             else\r
1147             {\r
1148                 /* Populate the receipt with the freed resources and the result */\r
1149                 transaction->state = approve reason;\r
1150                 return ...\r
1151             }\r
1152         }\r
1153         else if (policy check denies resource free)\r
1154         {\r
1155             /* Policy check denied resource. */\r
1156             transaction->state = deny reason;\r
1157             return ...\r
1158         }\r
1159         else if (policy check says forward to Server for validation)\r
1160         {\r
1161             /* Forward the transaction to the Server */\r
1162             Rm_transactionForwarder(rmInst, transaction);\r
1163         }\r
1164 #endif         \r
1165     }\r
1166     else if (rmInst->instType == Rm_instType_SERVER)\r
1167     {\r
1168 #if 0        \r
1169         /* Check global policy to see if resource can be freed. return result\r
1170          * no matter what */\r
1171         Rm_policy...API()\r
1172         if (policy approves)\r
1173         {\r
1174             /* call allocator to free resources */\r
1175         }\r
1176             \r
1177         transaction->state = approve or deny reason;\r
1178         transaction->resourceInfo.base = ...;\r
1179         transaction->resourceInfo.range = ...;\r
1180 \r
1181         /* If source instance name does not match the current instance\r
1182          * name the allocation request came from a client.  The result\r
1183          * must be sent back to the Client */\r
1184         if (strcmp(transaction->sourceInstName, rmInst->name))\r
1185         {\r
1186             /* Names don't match.  Send the transaction back to the Client Delegate or Client */\r
1187             Rm_transactionResponder(rmInst, transaction);\r
1188         }\r
1189         else\r
1190         {\r
1191             /* Resource allocation request originated locally on the active\r
1192              * instance. Send the response via the service responder. */\r
1193             Rm_serviceResponder(rmInst, transaction);                            \r
1194         }\r
1195 #endif        \r
1196     }   \r
1197 }\r
1198 \r
1199 /* Function used to forward RM transactions to higher level agents */\r
1200 void Rm_transactionForwarder (Rm_Inst *rmInst, Rm_Transaction *transaction)\r
1201 {\r
1202     Rm_TransportNode *dstTransportNode = NULL;\r
1203     Rm_Packet *rmPkt = NULL;\r
1204 \r
1205     /* Make sure the RM instance has a transport registered with a higher level agent */\r
1206     if (rmInst->registeredWithDelegateOrServer == false)\r
1207     {\r
1208         transaction->state = RM_SERVICE_ERROR_NOT_REGISTERED_WITH_DEL_OR_SERVER;\r
1209         return;\r
1210     }\r
1211 \r
1212     /* Find the transport for the higher level agent.  Check for a connection to a Client Delegate\r
1213      * or a Server.  Clients will be connected to either a Client Delegate or a Server.  Client\r
1214      * Delegates will be connected to a Server. */\r
1215     if (rmInst->instType == Rm_instType_CLIENT)\r
1216     {\r
1217         dstTransportNode = Rm_transportNodeFindRemoteInstType(rmInst, Rm_instType_CLIENT_DELEGATE);\r
1218     } \r
1219     else if (rmInst->instType == Rm_instType_CLIENT_DELEGATE)\r
1220     {\r
1221         dstTransportNode = Rm_transportNodeFindRemoteInstType(rmInst, Rm_instType_SERVER);\r
1222     }\r
1223 \r
1224     /* Create a RM packet using the service information */\r
1225     switch (transaction->type)\r
1226     {\r
1227         case Rm_service_RESOURCE_ALLOCATE:\r
1228         case Rm_service_RESOURCE_BLOCK_ALLOCATE:\r
1229         case Rm_service_RESOURCE_ALLOCATE_BY_NAME:\r
1230         case Rm_service_RESOURCE_FREE:\r
1231         case Rm_service_RESOURCE_BLOCK_FREE:\r
1232         case Rm_service_RESOURCE_FREE_BY_NAME:\r
1233             rmPkt = Rm_transportCreateResourceReqPkt(rmInst, dstTransportNode, \r
1234                                                      transaction);\r
1235             break;\r
1236         case Rm_service_RESOURCE_MAP_TO_NAME:\r
1237         case Rm_service_RESOURCE_UNMAP_NAME:\r
1238             rmPkt = Rm_transportCreateNsRequestPkt(rmInst, dstTransportNode,\r
1239                                                    transaction);\r
1240             break;\r
1241         default:\r
1242             /* Invalid service type.  Flag the error and return */\r
1243             transaction->state = RM_SERVICE_ERROR_INVALID_SERVICE_TYPE;\r
1244             break;\r
1245     }\r
1246 \r
1247     if (transaction->state <= RM_SERVICE_ERROR_BASE)\r
1248     {\r
1249         /* Return immediately because an error occurred allocating the packet */\r
1250         return;\r
1251     }\r
1252 \r
1253     /* Send the RM packet to the application transport */\r
1254     if (rmInst->transport.rmSend((Rm_TransportHandle) dstTransportNode, rmPkt) < RM_TRANSPORT_SUCCESSFUL)\r
1255     {\r
1256         /* Negative value returned by transport send.  An error occurred\r
1257          * in the transport while attempting to send the packet.*/\r
1258         transaction->state = RM_SERVICE_ERROR_TRANPSPORT_SEND_ERROR;\r
1259         /* Clean up the packet */\r
1260         if (rmInst->transport.rmFreePkt((Rm_TransportHandle) dstTransportNode, rmPkt))\r
1261         {\r
1262             /* Non-NULL value returned by transport packet free. Flag the\r
1263              * error */\r
1264             transaction->state = RM_SERVICE_ERROR_TRANSPORT_FREE_PKT_ERROR;\r
1265         }\r
1266         return;\r
1267     }\r
1268 \r
1269     /* Transaction is not deleted because it is awaiting a response from the higher level\r
1270      * RM instance */\r
1271 }\r
1272 \r
1273 void Rm_transactionProcessor (Rm_Inst *rmInst, Rm_Transaction *transaction)\r
1274 {\r
1275     /* Handle auto-forwarded transactions.  These transactions include:\r
1276      * - All request transactions received on Clients are forwarded to the Client Delegate\r
1277      * - NameServer requests received on the Client Delegate are forwarded to the Server */\r
1278     if ((rmInst->instType == Rm_instType_CLIENT) ||\r
1279         ((rmInst->instType == Rm_instType_CLIENT_DELEGATE) &&\r
1280          (transaction->type == Rm_service_RESOURCE_MAP_TO_NAME) ||\r
1281          (transaction->type == Rm_service_RESOURCE_UNMAP_NAME)))\r
1282     {\r
1283         /* Check if the transaction is a transaction that received a response to its\r
1284          * request. */\r
1285         if (transaction->state != RM_SERVICE_PROCESSING)\r
1286         {\r
1287 \r
1288             /* A transaction has received a response. Send the response to either the \r
1289              * transaction or service responder based on the source instance */\r
1290             if (strcmp(transaction->sourceInstName, rmInst->name))\r
1291             {\r
1292                 /* Transaction originated from another instance.  Use the \r
1293                  * transaction responder to send the result to the source instance.  This\r
1294                  * is not possible on RM Clients since they can't forward RM services */\r
1295                 Rm_transactionResponder(rmInst, transaction);\r
1296             }\r
1297             else\r
1298             {\r
1299                 /* Transaction originated on this instance.  Send to the\r
1300                  * service responder */\r
1301                 Rm_serviceResponder(rmInst, transaction);\r
1302             }\r
1303         }\r
1304         else\r
1305         {\r
1306             /* This is a new transaction that must be forwarded to a higher level RM instance. */\r
1307             Rm_transactionForwarder(rmInst, transaction);\r
1308         }\r
1309     }\r
1310     else\r
1311     {\r
1312         /* Client Delegate and Server transaction processors. */\r
1313         switch (transaction->type)\r
1314         {\r
1315             case Rm_service_RESOURCE_ALLOCATE:\r
1316             case Rm_service_RESOURCE_BLOCK_ALLOCATE:\r
1317             case Rm_service_RESOURCE_ALLOCATE_BY_NAME:\r
1318             case Rm_service_RESOURCE_FREE:\r
1319             case Rm_service_RESOURCE_BLOCK_FREE:\r
1320             case Rm_service_RESOURCE_FREE_BY_NAME:                \r
1321                 /* Check if the transaction is fulfilled request */\r
1322                 if (transaction->state != RM_SERVICE_PROCESSING)\r
1323                 {\r
1324                     /* If source instance name does not match the current instance\r
1325                      * name the allocation request came from a client.  The result\r
1326                      * must be sent back to the Client */\r
1327                     if (strcmp(transaction->sourceInstName, rmInst->name))\r
1328                     {\r
1329                         Rm_transactionResponder(rmInst, transaction);\r
1330                     }\r
1331                     else\r
1332                     {\r
1333                         /* Resource allocation request originated locally.  Send the response\r
1334                          * via the service responder. */\r
1335                         Rm_serviceResponder(rmInst, transaction);      \r
1336                     }\r
1337                 }\r
1338                 else\r
1339                 {\r
1340                     /* This is a new transaction request originating from an RM instance with fewer\r
1341                      * allocate/free privileges.  Run the allocation or free handler to see if the resource\r
1342                      * request can be handled locally or if it needs to be forwarded to a higher level\r
1343                      * agent */\r
1344                     if ((transaction->type == Rm_service_RESOURCE_ALLOCATE) ||\r
1345                         (transaction->type == Rm_service_RESOURCE_BLOCK_ALLOCATE) ||\r
1346                         (transaction->type == Rm_service_RESOURCE_ALLOCATE_BY_NAME))\r
1347                     {\r
1348                         Rm_allocationHandler(rmInst, transaction);\r
1349                     }\r
1350                     else\r
1351                     {\r
1352                         Rm_freeHandler(rmInst, transaction);\r
1353                     }\r
1354                 }\r
1355                 break;\r
1356             case Rm_service_RESOURCE_MAP_TO_NAME:\r
1357             case Rm_service_RESOURCE_UNMAP_NAME:                \r
1358                 /* Server is the only RM instance capable of adding NameServer objects */\r
1359                 if (rmInst->instType == Rm_instType_SERVER)\r
1360                 {\r
1361                     if (transaction->type == Rm_service_RESOURCE_MAP_TO_NAME)\r
1362                     {\r
1363                         /* Create a new NameServer object with the request transaction information.\r
1364                          * Transaction will contain the state result of the NameServer addition. */\r
1365                         if (Rm_nsAddObject(rmInst, transaction->resourceInfo.nsName,\r
1366                                            transaction->resourceInfo.base) == RM_NS_ACTION_APPROVED)\r
1367                         {\r
1368                             transaction->state = RM_SERVICE_APPROVED_AND_COMPLETED;\r
1369                         }\r
1370                         else\r
1371                         {\r
1372                             /* TEMP: UPDATE THIS STATE VALUE */\r
1373                             transaction->state = RM_SERVICE_DENIED_BEGIN;\r
1374                         }\r
1375                     }\r
1376                     else\r
1377                     {\r
1378                         /* Delete an existing NameServer object with the request transaction information\r
1379                          * Transaction will contain the state result of the NameServer addition. */\r
1380                         if (Rm_nsDeleteObject(rmInst, transaction->resourceInfo.nsName) == \r
1381                             RM_NS_ACTION_APPROVED)\r
1382                         {\r
1383                             transaction->state = RM_SERVICE_APPROVED_AND_COMPLETED;\r
1384                         }\r
1385                         else\r
1386                         {\r
1387                             /* TEMP: UPDATE THIS STATE VALUE */\r
1388                             transaction->state = RM_SERVICE_DENIED_BEGIN;\r
1389                         }\r
1390                     }\r
1391 \r
1392                     /* If source instance name does not match the local instance\r
1393                      * name the NameServer request came from a Client or Client Delegate.  The \r
1394                      * result must be sent back to the Client or Client Delegate.  Just return if it does\r
1395                      * match since the NameServer transaction result can be returned immediately by the\r
1396                      * Rm_serviceHandler. */\r
1397                     if (strcmp(transaction->sourceInstName, rmInst->name))\r
1398                     {\r
1399                         Rm_transactionResponder(rmInst, transaction);\r
1400                     }\r
1401                 }\r
1402                 else\r
1403                 {\r
1404                     transaction->state = RM_SERVICE_ERROR_NAMESERVER_OBJECT_MOD_ON_INVALID_INSTANCE;\r
1405                 }\r
1406                 break;\r
1407         }\r
1408     }\r
1409 }\r
1410 \r
1411 int32_t Rm_initializeAllocators(Rm_Inst *rmInst, void *globalResourceDtb)\r
1412 {\r
1413     int32_t nodeOffset = RM_DTB_UTIL_STARTING_NODE_OFFSET;\r
1414     int32_t startDepth = RM_DTB_UTIL_STARTING_DEPTH;\r
1415     int32_t result = RM_DTB_UTIL_RESULT_OKAY;\r
1416 \r
1417     /* Recursively parse the Global Resource List, creating an allocator for\r
1418      * each resource as specified in the node */\r
1419     result = Rm_parseResourceNode(rmInst, globalResourceDtb, nodeOffset, startDepth);\r
1420 \r
1421     return(result);\r
1422 }\r
1423          \r
1424 int32_t Rm_reserveLinuxResources(Rm_Inst *rmInst, void *linuxResourceDtb)\r
1425 {\r
1426     return(0);\r
1427 \r
1428 }\r
1429 \r
1430 /**********************************************************************\r
1431  ********************** Application visible APIs **********************\r
1432  **********************************************************************/\r
1433 \r
1434 Rm_Handle Rm_init(Rm_InitCfg *initCfg)\r
1435 {\r
1436     Rm_Inst *rmInst;\r
1437     void *globalResourceDtb = NULL;\r
1438     void *linuxResourceDtb = NULL;\r
1439 \r
1440     /* Instance creation checks.  Add one to strlen calculation for null character */\r
1441     if ((strlen(initCfg->instName) + 1) > RM_INSTANCE_NAME_MAX_CHARS)\r
1442     {\r
1443         /* Failure: Instance name is too big */\r
1444         return (NULL);\r
1445     }\r
1446     \r
1447     /* Get memory for RM instance from local memory */\r
1448     rmInst = Rm_osalMalloc (sizeof(Rm_Inst));\r
1449     /* Populate instance based on input parameters */\r
1450     strcpy (&rmInst->name[0], initCfg->instName);\r
1451     rmInst->instType = initCfg->instType;\r
1452     rmInst->registeredWithDelegateOrServer = false;\r
1453     rmInst->policyDtb = NULL;\r
1454 \r
1455     /* Initialize the transport routing map linked list pointer to NULL.  The linked list\r
1456      * nodes will be created when the application registers transports */\r
1457     rmInst->routeMap = NULL;\r
1458 \r
1459     /* Initialize the allocators linked list pointer to NULL.  The linked list nodes will\r
1460      * be created on the Server instance when the application reads in the resource list.\r
1461      * Nodes will also be created on Client Delegates when blocks of resources are requested\r
1462      * for allocation to clients. */\r
1463     rmInst->allocators = NULL;\r
1464 \r
1465     /* Initialize the transaction queue elements. */\r
1466     rmInst->transactionSeqNum = Rm_transactionInitSequenceNum();\r
1467     rmInst->transactionQueue= NULL;\r
1468 \r
1469     /* RM Server specific actions */\r
1470     if (rmInst->instType == Rm_instType_SERVER)\r
1471     {\r
1472         /* Open the ResourceList file and provide it to the resource initializer. */\r
1473         if (initCfg->globalResourceList)\r
1474         {\r
1475             globalResourceDtb = initCfg->globalResourceList;\r
1476             fdt_open_into(globalResourceDtb, globalResourceDtb, fdt_totalsize(globalResourceDtb));            \r
1477             Rm_initializeAllocators(rmInst, globalResourceDtb);\r
1478         }\r
1479 \r
1480         /* Parse the Linux DTB for the resources reserved by the Linux kernel.  These resources\r
1481          * will be marked as used in the resource allocators. */\r
1482         if (initCfg->linuxDtb)\r
1483         {\r
1484             linuxResourceDtb = initCfg->linuxDtb;\r
1485             fdt_open_into(linuxResourceDtb, linuxResourceDtb, fdt_totalsize(linuxResourceDtb));            \r
1486             Rm_reserveLinuxResources(rmInst, linuxResourceDtb);\r
1487         }\r
1488     }\r
1489 \r
1490     /* Instance startup policies are only used for Servers and Client Delegates */\r
1491     if (rmInst->instType != Rm_instType_CLIENT)\r
1492     {\r
1493         /* Open the instance's policy and store it */\r
1494         if (initCfg->startupPolicy)\r
1495         {\r
1496             rmInst->policyDtb = initCfg->startupPolicy;\r
1497             fdt_open_into(rmInst->policyDtb, rmInst->policyDtb, fdt_totalsize(rmInst->policyDtb));  \r
1498         }\r
1499 \r
1500         /* Store policy via policy APIs ... */\r
1501     }\r
1502 \r
1503     /* Return the RM Handle */\r
1504     return ((Rm_Handle) rmInst);\r
1505 }\r
1506 \r
1507 uint32_t Rm_getVersion (void)\r
1508 {\r
1509     return RM_VERSION_ID;\r
1510 }\r
1511 \r
1512 \r
1513 const char* Rm_getVersionStr (void)\r
1514 {\r
1515     return rmVersionStr;\r
1516 }\r
1517 \r
1518 /**\r
1519 @}\r
1520 */\r