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 <linux/prime_numbers.h>
27 #include "../i915_selftest.h"
28 #include "i915_random.h"
30 static int cpu_set(struct drm_i915_gem_object *obj,
34 unsigned int needs_clflush;
40 err = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
44 page = i915_gem_object_get_page(obj, offset >> PAGE_SHIFT);
45 map = kmap_atomic(page);
46 cpu = map + offset_in_page(offset);
48 if (needs_clflush & CLFLUSH_BEFORE)
49 drm_clflush_virt_range(cpu, sizeof(*cpu));
53 if (needs_clflush & CLFLUSH_AFTER)
54 drm_clflush_virt_range(cpu, sizeof(*cpu));
57 i915_gem_obj_finish_shmem_access(obj);
62 static int cpu_get(struct drm_i915_gem_object *obj,
66 unsigned int needs_clflush;
72 err = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
76 page = i915_gem_object_get_page(obj, offset >> PAGE_SHIFT);
77 map = kmap_atomic(page);
78 cpu = map + offset_in_page(offset);
80 if (needs_clflush & CLFLUSH_BEFORE)
81 drm_clflush_virt_range(cpu, sizeof(*cpu));
86 i915_gem_obj_finish_shmem_access(obj);
91 static int gtt_set(struct drm_i915_gem_object *obj,
99 err = i915_gem_object_set_to_gtt_domain(obj, true);
103 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
107 map = i915_vma_pin_iomap(vma);
112 iowrite32(v, &map[offset / sizeof(*map)]);
113 i915_vma_unpin_iomap(vma);
118 static int gtt_get(struct drm_i915_gem_object *obj,
119 unsigned long offset,
122 struct i915_vma *vma;
126 err = i915_gem_object_set_to_gtt_domain(obj, false);
130 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
134 map = i915_vma_pin_iomap(vma);
139 *v = ioread32(&map[offset / sizeof(*map)]);
140 i915_vma_unpin_iomap(vma);
145 static int wc_set(struct drm_i915_gem_object *obj,
146 unsigned long offset,
152 err = i915_gem_object_set_to_wc_domain(obj, true);
156 map = i915_gem_object_pin_map(obj, I915_MAP_WC);
160 map[offset / sizeof(*map)] = v;
161 i915_gem_object_unpin_map(obj);
166 static int wc_get(struct drm_i915_gem_object *obj,
167 unsigned long offset,
173 err = i915_gem_object_set_to_wc_domain(obj, false);
177 map = i915_gem_object_pin_map(obj, I915_MAP_WC);
181 *v = map[offset / sizeof(*map)];
182 i915_gem_object_unpin_map(obj);
187 static int gpu_set(struct drm_i915_gem_object *obj,
188 unsigned long offset,
191 struct drm_i915_private *i915 = to_i915(obj->base.dev);
192 struct i915_request *rq;
193 struct i915_vma *vma;
197 err = i915_gem_object_set_to_gtt_domain(obj, true);
201 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
205 rq = i915_request_alloc(i915->engine[RCS0], i915->kernel_context);
211 cs = intel_ring_begin(rq, 4);
213 i915_request_add(rq);
218 if (INTEL_GEN(i915) >= 8) {
219 *cs++ = MI_STORE_DWORD_IMM_GEN4 | 1 << 22;
220 *cs++ = lower_32_bits(i915_ggtt_offset(vma) + offset);
221 *cs++ = upper_32_bits(i915_ggtt_offset(vma) + offset);
223 } else if (INTEL_GEN(i915) >= 4) {
224 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
226 *cs++ = i915_ggtt_offset(vma) + offset;
229 *cs++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
230 *cs++ = i915_ggtt_offset(vma) + offset;
234 intel_ring_advance(rq, cs);
236 err = i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
239 i915_request_add(rq);
244 static bool always_valid(struct drm_i915_private *i915)
249 static bool needs_fence_registers(struct drm_i915_private *i915)
251 return !i915_terminally_wedged(i915);
254 static bool needs_mi_store_dword(struct drm_i915_private *i915)
256 if (i915_terminally_wedged(i915))
259 return intel_engine_can_store_dword(i915->engine[RCS0]);
262 static const struct igt_coherency_mode {
264 int (*set)(struct drm_i915_gem_object *, unsigned long offset, u32 v);
265 int (*get)(struct drm_i915_gem_object *, unsigned long offset, u32 *v);
266 bool (*valid)(struct drm_i915_private *i915);
267 } igt_coherency_mode[] = {
268 { "cpu", cpu_set, cpu_get, always_valid },
269 { "gtt", gtt_set, gtt_get, needs_fence_registers },
270 { "wc", wc_set, wc_get, always_valid },
271 { "gpu", gpu_set, NULL, needs_mi_store_dword },
275 static int igt_gem_coherency(void *arg)
277 const unsigned int ncachelines = PAGE_SIZE/64;
278 I915_RND_STATE(prng);
279 struct drm_i915_private *i915 = arg;
280 const struct igt_coherency_mode *read, *write, *over;
281 struct drm_i915_gem_object *obj;
282 intel_wakeref_t wakeref;
283 unsigned long count, n;
284 u32 *offsets, *values;
287 /* We repeatedly write, overwrite and read from a sequence of
288 * cachelines in order to try and detect incoherency (unflushed writes
289 * from either the CPU or GPU). Each setter/getter uses our cache
290 * domain API which should prevent incoherency.
293 offsets = kmalloc_array(ncachelines, 2*sizeof(u32), GFP_KERNEL);
296 for (count = 0; count < ncachelines; count++)
297 offsets[count] = count * 64 + 4 * (count % 16);
299 values = offsets + ncachelines;
301 mutex_lock(&i915->drm.struct_mutex);
302 wakeref = intel_runtime_pm_get(i915);
303 for (over = igt_coherency_mode; over->name; over++) {
307 if (!over->valid(i915))
310 for (write = igt_coherency_mode; write->name; write++) {
314 if (!write->valid(i915))
317 for (read = igt_coherency_mode; read->name; read++) {
321 if (!read->valid(i915))
324 for_each_prime_number_from(count, 1, ncachelines) {
325 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
331 i915_random_reorder(offsets, ncachelines, &prng);
332 for (n = 0; n < count; n++)
333 values[n] = prandom_u32_state(&prng);
335 for (n = 0; n < count; n++) {
336 err = over->set(obj, offsets[n], ~values[n]);
338 pr_err("Failed to set stale value[%ld/%ld] in object using %s, err=%d\n",
339 n, count, over->name, err);
344 for (n = 0; n < count; n++) {
345 err = write->set(obj, offsets[n], values[n]);
347 pr_err("Failed to set value[%ld/%ld] in object using %s, err=%d\n",
348 n, count, write->name, err);
353 for (n = 0; n < count; n++) {
356 err = read->get(obj, offsets[n], &found);
358 pr_err("Failed to get value[%ld/%ld] in object using %s, err=%d\n",
359 n, count, read->name, err);
363 if (found != values[n]) {
364 pr_err("Value[%ld/%ld] mismatch, (overwrite with %s) wrote [%s] %x read [%s] %x (inverse %x), at offset %x\n",
365 n, count, over->name,
366 write->name, values[n],
368 ~values[n], offsets[n]);
374 __i915_gem_object_release_unless_active(obj);
380 intel_runtime_pm_put(i915, wakeref);
381 mutex_unlock(&i915->drm.struct_mutex);
386 __i915_gem_object_release_unless_active(obj);
390 int i915_gem_coherency_live_selftests(struct drm_i915_private *i915)
392 static const struct i915_subtest tests[] = {
393 SUBTEST(igt_gem_coherency),
396 return i915_subtests(tests, i915);