blob: 9469e5de2cdcc2f4b6afc11cc3ff2cb1d3ee0759 [file] [log] [blame]
Chris Wilson37339e72017-12-07 10:04:00 +00001/*
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
Michał Winiarskibdf280f2019-03-13 12:35:12 +010025#include <sys/stat.h>
26#include <sys/sysmacros.h>
Chris Wilson37339e72017-12-07 10:04:00 +000027#include "igt.h"
28#include "igt_device.h"
29
30int __igt_device_set_master(int fd)
31{
32 int err;
33
34 err = 0;
Chris Wilson61dd8ee2019-05-15 11:18:45 +010035 if (drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL)) {
Chris Wilson37339e72017-12-07 10:04:00 +000036 err = -errno;
Chris Wilson61dd8ee2019-05-15 11:18:45 +010037 igt_assume(err);
38 }
Chris Wilson37339e72017-12-07 10:04:00 +000039
40 errno = 0;
41 return err;
42}
43
Chris Wilson61dd8ee2019-05-15 11:18:45 +010044static void show_clients(int fd)
45{
46 __igt_debugfs_dump(fd, "clients", IGT_LOG_WARN);
47}
48
Chris Wilson37339e72017-12-07 10:04:00 +000049/**
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 */
55void igt_device_set_master(int fd)
56{
57 if (__igt_device_set_master(fd)) {
Chris Wilson61dd8ee2019-05-15 11:18:45 +010058 show_clients(fd);
Chris Wilson37339e72017-12-07 10:04:00 +000059 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
65int __igt_device_drop_master(int fd)
66{
67 int err;
68
69 err = 0;
Chris Wilson61dd8ee2019-05-15 11:18:45 +010070 if (drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL)) {
Chris Wilson37339e72017-12-07 10:04:00 +000071 err = -errno;
Chris Wilson61dd8ee2019-05-15 11:18:45 +010072 igt_assume(err);
73 }
Chris Wilson37339e72017-12-07 10:04:00 +000074
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;
Katarzyna Decb7432bf2018-05-15 10:40:55 +020084 * asserting that we lose the privilege. Return if we are master already.
Chris Wilson37339e72017-12-07 10:04:00 +000085 */
86void igt_device_drop_master(int fd)
87{
Katarzyna Decb7432bf2018-05-15 10:40:55 +020088 /* Check if we are master before dropping */
89 if (__igt_device_set_master(fd))
90 return;
91
92 if (__igt_device_drop_master(fd)) {
Chris Wilson61dd8ee2019-05-15 11:18:45 +010093 show_clients(fd);
Katarzyna Decb7432bf2018-05-15 10:40:55 +020094 igt_assert_f(__igt_device_drop_master(fd) == 0,
95 "Failed to drop DRM master.\n");
96 }
Chris Wilson37339e72017-12-07 10:04:00 +000097}
Michał Winiarskibdf280f2019-03-13 12:35:12 +010098
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 */
107int 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}