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;
70 handle = gem_create(fd, sizeof(linear));
71 gem_set_tiling(fd, handle, I915_TILING_X, WIDTH * sizeof(uint32_t));
73 /* Fill the BO with dwords starting at start_val */
74 data = gem_mmap(fd, handle, sizeof(linear), PROT_READ | PROT_WRITE);
75 for (i = 0; i < WIDTH*HEIGHT; i++)
77 munmap(data, sizeof(linear));
83 swizzle_bit(int bit, int offset)
85 return (offset & (1 << bit)) >> (bit - 6);
88 /* Translate from a swizzled offset in the tiled buffer to the corresponding
89 * value from the original linear buffer.
92 calculate_expected(int offset)
94 int tile_off = offset & (tile_size - 1);
95 int tile_base = offset & -tile_size;
96 int tile_index = tile_base / tile_size;
97 int tiles_per_row = 4*WIDTH / tile_width;
99 /* base x,y values from the tile (page) index. */
100 int base_y = tile_index / tiles_per_row * tile_height;
101 int base_x = tile_index % tiles_per_row * (tile_width/4);
103 /* x, y offsets within the tile */
104 int tile_y = tile_off / tile_width;
105 int tile_x = (tile_off % tile_width) / 4;
107 igt_debug("%3d, %3d, %3d,%3d\n", base_x, base_y, tile_x, tile_y);
108 return (base_y + tile_y) * WIDTH + base_x + tile_x;
115 uint32_t tiling, swizzle;
121 handle = create_bo(fd);
122 gem_get_tiling(fd, handle, &tiling, &swizzle);
124 devid = intel_get_drm_devid(fd);
126 if (IS_GEN2(devid)) {
133 tile_size = PAGE_SIZE;
136 /* Read a bunch of random subsets of the data and check that they come
139 for (i = 0; i < iter; i++) {
140 int size = WIDTH * HEIGHT * 4;
141 int offset = (random() % size) & ~3;
142 int len = (random() % size) & ~3;
148 if (offset + len > size)
156 gem_read(fd, handle, offset, linear, len);
158 /* Translate from offsets in the read buffer to the swizzled
159 * address that it corresponds to. This is the opposite of
160 * what Mesa does (calculate offset to be read given the linear
161 * offset it's looking for).
163 for (j = offset; j < offset + len; j += 4) {
164 uint32_t expected_val, found_val;
166 const char *swizzle_str;
169 case I915_BIT_6_SWIZZLE_NONE:
171 swizzle_str = "none";
173 case I915_BIT_6_SWIZZLE_9:
174 swizzled_offset = j ^
176 swizzle_str = "bit9";
178 case I915_BIT_6_SWIZZLE_9_10:
179 swizzled_offset = j ^
182 swizzle_str = "bit9^10";
184 case I915_BIT_6_SWIZZLE_9_11:
185 swizzled_offset = j ^
188 swizzle_str = "bit9^11";
190 case I915_BIT_6_SWIZZLE_9_10_11:
191 swizzled_offset = j ^
195 swizzle_str = "bit9^10^11";
198 igt_assert_f(0, "Bad swizzle bits; %d\n",
201 expected_val = calculate_expected(swizzled_offset);
202 found_val = linear[(j - offset) / 4];
203 igt_assert_f(expected_val == found_val,
204 "Bad read [%d]: %d instead of %d at 0x%08x "
205 "for read from 0x%08x to 0x%08x, swizzle=%s\n",
206 i, found_val, expected_val, j,
207 offset, offset + len,