d805116b546a1860f8b04353a81435434f2a347c
[ipc/ipcdev.git] / qnx / src / ipc3x_dev / ti / syslink / resources / RscTable.c
1 /*
2  *  @file       RscTable.c
3  *
4  *  @brief      Resource Table parser.
5  *
6  *  ============================================================================
7  *
8  *  Copyright (c) 2012-2014, Texas Instruments Incorporated
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *
14  *  *  Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions and the following disclaimer.
16  *
17  *  *  Redistributions in binary form must reproduce the above copyright
18  *     notice, this list of conditions and the following disclaimer in the
19  *     documentation and/or other materials provided with the distribution.
20  *
21  *  *  Neither the name of Texas Instruments Incorporated nor the names of
22  *     its contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27  *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35  *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *  Contact information for paper mail:
37  *  Texas Instruments
38  *  Post Office Box 655303
39  *  Dallas, Texas 75265
40  *  Contact information:
41  *  http://www-k.ext.ti.com/sc/technical-support/product-information-centers.htm?
42  *  DCMP=TIHomeTracking&HQS=Other+OT+home_d_contact
43  *  ============================================================================
44  *
45  */
48 #if defined (__cplusplus)
49 extern "C" {
50 #endif
52 #include <ti/syslink/Std.h>
53 #include <ti/syslink/utils/List.h>
54 #include <ti/syslink/utils/Memory.h>
55 #include <ti/syslink/utils/Trace.h>
56 #include <RscTable.h>
57 #include <OsalKfile.h>
58 #include <_MultiProc.h>
59 #include <dload_api.h>
60 #include <ti/syslink/ProcMgr.h>
61 #include <Bitops.h>
62 #include <_MessageQCopyDefs.h>
63 #include <ProcDefs.h>
64 #include <sys/mman.h>
66 /* =============================================================================
67  *  Macros and types
68  * =============================================================================
69  */
70 /*
71  * ZEROINIT_CHUNKS - Set to 1 to zero-init chunk allocations for
72  *                   dynamically allocated remote core memory carveout
73  *                   sections. Default is 0 to achieve better startup
74  *                   performance.
75  */
76 #define ZEROINIT_CHUNKS 0
78 #define RSC_TABLE_STRING ".resource_table"
79 #define DDR_MEM 0x80000000
81 typedef struct Carveout_Elem_tag {
82     List_Elem  elem;
83     UInt32     addr;
84     UInt32     size;
85 } Carveout_Elem;
87 /*!
88  *  @brief  RscTable Header
89  */
90 typedef struct RscTable_Header_tag {
91     UInt32 ver;
92     UInt32 num;
93     UInt32 reserved[2];
94     UInt32 offset[];
95 } RscTable_Header;
97 /*!
98  *  @brief  RscTable Entry
99  */
100 typedef struct RscTable_MemEntry_tag {
101     UInt32 type;
102     UInt32 da;       /* Device Virtual Address */
103     UInt32 pa;       /* Physical Address */
104     UInt32 len;
105     UInt32 flags;
106     UInt32 reserved;
107     Char   name[32];
108 } RscTable_MemEntry;
110 /*!
111  *  @brief  RscTable Module state object
112  */
113 typedef struct RscTable_ModuleObject_tag {
114     RscTable_Handle       handles [MultiProc_MAXPROCESSORS];
115     /*!< Array of Handles of RscTable instances */
116 } RscTable_ModuleObject;
118 /*!
119  *  @brief  RscTable instance object
120  */
121 struct RscTable_Object_tag {
122     UInt16                 procId;
123     /*!< Processor ID associated with this RscTable. */
124     Char *                 fileName;
125     /*!< File name associated with this RscTable. */
126     Void *                 rscTable;
127     /*!< Pointer to the resource table. */
128     UInt32                 rscTableLen;
129     /*!< Resource table length. */
130     UInt32                 rscTableDA;
131     /*!< Resource table device address. */
132     SysLink_MemEntry       memEntries[SYSLINK_MAX_MEMENTRIES];
133     /*!< Memory Entries for the remote processor. */
134     UInt32                 numMemEntries;
135     /*!< Number of Memory Entries in the memEntries array. */
136     struct fw_rsc_vdev_vring * vrings;
137     /*!< Vring info for the remote processor. */
138     UInt32                 numVrings;
139     /*!< Number of vrings. */
140     UInt32                 vringPa;
141     /*!< PAddr of vrings space. */
142     UInt32                 vringBufsPa;
143     /*!< PAddr of vring bufs space. */
144     struct fw_rsc_trace    trace;
145     /*!< Trace info. */
146     List_Handle            chunks;
147     /*!< Allocated Chunks. */
148 } ;
150 /* Defines the RscTable_Object type. */
151 typedef struct RscTable_Object_tag RscTable_Object;
153 /* =============================================================================
154  *  Globals
155  * =============================================================================
156  */
157 /*!
158  *  @var    RscTable_state
159  *
160  *  @brief  RscTable state object variable
161  */
162 #if !defined(SYSLINK_BUILD_DEBUG)
163 static
164 #endif /* if !defined(SYSLINK_BUILD_DEBUG) */
165 RscTable_ModuleObject RscTable_state;
168 /* =============================================================================
169  *  APIs
170  * =============================================================================
171  */
173 RscTable_Handle
174 RscTable_alloc (Char * fileName, UInt16 procId)
176     Int status = 0;
177     RscTable_Object * obj = NULL;
178     OsalKfile_Handle fileDesc  = NULL;
179     UInt32 res_offs = 0, res_addr = 0, res_size = 0;
180     List_Params params;
182     // Check if this one is already registered
183     if (RscTable_state.handles[procId]) {
184         /* Not setting status here since this function does not return status.*/
185         GT_setFailureReason (curTrace,
186                              GT_4CLASS,
187                              "RscTable_alloc",
188                              RSCTABLE_E_INVALIDARG,
189                              "Invalid procId specified");
190         return NULL;
191     }
193     // Find the resource table in the file
194     status = OsalKfile_open (fileName, "r", &fileDesc);
195     if (status < 0) {
196         /* Not setting status here since this function does not return status.*/
197         GT_setFailureReason (curTrace,
198                              GT_4CLASS,
199                              "RscTable_alloc",
200                              RSCTABLE_E_INVALIDARG,
201                              "Invalid fileDesc specified");
202         return NULL;
203     }
205     status = DLOAD_get_section_offset((LOADER_FILE_DESC *)fileDesc,
206                                       RSC_TABLE_STRING, &res_offs, &res_size,
207                                       &res_addr);
208     if (status >= 0) {
209         obj = Memory_calloc(NULL, sizeof (RscTable_Object), 0, NULL);
210         if (obj != NULL) {
211             // Allocate memory to hold the table
212             obj->rscTable = Memory_alloc(NULL, res_size, 0, NULL);
213             if (obj->rscTable == NULL) {
214                 /* Not setting status here since this function does not return
215                  * status.*/
216                 GT_setFailureReason (curTrace,
217                                      GT_4CLASS,
218                                      "RscTable_alloc",
219                                      RSCTABLE_E_MEMORY,
220                                      "Unable to allocate rscTable");
221                 Memory_free(NULL, obj, sizeof (RscTable_Object));
222                 obj = NULL;
223             }
224             else {
225                 obj->rscTableLen = res_size;
226                 status = OsalKfile_seek(fileDesc, res_offs,
227                                         OsalKfile_Pos_SeekSet);
228                 if (status < 0) {
229                     /* Not setting status here since this function does not
230                      * return status.*/
231                     GT_setFailureReason (curTrace,
232                                          GT_4CLASS,
233                                          "RscTable_alloc",
234                                          RSCTABLE_E_FAIL,
235                                          "Unable to seek to rsctable in file");
236                     Memory_free(NULL, obj->rscTable, res_size);
237                     Memory_free(NULL, obj, sizeof (RscTable_Object));
238                     obj = NULL;
239                 }
240                 else {
241                     // Copy the table to the allocated memory
242                     status = OsalKfile_read(fileDesc, (char *)obj->rscTable, 1,
243                                             res_size);
244                     if (status < 0) {
245                         /* Not setting status here since this function does not
246                          * return status.*/
247                         GT_setFailureReason (curTrace,
248                                              GT_4CLASS,
249                                              "RscTable_alloc",
250                                              RSCTABLE_E_FAIL,
251                                              "Unable to read rsctable in file");
252                         Memory_free(NULL, obj->rscTable, res_size);
253                         Memory_free(NULL, obj, sizeof (RscTable_Object));
254                         obj = NULL;
255                     }
256                     else {
257                         obj->rscTableDA = res_addr;
258                         List_Params_init(&params);
259                         obj->chunks = List_create(&params);
260                         if (obj->chunks == NULL) {
261                             /* Not setting status here since this function does
262                              * not return status.*/
263                             GT_setFailureReason (curTrace,
264                                                  GT_4CLASS,
265                                                  "RscTable_alloc",
266                                                  RSCTABLE_E_FAIL,
267                                                  "Unable to create list");
268                             Memory_free(NULL, obj->rscTable, res_size);
269                             Memory_free(NULL, obj, sizeof (RscTable_Object));
270                             obj = NULL;
271                         }
272                     }
273                 }
274             }
275         }
276         else {
277             /* Not setting status here since this function does not return
278              * status.*/
279             GT_setFailureReason (curTrace,
280                                  GT_4CLASS,
281                                  "RscTable_alloc",
282                                  RSCTABLE_E_MEMORY,
283                                  "Unable to allocate obj");
284         }
285     }
286     else {
287         /* Not setting status here since this function does not return status.*/
288         GT_setFailureReason (curTrace,
289                              GT_4CLASS,
290                              "RscTable_alloc",
291                              RSCTABLE_E_FAIL,
292                              "Unable to find resource table in file");
293     }
295     OsalKfile_close(&fileDesc);
297     // If successful, save coreId-handle pair for later access
298     if (obj) {
299         obj->procId = procId;
300         RscTable_state.handles[procId] = (RscTable_Handle)obj;
301     }
303     return (RscTable_Handle)obj;
306 // allocate any addr
307 Int Chunk_allocate (RscTable_Object *obj, UInt32 size, UInt32 * pa)
309     Int status = 0;
310     List_Elem * elem = NULL;
311     Void * da;
312     long long paddr;
313     UInt32 len;
315     if (!pa || !obj || !obj->chunks) {
316         status = RSCTABLE_E_INVALIDARG;
317         GT_setFailureReason (curTrace,
318                              GT_4CLASS,
319                              "Chunk_allocate",
320                              status,
321                              "Invalid arg passed");
322         GT_3trace(curTrace, GT_4CLASS, "obj [0x%x] obj->chunks [0x%x] pa [0x%x]", obj, obj ? obj->chunks : NULL, pa);
323     }
324     else {
325         // first try to allocate contiguous mem
326         da = mmap64(NULL, size,
327                     PROT_NOCACHE | PROT_READ | PROT_WRITE,
328 #if ZEROINIT_CHUNKS
329                     MAP_ANON | MAP_PHYS | MAP_SHARED,
330 #else
331                     MAP_ANON | MAP_PHYS | MAP_SHARED | MAP_NOINIT,
332 #endif
333                     NOFD,
334                     0);
335         if (da == MAP_FAILED) {
336             // TODO: try to allocate non-contigous mem then get the pages
337             status = RSCTABLE_E_MEMORY;
338             GT_setFailureReason (curTrace,
339                                  GT_4CLASS,
340                                  "Chunk_allocate",
341                                  status,
342                                  "Unable to allocate chunk");
343         }
344         else {
345             /* Make sure the memory is contiguous */
346             status = mem_offset64(da, NOFD, size, &paddr, &len);
347             if (status || (len != size)) {
348                 munmap(da, size);
349                 status = RSCTABLE_E_MEMORY;
350                 GT_setFailureReason (curTrace,
351                                      GT_4CLASS,
352                                      "Chunk_allocate",
353                                      status,
354                                      "mem_offset64 failed");
355             }
356             else {
357                 *pa = (UInt32)paddr;
358                 // save the memory so that it can be freed later
359                 elem = Memory_alloc(NULL, sizeof(Carveout_Elem), 0, NULL);
360                 if (elem == NULL) {
361                     munmap(da, size);
362                     status = RSCTABLE_E_MEMORY;
363                     GT_setFailureReason (curTrace,
364                                          GT_4CLASS,
365                                          "Chunk_allocate",
366                                          status,
367                                          "Memory_alloc failed");
368                 }
369                 else {
370                     ((Carveout_Elem *)elem)->addr = (UInt32)da;
371                     ((Carveout_Elem *)elem)->size = size;
372                     List_put(obj->chunks, elem);
373                 }
374             }
375         }
376     }
377     return status;
380 Int
381 RscTable_process (UInt16 procId, Bool tryAlloc, UInt32 * numBlocks)
383     Int status = 0;
384     Int ret = 0;
385     RscTable_Object * obj = NULL;
386     RscTable_Header * table = NULL;
387     UInt i = 0, j = 0;
388     UInt dmem_num = 0;
389 #if !ZEROINIT_CHUNKS
390     UInt32 vringVA = 0, vringSize = 0;
391 #endif
393     // Find the table for this coreId, if not found, return an error
394     if (procId >= MultiProc_MAXPROCESSORS || !RscTable_state.handles[procId]) {
395         status = RSCTABLE_E_INVALIDARG;
396         GT_setFailureReason (curTrace,
397                              GT_4CLASS,
398                              "RscTable_process",
399                              status,
400                              "Invalid procId specified");
401         return status;
402     }
404     obj = (RscTable_Object *)RscTable_state.handles[procId];
405     table = (RscTable_Header *)obj->rscTable;
407     obj->numMemEntries = 0;
409     // TODO: Check the version
410     printf("RscTable_process: RscTable version is [%d]\n", table->ver);
412     // Process the table for this core, allocating carveout memory if necessary
413     for (i = 0; i < table->num && !ret; i++) {
414         RscTable_MemEntry * entry =
415                 (RscTable_MemEntry *)((UInt32)table + table->offset[i]);
416         switch (entry->type) {
417             case TYPE_CARVEOUT :
418             {
419                 // TODO: need to allocate this mem from carveout
420                 struct fw_rsc_carveout * cout = (struct fw_rsc_carveout *)entry;
421                 UInt32 pa = 0;
422                 if (cout->pa == 0) {
423                     ret = Chunk_allocate (obj, cout->len, &pa);
424                 }
425                 if (!ret) {
426                     cout->pa = pa;
427                     if (obj->numMemEntries == SYSLINK_MAX_MEMENTRIES) {
428                         ret = -1;
429                     }
430                     else {
431                         obj->memEntries[obj->numMemEntries].slaveVirtAddr =
432                                 cout->da;
433                         obj->memEntries[obj->numMemEntries].masterPhysAddr =
434                                 cout->pa;
435                         obj->memEntries[obj->numMemEntries].size = cout->len;
436                         obj->memEntries[obj->numMemEntries].map = TRUE;
437                         obj->memEntries[obj->numMemEntries].mapMask =
438                             ProcMgr_SLAVEVIRT;
439                         obj->memEntries[obj->numMemEntries].isCached = FALSE;
440                         obj->memEntries[obj->numMemEntries].isValid = TRUE;
441                         obj->numMemEntries++;
442                     }
443                 }
444                 printf ("RscTable_process: carveout [%s] @ da [0x%08x] pa [0x%08x] len [0x%x]\n", cout->name, cout->da, cout->pa, cout->len);
445                 break;
446             }
447             case TYPE_DEVMEM :
448             {
449                 // only care about mem in DDR for now
450                 struct fw_rsc_devmem * dmem = (struct fw_rsc_devmem *)entry;
451                 if (dmem->pa >= DDR_MEM) {
452                     // HACK: treat vring mem specially, vring is always the
453                     //       first devmem entry, may change in future
454                     if (dmem_num++ == 0) {
455                         // memory should already be reserved for vring
456                         if (obj->vringPa == 0) {
457                             // vdev must have been defined first
458                             GT_setFailureReason (curTrace,
459                                                  GT_4CLASS,
460                                                  "RscTable_process",
461                                                  status,
462                                                  "Vring Must be Defined First");
464                             ret = -1;
465                         }
466                         else if (obj->vringPa != dmem->pa && (!tryAlloc)) {
467                             // defined pa does not match allocated pa, and
468                             // either the mmu is disabled or the platform has
469                             // not given permission to allocate on our own
470                             ret = -1;
471                             GT_setFailureReason (curTrace,
472                                                  GT_4CLASS,
473                                                  "RscTable_process",
474                                                  status,
475                                                  "Vring PA Mis-match");
477                             GT_2trace (curTrace, GT_4CLASS,
478                                        "vringPa is 0x%x, dmem->pa is 0x%x",
479                                        obj->vringPa, dmem->pa);
480                         }
481                         else {
482                             dmem->pa = obj->vringPa;
483                         }
484                     }
485                 }
486                 if (!ret) {
487                     if (obj->numMemEntries == SYSLINK_MAX_MEMENTRIES) {
488                         ret = -1;
489                     }
490                     else {
491                         obj->memEntries[obj->numMemEntries].slaveVirtAddr =
492                                 dmem->da;
493                         obj->memEntries[obj->numMemEntries].masterPhysAddr =
494                                 dmem->pa;
495                         obj->memEntries[obj->numMemEntries].size = dmem->len;
496                         obj->memEntries[obj->numMemEntries].map = TRUE;
497                         obj->memEntries[obj->numMemEntries].mapMask =
498                             ProcMgr_SLAVEVIRT;
499                         obj->memEntries[obj->numMemEntries].isCached = FALSE;
500                         obj->memEntries[obj->numMemEntries].isValid = TRUE;
501                         obj->numMemEntries++;
502                     }
503                 }
505                 // TODO: need to make sure this mem exists/is set aside, or allocate it
506                 printf ("RscTable_process: devmem [%s] @ da [0x%08x] pa [0x%08x] len [0x%x]\n", dmem->name, dmem->da, dmem->pa, dmem->len);
507                 break;
508             }
509             case TYPE_TRACE :
510             {
511                 // TODO: save da for future use
512                 struct fw_rsc_trace * trace = (struct fw_rsc_trace *)entry;
513                 Memory_copy(&obj->trace, trace, sizeof(*trace));
514                 printf ("RscTable_process: trace [%s] @ da [0x%08x] len [0x%x]\n", trace->name, trace->da, trace->len);
515                 break;
516             }
517             case TYPE_VDEV :
518             {
519                 // TODO: save vring info for future use
520                 struct fw_rsc_vdev *vdev = (struct fw_rsc_vdev *)entry;
521                 struct fw_rsc_vdev_vring * vring = NULL;
522                 UInt32 vr_size = 0;
523                 UInt32 vr_bufs_size = 0;
524                 UInt32 pa = 0;
525                 obj->numVrings = vdev->num_of_vrings;
526                 obj->vrings = Memory_alloc(NULL,
527                                            sizeof (*vring) * obj->numVrings, 0,
528                                            NULL);
529                 if (!obj->vrings)
530                     ret = -1;
531                 for (j = 0; j < vdev->num_of_vrings && !ret; j++) {
532                     vring = (struct fw_rsc_vdev_vring *)
533                                 ((UInt32)vdev + sizeof(*vdev) +
534                                  (sizeof(*vring) * j));
535                     /*
536                      * Making a copy of original vring, given obj->vrings[j].da
537                      * is used as slave virtual address in RscTable_getInfo(),
538                      * and that is what the original vring definition contains.
539                      */
540                     Memory_copy (&obj->vrings[j], vring, sizeof (*vring));
541                     printf ("RscTable_process: vring [%d] @ [0x%08x]\n",
542                             vring->num, vring->da);
543                     // Hack, we know this is the size alignment
544                     if (vring->num != MessageQCopy_NUMBUFS ||
545                         vring->align != MessageQCopy_VRINGALIGN) {
546                         printf ("RscTable_process: warning size mis-match!\n");
547                         ret = -1;
548                     }
549                     vr_size += ROUND_UP(MessageQCopy_RINGSIZE, 0x4000);
550                     vr_bufs_size += (vring->num) * RPMSG_BUF_SIZE;
551                 }
552                 if (!ret) {
553                     // HACK: round up to multiple of 1MB, because we know this
554                     //       is the size of the remote entry
555                     ret = Chunk_allocate(obj,
556                                  ROUND_UP(vr_size + vr_bufs_size, 0x100000),
557                                  &pa);
558                 }
559                 else if (obj->vrings) {
560                     Memory_free (NULL, obj->vrings,
561                                  sizeof(*vring) * obj->numVrings);
562                     obj->vrings = NULL;
563                 }
565                 if (!ret) {
566 #if !ZEROINIT_CHUNKS
567                     /* Map the phys mem to local */
568                     vringSize = vr_size + vr_bufs_size;
569                     vringVA = (UInt32)mmap_device_io(vringSize, pa);
570                     if (vringVA != MAP_DEVICE_FAILED) {
571                         /* Zero-init the vring */
572                         Memory_set((Ptr)vringVA, 0, vringSize);
573                         munmap_device_io(vringVA, vringSize);
574                     }
575                     else {
576                         GT_0trace(curTrace, GT_4CLASS,
577                                   "RscTable_alloc: "
578                                   "Warning - Unable to zero-init vring mem");
579                     }
580 #endif
582                     obj->vringPa = pa;
583                     obj->vringBufsPa = pa + vr_size;
585                     /*
586                      * Override the vring 'da' field in resource table.
587                      * This allows the slave to look it up when creating
588                      * the VirtQueues
589                      */
590                     for (j = 0; j < vdev->num_of_vrings; j++) {
591                         vring = (struct fw_rsc_vdev_vring *)
592                                 ((UInt32)vdev + sizeof(*vdev) +
593                                  (sizeof(*vring) * j));
594                         vring->da = obj->vringPa + (j *
595                             vr_size / vdev->num_of_vrings);
596                     }
597                 }
598                 break;
599             }
600             case TYPE_CRASHDUMP :
601                 // what to do with this?
602                 break;
603             default :
604                 break;
605         }
606     }
608     if (ret) {
609         status = RSCTABLE_E_FAIL;
610         GT_setFailureReason (curTrace,
611                              GT_4CLASS,
612                              "RscTable_process",
613                              status,
614                              "Failure happened in processing table");
615         if (obj->vrings)
616             Memory_free(NULL, obj->vrings,
617                         sizeof(*obj->vrings) * obj->numVrings);
618         obj->vrings = NULL;
619         obj->numMemEntries = 0;
620     }
621     *numBlocks = obj->numMemEntries;
623     return status;
626 Int
627 RscTable_getMemEntries (UInt16 procId, SysLink_MemEntry *memEntries,
628                         UInt32 * numMemEntries)
630     Int status = 0;
631     RscTable_Object *obj = NULL;
633     // Find the table for this procId, if not found, return an error
634     if (procId >= MultiProc_MAXPROCESSORS || !RscTable_state.handles[procId]) {
635         status = RSCTABLE_E_INVALIDARG;
636         GT_setFailureReason (curTrace,
637                              GT_4CLASS,
638                              "RscTable_getMemEntries",
639                              status,
640                              "Invalid ProcId specified");
641         return status;
642     }
644     obj = (RscTable_Object *)RscTable_state.handles[procId];
646     if (obj->memEntries && obj->numMemEntries &&
647         *numMemEntries >= obj->numMemEntries) {
648         *numMemEntries = obj->numMemEntries;
649         Memory_copy(memEntries, obj->memEntries,
650                     sizeof(*memEntries) * obj->numMemEntries);
651     }
652     else {
653         status = RSCTABLE_E_INVALIDARG;
654         GT_setFailureReason (curTrace,
655                              GT_4CLASS,
656                              "RscTable_getMemEntries",
657                              status,
658                              "Invalid num of memEntries or memEntries is NULL");
659     }
661     return status;
664 Int
665 RscTable_getInfo (UInt16 procId, UInt32 type, UInt32 extra, UInt32 * da,
666                   UInt32 *pa, UInt32 * len)
668     Int status = 0;
669     RscTable_Object *obj = NULL;
671     // Find the table for this procId, if not found, return an error
672     if (procId >= MultiProc_MAXPROCESSORS || !RscTable_state.handles[procId]) {
673         status = RSCTABLE_E_INVALIDARG;
674         GT_setFailureReason (curTrace,
675                              GT_4CLASS,
676                              "RscTable_getInfo",
677                              status,
678                              "Invalid procId specified");
679         return status;
680     }
682     obj = (RscTable_Object *)RscTable_state.handles[procId];
684     if (type == TYPE_TRACE) {
685         if (da && len) {
686             *da = obj->trace.da;
687             *len = obj->trace.len;
688         }
689         else {
690             status = RSCTABLE_E_INVALIDARG;
691             GT_setFailureReason (curTrace,
692                                  GT_4CLASS,
693                                  "RscTable_getInfo",
694                                  status,
695                                  "Invalid pointer passed");
696         }
697     }
698     else if (type == TYPE_VDEV) {
699         if (extra == 0 && len) {
700             *len = obj->numVrings;
701         }
702         else if (extra < obj->numVrings && da) {
703             *da = obj->vrings[extra-1].da;
704         }
705         else {
706             status = RSCTABLE_E_INVALIDARG;
707             GT_setFailureReason (curTrace,
708                                  GT_4CLASS,
709                                  "RscTable_getInfo",
710                                  status,
711                                  "Invalid pointer passed");
712         }
713     }
714     else {
715         // type not supported
716         status = -1;
717     }
719     return status;
720     // Retrieve the info for a certain resource type (ex. Vring info)
723 Int
724 RscTable_update (UInt16 procId, ProcMgr_Handle procHandle)
726     // Updates the table to physical memory so that phys addrs are available to
727     // the remote cores.  This should be called only after loading the remote
728     // cores.
729     Int status = 0;
730     RscTable_Object *obj = NULL;
731     UInt32 rscTableVA = 0, rscTablePA = 0;
733     // Find the table for this procId, if not found, return an error
734     if (procId >= MultiProc_MAXPROCESSORS || !RscTable_state.handles[procId]) {
735         status = RSCTABLE_E_INVALIDARG;
736         GT_setFailureReason (curTrace,
737                              GT_4CLASS,
738                              "RscTable_update",
739                              status,
740                              "Invalid ProcId specified");
741         return status;
742     }
744     obj = (RscTable_Object *)RscTable_state.handles[procId];
746     status = ProcMgr_translateAddr(procHandle, (Ptr *)&rscTablePA,
747                                    ProcMgr_AddrType_MasterPhys,
748                                    (Ptr)obj->rscTableDA,
749                                    ProcMgr_AddrType_SlaveVirt);
750     if (status < 0) {
751         status = RSCTABLE_E_FAIL;
752         GT_setFailureReason (curTrace,
753                              GT_4CLASS,
754                              "RscTable_update",
755                              status,
756                              "Unable to get the Master PA for RscTable");
757     }
758     else {
759         /* Map the phys mem to local */
760         rscTableVA = (UInt32)mmap_device_io(obj->rscTableLen, rscTablePA);
761         if (rscTableVA != MAP_DEVICE_FAILED) {
762             /* Copy the updated table to the memory */
763             Memory_copy((Ptr)rscTableVA, obj->rscTable, obj->rscTableLen);
764             munmap_device_io(rscTableVA, obj->rscTableLen);
765         }
766         else {
767             status = RSCTABLE_E_FAIL;
768             GT_setFailureReason (curTrace,
769                                  GT_4CLASS,
770                                  "RscTable_update",
771                                  status,
772                                  "Unable to map memory");
773         }
774     }
776     return status;
779 Int
780 RscTable_free (RscTable_Handle * handle)
782     Int status = 0;
783     RscTable_Object * obj = NULL;
784     Int i = 0;
785     List_Elem * elem;
787     // Remove resource table from coreId-Handle pair list
788     if (handle && *handle) {
789         // sanity check
790         for (i = 0; i < MultiProc_MAXPROCESSORS; i++) {
791             if (RscTable_state.handles[i] == *handle)
792                 break;
793         }
794         if (i < MultiProc_MAXPROCESSORS) {
795             obj = (RscTable_Object *)(*handle);
796             // Additional sanity check
797             if (obj->procId == i) {
798                 // Free vrings, memEntries, etc.
799                 if (obj->chunks) {
800                     while ((elem = List_get(obj->chunks))) {
801                         Carveout_Elem * co_elem = (Carveout_Elem *)elem;
802                         munmap((Void *)co_elem->addr, co_elem->size);
803                         Memory_free(NULL, co_elem, sizeof(Carveout_Elem));
804                     }
805                     List_delete(&obj->chunks);
806                     obj->chunks = NULL;
807                 }
808                 if (obj->vrings)
809                     Memory_free(NULL, obj->vrings,
810                                 sizeof(*obj->vrings) * obj->numVrings);
811                 obj->vrings = NULL;
812                 obj->numMemEntries = 0;
814                 if (obj->rscTable)
815                     Memory_free(NULL, obj->rscTable, obj->rscTableLen);
816                 obj->rscTableDA = 0;
817                 Memory_free(NULL, obj, sizeof (RscTable_Object));
818                 *handle = NULL;
819                 RscTable_state.handles[i] = NULL;
820             }
821         }
822         else {
823             status = RSCTABLE_E_INVALIDARG;
824             GT_setFailureReason (curTrace,
825                                  GT_4CLASS,
826                                  "RscTable_free",
827                                  status,
828                                  "Invalid handle passed");
829         }
830     }
831     else {
832         status = RSCTABLE_E_INVALIDARG;
833         GT_setFailureReason (curTrace,
834                              GT_4CLASS,
835                              "RscTable_free",
836                              status,
837                              "Invalid handle passed");
838     }
840     return status;
843 Int RscTable_setStatus(UInt16 procId, UInt32 value)
845     Int i;
846     Int status = 0;
848     RscTable_Object * obj = (RscTable_Object *)RscTable_state.handles[procId];
849     RscTable_Header * table = (RscTable_Header *)obj->rscTable;
851     /* Look for the vdev entry and update the status */
852     for (i = 0; i < table->num; i++) {
853         RscTable_MemEntry * entry =
854                 (RscTable_MemEntry *)((UInt32)table + table->offset[i]);
855         if (entry->type == TYPE_VDEV) {
856             struct fw_rsc_vdev *vdev = (struct fw_rsc_vdev *)entry;
857             vdev->status = value;
858             break;
859         }
860     }
862     if (i == table->num) {
863         status = RSCTABLE_E_FAIL;
864     }
866     return status;
869 #if defined (__cplusplus)
871 #endif /* defined (__cplusplus) */