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