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;
39 err = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
43 page = i915_gem_object_get_page(obj, offset >> PAGE_SHIFT);
44 map = kmap_atomic(page);
46 if (needs_clflush & CLFLUSH_BEFORE) {
48 clflush(map+offset_in_page(offset) / sizeof(*map));
52 map[offset_in_page(offset) / sizeof(*map)] = v;
54 if (needs_clflush & CLFLUSH_AFTER) {
56 clflush(map+offset_in_page(offset) / sizeof(*map));
62 i915_gem_obj_finish_shmem_access(obj);
66 static int cpu_get(struct drm_i915_gem_object *obj,
70 unsigned int needs_clflush;
75 err = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
79 page = i915_gem_object_get_page(obj, offset >> PAGE_SHIFT);
80 map = kmap_atomic(page);
82 if (needs_clflush & CLFLUSH_BEFORE) {
84 clflush(map+offset_in_page(offset) / sizeof(*map));
88 *v = map[offset_in_page(offset) / sizeof(*map)];
91 i915_gem_obj_finish_shmem_access(obj);
95 static int gtt_set(struct drm_i915_gem_object *obj,
103 err = i915_gem_object_set_to_gtt_domain(obj, true);
107 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
111 map = i915_vma_pin_iomap(vma);
116 iowrite32(v, &map[offset / sizeof(*map)]);
117 i915_vma_unpin_iomap(vma);
122 static int gtt_get(struct drm_i915_gem_object *obj,
123 unsigned long offset,
126 struct i915_vma *vma;
130 err = i915_gem_object_set_to_gtt_domain(obj, false);
134 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
138 map = i915_vma_pin_iomap(vma);
143 *v = ioread32(&map[offset / sizeof(*map)]);
144 i915_vma_unpin_iomap(vma);
149 static int wc_set(struct drm_i915_gem_object *obj,
150 unsigned long offset,
156 err = i915_gem_object_set_to_wc_domain(obj, true);
160 map = i915_gem_object_pin_map(obj, I915_MAP_WC);
164 map[offset / sizeof(*map)] = v;
165 i915_gem_object_unpin_map(obj);
170 static int wc_get(struct drm_i915_gem_object *obj,
171 unsigned long offset,
177 err = i915_gem_object_set_to_wc_domain(obj, false);
181 map = i915_gem_object_pin_map(obj, I915_MAP_WC);
185 *v = map[offset / sizeof(*map)];
186 i915_gem_object_unpin_map(obj);
191 static int gpu_set(struct drm_i915_gem_object *obj,
192 unsigned long offset,
195 struct drm_i915_private *i915 = to_i915(obj->base.dev);
196 struct i915_request *rq;
197 struct i915_vma *vma;
201 err = i915_gem_object_set_to_gtt_domain(obj, true);
205 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
209 rq = i915_request_alloc(i915->engine[RCS], i915->kernel_context);
215 cs = intel_ring_begin(rq, 4);
217 i915_request_add(rq);
222 if (INTEL_GEN(i915) >= 8) {
223 *cs++ = MI_STORE_DWORD_IMM_GEN4 | 1 << 22;
224 *cs++ = lower_32_bits(i915_ggtt_offset(vma) + offset);
225 *cs++ = upper_32_bits(i915_ggtt_offset(vma) + offset);
227 } else if (INTEL_GEN(i915) >= 4) {
228 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
230 *cs++ = i915_ggtt_offset(vma) + offset;
233 *cs++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
234 *cs++ = i915_ggtt_offset(vma) + offset;
238 intel_ring_advance(rq, cs);
240 err = i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
243 i915_request_add(rq);
248 static bool always_valid(struct drm_i915_private *i915)
253 static bool needs_fence_registers(struct drm_i915_private *i915)
255 return !i915_terminally_wedged(&i915->gpu_error);
258 static bool needs_mi_store_dword(struct drm_i915_private *i915)
260 if (i915_terminally_wedged(&i915->gpu_error))
263 return intel_engine_can_store_dword(i915->engine[RCS]);
266 static const struct igt_coherency_mode {
268 int (*set)(struct drm_i915_gem_object *, unsigned long offset, u32 v);
269 int (*get)(struct drm_i915_gem_object *, unsigned long offset, u32 *v);
270 bool (*valid)(struct drm_i915_private *i915);
271 } igt_coherency_mode[] = {
272 { "cpu", cpu_set, cpu_get, always_valid },
273 { "gtt", gtt_set, gtt_get, needs_fence_registers },
274 { "wc", wc_set, wc_get, always_valid },
275 { "gpu", gpu_set, NULL, needs_mi_store_dword },
279 static int igt_gem_coherency(void *arg)
281 const unsigned int ncachelines = PAGE_SIZE/64;
282 I915_RND_STATE(prng);
283 struct drm_i915_private *i915 = arg;
284 const struct igt_coherency_mode *read, *write, *over;
285 struct drm_i915_gem_object *obj;
286 unsigned long count, n;
287 u32 *offsets, *values;
290 /* We repeatedly write, overwrite and read from a sequence of
291 * cachelines in order to try and detect incoherency (unflushed writes
292 * from either the CPU or GPU). Each setter/getter uses our cache
293 * domain API which should prevent incoherency.
296 offsets = kmalloc_array(ncachelines, 2*sizeof(u32), GFP_KERNEL);
299 for (count = 0; count < ncachelines; count++)
300 offsets[count] = count * 64 + 4 * (count % 16);
302 values = offsets + ncachelines;
304 mutex_lock(&i915->drm.struct_mutex);
305 for (over = igt_coherency_mode; over->name; over++) {
309 if (!over->valid(i915))
312 for (write = igt_coherency_mode; write->name; write++) {
316 if (!write->valid(i915))
319 for (read = igt_coherency_mode; read->name; read++) {
323 if (!read->valid(i915))
326 for_each_prime_number_from(count, 1, ncachelines) {
327 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
333 i915_random_reorder(offsets, ncachelines, &prng);
334 for (n = 0; n < count; n++)
335 values[n] = prandom_u32_state(&prng);
337 for (n = 0; n < count; n++) {
338 err = over->set(obj, offsets[n], ~values[n]);
340 pr_err("Failed to set stale value[%ld/%ld] in object using %s, err=%d\n",
341 n, count, over->name, err);
346 for (n = 0; n < count; n++) {
347 err = write->set(obj, offsets[n], values[n]);
349 pr_err("Failed to set value[%ld/%ld] in object using %s, err=%d\n",
350 n, count, write->name, err);
355 for (n = 0; n < count; n++) {
358 err = read->get(obj, offsets[n], &found);
360 pr_err("Failed to get value[%ld/%ld] in object using %s, err=%d\n",
361 n, count, read->name, err);
365 if (found != values[n]) {
366 pr_err("Value[%ld/%ld] mismatch, (overwrite with %s) wrote [%s] %x read [%s] %x (inverse %x), at offset %x\n",
367 n, count, over->name,
368 write->name, values[n],
370 ~values[n], offsets[n]);
376 __i915_gem_object_release_unless_active(obj);
382 mutex_unlock(&i915->drm.struct_mutex);
387 __i915_gem_object_release_unless_active(obj);
391 int i915_gem_coherency_live_selftests(struct drm_i915_private *i915)
393 static const struct i915_subtest tests[] = {
394 SUBTEST(igt_gem_coherency),
397 return i915_subtests(tests, i915);