2 * Copyright © 2017 Intel Corporation
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:
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
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
25 #include "../i915_selftest.h"
28 #include "huge_gem_object.h"
30 #define DW_PER_PAGE (PAGE_SIZE / sizeof(u32))
32 static struct i915_vma *
33 gpu_fill_dw(struct i915_vma *vma, u64 offset, unsigned long count, u32 value)
35 struct drm_i915_gem_object *obj;
36 const int gen = INTEL_GEN(vma->vm->i915);
37 unsigned long n, size;
41 size = (4 * count + 1) * sizeof(u32);
42 size = round_up(size, PAGE_SIZE);
43 obj = i915_gem_object_create_internal(vma->vm->i915, size);
47 cmd = i915_gem_object_pin_map(obj, I915_MAP_WB);
53 GEM_BUG_ON(offset + (count - 1) * PAGE_SIZE > vma->node.size);
54 offset += vma->node.start;
56 for (n = 0; n < count; n++) {
58 *cmd++ = MI_STORE_DWORD_IMM_GEN4;
59 *cmd++ = lower_32_bits(offset);
60 *cmd++ = upper_32_bits(offset);
62 } else if (gen >= 4) {
63 *cmd++ = MI_STORE_DWORD_IMM_GEN4 |
64 (gen < 6 ? 1 << 22 : 0);
69 *cmd++ = MI_STORE_DWORD_IMM | 1 << 22;
75 *cmd = MI_BATCH_BUFFER_END;
76 i915_gem_object_unpin_map(obj);
78 err = i915_gem_object_set_to_gtt_domain(obj, false);
82 vma = i915_vma_instance(obj, vma->vm, NULL);
88 err = i915_vma_pin(vma, 0, 0, PIN_USER);
95 i915_gem_object_put(obj);
99 static unsigned long real_page_count(struct drm_i915_gem_object *obj)
101 return huge_gem_object_phys_size(obj) >> PAGE_SHIFT;
104 static unsigned long fake_page_count(struct drm_i915_gem_object *obj)
106 return huge_gem_object_dma_size(obj) >> PAGE_SHIFT;
109 static int gpu_fill(struct drm_i915_gem_object *obj,
110 struct i915_gem_context *ctx,
111 struct intel_engine_cs *engine,
114 struct drm_i915_private *i915 = to_i915(obj->base.dev);
115 struct i915_address_space *vm =
116 ctx->ppgtt ? &ctx->ppgtt->base : &i915->ggtt.base;
117 struct drm_i915_gem_request *rq;
118 struct i915_vma *vma;
119 struct i915_vma *batch;
123 GEM_BUG_ON(obj->base.size > vm->total);
124 GEM_BUG_ON(!intel_engine_can_store_dword(engine));
126 vma = i915_vma_instance(obj, vm, NULL);
130 err = i915_gem_object_set_to_gtt_domain(obj, false);
134 err = i915_vma_pin(vma, 0, 0, PIN_HIGH | PIN_USER);
138 /* Within the GTT the huge objects maps every page onto
139 * its 1024 real pages (using phys_pfn = dma_pfn % 1024).
140 * We set the nth dword within the page using the nth
141 * mapping via the GTT - this should exercise the GTT mapping
142 * whilst checking that each context provides a unique view
145 batch = gpu_fill_dw(vma,
146 (dw * real_page_count(obj)) << PAGE_SHIFT |
148 real_page_count(obj),
151 err = PTR_ERR(batch);
155 rq = i915_gem_request_alloc(engine, ctx);
161 err = i915_switch_context(rq);
166 if (INTEL_GEN(vm->i915) <= 5)
167 flags |= I915_DISPATCH_SECURE;
169 err = engine->emit_bb_start(rq,
170 batch->node.start, batch->node.size,
175 i915_vma_move_to_active(batch, rq, 0);
176 i915_gem_object_set_active_reference(batch->obj);
177 i915_vma_unpin(batch);
178 i915_vma_close(batch);
180 i915_vma_move_to_active(vma, rq, 0);
183 reservation_object_lock(obj->resv, NULL);
184 reservation_object_add_excl_fence(obj->resv, &rq->fence);
185 reservation_object_unlock(obj->resv);
187 __i915_add_request(rq, true);
192 __i915_add_request(rq, false);
194 i915_vma_unpin(batch);
200 static int cpu_fill(struct drm_i915_gem_object *obj, u32 value)
202 const bool has_llc = HAS_LLC(to_i915(obj->base.dev));
203 unsigned int n, m, need_flush;
206 err = i915_gem_obj_prepare_shmem_write(obj, &need_flush);
210 for (n = 0; n < real_page_count(obj); n++) {
213 map = kmap_atomic(i915_gem_object_get_page(obj, n));
214 for (m = 0; m < DW_PER_PAGE; m++)
217 drm_clflush_virt_range(map, PAGE_SIZE);
221 i915_gem_obj_finish_shmem_access(obj);
222 obj->base.read_domains = I915_GEM_DOMAIN_GTT | I915_GEM_DOMAIN_CPU;
223 obj->base.write_domain = 0;
227 static int cpu_check(struct drm_i915_gem_object *obj, unsigned int max)
229 unsigned int n, m, needs_flush;
232 err = i915_gem_obj_prepare_shmem_read(obj, &needs_flush);
236 for (n = 0; n < real_page_count(obj); n++) {
239 map = kmap_atomic(i915_gem_object_get_page(obj, n));
240 if (needs_flush & CLFLUSH_BEFORE)
241 drm_clflush_virt_range(map, PAGE_SIZE);
243 for (m = 0; m < max; m++) {
245 pr_err("Invalid value at page %d, offset %d: found %x expected %x\n",
252 for (; m < DW_PER_PAGE; m++) {
253 if (map[m] != 0xdeadbeef) {
254 pr_err("Invalid value at page %d, offset %d: found %x expected %x\n",
255 n, m, map[m], 0xdeadbeef);
267 i915_gem_obj_finish_shmem_access(obj);
271 static struct drm_i915_gem_object *
272 create_test_object(struct i915_gem_context *ctx,
273 struct drm_file *file,
274 struct list_head *objects)
276 struct drm_i915_gem_object *obj;
277 struct i915_address_space *vm =
278 ctx->ppgtt ? &ctx->ppgtt->base : &ctx->i915->ggtt.base;
283 size = min(vm->total / 2, 1024ull * DW_PER_PAGE * PAGE_SIZE);
284 size = round_down(size, DW_PER_PAGE * PAGE_SIZE);
286 obj = huge_gem_object(ctx->i915, DW_PER_PAGE * PAGE_SIZE, size);
290 /* tie the handle to the drm_file for easy reaping */
291 err = drm_gem_handle_create(file, &obj->base, &handle);
292 i915_gem_object_put(obj);
296 err = cpu_fill(obj, 0xdeadbeef);
298 pr_err("Failed to fill object with cpu, err=%d\n",
303 list_add_tail(&obj->st_link, objects);
307 static unsigned long max_dwords(struct drm_i915_gem_object *obj)
309 unsigned long npages = fake_page_count(obj);
311 GEM_BUG_ON(!IS_ALIGNED(npages, DW_PER_PAGE));
312 return npages / DW_PER_PAGE;
315 static int igt_ctx_exec(void *arg)
317 struct drm_i915_private *i915 = arg;
318 struct drm_i915_gem_object *obj = NULL;
319 struct drm_file *file;
320 IGT_TIMEOUT(end_time);
322 unsigned long ncontexts, ndwords, dw;
323 bool first_shared_gtt = true;
326 /* Create a few different contexts (with different mm) and write
327 * through each ctx/mm using the GPU making sure those writes end
328 * up in the expected pages of our obj.
331 file = mock_file(i915);
333 return PTR_ERR(file);
335 mutex_lock(&i915->drm.struct_mutex);
340 while (!time_after(jiffies, end_time)) {
341 struct intel_engine_cs *engine;
342 struct i915_gem_context *ctx;
345 if (first_shared_gtt) {
346 ctx = __create_hw_context(i915, file->driver_priv);
347 first_shared_gtt = false;
349 ctx = i915_gem_create_context(i915, file->driver_priv);
356 for_each_engine(engine, i915, id) {
357 if (!intel_engine_can_store_dword(engine))
361 obj = create_test_object(ctx, file, &objects);
368 err = gpu_fill(obj, ctx, engine, dw);
370 pr_err("Failed to fill dword %lu [%lu/%lu] with gpu (%s) in ctx %u [full-ppgtt? %s], err=%d\n",
371 ndwords, dw, max_dwords(obj),
372 engine->name, ctx->hw_id,
373 yesno(!!ctx->ppgtt), err);
377 if (++dw == max_dwords(obj)) {
385 pr_info("Submitted %lu contexts (across %u engines), filling %lu dwords\n",
386 ncontexts, INTEL_INFO(i915)->num_rings, ndwords);
389 list_for_each_entry(obj, &objects, st_link) {
391 min_t(unsigned int, ndwords - dw, max_dwords(obj));
393 err = cpu_check(obj, rem);
401 mutex_unlock(&i915->drm.struct_mutex);
403 mock_file_free(i915, file);
407 static int fake_aliasing_ppgtt_enable(struct drm_i915_private *i915)
409 struct drm_i915_gem_object *obj;
412 err = i915_gem_init_aliasing_ppgtt(i915);
416 list_for_each_entry(obj, &i915->mm.bound_list, mm.link) {
417 struct i915_vma *vma;
419 vma = i915_vma_instance(obj, &i915->ggtt.base, NULL);
423 vma->flags &= ~I915_VMA_LOCAL_BIND;
429 static void fake_aliasing_ppgtt_disable(struct drm_i915_private *i915)
431 i915_gem_fini_aliasing_ppgtt(i915);
434 int i915_gem_context_live_selftests(struct drm_i915_private *dev_priv)
436 static const struct i915_subtest tests[] = {
437 SUBTEST(igt_ctx_exec),
439 bool fake_alias = false;
442 /* Install a fake aliasing gtt for exercise */
443 if (USES_PPGTT(dev_priv) && !dev_priv->mm.aliasing_ppgtt) {
444 mutex_lock(&dev_priv->drm.struct_mutex);
445 err = fake_aliasing_ppgtt_enable(dev_priv);
446 mutex_unlock(&dev_priv->drm.struct_mutex);
450 GEM_BUG_ON(!dev_priv->mm.aliasing_ppgtt);
454 err = i915_subtests(tests, dev_priv);
457 mutex_lock(&dev_priv->drm.struct_mutex);
458 fake_aliasing_ppgtt_disable(dev_priv);
459 mutex_unlock(&dev_priv->drm.struct_mutex);