blob: efd612bb61c43d0fd0b185c781a83ba5ce8256c1 [file] [log] [blame]
Chris Wilson51fcf032016-05-19 12:26:59 +01001/*
2 * Copyright © 2016 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#include "igt.h"
Chris Wilson376b8132016-07-03 09:42:38 +010025#include "igt_rand.h"
Chris Wilson51fcf032016-05-19 12:26:59 +010026
27IGT_TEST_DESCRIPTION("Fill the GTT with batches.");
28
29#define BATCH_SIZE (4096<<10)
30
Chris Wilson840d12e2018-06-28 22:18:52 +010031struct batch {
32 uint32_t handle;
33 void *ptr;
34};
35
36static void xchg_batch(void *array, unsigned int i, unsigned int j)
Chris Wilson51fcf032016-05-19 12:26:59 +010037{
Chris Wilson840d12e2018-06-28 22:18:52 +010038 struct batch *batches = array;
39 struct batch tmp;
40
41 tmp = batches[i];
42 batches[i] = batches[j];
43 batches[j] = tmp;
Chris Wilson51fcf032016-05-19 12:26:59 +010044}
45
Chris Wilson09605652016-05-19 16:12:34 +010046static void submit(int fd, int gen,
47 struct drm_i915_gem_execbuffer2 *eb,
48 struct drm_i915_gem_relocation_entry *reloc,
Chris Wilson840d12e2018-06-28 22:18:52 +010049 struct batch *batches, unsigned int count)
Chris Wilson09605652016-05-19 16:12:34 +010050{
51 struct drm_i915_gem_exec_object2 obj;
Chris Wilson376b8132016-07-03 09:42:38 +010052 uint32_t batch[16];
53 unsigned n;
54
55 memset(&obj, 0, sizeof(obj));
Chris Wilson4de67b22017-01-02 11:05:21 +000056 obj.relocs_ptr = to_user_pointer(reloc);
Chris Wilson376b8132016-07-03 09:42:38 +010057 obj.relocation_count = 2;
58
59 memset(reloc, 0, 2*sizeof(*reloc));
60 reloc[0].offset = eb->batch_start_offset;
61 reloc[0].offset += sizeof(uint32_t);
62 reloc[0].delta = BATCH_SIZE - eb->batch_start_offset - 8;
63 reloc[0].read_domains = I915_GEM_DOMAIN_INSTRUCTION;
64 reloc[1].offset = eb->batch_start_offset;
65 reloc[1].offset += 3*sizeof(uint32_t);
66 reloc[1].read_domains = I915_GEM_DOMAIN_INSTRUCTION;
67
68 n = 0;
69 batch[n] = MI_STORE_DWORD_IMM | (gen < 6 ? 1 << 22 : 0);
70 if (gen >= 8) {
71 batch[n] |= 1 << 21;
72 batch[n]++;
73 batch[++n] = reloc[0].delta;/* lower_32_bits(address) */
74 batch[++n] = 0; /* upper_32_bits(address) */
75 } else if (gen >= 4) {
76 batch[++n] = 0;
77 batch[++n] = reloc[0].delta;/* lower_32_bits(address) */
78 reloc[0].offset += sizeof(uint32_t);
79 } else {
80 batch[n]--;
81 batch[++n] = reloc[0].delta;/* lower_32_bits(address) */
82 reloc[1].offset -= sizeof(uint32_t);
83 }
84 batch[++n] = 0; /* lower_32_bits(value) */
85 batch[++n] = 0; /* upper_32_bits(value) / nop */
86 batch[++n] = MI_BATCH_BUFFER_END;
Chris Wilson09605652016-05-19 16:12:34 +010087
Chris Wilson4de67b22017-01-02 11:05:21 +000088 eb->buffers_ptr = to_user_pointer(&obj);
Chris Wilson09605652016-05-19 16:12:34 +010089 for (unsigned i = 0; i < count; i++) {
Chris Wilson840d12e2018-06-28 22:18:52 +010090 obj.handle = batches[i].handle;
Chris Wilson09605652016-05-19 16:12:34 +010091 reloc[0].target_handle = obj.handle;
Chris Wilson09605652016-05-19 16:12:34 +010092 reloc[1].target_handle = obj.handle;
Chris Wilson09605652016-05-19 16:12:34 +010093
Chris Wilson376b8132016-07-03 09:42:38 +010094 obj.offset = 0;
95 reloc[0].presumed_offset = obj.offset;
96 reloc[1].presumed_offset = obj.offset;
97
Chris Wilson840d12e2018-06-28 22:18:52 +010098 memcpy(batches[i].ptr + eb->batch_start_offset,
99 batch, sizeof(batch));
Chris Wilson09605652016-05-19 16:12:34 +0100100
101 gem_execbuf(fd, eb);
102 }
Chris Wilson376b8132016-07-03 09:42:38 +0100103 /* As we have been lying about the write_domain, we need to do a sync */
104 gem_sync(fd, obj.handle);
Chris Wilson09605652016-05-19 16:12:34 +0100105}
106
Chris Wilson51fcf032016-05-19 12:26:59 +0100107static void fillgtt(int fd, unsigned ring, int timeout)
108{
109 const int gen = intel_gen(intel_get_drm_devid(fd));
110 struct drm_i915_gem_execbuffer2 execbuf;
111 struct drm_i915_gem_relocation_entry reloc[2];
Chris Wilson376b8132016-07-03 09:42:38 +0100112 volatile uint64_t *shared;
Chris Wilson840d12e2018-06-28 22:18:52 +0100113 struct batch *batches;
Chris Wilson51fcf032016-05-19 12:26:59 +0100114 unsigned engines[16];
115 unsigned nengine;
116 unsigned engine;
117 uint64_t size;
118 unsigned count;
119
Chris Wilson376b8132016-07-03 09:42:38 +0100120 shared = mmap(NULL, 4096, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
121 igt_assert(shared != MAP_FAILED);
122
Chris Wilson51fcf032016-05-19 12:26:59 +0100123 nengine = 0;
124 if (ring == 0) {
Chris Wilson305ebce2018-02-21 14:13:16 +0000125 for_each_physical_engine(fd, engine) {
Chris Wilsonbc787762017-05-18 12:11:59 +0100126 if (!gem_can_store_dword(fd, engine))
Chris Wilson51fcf032016-05-19 12:26:59 +0100127 continue;
128
129 engines[nengine++] = engine;
130 }
131 } else {
132 gem_require_ring(fd, ring);
Chris Wilsonbc787762017-05-18 12:11:59 +0100133 igt_require(gem_can_store_dword(fd, ring));
Chris Wilson51fcf032016-05-19 12:26:59 +0100134
135 engines[nengine++] = ring;
136 }
137 igt_require(nengine);
138
139 size = gem_aperture_size(fd);
Chris Wilsona7d58542016-05-20 20:56:29 +0100140 if (size > 1ull<<32) /* Limit to 4GiB as we do not use allow-48b */
141 size = 1ull << 32;
Chris Wilson51fcf032016-05-19 12:26:59 +0100142 igt_require(size < (1ull<<32) * BATCH_SIZE);
143
144 count = size / BATCH_SIZE + 1;
145 igt_debug("Using %'d batches to fill %'llu aperture on %d engines\n",
146 count, (long long)size, nengine);
Chris Wilsona7d58542016-05-20 20:56:29 +0100147 intel_require_memory(count, BATCH_SIZE, CHECK_RAM);
Chris Wilsona56cacc2016-05-21 20:58:58 +0100148 intel_detect_and_clear_missed_interrupts(fd);
Chris Wilson51fcf032016-05-19 12:26:59 +0100149
150 memset(&execbuf, 0, sizeof(execbuf));
151 execbuf.buffer_count = 1;
Chris Wilson51fcf032016-05-19 12:26:59 +0100152 if (gen < 6)
153 execbuf.flags |= I915_EXEC_SECURE;
154
Chris Wilson840d12e2018-06-28 22:18:52 +0100155 batches = calloc(count, sizeof(*batches));
156 igt_assert(batches);
157 for (unsigned i = 0; i < count; i++) {
158 batches[i].handle = gem_create(fd, BATCH_SIZE);
159 batches[i].ptr =
160 __gem_mmap__wc(fd, batches[i].handle,
161 0, BATCH_SIZE, PROT_WRITE);
162 if (!batches[i].ptr) {
163 batches[i].ptr =
164 __gem_mmap__gtt(fd, batches[i].handle,
165 BATCH_SIZE, PROT_WRITE);
166 }
167 igt_require(batches[i].ptr);
168 }
Chris Wilson51fcf032016-05-19 12:26:59 +0100169
Chris Wilson09605652016-05-19 16:12:34 +0100170 /* Flush all memory before we start the timer */
Chris Wilson840d12e2018-06-28 22:18:52 +0100171 submit(fd, gen, &execbuf, reloc, batches, count);
Chris Wilson09605652016-05-19 16:12:34 +0100172
Chris Wilson51fcf032016-05-19 12:26:59 +0100173 igt_fork(child, nengine) {
Chris Wilson376b8132016-07-03 09:42:38 +0100174 uint64_t cycles = 0;
175 hars_petruska_f54_1_random_perturb(child);
Chris Wilson840d12e2018-06-28 22:18:52 +0100176 igt_permute_array(batches, count, xchg_batch);
Chris Wilson51fcf032016-05-19 12:26:59 +0100177 execbuf.batch_start_offset = child*64;
178 execbuf.flags |= engines[child];
179 igt_until_timeout(timeout) {
Chris Wilson840d12e2018-06-28 22:18:52 +0100180 submit(fd, gen, &execbuf, reloc, batches, count);
Chris Wilson51fcf032016-05-19 12:26:59 +0100181 for (unsigned i = 0; i < count; i++) {
Chris Wilson840d12e2018-06-28 22:18:52 +0100182 uint64_t offset, delta;
Chris Wilson51fcf032016-05-19 12:26:59 +0100183
Chris Wilson840d12e2018-06-28 22:18:52 +0100184 offset = *(uint64_t *)(batches[i].ptr + reloc[1].offset);
185 delta = *(uint64_t *)(batches[i].ptr + reloc[0].delta);
186 igt_assert_eq_u64(offset, delta);
Chris Wilson51fcf032016-05-19 12:26:59 +0100187 }
Chris Wilson376b8132016-07-03 09:42:38 +0100188 cycles++;
Chris Wilson51fcf032016-05-19 12:26:59 +0100189 }
Chris Wilson376b8132016-07-03 09:42:38 +0100190 shared[child] = cycles;
191 igt_info("engine[%d]: %llu cycles\n", child, (long long)cycles);
Chris Wilson51fcf032016-05-19 12:26:59 +0100192 }
193 igt_waitchildren();
194
Chris Wilson840d12e2018-06-28 22:18:52 +0100195 for (unsigned i = 0; i < count; i++) {
196 munmap(batches[i].ptr, BATCH_SIZE);
197 gem_close(fd, batches[i].handle);
198 }
Chris Wilsona56cacc2016-05-21 20:58:58 +0100199
Chris Wilson376b8132016-07-03 09:42:38 +0100200 shared[nengine] = 0;
201 for (unsigned i = 0; i < nengine; i++)
202 shared[nengine] += shared[i];
203 igt_info("Total: %llu cycles\n", (long long)shared[nengine]);
204
Chris Wilsona56cacc2016-05-21 20:58:58 +0100205 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Chris Wilson51fcf032016-05-19 12:26:59 +0100206}
207
208igt_main
209{
210 const struct intel_execution_engine *e;
211 int device = -1;
212
213 igt_skip_on_simulation();
214
215 igt_fixture {
216 device = drm_open_driver(DRIVER_INTEL);
Chris Wilson9518cb52017-02-22 15:24:54 +0000217 igt_require_gem(device);
Chris Wilsonbc787762017-05-18 12:11:59 +0100218 igt_require(gem_can_store_dword(device, 0));
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200219 igt_fork_hang_detector(device);
Chris Wilson51fcf032016-05-19 12:26:59 +0100220 }
221
Chris Wilson51fcf032016-05-19 12:26:59 +0100222 igt_subtest("basic")
Chris Wilsona7d58542016-05-20 20:56:29 +0100223 fillgtt(device, 0, 1); /* just enough to run a single pass */
Chris Wilson51fcf032016-05-19 12:26:59 +0100224
225 for (e = intel_execution_engines; e->name; e++)
226 igt_subtest_f("%s", e->name)
227 fillgtt(device, e->exec_id | e->flags, 20);
228
229 igt_subtest("all")
230 fillgtt(device, 0, 150);
231
Chris Wilson51fcf032016-05-19 12:26:59 +0100232 igt_fixture {
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200233 igt_stop_hang_detector();
Chris Wilson51fcf032016-05-19 12:26:59 +0100234 close(device);
235 }
236}