Home | History | Annotate | Download | only in nouveau
      1 /*
      2  * Copyright  2015 Canonical Ltd. (Maarten Lankhorst)
      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 shall be included in
     12  * all copies or substantial portions of the Software.
     13  *
     14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     20  * OTHER DEALINGS IN THE SOFTWARE.
     21  */
     22 
     23 #include <sys/ioctl.h>
     24 #include <dlfcn.h>
     25 #include <fcntl.h>
     26 #include <stdio.h>
     27 #include <unistd.h>
     28 #include <errno.h>
     29 #include <pthread.h>
     30 
     31 #include "xf86drm.h"
     32 #include "nouveau.h"
     33 
     34 static typeof(ioctl) *old_ioctl;
     35 static int failed;
     36 
     37 static int import_fd;
     38 
     39 int ioctl(int fd, unsigned long request, ...)
     40 {
     41 	va_list va;
     42 	int ret;
     43 	void *arg;
     44 
     45 	va_start(va, request);
     46 	arg = va_arg(va, void *);
     47 	ret = old_ioctl(fd, request, arg);
     48 	va_end(va);
     49 
     50 	if (ret < 0 && request == DRM_IOCTL_GEM_CLOSE && errno == EINVAL)
     51 		failed = 1;
     52 
     53 	return ret;
     54 }
     55 
     56 static void *
     57 openclose(void *dev)
     58 {
     59 	struct nouveau_device *nvdev = dev;
     60 	struct nouveau_bo *bo = NULL;
     61 	int i;
     62 
     63 	for (i = 0; i < 100000; ++i) {
     64 		if (!nouveau_bo_prime_handle_ref(nvdev, import_fd, &bo))
     65 			nouveau_bo_ref(NULL, &bo);
     66 	}
     67 	return NULL;
     68 }
     69 
     70 int main(int argc, char *argv[])
     71 {
     72 	drmVersionPtr version;
     73 	const char *device = NULL;
     74 	int err, fd, fd2;
     75 	struct nouveau_device *nvdev, *nvdev2;
     76 	struct nouveau_bo *bo;
     77 	pthread_t t1, t2;
     78 
     79 	old_ioctl = dlsym(RTLD_NEXT, "ioctl");
     80 
     81 	if (argc < 2) {
     82 		fd = drmOpenWithType("nouveau", NULL, DRM_NODE_RENDER);
     83 		if (fd >= 0)
     84 			fd2 = drmOpenWithType("nouveau", NULL, DRM_NODE_RENDER);
     85 	} else {
     86 		device = argv[1];
     87 
     88 		fd = open(device, O_RDWR);
     89 		if (fd >= 0)
     90 			fd2 = open(device, O_RDWR);
     91 		else
     92 			fd2 = fd = -errno;
     93 	}
     94 
     95 	if (fd < 0) {
     96 		fprintf(stderr, "Opening nouveau render node failed with %i\n", fd);
     97 		return device ? -fd : 77;
     98 	}
     99 
    100 	if (fd2 < 0) {
    101 		fprintf(stderr, "Opening second nouveau render node failed with %i\n", -errno);
    102 		return errno;
    103 	}
    104 
    105 	version = drmGetVersion(fd);
    106 	if (version) {
    107 		printf("Version: %d.%d.%d\n", version->version_major,
    108 		       version->version_minor, version->version_patchlevel);
    109 		printf("  Name: %s\n", version->name);
    110 		printf("  Date: %s\n", version->date);
    111 		printf("  Description: %s\n", version->desc);
    112 
    113 		drmFreeVersion(version);
    114 	}
    115 
    116 	err = nouveau_device_wrap(fd, 0, &nvdev);
    117 	if (!err)
    118 		err = nouveau_device_wrap(fd2, 0, &nvdev2);
    119 	if (err < 0)
    120 		return 1;
    121 
    122 	err = nouveau_bo_new(nvdev2, NOUVEAU_BO_GART, 0, 4096, NULL, &bo);
    123 	if (!err)
    124 		err = nouveau_bo_set_prime(bo, &import_fd);
    125 
    126 	if (!err) {
    127 		pthread_create(&t1, NULL, openclose, nvdev);
    128 		pthread_create(&t2, NULL, openclose, nvdev);
    129 	}
    130 
    131 	pthread_join(t1, NULL);
    132 	pthread_join(t2, NULL);
    133 
    134 	close(import_fd);
    135 	nouveau_bo_ref(NULL, &bo);
    136 
    137 	nouveau_device_del(&nvdev2);
    138 	nouveau_device_del(&nvdev);
    139 	if (device) {
    140 		close(fd2);
    141 		close(fd);
    142 	} else {
    143 		drmClose(fd2);
    144 		drmClose(fd);
    145 	}
    146 
    147 	if (failed)
    148 		fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed with EINVAL,\n"
    149 				"race in opening/closing bo is likely.\n");
    150 
    151 	return failed;
    152 }
    153