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_pwrite.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 /*
39 * Testcase: Test swizzling by testing pwrite does the inverse of pread
40 *
41 * Together with the explicit pread testcase, this should cover our swizzle
42 * handling.
43 *
44 * Note that this test will use swap in an effort to test all of ram.
45 */
46
47 #include "igt.h"
48 #include "igt_x86.h"
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <fcntl.h>
53 #include <inttypes.h>
54 #include <errno.h>
55 #include <sys/stat.h>
56 #include <sys/time.h>
57 #include <sys/ioctl.h>
58
59 #include <drm.h>
60
61
62 IGT_TEST_DESCRIPTION("Test swizzling by testing pwrite does the inverse of"
63 " pread.");
64
65 #define WIDTH 512
66 #define HEIGHT 512
67 static uint32_t linear[WIDTH * HEIGHT];
68 static uint32_t current_tiling_mode;
69
70 #define PAGE_SIZE 4096
71
72 static uint32_t
create_bo_and_fill(int fd)73 create_bo_and_fill(int fd)
74 {
75 uint32_t handle;
76 uint32_t *data;
77 int i;
78
79 handle = gem_create(fd, sizeof(linear));
80 gem_set_tiling(fd, handle, current_tiling_mode, WIDTH * sizeof(uint32_t));
81
82 /* Fill the BO with dwords starting at start_val */
83 data = gem_mmap__gtt(fd, handle, sizeof(linear),
84 PROT_READ | PROT_WRITE);
85 gem_set_domain(fd, handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
86 for (i = 0; i < WIDTH*HEIGHT; i++)
87 data[i] = i;
88 munmap(data, sizeof(linear));
89
90 return handle;
91 }
92
93 static uint32_t
create_bo(int fd)94 create_bo(int fd)
95 {
96 uint32_t handle;
97
98 handle = gem_create(fd, sizeof(linear));
99 gem_set_tiling(fd, handle, current_tiling_mode, WIDTH * sizeof(uint32_t));
100
101 return handle;
102 }
103
copy_wc_page(void * dst,const void * src)104 static void copy_wc_page(void *dst, const void *src)
105 {
106 igt_memcpy_from_wc(dst, src, PAGE_SIZE);
107 }
108
109 igt_simple_main
110 {
111 uint32_t tiling, swizzle;
112 int count;
113 int fd;
114
115 fd = drm_open_driver(DRIVER_INTEL);
116 count = SLOW_QUICK(intel_get_total_ram_mb() * 9 / 10, 8) ;
117
118 for (int i = 0; i < count/2; i++) {
119 uint32_t handle, handle_target;
120 char *data;
121 int n;
122
123 current_tiling_mode = I915_TILING_X;
124
125 handle = create_bo_and_fill(fd);
126 igt_require(gem_get_tiling(fd, handle, &tiling, &swizzle));
127
128 gem_read(fd, handle, 0, linear, sizeof(linear));
129
130 handle_target = create_bo(fd);
131 gem_write(fd, handle_target, 0, linear, sizeof(linear));
132
133 /* Check the target bo's contents. */
134 data = gem_mmap__gtt(fd, handle_target, sizeof(linear), PROT_READ);
135 n = 0;
136 for (int pfn = 0; pfn < sizeof(linear)/PAGE_SIZE; pfn++) {
137 uint32_t page[PAGE_SIZE/sizeof(uint32_t)];
138 copy_wc_page(page, data + PAGE_SIZE*pfn);
139 for (int j = 0; j < PAGE_SIZE/sizeof(uint32_t); j++) {
140 igt_assert_f(page[j] == n,
141 "mismatch at %i: %i\n",
142 n, page[j]);
143 n++;
144 }
145 }
146 munmap(data, sizeof(linear));
147
148 /* Leak both bos so that we use all of system mem! */
149 gem_madvise(fd, handle_target, I915_MADV_DONTNEED);
150 gem_madvise(fd, handle, I915_MADV_DONTNEED);
151
152 igt_progress("gem_tiled_pread_pwrite: ", i, count/2);
153 }
154
155 close(fd);
156 }
157