1 From 31501b71a0237f3753d0210e3e122c548d6f3051 Mon Sep 17 00:00:00 2001
2 From: John Stultz <john.stultz@linaro.org>
3 Date: Tue, 3 Dec 2019 17:26:41 +0000
4 Subject: [PATCH] kselftests: Add dma-heap test
6 Commit a8779927fd86c91f5400bfcbccfa018a667d8350 upstream.
8 Add very trivial allocation and import test for dma-heaps,
9 utilizing the vgem driver as a test importer.
11 A good chunk of this code taken from:
12 tools/testing/selftests/android/ion/ionmap_test.c
13 Originally by Laura Abbott <labbott@redhat.com>
15 Cc: Benjamin Gaignard <benjamin.gaignard@linaro.org>
16 Cc: Sumit Semwal <sumit.semwal@linaro.org>
17 Cc: Liam Mark <lmark@codeaurora.org>
18 Cc: Pratik Patel <pratikp@codeaurora.org>
19 Cc: Brian Starkey <Brian.Starkey@arm.com>
20 Cc: Vincent Donnefort <Vincent.Donnefort@arm.com>
21 Cc: Sudipto Paul <Sudipto.Paul@arm.com>
22 Cc: Andrew F. Davis <afd@ti.com>
23 Cc: Christoph Hellwig <hch@infradead.org>
24 Cc: Chenbo Feng <fengc@google.com>
25 Cc: Alistair Strachan <astrachan@google.com>
26 Cc: Hridya Valsaraju <hridya@google.com>
27 Cc: Sandeep Patil <sspatil@google.com>
28 Cc: Hillf Danton <hdanton@sina.com>
29 Cc: Dave Airlie <airlied@gmail.com>
30 Cc: dri-devel@lists.freedesktop.org
31 Reviewed-by: Benjamin Gaignard <benjamin.gaignard@linaro.org>
32 Reviewed-by: Brian Starkey <brian.starkey@arm.com>
33 Acked-by: Sandeep Patil <sspatil@android.com>
34 Acked-by: Laura Abbott <labbott@redhat.com>
35 Tested-by: Ayan Kumar Halder <ayan.halder@arm.com>
36 Signed-off-by: John Stultz <john.stultz@linaro.org>
37 Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
38 Link: https://patchwork.freedesktop.org/patch/msgid/20191203172641.66642-6-john.stultz@linaro.org
40 tools/testing/selftests/dmabuf-heaps/Makefile | 6 +
41 .../selftests/dmabuf-heaps/dmabuf-heap.c | 396 ++++++++++++++++++
42 2 files changed, 402 insertions(+)
43 create mode 100644 tools/testing/selftests/dmabuf-heaps/Makefile
44 create mode 100644 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
47 +++ b/tools/testing/selftests/dmabuf-heaps/Makefile
49 +# SPDX-License-Identifier: GPL-2.0
50 +CFLAGS += -static -O3 -Wl,-no-as-needed -Wall -I../../../../usr/include
52 +TEST_GEN_PROGS = dmabuf-heap
56 +++ b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
58 +// SPDX-License-Identifier: GPL-2.0
68 +#include <sys/ioctl.h>
69 +#include <sys/mman.h>
70 +#include <sys/types.h>
72 +#include <linux/dma-buf.h>
75 +#include "../../../../include/uapi/linux/dma-heap.h"
77 +#define DEVPATH "/dev/dma_heap"
79 +static int check_vgem(int fd)
81 + drm_version_t version = { 0 };
85 + version.name_len = 4;
86 + version.name = name;
88 + ret = ioctl(fd, DRM_IOCTL_VERSION, &version);
92 + return !strcmp(name, "vgem");
95 +static int open_vgem(void)
98 + const char *drmstr = "/dev/dri/card";
101 + for (i = 0; i < 16; i++) {
104 + snprintf(name, 80, "%s%u", drmstr, i);
106 + fd = open(name, O_RDWR);
110 + if (!check_vgem(fd)) {
121 +static int import_vgem_fd(int vgem_fd, int dma_buf_fd, uint32_t *handle)
123 + struct drm_prime_handle import_handle = {
130 + ret = ioctl(vgem_fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &import_handle);
132 + *handle = import_handle.handle;
136 +static void close_handle(int vgem_fd, uint32_t handle)
138 + struct drm_gem_close close = {
142 + ioctl(vgem_fd, DRM_IOCTL_GEM_CLOSE, &close);
145 +static int dmabuf_heap_open(char *name)
150 + ret = snprintf(buf, 256, "%s/%s", DEVPATH, name);
152 + printf("snprintf failed!\n");
156 + fd = open(buf, O_RDWR);
158 + printf("open %s failed!\n", buf);
162 +static int dmabuf_heap_alloc_fdflags(int fd, size_t len, unsigned int fd_flags,
163 + unsigned int heap_flags, int *dmabuf_fd)
165 + struct dma_heap_allocation_data data = {
168 + .fd_flags = fd_flags,
169 + .heap_flags = heap_flags,
176 + ret = ioctl(fd, DMA_HEAP_IOC_ALLOC, &data);
179 + *dmabuf_fd = (int)data.fd;
183 +static int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags,
186 + return dmabuf_heap_alloc_fdflags(fd, len, O_RDWR | O_CLOEXEC, flags,
190 +static void dmabuf_sync(int fd, int start_stop)
192 + struct dma_buf_sync sync = {
193 + .flags = start_stop | DMA_BUF_SYNC_RW,
197 + ret = ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
199 + printf("sync failed %d\n", errno);
202 +#define ONE_MEG (1024 * 1024)
204 +static int test_alloc_and_import(char *heap_name)
206 + int heap_fd = -1, dmabuf_fd = -1, importer_fd = -1;
207 + uint32_t handle = 0;
211 + printf("Testing heap: %s\n", heap_name);
213 + heap_fd = dmabuf_heap_open(heap_name);
217 + printf("Allocating 1 MEG\n");
218 + ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);
220 + printf("Allocation Failed!\n");
224 + /* mmap and write a simple pattern */
227 + PROT_READ | PROT_WRITE,
231 + if (p == MAP_FAILED) {
232 + printf("mmap() failed: %m\n");
236 + printf("mmap passed\n");
238 + dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START);
239 + memset(p, 1, ONE_MEG / 2);
240 + memset((char *)p + ONE_MEG / 2, 0, ONE_MEG / 2);
241 + dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END);
243 + importer_fd = open_vgem();
244 + if (importer_fd < 0) {
246 + printf("Failed to open vgem\n");
250 + ret = import_vgem_fd(importer_fd, dmabuf_fd, &handle);
252 + printf("Failed to import buffer\n");
255 + printf("import passed\n");
257 + dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START);
258 + memset(p, 0xff, ONE_MEG);
259 + dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END);
260 + printf("syncs passed\n");
262 + close_handle(importer_fd, handle);
267 + munmap(p, ONE_MEG);
268 + if (importer_fd >= 0)
269 + close(importer_fd);
270 + if (dmabuf_fd >= 0)
278 +/* Test the ioctl version compatibility w/ a smaller structure then expected */
279 +static int dmabuf_heap_alloc_older(int fd, size_t len, unsigned int flags,
283 + unsigned int older_alloc_ioctl;
284 + struct dma_heap_allocation_data_smaller {
291 + .fd_flags = O_RDWR | O_CLOEXEC,
294 + older_alloc_ioctl = _IOWR(DMA_HEAP_IOC_MAGIC, 0x0,
295 + struct dma_heap_allocation_data_smaller);
299 + ret = ioctl(fd, older_alloc_ioctl, &data);
302 + *dmabuf_fd = (int)data.fd;
306 +/* Test the ioctl version compatibility w/ a larger structure then expected */
307 +static int dmabuf_heap_alloc_newer(int fd, size_t len, unsigned int flags,
311 + unsigned int newer_alloc_ioctl;
312 + struct dma_heap_allocation_data_bigger {
323 + .fd_flags = O_RDWR | O_CLOEXEC,
324 + .heap_flags = flags,
325 + .garbage1 = 0xffffffff,
326 + .garbage2 = 0x88888888,
327 + .garbage3 = 0x11111111,
330 + newer_alloc_ioctl = _IOWR(DMA_HEAP_IOC_MAGIC, 0x0,
331 + struct dma_heap_allocation_data_bigger);
335 + ret = ioctl(fd, newer_alloc_ioctl, &data);
339 + *dmabuf_fd = (int)data.fd;
343 +static int test_alloc_compat(char *heap_name)
345 + int heap_fd = -1, dmabuf_fd = -1;
348 + heap_fd = dmabuf_heap_open(heap_name);
352 + printf("Testing (theoretical)older alloc compat\n");
353 + ret = dmabuf_heap_alloc_older(heap_fd, ONE_MEG, 0, &dmabuf_fd);
355 + printf("Older compat allocation failed!\n");
361 + printf("Testing (theoretical)newer alloc compat\n");
362 + ret = dmabuf_heap_alloc_newer(heap_fd, ONE_MEG, 0, &dmabuf_fd);
364 + printf("Newer compat allocation failed!\n");
368 + printf("Ioctl compatibility tests passed\n");
370 + if (dmabuf_fd >= 0)
378 +static int test_alloc_errors(char *heap_name)
380 + int heap_fd = -1, dmabuf_fd = -1;
383 + heap_fd = dmabuf_heap_open(heap_name);
387 + printf("Testing expected error cases\n");
388 + ret = dmabuf_heap_alloc(0, ONE_MEG, 0x111111, &dmabuf_fd);
390 + printf("Did not see expected error (invalid fd)!\n");
395 + ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0x111111, &dmabuf_fd);
397 + printf("Did not see expected error (invalid heap flags)!\n");
402 + ret = dmabuf_heap_alloc_fdflags(heap_fd, ONE_MEG,
403 + ~(O_RDWR | O_CLOEXEC), 0, &dmabuf_fd);
405 + printf("Did not see expected error (invalid fd flags)!\n");
410 + printf("Expected error checking passed\n");
412 + if (dmabuf_fd >= 0)
423 + struct dirent *dir;
426 + d = opendir(DEVPATH);
428 + printf("No %s directory?\n", DEVPATH);
432 + while ((dir = readdir(d)) != NULL) {
433 + if (!strncmp(dir->d_name, ".", 2))
435 + if (!strncmp(dir->d_name, "..", 3))
438 + ret = test_alloc_and_import(dir->d_name);
442 + ret = test_alloc_compat(dir->d_name);
446 + ret = test_alloc_errors(dir->d_name);