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 int dmabuf_sync(int fd, int start_stop)
135 struct dma_buf_sync sync = {
136 .flags = start_stop | DMA_BUF_SYNC_RW,
139 return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
142 #define ONE_MEG (1024 * 1024)
144 static int test_alloc_and_import(char *heap_name)
146 int heap_fd = -1, dmabuf_fd = -1, importer_fd = -1;
151 heap_fd = dmabuf_heap_open(heap_name);
155 printf(" Testing allocation and importing: ");
156 ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);
158 printf("FAIL (Allocation Failed!)\n");
162 /* mmap and write a simple pattern */
165 PROT_READ | PROT_WRITE,
169 if (p == MAP_FAILED) {
170 printf("FAIL (mmap() failed)\n");
175 dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START);
176 memset(p, 1, ONE_MEG / 2);
177 memset((char *)p + ONE_MEG / 2, 0, ONE_MEG / 2);
178 dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END);
180 importer_fd = open_vgem();
181 if (importer_fd < 0) {
183 printf("(Could not open vgem - skipping): ");
185 ret = import_vgem_fd(importer_fd, dmabuf_fd, &handle);
187 printf("FAIL (Failed to import buffer)\n");
192 ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START);
194 printf("FAIL (DMA_BUF_SYNC_START failed!)\n");
198 memset(p, 0xff, ONE_MEG);
199 ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END);
201 printf("FAIL (DMA_BUF_SYNC_END failed!)\n");
205 close_handle(importer_fd, handle);
211 if (importer_fd >= 0)
221 static int test_alloc_zeroed(char *heap_name, size_t size)
223 int heap_fd = -1, dmabuf_fd[32];
228 printf(" Testing alloced %ldk buffers are zeroed: ", size / 1024);
229 heap_fd = dmabuf_heap_open(heap_name);
233 /* Allocate and fill a bunch of buffers */
234 for (i = 0; i < 32; i++) {
235 ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]);
237 printf("FAIL (Allocation (%i) failed)\n", i);
240 /* mmap and fill with simple pattern */
241 p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0);
242 if (p == MAP_FAILED) {
243 printf("FAIL (mmap() failed!)\n");
247 dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_START);
248 memset(p, 0xff, size);
249 dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END);
253 for (i = 0; i < 32; i++)
256 /* Allocate and validate all buffers are zeroed */
257 for (i = 0; i < 32; i++) {
258 ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]);
260 printf("FAIL (Allocation (%i) failed)\n", i);
264 /* mmap and validate everything is zero */
265 p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0);
266 if (p == MAP_FAILED) {
267 printf("FAIL (mmap() failed!)\n");
271 dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_START);
273 for (j = 0; j < size; j++) {
275 printf("FAIL (Allocated buffer not zeroed @ %i)\n", j);
279 dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END);
283 for (i = 0; i < 32; i++)
299 /* Test the ioctl version compatibility w/ a smaller structure then expected */
300 static int dmabuf_heap_alloc_older(int fd, size_t len, unsigned int flags,
304 unsigned int older_alloc_ioctl;
305 struct dma_heap_allocation_data_smaller {
312 .fd_flags = O_RDWR | O_CLOEXEC,
315 older_alloc_ioctl = _IOWR(DMA_HEAP_IOC_MAGIC, 0x0,
316 struct dma_heap_allocation_data_smaller);
320 ret = ioctl(fd, older_alloc_ioctl, &data);
323 *dmabuf_fd = (int)data.fd;
327 /* Test the ioctl version compatibility w/ a larger structure then expected */
328 static int dmabuf_heap_alloc_newer(int fd, size_t len, unsigned int flags,
332 unsigned int newer_alloc_ioctl;
333 struct dma_heap_allocation_data_bigger {
344 .fd_flags = O_RDWR | O_CLOEXEC,
346 .garbage1 = 0xffffffff,
347 .garbage2 = 0x88888888,
348 .garbage3 = 0x11111111,
351 newer_alloc_ioctl = _IOWR(DMA_HEAP_IOC_MAGIC, 0x0,
352 struct dma_heap_allocation_data_bigger);
356 ret = ioctl(fd, newer_alloc_ioctl, &data);
360 *dmabuf_fd = (int)data.fd;
364 static int test_alloc_compat(char *heap_name)
366 int heap_fd = -1, dmabuf_fd = -1;
369 heap_fd = dmabuf_heap_open(heap_name);
373 printf(" Testing (theoretical)older alloc compat: ");
374 ret = dmabuf_heap_alloc_older(heap_fd, ONE_MEG, 0, &dmabuf_fd);
376 printf("FAIL (Older compat allocation failed!)\n");
383 printf(" Testing (theoretical)newer alloc compat: ");
384 ret = dmabuf_heap_alloc_newer(heap_fd, ONE_MEG, 0, &dmabuf_fd);
386 printf("FAIL (Newer compat allocation failed!)\n");
400 static int test_alloc_errors(char *heap_name)
402 int heap_fd = -1, dmabuf_fd = -1;
405 heap_fd = dmabuf_heap_open(heap_name);
409 printf(" Testing expected error cases: ");
410 ret = dmabuf_heap_alloc(0, ONE_MEG, 0x111111, &dmabuf_fd);
412 printf("FAIL (Did not see expected error (invalid fd)!)\n");
417 ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0x111111, &dmabuf_fd);
419 printf("FAIL (Did not see expected error (invalid heap flags)!)\n");
424 ret = dmabuf_heap_alloc_fdflags(heap_fd, ONE_MEG,
425 ~(O_RDWR | O_CLOEXEC), 0, &dmabuf_fd);
427 printf("FAIL (Did not see expected error (invalid fd flags)!)\n");
449 d = opendir(DEVPATH);
451 printf("No %s directory?\n", DEVPATH);
455 while ((dir = readdir(d)) != NULL) {
456 if (!strncmp(dir->d_name, ".", 2))
458 if (!strncmp(dir->d_name, "..", 3))
461 printf("Testing heap: %s\n", dir->d_name);
462 printf("=======================================\n");
463 ret = test_alloc_and_import(dir->d_name);
467 ret = test_alloc_zeroed(dir->d_name, 4 * 1024);
471 ret = test_alloc_zeroed(dir->d_name, ONE_MEG);
475 ret = test_alloc_compat(dir->d_name);
479 ret = test_alloc_errors(dir->d_name);