xref: /OK3568_Linux_fs/external/linux-rga/samples/im2d_slt/sources/dma_alloc.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2022 Rockchip Electronics Co., Ltd.
3  * Authors:
4  *  Cerf Yu <cerf.yu@rock-chips.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <getopt.h>
20 #include <sys/mman.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <time.h>
24 #include <stdbool.h>
25 #include <assert.h>
26 #include <endian.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/ioctl.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <sys/poll.h>
37 #include <unistd.h>
38 #include <stdbool.h>
39 #include <sys/eventfd.h>
40 
41 #include <sched.h>
42 #include <pthread.h>
43 
44 #include <stdint.h>
45 #include <math.h>
46 #include <memory.h>
47 #include <sys/time.h>
48 
49 #include "dma_alloc.h"
50 #include "RgaUtils.h"
51 
52 
53 typedef unsigned long long __u64;
54 typedef  unsigned int __u32;
55 
56 struct dma_heap_allocation_data {
57 	__u64 len;
58 	__u32 fd;
59 	__u32 fd_flags;
60 	__u64 heap_flags;
61 };
62 
63 #define DMA_HEAP_IOC_MAGIC		'H'
64 #define DMA_HEAP_IOCTL_ALLOC	_IOWR(DMA_HEAP_IOC_MAGIC, 0x0,\
65 				      struct dma_heap_allocation_data)
66 
67 #define DMA_BUF_SYNC_READ      (1 << 0)
68 #define DMA_BUF_SYNC_WRITE     (2 << 0)
69 #define DMA_BUF_SYNC_RW        (DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE)
70 #define DMA_BUF_SYNC_START     (0 << 2)
71 #define DMA_BUF_SYNC_END       (1 << 2)
72 
73 struct dma_buf_sync {
74 	__u64 flags;
75 };
76 
77 #define DMA_BUF_BASE		'b'
78 #define DMA_BUF_IOCTL_SYNC	_IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
79 
80 #define CMA_HEAP_PATH	"/dev/rk_dma_heap/rk-dma-heap-cma"
81 #define CMA_HEAP_SIZE	1024 * 1024
82 
83 int cma_heap_fd = -1;
84 
dma_sync_device_to_cpu(int fd)85 int dma_sync_device_to_cpu(int fd) {
86     struct dma_buf_sync sync = {0};
87 
88     sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW;
89     return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
90 }
91 
dma_sync_cpu_to_device(int fd)92 int dma_sync_cpu_to_device(int fd) {
93     struct dma_buf_sync sync = {0};
94 
95     sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW;
96     return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
97 }
98 
dma_buf_alloc(int width,int height,int format,int * fd,void ** va)99 int dma_buf_alloc(int width, int height, int format, int *fd, void **va) {
100     int ret;
101     int prot;
102     void *mmap_va;
103     struct dma_heap_allocation_data buf_data;
104 
105     /* open cma fd */
106     if (cma_heap_fd < 0) {
107         cma_heap_fd = open(CMA_HEAP_PATH, O_RDWR);
108         if (cma_heap_fd < 0) {
109             printf("open %s fail!\n", CMA_HEAP_PATH);
110             return cma_heap_fd;
111         }
112     }
113 
114     /* alloc buffer */
115     memset(&buf_data, 0x0, sizeof(struct dma_heap_allocation_data));
116 
117     buf_data.len = width * height * get_bpp_from_format(format);
118     buf_data.fd_flags = O_CLOEXEC | O_RDWR;
119     ret = ioctl(cma_heap_fd, DMA_HEAP_IOCTL_ALLOC, &buf_data);
120     if (ret < 0) {
121         printf("RK_DMA_HEAP_ALLOC_BUFFER failed\n");
122         return ret;
123     }
124 
125     /* mmap va */
126     if (fcntl(buf_data.fd, F_GETFL) & O_RDWR)
127         prot = PROT_READ | PROT_WRITE;
128     else
129         prot = PROT_READ;
130 
131     /* mmap contiguors buffer to user */
132     mmap_va = (void *)mmap(NULL, buf_data.len, prot, MAP_SHARED, buf_data.fd, 0);
133     if (mmap_va == MAP_FAILED) {
134         printf("mmap failed: %s\n", strerror(errno));
135         return -errno;
136     }
137 
138     *va = mmap_va;
139     *fd = buf_data.fd;
140 
141     return 0;
142 }
143 
dma_buf_free(int width,int height,int format,int * fd,void * va)144 void dma_buf_free(int width, int height, int format, int *fd, void *va) {
145     int len;
146 
147     len =  width * height * get_bpp_from_format(format);
148     munmap(va, len);
149 
150     close(*fd);
151     *fd = -1;
152 }
153 
154 
155 
156