lib/drmtest: extract gem_close
[platform/upstream/intel-gpu-tools.git] / tests / gem_linear_blits.c
1 /*
2  * Copyright © 2009 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  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27
28 /** @file gem_linear_blits.c
29  *
30  * This is a test of doing many blits, with a working set
31  * larger than the aperture size.
32  *
33  * The goal is to simply ensure the basics work.
34  */
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <assert.h>
40 #include <fcntl.h>
41 #include <inttypes.h>
42 #include <errno.h>
43 #include <sys/stat.h>
44 #include <sys/time.h>
45 #include "drm.h"
46 #include "i915_drm.h"
47 #include "drmtest.h"
48 #include "intel_bufmgr.h"
49 #include "intel_batchbuffer.h"
50 #include "intel_gpu_tools.h"
51
52 #define WIDTH 512
53 #define HEIGHT 512
54
55 static uint32_t linear[WIDTH*HEIGHT];
56
57 static uint32_t gem_create(int fd, int size)
58 {
59         struct drm_i915_gem_create create;
60
61         create.handle = 0;
62         create.size = size;
63         (void)drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
64         assert(create.handle);
65
66         return create.handle;
67 }
68
69 static uint64_t
70 gem_aperture_size(int fd)
71 {
72         struct drm_i915_gem_get_aperture aperture;
73
74         aperture.aper_size = 512*1024*1024;
75         (void)drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
76         return aperture.aper_size;
77 }
78
79 static void
80 gem_write(int fd, uint32_t handle, int offset, int size, const void *buf)
81 {
82         struct drm_i915_gem_pwrite pwrite;
83         int ret;
84
85         pwrite.handle = handle;
86         pwrite.offset = offset;
87         pwrite.size = size;
88         pwrite.data_ptr = (uintptr_t)buf;
89         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite);
90         assert(ret == 0);
91 }
92
93 static void
94 gem_read(int fd, uint32_t handle, int offset, int size, void *buf)
95 {
96         struct drm_i915_gem_pread pread;
97         int ret;
98
99         pread.handle = handle;
100         pread.offset = offset;
101         pread.size = size;
102         pread.data_ptr = (uintptr_t)buf;
103         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_PREAD, &pread);
104         assert(ret == 0);
105 }
106
107 static void
108 copy(int fd, uint32_t dst, uint32_t src)
109 {
110         uint32_t batch[10];
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;
114         uint32_t handle;
115         int ret;
116
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 */
122                   WIDTH*4;
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 */
127         batch[6] = WIDTH*4;
128         batch[7] = 0; /* src reloc */
129         batch[8] = MI_BATCH_BUFFER_END;
130         batch[9] = MI_NOOP;
131
132         handle = gem_create(fd, 4096);
133         gem_write(fd, handle, 0, sizeof(batch), batch);
134
135         reloc[0].target_handle = dst;
136         reloc[0].delta = 0;
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;
141
142         reloc[1].target_handle = src;
143         reloc[1].delta = 0;
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;
148
149         obj[0].handle = dst;
150         obj[0].relocation_count = 0;
151         obj[0].relocs_ptr = 0;
152         obj[0].alignment = 0;
153         obj[0].offset = 0;
154         obj[0].flags = 0;
155         obj[0].rsvd1 = 0;
156         obj[0].rsvd2 = 0;
157
158         obj[1].handle = src;
159         obj[1].relocation_count = 0;
160         obj[1].relocs_ptr = 0;
161         obj[1].alignment = 0;
162         obj[1].offset = 0;
163         obj[1].flags = 0;
164         obj[1].rsvd1 = 0;
165         obj[1].rsvd2 = 0;
166
167         obj[2].handle = handle;
168         obj[2].relocation_count = 2;
169         obj[2].relocs_ptr = (uintptr_t)reloc;
170         obj[2].alignment = 0;
171         obj[2].offset = 0;
172         obj[2].flags = 0;
173         obj[2].rsvd1 = obj[2].rsvd2 = 0;
174
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;
184
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);
189         }
190         assert(ret == 0);
191
192         gem_close(fd, handle);
193 }
194
195 static uint32_t
196 create_bo(int fd, uint32_t val)
197 {
198         uint32_t handle;
199         int i;
200
201         handle = gem_create(fd, sizeof(linear));
202
203         /* Fill the BO with dwords starting at val */
204         for (i = 0; i < WIDTH*HEIGHT; i++)
205                 linear[i] = val++;
206         gem_write(fd, handle, 0, sizeof(linear), linear);
207
208         return handle;
209 }
210
211 static void
212 check_bo(int fd, uint32_t handle, uint32_t val)
213 {
214         int i;
215
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);
222                         abort();
223                 }
224                 val++;
225         }
226 }
227
228 int main(int argc, char **argv)
229 {
230         uint32_t *handle, *start_val;
231         uint32_t start = 0;
232         int i, fd, count;
233
234         fd = drm_open_any();
235
236         count = 0;
237         if (argc > 1)
238                 count = atoi(argv[1]);
239         if (count == 0)
240                 count = 3 * gem_aperture_size(fd) / (1024*1024) / 2;
241
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");
245                 return 77;
246         }
247
248         printf("Using %d 1MiB buffers\n", count);
249
250         handle = malloc(sizeof(uint32_t)*count*2);
251         start_val = handle + count;
252
253         for (i = 0; i < count; i++) {
254                 handle[i] = create_bo(fd, start);
255                 start_val[i] = start;
256                 start += 1024 * 1024 / 4;
257         }
258
259         printf("Verifying initialisation...\n");
260         for (i = 0; i < count; i++)
261                 check_bo(fd, handle[i], start_val[i]);
262
263         printf("Cyclic blits, forward...\n");
264         for (i = 0; i < count * 4; i++) {
265                 int src = i % count;
266                 int dst = (i + 1) % count;
267
268                 copy(fd, handle[dst], handle[src]);
269                 start_val[dst] = start_val[src];
270         }
271         for (i = 0; i < count; i++)
272                 check_bo(fd, handle[i], start_val[i]);
273
274         printf("Cyclic blits, backward...\n");
275         for (i = 0; i < count * 4; i++) {
276                 int src = (i + 1) % count;
277                 int dst = i % count;
278
279                 copy(fd, handle[dst], handle[src]);
280                 start_val[dst] = start_val[src];
281         }
282         for (i = 0; i < count; i++)
283                 check_bo(fd, handle[i], start_val[i]);
284
285         printf("Random blits...\n");
286         for (i = 0; i < count * 4; i++) {
287                 int src = random() % count;
288                 int dst = random() % count;
289
290                 if (src == dst)
291                         continue;
292
293                 copy(fd, handle[dst], handle[src]);
294                 start_val[dst] = start_val[src];
295         }
296         for (i = 0; i < count; i++)
297                 check_bo(fd, handle[i], start_val[i]);
298
299         return 0;
300 }