[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1553818562-2516-7-git-send-email-john.stultz@linaro.org>
Date: Thu, 28 Mar 2019 17:16:02 -0700
From: John Stultz <john.stultz@...aro.org>
To: lkml <linux-kernel@...r.kernel.org>
Cc: John Stultz <john.stultz@...aro.org>,
Benjamin Gaignard <benjamin.gaignard@...aro.org>,
Sumit Semwal <sumit.semwal@...aro.org>,
Liam Mark <lmark@...eaurora.org>,
Pratik Patel <pratikp@...eaurora.org>,
Brian Starkey <Brian.Starkey@....com>,
Vincent Donnefort <Vincent.Donnefort@....com>,
Sudipto Paul <Sudipto.Paul@....com>,
"Andrew F . Davis" <afd@...com>,
Xu YiPing <xuyiping@...ilicon.com>,
"Chenfeng (puck)" <puck.chen@...ilicon.com>,
butao <butao@...ilicon.com>,
"Xiaqing (A)" <saberlily.xia@...ilicon.com>,
Yudongbin <yudongbin@...ilicon.com>,
Christoph Hellwig <hch@...radead.org>,
Chenbo Feng <fengc@...gle.com>,
Alistair Strachan <astrachan@...gle.com>,
dri-devel@...ts.freedesktop.org
Subject: [RFC][PATCH 6/6 v3] kselftests: Add dma-heap test
Add very trivial allocation and import test for dma-heaps.
TODO: Still needs improvements
Cc: Benjamin Gaignard <benjamin.gaignard@...aro.org>
Cc: Sumit Semwal <sumit.semwal@...aro.org>
Cc: Liam Mark <lmark@...eaurora.org>
Cc: Pratik Patel <pratikp@...eaurora.org>
Cc: Brian Starkey <Brian.Starkey@....com>
Cc: Vincent Donnefort <Vincent.Donnefort@....com>
Cc: Sudipto Paul <Sudipto.Paul@....com>
Cc: Andrew F. Davis <afd@...com>
Cc: Xu YiPing <xuyiping@...ilicon.com>
Cc: "Chenfeng (puck)" <puck.chen@...ilicon.com>
Cc: butao <butao@...ilicon.com>
Cc: "Xiaqing (A)" <saberlily.xia@...ilicon.com>
Cc: Yudongbin <yudongbin@...ilicon.com>
Cc: Christoph Hellwig <hch@...radead.org>
Cc: Chenbo Feng <fengc@...gle.com>
Cc: Alistair Strachan <astrachan@...gle.com>
Cc: dri-devel@...ts.freedesktop.org
Signed-off-by: John Stultz <john.stultz@...aro.org>
---
v2: Switched to use reworked dma-heap apis
v3:
* Add simple mmap
* Utilize dma-buf testdev to test importing
---
tools/testing/selftests/dmabuf-heaps/Makefile | 11 ++
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c | 169 +++++++++++++++++++++
2 files changed, 180 insertions(+)
create mode 100644 tools/testing/selftests/dmabuf-heaps/Makefile
create mode 100644 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
diff --git a/tools/testing/selftests/dmabuf-heaps/Makefile b/tools/testing/selftests/dmabuf-heaps/Makefile
new file mode 100644
index 0000000..c414ad3
--- /dev/null
+++ b/tools/testing/selftests/dmabuf-heaps/Makefile
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0
+CFLAGS += -static -O3 -Wl,-no-as-needed -Wall
+#LDLIBS += -lrt -lpthread -lm
+
+# these are all "safe" tests that don't modify
+# system time or require escalated privileges
+TEST_GEN_PROGS = dmabuf-heap
+
+
+include ../lib.mk
+
diff --git a/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
new file mode 100644
index 0000000..df30465
--- /dev/null
+++ b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
@@ -0,0 +1,169 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "../../../../include/uapi/linux/dma-heap.h"
+#include "../../../../include/uapi/linux/dma-buf-testdev.h"
+
+#define DEVPATH "/dev/dma_heap"
+
+int dmabuf_importer_open(char *name)
+{
+ int ret, fd;
+ char buf[256];
+
+ ret = sprintf(buf, "/dev/%s", name);
+ if (ret < 0) {
+ printf("sprintf failed!\n");
+ return ret;
+ }
+
+ fd = open(buf, O_RDWR);
+ if (fd < 0)
+ printf("open %s failed!\n", buf);
+ return fd;
+}
+
+int dmabuf_importer_import(int importer_fd, int dmabuf_fd, void *phys_addr)
+{
+ struct dma_buf_testdev_data data = {
+ .fd = dmabuf_fd,
+ .phys = 0,
+ };
+
+ int ret;
+
+ if (phys_addr == NULL)
+ return -EINVAL;
+
+ ret = ioctl(importer_fd, DMA_BUF_TESTDEV_IOC_CONVERT, &data);
+ if (ret < 0)
+ return ret;
+
+ *(__u64 *)phys_addr = data.phys;
+
+ return ret;
+}
+
+
+int dmabuf_heap_open(char *name)
+{
+ int ret, fd;
+ char buf[256];
+
+ ret = sprintf(buf, "%s/%s", DEVPATH, name);
+ if (ret < 0) {
+ printf("sprintf failed!\n");
+ return ret;
+ }
+
+ fd = open(buf, O_RDWR);
+ if (fd < 0)
+ printf("open %s failed!\n", buf);
+ return fd;
+}
+
+int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags, int *dmabuf_fd)
+{
+ struct dma_heap_allocation_data data = {
+ .len = len,
+ .flags = flags,
+ };
+ int ret;
+
+ if (dmabuf_fd == NULL)
+ return -EINVAL;
+
+ ret = ioctl(fd, DMA_HEAP_IOC_ALLOC, &data);
+ if (ret < 0)
+ return ret;
+ *dmabuf_fd = (int)data.fd;
+ return ret;
+}
+
+#define ONE_MEG (1024*1024)
+
+void do_test(char *heap_name)
+{
+ int heap_fd = -1, dmabuf_fd = -1, importer_fd = -1;
+ void *p;
+ int ret;
+ long phys;
+
+ printf("Testing heap: %s\n", heap_name);
+
+ heap_fd = dmabuf_heap_open(heap_name);
+ if (heap_fd < 0)
+ return;
+
+ printf("Allocating 1 MEG\n");
+ ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);
+ if (ret)
+ goto out;
+
+
+ /* mmap and write a simple pattern */
+ p = mmap(NULL,
+ ONE_MEG,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED,
+ dmabuf_fd,
+ 0);
+ if (p == MAP_FAILED) {
+ printf("mmap() failed: %m\n");
+ abort();
+ }
+ printf("mmap passed\n");
+
+ memset(p, 1, ONE_MEG/2);
+ p = (char *)p + ONE_MEG/2;
+ memset(p, 0, ONE_MEG/2);
+ munmap(p, ONE_MEG);
+
+
+ importer_fd = dmabuf_importer_open("dma-buf-testdev");
+
+ ret = dmabuf_importer_import(importer_fd, dmabuf_fd, &phys);
+ if (ret) {
+ printf("Error importing to test device!\n");
+ goto out;
+ }
+ printf("Import passed!\n");
+
+ /* DO SOMETHING WITH THE DMABUF HERE? */
+
+out:
+ if (importer_fd >= 0)
+ close(importer_fd);
+ if (dmabuf_fd >= 0)
+ close(dmabuf_fd);
+ if (heap_fd >= 0)
+ close(heap_fd);
+}
+
+
+int main(void)
+{
+ DIR *d;
+ struct dirent *dir;
+
+ d = opendir(DEVPATH);
+ if (!d) {
+ printf("No %s directory?\n", DEVPATH);
+ return -1;
+ }
+
+ while ((dir = readdir(d)) != NULL)
+ do_test(dir->d_name);
+
+ return 0;
+}
--
2.7.4
Powered by blists - more mailing lists