Vision Apps User Guide
app_mem.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2017-2025 Texas Instruments Incorporated
4  *
5  * All rights reserved not granted herein.
6  *
7  * Limited License.
8  *
9  * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
10  * license under copyrights and patents it now or hereafter owns or controls to make,
11  * have made, use, import, offer to sell and sell ("Utilize") this software subject to the
12  * terms herein. With respect to the foregoing patent license, such license is granted
13  * solely to the extent that any such patent is necessary to Utilize the software alone.
14  * The patent license shall not apply to any combinations which include this software,
15  * other than combinations with devices manufactured by or for TI ("TI Devices").
16  * No hardware patent is licensed hereunder.
17  *
18  * Redistributions must preserve existing copyright notices and reproduce this license
19  * (including the above copyright notice and the disclaimer and (if applicable) source
20  * code license limitations below) in the documentation and/or other materials provided
21  * with the distribution
22  *
23  * Redistribution and use in binary form, without modification, are permitted provided
24  * that the following conditions are met:
25  *
26  * * No reverse engineering, decompilation, or disassembly of this software is
27  * permitted with respect to any software provided in binary form.
28  *
29  * * any redistribution and use are licensed by TI for use only with TI Devices.
30  *
31  * * Nothing shall obligate TI to provide you with source code for the software
32  * licensed and provided to you in object code.
33  *
34  * If software source code is provided to you, modification and redistribution of the
35  * source code are permitted provided that the following conditions are met:
36  *
37  * * any redistribution and use of the source code, including any resulting derivative
38  * works, are licensed by TI for use only with TI Devices.
39  *
40  * * any redistribution and use of any object code compiled from the source code
41  * and any resulting derivative works, are licensed by TI for use only with TI Devices.
42  *
43  * Neither the name of Texas Instruments Incorporated nor the names of its suppliers
44  *
45  * may be used to endorse or promote products derived from this software without
46  * specific prior written permission.
47  *
48  * DISCLAIMER.
49  *
50  * THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS
51  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53  * IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
55  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
57  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
58  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59  * OF THE POSSIBILITY OF SUCH DAMAGE.
60  *
61  */
62 
63 #ifndef APP_MEM_H_
64 #define APP_MEM_H_
65 
66 #include <stdint.h>
67 #include <stdbool.h>
68 
69 #if defined(FREERTOS) || defined(SAFERTOS) || defined(THREADX)
70 #if defined(MCU_PLUS_SDK)
71 #if defined(HOST_EMULATION)
72 #include <drivers/dmautils/udma_standalone/udma.h>
73 #else
74 #include <drivers/udma.h>
75 #include <drivers/udma/udma_priv.h>
76 #endif
77 #else
78 #include <ti/drv/udma/udma.h>
79 #endif
80 #endif
81 
82 #if defined(R5F) && (defined(SOC_J784S4) || defined(SOC_J721S2) || defined(SOC_J742S2))
83 #include <ti/csl/csl_rat.h>
84 #endif
85 
86 #ifdef __cplusplus
87 extern "C" {
88 #endif
89 
101 #define APP_MEM_HEAP_NAME_MAX (16u)
102 
120 typedef uint64_t (*app_mem_shared_target_fxn)(const uint64_t sharedAddr);
121 
132 typedef uint64_t (*app_mem_target_shared_fxn)(const uint64_t targetAddr);
133 
135 #define APP_MEM_HEAP_DDR (0u)
136 
138 #define APP_MEM_HEAP_L3 (1u)
139 
141 #define APP_MEM_HEAP_L2 (2u)
142 
144 #define APP_MEM_HEAP_L1 (3u)
145 
147 #define APP_MEM_HEAP_DDR_SCRATCH (4u)
148 
150 #define APP_MEM_HEAP_DDR_NON_CACHE (5u)
151 
153 #define APP_MEM_HEAP_DDR_NON_CACHE_SCRATCH (6u)
154 
156 #define APP_MEM_HEAP_DDR_WT_CACHE (7u)
157 
159 #define APP_MEM_HEAP_MAX (8u)
160 
161 /* @} */
162 
178 #define APP_MEM_HEAP_FLAGS_TYPE_LINEAR_ALLOCATE (0x00000001u)
179 
182 #define APP_MEM_HEAP_FLAGS_IS_SHARED (0x00000004u)
183 
186 #define APP_MEM_HEAP_FLAGS_DO_CLEAR_ON_ALLOC (0x00000008u)
187 
188 /* @} */
189 
193 typedef struct {
194 
195  void *base;
197  uint32_t size;
198  uint32_t flags;
201 
205 typedef struct {
206 
208  #if defined(FREERTOS) || defined(SAFERTOS) || defined(THREADX)
209  Udma_VirtToPhyFxn virtToPhyFxn;
210  app_mem_shared_target_fxn shared2TargetFxn;
211  app_mem_target_shared_fxn target2SharedFxn;
212  #endif
213  #if defined(R5F) && (defined(SOC_J784S4) || defined(SOC_J721S2) || defined(SOC_J742S2))
214 
215  CSL_ratRegs *pRatRegs;
216  #endif
217  #if defined(A72) || defined(A53) || defined(PC)
218  uint64_t base;
219  uint32_t size;
220  #endif
222 
223 
227 typedef struct {
228 
229  uint32_t heap_id;
230  char heap_name[APP_MEM_HEAP_NAME_MAX];
231  uint32_t heap_size;
232  uint32_t free_size;
235 
236 #if defined(R5F) && (defined(SOC_J784S4) || defined(SOC_J721S2) || defined(SOC_J742S2))
237 
242 typedef struct {
244  uint64_t size;
246  uint64_t translatedAddress;
248  uint32_t baseAddress;
249 } app_mem_rat_prm_t;
250 #endif
251 
252 static inline void *APP_MEM_ALIGNPTR(void *val, uint32_t align);
253 static inline uint64_t APP_MEM_ALIGN64(uint64_t val, uint32_t align);
254 static inline uint32_t APP_MEM_ALIGN32(uint32_t val, uint32_t align);
255 
259 static inline void *APP_MEM_ALIGNPTR(void *val, uint32_t align)
260 {
261  return (void*)((((uintptr_t)val+align-1u) / align) * align);
262 }
263 
267 static inline uint64_t APP_MEM_ALIGN64(uint64_t val, uint32_t align)
268 {
269  return (uint64_t)( (uint64_t)(val+align-1u) / align) * align;
270 }
271 
275 static inline uint32_t APP_MEM_ALIGN32(uint32_t val, uint32_t align)
276 {
277  return (uint32_t)( (uint32_t)(val+align-1u) / align) * align;
278 }
279 
280 #if defined(__C7120__) && (defined(SOC_J784S4) || defined(SOC_J742S2))
281 
285 void appMemC7xSetL2WBINV(uint64_t param);
286 
290 uint64_t appMemC7xGetL2WBINV(void);
291 
295 void appMemC7xCleaninvalidateL2Cache(void);
296 
300 void appMemC7xSetL1DWBINV(uint64_t param);
301 
305 uint64_t appMemC7xGetL1DWBINV(void);
306 
310 void appMemC7xCleaninvalidateL1DCache(void);
311 #endif
312 
319 void static inline appMemEnableL1DandL2CacheWb(void)
320 {
321 #if defined(__C7120__) && (defined(SOC_J784S4) || defined(SOC_J742S2))
322  appMemC7xCleaninvalidateL1DCache();
323  appMemC7xCleaninvalidateL2Cache();
324 #endif
325 }
326 
338 
346 int32_t appMemInit(app_mem_init_prm_t *prm);
347 
353 int32_t appMemDeInit(void);
354 
365 void *appMemAlloc(uint32_t heap_id, uint32_t size, uint32_t align);
366 
375 bool appMemRegionQuery(uint32_t heap_id);
376 
386 int32_t tivxMemRegionTranslate (uint32_t mem_heap_region, uint32_t *heap_id);
387 
388 
397 int32_t appMemResetScratchHeap(uint32_t heap_id);
398 
409 int32_t appMemFree(uint32_t heap_id, void *ptr, uint32_t size);
410 
419 #if defined(x86_64) || defined(QNX)
420 uint64_t appMemGetDmaBufFd(void *virPtr, volatile uint32_t *dmaBufFdOffset);
421 #else
422 uint32_t appMemGetDmaBufFd(void *virPtr, volatile uint32_t *dmaBufFdOffset);
423 #endif
424 
435 #if defined(x86_64) || defined(QNX)
436 int32_t appMemTranslateDmaBufFd(uint64_t dmaBufFd, uint32_t size, uint64_t *virtPtr, uint64_t *phyPtr);
437 #else
438 int32_t appMemTranslateDmaBufFd(uint32_t dmaBufFd, uint32_t size, uint64_t *virtPtr, uint64_t *phyPtr);
439 #endif
440 
446 #if defined(x86_64) || defined(QNX)
447 void appMemCloseDmaBufFd(uint64_t dmaBufFd);
448 #else
449 void appMemCloseDmaBufFd(uint32_t dmaBufFd);
450 #endif
451 
460 uint64_t appMemGetVirt2PhyBufPtr(uint64_t virtPtr, uint32_t heap_id);
461 
471 int32_t appMemStats(uint32_t heap_id, app_mem_stats_t *stats);
472 
480 void appMemCacheInv(void *ptr, uint32_t size);
481 
489 void appMemCacheWb(void *ptr, uint32_t size);
490 
498 void appMemCacheWbInv(void *ptr, uint32_t size);
499 
504 void appMemPrintMemAllocInfo(void);
505 
514 uint64_t appMemShared2PhysPtr(uint64_t shared_ptr, uint32_t heap_id);
515 
523 uint64_t appMemShared2TargetPtr(uint64_t shared_ptr);
524 
525 
534 void *appMemMap(void *phys_addr, uint32_t size);
535 
544 int32_t appMemUnMap(void *virt_addr, uint32_t size);
545 
550 uint32_t appMemGetNumAllocs(void);
551 
552 #if defined(R5F) && (defined(SOC_J784S4) || defined(SOC_J721S2) || defined(SOC_J742S2))
553 
559 int32_t appMemAddrTranslate(app_mem_rat_prm_t *prm);
560 
566 int32_t appMemSetRatRegs(CSL_ratRegs *pRatRegs);
567 #endif
568 
569 #if defined(x86_64)
570 
583 int32_t appMemSetFdPath(char *path, uint32_t size);
584 #endif
585 
586 
587 /* @} */
588 
589 #ifdef __cplusplus
590 }
591 #endif
592 
593 #endif
594 
Heap initialization parameters.
Definition: app_mem.h:193
void * base
Definition: app_mem.h:195
void appMemCacheWbInv(void *ptr, uint32_t size)
Write back and invalidate a section of memory from cache.
int32_t appMemResetScratchHeap(uint32_t heap_id)
Reset scratch memory.
uint64_t appMemShared2PhysPtr(uint64_t shared_ptr, uint32_t heap_id)
Return the physical pointer from a shared pointer.
uint32_t heap_id
Definition: app_mem.h:229
Heap statistics and information.
Definition: app_mem.h:227
uint64_t appMemGetVirt2PhyBufPtr(uint64_t virtPtr, uint32_t heap_id)
Return physical ptr corresponds to a CMEM virtual ptr.
#define APP_MEM_HEAP_NAME_MAX
Max characters to use for heap name.
Definition: app_mem.h:101
uint32_t appMemGetNumAllocs(void)
Get number of memory allocation.
void appMemCloseDmaBufFd(uint32_t dmaBufFd)
Close the dmaBufFd of a CMEM buffer pointer.
int32_t appMemStats(uint32_t heap_id, app_mem_stats_t *stats)
Return heap statistics and information.
static uint32_t APP_MEM_ALIGN32(uint32_t val, uint32_t align)
Align 32b value to &#39;align&#39; bytes.
Definition: app_mem.h:275
int32_t appMemTranslateDmaBufFd(uint32_t dmaBufFd, uint32_t size, uint64_t *virtPtr, uint64_t *phyPtr)
Translates a given &#39;dmaBufFd&#39; to virtual and physical addresses.
static void appMemEnableL1DandL2CacheWb(void)
Enable L1D and L2 Cache by performing a WB.
Definition: app_mem.h:319
int32_t appMemInit(app_mem_init_prm_t *prm)
Init heaps for memory allocation.
uint64_t(* app_mem_target_shared_fxn)(const uint64_t targetAddr)
target pointer to shared pointer address translation callback function.
Definition: app_mem.h:132
bool appMemRegionQuery(uint32_t heap_id)
Check is specified memory region is enabled.
uint32_t appMemGetDmaBufFd(void *virPtr, volatile uint32_t *dmaBufFdOffset)
Return dmaBufFd of a CMEM buffer pointer.
void appMemCacheWb(void *ptr, uint32_t size)
Write back a section of memory from cache.
uint32_t heap_size
Definition: app_mem.h:231
void * appMemMap(void *phys_addr, uint32_t size)
Map the provided physical memory to a virtual address.
void appMemCacheInv(void *ptr, uint32_t size)
Invalidate a section of memory from cache.
Memory module initialization parameters.
Definition: app_mem.h:205
uint32_t flags
Definition: app_mem.h:198
uint64_t(* app_mem_shared_target_fxn)(const uint64_t sharedAddr)
Shared pointer to target pointer address translation callback function.
Definition: app_mem.h:120
uint64_t appMemShared2TargetPtr(uint64_t shared_ptr)
Return the physical pointer from a shared pointer.
uint32_t free_size
Definition: app_mem.h:232
#define APP_MEM_HEAP_MAX
Max heaps in system.
Definition: app_mem.h:159
int32_t appMemUnMap(void *virt_addr, uint32_t size)
Unmap the provided virtual memory.
int32_t appMemFree(uint32_t heap_id, void *ptr, uint32_t size)
Free memory that was previously allocated.
uint32_t size
Definition: app_mem.h:197
static void * APP_MEM_ALIGNPTR(void *val, uint32_t align)
Align ptr value to &#39;align&#39; bytes.
Definition: app_mem.h:259
int32_t tivxMemRegionTranslate(uint32_t mem_heap_region, uint32_t *heap_id)
Convert tivx enumerated memory regions to app memory regions.
void appMemInitPrmSetDefault(app_mem_init_prm_t *prm)
Set defaults to app_mem_init_prm_t.
int32_t appMemDeInit(void)
De-Init heaps for memory allocation.
static uint64_t APP_MEM_ALIGN64(uint64_t val, uint32_t align)
Align 64b value to &#39;align&#39; bytes.
Definition: app_mem.h:267
void appMemPrintMemAllocInfo(void)
Print memory allocation from shared memory region.
void * appMemAlloc(uint32_t heap_id, uint32_t size, uint32_t align)
Alloc memory from specific heap.