Chris Wilson | 37339e7 | 2017-12-07 10:04:00 +0000 | [diff] [blame] | 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 | |
Michał Winiarski | bdf280f | 2019-03-13 12:35:12 +0100 | [diff] [blame] | 25 | #include <sys/stat.h> |
| 26 | #include <sys/sysmacros.h> |
Chris Wilson | 37339e7 | 2017-12-07 10:04:00 +0000 | [diff] [blame] | 27 | #include "igt.h" |
| 28 | #include "igt_device.h" |
| 29 | |
| 30 | int __igt_device_set_master(int fd) |
| 31 | { |
| 32 | int err; |
| 33 | |
| 34 | err = 0; |
Chris Wilson | 61dd8ee | 2019-05-15 11:18:45 +0100 | [diff] [blame] | 35 | if (drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL)) { |
Chris Wilson | 37339e7 | 2017-12-07 10:04:00 +0000 | [diff] [blame] | 36 | err = -errno; |
Chris Wilson | 61dd8ee | 2019-05-15 11:18:45 +0100 | [diff] [blame] | 37 | igt_assume(err); |
| 38 | } |
Chris Wilson | 37339e7 | 2017-12-07 10:04:00 +0000 | [diff] [blame] | 39 | |
| 40 | errno = 0; |
| 41 | return err; |
| 42 | } |
| 43 | |
Chris Wilson | 61dd8ee | 2019-05-15 11:18:45 +0100 | [diff] [blame] | 44 | static void show_clients(int fd) |
| 45 | { |
| 46 | __igt_debugfs_dump(fd, "clients", IGT_LOG_WARN); |
| 47 | } |
| 48 | |
Chris Wilson | 37339e7 | 2017-12-07 10:04:00 +0000 | [diff] [blame] | 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 | */ |
| 55 | void igt_device_set_master(int fd) |
| 56 | { |
| 57 | if (__igt_device_set_master(fd)) { |
Chris Wilson | 61dd8ee | 2019-05-15 11:18:45 +0100 | [diff] [blame] | 58 | show_clients(fd); |
Chris Wilson | 37339e7 | 2017-12-07 10:04:00 +0000 | [diff] [blame] | 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 | |
| 65 | int __igt_device_drop_master(int fd) |
| 66 | { |
| 67 | int err; |
| 68 | |
| 69 | err = 0; |
Chris Wilson | 61dd8ee | 2019-05-15 11:18:45 +0100 | [diff] [blame] | 70 | if (drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL)) { |
Chris Wilson | 37339e7 | 2017-12-07 10:04:00 +0000 | [diff] [blame] | 71 | err = -errno; |
Chris Wilson | 61dd8ee | 2019-05-15 11:18:45 +0100 | [diff] [blame] | 72 | igt_assume(err); |
| 73 | } |
Chris Wilson | 37339e7 | 2017-12-07 10:04:00 +0000 | [diff] [blame] | 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; |
Katarzyna Dec | b7432bf | 2018-05-15 10:40:55 +0200 | [diff] [blame] | 84 | * asserting that we lose the privilege. Return if we are master already. |
Chris Wilson | 37339e7 | 2017-12-07 10:04:00 +0000 | [diff] [blame] | 85 | */ |
| 86 | void igt_device_drop_master(int fd) |
| 87 | { |
Katarzyna Dec | b7432bf | 2018-05-15 10:40:55 +0200 | [diff] [blame] | 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)) { |
Chris Wilson | 61dd8ee | 2019-05-15 11:18:45 +0100 | [diff] [blame] | 93 | show_clients(fd); |
Katarzyna Dec | b7432bf | 2018-05-15 10:40:55 +0200 | [diff] [blame] | 94 | igt_assert_f(__igt_device_drop_master(fd) == 0, |
| 95 | "Failed to drop DRM master.\n"); |
| 96 | } |
Chris Wilson | 37339e7 | 2017-12-07 10:04:00 +0000 | [diff] [blame] | 97 | } |
Michał Winiarski | bdf280f | 2019-03-13 12:35:12 +0100 | [diff] [blame] | 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 | */ |
| 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 | } |