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 IGT_TEST_DESCRIPTION("This is a test of write-combining mmap's behavior on"
48 " tiled objects with respect to the reported swizzling"
49 " value.");
50
51 #define WIDTH 512
52 #define HEIGHT 512
53 #define SIZE (WIDTH*HEIGHT*sizeof(uint32_t))
54
55 #define PAGE_SIZE 4096
56
57 static int tile_width;
58 static int tile_height;
59 static int tile_size;
60
61 static uint32_t
create_bo(int fd)62 create_bo(int fd)
63 {
64 uint32_t handle;
65 uint32_t *data;
66 int i;
67
68 handle = gem_create(fd, SIZE);
69 gem_set_tiling(fd, handle, I915_TILING_X, WIDTH * sizeof(uint32_t));
70
71 /* Write throught the fence to tiled the data.
72 * We then manually detile on reading back through the mmap(wc).
73 */
74 data = gem_mmap__gtt(fd, handle, SIZE, PROT_READ | PROT_WRITE);
75 gem_set_domain(fd, handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
76 for (i = 0; i < WIDTH*HEIGHT; i++)
77 data[i] = i;
78 munmap(data, SIZE);
79
80 gem_set_domain(fd, handle, I915_GEM_DOMAIN_CPU, 0);
81 return handle;
82 }
83
84 static int
swizzle_bit(int bit,int offset)85 swizzle_bit(int bit, int offset)
86 {
87 return (offset & (1 << bit)) >> (bit - 6);
88 }
89
90 /* Translate from a swizzled offset in the tiled buffer to the corresponding
91 * value from the original linear buffer.
92 */
93 static uint32_t
calculate_expected(int offset)94 calculate_expected(int offset)
95 {
96 int tile_off = offset & (tile_size - 1);
97 int tile_base = offset & -tile_size;
98 int tile_index = tile_base / tile_size;
99 int tiles_per_row = 4*WIDTH / tile_width;
100
101 /* base x,y values from the tile (page) index. */
102 int base_y = tile_index / tiles_per_row * tile_height;
103 int base_x = tile_index % tiles_per_row * (tile_width/4);
104
105 /* x, y offsets within the tile */
106 int tile_y = tile_off / tile_width;
107 int tile_x = (tile_off % tile_width) / 4;
108
109 igt_debug("%3d, %3d, %3d,%3d\n", base_x, base_y, tile_x, tile_y);
110 return (base_y + tile_y) * WIDTH + base_x + tile_x;
111 }
112
113 static void
get_tiling(int fd,uint32_t handle,uint32_t * tiling,uint32_t * swizzle)114 get_tiling(int fd, uint32_t handle, uint32_t *tiling, uint32_t *swizzle)
115 {
116 struct drm_i915_gem_get_tiling2 {
117 uint32_t handle;
118 uint32_t tiling_mode;
119 uint32_t swizzle_mode;
120 uint32_t phys_swizzle_mode;
121 } arg;
122 #define DRM_IOCTL_I915_GEM_GET_TILING2 DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_GET_TILING, struct drm_i915_gem_get_tiling2)
123
124 memset(&arg, 0, sizeof(arg));
125 arg.handle = handle;
126
127 do_ioctl(fd, DRM_IOCTL_I915_GEM_GET_TILING2, &arg);
128 igt_require(arg.phys_swizzle_mode == arg.swizzle_mode);
129
130 *tiling = arg.tiling_mode;
131 *swizzle = arg.swizzle_mode;
132 }
133
134 igt_simple_main
135 {
136 int fd;
137 int i, iter = 100;
138 uint32_t tiling, swizzle;
139 uint32_t handle;
140
141 fd = drm_open_driver(DRIVER_INTEL);
142
143 handle = create_bo(fd);
144 get_tiling(fd, handle, &tiling, &swizzle);
145
146 if (IS_GEN2(intel_get_drm_devid(fd))) {
147 tile_height = 16;
148 tile_width = 128;
149 tile_size = 2048;
150 } else {
151 tile_height = 8;
152 tile_width = 512;
153 tile_size = PAGE_SIZE;
154 }
155
156 /* Read a bunch of random subsets of the data and check that they come
157 * out right.
158 */
159 for (i = 0; i < iter; i++) {
160 int size = WIDTH * HEIGHT * 4;
161 int offset = (random() % size) & ~3;
162 int len = (random() % size) & ~3;
163 int first_page, last_page;
164 uint32_t *linear;
165 int j;
166
167 if (len == 0)
168 len = 4;
169
170 if (offset + len > size)
171 len = size - offset;
172
173 if (i == 0) {
174 offset = 0;
175 len = size;
176 }
177
178 first_page = offset & -PAGE_SIZE;
179 last_page = (offset + len + PAGE_SIZE - 1) & -PAGE_SIZE;
180 offset -= first_page;
181
182 linear = gem_mmap__cpu(fd, handle, first_page, last_page - first_page, PROT_READ);
183
184 /* Translate from offsets in the read buffer to the swizzled
185 * address that it corresponds to. This is the opposite of
186 * what Mesa does (calculate offset to be read given the linear
187 * offset it's looking for).
188 */
189 for (j = offset; j < offset + len; j += 4) {
190 uint32_t expected_val, found_val;
191 int swizzled_offset = j + first_page;
192 const char *swizzle_str;
193
194 switch (swizzle) {
195 case I915_BIT_6_SWIZZLE_NONE:
196 swizzle_str = "none";
197 break;
198 case I915_BIT_6_SWIZZLE_9:
199 swizzled_offset ^=
200 swizzle_bit(9, swizzled_offset);
201 swizzle_str = "bit9";
202 break;
203 case I915_BIT_6_SWIZZLE_9_10:
204 swizzled_offset ^=
205 swizzle_bit(9, swizzled_offset) ^
206 swizzle_bit(10, swizzled_offset);
207 swizzle_str = "bit9^10";
208 break;
209 case I915_BIT_6_SWIZZLE_9_11:
210 swizzled_offset ^=
211 swizzle_bit(9, swizzled_offset) ^
212 swizzle_bit(11, swizzled_offset);
213 swizzle_str = "bit9^11";
214 break;
215 case I915_BIT_6_SWIZZLE_9_10_11:
216 swizzled_offset ^=
217 swizzle_bit(9, swizzled_offset) ^
218 swizzle_bit(10, swizzled_offset) ^
219 swizzle_bit(11, swizzled_offset);
220 swizzle_str = "bit9^10^11";
221 break;
222 default:
223 igt_skip("unknown swizzling");
224 break;
225 }
226 expected_val = calculate_expected(swizzled_offset);
227 found_val = linear[j / 4];
228 igt_assert_f(expected_val == found_val,
229 "Bad read [%d]: %d instead of %d at 0x%08x "
230 "for read from 0x%08x to 0x%08x, swizzle=%s\n",
231 i, found_val, expected_val, j + first_page,
232 offset, offset + len,
233 swizzle_str);
234 }
235 munmap(linear, last_page - first_page);
236 }
237
238 close(fd);
239 }
240