• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2008 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 #include "igt.h"
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <inttypes.h>
35 #include <errno.h>
36 #include <sys/stat.h>
37 #include <sys/ioctl.h>
38 #include "drm.h"
39 
40 #define OBJECT_SIZE 16384
41 #define PAGE_SIZE 4096
42 int fd;
43 
44 static void
test_huge_bo(int huge)45 test_huge_bo(int huge)
46 {
47 	uint64_t huge_object_size, last_offset, i;
48 	unsigned check = CHECK_RAM;
49 	char *ptr_cpu;
50 	char *cpu_pattern;
51 	uint32_t bo;
52 	int loop;
53 
54 	switch (huge) {
55 	case -1:
56 		huge_object_size = gem_mappable_aperture_size() / 2;
57 		break;
58 	case 0:
59 		huge_object_size = gem_mappable_aperture_size() + PAGE_SIZE;
60 		break;
61 	case 1:
62 		huge_object_size = gem_aperture_size(fd) + PAGE_SIZE;
63 		break;
64 	case 2:
65 		huge_object_size = (intel_get_total_ram_mb() + 1) << 20;
66 		check |= CHECK_SWAP;
67 		break;
68 	default:
69 		return;
70 	}
71 	intel_require_memory(1, huge_object_size, check);
72 
73 	last_offset = huge_object_size - PAGE_SIZE;
74 
75 	cpu_pattern = malloc(PAGE_SIZE);
76 	igt_assert(cpu_pattern);
77 	for (i = 0; i < PAGE_SIZE; i++)
78 		cpu_pattern[i] = i;
79 
80 	bo = gem_create(fd, huge_object_size);
81 
82 	/* Obtain CPU mapping for the object. */
83 	ptr_cpu = __gem_mmap__cpu(fd, bo, 0, huge_object_size,
84 				PROT_READ | PROT_WRITE);
85 	igt_require(ptr_cpu);
86 	gem_set_domain(fd, bo, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
87 	gem_close(fd, bo);
88 
89 	igt_debug("Exercising %'llu bytes\n", (long long)huge_object_size);
90 
91 	loop = 0;
92 	do {
93 		/* Write first page through the mapping and
94 		 * assert reading it back works.
95 		 */
96 		memcpy(ptr_cpu, cpu_pattern, PAGE_SIZE);
97 		igt_assert(memcmp(ptr_cpu, cpu_pattern, PAGE_SIZE) == 0);
98 		memset(ptr_cpu, 0xcc, PAGE_SIZE);
99 
100 		/* Write last page through the mapping and
101 		 * assert reading it back works.
102 		 */
103 		memcpy(ptr_cpu + last_offset, cpu_pattern, PAGE_SIZE);
104 		igt_assert(memcmp(ptr_cpu + last_offset, cpu_pattern, PAGE_SIZE) == 0);
105 		memset(ptr_cpu + last_offset, 0xcc, PAGE_SIZE);
106 
107 		/* Cross check that accessing two simultaneous pages works. */
108 		igt_assert(memcmp(ptr_cpu, ptr_cpu + last_offset, PAGE_SIZE) == 0);
109 
110 		/* Force every page to be faulted and retest */
111 		for (i = 0; i < huge_object_size; i += 4096)
112 			ptr_cpu[i] = i >> 12;
113 	} while (loop++ == 0);
114 
115 	munmap(ptr_cpu, huge_object_size);
116 	free(cpu_pattern);
117 }
118 
119 static void
test_pf_nonblock(int i915)120 test_pf_nonblock(int i915)
121 {
122 	igt_spin_t *spin;
123 	uint32_t *ptr;
124 
125 	spin = igt_spin_new(i915);
126 
127 	igt_set_timeout(1, "initial pagefaulting did not complete within 1s");
128 
129 	ptr = gem_mmap__cpu(i915, spin->handle, 0, 4096, PROT_WRITE);
130 	ptr[256] = 0;
131 	munmap(ptr, 4096);
132 
133 	igt_reset_timeout();
134 
135 	igt_spin_free(i915, spin);
136 }
137 
mmap_ioctl(int i915,struct drm_i915_gem_mmap * arg)138 static int mmap_ioctl(int i915, struct drm_i915_gem_mmap *arg)
139 {
140 	int err = 0;
141 
142 	if (igt_ioctl(i915, DRM_IOCTL_I915_GEM_MMAP, arg))
143 		err = -errno;
144 
145 	errno = 0;
146 	return err;
147 }
148 
149 igt_main
150 {
151 	uint8_t expected[OBJECT_SIZE];
152 	uint8_t buf[OBJECT_SIZE];
153 	uint8_t *addr;
154 
155 	igt_fixture
156 		fd = drm_open_driver(DRIVER_INTEL);
157 
158 	igt_subtest("bad-object") {
159 		uint32_t real_handle = gem_create(fd, 4096);
160 		uint32_t handles[20];
161 		size_t i = 0, len;
162 
163 		handles[i++] = 0xdeadbeef;
164 		for(int bit = 0; bit < 16; bit++)
165 			handles[i++] = real_handle | (1 << (bit + 16));
166 		handles[i++] = real_handle + 1;
167 		len = i;
168 
169 		for (i = 0; i < len; ++i) {
170 			struct drm_i915_gem_mmap arg = {
171 				.handle = handles[i],
172 				.size = 4096,
173 			};
174 
175 			igt_debug("Trying MMAP IOCTL with handle %x\n", handles[i]);
176 			igt_assert_eq(mmap_ioctl(fd, &arg), -ENOENT);
177 		}
178 
179 		gem_close(fd, real_handle);
180 	}
181 
182 	igt_subtest("bad-offset") {
183 		struct bad_offset {
184 			uint64_t size;
185 			uint64_t offset;
186 		} bad_offsets[] = {
187 				{4096, 4096 + 1},
188 				{4096, -4096},
189 				{ 2 * 4096, -4096},
190 				{ 4096, ~0},
191 				{}
192 		};
193 
194 		for (int i = 0; i < ARRAY_SIZE(bad_offsets); i++) {
195 			struct drm_i915_gem_mmap arg = {
196 				.handle = gem_create(fd, 4096),
197 				.offset = bad_offsets[i].offset,
198 				.size = bad_offsets[i].size,
199 			};
200 
201 			igt_debug("Trying to mmap bad offset; size: %'"PRIu64", offset: %'"PRIu64"\n",
202 				  bad_offsets[i].size, bad_offsets[i].offset);
203 
204 			igt_assert_eq(mmap_ioctl(fd, &arg), -EINVAL);
205 			gem_close(fd, arg.handle);
206 		}
207 	}
208 
209 	igt_subtest("bad-size") {
210 		uint64_t bad_size[] = {
211 			0,
212 			-4096,
213 			4096 + 1,
214 			2 * 4096,
215 			~0,
216 		};
217 
218 		for (int i = 0; i < ARRAY_SIZE(bad_size); i++) {
219 			struct drm_i915_gem_mmap arg = {
220 				.handle = gem_create(fd, 4096),
221 				.offset = 4096,
222 				.size = bad_size[i],
223 			};
224 
225 			igt_debug("Trying to mmap bad size; size: %'"PRIu64"\n", bad_size[i]);
226 			igt_assert_eq(mmap_ioctl(fd, &arg), -EINVAL);
227 
228 			gem_close(fd, arg.handle);
229 		}
230 	}
231 
232 	igt_subtest("basic") {
233 		struct drm_i915_gem_mmap arg = {
234 			.handle = gem_create(fd, OBJECT_SIZE),
235 			.size = OBJECT_SIZE,
236 		};
237 		igt_assert_eq(mmap_ioctl(fd, &arg), 0);
238 		addr = from_user_pointer(arg.addr_ptr);
239 
240 		igt_info("Testing contents of newly created object.\n");
241 		memset(expected, 0, sizeof(expected));
242 		igt_assert(memcmp(addr, expected, sizeof(expected)) == 0);
243 
244 		igt_info("Testing coherency of writes and mmap reads.\n");
245 		memset(buf, 0, sizeof(buf));
246 		memset(buf + 1024, 0x01, 1024);
247 		memset(expected + 1024, 0x01, 1024);
248 		gem_write(fd, arg.handle, 0, buf, OBJECT_SIZE);
249 		igt_assert(memcmp(buf, addr, sizeof(buf)) == 0);
250 
251 		igt_info("Testing that mapping stays after close\n");
252 		gem_close(fd, arg.handle);
253 		igt_assert(memcmp(buf, addr, sizeof(buf)) == 0);
254 
255 		igt_info("Testing unmapping\n");
256 		munmap(addr, OBJECT_SIZE);
257 	}
258 
259 	igt_subtest("short-mmap") {
260 		uint32_t handle = gem_create(fd, OBJECT_SIZE);
261 
262 		igt_assert(OBJECT_SIZE > 4096);
263 
264 		addr = gem_mmap__cpu(fd, handle, 0, 4096, PROT_WRITE);
265 		memset(addr, 0, 4096);
266 		munmap(addr, 4096);
267 
268 		gem_close(fd, handle);
269 	}
270 
271 	igt_subtest("pf-nonblock")
272 		test_pf_nonblock(fd);
273 
274 	igt_subtest("basic-small-bo")
275 		test_huge_bo(-1);
276 	igt_subtest("big-bo")
277 		test_huge_bo(0);
278 	igt_subtest("huge-bo")
279 		test_huge_bo(1);
280 	igt_subtest("swap-bo")
281 		test_huge_bo(2);
282 
283 	igt_fixture
284 		close(fd);
285 }
286