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 typedef unsigned long long __u64;
53 typedef unsigned int __u32;
54
55 struct dma_heap_allocation_data {
56 __u64 len;
57 __u32 fd;
58 __u32 fd_flags;
59 __u64 heap_flags;
60 };
61
62 #define DMA_HEAP_IOC_MAGIC 'H'
63 #define DMA_HEAP_IOCTL_ALLOC _IOWR(DMA_HEAP_IOC_MAGIC, 0x0,\
64 struct dma_heap_allocation_data)
65
66 #define DMA_BUF_SYNC_READ (1 << 0)
67 #define DMA_BUF_SYNC_WRITE (2 << 0)
68 #define DMA_BUF_SYNC_RW (DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE)
69 #define DMA_BUF_SYNC_START (0 << 2)
70 #define DMA_BUF_SYNC_END (1 << 2)
71
72 struct dma_buf_sync {
73 __u64 flags;
74 };
75
76 #define DMA_BUF_BASE 'b'
77 #define DMA_BUF_IOCTL_SYNC _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
78
79 #define CMA_HEAP_SIZE 1024 * 1024
80
dma_sync_device_to_cpu(int fd)81 int dma_sync_device_to_cpu(int fd) {
82 struct dma_buf_sync sync = {0};
83
84 sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW;
85 return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
86 }
87
dma_sync_cpu_to_device(int fd)88 int dma_sync_cpu_to_device(int fd) {
89 struct dma_buf_sync sync = {0};
90
91 sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW;
92 return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
93 }
94
dma_buf_alloc(const char * path,size_t size,int * fd,void ** va)95 int dma_buf_alloc(const char *path, size_t size, int *fd, void **va) {
96 int ret;
97 int prot;
98 void *mmap_va;
99 int dma_heap_fd = -1;
100 struct dma_heap_allocation_data buf_data;
101
102 /* open dma_heap fd */
103 dma_heap_fd = open(path, O_RDWR);
104 if (dma_heap_fd < 0) {
105 printf("open %s fail!\n", path);
106 return dma_heap_fd;
107 }
108
109 /* alloc buffer */
110 memset(&buf_data, 0x0, sizeof(struct dma_heap_allocation_data));
111
112 buf_data.len = size;
113 buf_data.fd_flags = O_CLOEXEC | O_RDWR;
114 ret = ioctl(dma_heap_fd, DMA_HEAP_IOCTL_ALLOC, &buf_data);
115 if (ret < 0) {
116 printf("RK_DMA_HEAP_ALLOC_BUFFER failed\n");
117 return ret;
118 }
119
120 /* mmap va */
121 if (fcntl(buf_data.fd, F_GETFL) & O_RDWR)
122 prot = PROT_READ | PROT_WRITE;
123 else
124 prot = PROT_READ;
125
126 /* mmap contiguors buffer to user */
127 mmap_va = (void *)mmap(NULL, buf_data.len, prot, MAP_SHARED, buf_data.fd, 0);
128 if (mmap_va == MAP_FAILED) {
129 printf("mmap failed: %s\n", strerror(errno));
130 return -errno;
131 }
132
133 *va = mmap_va;
134 *fd = buf_data.fd;
135
136 close(dma_heap_fd);
137
138 return 0;
139 }
140
dma_buf_free(size_t size,int * fd,void * va)141 void dma_buf_free(size_t size, int *fd, void *va) {
142 int len;
143
144 len = size;
145 munmap(va, len);
146
147 close(*fd);
148 *fd = -1;
149 }
150
151
152
153