Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2014 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 | * Authors: |
| 24 | * Chris Wilson <chris@chris-wilson.co.uk> |
| 25 | * |
| 26 | */ |
| 27 | |
Thomas Wood | 804e11f | 2015-08-17 17:57:43 +0100 | [diff] [blame] | 28 | #include "igt.h" |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdint.h> |
| 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <inttypes.h> |
| 36 | #include <errno.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/ioctl.h> |
| 39 | #include <sys/time.h> |
| 40 | #include "drm.h" |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 41 | |
Thomas Wood | b2ac264 | 2014-11-28 11:02:44 +0000 | [diff] [blame] | 42 | IGT_TEST_DESCRIPTION("Simulates SNA behaviour using negative self-relocations" |
| 43 | " for STATE_BASE_ADDRESS command packets."); |
| 44 | |
Chris Wilson | efd5a57 | 2014-05-15 13:10:21 +0100 | [diff] [blame] | 45 | #define USE_LUT (1 << 12) |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 46 | #define BIAS (256*1024) |
Chris Wilson | efd5a57 | 2014-05-15 13:10:21 +0100 | [diff] [blame] | 47 | |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 48 | /* Simulates SNA behaviour using negative self-relocations for |
| 49 | * STATE_BASE_ADDRESS command packets. If they wrap around (to values greater |
| 50 | * than the total size of the GTT), the GPU will hang. |
| 51 | * See https://bugs.freedesktop.org/show_bug.cgi?id=78533 |
| 52 | */ |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 53 | static void negative_reloc(int fd, unsigned engine, unsigned flags) |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 54 | { |
| 55 | struct drm_i915_gem_execbuffer2 execbuf; |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 56 | struct drm_i915_gem_exec_object2 obj; |
| 57 | struct drm_i915_gem_relocation_entry reloc[1000]; |
Chris Wilson | 391b32c | 2016-02-05 18:35:21 +0000 | [diff] [blame] | 58 | uint64_t gtt_max = gem_aperture_size(fd); |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 59 | uint32_t bbe = MI_BATCH_BUFFER_END; |
| 60 | uint64_t *offsets; |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 61 | int i; |
| 62 | |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 63 | gem_require_ring(fd, engine); |
Chris Wilson | ef51988 | 2014-05-15 13:23:44 +0100 | [diff] [blame] | 64 | igt_require(intel_gen(intel_get_drm_devid(fd)) >= 7); |
| 65 | |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 66 | memset(&obj, 0, sizeof(obj)); |
| 67 | obj.handle = gem_create(fd, 8192); |
| 68 | gem_write(fd, obj.handle, 0, &bbe, sizeof(bbe)); |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 69 | |
| 70 | memset(&execbuf, 0, sizeof(execbuf)); |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 71 | execbuf.buffers_ptr = (uintptr_t)&obj; |
| 72 | execbuf.buffer_count = 1; |
| 73 | execbuf.flags = engine | (flags & USE_LUT); |
| 74 | igt_require(__gem_execbuf(fd, &execbuf) == 0); |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 75 | |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 76 | igt_info("Found offset %lld for 4k batch\n", (long long)obj.offset); |
Damien Lespiau | 88ff1ce | 2014-12-04 13:42:12 +0000 | [diff] [blame] | 77 | /* |
| 78 | * Ideally we'd like to be able to control where the kernel is going to |
| 79 | * place the buffer. We don't SKIP here because it causes the test |
| 80 | * to "randomly" flip-flop between the SKIP and PASS states. |
| 81 | */ |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 82 | if (obj.offset < BIAS) { |
Damien Lespiau | 88ff1ce | 2014-12-04 13:42:12 +0000 | [diff] [blame] | 83 | igt_info("Offset is below BIAS, not testing anything\n"); |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 84 | return; |
Damien Lespiau | 88ff1ce | 2014-12-04 13:42:12 +0000 | [diff] [blame] | 85 | } |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 86 | |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 87 | memset(reloc, 0, sizeof(reloc)); |
| 88 | for (i = 0; i < ARRAY_SIZE(reloc); i++) { |
| 89 | reloc[i].offset = 8 + 8*i; |
| 90 | reloc[i].delta = -BIAS*i/1024; |
| 91 | reloc[i].presumed_offset = -1; |
| 92 | reloc[i].target_handle = flags & USE_LUT ? 0 : obj.handle; |
| 93 | reloc[i].read_domains = I915_GEM_DOMAIN_COMMAND; |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 94 | } |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 95 | obj.relocation_count = i; |
| 96 | obj.relocs_ptr = (uintptr_t)reloc; |
| 97 | gem_execbuf(fd, &execbuf); |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 98 | |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 99 | igt_info("Batch is now at offset %#llx, max GTT %#llx\n", |
| 100 | (long long)obj.offset, (long long)gtt_max); |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 101 | |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 102 | offsets = gem_mmap__cpu(fd, obj.handle, 0, 8192, PROT_READ); |
| 103 | gem_set_domain(fd, obj.handle, I915_GEM_DOMAIN_CPU, 0); |
| 104 | gem_close(fd, obj.handle); |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 105 | |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 106 | for (i = 0; i < ARRAY_SIZE(reloc); i++) |
| 107 | igt_assert_f(offsets[1 + i] < gtt_max, |
| 108 | "Offset[%d]=%#llx, expected less than %#llx\n", |
| 109 | i, (long long)offsets[i+i], (long long)gtt_max); |
| 110 | munmap(offsets, 8192); |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 113 | static void negative_reloc_blt(int fd) |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 114 | { |
| 115 | const int gen = intel_gen(intel_get_drm_devid(fd)); |
| 116 | struct drm_i915_gem_execbuffer2 execbuf; |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 117 | struct drm_i915_gem_exec_object2 obj[1024][2]; |
| 118 | struct drm_i915_gem_relocation_entry reloc; |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 119 | uint32_t buf[1024], *b; |
| 120 | int i; |
| 121 | |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 122 | memset(&reloc, 0, sizeof(reloc)); |
| 123 | reloc.offset = 4 * sizeof(uint32_t); |
| 124 | reloc.presumed_offset = ~0ULL; |
| 125 | reloc.delta = -4096; |
| 126 | reloc.target_handle = 0; |
| 127 | reloc.read_domains = I915_GEM_DOMAIN_RENDER; |
| 128 | reloc.write_domain = I915_GEM_DOMAIN_RENDER; |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 129 | |
| 130 | for (i = 0; i < 1024; i++) { |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 131 | memset(obj[i], 0, sizeof(obj[i])); |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 132 | |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 133 | obj[i][0].handle = gem_create(fd, 4096); |
| 134 | obj[i][0].flags = EXEC_OBJECT_NEEDS_FENCE; |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 135 | |
| 136 | b = buf; |
| 137 | *b++ = XY_COLOR_BLT_CMD_NOLEN | |
| 138 | ((gen >= 8) ? 5 : 4) | |
| 139 | COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB; |
| 140 | *b++ = 0xf0 << 16 | 1 << 25 | 1 << 24 | 4096; |
| 141 | *b++ = 1 << 16 | 0; |
| 142 | *b++ = 2 << 16 | 1024; |
Chris Wilson | 8aa7fda | 2014-10-14 10:10:42 +0100 | [diff] [blame] | 143 | *b++ = ~0; |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 144 | if (gen >= 8) |
Chris Wilson | 8aa7fda | 2014-10-14 10:10:42 +0100 | [diff] [blame] | 145 | *b++ = ~0; |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 146 | *b++ = 0xc0ffee ^ i; |
| 147 | *b++ = MI_BATCH_BUFFER_END; |
| 148 | if ((b - buf) & 1) |
| 149 | *b++ = 0; |
| 150 | |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 151 | obj[i][1].handle = gem_create(fd, 4096); |
| 152 | gem_write(fd, obj[i][1].handle, 0, buf, (b - buf) * sizeof(uint32_t)); |
| 153 | obj[i][1].relocation_count = 1; |
| 154 | obj[i][1].relocs_ptr = (uintptr_t)&reloc; |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | memset(&execbuf, 0, sizeof(execbuf)); |
| 158 | execbuf.buffer_count = 2; |
| 159 | execbuf.batch_len = (b - buf) * sizeof(uint32_t); |
| 160 | execbuf.flags = USE_LUT; |
| 161 | if (gen >= 6) |
| 162 | execbuf.flags |= I915_EXEC_BLT; |
| 163 | |
| 164 | for (i = 0; i < 1024; i++) { |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 165 | execbuf.buffers_ptr = (uintptr_t)obj[i]; |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 166 | gem_execbuf(fd, &execbuf); |
| 167 | } |
| 168 | |
| 169 | for (i = 1024; i--;) { |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 170 | gem_read(fd, obj[i][0].handle, |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 171 | i*sizeof(uint32_t), buf + i, sizeof(uint32_t)); |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 172 | gem_close(fd, obj[i][0].handle); |
| 173 | gem_close(fd, obj[i][1].handle); |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | if (0) { |
| 177 | for (i = 0; i < 1024; i += 8) |
Daniel Vetter | 8b556f7 | 2014-10-23 17:54:44 +0200 | [diff] [blame] | 178 | igt_info("%08x %08x %08x %08x %08x %08x %08x %08x\n", |
| 179 | buf[i + 0], buf[i + 1], buf[i + 2], buf[i + 3], |
| 180 | buf[i + 4], buf[i + 5], buf[i + 6], buf[i + 7]); |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 181 | } |
| 182 | for (i = 0; i < 1024; i++) |
| 183 | igt_assert_eq(buf[i], 0xc0ffee ^ i); |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 186 | igt_main |
| 187 | { |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 188 | const struct intel_execution_engine *e; |
| 189 | int fd = -1; |
| 190 | |
| 191 | igt_fixture |
Micah Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 192 | fd = drm_open_driver(DRIVER_INTEL); |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 193 | |
| 194 | for (e = intel_execution_engines; e->name; e++) { |
| 195 | igt_subtest_f("negative-reloc-%s", e->name) |
| 196 | negative_reloc(fd, e->exec_id | e->flags, 0); |
| 197 | |
| 198 | igt_subtest_f("negative-reloc-lut-%s", e->name) |
| 199 | negative_reloc(fd, e->exec_id | e->flags, USE_LUT); |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Chris Wilson | 314fa17 | 2016-03-15 09:44:48 +0000 | [diff] [blame] | 202 | igt_subtest("negative-reloc-bltcopy") |
Chris Wilson | cd5ed45 | 2014-10-14 09:27:40 +0100 | [diff] [blame] | 203 | negative_reloc_blt(fd); |
| 204 | |
Chris Wilson | 1430eb0 | 2016-03-14 20:40:53 +0000 | [diff] [blame] | 205 | igt_fixture |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 206 | close(fd); |
Chris Wilson | daa9e3d | 2014-05-15 08:43:11 +0100 | [diff] [blame] | 207 | } |