6b0588c8267f8c4411cc7d9bf51a4f598f987a09
[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 <assert.h>
41 #include <fcntl.h>
42 #include <inttypes.h>
43 #include <errno.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include "drm.h"
47 #include "i915_drm.h"
48 #include "drmtest.h"
49 #include "intel_bufmgr.h"
50 #include "intel_batchbuffer.h"
51 #include "intel_gpu_tools.h"
52
53 static drm_intel_bufmgr *bufmgr;
54 struct intel_batchbuffer *batch;
55 static const int width = 512, height = 512;
56 static const int size = 1024 * 1024;
57
58 #define PAGE_SIZE 4096
59
60 static drm_intel_bo *
61 create_bo(uint32_t val)
62 {
63         drm_intel_bo *bo;
64         uint32_t *vaddr;
65         int i;
66
67         bo = drm_intel_bo_alloc(bufmgr, "src bo", size, 4096);
68
69         /* Fill the BO with dwords starting at start_val */
70         drm_intel_bo_map(bo, 1);
71         vaddr = bo->virtual;
72
73         for (i = 0; i < 1024 * 1024 / 4; i++)
74                 vaddr[i] = val++;
75
76         drm_intel_bo_unmap(bo);
77
78         return bo;
79 }
80
81 static void
82 verify_large_read(drm_intel_bo *bo, uint32_t val)
83 {
84         uint32_t buf[size / 4];
85         int i;
86
87         drm_intel_bo_get_subdata(bo, 0, size, buf);
88
89         for (i = 0; i < size / 4; i++) {
90                 if (buf[i] != val) {
91                         fprintf(stderr,
92                                 "Unexpected value 0x%08x instead of "
93                                 "0x%08x at offset 0x%08x (%p)\n",
94                                 buf[i], val, i * 4, buf);
95                         abort();
96                 }
97                 val++;
98         }
99 }
100
101 /** This reads at the size that Mesa usees for software fallbacks. */
102 static void
103 verify_small_read(drm_intel_bo *bo, uint32_t val)
104 {
105         uint32_t buf[4096 / 4];
106         int offset, i;
107
108         for (i = 0; i < 4096 / 4; i++)
109                 buf[i] = 0x00c0ffee;
110
111         for (offset = 0; offset < size; offset += PAGE_SIZE) {
112                 drm_intel_bo_get_subdata(bo, offset, PAGE_SIZE, buf);
113
114                 for (i = 0; i < PAGE_SIZE; i += 4) {
115                         if (buf[i / 4] != val) {
116                                 fprintf(stderr,
117                                         "Unexpected value 0x%08x instead of "
118                                         "0x%08x at offset 0x%08x\n",
119                                         buf[i / 4], val, i * 4);
120                                 abort();
121                         }
122                         val++;
123                 }
124         }
125 }
126
127 static void do_test(int fd, int cache_level,
128                     drm_intel_bo *src[2],
129                     const uint32_t start[2],
130                     drm_intel_bo *tmp[2],
131                     int loop)
132 {
133         if (cache_level != -1) {
134                 gem_set_caching(fd, tmp[0]->handle, cache_level);
135                 gem_set_caching(fd, tmp[1]->handle, cache_level);
136         }
137
138         printf("meh");
139
140         do {
141                 /* First, do a full-buffer read after blitting */
142                 intel_copy_bo(batch, tmp[0], src[0], width, height);
143                 verify_large_read(tmp[0], start[0]);
144                 intel_copy_bo(batch, tmp[0], src[1], width, height);
145                 verify_large_read(tmp[0], start[1]);
146
147                 intel_copy_bo(batch, tmp[0], src[0], width, height);
148                 verify_small_read(tmp[0], start[0]);
149                 intel_copy_bo(batch, tmp[0], src[1], width, height);
150                 verify_small_read(tmp[0], start[1]);
151
152                 intel_copy_bo(batch, tmp[0], src[0], width, height);
153                 verify_large_read(tmp[0], start[0]);
154
155                 intel_copy_bo(batch, tmp[0], src[0], width, height);
156                 intel_copy_bo(batch, tmp[1], src[1], width, height);
157                 verify_large_read(tmp[0], start[0]);
158                 verify_large_read(tmp[1], start[1]);
159
160                 intel_copy_bo(batch, tmp[0], src[0], width, height);
161                 intel_copy_bo(batch, tmp[1], src[1], width, height);
162                 verify_large_read(tmp[1], start[1]);
163                 verify_large_read(tmp[0], start[0]);
164
165                 intel_copy_bo(batch, tmp[1], src[0], width, height);
166                 intel_copy_bo(batch, tmp[0], src[1], width, height);
167                 verify_large_read(tmp[0], start[1]);
168                 verify_large_read(tmp[1], start[0]);
169         } while (--loop);
170 }
171
172 int
173 main(int argc, char **argv)
174 {
175         const uint32_t start[2] = {0, 1024 * 1024 / 4};
176         drm_intel_bo *src[2], *dst[2];
177         int fd;
178
179         igt_subtest_init(argc, argv);
180         igt_skip_on_simulation();
181
182         fd = drm_open_any();
183
184         bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
185         drm_intel_bufmgr_gem_enable_reuse(bufmgr);
186         batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
187
188         src[0] = create_bo(start[0]);
189         src[1] = create_bo(start[1]);
190
191         dst[0] = drm_intel_bo_alloc(bufmgr, "dst bo", size, 4096);
192         dst[1] = drm_intel_bo_alloc(bufmgr, "dst bo", size, 4096);
193
194         igt_subtest("normal")
195                 do_test(fd, -1, src, start, dst, 1);
196
197         igt_subtest("interruptible") {
198                 igt_fork_signal_helper();
199                 do_test(fd, -1, src, start, dst, 100);
200                 igt_stop_signal_helper();
201         }
202
203         igt_subtest("normal-uncached")
204                 do_test(fd, 0, src, start, dst, 1);
205
206         igt_subtest("interruptible-uncached") {
207                 igt_fork_signal_helper();
208                 do_test(fd, 0, src, start, dst, 100);
209                 igt_stop_signal_helper();
210         }
211
212         igt_subtest("normal-snoop")
213                 do_test(fd, 1, src, start, dst, 1);
214
215         igt_subtest("interruptible-snoop") {
216                 igt_fork_signal_helper();
217                 do_test(fd, 1, src, start, dst, 100);
218                 igt_stop_signal_helper();
219         }
220
221         igt_subtest("normal-display")
222                 do_test(fd, 2, src, start, dst, 1);
223
224         igt_subtest("interruptible-display") {
225                 igt_fork_signal_helper();
226                 do_test(fd, 2, src, start, dst, 100);
227                 igt_stop_signal_helper();
228         }
229         drm_intel_bo_unreference(src[0]);
230         drm_intel_bo_unreference(src[1]);
231         drm_intel_bo_unreference(dst[0]);
232         drm_intel_bo_unreference(dst[1]);
233
234         intel_batchbuffer_free(batch);
235         drm_intel_bufmgr_destroy(bufmgr);
236
237         close(fd);
238
239         return igt_retval();
240 }