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 #ifdef HAVE_CONFIG_H
     24 #  include "config.h"
     25 #endif
     26 
     27 #include <sys/ioctl.h>
     28 #include <dlfcn.h>
     29 #include <fcntl.h>
     30 #include <stdio.h>
     31 #include <unistd.h>
     32 #include <errno.h>
     33 #include <pthread.h>
     34 
     35 #include "xf86drm.h"
     36 #include "nouveau.h"
     37 
     38 static typeof(ioctl) *old_ioctl;
     39 static int failed;
     40 
     41 static int import_fd;
     42 
     43 int ioctl(int fd, unsigned long request, ...)
     44 {
     45 	va_list va;
     46 	int ret;
     47 	void *arg;
     48 
     49 	va_start(va, request);
     50 	arg = va_arg(va, void *);
     51 	ret = old_ioctl(fd, request, arg);
     52 	va_end(va);
     53 
     54 	if (ret < 0 && request == DRM_IOCTL_GEM_CLOSE && errno == EINVAL)
     55 		failed = 1;
     56 
     57 	return ret;
     58 }
     59 
     60 static void *
     61 openclose(void *dev)
     62 {
     63 	struct nouveau_device *nvdev = dev;
     64 	struct nouveau_bo *bo = NULL;
     65 	int i;
     66 
     67 	for (i = 0; i < 100000; ++i) {
     68 		if (!nouveau_bo_prime_handle_ref(nvdev, import_fd, &bo))
     69 			nouveau_bo_ref(NULL, &bo);
     70 	}
     71 	return NULL;
     72 }
     73 
     74 int main(int argc, char *argv[])
     75 {
     76 	drmVersionPtr version;
     77 	const char *device = NULL;
     78 	int err, fd, fd2;
     79 	struct nouveau_device *nvdev, *nvdev2;
     80 	struct nouveau_bo *bo;
     81 	pthread_t t1, t2;
     82 
     83 	old_ioctl = dlsym(RTLD_NEXT, "ioctl");
     84 
     85 	if (argc < 2) {
     86 		fd = drmOpenWithType("nouveau", NULL, DRM_NODE_RENDER);
     87 		if (fd >= 0)
     88 			fd2 = drmOpenWithType("nouveau", NULL, DRM_NODE_RENDER);
     89 	} else {
     90 		device = argv[1];
     91 
     92 		fd = open(device, O_RDWR);
     93 		if (fd >= 0)
     94 			fd2 = open(device, O_RDWR);
     95 		else
     96 			fd2 = fd = -errno;
     97 	}
     98 
     99 	if (fd < 0) {
    100 		fprintf(stderr, "Opening nouveau render node failed with %i\n", fd);
    101 		return device ? -fd : 77;
    102 	}
    103 
    104 	if (fd2 < 0) {
    105 		fprintf(stderr, "Opening second nouveau render node failed with %i\n", -errno);
    106 		return errno;
    107 	}
    108 
    109 	version = drmGetVersion(fd);
    110 	if (version) {
    111 		printf("Version: %d.%d.%d\n", version->version_major,
    112 		       version->version_minor, version->version_patchlevel);
    113 		printf("  Name: %s\n", version->name);
    114 		printf("  Date: %s\n", version->date);
    115 		printf("  Description: %s\n", version->desc);
    116 
    117 		drmFreeVersion(version);
    118 	}
    119 
    120 	err = nouveau_device_wrap(fd, 0, &nvdev);
    121 	if (!err)
    122 		err = nouveau_device_wrap(fd2, 0, &nvdev2);
    123 	if (err < 0)
    124 		return 1;
    125 
    126 	err = nouveau_bo_new(nvdev2, NOUVEAU_BO_GART, 0, 4096, NULL, &bo);
    127 	if (!err)
    128 		err = nouveau_bo_set_prime(bo, &import_fd);
    129 
    130 	if (!err) {
    131 		pthread_create(&t1, NULL, openclose, nvdev);
    132 		pthread_create(&t2, NULL, openclose, nvdev);
    133 	}
    134 
    135 	pthread_join(t1, NULL);
    136 	pthread_join(t2, NULL);
    137 
    138 	close(import_fd);
    139 	nouveau_bo_ref(NULL, &bo);
    140 
    141 	nouveau_device_del(&nvdev2);
    142 	nouveau_device_del(&nvdev);
    143 	if (device) {
    144 		close(fd2);
    145 		close(fd);
    146 	} else {
    147 		drmClose(fd2);
    148 		drmClose(fd);
    149 	}
    150 
    151 	if (failed)
    152 		fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed with EINVAL,\n"
    153 				"race in opening/closing bo is likely.\n");
    154 
    155 	return failed;
    156 }
    157