1 // SPDX-License-Identifier: GPL-2.0
11 #include <sys/ioctl.h>
13 #include <sys/types.h>
15 #include <linux/dma-buf.h>
18 #include "../../../../include/uapi/linux/dma-heap.h"
20 #define DEVPATH "/dev/dma_heap"
22 static int check_vgem(int fd
)
24 drm_version_t version
= { 0 };
31 ret
= ioctl(fd
, DRM_IOCTL_VERSION
, &version
);
35 return !strcmp(name
, "vgem");
38 static int open_vgem(void)
41 const char *drmstr
= "/dev/dri/card";
44 for (i
= 0; i
< 16; i
++) {
47 snprintf(name
, 80, "%s%u", drmstr
, i
);
49 fd
= open(name
, O_RDWR
);
53 if (!check_vgem(fd
)) {
64 static int import_vgem_fd(int vgem_fd
, int dma_buf_fd
, uint32_t *handle
)
66 struct drm_prime_handle import_handle
= {
73 ret
= ioctl(vgem_fd
, DRM_IOCTL_PRIME_FD_TO_HANDLE
, &import_handle
);
75 *handle
= import_handle
.handle
;
79 static void close_handle(int vgem_fd
, uint32_t handle
)
81 struct drm_gem_close close
= {
85 ioctl(vgem_fd
, DRM_IOCTL_GEM_CLOSE
, &close
);
88 static int dmabuf_heap_open(char *name
)
93 ret
= snprintf(buf
, 256, "%s/%s", DEVPATH
, name
);
95 printf("snprintf failed!\n");
99 fd
= open(buf
, O_RDWR
);
101 printf("open %s failed!\n", buf
);
105 static int dmabuf_heap_alloc_fdflags(int fd
, size_t len
, unsigned int fd_flags
,
106 unsigned int heap_flags
, int *dmabuf_fd
)
108 struct dma_heap_allocation_data data
= {
111 .fd_flags
= fd_flags
,
112 .heap_flags
= heap_flags
,
119 ret
= ioctl(fd
, DMA_HEAP_IOCTL_ALLOC
, &data
);
122 *dmabuf_fd
= (int)data
.fd
;
126 static int dmabuf_heap_alloc(int fd
, size_t len
, unsigned int flags
,
129 return dmabuf_heap_alloc_fdflags(fd
, len
, O_RDWR
| O_CLOEXEC
, flags
,
133 static void dmabuf_sync(int fd
, int start_stop
)
135 struct dma_buf_sync sync
= {
136 .flags
= start_stop
| DMA_BUF_SYNC_RW
,
140 ret
= ioctl(fd
, DMA_BUF_IOCTL_SYNC
, &sync
);
142 printf("sync failed %d\n", errno
);
145 #define ONE_MEG (1024 * 1024)
147 static int test_alloc_and_import(char *heap_name
)
149 int heap_fd
= -1, dmabuf_fd
= -1, importer_fd
= -1;
154 printf("Testing heap: %s\n", heap_name
);
156 heap_fd
= dmabuf_heap_open(heap_name
);
160 printf("Allocating 1 MEG\n");
161 ret
= dmabuf_heap_alloc(heap_fd
, ONE_MEG
, 0, &dmabuf_fd
);
163 printf("Allocation Failed!\n");
167 /* mmap and write a simple pattern */
170 PROT_READ
| PROT_WRITE
,
174 if (p
== MAP_FAILED
) {
175 printf("mmap() failed: %m\n");
179 printf("mmap passed\n");
181 dmabuf_sync(dmabuf_fd
, DMA_BUF_SYNC_START
);
182 memset(p
, 1, ONE_MEG
/ 2);
183 memset((char *)p
+ ONE_MEG
/ 2, 0, ONE_MEG
/ 2);
184 dmabuf_sync(dmabuf_fd
, DMA_BUF_SYNC_END
);
186 importer_fd
= open_vgem();
187 if (importer_fd
< 0) {
189 printf("Failed to open vgem\n");
193 ret
= import_vgem_fd(importer_fd
, dmabuf_fd
, &handle
);
195 printf("Failed to import buffer\n");
198 printf("import passed\n");
200 dmabuf_sync(dmabuf_fd
, DMA_BUF_SYNC_START
);
201 memset(p
, 0xff, ONE_MEG
);
202 dmabuf_sync(dmabuf_fd
, DMA_BUF_SYNC_END
);
203 printf("syncs passed\n");
205 close_handle(importer_fd
, handle
);
211 if (importer_fd
>= 0)
221 /* Test the ioctl version compatibility w/ a smaller structure then expected */
222 static int dmabuf_heap_alloc_older(int fd
, size_t len
, unsigned int flags
,
226 unsigned int older_alloc_ioctl
;
227 struct dma_heap_allocation_data_smaller
{
234 .fd_flags
= O_RDWR
| O_CLOEXEC
,
237 older_alloc_ioctl
= _IOWR(DMA_HEAP_IOC_MAGIC
, 0x0,
238 struct dma_heap_allocation_data_smaller
);
242 ret
= ioctl(fd
, older_alloc_ioctl
, &data
);
245 *dmabuf_fd
= (int)data
.fd
;
249 /* Test the ioctl version compatibility w/ a larger structure then expected */
250 static int dmabuf_heap_alloc_newer(int fd
, size_t len
, unsigned int flags
,
254 unsigned int newer_alloc_ioctl
;
255 struct dma_heap_allocation_data_bigger
{
266 .fd_flags
= O_RDWR
| O_CLOEXEC
,
268 .garbage1
= 0xffffffff,
269 .garbage2
= 0x88888888,
270 .garbage3
= 0x11111111,
273 newer_alloc_ioctl
= _IOWR(DMA_HEAP_IOC_MAGIC
, 0x0,
274 struct dma_heap_allocation_data_bigger
);
278 ret
= ioctl(fd
, newer_alloc_ioctl
, &data
);
282 *dmabuf_fd
= (int)data
.fd
;
286 static int test_alloc_compat(char *heap_name
)
288 int heap_fd
= -1, dmabuf_fd
= -1;
291 heap_fd
= dmabuf_heap_open(heap_name
);
295 printf("Testing (theoretical)older alloc compat\n");
296 ret
= dmabuf_heap_alloc_older(heap_fd
, ONE_MEG
, 0, &dmabuf_fd
);
298 printf("Older compat allocation failed!\n");
304 printf("Testing (theoretical)newer alloc compat\n");
305 ret
= dmabuf_heap_alloc_newer(heap_fd
, ONE_MEG
, 0, &dmabuf_fd
);
307 printf("Newer compat allocation failed!\n");
311 printf("Ioctl compatibility tests passed\n");
321 static int test_alloc_errors(char *heap_name
)
323 int heap_fd
= -1, dmabuf_fd
= -1;
326 heap_fd
= dmabuf_heap_open(heap_name
);
330 printf("Testing expected error cases\n");
331 ret
= dmabuf_heap_alloc(0, ONE_MEG
, 0x111111, &dmabuf_fd
);
333 printf("Did not see expected error (invalid fd)!\n");
338 ret
= dmabuf_heap_alloc(heap_fd
, ONE_MEG
, 0x111111, &dmabuf_fd
);
340 printf("Did not see expected error (invalid heap flags)!\n");
345 ret
= dmabuf_heap_alloc_fdflags(heap_fd
, ONE_MEG
,
346 ~(O_RDWR
| O_CLOEXEC
), 0, &dmabuf_fd
);
348 printf("Did not see expected error (invalid fd flags)!\n");
353 printf("Expected error checking passed\n");
369 d
= opendir(DEVPATH
);
371 printf("No %s directory?\n", DEVPATH
);
375 while ((dir
= readdir(d
)) != NULL
) {
376 if (!strncmp(dir
->d_name
, ".", 2))
378 if (!strncmp(dir
->d_name
, "..", 3))
381 ret
= test_alloc_and_import(dir
->d_name
);
385 ret
= test_alloc_compat(dir
->d_name
);
389 ret
= test_alloc_errors(dir
->d_name
);