blob: 67d54bd30f5516d54f8e6443e58539078ee00db6 [file] [log] [blame]
Chris Wilsonc45216b2014-11-04 07:30:57 +00001/*
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
Thomas Wood804e11f2015-08-17 17:57:43 +010035#include "igt.h"
Chris Wilsonc45216b2014-11-04 07:30:57 +000036#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"
Chris Wilsonc45216b2014-11-04 07:30:57 +000046
Thomas Woodb2ac2642014-11-28 11:02:44 +000047IGT_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
Chris Wilsonc45216b2014-11-04 07:30:57 +000051#define WIDTH 512
52#define HEIGHT 512
53#define SIZE (WIDTH*HEIGHT*sizeof(uint32_t))
54
55#define PAGE_SIZE 4096
56
57static int tile_width;
58static int tile_height;
59static int tile_size;
60
61static uint32_t
62create_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 */
Ville Syrjäläf52e7ec2015-10-09 19:11:39 +030074 data = gem_mmap__gtt(fd, handle, SIZE, PROT_READ | PROT_WRITE);
Chris Wilsonc45216b2014-11-04 07:30:57 +000075 for (i = 0; i < WIDTH*HEIGHT; i++)
76 data[i] = i;
77 munmap(data, SIZE);
78
79 gem_set_domain(fd, handle, I915_GEM_DOMAIN_CPU, 0);
80 return handle;
81}
82
83static int
84swizzle_bit(int bit, int offset)
85{
86 return (offset & (1 << bit)) >> (bit - 6);
87}
88
89/* Translate from a swizzled offset in the tiled buffer to the corresponding
90 * value from the original linear buffer.
91 */
92static uint32_t
93calculate_expected(int offset)
94{
95 int tile_off = offset & (tile_size - 1);
96 int tile_base = offset & -tile_size;
97 int tile_index = tile_base / tile_size;
98 int tiles_per_row = 4*WIDTH / tile_width;
99
100 /* base x,y values from the tile (page) index. */
101 int base_y = tile_index / tiles_per_row * tile_height;
102 int base_x = tile_index % tiles_per_row * (tile_width/4);
103
104 /* x, y offsets within the tile */
105 int tile_y = tile_off / tile_width;
106 int tile_x = (tile_off % tile_width) / 4;
107
108 igt_debug("%3d, %3d, %3d,%3d\n", base_x, base_y, tile_x, tile_y);
109 return (base_y + tile_y) * WIDTH + base_x + tile_x;
110}
111
112static void
113get_tiling(int fd, uint32_t handle, uint32_t *tiling, uint32_t *swizzle)
114{
115 struct drm_i915_gem_get_tiling2 {
116 uint32_t handle;
117 uint32_t tiling_mode;
118 uint32_t swizzle_mode;
119 uint32_t phys_swizzle_mode;
120 } arg;
121#define DRM_IOCTL_I915_GEM_GET_TILING2 DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_GET_TILING, struct drm_i915_gem_get_tiling2)
122
123 memset(&arg, 0, sizeof(arg));
124 arg.handle = handle;
125
Daniel Stonede7ccdd2015-10-01 14:16:48 +0100126 do_ioctl(fd, DRM_IOCTL_I915_GEM_GET_TILING2, &arg);
Chris Wilsonc45216b2014-11-04 07:30:57 +0000127 igt_require(arg.phys_swizzle_mode == arg.swizzle_mode);
128
129 *tiling = arg.tiling_mode;
130 *swizzle = arg.swizzle_mode;
131}
132
133igt_simple_main
134{
135 int fd;
136 int i, iter = 100;
137 uint32_t tiling, swizzle;
138 uint32_t handle;
139
Micah Fedkec81d2932015-07-22 21:54:02 +0000140 fd = drm_open_driver(DRIVER_INTEL);
Chris Wilsonc45216b2014-11-04 07:30:57 +0000141
142 handle = create_bo(fd);
143 get_tiling(fd, handle, &tiling, &swizzle);
144
145 if (IS_GEN2(intel_get_drm_devid(fd))) {
146 tile_height = 16;
147 tile_width = 128;
148 tile_size = 2048;
149 } else {
150 tile_height = 8;
151 tile_width = 512;
152 tile_size = PAGE_SIZE;
153 }
154
155 /* Read a bunch of random subsets of the data and check that they come
156 * out right.
157 */
158 for (i = 0; i < iter; i++) {
159 int size = WIDTH * HEIGHT * 4;
160 int offset = (random() % size) & ~3;
161 int len = (random() % size) & ~3;
162 int first_page, last_page;
163 uint32_t *linear;
164 int j;
165
166 if (len == 0)
167 len = 4;
168
169 if (offset + len > size)
170 len = size - offset;
171
172 if (i == 0) {
173 offset = 0;
174 len = size;
175 }
176
177 first_page = offset & ~(PAGE_SIZE-1);
178 last_page = (offset + len + PAGE_SIZE) & ~(PAGE_SIZE-1);
179 offset -= first_page;
180
Ville Syrjäläf52e7ec2015-10-09 19:11:39 +0300181 linear = gem_mmap__cpu(fd, handle, first_page, last_page - first_page, PROT_READ);
Chris Wilsonc45216b2014-11-04 07:30:57 +0000182
183 /* Translate from offsets in the read buffer to the swizzled
184 * address that it corresponds to. This is the opposite of
185 * what Mesa does (calculate offset to be read given the linear
186 * offset it's looking for).
187 */
188 for (j = offset; j < offset + len; j += 4) {
189 uint32_t expected_val, found_val;
190 int swizzled_offset = j + first_page;
191 const char *swizzle_str;
192
193 switch (swizzle) {
194 case I915_BIT_6_SWIZZLE_NONE:
195 swizzle_str = "none";
196 break;
197 case I915_BIT_6_SWIZZLE_9:
198 swizzled_offset ^=
199 swizzle_bit(9, swizzled_offset);
200 swizzle_str = "bit9";
201 break;
202 case I915_BIT_6_SWIZZLE_9_10:
203 swizzled_offset ^=
204 swizzle_bit(9, swizzled_offset) ^
205 swizzle_bit(10, swizzled_offset);
206 swizzle_str = "bit9^10";
207 break;
208 case I915_BIT_6_SWIZZLE_9_11:
209 swizzled_offset ^=
210 swizzle_bit(9, swizzled_offset) ^
211 swizzle_bit(11, swizzled_offset);
212 swizzle_str = "bit9^11";
213 break;
214 case I915_BIT_6_SWIZZLE_9_10_11:
215 swizzled_offset ^=
216 swizzle_bit(9, swizzled_offset) ^
217 swizzle_bit(10, swizzled_offset) ^
218 swizzle_bit(11, swizzled_offset);
219 swizzle_str = "bit9^10^11";
220 break;
221 default:
222 igt_skip("unknown swizzling");
223 break;
224 }
225 expected_val = calculate_expected(swizzled_offset);
226 found_val = linear[j / 4];
227 igt_assert_f(expected_val == found_val,
228 "Bad read [%d]: %d instead of %d at 0x%08x "
229 "for read from 0x%08x to 0x%08x, swizzle=%s\n",
230 i, found_val, expected_val, j + first_page,
231 offset, offset + len,
232 swizzle_str);
233 }
234 munmap(linear, last_page - first_page);
235 }
236
237 close(fd);
238}