2 * SPDX-License-Identifier: MIT
4 * Copyright © 2016 Intel Corporation
7 #include "display/intel_frontbuffer.h"
10 #include "i915_gem_clflush.h"
11 #include "i915_sw_fence_work.h"
12 #include "i915_trace.h"
15 struct dma_fence_work base;
16 struct drm_i915_gem_object *obj;
19 static void __do_clflush(struct drm_i915_gem_object *obj)
21 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
22 drm_clflush_sg(obj->mm.pages);
24 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
27 static int clflush_work(struct dma_fence_work *base)
29 struct clflush *clflush = container_of(base, typeof(*clflush), base);
31 __do_clflush(clflush->obj);
36 static void clflush_release(struct dma_fence_work *base)
38 struct clflush *clflush = container_of(base, typeof(*clflush), base);
40 i915_gem_object_unpin_pages(clflush->obj);
41 i915_gem_object_put(clflush->obj);
44 static const struct dma_fence_work_ops clflush_ops = {
47 .release = clflush_release,
50 static struct clflush *clflush_work_create(struct drm_i915_gem_object *obj)
52 struct clflush *clflush;
54 GEM_BUG_ON(!obj->cache_dirty);
56 clflush = kmalloc(sizeof(*clflush), GFP_KERNEL);
60 if (__i915_gem_object_get_pages(obj) < 0) {
65 dma_fence_work_init(&clflush->base, &clflush_ops);
66 clflush->obj = i915_gem_object_get(obj); /* obj <-> clflush cycle */
71 bool i915_gem_clflush_object(struct drm_i915_gem_object *obj,
74 struct clflush *clflush;
76 assert_object_held(obj);
79 * Stolen memory is always coherent with the GPU as it is explicitly
80 * marked as wc by the system, or the system is cache-coherent.
81 * Similarly, we only access struct pages through the CPU cache, so
82 * anything not backed by physical memory we consider to be always
83 * coherent and not need clflushing.
85 if (!i915_gem_object_has_struct_page(obj)) {
86 obj->cache_dirty = false;
90 /* If the GPU is snooping the contents of the CPU cache,
91 * we do not need to manually clear the CPU cache lines. However,
92 * the caches are only snooped when the render cache is
93 * flushed/invalidated. As we always have to emit invalidations
94 * and flushes when moving into and out of the RENDER domain, correct
95 * snooping behaviour occurs naturally as the result of our domain
98 if (!(flags & I915_CLFLUSH_FORCE) &&
99 obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
102 trace_i915_gem_object_clflush(obj);
105 if (!(flags & I915_CLFLUSH_SYNC))
106 clflush = clflush_work_create(obj);
108 i915_sw_fence_await_reservation(&clflush->base.chain,
109 obj->base.resv, NULL, true,
110 i915_fence_timeout(to_i915(obj->base.dev)),
112 dma_resv_add_excl_fence(obj->base.resv, &clflush->base.dma);
113 dma_fence_work_commit(&clflush->base);
114 } else if (obj->mm.pages) {
117 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
120 obj->cache_dirty = false;