2 * Copyright © 2009 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
28 /** @file gem_linear_blits.c
30 * This is a test of doing many blits, with a working set
31 * larger than the aperture size.
33 * The goal is to simply ensure the basics work.
48 #include "intel_bufmgr.h"
49 #include "intel_batchbuffer.h"
50 #include "intel_gpu_tools.h"
55 static uint32_t linear[WIDTH*HEIGHT];
57 static uint32_t gem_create(int fd, int size)
59 struct drm_i915_gem_create create;
63 (void)drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
64 assert(create.handle);
70 gem_aperture_size(int fd)
72 struct drm_i915_gem_get_aperture aperture;
74 aperture.aper_size = 512*1024*1024;
75 (void)drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
76 return aperture.aper_size;
80 gem_write(int fd, uint32_t handle, int offset, int size, const void *buf)
82 struct drm_i915_gem_pwrite pwrite;
85 pwrite.handle = handle;
86 pwrite.offset = offset;
88 pwrite.data_ptr = (uintptr_t)buf;
89 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite);
94 gem_read(int fd, uint32_t handle, int offset, int size, void *buf)
96 struct drm_i915_gem_pread pread;
99 pread.handle = handle;
100 pread.offset = offset;
102 pread.data_ptr = (uintptr_t)buf;
103 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_PREAD, &pread);
108 copy(int fd, uint32_t dst, uint32_t src)
111 struct drm_i915_gem_relocation_entry reloc[2];
112 struct drm_i915_gem_exec_object2 obj[3];
113 struct drm_i915_gem_execbuffer2 exec;
117 batch[0] = XY_SRC_COPY_BLT_CMD |
118 XY_SRC_COPY_BLT_WRITE_ALPHA |
119 XY_SRC_COPY_BLT_WRITE_RGB;
120 batch[1] = (3 << 24) | /* 32 bits */
121 (0xcc << 16) | /* copy ROP */
123 batch[2] = 0; /* dst x1,y1 */
124 batch[3] = (HEIGHT << 16) | WIDTH; /* dst x2,y2 */
125 batch[4] = 0; /* dst reloc */
126 batch[5] = 0; /* src x1,y1 */
128 batch[7] = 0; /* src reloc */
129 batch[8] = MI_BATCH_BUFFER_END;
132 handle = gem_create(fd, 4096);
133 gem_write(fd, handle, 0, sizeof(batch), batch);
135 reloc[0].target_handle = dst;
137 reloc[0].offset = 4 * sizeof(batch[0]);
138 reloc[0].presumed_offset = 0;
139 reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;;
140 reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
142 reloc[1].target_handle = src;
144 reloc[1].offset = 7 * sizeof(batch[0]);
145 reloc[1].presumed_offset = 0;
146 reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;;
147 reloc[1].write_domain = 0;
150 obj[0].relocation_count = 0;
151 obj[0].relocs_ptr = 0;
152 obj[0].alignment = 0;
159 obj[1].relocation_count = 0;
160 obj[1].relocs_ptr = 0;
161 obj[1].alignment = 0;
167 obj[2].handle = handle;
168 obj[2].relocation_count = 2;
169 obj[2].relocs_ptr = (uintptr_t)reloc;
170 obj[2].alignment = 0;
173 obj[2].rsvd1 = obj[2].rsvd2 = 0;
175 exec.buffers_ptr = (uintptr_t)obj;
176 exec.buffer_count = 3;
177 exec.batch_start_offset = 0;
178 exec.batch_len = sizeof(batch);
179 exec.DR1 = exec.DR4 = 0;
180 exec.num_cliprects = 0;
181 exec.cliprects_ptr = 0;
182 exec.flags = HAS_BLT_RING(intel_get_drm_devid(fd)) ? I915_EXEC_BLT : 0;
183 exec.rsvd1 = exec.rsvd2 = 0;
185 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
186 while (ret && errno == EBUSY) {
187 drmCommandNone(fd, DRM_I915_GEM_THROTTLE);
188 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
192 gem_close(fd, handle);
196 create_bo(int fd, uint32_t val)
201 handle = gem_create(fd, sizeof(linear));
203 /* Fill the BO with dwords starting at val */
204 for (i = 0; i < WIDTH*HEIGHT; i++)
206 gem_write(fd, handle, 0, sizeof(linear), linear);
212 check_bo(int fd, uint32_t handle, uint32_t val)
216 gem_read(fd, handle, 0, sizeof(linear), linear);
217 for (i = 0; i < WIDTH*HEIGHT; i++) {
218 if (linear[i] != val) {
219 fprintf(stderr, "Expected 0x%08x, found 0x%08x "
220 "at offset 0x%08x\n",
221 val, linear[i], i * 4);
228 int main(int argc, char **argv)
230 uint32_t *handle, *start_val;
238 count = atoi(argv[1]);
240 count = 3 * gem_aperture_size(fd) / (1024*1024) / 2;
242 if (count > intel_get_total_ram_mb() * 9 / 10) {
243 count = intel_get_total_ram_mb() * 9 / 10;
244 fprintf(stderr, "not enough RAM to run test, reducing buffer count\n");
248 printf("Using %d 1MiB buffers\n", count);
250 handle = malloc(sizeof(uint32_t)*count*2);
251 start_val = handle + count;
253 for (i = 0; i < count; i++) {
254 handle[i] = create_bo(fd, start);
255 start_val[i] = start;
256 start += 1024 * 1024 / 4;
259 printf("Verifying initialisation...\n");
260 for (i = 0; i < count; i++)
261 check_bo(fd, handle[i], start_val[i]);
263 printf("Cyclic blits, forward...\n");
264 for (i = 0; i < count * 4; i++) {
266 int dst = (i + 1) % count;
268 copy(fd, handle[dst], handle[src]);
269 start_val[dst] = start_val[src];
271 for (i = 0; i < count; i++)
272 check_bo(fd, handle[i], start_val[i]);
274 printf("Cyclic blits, backward...\n");
275 for (i = 0; i < count * 4; i++) {
276 int src = (i + 1) % count;
279 copy(fd, handle[dst], handle[src]);
280 start_val[dst] = start_val[src];
282 for (i = 0; i < count; i++)
283 check_bo(fd, handle[i], start_val[i]);
285 printf("Random blits...\n");
286 for (i = 0; i < count * 4; i++) {
287 int src = random() % count;
288 int dst = random() % count;
293 copy(fd, handle[dst], handle[src]);
294 start_val[dst] = start_val[src];
296 for (i = 0; i < count; i++)
297 check_bo(fd, handle[i], start_val[i]);