aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaura Abbott <labbott@redhat.com>2018-02-15 20:24:45 -0500
committerShuah Khan <shuahkh@osg.samsung.com>2018-03-05 20:56:24 -0500
commitac93f7046a5371ff08268965c9a696bf97bec105 (patch)
treeafcfa4adb7356a0212871f7d4595a593908c52d6
parent829dd7d642c90b641f14aed03a722a4d3e058083 (diff)
selftests: ion: Add simple test with the vgem driver
Ion is designed to be a framework used by other clients who perform operations on the buffer. Use the DRM vgem client as a simple consumer. In conjunction with the dma-buf sync ioctls, this tests the full attach/map path for the system heap. Signed-off-by: Laura Abbott <labbott@redhat.com> Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch> Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>
-rw-r--r--tools/testing/selftests/android/ion/Makefile3
-rw-r--r--tools/testing/selftests/android/ion/config1
-rw-r--r--tools/testing/selftests/android/ion/ionmap_test.c136
3 files changed, 139 insertions, 1 deletions
diff --git a/tools/testing/selftests/android/ion/Makefile b/tools/testing/selftests/android/ion/Makefile
index 96e0c448b39d..d23b6d537d8b 100644
--- a/tools/testing/selftests/android/ion/Makefile
+++ b/tools/testing/selftests/android/ion/Makefile
@@ -2,7 +2,7 @@
2INCLUDEDIR := -I. -I../../../../../drivers/staging/android/uapi/ 2INCLUDEDIR := -I. -I../../../../../drivers/staging/android/uapi/
3CFLAGS := $(CFLAGS) $(INCLUDEDIR) -Wall -O2 -g 3CFLAGS := $(CFLAGS) $(INCLUDEDIR) -Wall -O2 -g
4 4
5TEST_GEN_FILES := ionapp_export ionapp_import 5TEST_GEN_FILES := ionapp_export ionapp_import ionmap_test
6 6
7all: $(TEST_GEN_FILES) 7all: $(TEST_GEN_FILES)
8 8
@@ -14,3 +14,4 @@ include ../../lib.mk
14 14
15$(OUTPUT)/ionapp_export: ionapp_export.c ipcsocket.c ionutils.c 15$(OUTPUT)/ionapp_export: ionapp_export.c ipcsocket.c ionutils.c
16$(OUTPUT)/ionapp_import: ionapp_import.c ipcsocket.c ionutils.c 16$(OUTPUT)/ionapp_import: ionapp_import.c ipcsocket.c ionutils.c
17$(OUTPUT)/ionmap_test: ionmap_test.c ionutils.c
diff --git a/tools/testing/selftests/android/ion/config b/tools/testing/selftests/android/ion/config
index 19db6ca9aa2b..b4ad748a9dd9 100644
--- a/tools/testing/selftests/android/ion/config
+++ b/tools/testing/selftests/android/ion/config
@@ -2,3 +2,4 @@ CONFIG_ANDROID=y
2CONFIG_STAGING=y 2CONFIG_STAGING=y
3CONFIG_ION=y 3CONFIG_ION=y
4CONFIG_ION_SYSTEM_HEAP=y 4CONFIG_ION_SYSTEM_HEAP=y
5CONFIG_DRM_VGEM=y
diff --git a/tools/testing/selftests/android/ion/ionmap_test.c b/tools/testing/selftests/android/ion/ionmap_test.c
new file mode 100644
index 000000000000..dab36b06b37d
--- /dev/null
+++ b/tools/testing/selftests/android/ion/ionmap_test.c
@@ -0,0 +1,136 @@
1#include <errno.h>
2#include <fcntl.h>
3#include <stdio.h>
4#include <stdint.h>
5#include <string.h>
6#include <unistd.h>
7
8#include <sys/ioctl.h>
9#include <sys/types.h>
10#include <sys/stat.h>
11
12#include <linux/dma-buf.h>
13
14#include <drm/drm.h>
15
16#include "ion.h"
17#include "ionutils.h"
18
19int check_vgem(int fd)
20{
21 drm_version_t version = { 0 };
22 char name[5];
23 int ret;
24
25 version.name_len = 4;
26 version.name = name;
27
28 ret = ioctl(fd, DRM_IOCTL_VERSION, &version);
29 if (ret)
30 return 1;
31
32 return strcmp(name, "vgem");
33}
34
35int open_vgem(void)
36{
37 int i, fd;
38 const char *drmstr = "/dev/dri/card";
39
40 fd = -1;
41 for (i = 0; i < 16; i++) {
42 char name[80];
43
44 sprintf(name, "%s%u", drmstr, i);
45
46 fd = open(name, O_RDWR);
47 if (fd < 0)
48 continue;
49
50 if (check_vgem(fd)) {
51 close(fd);
52 continue;
53 } else {
54 break;
55 }
56
57 }
58 return fd;
59}
60
61int import_vgem_fd(int vgem_fd, int dma_buf_fd, uint32_t *handle)
62{
63 struct drm_prime_handle import_handle = { 0 };
64 int ret;
65
66 import_handle.fd = dma_buf_fd;
67 import_handle.flags = 0;
68 import_handle.handle = 0;
69
70 ret = ioctl(vgem_fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &import_handle);
71 if (ret == 0)
72 *handle = import_handle.handle;
73 return ret;
74}
75
76void close_handle(int vgem_fd, uint32_t handle)
77{
78 struct drm_gem_close close = { 0 };
79
80 close.handle = handle;
81 ioctl(vgem_fd, DRM_IOCTL_GEM_CLOSE, &close);
82}
83
84int main()
85{
86 int ret, vgem_fd;
87 struct ion_buffer_info info;
88 uint32_t handle = 0;
89 struct dma_buf_sync sync = { 0 };
90
91 info.heap_type = ION_HEAP_TYPE_SYSTEM;
92 info.heap_size = 4096;
93 info.flag_type = ION_FLAG_CACHED;
94
95 ret = ion_export_buffer_fd(&info);
96 if (ret < 0) {
97 printf("ion buffer alloc failed\n");
98 return -1;
99 }
100
101 vgem_fd = open_vgem();
102 if (vgem_fd < 0) {
103 ret = vgem_fd;
104 printf("Failed to open vgem\n");
105 goto out_ion;
106 }
107
108 ret = import_vgem_fd(vgem_fd, info.buffd, &handle);
109
110 if (ret < 0) {
111 printf("Failed to import buffer\n");
112 goto out_vgem;
113 }
114
115 sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW;
116 ret = ioctl(info.buffd, DMA_BUF_IOCTL_SYNC, &sync);
117 if (ret)
118 printf("sync start failed %d\n", errno);
119
120 memset(info.buffer, 0xff, 4096);
121
122 sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW;
123 ret = ioctl(info.buffd, DMA_BUF_IOCTL_SYNC, &sync);
124 if (ret)
125 printf("sync end failed %d\n", errno);
126
127 close_handle(vgem_fd, handle);
128 ret = 0;
129
130out_vgem:
131 close(vgem_fd);
132out_ion:
133 ion_close_buffer_fd(&info);
134 printf("done.\n");
135 return ret;
136}