kms_rotation_crc: Remove useless comments
[platform/upstream/intel-gpu-tools.git] / tests / gem_tiled_pread.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_tiled_pread.c
29  *
30  * This is a test of pread's behavior on tiled objects with respect to the
31  * reported swizzling value.
32  *
33  * The goal is to exercise the slow_bit17_copy path for reading on bit17
34  * machines, but will also be useful for catching swizzling value bugs on
35  * other systems.
36  */
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.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 <sys/ioctl.h>
47 #include "drm.h"
48 #include "ioctl_wrappers.h"
49 #include "drmtest.h"
50 #include "intel_io.h"
51 #include "intel_chipset.h"
52
53 #define WIDTH 512
54 #define HEIGHT 512
55 static uint32_t linear[WIDTH * HEIGHT];
56
57 #define PAGE_SIZE 4096
58
59 static int tile_width;
60 static int tile_height;
61 static int tile_size;
62
63 static void
64 gem_get_tiling(int fd, uint32_t handle, uint32_t *tiling, uint32_t *swizzle)
65 {
66         struct drm_i915_gem_get_tiling get_tiling;
67         int ret;
68
69         memset(&get_tiling, 0, sizeof(get_tiling));
70         get_tiling.handle = handle;
71
72         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling);
73         igt_assert(ret == 0);
74
75         *tiling = get_tiling.tiling_mode;
76         *swizzle = get_tiling.swizzle_mode;
77 }
78
79 static uint32_t
80 create_bo(int fd)
81 {
82         uint32_t handle;
83         uint32_t *data;
84         int i;
85
86         handle = gem_create(fd, sizeof(linear));
87         gem_set_tiling(fd, handle, I915_TILING_X, WIDTH * sizeof(uint32_t));
88
89         /* Fill the BO with dwords starting at start_val */
90         data = gem_mmap(fd, handle, sizeof(linear), PROT_READ | PROT_WRITE);
91         for (i = 0; i < WIDTH*HEIGHT; i++)
92                 data[i] = i;
93         munmap(data, sizeof(linear));
94
95         return handle;
96 }
97
98 static int
99 swizzle_bit(int bit, int offset)
100 {
101         return (offset & (1 << bit)) >> (bit - 6);
102 }
103
104 /* Translate from a swizzled offset in the tiled buffer to the corresponding
105  * value from the original linear buffer.
106  */
107 static uint32_t
108 calculate_expected(int offset)
109 {
110         int tile_off = offset & (tile_size - 1);
111         int tile_base = offset & -tile_size;
112         int tile_index = tile_base / tile_size;
113         int tiles_per_row = 4*WIDTH / tile_width;
114
115         /* base x,y values from the tile (page) index. */
116         int base_y = tile_index / tiles_per_row * tile_height;
117         int base_x = tile_index % tiles_per_row * (tile_width/4);
118
119         /* x, y offsets within the tile */
120         int tile_y = tile_off / tile_width;
121         int tile_x = (tile_off % tile_width) / 4;
122
123         igt_debug("%3d, %3d, %3d,%3d\n", base_x, base_y, tile_x, tile_y);
124         return (base_y + tile_y) * WIDTH + base_x + tile_x;
125 }
126
127 igt_simple_main
128 {
129         int fd;
130         int i, iter = 100;
131         uint32_t tiling, swizzle;
132         uint32_t handle;
133         uint32_t devid;
134
135         fd = drm_open_any();
136
137         handle = create_bo(fd);
138         gem_get_tiling(fd, handle, &tiling, &swizzle);
139
140         devid = intel_get_drm_devid(fd);
141
142         if (IS_GEN2(devid)) {
143                 tile_height = 16;
144                 tile_width = 128;
145                 tile_size = 2048;
146         } else {
147                 tile_height = 8;
148                 tile_width = 512;
149                 tile_size = PAGE_SIZE;
150         }
151
152         /* Read a bunch of random subsets of the data and check that they come
153          * out right.
154          */
155         for (i = 0; i < iter; i++) {
156                 int size = WIDTH * HEIGHT * 4;
157                 int offset = (random() % size) & ~3;
158                 int len = (random() % size) & ~3;
159                 int j;
160
161                 if (len == 0)
162                         len = 4;
163
164                 if (offset + len > size)
165                         len = size - offset;
166
167                 if (i == 0) {
168                         offset = 0;
169                         len = size;
170                 }
171
172                 gem_read(fd, handle, offset, linear, len);
173
174                 /* Translate from offsets in the read buffer to the swizzled
175                  * address that it corresponds to.  This is the opposite of
176                  * what Mesa does (calculate offset to be read given the linear
177                  * offset it's looking for).
178                  */
179                 for (j = offset; j < offset + len; j += 4) {
180                         uint32_t expected_val, found_val;
181                         int swizzled_offset;
182                         const char *swizzle_str;
183
184                         switch (swizzle) {
185                         case I915_BIT_6_SWIZZLE_NONE:
186                                 swizzled_offset = j;
187                                 swizzle_str = "none";
188                                 break;
189                         case I915_BIT_6_SWIZZLE_9:
190                                 swizzled_offset = j ^
191                                         swizzle_bit(9, j);
192                                 swizzle_str = "bit9";
193                                 break;
194                         case I915_BIT_6_SWIZZLE_9_10:
195                                 swizzled_offset = j ^
196                                         swizzle_bit(9, j) ^
197                                         swizzle_bit(10, j);
198                                 swizzle_str = "bit9^10";
199                                 break;
200                         case I915_BIT_6_SWIZZLE_9_11:
201                                 swizzled_offset = j ^
202                                         swizzle_bit(9, j) ^
203                                         swizzle_bit(11, j);
204                                 swizzle_str = "bit9^11";
205                                 break;
206                         case I915_BIT_6_SWIZZLE_9_10_11:
207                                 swizzled_offset = j ^
208                                         swizzle_bit(9, j) ^
209                                         swizzle_bit(10, j) ^
210                                         swizzle_bit(11, j);
211                                 swizzle_str = "bit9^10^11";
212                                 break;
213                         default:
214                                 igt_assert_f(0, "Bad swizzle bits; %d\n",
215                                              swizzle);
216                         }
217                         expected_val = calculate_expected(swizzled_offset);
218                         found_val = linear[(j - offset) / 4];
219                         igt_assert_f(expected_val == found_val,
220                                      "Bad read [%d]: %d instead of %d at 0x%08x "
221                                      "for read from 0x%08x to 0x%08x, swizzle=%s\n",
222                                      i, found_val, expected_val, j,
223                                      offset, offset + len,
224                                      swizzle_str);
225                 }
226         }
227
228         close(fd);
229 }