2 * Copyright © 2015 Canonical Ltd. (Maarten Lankhorst)
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 #include <sys/ioctl.h>
34 static __typeof__(ioctl
) *old_ioctl
;
39 #if defined(__GLIBC__) || defined(__FreeBSD__)
40 int ioctl(int fd
, unsigned long request
, ...)
42 int ioctl(int fd
, int request
, ...)
49 va_start(va
, request
);
50 arg
= va_arg(va
, void *);
51 ret
= old_ioctl(fd
, request
, arg
);
54 if (ret
< 0 && request
== DRM_IOCTL_GEM_CLOSE
&& errno
== EINVAL
)
63 struct nouveau_device
*nvdev
= dev
;
64 struct nouveau_bo
*bo
= NULL
;
67 for (i
= 0; i
< 100000; ++i
) {
68 if (!nouveau_bo_prime_handle_ref(nvdev
, import_fd
, &bo
))
69 nouveau_bo_ref(NULL
, &bo
);
74 int main(int argc
, char *argv
[])
76 drmVersionPtr version
;
77 const char *device
= NULL
;
79 struct nouveau_device
*nvdev
, *nvdev2
;
80 struct nouveau_bo
*bo
;
83 old_ioctl
= dlsym(RTLD_NEXT
, "ioctl");
86 fd
= drmOpenWithType("nouveau", NULL
, DRM_NODE_RENDER
);
88 fd2
= drmOpenWithType("nouveau", NULL
, DRM_NODE_RENDER
);
92 fd
= open(device
, O_RDWR
);
94 fd2
= open(device
, O_RDWR
);
100 fprintf(stderr
, "Opening nouveau render node failed with %i\n", fd
);
101 return device
? -fd
: 77;
105 fprintf(stderr
, "Opening second nouveau render node failed with %i\n", -errno
);
109 version
= drmGetVersion(fd
);
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
);
117 drmFreeVersion(version
);
120 err
= nouveau_device_wrap(fd
, 0, &nvdev
);
122 err
= nouveau_device_wrap(fd2
, 0, &nvdev2
);
126 err
= nouveau_bo_new(nvdev2
, NOUVEAU_BO_GART
, 0, 4096, NULL
, &bo
);
128 err
= nouveau_bo_set_prime(bo
, &import_fd
);
131 pthread_create(&t1
, NULL
, openclose
, nvdev
);
132 pthread_create(&t2
, NULL
, openclose
, nvdev
);
135 pthread_join(t1
, NULL
);
136 pthread_join(t2
, NULL
);
139 nouveau_bo_ref(NULL
, &bo
);
141 nouveau_device_del(&nvdev2
);
142 nouveau_device_del(&nvdev
);
152 fprintf(stderr
, "DRM_IOCTL_GEM_CLOSE failed with EINVAL,\n"
153 "race in opening/closing bo is likely.\n");