1 /*
2 * Copyright © 2015 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 <[email protected]>
25 *
26 */
27
28 /* Exercises the basic execbuffer using object alignments */
29
30 #include "igt.h"
31 #include <stdlib.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/time.h>
39 #include "drm.h"
40
41 IGT_TEST_DESCRIPTION("Exercises the basic execbuffer using object alignments");
42
find_last_bit(uint64_t x)43 static uint32_t find_last_bit(uint64_t x)
44 {
45 uint32_t i = 0;
46 while (x) {
47 x >>= 1;
48 i++;
49 }
50 return i;
51 }
52
file_max(void)53 static uint32_t file_max(void)
54 {
55 static uint32_t max;
56 if (max == 0) {
57 FILE *file = fopen("/proc/sys/fs/file-max", "r");
58 max = 80000;
59 if (file) {
60 igt_assert(fscanf(file, "%d", &max) == 1);
61 fclose(file);
62 }
63 max /= 2;
64 }
65 return max;
66 }
67
many(int fd)68 static void many(int fd)
69 {
70 uint32_t bbe = MI_BATCH_BUFFER_END;
71 struct drm_i915_gem_exec_object2 *execobj;
72 struct drm_i915_gem_execbuffer2 execbuf;
73 uint64_t gtt_size, ram_size;
74 uint64_t alignment, max_alignment, count, i;
75
76 gtt_size = gem_aperture_size(fd);
77 if (!gem_uses_full_ppgtt(fd))
78 gtt_size /= 2; /* We have to *share* our GTT! */
79 ram_size = intel_get_total_ram_mb();
80 ram_size *= 1024 * 1024;
81 count = ram_size / 4096;
82 if (count > file_max()) /* vfs cap */
83 count = file_max();
84 max_alignment = find_last_bit(gtt_size / count);
85 if (max_alignment <= 13)
86 max_alignment = 4096;
87 else
88 max_alignment = 1ull << (max_alignment - 1);
89 count = gtt_size / max_alignment / 2;
90
91 igt_info("gtt_size=%lld MiB, max-alignment=%lld, count=%lld\n",
92 (long long)gtt_size/1024/1024,
93 (long long)max_alignment,
94 (long long)count);
95 intel_require_memory(count, 4096, CHECK_RAM);
96
97 execobj = calloc(sizeof(*execobj), count + 1);
98 igt_assert(execobj);
99
100 for (i = 0; i < count; i++) {
101 execobj[i].handle = gem_create(fd, 4096);
102 if ((gtt_size-1) >> 32)
103 execobj[i].flags = 1<<3; /* EXEC_OBJECT_SUPPORTS_48B_ADDRESS */
104 }
105 execobj[i].handle = gem_create(fd, 4096);
106 if ((gtt_size-1) >> 32)
107 execobj[i].flags = 1<<3; /* EXEC_OBJECT_SUPPORTS_48B_ADDRESS */
108 gem_write(fd, execobj[i].handle, 0, &bbe, sizeof(bbe));
109
110 memset(&execbuf, 0, sizeof(execbuf));
111 execbuf.buffers_ptr = to_user_pointer(execobj);
112 execbuf.buffer_count = count + 1;
113 igt_require(__gem_execbuf(fd, &execbuf) == 0);
114
115 for (alignment = 4096; alignment < gtt_size; alignment <<= 1) {
116 for (i = 0; i < count; i++)
117 execobj[i].alignment = alignment;
118 if (alignment > max_alignment) {
119 uint64_t factor = alignment / max_alignment;
120 execbuf.buffer_count = 2*count / factor;
121 execbuf.buffers_ptr =
122 to_user_pointer(execobj + count - execbuf.buffer_count + 1);
123 }
124
125 igt_debug("testing %lld x alignment=%#llx [%db]\n",
126 (long long)execbuf.buffer_count - 1,
127 (long long)alignment,
128 find_last_bit(alignment)-1);
129 gem_execbuf(fd, &execbuf);
130 for(i = count - execbuf.buffer_count + 1; i < count; i++) {
131 igt_assert_eq_u64(execobj[i].alignment, alignment);
132 igt_assert_eq_u64(execobj[i].offset % alignment, 0);
133 }
134 }
135
136 for (i = 0; i < count; i++)
137 gem_close(fd, execobj[i].handle);
138 gem_close(fd, execobj[i].handle);
139 free(execobj);
140 }
141
single(int fd)142 static void single(int fd)
143 {
144 struct drm_i915_gem_exec_object2 execobj;
145 struct drm_i915_gem_execbuffer2 execbuf;
146 uint32_t batch = MI_BATCH_BUFFER_END;
147 uint64_t gtt_size;
148 int non_pot;
149
150 memset(&execobj, 0, sizeof(execobj));
151 execobj.handle = gem_create(fd, 4096);
152 execobj.flags = 1<<3; /* EXEC_OBJECT_SUPPORTS_48B_ADDRESS */
153 gem_write(fd, execobj.handle, 0, &batch, sizeof(batch));
154
155 memset(&execbuf, 0, sizeof(execbuf));
156 execbuf.buffers_ptr = to_user_pointer(&execobj);
157 execbuf.buffer_count = 1;
158
159 gtt_size = gem_aperture_size(fd);
160 if (__gem_execbuf(fd, &execbuf)) {
161 execobj.flags = 0;
162 gtt_size = 1ull << 32;
163 gem_execbuf(fd, &execbuf);
164 }
165
166 execobj.alignment = 3*4096;
167 non_pot = __gem_execbuf(fd, &execbuf) == 0;
168 igt_debug("execbuffer() accepts non-power-of-two alignment? %s\n",
169 non_pot ? "yes" : "no");
170
171 for (execobj.alignment = 4096;
172 execobj.alignment <= 64<<20;
173 execobj.alignment += 4096) {
174 if (!non_pot && execobj.alignment & -execobj.alignment)
175 continue;
176
177 igt_debug("starting offset: %#llx, next alignment: %#llx\n",
178 (long long)execobj.offset,
179 (long long)execobj.alignment);
180 gem_execbuf(fd, &execbuf);
181 igt_assert_eq_u64(execobj.offset % execobj.alignment, 0);
182 }
183
184 for (execobj.alignment = 4096;
185 execobj.alignment < gtt_size;
186 execobj.alignment <<= 1) {
187 igt_debug("starting offset: %#llx, next alignment: %#llx [%db]\n",
188 (long long)execobj.offset,
189 (long long)execobj.alignment,
190 find_last_bit(execobj.alignment)-1);
191 gem_execbuf(fd, &execbuf);
192 igt_assert_eq_u64(execobj.offset % execobj.alignment, 0);
193 }
194
195 gem_close(fd, execobj.handle);
196 }
197
198 igt_main
199 {
200 int fd = -1;
201
202 igt_fixture {
203 fd = drm_open_driver(DRIVER_INTEL);
204 igt_require_gem(fd);
205 }
206
207 igt_subtest("single") /* basic! */
208 single(fd);
209 igt_subtest("many")
210 many(fd);
211
212 }
213