1*d83cc019SAndroid Build Coastguard Worker /*
2*d83cc019SAndroid Build Coastguard Worker * Copyright © 2009 Intel Corporation
3*d83cc019SAndroid Build Coastguard Worker *
4*d83cc019SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a
5*d83cc019SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"),
6*d83cc019SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation
7*d83cc019SAndroid Build Coastguard Worker * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*d83cc019SAndroid Build Coastguard Worker * and/or sell copies of the Software, and to permit persons to whom the
9*d83cc019SAndroid Build Coastguard Worker * Software is furnished to do so, subject to the following conditions:
10*d83cc019SAndroid Build Coastguard Worker *
11*d83cc019SAndroid Build Coastguard Worker * The above copyright notice and this permission notice (including the next
12*d83cc019SAndroid Build Coastguard Worker * paragraph) shall be included in all copies or substantial portions of the
13*d83cc019SAndroid Build Coastguard Worker * Software.
14*d83cc019SAndroid Build Coastguard Worker *
15*d83cc019SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*d83cc019SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*d83cc019SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*d83cc019SAndroid Build Coastguard Worker * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*d83cc019SAndroid Build Coastguard Worker * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*d83cc019SAndroid Build Coastguard Worker * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*d83cc019SAndroid Build Coastguard Worker * IN THE SOFTWARE.
22*d83cc019SAndroid Build Coastguard Worker */
23*d83cc019SAndroid Build Coastguard Worker
24*d83cc019SAndroid Build Coastguard Worker /** @file gem_tiled_wc.c
25*d83cc019SAndroid Build Coastguard Worker *
26*d83cc019SAndroid Build Coastguard Worker * This is a test of write-combining mmap's behavior on tiled objects
27*d83cc019SAndroid Build Coastguard Worker * with respect to the reported swizzling value.
28*d83cc019SAndroid Build Coastguard Worker *
29*d83cc019SAndroid Build Coastguard Worker * The goal is to exercise the complications that arise when using a linear
30*d83cc019SAndroid Build Coastguard Worker * view of a tiled object that is subject to hardware swizzling. This is
31*d83cc019SAndroid Build Coastguard Worker * useful to check that we are presenting the correct view of the object
32*d83cc019SAndroid Build Coastguard Worker * to userspace, and that userspace has to respect the swizzle.
33*d83cc019SAndroid Build Coastguard Worker */
34*d83cc019SAndroid Build Coastguard Worker
35*d83cc019SAndroid Build Coastguard Worker #include "igt.h"
36*d83cc019SAndroid Build Coastguard Worker #include <stdlib.h>
37*d83cc019SAndroid Build Coastguard Worker #include <stdio.h>
38*d83cc019SAndroid Build Coastguard Worker #include <string.h>
39*d83cc019SAndroid Build Coastguard Worker #include <fcntl.h>
40*d83cc019SAndroid Build Coastguard Worker #include <inttypes.h>
41*d83cc019SAndroid Build Coastguard Worker #include <errno.h>
42*d83cc019SAndroid Build Coastguard Worker #include <sys/stat.h>
43*d83cc019SAndroid Build Coastguard Worker #include <sys/time.h>
44*d83cc019SAndroid Build Coastguard Worker #include <sys/ioctl.h>
45*d83cc019SAndroid Build Coastguard Worker #include "drm.h"
46*d83cc019SAndroid Build Coastguard Worker
47*d83cc019SAndroid Build Coastguard Worker IGT_TEST_DESCRIPTION("This is a test of write-combining mmap's behavior on"
48*d83cc019SAndroid Build Coastguard Worker " tiled objects with respect to the reported swizzling"
49*d83cc019SAndroid Build Coastguard Worker " value.");
50*d83cc019SAndroid Build Coastguard Worker
51*d83cc019SAndroid Build Coastguard Worker #define WIDTH 512
52*d83cc019SAndroid Build Coastguard Worker #define HEIGHT 512
53*d83cc019SAndroid Build Coastguard Worker #define SIZE (WIDTH*HEIGHT*sizeof(uint32_t))
54*d83cc019SAndroid Build Coastguard Worker
55*d83cc019SAndroid Build Coastguard Worker #define PAGE_SIZE 4096
56*d83cc019SAndroid Build Coastguard Worker
57*d83cc019SAndroid Build Coastguard Worker static int tile_width;
58*d83cc019SAndroid Build Coastguard Worker static int tile_height;
59*d83cc019SAndroid Build Coastguard Worker static int tile_size;
60*d83cc019SAndroid Build Coastguard Worker
61*d83cc019SAndroid Build Coastguard Worker static uint32_t
create_bo(int fd)62*d83cc019SAndroid Build Coastguard Worker create_bo(int fd)
63*d83cc019SAndroid Build Coastguard Worker {
64*d83cc019SAndroid Build Coastguard Worker uint32_t handle;
65*d83cc019SAndroid Build Coastguard Worker uint32_t *data;
66*d83cc019SAndroid Build Coastguard Worker int i;
67*d83cc019SAndroid Build Coastguard Worker
68*d83cc019SAndroid Build Coastguard Worker handle = gem_create(fd, SIZE);
69*d83cc019SAndroid Build Coastguard Worker gem_set_tiling(fd, handle, I915_TILING_X, WIDTH * sizeof(uint32_t));
70*d83cc019SAndroid Build Coastguard Worker
71*d83cc019SAndroid Build Coastguard Worker /* Write throught the fence to tiled the data.
72*d83cc019SAndroid Build Coastguard Worker * We then manually detile on reading back through the mmap(wc).
73*d83cc019SAndroid Build Coastguard Worker */
74*d83cc019SAndroid Build Coastguard Worker data = gem_mmap__gtt(fd, handle, SIZE, PROT_READ | PROT_WRITE);
75*d83cc019SAndroid Build Coastguard Worker gem_set_domain(fd, handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
76*d83cc019SAndroid Build Coastguard Worker for (i = 0; i < WIDTH*HEIGHT; i++)
77*d83cc019SAndroid Build Coastguard Worker data[i] = i;
78*d83cc019SAndroid Build Coastguard Worker munmap(data, SIZE);
79*d83cc019SAndroid Build Coastguard Worker
80*d83cc019SAndroid Build Coastguard Worker gem_set_domain(fd, handle, I915_GEM_DOMAIN_CPU, 0);
81*d83cc019SAndroid Build Coastguard Worker return handle;
82*d83cc019SAndroid Build Coastguard Worker }
83*d83cc019SAndroid Build Coastguard Worker
84*d83cc019SAndroid Build Coastguard Worker static int
swizzle_bit(int bit,int offset)85*d83cc019SAndroid Build Coastguard Worker swizzle_bit(int bit, int offset)
86*d83cc019SAndroid Build Coastguard Worker {
87*d83cc019SAndroid Build Coastguard Worker return (offset & (1 << bit)) >> (bit - 6);
88*d83cc019SAndroid Build Coastguard Worker }
89*d83cc019SAndroid Build Coastguard Worker
90*d83cc019SAndroid Build Coastguard Worker /* Translate from a swizzled offset in the tiled buffer to the corresponding
91*d83cc019SAndroid Build Coastguard Worker * value from the original linear buffer.
92*d83cc019SAndroid Build Coastguard Worker */
93*d83cc019SAndroid Build Coastguard Worker static uint32_t
calculate_expected(int offset)94*d83cc019SAndroid Build Coastguard Worker calculate_expected(int offset)
95*d83cc019SAndroid Build Coastguard Worker {
96*d83cc019SAndroid Build Coastguard Worker int tile_off = offset & (tile_size - 1);
97*d83cc019SAndroid Build Coastguard Worker int tile_base = offset & -tile_size;
98*d83cc019SAndroid Build Coastguard Worker int tile_index = tile_base / tile_size;
99*d83cc019SAndroid Build Coastguard Worker int tiles_per_row = 4*WIDTH / tile_width;
100*d83cc019SAndroid Build Coastguard Worker
101*d83cc019SAndroid Build Coastguard Worker /* base x,y values from the tile (page) index. */
102*d83cc019SAndroid Build Coastguard Worker int base_y = tile_index / tiles_per_row * tile_height;
103*d83cc019SAndroid Build Coastguard Worker int base_x = tile_index % tiles_per_row * (tile_width/4);
104*d83cc019SAndroid Build Coastguard Worker
105*d83cc019SAndroid Build Coastguard Worker /* x, y offsets within the tile */
106*d83cc019SAndroid Build Coastguard Worker int tile_y = tile_off / tile_width;
107*d83cc019SAndroid Build Coastguard Worker int tile_x = (tile_off % tile_width) / 4;
108*d83cc019SAndroid Build Coastguard Worker
109*d83cc019SAndroid Build Coastguard Worker igt_debug("%3d, %3d, %3d,%3d\n", base_x, base_y, tile_x, tile_y);
110*d83cc019SAndroid Build Coastguard Worker return (base_y + tile_y) * WIDTH + base_x + tile_x;
111*d83cc019SAndroid Build Coastguard Worker }
112*d83cc019SAndroid Build Coastguard Worker
113*d83cc019SAndroid Build Coastguard Worker static void
get_tiling(int fd,uint32_t handle,uint32_t * tiling,uint32_t * swizzle)114*d83cc019SAndroid Build Coastguard Worker get_tiling(int fd, uint32_t handle, uint32_t *tiling, uint32_t *swizzle)
115*d83cc019SAndroid Build Coastguard Worker {
116*d83cc019SAndroid Build Coastguard Worker struct drm_i915_gem_get_tiling2 {
117*d83cc019SAndroid Build Coastguard Worker uint32_t handle;
118*d83cc019SAndroid Build Coastguard Worker uint32_t tiling_mode;
119*d83cc019SAndroid Build Coastguard Worker uint32_t swizzle_mode;
120*d83cc019SAndroid Build Coastguard Worker uint32_t phys_swizzle_mode;
121*d83cc019SAndroid Build Coastguard Worker } arg;
122*d83cc019SAndroid Build Coastguard Worker #define DRM_IOCTL_I915_GEM_GET_TILING2 DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GEM_GET_TILING, struct drm_i915_gem_get_tiling2)
123*d83cc019SAndroid Build Coastguard Worker
124*d83cc019SAndroid Build Coastguard Worker memset(&arg, 0, sizeof(arg));
125*d83cc019SAndroid Build Coastguard Worker arg.handle = handle;
126*d83cc019SAndroid Build Coastguard Worker
127*d83cc019SAndroid Build Coastguard Worker do_ioctl(fd, DRM_IOCTL_I915_GEM_GET_TILING2, &arg);
128*d83cc019SAndroid Build Coastguard Worker igt_require(arg.phys_swizzle_mode == arg.swizzle_mode);
129*d83cc019SAndroid Build Coastguard Worker
130*d83cc019SAndroid Build Coastguard Worker *tiling = arg.tiling_mode;
131*d83cc019SAndroid Build Coastguard Worker *swizzle = arg.swizzle_mode;
132*d83cc019SAndroid Build Coastguard Worker }
133*d83cc019SAndroid Build Coastguard Worker
134*d83cc019SAndroid Build Coastguard Worker igt_simple_main
135*d83cc019SAndroid Build Coastguard Worker {
136*d83cc019SAndroid Build Coastguard Worker int fd;
137*d83cc019SAndroid Build Coastguard Worker int i, iter = 100;
138*d83cc019SAndroid Build Coastguard Worker uint32_t tiling, swizzle;
139*d83cc019SAndroid Build Coastguard Worker uint32_t handle;
140*d83cc019SAndroid Build Coastguard Worker
141*d83cc019SAndroid Build Coastguard Worker fd = drm_open_driver(DRIVER_INTEL);
142*d83cc019SAndroid Build Coastguard Worker
143*d83cc019SAndroid Build Coastguard Worker handle = create_bo(fd);
144*d83cc019SAndroid Build Coastguard Worker get_tiling(fd, handle, &tiling, &swizzle);
145*d83cc019SAndroid Build Coastguard Worker
146*d83cc019SAndroid Build Coastguard Worker if (IS_GEN2(intel_get_drm_devid(fd))) {
147*d83cc019SAndroid Build Coastguard Worker tile_height = 16;
148*d83cc019SAndroid Build Coastguard Worker tile_width = 128;
149*d83cc019SAndroid Build Coastguard Worker tile_size = 2048;
150*d83cc019SAndroid Build Coastguard Worker } else {
151*d83cc019SAndroid Build Coastguard Worker tile_height = 8;
152*d83cc019SAndroid Build Coastguard Worker tile_width = 512;
153*d83cc019SAndroid Build Coastguard Worker tile_size = PAGE_SIZE;
154*d83cc019SAndroid Build Coastguard Worker }
155*d83cc019SAndroid Build Coastguard Worker
156*d83cc019SAndroid Build Coastguard Worker /* Read a bunch of random subsets of the data and check that they come
157*d83cc019SAndroid Build Coastguard Worker * out right.
158*d83cc019SAndroid Build Coastguard Worker */
159*d83cc019SAndroid Build Coastguard Worker for (i = 0; i < iter; i++) {
160*d83cc019SAndroid Build Coastguard Worker int size = WIDTH * HEIGHT * 4;
161*d83cc019SAndroid Build Coastguard Worker int offset = (random() % size) & ~3;
162*d83cc019SAndroid Build Coastguard Worker int len = (random() % size) & ~3;
163*d83cc019SAndroid Build Coastguard Worker int first_page, last_page;
164*d83cc019SAndroid Build Coastguard Worker uint32_t *linear;
165*d83cc019SAndroid Build Coastguard Worker int j;
166*d83cc019SAndroid Build Coastguard Worker
167*d83cc019SAndroid Build Coastguard Worker if (len == 0)
168*d83cc019SAndroid Build Coastguard Worker len = 4;
169*d83cc019SAndroid Build Coastguard Worker
170*d83cc019SAndroid Build Coastguard Worker if (offset + len > size)
171*d83cc019SAndroid Build Coastguard Worker len = size - offset;
172*d83cc019SAndroid Build Coastguard Worker
173*d83cc019SAndroid Build Coastguard Worker if (i == 0) {
174*d83cc019SAndroid Build Coastguard Worker offset = 0;
175*d83cc019SAndroid Build Coastguard Worker len = size;
176*d83cc019SAndroid Build Coastguard Worker }
177*d83cc019SAndroid Build Coastguard Worker
178*d83cc019SAndroid Build Coastguard Worker first_page = offset & -PAGE_SIZE;
179*d83cc019SAndroid Build Coastguard Worker last_page = (offset + len + PAGE_SIZE - 1) & -PAGE_SIZE;
180*d83cc019SAndroid Build Coastguard Worker offset -= first_page;
181*d83cc019SAndroid Build Coastguard Worker
182*d83cc019SAndroid Build Coastguard Worker linear = gem_mmap__cpu(fd, handle, first_page, last_page - first_page, PROT_READ);
183*d83cc019SAndroid Build Coastguard Worker
184*d83cc019SAndroid Build Coastguard Worker /* Translate from offsets in the read buffer to the swizzled
185*d83cc019SAndroid Build Coastguard Worker * address that it corresponds to. This is the opposite of
186*d83cc019SAndroid Build Coastguard Worker * what Mesa does (calculate offset to be read given the linear
187*d83cc019SAndroid Build Coastguard Worker * offset it's looking for).
188*d83cc019SAndroid Build Coastguard Worker */
189*d83cc019SAndroid Build Coastguard Worker for (j = offset; j < offset + len; j += 4) {
190*d83cc019SAndroid Build Coastguard Worker uint32_t expected_val, found_val;
191*d83cc019SAndroid Build Coastguard Worker int swizzled_offset = j + first_page;
192*d83cc019SAndroid Build Coastguard Worker const char *swizzle_str;
193*d83cc019SAndroid Build Coastguard Worker
194*d83cc019SAndroid Build Coastguard Worker switch (swizzle) {
195*d83cc019SAndroid Build Coastguard Worker case I915_BIT_6_SWIZZLE_NONE:
196*d83cc019SAndroid Build Coastguard Worker swizzle_str = "none";
197*d83cc019SAndroid Build Coastguard Worker break;
198*d83cc019SAndroid Build Coastguard Worker case I915_BIT_6_SWIZZLE_9:
199*d83cc019SAndroid Build Coastguard Worker swizzled_offset ^=
200*d83cc019SAndroid Build Coastguard Worker swizzle_bit(9, swizzled_offset);
201*d83cc019SAndroid Build Coastguard Worker swizzle_str = "bit9";
202*d83cc019SAndroid Build Coastguard Worker break;
203*d83cc019SAndroid Build Coastguard Worker case I915_BIT_6_SWIZZLE_9_10:
204*d83cc019SAndroid Build Coastguard Worker swizzled_offset ^=
205*d83cc019SAndroid Build Coastguard Worker swizzle_bit(9, swizzled_offset) ^
206*d83cc019SAndroid Build Coastguard Worker swizzle_bit(10, swizzled_offset);
207*d83cc019SAndroid Build Coastguard Worker swizzle_str = "bit9^10";
208*d83cc019SAndroid Build Coastguard Worker break;
209*d83cc019SAndroid Build Coastguard Worker case I915_BIT_6_SWIZZLE_9_11:
210*d83cc019SAndroid Build Coastguard Worker swizzled_offset ^=
211*d83cc019SAndroid Build Coastguard Worker swizzle_bit(9, swizzled_offset) ^
212*d83cc019SAndroid Build Coastguard Worker swizzle_bit(11, swizzled_offset);
213*d83cc019SAndroid Build Coastguard Worker swizzle_str = "bit9^11";
214*d83cc019SAndroid Build Coastguard Worker break;
215*d83cc019SAndroid Build Coastguard Worker case I915_BIT_6_SWIZZLE_9_10_11:
216*d83cc019SAndroid Build Coastguard Worker swizzled_offset ^=
217*d83cc019SAndroid Build Coastguard Worker swizzle_bit(9, swizzled_offset) ^
218*d83cc019SAndroid Build Coastguard Worker swizzle_bit(10, swizzled_offset) ^
219*d83cc019SAndroid Build Coastguard Worker swizzle_bit(11, swizzled_offset);
220*d83cc019SAndroid Build Coastguard Worker swizzle_str = "bit9^10^11";
221*d83cc019SAndroid Build Coastguard Worker break;
222*d83cc019SAndroid Build Coastguard Worker default:
223*d83cc019SAndroid Build Coastguard Worker igt_skip("unknown swizzling");
224*d83cc019SAndroid Build Coastguard Worker break;
225*d83cc019SAndroid Build Coastguard Worker }
226*d83cc019SAndroid Build Coastguard Worker expected_val = calculate_expected(swizzled_offset);
227*d83cc019SAndroid Build Coastguard Worker found_val = linear[j / 4];
228*d83cc019SAndroid Build Coastguard Worker igt_assert_f(expected_val == found_val,
229*d83cc019SAndroid Build Coastguard Worker "Bad read [%d]: %d instead of %d at 0x%08x "
230*d83cc019SAndroid Build Coastguard Worker "for read from 0x%08x to 0x%08x, swizzle=%s\n",
231*d83cc019SAndroid Build Coastguard Worker i, found_val, expected_val, j + first_page,
232*d83cc019SAndroid Build Coastguard Worker offset, offset + len,
233*d83cc019SAndroid Build Coastguard Worker swizzle_str);
234*d83cc019SAndroid Build Coastguard Worker }
235*d83cc019SAndroid Build Coastguard Worker munmap(linear, last_page - first_page);
236*d83cc019SAndroid Build Coastguard Worker }
237*d83cc019SAndroid Build Coastguard Worker
238*d83cc019SAndroid Build Coastguard Worker close(fd);
239*d83cc019SAndroid Build Coastguard Worker }
240