tests: roll out igt_fixture
[platform/upstream/intel-gpu-tools.git] / tests / gem_pread_after_blit.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_pread_after_blit.c
29  *
30  * This is a test of pread's behavior when getting values out of just-drawn-to
31  * buffers.
32  *
33  * The goal is to catch failure in the whole-buffer-flush or
34  * ranged-buffer-flush paths in the kernel.
35  */
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.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 static drm_intel_bufmgr *bufmgr;
53 struct intel_batchbuffer *batch;
54 static const int width = 512, height = 512;
55 static const int size = 1024 * 1024;
56
57 #define PAGE_SIZE 4096
58
59 static drm_intel_bo *
60 create_bo(uint32_t val)
61 {
62         drm_intel_bo *bo;
63         uint32_t *vaddr;
64         int i;
65
66         bo = drm_intel_bo_alloc(bufmgr, "src bo", size, 4096);
67
68         /* Fill the BO with dwords starting at start_val */
69         drm_intel_bo_map(bo, 1);
70         vaddr = bo->virtual;
71
72         for (i = 0; i < 1024 * 1024 / 4; i++)
73                 vaddr[i] = val++;
74
75         drm_intel_bo_unmap(bo);
76
77         return bo;
78 }
79
80 static void
81 verify_large_read(drm_intel_bo *bo, uint32_t val)
82 {
83         uint32_t buf[size / 4];
84         int i;
85
86         drm_intel_bo_get_subdata(bo, 0, size, buf);
87
88         for (i = 0; i < size / 4; i++) {
89                 if (buf[i] != val) {
90                         fprintf(stderr,
91                                 "Unexpected value 0x%08x instead of "
92                                 "0x%08x at offset 0x%08x (%p)\n",
93                                 buf[i], val, i * 4, buf);
94                         abort();
95                 }
96                 val++;
97         }
98 }
99
100 /** This reads at the size that Mesa usees for software fallbacks. */
101 static void
102 verify_small_read(drm_intel_bo *bo, uint32_t val)
103 {
104         uint32_t buf[4096 / 4];
105         int offset, i;
106
107         for (i = 0; i < 4096 / 4; i++)
108                 buf[i] = 0x00c0ffee;
109
110         for (offset = 0; offset < size; offset += PAGE_SIZE) {
111                 drm_intel_bo_get_subdata(bo, offset, PAGE_SIZE, buf);
112
113                 for (i = 0; i < PAGE_SIZE; i += 4) {
114                         if (buf[i / 4] != val) {
115                                 fprintf(stderr,
116                                         "Unexpected value 0x%08x instead of "
117                                         "0x%08x at offset 0x%08x\n",
118                                         buf[i / 4], val, i * 4);
119                                 abort();
120                         }
121                         val++;
122                 }
123         }
124 }
125
126 static void do_test(int fd, int cache_level,
127                     drm_intel_bo *src[2],
128                     const uint32_t start[2],
129                     drm_intel_bo *tmp[2],
130                     int loop)
131 {
132         if (cache_level != -1) {
133                 gem_set_caching(fd, tmp[0]->handle, cache_level);
134                 gem_set_caching(fd, tmp[1]->handle, cache_level);
135         }
136
137         printf("meh");
138
139         do {
140                 /* First, do a full-buffer read after blitting */
141                 intel_copy_bo(batch, tmp[0], src[0], width, height);
142                 verify_large_read(tmp[0], start[0]);
143                 intel_copy_bo(batch, tmp[0], src[1], width, height);
144                 verify_large_read(tmp[0], start[1]);
145
146                 intel_copy_bo(batch, tmp[0], src[0], width, height);
147                 verify_small_read(tmp[0], start[0]);
148                 intel_copy_bo(batch, tmp[0], src[1], width, height);
149                 verify_small_read(tmp[0], start[1]);
150
151                 intel_copy_bo(batch, tmp[0], src[0], width, height);
152                 verify_large_read(tmp[0], start[0]);
153
154                 intel_copy_bo(batch, tmp[0], src[0], width, height);
155                 intel_copy_bo(batch, tmp[1], src[1], width, height);
156                 verify_large_read(tmp[0], start[0]);
157                 verify_large_read(tmp[1], start[1]);
158
159                 intel_copy_bo(batch, tmp[0], src[0], width, height);
160                 intel_copy_bo(batch, tmp[1], src[1], width, height);
161                 verify_large_read(tmp[1], start[1]);
162                 verify_large_read(tmp[0], start[0]);
163
164                 intel_copy_bo(batch, tmp[1], src[0], width, height);
165                 intel_copy_bo(batch, tmp[0], src[1], width, height);
166                 verify_large_read(tmp[0], start[1]);
167                 verify_large_read(tmp[1], start[0]);
168         } while (--loop);
169 }
170
171 drm_intel_bo *src[2], *dst[2];
172 int fd;
173
174 int
175 main(int argc, char **argv)
176 {
177         const uint32_t start[2] = {0, 1024 * 1024 / 4};
178
179         igt_subtest_init(argc, argv);
180         igt_skip_on_simulation();
181
182         igt_fixture {
183                 fd = drm_open_any();
184
185                 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
186                 drm_intel_bufmgr_gem_enable_reuse(bufmgr);
187                 batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
188
189                 src[0] = create_bo(start[0]);
190                 src[1] = create_bo(start[1]);
191
192                 dst[0] = drm_intel_bo_alloc(bufmgr, "dst bo", size, 4096);
193                 dst[1] = drm_intel_bo_alloc(bufmgr, "dst bo", size, 4096);
194         }
195
196         igt_subtest("normal")
197                 do_test(fd, -1, src, start, dst, 1);
198
199         igt_subtest("interruptible") {
200                 igt_fork_signal_helper();
201                 do_test(fd, -1, src, start, dst, 100);
202                 igt_stop_signal_helper();
203         }
204
205         igt_subtest("normal-uncached")
206                 do_test(fd, 0, src, start, dst, 1);
207
208         igt_subtest("interruptible-uncached") {
209                 igt_fork_signal_helper();
210                 do_test(fd, 0, src, start, dst, 100);
211                 igt_stop_signal_helper();
212         }
213
214         igt_subtest("normal-snoop")
215                 do_test(fd, 1, src, start, dst, 1);
216
217         igt_subtest("interruptible-snoop") {
218                 igt_fork_signal_helper();
219                 do_test(fd, 1, src, start, dst, 100);
220                 igt_stop_signal_helper();
221         }
222
223         igt_subtest("normal-display")
224                 do_test(fd, 2, src, start, dst, 1);
225
226         igt_subtest("interruptible-display") {
227                 igt_fork_signal_helper();
228                 do_test(fd, 2, src, start, dst, 100);
229                 igt_stop_signal_helper();
230         }
231
232         igt_fixture {
233                 drm_intel_bo_unreference(src[0]);
234                 drm_intel_bo_unreference(src[1]);
235                 drm_intel_bo_unreference(dst[0]);
236                 drm_intel_bo_unreference(dst[1]);
237
238                 intel_batchbuffer_free(batch);
239                 drm_intel_bufmgr_destroy(bufmgr);
240         }
241
242         close(fd);
243
244         igt_exit();
245 }