1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <dirent.h> 4 #include <errno.h> 5 #include <fcntl.h> 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <stdint.h> 9 #include <string.h> 10 #include <unistd.h> 11 #include <sys/ioctl.h> 12 #include <sys/mman.h> 13 #include <sys/types.h> 14 15 #include <linux/dma-buf.h> 16 #include <drm/drm.h> 17 18 #include "../../../../include/uapi/linux/dma-heap.h" 19 20 #define DEVPATH "/dev/dma_heap" 21 22 static int check_vgem(int fd) 23 { 24 drm_version_t version = { 0 }; 25 char name[5]; 26 int ret; 27 28 version.name_len = 4; 29 version.name = name; 30 31 ret = ioctl(fd, DRM_IOCTL_VERSION, &version); 32 if (ret) 33 return 0; 34 35 return !strcmp(name, "vgem"); 36 } 37 38 static int open_vgem(void) 39 { 40 int i, fd; 41 const char *drmstr = "/dev/dri/card"; 42 43 fd = -1; 44 for (i = 0; i < 16; i++) { 45 char name[80]; 46 47 snprintf(name, 80, "%s%u", drmstr, i); 48 49 fd = open(name, O_RDWR); 50 if (fd < 0) 51 continue; 52 53 if (!check_vgem(fd)) { 54 close(fd); 55 fd = -1; 56 continue; 57 } else { 58 break; 59 } 60 } 61 return fd; 62 } 63 64 static int import_vgem_fd(int vgem_fd, int dma_buf_fd, uint32_t *handle) 65 { 66 struct drm_prime_handle import_handle = { 67 .fd = dma_buf_fd, 68 .flags = 0, 69 .handle = 0, 70 }; 71 int ret; 72 73 ret = ioctl(vgem_fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &import_handle); 74 if (ret == 0) 75 *handle = import_handle.handle; 76 return ret; 77 } 78 79 static void close_handle(int vgem_fd, uint32_t handle) 80 { 81 struct drm_gem_close close = { 82 .handle = handle, 83 }; 84 85 ioctl(vgem_fd, DRM_IOCTL_GEM_CLOSE, &close); 86 } 87 88 static int dmabuf_heap_open(char *name) 89 { 90 int ret, fd; 91 char buf[256]; 92 93 ret = snprintf(buf, 256, "%s/%s", DEVPATH, name); 94 if (ret < 0) { 95 printf("snprintf failed!\n"); 96 return ret; 97 } 98 99 fd = open(buf, O_RDWR); 100 if (fd < 0) 101 printf("open %s failed!\n", buf); 102 return fd; 103 } 104 105 static int dmabuf_heap_alloc_fdflags(int fd, size_t len, unsigned int fd_flags, 106 unsigned int heap_flags, int *dmabuf_fd) 107 { 108 struct dma_heap_allocation_data data = { 109 .len = len, 110 .fd = 0, 111 .fd_flags = fd_flags, 112 .heap_flags = heap_flags, 113 }; 114 int ret; 115 116 if (!dmabuf_fd) 117 return -EINVAL; 118 119 ret = ioctl(fd, DMA_HEAP_IOCTL_ALLOC, &data); 120 if (ret < 0) 121 return ret; 122 *dmabuf_fd = (int)data.fd; 123 return ret; 124 } 125 126 static int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags, 127 int *dmabuf_fd) 128 { 129 return dmabuf_heap_alloc_fdflags(fd, len, O_RDWR | O_CLOEXEC, flags, 130 dmabuf_fd); 131 } 132 133 static int dmabuf_sync(int fd, int start_stop) 134 { 135 struct dma_buf_sync sync = { 136 .flags = start_stop | DMA_BUF_SYNC_RW, 137 }; 138 139 return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync); 140 } 141 142 #define ONE_MEG (1024 * 1024) 143 144 static int test_alloc_and_import(char *heap_name) 145 { 146 int heap_fd = -1, dmabuf_fd = -1, importer_fd = -1; 147 uint32_t handle = 0; 148 void *p = NULL; 149 int ret; 150 151 printf("Testing heap: %s\n", heap_name); 152 153 heap_fd = dmabuf_heap_open(heap_name); 154 if (heap_fd < 0) 155 return -1; 156 157 printf("Allocating 1 MEG\n"); 158 ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd); 159 if (ret) { 160 printf("Allocation Failed!\n"); 161 ret = -1; 162 goto out; 163 } 164 /* mmap and write a simple pattern */ 165 p = mmap(NULL, 166 ONE_MEG, 167 PROT_READ | PROT_WRITE, 168 MAP_SHARED, 169 dmabuf_fd, 170 0); 171 if (p == MAP_FAILED) { 172 printf("mmap() failed: %m\n"); 173 ret = -1; 174 goto out; 175 } 176 printf("mmap passed\n"); 177 178 dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START); 179 memset(p, 1, ONE_MEG / 2); 180 memset((char *)p + ONE_MEG / 2, 0, ONE_MEG / 2); 181 dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END); 182 183 importer_fd = open_vgem(); 184 if (importer_fd < 0) { 185 ret = importer_fd; 186 printf("Failed to open vgem\n"); 187 } else { 188 ret = import_vgem_fd(importer_fd, dmabuf_fd, &handle); 189 if (ret < 0) { 190 printf("Failed to import buffer\n"); 191 goto out; 192 } 193 printf("import passed\n"); 194 } 195 196 ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START); 197 if (ret < 0) { 198 printf("Sync start failed!\n"); 199 goto out; 200 } 201 202 memset(p, 0xff, ONE_MEG); 203 ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END); 204 if (ret < 0) { 205 printf("Sync end failed!\n"); 206 goto out; 207 } 208 printf("syncs passed\n"); 209 210 close_handle(importer_fd, handle); 211 ret = 0; 212 213 out: 214 if (p) 215 munmap(p, ONE_MEG); 216 if (importer_fd >= 0) 217 close(importer_fd); 218 if (dmabuf_fd >= 0) 219 close(dmabuf_fd); 220 if (heap_fd >= 0) 221 close(heap_fd); 222 223 return ret; 224 } 225 226 /* Test the ioctl version compatibility w/ a smaller structure then expected */ 227 static int dmabuf_heap_alloc_older(int fd, size_t len, unsigned int flags, 228 int *dmabuf_fd) 229 { 230 int ret; 231 unsigned int older_alloc_ioctl; 232 struct dma_heap_allocation_data_smaller { 233 __u64 len; 234 __u32 fd; 235 __u32 fd_flags; 236 } data = { 237 .len = len, 238 .fd = 0, 239 .fd_flags = O_RDWR | O_CLOEXEC, 240 }; 241 242 older_alloc_ioctl = _IOWR(DMA_HEAP_IOC_MAGIC, 0x0, 243 struct dma_heap_allocation_data_smaller); 244 if (!dmabuf_fd) 245 return -EINVAL; 246 247 ret = ioctl(fd, older_alloc_ioctl, &data); 248 if (ret < 0) 249 return ret; 250 *dmabuf_fd = (int)data.fd; 251 return ret; 252 } 253 254 /* Test the ioctl version compatibility w/ a larger structure then expected */ 255 static int dmabuf_heap_alloc_newer(int fd, size_t len, unsigned int flags, 256 int *dmabuf_fd) 257 { 258 int ret; 259 unsigned int newer_alloc_ioctl; 260 struct dma_heap_allocation_data_bigger { 261 __u64 len; 262 __u32 fd; 263 __u32 fd_flags; 264 __u64 heap_flags; 265 __u64 garbage1; 266 __u64 garbage2; 267 __u64 garbage3; 268 } data = { 269 .len = len, 270 .fd = 0, 271 .fd_flags = O_RDWR | O_CLOEXEC, 272 .heap_flags = flags, 273 .garbage1 = 0xffffffff, 274 .garbage2 = 0x88888888, 275 .garbage3 = 0x11111111, 276 }; 277 278 newer_alloc_ioctl = _IOWR(DMA_HEAP_IOC_MAGIC, 0x0, 279 struct dma_heap_allocation_data_bigger); 280 if (!dmabuf_fd) 281 return -EINVAL; 282 283 ret = ioctl(fd, newer_alloc_ioctl, &data); 284 if (ret < 0) 285 return ret; 286 287 *dmabuf_fd = (int)data.fd; 288 return ret; 289 } 290 291 static int test_alloc_compat(char *heap_name) 292 { 293 int heap_fd = -1, dmabuf_fd = -1; 294 int ret; 295 296 heap_fd = dmabuf_heap_open(heap_name); 297 if (heap_fd < 0) 298 return -1; 299 300 printf("Testing (theoretical)older alloc compat\n"); 301 ret = dmabuf_heap_alloc_older(heap_fd, ONE_MEG, 0, &dmabuf_fd); 302 if (ret) { 303 printf("Older compat allocation failed!\n"); 304 ret = -1; 305 goto out; 306 } 307 close(dmabuf_fd); 308 309 printf("Testing (theoretical)newer alloc compat\n"); 310 ret = dmabuf_heap_alloc_newer(heap_fd, ONE_MEG, 0, &dmabuf_fd); 311 if (ret) { 312 printf("Newer compat allocation failed!\n"); 313 ret = -1; 314 goto out; 315 } 316 printf("Ioctl compatibility tests passed\n"); 317 out: 318 if (dmabuf_fd >= 0) 319 close(dmabuf_fd); 320 if (heap_fd >= 0) 321 close(heap_fd); 322 323 return ret; 324 } 325 326 static int test_alloc_errors(char *heap_name) 327 { 328 int heap_fd = -1, dmabuf_fd = -1; 329 int ret; 330 331 heap_fd = dmabuf_heap_open(heap_name); 332 if (heap_fd < 0) 333 return -1; 334 335 printf("Testing expected error cases\n"); 336 ret = dmabuf_heap_alloc(0, ONE_MEG, 0x111111, &dmabuf_fd); 337 if (!ret) { 338 printf("Did not see expected error (invalid fd)!\n"); 339 ret = -1; 340 goto out; 341 } 342 343 ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0x111111, &dmabuf_fd); 344 if (!ret) { 345 printf("Did not see expected error (invalid heap flags)!\n"); 346 ret = -1; 347 goto out; 348 } 349 350 ret = dmabuf_heap_alloc_fdflags(heap_fd, ONE_MEG, 351 ~(O_RDWR | O_CLOEXEC), 0, &dmabuf_fd); 352 if (!ret) { 353 printf("Did not see expected error (invalid fd flags)!\n"); 354 ret = -1; 355 goto out; 356 } 357 358 printf("Expected error checking passed\n"); 359 ret = 0; 360 out: 361 if (dmabuf_fd >= 0) 362 close(dmabuf_fd); 363 if (heap_fd >= 0) 364 close(heap_fd); 365 366 return ret; 367 } 368 369 int main(void) 370 { 371 DIR *d; 372 struct dirent *dir; 373 int ret = -1; 374 375 d = opendir(DEVPATH); 376 if (!d) { 377 printf("No %s directory?\n", DEVPATH); 378 return -1; 379 } 380 381 while ((dir = readdir(d)) != NULL) { 382 if (!strncmp(dir->d_name, ".", 2)) 383 continue; 384 if (!strncmp(dir->d_name, "..", 3)) 385 continue; 386 387 ret = test_alloc_and_import(dir->d_name); 388 if (ret) 389 break; 390 391 ret = test_alloc_compat(dir->d_name); 392 if (ret) 393 break; 394 395 ret = test_alloc_errors(dir->d_name); 396 if (ret) 397 break; 398 } 399 closedir(d); 400 401 return ret; 402 } 403