blob: 623b7e531f60cec957b216c8aea739414959e3bd [file] [log] [blame]
Daniel Vetterf5854c82011-12-02 20:36:06 +01001/*
2 * Copyright © 2011 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 * Daniel Vetter <daniel.vetter@ffwll.ch>
25 *
26 */
27
Thomas Wood804e11f2015-08-17 17:57:43 +010028#include "igt.h"
Daniel Vetterf5854c82011-12-02 20:36:06 +010029#include <unistd.h>
30#include <stdlib.h>
31#include <stdint.h>
32#include <stdio.h>
33#include <string.h>
Daniel Vetterf5854c82011-12-02 20:36:06 +010034#include <fcntl.h>
35#include <inttypes.h>
36#include <errno.h>
37#include <sys/stat.h>
38#include <sys/ioctl.h>
Daniel Vetterf5854c82011-12-02 20:36:06 +010039#include <sys/time.h>
40#include "drm.h"
Daniel Vetterf5854c82011-12-02 20:36:06 +010041
42/* Testcase: Submit patches with relocations in memory that will fault
43 *
44 * To be really evil, use a gtt mmap for them.
45 */
46
Thomas Woodb2ac2642014-11-28 11:02:44 +000047IGT_TEST_DESCRIPTION("Submit patches with relocations in memory that will"
48 " fault.");
49
Daniel Vetterf5854c82011-12-02 20:36:06 +010050#define OBJECT_SIZE 16384
51
Ben Widawsky8ac7b932013-12-05 16:02:52 -080052#define COPY_BLT_CMD_NOLEN (2<<29|0x53<<22)
Daniel Vetterf5854c82011-12-02 20:36:06 +010053#define BLT_WRITE_ALPHA (1<<21)
54#define BLT_WRITE_RGB (1<<20)
55#define BLT_SRC_TILED (1<<15)
56#define BLT_DST_TILED (1<<11)
57
Ben Widawsky3e2937b2013-10-08 17:38:43 -070058uint32_t devid;
59
Daniel Vetterf5854c82011-12-02 20:36:06 +010060static int gem_linear_blt(uint32_t *batch,
61 uint32_t src,
62 uint32_t dst,
63 uint32_t length,
64 struct drm_i915_gem_relocation_entry *reloc)
65{
66 uint32_t *b = batch;
67 int height = length / (16 * 1024);
68
Matt Roper07be8fe2015-03-05 15:01:00 -080069 igt_assert_lte(height, 1 << 16);
Daniel Vetterf5854c82011-12-02 20:36:06 +010070
71 if (height) {
Ben Widawsky4de3b172013-10-08 17:36:21 -070072 int i = 0;
Ben Widawsky8ac7b932013-12-05 16:02:52 -080073 b[i++] = COPY_BLT_CMD_NOLEN | BLT_WRITE_ALPHA | BLT_WRITE_RGB;
74 if (intel_gen(devid) >= 8)
75 b[i-1] |= 8;
76 else
77 b[i-1] |= 6;
Ben Widawsky4de3b172013-10-08 17:36:21 -070078 b[i++] = 0xcc << 16 | 1 << 25 | 1 << 24 | (16*1024);
79 b[i++] = 0;
80 b[i++] = height << 16 | (4*1024);
81 b[i++] = 0;
Daniel Vetterf5854c82011-12-02 20:36:06 +010082 reloc->offset = (b-batch+4) * sizeof(uint32_t);
83 reloc->delta = 0;
84 reloc->target_handle = dst;
85 reloc->read_domains = I915_GEM_DOMAIN_RENDER;
86 reloc->write_domain = I915_GEM_DOMAIN_RENDER;
87 reloc->presumed_offset = 0;
88 reloc++;
89
Ben Widawsky3e2937b2013-10-08 17:38:43 -070090 if (intel_gen(devid) >= 8)
91 b[i++] = 0; /* FIXME: use real high dword */
92
Ben Widawsky4de3b172013-10-08 17:36:21 -070093 b[i++] = 0;
94 b[i++] = 16*1024;
95 b[i++] = 0;
Daniel Vetterf5854c82011-12-02 20:36:06 +010096 reloc->offset = (b-batch+7) * sizeof(uint32_t);
Ben Widawsky8ac7b932013-12-05 16:02:52 -080097 if (intel_gen(devid) >= 8) {
98 reloc->offset += sizeof(uint32_t);
99 b[i++] = 0; /* FIXME: use real high dword */
100 }
Daniel Vetterf5854c82011-12-02 20:36:06 +0100101 reloc->delta = 0;
102 reloc->target_handle = src;
103 reloc->read_domains = I915_GEM_DOMAIN_RENDER;
104 reloc->write_domain = 0;
105 reloc->presumed_offset = 0;
106 reloc++;
107
Ben Widawsky3e2937b2013-10-08 17:38:43 -0700108 if (intel_gen(devid) >= 8)
109 b += 10;
110 else
111 b += 8;
Daniel Vetterf5854c82011-12-02 20:36:06 +0100112 length -= height * 16*1024;
113 }
114
115 if (length) {
Ben Widawsky4de3b172013-10-08 17:36:21 -0700116 int i = 0;
Ben Widawsky8ac7b932013-12-05 16:02:52 -0800117 b[i++] = COPY_BLT_CMD_NOLEN | BLT_WRITE_ALPHA | BLT_WRITE_RGB;
118 if (intel_gen(devid) >= 8)
119 b[i-1] |= 8;
120 else
121 b[i-1] |= 6;
Ben Widawsky4de3b172013-10-08 17:36:21 -0700122 b[i++] = 0xcc << 16 | 1 << 25 | 1 << 24 | (16*1024);
123 b[i++] = height << 16;
124 b[i++] = (1+height) << 16 | (length / 4);
125 b[i++] = 0;
Daniel Vetterf5854c82011-12-02 20:36:06 +0100126 reloc->offset = (b-batch+4) * sizeof(uint32_t);
127 reloc->delta = 0;
128 reloc->target_handle = dst;
129 reloc->read_domains = I915_GEM_DOMAIN_RENDER;
130 reloc->write_domain = I915_GEM_DOMAIN_RENDER;
131 reloc->presumed_offset = 0;
132 reloc++;
Ben Widawsky3e2937b2013-10-08 17:38:43 -0700133 if (intel_gen(devid) >= 8)
134 b[i++] = 0; /* FIXME: use real high dword */
Daniel Vetterf5854c82011-12-02 20:36:06 +0100135
Ben Widawsky4de3b172013-10-08 17:36:21 -0700136 b[i++] = height << 16;
137 b[i++] = 16*1024;
138 b[i++] = 0;
Daniel Vetterf5854c82011-12-02 20:36:06 +0100139 reloc->offset = (b-batch+7) * sizeof(uint32_t);
Ben Widawsky8ac7b932013-12-05 16:02:52 -0800140 if (intel_gen(devid) >= 8) {
141 reloc->offset += sizeof(uint32_t);
142 b[i++] = 0; /* FIXME: use real high dword */
143 }
Daniel Vetterf5854c82011-12-02 20:36:06 +0100144 reloc->delta = 0;
145 reloc->target_handle = src;
146 reloc->read_domains = I915_GEM_DOMAIN_RENDER;
147 reloc->write_domain = 0;
148 reloc->presumed_offset = 0;
149 reloc++;
150
Ben Widawsky3e2937b2013-10-08 17:38:43 -0700151 if (intel_gen(devid) >= 8)
152 b += 10;
153 else
154 b += 8;
Daniel Vetterf5854c82011-12-02 20:36:06 +0100155 }
156
157 b[0] = MI_BATCH_BUFFER_END;
158 b[1] = 0;
159
160 return (b+2 - batch) * sizeof(uint32_t);
161}
162
Daniel Vetterf5854c82011-12-02 20:36:06 +0100163static void run(int object_size)
164{
165 struct drm_i915_gem_execbuffer2 execbuf;
166 struct drm_i915_gem_exec_object2 exec[3];
167 struct drm_i915_gem_relocation_entry reloc[4];
Ben Widawsky8ac7b932013-12-05 16:02:52 -0800168 uint32_t buf[40];
Daniel Vetterf5854c82011-12-02 20:36:06 +0100169 uint32_t handle, handle_relocs, src, dst;
170 void *gtt_relocs;
171 int fd, len;
Daniel Vettera7a80c22012-01-10 15:50:20 +0100172 int ring;
Daniel Vetterf5854c82011-12-02 20:36:06 +0100173
Micah Fedkec81d2932015-07-22 21:54:02 +0000174 fd = drm_open_driver(DRIVER_INTEL);
Ben Widawsky3e2937b2013-10-08 17:38:43 -0700175 devid = intel_get_drm_devid(fd);
Daniel Vetterf5854c82011-12-02 20:36:06 +0100176 handle = gem_create(fd, 4096);
177 src = gem_create(fd, object_size);
178 dst = gem_create(fd, object_size);
179
180 len = gem_linear_blt(buf, src, dst, object_size, reloc);
181 gem_write(fd, handle, 0, buf, len);
182
183 exec[0].handle = src;
184 exec[0].relocation_count = 0;
185 exec[0].relocs_ptr = 0;
186 exec[0].alignment = 0;
187 exec[0].offset = 0;
188 exec[0].flags = 0;
189 exec[0].rsvd1 = 0;
190 exec[0].rsvd2 = 0;
191
192 exec[1].handle = dst;
193 exec[1].relocation_count = 0;
194 exec[1].relocs_ptr = 0;
195 exec[1].alignment = 0;
196 exec[1].offset = 0;
197 exec[1].flags = 0;
198 exec[1].rsvd1 = 0;
199 exec[1].rsvd2 = 0;
200
201 handle_relocs = gem_create(fd, 4096);
Daniel Vetter319638b2012-01-10 15:31:11 +0100202 gem_write(fd, handle_relocs, 0, reloc, sizeof(reloc));
Daniel Vetter11fa3582012-03-24 19:17:43 +0100203 gtt_relocs = gem_mmap(fd, handle_relocs, 4096,
204 PROT_READ | PROT_WRITE);
Daniel Vetter83440952013-08-13 12:35:58 +0200205 igt_assert(gtt_relocs);
Daniel Vetterf5854c82011-12-02 20:36:06 +0100206
207 exec[2].handle = handle;
Ben Widawsky8ac7b932013-12-05 16:02:52 -0800208 if (intel_gen(devid) >= 8)
209 exec[2].relocation_count = len > 56 ? 4 : 2;
210 else
211 exec[2].relocation_count = len > 40 ? 4 : 2;
Daniel Vetterf5854c82011-12-02 20:36:06 +0100212 /* A newly mmap gtt bo will fault on first access. */
213 exec[2].relocs_ptr = (uintptr_t)gtt_relocs;
214 exec[2].alignment = 0;
215 exec[2].offset = 0;
216 exec[2].flags = 0;
217 exec[2].rsvd1 = 0;
218 exec[2].rsvd2 = 0;
219
220 ring = 0;
Ben Widawsky3e2937b2013-10-08 17:38:43 -0700221 if (HAS_BLT_RING(devid))
Daniel Vetterf5854c82011-12-02 20:36:06 +0100222 ring = I915_EXEC_BLT;
223
224 execbuf.buffers_ptr = (uintptr_t)exec;
225 execbuf.buffer_count = 3;
226 execbuf.batch_start_offset = 0;
227 execbuf.batch_len = len;
228 execbuf.cliprects_ptr = 0;
229 execbuf.num_cliprects = 0;
230 execbuf.DR1 = 0;
231 execbuf.DR4 = 0;
232 execbuf.flags = ring;
Ben Widawsky5a28ef82012-03-18 18:42:44 -0700233 i915_execbuffer2_set_context_id(execbuf, 0);
Daniel Vetterf5854c82011-12-02 20:36:06 +0100234 execbuf.rsvd2 = 0;
235
Daniel Vetter9cc16e82013-09-03 10:37:14 +0200236 gem_execbuf(fd, &execbuf);
Daniel Vetterf5854c82011-12-02 20:36:06 +0100237 gem_sync(fd, handle);
238
239 gem_close(fd, handle);
240
241 close(fd);
242}
243
Daniel Vetter071e9ca2013-10-31 16:23:26 +0100244igt_main
Daniel Vetterf5854c82011-12-02 20:36:06 +0100245{
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200246 igt_subtest("normal")
Xiong Zhangfc5c6932013-07-19 18:42:52 +0800247 run(OBJECT_SIZE);
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200248 igt_subtest("no-prefault") {
249 igt_disable_prefault();
Xiong Zhangfc5c6932013-07-19 18:42:52 +0800250 run(OBJECT_SIZE);
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200251 igt_enable_prefault();
Xiong Zhangfc5c6932013-07-19 18:42:52 +0800252 }
Daniel Vetterf5854c82011-12-02 20:36:06 +0100253}