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
24 /** @file gem_tiled_wc.c
25 *
26 * This is a test of write-combining mmap's behavior on tiled objects
27 * with respect to the reported swizzling value.
28 *
29 * The goal is to exercise the complications that arise when using a linear
30 * view of a tiled object that is subject to hardware swizzling. This is
31 * useful to check that we are presenting the correct view of the object
32 * to userspace, and that userspace has to respect the swizzle.
33 */
34
35 #include "igt.h"
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <inttypes.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/ioctl.h>
45 #include "drm.h"
46
47 #define WIDTH 512
48 #define HEIGHT 512
49 #define SIZE (WIDTH*HEIGHT*sizeof(uint32_t))
50
51 #define PAGE_SIZE 4096
52
53 static int tile_width;
54 static int tile_height;
55 static int tile_size;
56
57 static uint32_t
create_bo(int fd)58 create_bo(int fd)
59 {
60 uint32_t handle;
61 uint32_t *data;
62 int i;
63
64 handle = gem_create(fd, SIZE);
65 gem_set_tiling(fd, handle, I915_TILING_X, WIDTH * sizeof(uint32_t));
66
67 /* Fill the BO with dwords starting at start_val */
68 data = gem_mmap__gtt(fd, handle, SIZE, PROT_READ | PROT_WRITE);
69 gem_set_domain(fd, handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
70 for (i = 0; i < WIDTH*HEIGHT; i++)
71 data[i] = i;
72 munmap(data, SIZE);
73
74 return handle;
75 }
76
77 static int
swizzle_bit(int bit,int offset)78 swizzle_bit(int bit, int offset)
79 {
80 return (offset & (1 << bit)) >> (bit - 6);
81 }
82
83 /* Translate from a swizzled offset in the tiled buffer to the corresponding
84 * value from the original linear buffer.
85 */
86 static uint32_t
calculate_expected(int offset)87 calculate_expected(int offset)
88 {
89 int tile_off = offset & (tile_size - 1);
90 int tile_base = offset & -tile_size;
91 int tile_index = tile_base / tile_size;
92 int tiles_per_row = 4*WIDTH / tile_width;
93
94 /* base x,y values from the tile (page) index. */
95 int base_y = tile_index / tiles_per_row * tile_height;
96 int base_x = tile_index % tiles_per_row * (tile_width/4);
97
98 /* x, y offsets within the tile */
99 int tile_y = tile_off / tile_width;
100 int tile_x = (tile_off % tile_width) / 4;
101
102 igt_debug("%s(%d): %3d, %3d, %3d,%3d = %d\n",
103 __func__, offset, base_x, base_y, tile_x, tile_y,
104 (base_y + tile_y) * WIDTH + base_x + tile_x);
105 return (base_y + tile_y) * WIDTH + base_x + tile_x;
106 }
107
108 igt_simple_main
109 {
110 int fd;
111 int i, iter = 100;
112 uint32_t tiling, swizzle;
113 uint32_t handle;
114
115 fd = drm_open_driver(DRIVER_INTEL);
116 gem_require_mmap_wc(fd);
117
118 handle = create_bo(fd);
119 igt_require(gem_get_tiling(fd, handle, &tiling, &swizzle));
120
121 if (IS_GEN2(intel_get_drm_devid(fd))) {
122 tile_height = 16;
123 tile_width = 128;
124 tile_size = 2048;
125 } else {
126 tile_height = 8;
127 tile_width = 512;
128 tile_size = PAGE_SIZE;
129 }
130
131 /* Read a bunch of random subsets of the data and check that they come
132 * out right.
133 */
134 for (i = 0; i < iter; i++) {
135 int size = WIDTH * HEIGHT * 4;
136 int offset = (random() % size) & ~3;
137 int len = (random() % size) & ~3;
138 int first_page, last_page;
139 uint32_t *linear;
140 int j;
141
142 if (len == 0)
143 len = 4;
144
145 if (offset + len > size)
146 len = size - offset;
147
148 if (i == 0) {
149 offset = 0;
150 len = size;
151 }
152
153 first_page = offset & -PAGE_SIZE;
154 last_page = (offset + len + PAGE_SIZE - 1) & -PAGE_SIZE;
155
156 linear = gem_mmap__wc(fd, handle, first_page, last_page - first_page, PROT_READ);
157
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).
162 */
163 for (j = offset; j < offset + len; j += 4) {
164 uint32_t expected_val, found_val;
165 int swizzled_offset;
166 const char *swizzle_str;
167
168 switch (swizzle) {
169 case I915_BIT_6_SWIZZLE_NONE:
170 swizzled_offset = j;
171 swizzle_str = "none";
172 break;
173 case I915_BIT_6_SWIZZLE_9:
174 swizzled_offset = j ^
175 swizzle_bit(9, j);
176 swizzle_str = "bit9";
177 break;
178 case I915_BIT_6_SWIZZLE_9_10:
179 swizzled_offset = j ^
180 swizzle_bit(9, j) ^
181 swizzle_bit(10, j);
182 swizzle_str = "bit9^10";
183 break;
184 case I915_BIT_6_SWIZZLE_9_11:
185 swizzled_offset = j ^
186 swizzle_bit(9, j) ^
187 swizzle_bit(11, j);
188 swizzle_str = "bit9^11";
189 break;
190 case I915_BIT_6_SWIZZLE_9_10_11:
191 swizzled_offset = j ^
192 swizzle_bit(9, j) ^
193 swizzle_bit(10, j) ^
194 swizzle_bit(11, j);
195 swizzle_str = "bit9^10^11";
196 break;
197 default:
198 igt_skip("unknown swizzling");
199 break;
200 }
201 igt_debug("Checking offset %d swizzled %s -> %d\n",
202 j, swizzle_str, swizzled_offset);
203 expected_val = calculate_expected(swizzled_offset);
204 found_val = linear[(j - first_page)/ 4];
205 igt_assert_f(expected_val == found_val,
206 "Bad read [%d]: %d instead of %d at 0x%08x "
207 "for read from 0x%08x to 0x%08x, swizzle=%s\n",
208 i, found_val, expected_val, j,
209 offset, offset + len,
210 swizzle_str);
211 }
212 munmap(linear, last_page - first_page);
213 }
214
215 close(fd);
216 }
217