From 3ef68d010b95fd97d440412974be91d2d1ad652a Mon Sep 17 00:00:00 2001 From: Han Gao Date: Sat, 20 Jul 2024 15:43:29 +0800 Subject: [PATCH] sync: SDK 1.5.4 video_memory Signed-off-by: Han Gao --- .../video/video_memory/driver/video_memory.c | 71 ++++++++++++++++++- .../video/video_memory/driver/video_memory.h | 9 ++- drivers/video/video_memory/lib/video_mem.c | 62 +++++++++++++--- drivers/video/video_memory/lib/video_mem.h | 15 +++- .../video_memory/test/video_memory_test.c | 8 +-- 5 files changed, 146 insertions(+), 19 deletions(-) diff --git a/drivers/video/video_memory/driver/video_memory.c b/drivers/video/video_memory/driver/video_memory.c index 1f22ab8a9..0ecb5d31c 100644 --- a/drivers/video/video_memory/driver/video_memory.c +++ b/drivers/video/video_memory/driver/video_memory.c @@ -86,6 +86,7 @@ #include "video_memory.h" #include "rsvmem_pool.h" + //#define VIDMEM_DMA_MAP #define DISCRETE_PAGES 0 //#define VIDMEM_DEBUG @@ -202,6 +203,7 @@ struct mem_block struct vm_area_struct * vma; bool is_cma; bool is_vi_mem; + bool cache_en; void *va; union @@ -441,6 +443,42 @@ OnError: return status; } + +static void invalid_data_cache(IN struct file *filp, IN unsigned long bus_address ) +{ + struct mem_block *memBlk = NULL; + struct mem_node *mnode = NULL; + mnode = get_mem_node(filp, bus_address, 0); + if (NULL == mnode) + { + return; + } + + memBlk = &mnode->memBlk; + dma_addr_t dma_handle = memBlk->dma_addr; + size_t size = memBlk->size; + dma_sync_single_for_cpu(gdev,dma_handle ,memBlk->size,DMA_FROM_DEVICE); + +} + +static void flush_data_cache(IN struct file *filp, IN unsigned long bus_address ) +{ + struct mem_block *memBlk = NULL; + struct mem_node *mnode = NULL; + + mnode = get_mem_node(filp, bus_address, 0); + if (NULL == mnode) + { + return; + } + + memBlk = &mnode->memBlk; + dma_addr_t dma_handle = memBlk->dma_addr; + size_t size = memBlk->size; + dma_sync_single_for_device(gdev,dma_handle ,memBlk->size,DMA_TO_DEVICE); + +} + static int Mmap( IN struct mem_block *MemBlk, @@ -455,16 +493,21 @@ Mmap( vma->vm_flags |= VM_FLAGS; /* Make this mapping write combined. */ - vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); - + if (!memBlk->cache_en) { + vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); + } + DEBUG_PRINT("vm_page_prot:0x%llx\n",vma->vm_page_prot); /* Now map all the vmalloc pages to this user address. */ if (memBlk->contiguous) { /* map kernel memory to user space.. */ + #if 0 if (memBlk->is_cma == true) { return dma_mmap_coherent(gdev, vma, memBlk->va, memBlk->dma_addr, vma->vm_end - vma->vm_start); - } else { + } else + #endif + { if (remap_pfn_range(vma, vma->vm_start, page_to_pfn(memBlk->contiguousPages) + skipPages, @@ -991,6 +1034,13 @@ GFP_Alloc( gfp |= __GFP_DMA32; } + + if (Flags & ALLOC_FLAG_ENABLE_CACHE) { + memBlk->cache_en = true; + } else { + memBlk->cache_en = false; + } + memBlk->contiguous = contiguous; memBlk->numPages = numPages; memBlk->size = size; @@ -1349,6 +1399,21 @@ static long vidalloc_ioctl(struct file *filp, unsigned int cmd, unsigned long ar } break; } + + case MEMORY_IOC_DMABUF_FLUSH_CACHE: + ret = copy_from_user(¶ms, (void*)arg, sizeof(VidmemParams)); + if (!ret) + { + flush_data_cache(filp, params.bus_address); + } + break; + case MEMORY_IOC_DMABUF_INVALID_CACHE: + ret = copy_from_user(¶ms, (void*)arg, sizeof(VidmemParams)); + if (!ret) + { + invalid_data_cache(filp, params.bus_address); + } + break; default: ret = EINVAL; } diff --git a/drivers/video/video_memory/driver/video_memory.h b/drivers/video/video_memory/driver/video_memory.h index a5b8785c4..7367b4101 100644 --- a/drivers/video/video_memory/driver/video_memory.h +++ b/drivers/video/video_memory/driver/video_memory.h @@ -37,6 +37,8 @@ #define ALLOC_FLAG_CMA 0x00000008 /* Use VI reserved memory */ #define ALLOC_FLAG_VI 0x00000010 +/* Buffer enable cache */ +#define ALLOC_FLAG_ENABLE_CACHE 0x00000020 /* Alloc rsvmem pool region id should be 0~15 */ #define SET_ALLOC_FLAG_REGION(flag, region_id) (flag & 0x00ffffff) | (region_id << 24) @@ -49,7 +51,9 @@ #define MEMORY_IOC_DMABUF_EXPORT _IOWR(MEMORY_IOC_MAGIC, 3, VidmemParams *) #define MEMORY_IOC_DMABUF_IMPORT _IOWR(MEMORY_IOC_MAGIC, 4, VidmemParams *) #define MEMORY_IOC_DMABUF_RELEASE _IOWR(MEMORY_IOC_MAGIC, 5, VidmemParams *) -#define MEMORY_IOC_MAXNR 5 +#define MEMORY_IOC_DMABUF_FLUSH_CACHE _IOWR(MEMORY_IOC_MAGIC, 6, VidmemParams *) +#define MEMORY_IOC_DMABUF_INVALID_CACHE _IOWR(MEMORY_IOC_MAGIC, 7, VidmemParams *) +#define MEMORY_IOC_MAXNR 7 typedef struct { unsigned long bus_address; @@ -59,4 +63,7 @@ typedef struct { int flags; } VidmemParams; + + + #endif /* __VIDEO_MEMORY_H_ */ diff --git a/drivers/video/video_memory/lib/video_mem.c b/drivers/video/video_memory/lib/video_mem.c index d660f6e38..db5f16698 100644 --- a/drivers/video/video_memory/lib/video_mem.c +++ b/drivers/video/video_memory/lib/video_mem.c @@ -26,7 +26,6 @@ #include "video_memory.h" #include "video_mem.h" - #define VMEM_PRINT(level, ...) \ { \ if (log_level >= VMEM_LOG_##level) \ @@ -115,7 +114,7 @@ VMEM_allocate(void *vmem, VmemParams *params) } params->phy_address = p.bus_address; - VMEM_LOGI("Allocated %d bytes, phy addr 0x%08x\n", + VMEM_LOGI("Allocated %d bytes, phy addr 0x%lx\n", params->size, params->phy_address); return VMEM_STATUS_OK; @@ -129,7 +128,7 @@ VMEM_mmap(void *vmem, VmemParams *params) if (vmem == NULL || params == NULL) return VMEM_STATUS_ERROR; - + if (params->vir_address != NULL) return VMEM_STATUS_OK; @@ -140,13 +139,13 @@ VMEM_mmap(void *vmem, VmemParams *params) MAP_SHARED, fd, offset); if (vir_addr == MAP_FAILED) { - VMEM_LOGE("Failed to mmap physical address: 0x%08x, using fd %d\n", + VMEM_LOGE("Failed to mmap physical address: 0x%lx, using fd %d\n", params->phy_address, fd); return VMEM_STATUS_ERROR; } params->vir_address = vir_addr; - VMEM_LOGI("Mapped phy addr 0x%08x to vir addr %p, size %d\n", + VMEM_LOGI("Mapped phy addr 0x%lx to vir addr %p, size %d\n", params->phy_address, params->vir_address, params->size); return VMEM_STATUS_OK; @@ -163,7 +162,7 @@ VMEM_free(void *vmem, VmemParams *params) ctx = (VmemContext *)vmem; - VMEM_LOGI("Free virt addr %p, phy addr 0x%08x, size %d\n", + VMEM_LOGI("Free virt addr %p, phy addr 0x%lx, size %d\n", params->vir_address, params->phy_address, params->size); if (params->vir_address != MAP_FAILED && params->vir_address != NULL) munmap(params->vir_address, params->size); @@ -188,7 +187,7 @@ VMEM_destroy(void *vmem) VmemContext *ctx = (VmemContext *)vmem; if (ctx->fd_alloc != -1) close(ctx->fd_alloc); - + free(vmem); } @@ -215,7 +214,7 @@ VMEM_export(void *vmem, VmemParams *params) } params->fd = p.fd; - VMEM_LOGI("Exported phy addr 0x%08x to fd %d, size %d\n", + VMEM_LOGI("Exported phy addr 0x%lx to fd %d, size %d\n", params->phy_address, params->fd, params->size); return VMEM_STATUS_OK; @@ -241,7 +240,7 @@ VMEM_import(void *vmem, VmemParams *params) params->phy_address = p.bus_address; params->size = p.size; - VMEM_LOGI("Imported fd %d to phy addr 0x%08x, size %d\n", + VMEM_LOGI("Imported fd %d to phy addr 0x%lx, size %d\n", params->fd, params->phy_address, params->size); return VMEM_STATUS_OK; @@ -256,7 +255,7 @@ VMEM_release(void *vmem, VmemParams *params) return VMEM_STATUS_ERROR; ctx = (VmemContext *)vmem; - VMEM_LOGI("Released imported phy addr 0x%08x, fd %d, size %d\n", + VMEM_LOGI("Released imported phy addr 0x%lx, fd %d, size %d\n", params->phy_address, params->fd, params->size); if (params->vir_address != MAP_FAILED && params->vir_address != NULL) munmap(params->vir_address, params->size); @@ -273,6 +272,49 @@ VMEM_release(void *vmem, VmemParams *params) return VMEM_STATUS_OK; } + +VmemStatus +VMEM_flush_cache(void *vmem, VmemParams *params,VmemCacheDir dir) +{ + VmemContext *ctx = NULL; + VidmemParams p; + int ret; + if (vmem == NULL || params == NULL) + return VMEM_STATUS_ERROR; + + ctx = (VmemContext *)vmem; + + VMEM_LOGI("Flush phy addr 0x%lx, size %d,dir:%d\n", + params->phy_address, params->size,dir); + + if (params->phy_address != 0) + { + memset(&p, 0, sizeof(p)); + p.bus_address = params->phy_address; + if(dir == VEME_CACHE_DIR_TO_DEV) + { + ret = ioctl(ctx->fd_alloc, MEMORY_IOC_DMABUF_FLUSH_CACHE, &p); + }else if(dir == VEME_CACHE_DIR_FROM_DEV) + { + ret = ioctl(ctx->fd_alloc, MEMORY_IOC_DMABUF_INVALID_CACHE, &p); + } + else + { + return VMEM_STATUS_ERROR; + } + + if(ret) + { + VMEM_LOGE("fail ret %d\n",ret); + return VMEM_STATUS_ERROR; + } + return VMEM_STATUS_OK; + } + + + return VMEM_STATUS_ERROR; +} + static int getLogLevel() { char *env = getenv("VMEM_LOG_LEVEL"); diff --git a/drivers/video/video_memory/lib/video_mem.h b/drivers/video/video_memory/lib/video_mem.h index fa7120519..253c0b555 100644 --- a/drivers/video/video_memory/lib/video_mem.h +++ b/drivers/video/video_memory/lib/video_mem.h @@ -34,11 +34,16 @@ extern "C" { #define VMEM_FLAG_CMA 0x00000008 /* Use VI reserved memory */ #define VMEM_FLAG_VI 0x00000010 +/* Buffer enable cache */ +#define VMEM_FLAG_ENABLE_CACHE 0x00000020 /* Alloc rsvmem pool region id should be 0~15 */ +#define MAX_REGION_ID 15 + #define SET_ALLOC_FLAG_REGION(flag, region_id) (flag & 0x00ffffff) | (region_id << 24) #define GET_ALLOC_FLAG_REGION(flag) (flag >> 24) + typedef enum _VmemStatus { VMEM_STATUS_OK = 0, @@ -50,11 +55,17 @@ typedef struct _VmemParams { int size; int flags; - unsigned int phy_address; + unsigned long phy_address; void *vir_address; int fd; } VmemParams; +typedef enum{ + VEME_CACHE_DIR_TO_DEV = 0, + VEME_CACHE_DIR_FROM_DEV, + VEME_CACHE_DIR_INVALID, +}VmemCacheDir; + VmemStatus VMEM_create(void **vmem); VmemStatus VMEM_allocate(void *vmem, VmemParams *params); VmemStatus VMEM_mmap(void *vmem, VmemParams *params); @@ -65,6 +76,8 @@ VmemStatus VMEM_export(void *vmem, VmemParams *params); VmemStatus VMEM_import(void *vmem, VmemParams *params); VmemStatus VMEM_release(void *vmem, VmemParams *params); +VmemStatus VMEM_flush_cache(void *vmem, VmemParams *params,VmemCacheDir dir); + #ifdef __cplusplus } #endif diff --git a/drivers/video/video_memory/test/video_memory_test.c b/drivers/video/video_memory/test/video_memory_test.c index 9f7a9c5be..2270f4703 100644 --- a/drivers/video/video_memory/test/video_memory_test.c +++ b/drivers/video/video_memory/test/video_memory_test.c @@ -120,20 +120,20 @@ int main(int argc, char **argv) } if (VMEM_mmap(vmem, params) != VMEM_STATUS_OK) { - printf("ERROR: Failed to mmap busAddress: 0x%08x\n", + printf("ERROR: Failed to mmap busAddress: 0x%lx\n", params->phy_address); err = 1; break; } if (VMEM_export(vmem, params) != VMEM_STATUS_OK) { - printf("ERROR: Failed to export buffer: 0x%08x\n", + printf("ERROR: Failed to export buffer: 0x%lx\n", params->phy_address); err = 1; break; } - printf("Allocated buffer %d of type %d at paddr 0x%08x vaddr %p size %d fd %d\n", + printf("Allocated buffer %d of type %d at paddr 0x%lx vaddr %p size %d fd %d\n", i, type, params->phy_address, params->vir_address, size, params->fd); memset(&imp_params, 0, sizeof(imp_params)); @@ -145,7 +145,7 @@ int main(int argc, char **argv) break; } - printf("Imported fd %d: paddr 0x%08x vaddr %p size %d\n", + printf("Imported fd %d: paddr 0x%lx vaddr %p size %d\n", params->fd, params->phy_address, params->vir_address, size); VMEM_release(vmem, &imp_params); }