gem_tiled_pread.c 5.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 * Copyright © 2009 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *
 */

/** @file gem_tiled_pread.c
 *
 * This is a test of pread's behavior on tiled objects with respect to the
 * reported swizzling value.
 *
 * The goal is to exercise the slow_bit17_copy path for reading on bit17
 * machines, but will also be useful for catching swizzling value bugs on
 * other systems.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/time.h>
46
#include <sys/ioctl.h>
47 48 49
#include "drm.h"
#include "i915_drm.h"
#include "drmtest.h"
50
#include "intel_gpu_tools.h"
51

52 53 54
#define WIDTH 512
#define HEIGHT 512
static uint32_t linear[WIDTH * HEIGHT];
55 56 57

#define PAGE_SIZE 4096

58 59 60 61
static int tile_width;
static int tile_height;
static int tile_size;

62 63 64 65 66
static void
gem_get_tiling(int fd, uint32_t handle, uint32_t *tiling, uint32_t *swizzle)
{
	struct drm_i915_gem_get_tiling get_tiling;
	int ret;
67

68 69 70 71
	memset(&get_tiling, 0, sizeof(get_tiling));
	get_tiling.handle = handle;

	ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling);
Daniel Vetter's avatar
Daniel Vetter committed
72
	igt_assert(ret == 0);
73 74 75 76 77 78 79 80 81 82 83 84 85

	*tiling = get_tiling.tiling_mode;
	*swizzle = get_tiling.swizzle_mode;
}

static uint32_t
create_bo(int fd)
{
	uint32_t handle;
	uint32_t *data;
	int i;

	handle = gem_create(fd, sizeof(linear));
86
	gem_set_tiling(fd, handle, I915_TILING_X, WIDTH * sizeof(uint32_t));
87 88 89 90 91 92 93 94

	/* Fill the BO with dwords starting at start_val */
	data = gem_mmap(fd, handle, sizeof(linear), PROT_READ | PROT_WRITE);
	for (i = 0; i < WIDTH*HEIGHT; i++)
		data[i] = i;
	munmap(data, sizeof(linear));

	return handle;
95 96 97 98 99 100 101 102 103 104 105 106 107 108
}

static int
swizzle_bit(int bit, int offset)
{
	return (offset & (1 << bit)) >> (bit - 6);
}

/* Translate from a swizzled offset in the tiled buffer to the corresponding
 * value from the original linear buffer.
 */
static uint32_t
calculate_expected(int offset)
{
109 110 111 112
	int tile_off = offset & (tile_size - 1);
	int tile_base = offset & -tile_size;
	int tile_index = tile_base / tile_size;
	int tiles_per_row = 4*WIDTH / tile_width;
113 114

	/* base x,y values from the tile (page) index. */
115 116
	int base_y = tile_index / tiles_per_row * tile_height;
	int base_x = tile_index % tiles_per_row * (tile_width/4);
117 118

	/* x, y offsets within the tile */
119 120
	int tile_y = tile_off / tile_width;
	int tile_x = (tile_off % tile_width) / 4;
121 122

	/* printf("%3d, %3d, %3d,%3d\n", base_x, base_y, tile_x, tile_y); */
123
	return (base_y + tile_y) * WIDTH + base_x + tile_x;
124 125 126 127 128 129 130 131
}

int
main(int argc, char **argv)
{
	int fd;
	int i, iter = 100;
	uint32_t tiling, swizzle;
132
	uint32_t handle;
133
	uint32_t devid;
134 135 136

	fd = drm_open_any();

137 138
	handle = create_bo(fd);
	gem_get_tiling(fd, handle, &tiling, &swizzle);
139

140 141 142 143 144 145 146 147 148 149 150 151
	devid = intel_get_drm_devid(fd);

	if (IS_GEN2(devid)) {
		tile_height = 16;
		tile_width = 128;
		tile_size = 2048;
	} else {
		tile_height = 8;
		tile_width = 512;
		tile_size = PAGE_SIZE;
	}

152 153 154 155
	/* Read a bunch of random subsets of the data and check that they come
	 * out right.
	 */
	for (i = 0; i < iter; i++) {
156
		int size = WIDTH * HEIGHT * 4;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
		int offset = (random() % size) & ~3;
		int len = (random() % size) & ~3;
		int j;

		if (len == 0)
			len = 4;

		if (offset + len > size)
			len = size - offset;

		if (i == 0) {
			offset = 0;
			len = size;
		}

Daniel Vetter's avatar
Daniel Vetter committed
172
		gem_read(fd, handle, offset, linear, len);
173 174 175 176 177 178 179 180 181

		/* Translate from offsets in the read buffer to the swizzled
		 * address that it corresponds to.  This is the opposite of
		 * what Mesa does (calculate offset to be read given the linear
		 * offset it's looking for).
		 */
		for (j = offset; j < offset + len; j += 4) {
			uint32_t expected_val, found_val;
			int swizzled_offset;
182
			const char *swizzle_str;
183 184 185 186

			switch (swizzle) {
			case I915_BIT_6_SWIZZLE_NONE:
				swizzled_offset = j;
187
				swizzle_str = "none";
188 189 190 191
				break;
			case I915_BIT_6_SWIZZLE_9:
				swizzled_offset = j ^
					swizzle_bit(9, j);
192
				swizzle_str = "bit9";
193 194 195 196 197
				break;
			case I915_BIT_6_SWIZZLE_9_10:
				swizzled_offset = j ^
					swizzle_bit(9, j) ^
					swizzle_bit(10, j);
198
				swizzle_str = "bit9^10";
199 200 201 202 203
				break;
			case I915_BIT_6_SWIZZLE_9_11:
				swizzled_offset = j ^
					swizzle_bit(9, j) ^
					swizzle_bit(11, j);
204
				swizzle_str = "bit9^11";
205 206 207 208 209 210
				break;
			case I915_BIT_6_SWIZZLE_9_10_11:
				swizzled_offset = j ^
					swizzle_bit(9, j) ^
					swizzle_bit(10, j) ^
					swizzle_bit(11, j);
211
				swizzle_str = "bit9^10^11";
212 213 214 215 216 217 218
				break;
			default:
				fprintf(stderr, "Bad swizzle bits; %d\n",
					swizzle);
				abort();
			}
			expected_val = calculate_expected(swizzled_offset);
219
			found_val = linear[(j - offset) / 4];
220 221
			if (expected_val != found_val) {
				fprintf(stderr,
222 223 224 225 226
					"Bad read [%d]: %d instead of %d at 0x%08x "
					"for read from 0x%08x to 0x%08x, swizzle=%s\n",
					i, found_val, expected_val, j,
					offset, offset + len,
					swizzle_str);
227 228 229 230 231 232 233 234 235
				abort();
			}
		}
	}

	close(fd);

	return 0;
}