Chris Wilson | c344044 | 2016-06-18 00:42:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include "igt.h" |
| 25 | #include "igt_vgem.h" |
Chris Wilson | 8deba38 | 2016-06-20 19:21:26 +0100 | [diff] [blame] | 26 | #include "igt_debugfs.h" |
| 27 | #include "igt_sysfs.h" |
Chris Wilson | c344044 | 2016-06-18 00:42:19 +0100 | [diff] [blame] | 28 | |
| 29 | #include <sys/mman.h> |
Chris Wilson | 8deba38 | 2016-06-20 19:21:26 +0100 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | #include <dirent.h> |
Chris Wilson | c344044 | 2016-06-18 00:42:19 +0100 | [diff] [blame] | 32 | |
| 33 | IGT_TEST_DESCRIPTION("Basic sanity check of Virtual GEM module (vGEM)."); |
| 34 | |
Chris Wilson | 68c5d53 | 2016-06-22 09:10:33 +0100 | [diff] [blame] | 35 | static void test_client(int fd) |
| 36 | { |
| 37 | close(drm_open_driver(DRIVER_VGEM)); |
| 38 | close(drm_open_driver_render(DRIVER_VGEM)); |
| 39 | } |
| 40 | |
Chris Wilson | c344044 | 2016-06-18 00:42:19 +0100 | [diff] [blame] | 41 | static void test_create(int fd) |
| 42 | { |
| 43 | struct vgem_bo bo; |
| 44 | |
| 45 | bo.width = 0; |
| 46 | bo.height = 0; |
| 47 | bo.bpp = 0; |
| 48 | igt_assert_eq(__vgem_create(fd, &bo), -EINVAL); |
| 49 | |
| 50 | bo.width = 1; |
| 51 | bo.height = 1; |
| 52 | bo.bpp = 1; |
| 53 | vgem_create(fd, &bo); |
| 54 | igt_assert_eq(bo.size, 4096); |
| 55 | gem_close(fd, bo.handle); |
| 56 | |
| 57 | bo.width = 1024; |
| 58 | bo.height = 1024; |
| 59 | bo.bpp = 8; |
| 60 | vgem_create(fd, &bo); |
| 61 | igt_assert_eq(bo.size, 1<<20); |
| 62 | gem_close(fd, bo.handle); |
| 63 | |
| 64 | bo.width = 1<<15; |
| 65 | bo.height = 1<<15; |
| 66 | bo.bpp = 16; |
| 67 | vgem_create(fd, &bo); |
| 68 | igt_assert_eq(bo.size, 1<<31); |
| 69 | gem_close(fd, bo.handle); |
| 70 | } |
| 71 | |
| 72 | static void test_mmap(int fd) |
| 73 | { |
| 74 | struct vgem_bo bo; |
| 75 | uint32_t *ptr; |
| 76 | |
| 77 | bo.width = 1024; |
| 78 | bo.height = 1024; |
| 79 | bo.bpp = 32; |
| 80 | vgem_create(fd, &bo); |
| 81 | |
| 82 | ptr = vgem_mmap(fd, &bo, PROT_WRITE); |
| 83 | gem_close(fd, bo.handle); |
| 84 | |
| 85 | for (int page = 0; page < bo.size >> 12; page++) |
| 86 | ptr[page] = 0; |
| 87 | |
| 88 | munmap(ptr, bo.size); |
| 89 | } |
| 90 | |
| 91 | static bool has_prime_import(int fd) |
| 92 | { |
| 93 | uint64_t value; |
| 94 | |
| 95 | if (drmGetCap(fd, DRM_CAP_PRIME, &value)) |
| 96 | return false; |
| 97 | |
| 98 | return value & DRM_PRIME_CAP_IMPORT; |
| 99 | } |
| 100 | |
| 101 | static void test_dmabuf_export(int fd) |
| 102 | { |
| 103 | struct vgem_bo bo; |
| 104 | uint32_t handle; |
| 105 | int other; |
| 106 | int dmabuf; |
| 107 | |
| 108 | other = drm_open_driver(DRIVER_ANY); |
| 109 | igt_require(has_prime_import(other)); |
| 110 | |
| 111 | bo.width = 1024; |
| 112 | bo.height = 1; |
| 113 | bo.bpp = 32; |
| 114 | |
| 115 | vgem_create(fd, &bo); |
| 116 | dmabuf = prime_handle_to_fd(fd, bo.handle); |
| 117 | gem_close(fd, bo.handle); |
| 118 | |
| 119 | handle = prime_fd_to_handle(other, dmabuf); |
| 120 | close(dmabuf); |
| 121 | gem_close(other, handle); |
| 122 | close(other); |
| 123 | } |
| 124 | |
| 125 | static void test_dmabuf_mmap(int fd) |
| 126 | { |
| 127 | struct vgem_bo bo; |
| 128 | uint32_t *ptr; |
| 129 | int export; |
| 130 | |
| 131 | bo.width = 1024; |
| 132 | bo.height = 1024; |
| 133 | bo.bpp = 32; |
| 134 | vgem_create(fd, &bo); |
| 135 | |
| 136 | export = prime_handle_to_fd_for_mmap(fd, bo.handle); |
| 137 | ptr = mmap(NULL, bo.size, PROT_WRITE, MAP_SHARED, export, 0); |
| 138 | close(export); |
| 139 | igt_assert(ptr != MAP_FAILED); |
| 140 | |
| 141 | for (int page = 0; page < bo.size >> 12; page++) |
| 142 | ptr[page] = page; |
| 143 | munmap(ptr, bo.size); |
| 144 | |
| 145 | ptr = vgem_mmap(fd, &bo, PROT_READ); |
| 146 | gem_close(fd, bo.handle); |
| 147 | |
| 148 | for (int page = 0; page < bo.size >> 12; page++) |
| 149 | igt_assert_eq(ptr[page], page); |
| 150 | munmap(ptr, bo.size); |
| 151 | } |
| 152 | |
Chris Wilson | 8deba38 | 2016-06-20 19:21:26 +0100 | [diff] [blame] | 153 | static void test_sysfs_read(int fd) |
| 154 | { |
| 155 | int dir = igt_sysfs_open(fd, NULL); |
| 156 | DIR *dirp = fdopendir(dir); |
| 157 | struct dirent *de; |
| 158 | |
| 159 | while ((de = readdir(dirp))) { |
| 160 | struct stat st; |
| 161 | |
| 162 | if (*de->d_name == '.') |
| 163 | continue; |
| 164 | |
| 165 | if (fstatat(dir, de->d_name, &st, 0)) |
| 166 | continue; |
| 167 | |
| 168 | if (S_ISDIR(st.st_mode)) |
| 169 | continue; |
| 170 | |
| 171 | igt_debug("Reading %s\n", de->d_name); |
| 172 | igt_set_timeout(1, "vgem sysfs read stalled"); |
| 173 | free(igt_sysfs_get(dir, de->d_name)); |
| 174 | igt_reset_timeout(); |
| 175 | } |
| 176 | |
| 177 | closedir(dirp); |
| 178 | close(dir); |
| 179 | } |
| 180 | |
| 181 | static void test_debugfs_read(int fd) |
| 182 | { |
| 183 | int dir = igt_debugfs_dir(fd); |
| 184 | DIR *dirp = fdopendir(dir); |
| 185 | struct dirent *de; |
| 186 | |
Chris Wilson | 2962c51 | 2016-07-09 09:04:05 +0100 | [diff] [blame^] | 187 | igt_assert(dirp); |
Chris Wilson | 8deba38 | 2016-06-20 19:21:26 +0100 | [diff] [blame] | 188 | while ((de = readdir(dirp))) { |
| 189 | struct stat st; |
| 190 | |
| 191 | if (*de->d_name == '.') |
| 192 | continue; |
| 193 | |
| 194 | if (fstatat(dir, de->d_name, &st, 0)) |
| 195 | continue; |
| 196 | |
| 197 | if (S_ISDIR(st.st_mode)) |
| 198 | continue; |
| 199 | |
| 200 | igt_debug("Reading %s\n", de->d_name); |
| 201 | igt_set_timeout(1, "vgem debugfs read stalled"); |
| 202 | free(igt_sysfs_get(dir, de->d_name)); |
| 203 | igt_reset_timeout(); |
| 204 | } |
| 205 | |
| 206 | closedir(dirp); |
| 207 | close(dir); |
| 208 | } |
| 209 | |
Chris Wilson | c344044 | 2016-06-18 00:42:19 +0100 | [diff] [blame] | 210 | static bool has_prime_export(int fd) |
| 211 | { |
| 212 | uint64_t value; |
| 213 | |
| 214 | if (drmGetCap(fd, DRM_CAP_PRIME, &value)) |
| 215 | return false; |
| 216 | |
| 217 | return value & DRM_PRIME_CAP_EXPORT; |
| 218 | } |
| 219 | |
| 220 | igt_main |
| 221 | { |
| 222 | int fd = -1; |
| 223 | |
| 224 | igt_fixture { |
| 225 | fd = drm_open_driver(DRIVER_VGEM); |
| 226 | } |
| 227 | |
Chris Wilson | 68c5d53 | 2016-06-22 09:10:33 +0100 | [diff] [blame] | 228 | igt_subtest_f("second-client") |
| 229 | test_client(fd); |
| 230 | |
Chris Wilson | c344044 | 2016-06-18 00:42:19 +0100 | [diff] [blame] | 231 | igt_subtest_f("create") |
| 232 | test_create(fd); |
| 233 | |
| 234 | igt_subtest_f("mmap") |
| 235 | test_mmap(fd); |
| 236 | |
| 237 | igt_subtest_group { |
| 238 | igt_fixture { |
| 239 | igt_require(has_prime_export(fd)); |
| 240 | } |
| 241 | |
| 242 | igt_subtest_f("dmabuf-export") |
| 243 | test_dmabuf_export(fd); |
| 244 | |
| 245 | igt_subtest_f("dmabuf-mmap") |
| 246 | test_dmabuf_mmap(fd); |
| 247 | } |
| 248 | |
Chris Wilson | 8deba38 | 2016-06-20 19:21:26 +0100 | [diff] [blame] | 249 | igt_subtest("sysfs") |
| 250 | test_sysfs_read(fd); |
| 251 | igt_subtest("debugfs") |
| 252 | test_debugfs_read(fd); |
| 253 | |
Chris Wilson | c344044 | 2016-06-18 00:42:19 +0100 | [diff] [blame] | 254 | igt_fixture { |
| 255 | close(fd); |
| 256 | } |
| 257 | } |