xref: /OK3568_Linux_fs/external/rockit/mpi/example/mod/test_mpi_mmz.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* Copyright 2020 Rockchip Electronics Co. LTD
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdint.h>
17 #include <inttypes.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <cstring>
21 #include <vector>
22 
23 #include "rk_debug.h"
24 #include "rk_comm_mb.h"
25 #include "rk_mpi_mmz.h"
26 
27 #include "test_comm_argparse.h"
28 
29 #define MB_POOL_COUNT           10
30 #define MB_POOL_MB_COUNT        10
31 #define MB_POOL_MB_SIZE         1280 * 720 * 2
32 
33 typedef struct _rkTestMMZCtx {
34     RK_S32      s32MmzCount;
35     RK_S32      s32MmzSize;
36     RK_S32      s32PoolCount;
37     RK_S32      s32LoopCount;
38     RK_BOOL     bExternal;
39     RK_BOOL     bPreAlloc;
40     RK_S32      s32RemapMode;
41     RK_S32      s32AllocType;
42 } TEST_MMZ_CTX_S;
43 
44 #define vaddr_to_fd_offset(vaddr, fd, offset) do {\
45     MB_BLK __blk = RK_MPI_MMZ_VirAddr2Handle(vaddr); \
46     if (__blk != (MB_BLK)RK_NULL) { \
47         RK_VOID* __vaddr = RK_MPI_MMZ_Handle2VirAddr(__blk); \
48         offset = vaddr - reinterpret_cast<RK_U8 *>(__vaddr); \
49         fd = RK_MPI_MMZ_Handle2Fd(__blk); \
50     } else { \
51         offset = -1; \
52         fd = -1; \
53     } \
54 } while (0);
55 
56 #define paddr_to_fd_offset(paddr, fd, offset) do {\
57     MB_BLK __blk = RK_MPI_MMZ_PhyAddr2Handle(paddr); \
58     if (__blk != (MB_BLK)RK_NULL) { \
59         RK_U64 __paddr = RK_MPI_MMZ_Handle2PhysAddr(__blk); \
60         offset = (RK_U8)paddr - (RK_U8)__paddr; \
61         fd = RK_MPI_MMZ_Handle2Fd(__blk); \
62     } else { \
63         offset = -1; \
64         fd = -1; \
65     } \
66 } while (0);
67 
unit_test_mpi_mmz(const TEST_MMZ_CTX_S * pCtx)68 RK_S32 unit_test_mpi_mmz(const TEST_MMZ_CTX_S *pCtx) {
69     MB_BLK mb;
70     RK_S32 len = 1048576;
71     RK_S32 ret = RK_SUCCESS;
72     RK_S32 flags = RK_MMZ_ALLOC_CACHEABLE | RK_MMZ_ALLOC_TYPE_CMA;
73 
74     ret = RK_MPI_MMZ_Alloc(&mb, len, flags);
75     if (ret < 0) {
76         RK_LOGI("alloc mmz fail");
77         return ret;
78     }
79 
80     RK_U8 *vaddr = reinterpret_cast<RK_U8 *>(RK_MPI_MMZ_Handle2VirAddr(mb));
81     RK_U64 paddr = RK_MPI_MMZ_Handle2PhysAddr(mb);
82     RK_S32 fd = RK_MPI_MMZ_Handle2Fd(mb);
83     len = RK_MPI_MMZ_GetSize(mb);
84     RK_S32 is_cacheable = RK_MPI_MMZ_IsCacheable(mb);
85 
86     RK_LOGI("alloc buffer: fd=%d, len=%d, paddr=0x%x, vaddr=%p, cacheable=%d", fd, len, paddr, vaddr, is_cacheable);
87     MB_BLK mb_by_fd = RK_MPI_MMZ_Fd2Handle(fd);
88     MB_BLK mb_by_vaddr = RK_MPI_MMZ_VirAddr2Handle(reinterpret_cast<RK_VOID *>(vaddr));
89     RK_LOGI("MB: %p %p %p", mb, mb_by_fd, mb_by_vaddr);
90 
91     RK_U8 *vaddr_temp = vaddr + 1;
92     RK_LOGI("vaddr+1: %p, mb: %p", vaddr_temp, RK_MPI_MMZ_VirAddr2Handle(reinterpret_cast<RK_VOID *>(vaddr_temp)));
93 
94     vaddr_temp = vaddr-1;
95     RK_LOGI("vaddr-1: %p, mb: %p", vaddr_temp, RK_MPI_MMZ_VirAddr2Handle(reinterpret_cast<RK_VOID *>(vaddr_temp)));
96 
97     vaddr_temp = vaddr+len;
98     RK_LOGI("vaddr+len: %p, mb: %p", vaddr_temp, RK_MPI_MMZ_VirAddr2Handle(reinterpret_cast<RK_VOID *>(vaddr_temp)));
99 
100     vaddr_temp = vaddr+len-1;
101     RK_LOGI("vaddr+len-1: %p, mb: %p", vaddr_temp, RK_MPI_MMZ_VirAddr2Handle(reinterpret_cast<RK_VOID *>(vaddr_temp)));
102 
103     RK_U64 paddr_temp = paddr+1;
104     RK_LOGI("paddr+1: 0x%x, mb: %p", paddr_temp, RK_MPI_MMZ_PhyAddr2Handle(paddr_temp));
105 
106     paddr_temp = paddr-1;
107     RK_LOGI("paddr-1: 0x%x, mb: %p", paddr_temp, RK_MPI_MMZ_PhyAddr2Handle(paddr_temp));
108 
109     paddr_temp = paddr+len;
110     RK_LOGI("paddr+len: 0x%x, mb: %p", paddr_temp, RK_MPI_MMZ_PhyAddr2Handle(paddr_temp));
111 
112     paddr_temp = paddr+len-1;
113     RK_LOGI("paddr+len-1: 0x%x, mb: %p", paddr_temp, RK_MPI_MMZ_PhyAddr2Handle(paddr_temp));
114 
115     RK_U32 offset;
116     vaddr_temp = vaddr+len-10;
117     vaddr_to_fd_offset(vaddr_temp, fd, offset);
118     RK_LOGI("vaddr+len-10: %p, fd: %d, offset: %d", vaddr_temp, fd, offset);
119 
120     paddr_temp = paddr+len-10;
121     paddr_to_fd_offset(paddr_temp, fd, offset);
122     RK_LOGI("paddr+len-10: 0x%x, fd: %d, offset: %d", paddr_temp, fd, offset);
123 
124     ret = RK_MPI_MMZ_FlushCacheStart(mb, 0, 0, RK_MMZ_SYNC_WRITEONLY);
125     memset(vaddr, 0x5A, len);
126     ret = RK_MPI_MMZ_FlushCacheEnd(mb, 0, 0, RK_MMZ_SYNC_WRITEONLY);
127 
128     ret = RK_MPI_MMZ_FlushCacheStart(mb, 4096, 4096, RK_MMZ_SYNC_RW);
129     memset(vaddr, 0x5A, len);
130     ret = RK_MPI_MMZ_FlushCacheEnd(mb, 4096, 4096, RK_MMZ_SYNC_RW);
131 
132     vaddr_temp = vaddr+len-10;
133     ret = RK_MPI_MMZ_FlushCacheVaddrStart(reinterpret_cast<RK_VOID *>(vaddr_temp), 4096, RK_MMZ_SYNC_WRITEONLY);
134     memset(vaddr_temp, 0x5A, 10);
135     ret = RK_MPI_MMZ_FlushCacheVaddrEnd(reinterpret_cast<RK_VOID *>(vaddr_temp), 4096, RK_MMZ_SYNC_WRITEONLY);
136 
137     paddr_temp = paddr_temp+len-10;
138     ret = RK_MPI_MMZ_FlushCachePaddrStart(paddr_temp, 4096, RK_MMZ_SYNC_WRITEONLY);
139     vaddr_temp = vaddr+len-10;
140     memset(vaddr_temp, 0x5A, 10);
141     ret = RK_MPI_MMZ_FlushCachePaddrEnd(paddr_temp, 4096, RK_MMZ_SYNC_WRITEONLY);
142 
143     usleep(100000);
144     RK_MPI_MMZ_Free(mb);
145     return RK_SUCCESS;
146 }
147 
148 
149 static const char *const usages[] = {
150     "./rk_mpi_mmz_test [-c MB_COUNT] [-s MB_SIZE]...",
151     NULL,
152 };
153 
main(RK_S32 argc,const char ** argv)154 RK_S32 main(RK_S32 argc, const char **argv) {
155     RK_S32 s32Ret = RK_SUCCESS;
156     TEST_MMZ_CTX_S stMmzCtx;
157 
158     memset(&stMmzCtx, 0, sizeof(TEST_MMZ_CTX_S));
159 
160     struct argparse_option options[] = {
161         OPT_HELP(),
162         OPT_GROUP("basic options:"),
163         OPT_END(),
164     };
165 
166     struct argparse argparse;
167     argparse_init(&argparse, options, usages, 0);
168     argparse_describe(&argparse, "\nselect a test case to run.",
169                                  "\nuse --help for details.");
170 
171     argc = argparse_parse(&argparse, argc, argv);
172 
173     s32Ret = unit_test_mpi_mmz(&stMmzCtx);
174     if (s32Ret != RK_SUCCESS) {
175         goto __FAILED;
176     }
177 
178     RK_LOGI("test running ok.");
179     return RK_SUCCESS;
180 __FAILED:
181     RK_LOGE("test running failed!");
182     return RK_ERR_MB_BUSY;
183 }
184