• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2017 Intel Corporation
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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include <sys/stat.h>
26 #include <sys/sysmacros.h>
27 #include "igt.h"
28 #include "igt_device.h"
29 
__igt_device_set_master(int fd)30 int __igt_device_set_master(int fd)
31 {
32 	int err;
33 
34 	err = 0;
35 	if (drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL)) {
36 		err = -errno;
37 		igt_assume(err);
38 	}
39 
40 	errno = 0;
41 	return err;
42 }
43 
show_clients(int fd)44 static void show_clients(int fd)
45 {
46 	__igt_debugfs_dump(fd, "clients", IGT_LOG_WARN);
47 }
48 
49 /**
50  * igt_device_set_master: Set the device fd to be DRM master
51  * @fd: the device
52  *
53  * Tell the kernel to make this device fd become DRM master or skip the test.
54  */
igt_device_set_master(int fd)55 void igt_device_set_master(int fd)
56 {
57 	if (__igt_device_set_master(fd)) {
58 		show_clients(fd);
59 		igt_require_f(__igt_device_set_master(fd) == 0,
60 			      "Can't become DRM master, "
61 			      "please check if no other DRM client is running.\n");
62 	}
63 }
64 
__igt_device_drop_master(int fd)65 int __igt_device_drop_master(int fd)
66 {
67 	int err;
68 
69 	err = 0;
70 	if (drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL)) {
71 		err = -errno;
72 		igt_assume(err);
73 	}
74 
75 	errno = 0;
76 	return err;
77 }
78 
79 /**
80  * igt_device_drop_master: Drop DRM master
81  * @fd: the device
82  *
83  * Tell the kernel we no longer want this device fd to be the DRM master;
84  * asserting that we lose the privilege. Return if we are master already.
85  */
igt_device_drop_master(int fd)86 void igt_device_drop_master(int fd)
87 {
88 	/* Check if we are master before dropping */
89 	if (__igt_device_set_master(fd))
90 		return;
91 
92 	if (__igt_device_drop_master(fd)) {
93 		show_clients(fd);
94 		igt_assert_f(__igt_device_drop_master(fd) == 0,
95 			      "Failed to drop DRM master.\n");
96 	}
97 }
98 
99 /**
100  * igt_device_get_card_index:
101  * @fd: the device
102  *
103  * Returns:
104  * Index (N) of /dev/dri/cardN or /dev/dri/renderDN corresponding with fd.
105  *
106  */
igt_device_get_card_index(int fd)107 int igt_device_get_card_index(int fd)
108 {
109 	struct stat st;
110 
111 	igt_fail_on(fstat(fd, &st) || !S_ISCHR(st.st_mode));
112 
113 	return minor(st.st_rdev);
114 }
115