lib/igt.cocci: Add stanza for for_each_pipe
[platform/upstream/intel-gpu-tools.git] / tests / gem_tiled_pread.c
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  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27
28 /** @file gem_tiled_pread.c
29  *
30  * This is a test of pread's behavior on tiled objects with respect to the
31  * reported swizzling value.
32  *
33  * The goal is to exercise the slow_bit17_copy path for reading on bit17
34  * machines, but will also be useful for catching swizzling value bugs on
35  * other systems.
36  */
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <inttypes.h>
43 #include <errno.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <sys/ioctl.h>
47 #include "drm.h"
48 #include "ioctl_wrappers.h"
49 #include "drmtest.h"
50 #include "intel_io.h"
51 #include "intel_chipset.h"
52
53 #define WIDTH 512
54 #define HEIGHT 512
55 static uint32_t linear[WIDTH * HEIGHT];
56
57 #define PAGE_SIZE 4096
58
59 static int tile_width;
60 static int tile_height;
61 static int tile_size;
62
63 static uint32_t
64 create_bo(int fd)
65 {
66         uint32_t handle;
67         uint32_t *data;
68         int i;
69
70         handle = gem_create(fd, sizeof(linear));
71         gem_set_tiling(fd, handle, I915_TILING_X, WIDTH * sizeof(uint32_t));
72
73         /* Fill the BO with dwords starting at start_val */
74         data = gem_mmap(fd, handle, sizeof(linear), PROT_READ | PROT_WRITE);
75         for (i = 0; i < WIDTH*HEIGHT; i++)
76                 data[i] = i;
77         munmap(data, sizeof(linear));
78
79         return handle;
80 }
81
82 static int
83 swizzle_bit(int bit, int offset)
84 {
85         return (offset & (1 << bit)) >> (bit - 6);
86 }
87
88 /* Translate from a swizzled offset in the tiled buffer to the corresponding
89  * value from the original linear buffer.
90  */
91 static uint32_t
92 calculate_expected(int offset)
93 {
94         int tile_off = offset & (tile_size - 1);
95         int tile_base = offset & -tile_size;
96         int tile_index = tile_base / tile_size;
97         int tiles_per_row = 4*WIDTH / tile_width;
98
99         /* base x,y values from the tile (page) index. */
100         int base_y = tile_index / tiles_per_row * tile_height;
101         int base_x = tile_index % tiles_per_row * (tile_width/4);
102
103         /* x, y offsets within the tile */
104         int tile_y = tile_off / tile_width;
105         int tile_x = (tile_off % tile_width) / 4;
106
107         igt_debug("%3d, %3d, %3d,%3d\n", base_x, base_y, tile_x, tile_y);
108         return (base_y + tile_y) * WIDTH + base_x + tile_x;
109 }
110
111 igt_simple_main
112 {
113         int fd;
114         int i, iter = 100;
115         uint32_t tiling, swizzle;
116         uint32_t handle;
117         uint32_t devid;
118
119         fd = drm_open_any();
120
121         handle = create_bo(fd);
122         gem_get_tiling(fd, handle, &tiling, &swizzle);
123
124         devid = intel_get_drm_devid(fd);
125
126         if (IS_GEN2(devid)) {
127                 tile_height = 16;
128                 tile_width = 128;
129                 tile_size = 2048;
130         } else {
131                 tile_height = 8;
132                 tile_width = 512;
133                 tile_size = PAGE_SIZE;
134         }
135
136         /* Read a bunch of random subsets of the data and check that they come
137          * out right.
138          */
139         for (i = 0; i < iter; i++) {
140                 int size = WIDTH * HEIGHT * 4;
141                 int offset = (random() % size) & ~3;
142                 int len = (random() % size) & ~3;
143                 int j;
144
145                 if (len == 0)
146                         len = 4;
147
148                 if (offset + len > size)
149                         len = size - offset;
150
151                 if (i == 0) {
152                         offset = 0;
153                         len = size;
154                 }
155
156                 gem_read(fd, handle, offset, linear, len);
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_assert_f(0, "Bad swizzle bits; %d\n",
199                                              swizzle);
200                         }
201                         expected_val = calculate_expected(swizzled_offset);
202                         found_val = linear[(j - offset) / 4];
203                         igt_assert_f(expected_val == found_val,
204                                      "Bad read [%d]: %d instead of %d at 0x%08x "
205                                      "for read from 0x%08x to 0x%08x, swizzle=%s\n",
206                                      i, found_val, expected_val, j,
207                                      offset, offset + len,
208                                      swizzle_str);
209                 }
210         }
211
212         close(fd);
213 }