Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 1 | /* |
Damien Lespiau | bb33d08 | 2013-02-13 16:29:01 +0000 | [diff] [blame] | 2 | * Copyright © 2007, 2011, 2013 Intel Corporation |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 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 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
Daniel Vetter | aa67b22 | 2012-01-10 14:59:58 +0100 | [diff] [blame] | 25 | * Daniel Vetter <daniel.vetter@ffwll.ch> |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 26 | * |
| 27 | */ |
| 28 | |
Oscar Mateo | 235ff9c | 2013-11-12 11:50:43 +0000 | [diff] [blame] | 29 | #ifndef ANDROID |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 30 | #define _GNU_SOURCE |
Oscar Mateo | 235ff9c | 2013-11-12 11:50:43 +0000 | [diff] [blame] | 31 | #else |
| 32 | #include <libgen.h> |
| 33 | #endif |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 34 | #include <stdio.h> |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 35 | #include <fcntl.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/ioctl.h> |
Daniel Vetter | aa67b22 | 2012-01-10 14:59:58 +0100 | [diff] [blame] | 38 | #include <string.h> |
Daniel Vetter | 527cad1 | 2012-01-10 18:41:46 +0100 | [diff] [blame] | 39 | #include <sys/mman.h> |
Daniel Vetter | cbaa8a3 | 2012-01-11 15:33:00 +0100 | [diff] [blame] | 40 | #include <signal.h> |
Daniel Vetter | 7b14b09 | 2012-01-15 17:40:41 +0100 | [diff] [blame] | 41 | #include <pciaccess.h> |
Damien Lespiau | bb33d08 | 2013-02-13 16:29:01 +0000 | [diff] [blame] | 42 | #include <stdlib.h> |
Damien Lespiau | 4b34765 | 2013-04-29 16:04:52 +0100 | [diff] [blame] | 43 | #include <unistd.h> |
Daniel Vetter | 7553ad6 | 2013-08-12 10:43:59 +0200 | [diff] [blame] | 44 | #include <sys/wait.h> |
Tvrtko Ursulin | 139c72f | 2013-12-03 16:44:55 +0000 | [diff] [blame] | 45 | #include <sys/types.h> |
| 46 | #include <sys/syscall.h> |
Daniel Vetter | f1c4ec9 | 2013-12-10 09:26:08 +0100 | [diff] [blame] | 47 | #include <sys/utsname.h> |
Damien Lespiau | 377f48a | 2014-02-06 21:06:07 +0000 | [diff] [blame] | 48 | #include <termios.h> |
Daniel Vetter | 662d732 | 2012-05-22 14:37:19 +0200 | [diff] [blame] | 49 | |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 50 | #include "drmtest.h" |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 51 | #include "i915_drm.h" |
| 52 | #include "intel_chipset.h" |
Daniel Vetter | c03c6ce | 2014-03-22 21:34:29 +0100 | [diff] [blame] | 53 | #include "intel_io.h" |
Daniel Vetter | 3cd45de | 2015-02-10 17:46:43 +0100 | [diff] [blame] | 54 | #include "igt_gt.h" |
Oscar Mateo | aa252d0 | 2013-11-05 14:15:19 +0000 | [diff] [blame] | 55 | #include "igt_debugfs.h" |
Rodrigo Vivi | 82ad15f | 2014-06-09 11:15:47 -0700 | [diff] [blame] | 56 | #include "version.h" |
Ben Widawsky | 8c33e9a | 2013-12-06 20:35:29 -0800 | [diff] [blame] | 57 | #include "config.h" |
Daniel Vetter | 6cfcd71 | 2014-03-22 20:07:35 +0100 | [diff] [blame] | 58 | #include "intel_reg.h" |
Daniel Vetter | e49ceb8 | 2014-03-22 21:07:37 +0100 | [diff] [blame] | 59 | #include "ioctl_wrappers.h" |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 60 | |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 61 | /** |
| 62 | * SECTION:drmtest |
| 63 | * @short_description: Base library for drm tests and tools |
| 64 | * @title: drmtest |
Thomas Wood | f0381d1 | 2015-09-07 09:26:01 +0100 | [diff] [blame] | 65 | * @include: igt.h |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 66 | * |
| 67 | * This library contains the basic support for writing tests, with the most |
| 68 | * important part being the helper function to open drm device nodes. |
| 69 | * |
| 70 | * But there's also a bit of other assorted stuff here. |
| 71 | * |
| 72 | * Note that this library's header pulls in the [i-g-t core](intel-gpu-tools-i-g-t-core.html) |
Daniel Vetter | c6c2b2b | 2014-03-26 15:15:49 +0100 | [diff] [blame] | 73 | * and [batchbuffer](intel-gpu-tools-intel-batchbuffer.html) libraries as dependencies. |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 74 | */ |
Daniel Vetter | aa67b22 | 2012-01-10 14:59:58 +0100 | [diff] [blame] | 75 | |
Chris Wilson | eaa7e61 | 2015-02-18 15:58:06 +0000 | [diff] [blame] | 76 | uint16_t __drm_device_id; |
| 77 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 78 | static int __get_drm_device_name(int fd, char *name) |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 79 | { |
| 80 | drm_version_t version; |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 81 | |
| 82 | memset(&version, 0, sizeof(version)); |
| 83 | version.name_len = 4; |
| 84 | version.name = name; |
| 85 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 86 | if (!drmIoctl(fd, DRM_IOCTL_VERSION, &version)){ |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 87 | return 0; |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 88 | } |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 89 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 90 | return -1; |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 93 | static bool is_i915_device(int fd) |
| 94 | { |
| 95 | int ret; |
| 96 | char name[5] = ""; |
| 97 | |
| 98 | ret = __get_drm_device_name(fd, name); |
| 99 | |
| 100 | return !ret && strcmp("i915", name) == 0; |
| 101 | } |
| 102 | |
Eric Anholt | 3f83f07 | 2016-01-22 17:18:56 -0800 | [diff] [blame] | 103 | static bool is_vc4_device(int fd) |
| 104 | { |
| 105 | int ret; |
| 106 | char name[5] = ""; |
| 107 | |
| 108 | ret = __get_drm_device_name(fd, name); |
| 109 | |
| 110 | return !ret && strcmp("vc4", name) == 0; |
| 111 | } |
| 112 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 113 | static bool is_intel(int fd) |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 114 | { |
| 115 | struct drm_i915_getparam gp; |
Chris Wilson | acca724 | 2014-07-21 07:57:25 +0100 | [diff] [blame] | 116 | int devid = 0; |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 117 | |
Chris Wilson | acca724 | 2014-07-21 07:57:25 +0100 | [diff] [blame] | 118 | memset(&gp, 0, sizeof(gp)); |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 119 | gp.param = I915_PARAM_CHIPSET_ID; |
| 120 | gp.value = &devid; |
| 121 | |
| 122 | if (ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp))) |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 123 | return false; |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 124 | |
Chris Wilson | eaa7e61 | 2015-02-18 15:58:06 +0000 | [diff] [blame] | 125 | if (!IS_INTEL(devid)) |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 126 | return false; |
Chris Wilson | eaa7e61 | 2015-02-18 15:58:06 +0000 | [diff] [blame] | 127 | |
| 128 | __drm_device_id = devid; |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 129 | return true; |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 130 | } |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 131 | |
Imre Deak | a3b47db | 2013-05-28 17:22:57 +0300 | [diff] [blame] | 132 | #define LOCAL_I915_EXEC_VEBOX (4 << 0) |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 133 | /** |
| 134 | * gem_quiescent_gpu: |
| 135 | * @fd: open i915 drm file descriptor |
| 136 | * |
| 137 | * Ensure the gpu is idle by launching a nop execbuf and stalling for it. This |
| 138 | * is automatically run when opening a drm device node and is also installed as |
| 139 | * an exit handler to have the best assurance that the test is run in a pristine |
| 140 | * and controlled environment. |
| 141 | * |
| 142 | * This function simply allows tests to make additional calls in-between, if so |
| 143 | * desired. |
| 144 | */ |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 145 | void gem_quiescent_gpu(int fd) |
| 146 | { |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 147 | uint32_t bbe = MI_BATCH_BUFFER_END; |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 148 | struct drm_i915_gem_execbuffer2 execbuf; |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 149 | struct drm_i915_gem_exec_object2 obj; |
| 150 | unsigned ring; |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 151 | |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 152 | memset(&obj, 0, sizeof(obj)); |
| 153 | obj.handle = gem_create(fd, 4096); |
| 154 | gem_write(fd, obj.handle, 0, &bbe, sizeof(&bbe)); |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 155 | |
Chris Wilson | 59f076a | 2015-11-20 11:23:37 +0000 | [diff] [blame] | 156 | memset(&execbuf, 0, sizeof(execbuf)); |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 157 | execbuf.buffers_ptr = (uintptr_t)&obj; |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 158 | execbuf.buffer_count = 1; |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 159 | |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 160 | for (ring = 0; ring < 1<<6; ring++) { |
| 161 | execbuf.flags = ring; |
| 162 | __gem_execbuf(fd, &execbuf); |
Imre Deak | a3b47db | 2013-05-28 17:22:57 +0300 | [diff] [blame] | 163 | } |
| 164 | |
Chris Wilson | 310f99c | 2016-03-04 15:03:42 +0000 | [diff] [blame] | 165 | if (gem_has_bsd2(fd)) { |
| 166 | execbuf.flags = I915_EXEC_BSD | (2 << 13); |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 167 | __gem_execbuf(fd, &execbuf); |
Chris Wilson | 310f99c | 2016-03-04 15:03:42 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 170 | gem_sync(fd, obj.handle); |
| 171 | gem_close(fd, obj.handle); |
Imre Deak | a3b47db | 2013-05-28 17:22:57 +0300 | [diff] [blame] | 172 | |
Oscar Mateo | aa252d0 | 2013-11-05 14:15:19 +0000 | [diff] [blame] | 173 | igt_drop_caches_set(DROP_RETIRE); |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 176 | /** |
Thomas Wood | 2d44386 | 2014-03-11 16:04:29 +0000 | [diff] [blame] | 177 | * drm_get_card: |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 178 | * |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 179 | * Get an i915 drm card index number for use in /dev or /sys. The minor index of |
| 180 | * the legacy node is returned, not of the control or render node. |
Thomas Wood | 2d44386 | 2014-03-11 16:04:29 +0000 | [diff] [blame] | 181 | * |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 182 | * Returns: |
| 183 | * The i915 drm index or -1 on error |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 184 | */ |
Daniel Vetter | 5951ffb | 2013-08-19 10:34:34 +0200 | [diff] [blame] | 185 | int drm_get_card(void) |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 186 | { |
| 187 | char *name; |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 188 | int i, fd; |
| 189 | |
| 190 | for (i = 0; i < 16; i++) { |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 191 | int ret; |
| 192 | |
| 193 | ret = asprintf(&name, "/dev/dri/card%u", i); |
Daniel Vetter | 8dbd1fb | 2013-08-19 11:09:25 +0200 | [diff] [blame] | 194 | igt_assert(ret != -1); |
| 195 | |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 196 | fd = open(name, O_RDWR); |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 197 | free(name); |
| 198 | |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 199 | if (fd == -1) |
| 200 | continue; |
| 201 | |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 202 | if (!is_i915_device(fd) || !is_intel(fd)) { |
Damien Lespiau | 8329acb | 2013-10-24 15:04:26 +0100 | [diff] [blame] | 203 | close(fd); |
Daniel Vetter | 8dbd1fb | 2013-08-19 11:09:25 +0200 | [diff] [blame] | 204 | continue; |
Damien Lespiau | 8329acb | 2013-10-24 15:04:26 +0100 | [diff] [blame] | 205 | } |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 206 | |
| 207 | close(fd); |
Daniel Vetter | 8dbd1fb | 2013-08-19 11:09:25 +0200 | [diff] [blame] | 208 | return i; |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 209 | } |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 210 | |
Daniel Vetter | 8dbd1fb | 2013-08-19 11:09:25 +0200 | [diff] [blame] | 211 | igt_skip("No intel gpu found\n"); |
| 212 | |
| 213 | return -1; |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 216 | /** |
| 217 | * __drm_open_driver: |
Thomas Wood | 683316c | 2015-11-30 16:36:16 +0000 | [diff] [blame] | 218 | * @chipset: OR'd flags for each chipset to search, eg. #DRIVER_INTEL |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 219 | * |
| 220 | * Open the first DRM device we can find, searching up to 16 device nodes |
| 221 | * |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 222 | * Returns: |
| 223 | * An open DRM fd or -1 on error |
| 224 | */ |
| 225 | int __drm_open_driver(int chipset) |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 226 | { |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 227 | for (int i = 0; i < 16; i++) { |
| 228 | char name[80]; |
| 229 | int fd; |
Eric Anholt | 3f83f07 | 2016-01-22 17:18:56 -0800 | [diff] [blame] | 230 | bool found_intel, found_vc4; |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 231 | |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 232 | sprintf(name, "/dev/dri/card%u", i); |
| 233 | fd = open(name, O_RDWR); |
| 234 | if (fd == -1) |
| 235 | continue; |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 236 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 237 | found_intel = is_i915_device(fd) && is_intel(fd) && (chipset & DRIVER_INTEL); |
| 238 | |
Eric Anholt | 3f83f07 | 2016-01-22 17:18:56 -0800 | [diff] [blame] | 239 | found_vc4 = is_vc4_device(fd) && (chipset & DRIVER_VC4); |
| 240 | |
| 241 | if ((chipset & DRIVER_ANY) || found_intel || found_vc4) |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 242 | return fd; |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 243 | |
Chris Wilson | 9eb7d89 | 2013-07-03 09:58:28 +0100 | [diff] [blame] | 244 | close(fd); |
Chris Wilson | 9eb7d89 | 2013-07-03 09:58:28 +0100 | [diff] [blame] | 245 | } |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 246 | |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 247 | igt_skip("No intel gpu found\n"); |
| 248 | return -1; |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 249 | } |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 250 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 251 | static int __drm_open_driver_render(int chipset) |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 252 | { |
| 253 | char *name; |
| 254 | int i, fd; |
| 255 | |
| 256 | for (i = 128; i < (128 + 16); i++) { |
| 257 | int ret; |
| 258 | |
| 259 | ret = asprintf(&name, "/dev/dri/renderD%u", i); |
| 260 | igt_assert(ret != -1); |
| 261 | |
| 262 | fd = open(name, O_RDWR); |
| 263 | free(name); |
| 264 | |
| 265 | if (fd == -1) |
| 266 | continue; |
| 267 | |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 268 | if (!is_i915_device(fd) || !is_intel(fd)) { |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 269 | close(fd); |
| 270 | fd = -1; |
| 271 | continue; |
| 272 | } |
| 273 | |
| 274 | return fd; |
| 275 | } |
| 276 | |
| 277 | return fd; |
| 278 | } |
| 279 | |
Imre Deak | 2423b6c | 2014-02-05 13:48:26 +0200 | [diff] [blame] | 280 | static int at_exit_drm_fd = -1; |
| 281 | static int at_exit_drm_render_fd = -1; |
| 282 | |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 283 | static void quiescent_gpu_at_exit(int sig) |
| 284 | { |
Imre Deak | 2423b6c | 2014-02-05 13:48:26 +0200 | [diff] [blame] | 285 | if (at_exit_drm_fd < 0) |
| 286 | return; |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 287 | |
Imre Deak | 2423b6c | 2014-02-05 13:48:26 +0200 | [diff] [blame] | 288 | gem_quiescent_gpu(at_exit_drm_fd); |
| 289 | close(at_exit_drm_fd); |
| 290 | at_exit_drm_fd = -1; |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 291 | } |
| 292 | |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 293 | static void quiescent_gpu_at_exit_render(int sig) |
| 294 | { |
Imre Deak | 2423b6c | 2014-02-05 13:48:26 +0200 | [diff] [blame] | 295 | if (at_exit_drm_render_fd < 0) |
| 296 | return; |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 297 | |
Imre Deak | 2423b6c | 2014-02-05 13:48:26 +0200 | [diff] [blame] | 298 | gem_quiescent_gpu(at_exit_drm_render_fd); |
| 299 | close(at_exit_drm_render_fd); |
| 300 | at_exit_drm_render_fd = -1; |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 303 | /** |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 304 | * drm_open_driver: |
Thomas Wood | 683316c | 2015-11-30 16:36:16 +0000 | [diff] [blame] | 305 | * @chipset: OR'd flags for each chipset to search, eg. #DRIVER_INTEL |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 306 | * |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 307 | * Open a drm legacy device node. This function always returns a valid |
Thomas Wood | 032f30c | 2015-01-13 13:33:57 +0000 | [diff] [blame] | 308 | * file descriptor. |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 309 | * |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 310 | * Returns: a drm file descriptor |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 311 | */ |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 312 | int drm_open_driver(int chipset) |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 313 | { |
| 314 | static int open_count; |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 315 | int fd = __drm_open_driver(chipset); |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 316 | |
Daniel Vetter | 4ba1f2e | 2013-08-19 07:57:03 +0200 | [diff] [blame] | 317 | igt_require(fd >= 0); |
Chris Wilson | 9eb7d89 | 2013-07-03 09:58:28 +0100 | [diff] [blame] | 318 | |
| 319 | if (__sync_fetch_and_add(&open_count, 1)) |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 320 | return fd; |
| 321 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 322 | if(chipset & DRIVER_INTEL){ |
| 323 | gem_quiescent_gpu(fd); |
| 324 | igt_install_exit_handler(quiescent_gpu_at_exit); |
| 325 | } |
| 326 | at_exit_drm_fd = __drm_open_driver(chipset); |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 327 | |
| 328 | return fd; |
| 329 | } |
| 330 | |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 331 | /** |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 332 | * drm_open_driver_master: |
Thomas Wood | 683316c | 2015-11-30 16:36:16 +0000 | [diff] [blame] | 333 | * @chipset: OR'd flags for each chipset to search, eg. #DRIVER_INTEL |
Imre Deak | bfda78c | 2014-09-18 18:25:37 +0300 | [diff] [blame] | 334 | * |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 335 | * Open a drm legacy device node and ensure that it is drm master. |
Imre Deak | bfda78c | 2014-09-18 18:25:37 +0300 | [diff] [blame] | 336 | * |
| 337 | * Returns: |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 338 | * The drm file descriptor or -1 on error |
Imre Deak | bfda78c | 2014-09-18 18:25:37 +0300 | [diff] [blame] | 339 | */ |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 340 | int drm_open_driver_master(int chipset) |
Imre Deak | bfda78c | 2014-09-18 18:25:37 +0300 | [diff] [blame] | 341 | { |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 342 | int fd = drm_open_driver(chipset); |
Imre Deak | bfda78c | 2014-09-18 18:25:37 +0300 | [diff] [blame] | 343 | |
| 344 | igt_require(fd >= 0); |
| 345 | igt_require_f(drmSetMaster(fd) == 0, "Can't become DRM master, " |
| 346 | "please check if no other DRM client is running.\n"); |
| 347 | |
| 348 | return fd; |
| 349 | } |
| 350 | |
| 351 | /** |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 352 | * drm_open_driver_render: |
Thomas Wood | 683316c | 2015-11-30 16:36:16 +0000 | [diff] [blame] | 353 | * @chipset: OR'd flags for each chipset to search, eg. #DRIVER_INTEL |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 354 | * |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 355 | * Open a drm render device node. |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 356 | * |
| 357 | * Returns: |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 358 | * The drm file descriptor or -1 on error |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 359 | */ |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 360 | int drm_open_driver_render(int chipset) |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 361 | { |
| 362 | static int open_count; |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 363 | int fd = __drm_open_driver_render(chipset); |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 364 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 365 | /* no render nodes, fallback to drm_open_driver() */ |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 366 | if (fd == -1) |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 367 | return drm_open_driver(chipset); |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 368 | |
| 369 | if (__sync_fetch_and_add(&open_count, 1)) |
| 370 | return fd; |
| 371 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 372 | at_exit_drm_render_fd = __drm_open_driver(chipset); |
| 373 | if(chipset & DRIVER_INTEL){ |
| 374 | gem_quiescent_gpu(fd); |
| 375 | igt_install_exit_handler(quiescent_gpu_at_exit_render); |
| 376 | } |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 377 | |
| 378 | return fd; |
| 379 | } |