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 | |
Daniel Vetter | 00215dd | 2017-09-05 14:36:05 +0200 | [diff] [blame] | 29 | #ifdef HAVE_LIBGEN_H |
Oscar Mateo | 235ff9c | 2013-11-12 11:50:43 +0000 | [diff] [blame] | 30 | #include <libgen.h> |
| 31 | #endif |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 32 | #include <stdio.h> |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 33 | #include <fcntl.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/ioctl.h> |
Daniel Vetter | aa67b22 | 2012-01-10 14:59:58 +0100 | [diff] [blame] | 36 | #include <string.h> |
Daniel Vetter | 527cad1 | 2012-01-10 18:41:46 +0100 | [diff] [blame] | 37 | #include <sys/mman.h> |
Daniel Vetter | cbaa8a3 | 2012-01-11 15:33:00 +0100 | [diff] [blame] | 38 | #include <signal.h> |
Daniel Vetter | 7b14b09 | 2012-01-15 17:40:41 +0100 | [diff] [blame] | 39 | #include <pciaccess.h> |
Damien Lespiau | bb33d08 | 2013-02-13 16:29:01 +0000 | [diff] [blame] | 40 | #include <stdlib.h> |
Damien Lespiau | 4b34765 | 2013-04-29 16:04:52 +0100 | [diff] [blame] | 41 | #include <unistd.h> |
Daniel Vetter | 7553ad6 | 2013-08-12 10:43:59 +0200 | [diff] [blame] | 42 | #include <sys/wait.h> |
Tvrtko Ursulin | 139c72f | 2013-12-03 16:44:55 +0000 | [diff] [blame] | 43 | #include <sys/types.h> |
| 44 | #include <sys/syscall.h> |
Daniel Vetter | f1c4ec9 | 2013-12-10 09:26:08 +0100 | [diff] [blame] | 45 | #include <sys/utsname.h> |
Damien Lespiau | 377f48a | 2014-02-06 21:06:07 +0000 | [diff] [blame] | 46 | #include <termios.h> |
Daniel Vetter | 662d732 | 2012-05-22 14:37:19 +0200 | [diff] [blame] | 47 | |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 48 | #include "drmtest.h" |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 49 | #include "i915_drm.h" |
| 50 | #include "intel_chipset.h" |
Daniel Vetter | c03c6ce | 2014-03-22 21:34:29 +0100 | [diff] [blame] | 51 | #include "intel_io.h" |
Oscar Mateo | aa252d0 | 2013-11-05 14:15:19 +0000 | [diff] [blame] | 52 | #include "igt_debugfs.h" |
Chris Wilson | 137360f | 2017-03-23 17:09:26 +0000 | [diff] [blame] | 53 | #include "igt_gt.h" |
| 54 | #include "igt_kmod.h" |
Rodrigo Vivi | 82ad15f | 2014-06-09 11:15:47 -0700 | [diff] [blame] | 55 | #include "version.h" |
Ben Widawsky | 8c33e9a | 2013-12-06 20:35:29 -0800 | [diff] [blame] | 56 | #include "config.h" |
Daniel Vetter | 6cfcd71 | 2014-03-22 20:07:35 +0100 | [diff] [blame] | 57 | #include "intel_reg.h" |
Daniel Vetter | e49ceb8 | 2014-03-22 21:07:37 +0100 | [diff] [blame] | 58 | #include "ioctl_wrappers.h" |
Abdiel Janulgue | 35fa2ca | 2016-10-11 21:30:08 +0300 | [diff] [blame] | 59 | #include "igt_dummyload.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 | |
Chris Wilson | 7ce6389 | 2016-06-18 00:17:06 +0100 | [diff] [blame] | 93 | static bool __is_device(int fd, const char *expect) |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 94 | { |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 95 | char name[5] = ""; |
| 96 | |
Chris Wilson | 7ce6389 | 2016-06-18 00:17:06 +0100 | [diff] [blame] | 97 | if (__get_drm_device_name(fd, name)) |
| 98 | return false; |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 99 | |
Chris Wilson | 7ce6389 | 2016-06-18 00:17:06 +0100 | [diff] [blame] | 100 | return strcmp(expect, name) == 0; |
| 101 | } |
| 102 | |
| 103 | bool is_i915_device(int fd) |
| 104 | { |
| 105 | return __is_device(fd, "i915"); |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Eric Anholt | 3f83f07 | 2016-01-22 17:18:56 -0800 | [diff] [blame] | 108 | static bool is_vc4_device(int fd) |
| 109 | { |
Chris Wilson | 7ce6389 | 2016-06-18 00:17:06 +0100 | [diff] [blame] | 110 | return __is_device(fd, "vc4"); |
| 111 | } |
Eric Anholt | 3f83f07 | 2016-01-22 17:18:56 -0800 | [diff] [blame] | 112 | |
Chris Wilson | 7ce6389 | 2016-06-18 00:17:06 +0100 | [diff] [blame] | 113 | static bool is_vgem_device(int fd) |
| 114 | { |
| 115 | return __is_device(fd, "vgem"); |
Eric Anholt | 3f83f07 | 2016-01-22 17:18:56 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Gustavo Padovan | 22779a7 | 2016-06-30 18:29:47 -0300 | [diff] [blame] | 118 | static bool is_virtio_device(int fd) |
| 119 | { |
| 120 | return __is_device(fd, "virt"); |
| 121 | } |
| 122 | |
Chris Wilson | 36b92e8 | 2017-04-22 13:45:18 +0100 | [diff] [blame] | 123 | static bool is_amd_device(int fd) |
| 124 | { |
| 125 | return __is_device(fd, "amdg"); |
| 126 | } |
| 127 | |
Tomeu Vizoso | 0eadf63 | 2016-03-08 09:18:45 +0100 | [diff] [blame] | 128 | static bool has_known_intel_chipset(int fd) |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 129 | { |
| 130 | struct drm_i915_getparam gp; |
Chris Wilson | acca724 | 2014-07-21 07:57:25 +0100 | [diff] [blame] | 131 | int devid = 0; |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 132 | |
Chris Wilson | acca724 | 2014-07-21 07:57:25 +0100 | [diff] [blame] | 133 | memset(&gp, 0, sizeof(gp)); |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 134 | gp.param = I915_PARAM_CHIPSET_ID; |
| 135 | gp.value = &devid; |
| 136 | |
| 137 | if (ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp))) |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 138 | return false; |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 139 | |
Chris Wilson | d308bb0 | 2016-06-29 08:28:58 +0100 | [diff] [blame] | 140 | if (!intel_gen(devid)) |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 141 | return false; |
Chris Wilson | eaa7e61 | 2015-02-18 15:58:06 +0000 | [diff] [blame] | 142 | |
| 143 | __drm_device_id = devid; |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 144 | return true; |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 145 | } |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 146 | |
Imre Deak | a3b47db | 2013-05-28 17:22:57 +0300 | [diff] [blame] | 147 | #define LOCAL_I915_EXEC_VEBOX (4 << 0) |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 148 | /** |
| 149 | * gem_quiescent_gpu: |
| 150 | * @fd: open i915 drm file descriptor |
| 151 | * |
| 152 | * Ensure the gpu is idle by launching a nop execbuf and stalling for it. This |
| 153 | * is automatically run when opening a drm device node and is also installed as |
| 154 | * an exit handler to have the best assurance that the test is run in a pristine |
| 155 | * and controlled environment. |
| 156 | * |
| 157 | * This function simply allows tests to make additional calls in-between, if so |
| 158 | * desired. |
| 159 | */ |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 160 | void gem_quiescent_gpu(int fd) |
| 161 | { |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 162 | uint32_t bbe = MI_BATCH_BUFFER_END; |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 163 | struct drm_i915_gem_execbuffer2 execbuf; |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 164 | struct drm_i915_gem_exec_object2 obj; |
| 165 | unsigned ring; |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 166 | |
Abdiel Janulgue | 35fa2ca | 2016-10-11 21:30:08 +0300 | [diff] [blame] | 167 | igt_terminate_spin_batches(); |
| 168 | |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 169 | memset(&obj, 0, sizeof(obj)); |
| 170 | obj.handle = gem_create(fd, 4096); |
| 171 | gem_write(fd, obj.handle, 0, &bbe, sizeof(&bbe)); |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 172 | |
Chris Wilson | 59f076a | 2015-11-20 11:23:37 +0000 | [diff] [blame] | 173 | memset(&execbuf, 0, sizeof(execbuf)); |
Chris Wilson | 39858a1 | 2017-01-02 11:05:21 +0000 | [diff] [blame] | 174 | execbuf.buffers_ptr = to_user_pointer(&obj); |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 175 | execbuf.buffer_count = 1; |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 176 | |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 177 | for (ring = 0; ring < 1<<6; ring++) { |
| 178 | execbuf.flags = ring; |
| 179 | __gem_execbuf(fd, &execbuf); |
Imre Deak | a3b47db | 2013-05-28 17:22:57 +0300 | [diff] [blame] | 180 | } |
| 181 | |
Chris Wilson | 310f99c | 2016-03-04 15:03:42 +0000 | [diff] [blame] | 182 | if (gem_has_bsd2(fd)) { |
| 183 | execbuf.flags = I915_EXEC_BSD | (2 << 13); |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 184 | __gem_execbuf(fd, &execbuf); |
Chris Wilson | 310f99c | 2016-03-04 15:03:42 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Chris Wilson | 0501dcf | 2016-03-04 15:35:24 +0000 | [diff] [blame] | 187 | gem_sync(fd, obj.handle); |
| 188 | gem_close(fd, obj.handle); |
Imre Deak | a3b47db | 2013-05-28 17:22:57 +0300 | [diff] [blame] | 189 | |
Chris Wilson | 83884e9 | 2017-03-21 17:16:03 +0000 | [diff] [blame] | 190 | igt_drop_caches_set(fd, DROP_RETIRE | DROP_FREED); |
Daniel Vetter | 9f87660 | 2012-01-11 17:19:53 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 193 | /** |
Thomas Wood | 2d44386 | 2014-03-11 16:04:29 +0000 | [diff] [blame] | 194 | * drm_get_card: |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 195 | * |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 196 | * Get an i915 drm card index number for use in /dev or /sys. The minor index of |
| 197 | * the legacy node is returned, not of the control or render node. |
Thomas Wood | 2d44386 | 2014-03-11 16:04:29 +0000 | [diff] [blame] | 198 | * |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 199 | * Returns: |
| 200 | * The i915 drm index or -1 on error |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 201 | */ |
Daniel Vetter | 5951ffb | 2013-08-19 10:34:34 +0200 | [diff] [blame] | 202 | int drm_get_card(void) |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 203 | { |
| 204 | char *name; |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 205 | int i, fd; |
| 206 | |
| 207 | for (i = 0; i < 16; i++) { |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 208 | int ret; |
| 209 | |
| 210 | ret = asprintf(&name, "/dev/dri/card%u", i); |
Daniel Vetter | 8dbd1fb | 2013-08-19 11:09:25 +0200 | [diff] [blame] | 211 | igt_assert(ret != -1); |
| 212 | |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 213 | fd = open(name, O_RDWR); |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 214 | free(name); |
| 215 | |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 216 | if (fd == -1) |
| 217 | continue; |
| 218 | |
Tomeu Vizoso | 0eadf63 | 2016-03-08 09:18:45 +0100 | [diff] [blame] | 219 | if (!is_i915_device(fd) || !has_known_intel_chipset(fd)) { |
Damien Lespiau | 8329acb | 2013-10-24 15:04:26 +0100 | [diff] [blame] | 220 | close(fd); |
Daniel Vetter | 8dbd1fb | 2013-08-19 11:09:25 +0200 | [diff] [blame] | 221 | continue; |
Damien Lespiau | 8329acb | 2013-10-24 15:04:26 +0100 | [diff] [blame] | 222 | } |
Chris Wilson | 3c5c8ba | 2011-02-01 13:35:36 +0000 | [diff] [blame] | 223 | |
| 224 | close(fd); |
Daniel Vetter | 8dbd1fb | 2013-08-19 11:09:25 +0200 | [diff] [blame] | 225 | return i; |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 226 | } |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 227 | |
Daniel Vetter | 8dbd1fb | 2013-08-19 11:09:25 +0200 | [diff] [blame] | 228 | igt_skip("No intel gpu found\n"); |
| 229 | |
| 230 | return -1; |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Chris Wilson | d86d6eb | 2016-06-23 21:07:36 +0100 | [diff] [blame] | 233 | static int modprobe(const char *driver) |
Chris Wilson | 7ce6389 | 2016-06-18 00:17:06 +0100 | [diff] [blame] | 234 | { |
Chris Wilson | 137360f | 2017-03-23 17:09:26 +0000 | [diff] [blame] | 235 | return igt_kmod_load(driver, ""); |
Chris Wilson | 7ce6389 | 2016-06-18 00:17:06 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 238 | /** |
| 239 | * __drm_open_driver: |
Thomas Wood | 683316c | 2015-11-30 16:36:16 +0000 | [diff] [blame] | 240 | * @chipset: OR'd flags for each chipset to search, eg. #DRIVER_INTEL |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 241 | * |
| 242 | * Open the first DRM device we can find, searching up to 16 device nodes |
| 243 | * |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 244 | * Returns: |
| 245 | * An open DRM fd or -1 on error |
| 246 | */ |
| 247 | int __drm_open_driver(int chipset) |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 248 | { |
Chris Wilson | 7ce6389 | 2016-06-18 00:17:06 +0100 | [diff] [blame] | 249 | if (chipset & DRIVER_VGEM) |
| 250 | modprobe("vgem"); |
| 251 | |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 252 | for (int i = 0; i < 16; i++) { |
| 253 | char name[80]; |
| 254 | int fd; |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 255 | |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 256 | sprintf(name, "/dev/dri/card%u", i); |
| 257 | fd = open(name, O_RDWR); |
| 258 | if (fd == -1) |
| 259 | continue; |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 260 | |
Chris Wilson | 7ce6389 | 2016-06-18 00:17:06 +0100 | [diff] [blame] | 261 | if (chipset & DRIVER_INTEL && is_i915_device(fd) && |
| 262 | has_known_intel_chipset(fd)) |
| 263 | return fd; |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 264 | |
Chris Wilson | 7ce6389 | 2016-06-18 00:17:06 +0100 | [diff] [blame] | 265 | if (chipset & DRIVER_VC4 && |
| 266 | is_vc4_device(fd)) |
| 267 | return fd; |
Eric Anholt | 3f83f07 | 2016-01-22 17:18:56 -0800 | [diff] [blame] | 268 | |
Chris Wilson | 7ce6389 | 2016-06-18 00:17:06 +0100 | [diff] [blame] | 269 | if (chipset & DRIVER_VGEM && |
| 270 | is_vgem_device(fd)) |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 271 | return fd; |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 272 | |
Gustavo Padovan | 22779a7 | 2016-06-30 18:29:47 -0300 | [diff] [blame] | 273 | if (chipset & DRIVER_VIRTIO && |
| 274 | is_virtio_device(fd)) |
| 275 | return fd; |
| 276 | |
Chris Wilson | 36b92e8 | 2017-04-22 13:45:18 +0100 | [diff] [blame] | 277 | if (chipset & DRIVER_AMDGPU && is_amd_device(fd)) |
| 278 | return fd; |
| 279 | |
Tomeu Vizoso | 9921aff | 2016-10-17 10:05:20 +0200 | [diff] [blame] | 280 | /* Only VGEM-specific tests should be run on VGEM */ |
Tomeu Vizoso | a8d774b | 2016-10-27 10:21:05 +0200 | [diff] [blame] | 281 | if (chipset == DRIVER_ANY && !is_vgem_device(fd)) |
Tomeu Vizoso | 9921aff | 2016-10-17 10:05:20 +0200 | [diff] [blame] | 282 | return fd; |
| 283 | |
Chris Wilson | 9eb7d89 | 2013-07-03 09:58:28 +0100 | [diff] [blame] | 284 | close(fd); |
Chris Wilson | 9eb7d89 | 2013-07-03 09:58:28 +0100 | [diff] [blame] | 285 | } |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 286 | |
Chris Wilson | a6e6c5b | 2014-07-25 17:34:06 +0100 | [diff] [blame] | 287 | return -1; |
Ben Widawsky | 2585bea | 2012-03-25 16:04:07 -0700 | [diff] [blame] | 288 | } |
Eric Anholt | 8c64183 | 2009-03-26 17:15:11 -0700 | [diff] [blame] | 289 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 290 | static int __drm_open_driver_render(int chipset) |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 291 | { |
| 292 | char *name; |
| 293 | int i, fd; |
| 294 | |
| 295 | for (i = 128; i < (128 + 16); i++) { |
| 296 | int ret; |
| 297 | |
| 298 | ret = asprintf(&name, "/dev/dri/renderD%u", i); |
| 299 | igt_assert(ret != -1); |
| 300 | |
| 301 | fd = open(name, O_RDWR); |
| 302 | free(name); |
| 303 | |
| 304 | if (fd == -1) |
| 305 | continue; |
| 306 | |
Tomeu Vizoso | 0eadf63 | 2016-03-08 09:18:45 +0100 | [diff] [blame] | 307 | if (!is_i915_device(fd) || !has_known_intel_chipset(fd)) { |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 308 | close(fd); |
| 309 | fd = -1; |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | return fd; |
| 314 | } |
| 315 | |
| 316 | return fd; |
| 317 | } |
| 318 | |
Imre Deak | 2423b6c | 2014-02-05 13:48:26 +0200 | [diff] [blame] | 319 | static int at_exit_drm_fd = -1; |
| 320 | static int at_exit_drm_render_fd = -1; |
| 321 | |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 322 | static void quiescent_gpu_at_exit(int sig) |
| 323 | { |
Imre Deak | 2423b6c | 2014-02-05 13:48:26 +0200 | [diff] [blame] | 324 | if (at_exit_drm_fd < 0) |
| 325 | return; |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 326 | |
Imre Deak | 2423b6c | 2014-02-05 13:48:26 +0200 | [diff] [blame] | 327 | gem_quiescent_gpu(at_exit_drm_fd); |
| 328 | close(at_exit_drm_fd); |
| 329 | at_exit_drm_fd = -1; |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 330 | } |
| 331 | |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 332 | static void quiescent_gpu_at_exit_render(int sig) |
| 333 | { |
Imre Deak | 2423b6c | 2014-02-05 13:48:26 +0200 | [diff] [blame] | 334 | if (at_exit_drm_render_fd < 0) |
| 335 | return; |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 336 | |
Imre Deak | 2423b6c | 2014-02-05 13:48:26 +0200 | [diff] [blame] | 337 | gem_quiescent_gpu(at_exit_drm_render_fd); |
| 338 | close(at_exit_drm_render_fd); |
| 339 | at_exit_drm_render_fd = -1; |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Daniel Vetter | 542658a | 2017-07-25 13:36:17 +0200 | [diff] [blame] | 342 | static const char *chipset_to_str(int chipset) |
| 343 | { |
| 344 | switch (chipset) { |
| 345 | case DRIVER_INTEL: |
| 346 | return "intel"; |
| 347 | case DRIVER_VC4: |
| 348 | return "vc4"; |
| 349 | case DRIVER_VGEM: |
| 350 | return "vgem"; |
| 351 | case DRIVER_VIRTIO: |
| 352 | return "virtio"; |
| 353 | case DRIVER_AMDGPU: |
| 354 | return "amdgpu"; |
| 355 | case DRIVER_ANY: |
| 356 | return "any"; |
| 357 | default: |
| 358 | return "otehr"; |
| 359 | } |
| 360 | } |
| 361 | |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 362 | /** |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 363 | * drm_open_driver: |
Thomas Wood | 683316c | 2015-11-30 16:36:16 +0000 | [diff] [blame] | 364 | * @chipset: OR'd flags for each chipset to search, eg. #DRIVER_INTEL |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 365 | * |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 366 | * Open a drm legacy device node. This function always returns a valid |
Thomas Wood | 032f30c | 2015-01-13 13:33:57 +0000 | [diff] [blame] | 367 | * file descriptor. |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 368 | * |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 369 | * Returns: a drm file descriptor |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 370 | */ |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 371 | int drm_open_driver(int chipset) |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 372 | { |
| 373 | static int open_count; |
Chris Wilson | 250f047 | 2016-07-19 18:33:27 +0100 | [diff] [blame] | 374 | int fd; |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 375 | |
Chris Wilson | 250f047 | 2016-07-19 18:33:27 +0100 | [diff] [blame] | 376 | fd = __drm_open_driver(chipset); |
Daniel Vetter | 542658a | 2017-07-25 13:36:17 +0200 | [diff] [blame] | 377 | igt_skip_on_f(fd<0, "No known gpu found for chipset flags 0x%u (%s)\n", |
| 378 | chipset, chipset_to_str(chipset)); |
Chris Wilson | 9eb7d89 | 2013-07-03 09:58:28 +0100 | [diff] [blame] | 379 | |
Chris Wilson | 27d3070 | 2016-10-18 12:37:20 +0100 | [diff] [blame] | 380 | /* For i915, at least, we ensure that the driver is idle before |
| 381 | * starting a test and we install an exit handler to wait until |
| 382 | * idle before quitting. |
| 383 | */ |
Robert Foss | e1c3cde | 2016-06-28 10:01:48 +0100 | [diff] [blame] | 384 | if (is_i915_device(fd)) { |
Chris Wilson | 27d3070 | 2016-10-18 12:37:20 +0100 | [diff] [blame] | 385 | if (__sync_fetch_and_add(&open_count, 1) == 0) { |
| 386 | gem_quiescent_gpu(fd); |
| 387 | |
| 388 | at_exit_drm_fd = __drm_open_driver(chipset); |
| 389 | igt_install_exit_handler(quiescent_gpu_at_exit); |
| 390 | } |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 391 | } |
Imre Deak | 1cb4f90 | 2013-05-28 17:35:32 +0300 | [diff] [blame] | 392 | |
| 393 | return fd; |
| 394 | } |
| 395 | |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 396 | /** |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 397 | * drm_open_driver_master: |
Thomas Wood | 683316c | 2015-11-30 16:36:16 +0000 | [diff] [blame] | 398 | * @chipset: OR'd flags for each chipset to search, eg. #DRIVER_INTEL |
Imre Deak | bfda78c | 2014-09-18 18:25:37 +0300 | [diff] [blame] | 399 | * |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 400 | * 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] | 401 | * |
| 402 | * Returns: |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 403 | * The drm file descriptor or -1 on error |
Imre Deak | bfda78c | 2014-09-18 18:25:37 +0300 | [diff] [blame] | 404 | */ |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 405 | int drm_open_driver_master(int chipset) |
Imre Deak | bfda78c | 2014-09-18 18:25:37 +0300 | [diff] [blame] | 406 | { |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 407 | int fd = drm_open_driver(chipset); |
Imre Deak | bfda78c | 2014-09-18 18:25:37 +0300 | [diff] [blame] | 408 | |
Imre Deak | bfda78c | 2014-09-18 18:25:37 +0300 | [diff] [blame] | 409 | igt_require_f(drmSetMaster(fd) == 0, "Can't become DRM master, " |
| 410 | "please check if no other DRM client is running.\n"); |
| 411 | |
| 412 | return fd; |
| 413 | } |
| 414 | |
| 415 | /** |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 416 | * drm_open_driver_render: |
Thomas Wood | 683316c | 2015-11-30 16:36:16 +0000 | [diff] [blame] | 417 | * @chipset: OR'd flags for each chipset to search, eg. #DRIVER_INTEL |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 418 | * |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 419 | * Open a drm render device node. |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 420 | * |
| 421 | * Returns: |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 422 | * The drm file descriptor or -1 on error |
Daniel Vetter | 187b66d | 2014-03-23 15:03:14 +0100 | [diff] [blame] | 423 | */ |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 424 | int drm_open_driver_render(int chipset) |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 425 | { |
| 426 | static int open_count; |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 427 | int fd = __drm_open_driver_render(chipset); |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 428 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 429 | /* no render nodes, fallback to drm_open_driver() */ |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 430 | if (fd == -1) |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 431 | return drm_open_driver(chipset); |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 432 | |
| 433 | if (__sync_fetch_and_add(&open_count, 1)) |
| 434 | return fd; |
| 435 | |
Micah Fedke | e224180 | 2015-07-22 21:42:51 +0000 | [diff] [blame] | 436 | at_exit_drm_render_fd = __drm_open_driver(chipset); |
| 437 | if(chipset & DRIVER_INTEL){ |
| 438 | gem_quiescent_gpu(fd); |
| 439 | igt_install_exit_handler(quiescent_gpu_at_exit_render); |
| 440 | } |
Damien Lespiau | 69c200b | 2013-10-24 15:19:32 +0100 | [diff] [blame] | 441 | |
| 442 | return fd; |
| 443 | } |
Tomeu Vizoso | 3fee80e | 2016-02-09 10:16:51 +0100 | [diff] [blame] | 444 | |
| 445 | void igt_require_intel(int fd) |
| 446 | { |
| 447 | igt_require(is_i915_device(fd) && has_known_intel_chipset(fd)); |
| 448 | } |