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_tiled_pread.c
30 * This is a test of pread's behavior on tiled objects with respect to the
31 * reported swizzling value.
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
46 #include <sys/ioctl.h>
48 #include "ioctl_wrappers.h"
51 #include "intel_chipset.h"
55 static uint32_t linear[WIDTH * HEIGHT];
57 #define PAGE_SIZE 4096
59 static int tile_width;
60 static int tile_height;
64 gem_get_tiling(int fd, uint32_t handle, uint32_t *tiling, uint32_t *swizzle)
66 struct drm_i915_gem_get_tiling get_tiling;
69 memset(&get_tiling, 0, sizeof(get_tiling));
70 get_tiling.handle = handle;
72 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling);
75 *tiling = get_tiling.tiling_mode;
76 *swizzle = get_tiling.swizzle_mode;
86 handle = gem_create(fd, sizeof(linear));
87 gem_set_tiling(fd, handle, I915_TILING_X, WIDTH * sizeof(uint32_t));
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++)
93 munmap(data, sizeof(linear));
99 swizzle_bit(int bit, int offset)
101 return (offset & (1 << bit)) >> (bit - 6);
104 /* Translate from a swizzled offset in the tiled buffer to the corresponding
105 * value from the original linear buffer.
108 calculate_expected(int offset)
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;
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);
119 /* x, y offsets within the tile */
120 int tile_y = tile_off / tile_width;
121 int tile_x = (tile_off % tile_width) / 4;
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;
131 uint32_t tiling, swizzle;
137 handle = create_bo(fd);
138 gem_get_tiling(fd, handle, &tiling, &swizzle);
140 devid = intel_get_drm_devid(fd);
142 if (IS_GEN2(devid)) {
149 tile_size = PAGE_SIZE;
152 /* Read a bunch of random subsets of the data and check that they come
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;
164 if (offset + len > size)
172 gem_read(fd, handle, offset, linear, len);
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).
179 for (j = offset; j < offset + len; j += 4) {
180 uint32_t expected_val, found_val;
182 const char *swizzle_str;
185 case I915_BIT_6_SWIZZLE_NONE:
187 swizzle_str = "none";
189 case I915_BIT_6_SWIZZLE_9:
190 swizzled_offset = j ^
192 swizzle_str = "bit9";
194 case I915_BIT_6_SWIZZLE_9_10:
195 swizzled_offset = j ^
198 swizzle_str = "bit9^10";
200 case I915_BIT_6_SWIZZLE_9_11:
201 swizzled_offset = j ^
204 swizzle_str = "bit9^11";
206 case I915_BIT_6_SWIZZLE_9_10_11:
207 swizzled_offset = j ^
211 swizzle_str = "bit9^10^11";
214 igt_assert_f(0, "Bad swizzle bits; %d\n",
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,