1a85cb24fSFrançois Tigeot /*
2a85cb24fSFrançois Tigeot * Copyright © 2016 Intel Corporation
3a85cb24fSFrançois Tigeot *
4a85cb24fSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a
5a85cb24fSFrançois Tigeot * copy of this software and associated documentation files (the "Software"),
6a85cb24fSFrançois Tigeot * to deal in the Software without restriction, including without limitation
7a85cb24fSFrançois Tigeot * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8a85cb24fSFrançois Tigeot * and/or sell copies of the Software, and to permit persons to whom the
9a85cb24fSFrançois Tigeot * Software is furnished to do so, subject to the following conditions:
10a85cb24fSFrançois Tigeot *
11a85cb24fSFrançois Tigeot * The above copyright notice and this permission notice (including the next
12a85cb24fSFrançois Tigeot * paragraph) shall be included in all copies or substantial portions of the
13a85cb24fSFrançois Tigeot * Software.
14a85cb24fSFrançois Tigeot *
15a85cb24fSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16a85cb24fSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17a85cb24fSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18a85cb24fSFrançois Tigeot * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19a85cb24fSFrançois Tigeot * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20a85cb24fSFrançois Tigeot * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21a85cb24fSFrançois Tigeot * IN THE SOFTWARE.
22a85cb24fSFrançois Tigeot *
23a85cb24fSFrançois Tigeot */
24a85cb24fSFrançois Tigeot
25a85cb24fSFrançois Tigeot #include "i915_drv.h"
26a85cb24fSFrançois Tigeot #include "intel_frontbuffer.h"
27a85cb24fSFrançois Tigeot #include "i915_gem_clflush.h"
28a85cb24fSFrançois Tigeot
29a85cb24fSFrançois Tigeot static DEFINE_SPINLOCK(clflush_lock);
30a85cb24fSFrançois Tigeot
31a85cb24fSFrançois Tigeot struct clflush {
32a85cb24fSFrançois Tigeot struct dma_fence dma; /* Must be first for dma_fence_free() */
33a85cb24fSFrançois Tigeot struct i915_sw_fence wait;
34a85cb24fSFrançois Tigeot struct work_struct work;
35a85cb24fSFrançois Tigeot struct drm_i915_gem_object *obj;
36a85cb24fSFrançois Tigeot };
37a85cb24fSFrançois Tigeot
i915_clflush_get_driver_name(struct dma_fence * fence)38a85cb24fSFrançois Tigeot static const char *i915_clflush_get_driver_name(struct dma_fence *fence)
39a85cb24fSFrançois Tigeot {
40a85cb24fSFrançois Tigeot return DRIVER_NAME;
41a85cb24fSFrançois Tigeot }
42a85cb24fSFrançois Tigeot
i915_clflush_get_timeline_name(struct dma_fence * fence)43a85cb24fSFrançois Tigeot static const char *i915_clflush_get_timeline_name(struct dma_fence *fence)
44a85cb24fSFrançois Tigeot {
45a85cb24fSFrançois Tigeot return "clflush";
46a85cb24fSFrançois Tigeot }
47a85cb24fSFrançois Tigeot
i915_clflush_enable_signaling(struct dma_fence * fence)48a85cb24fSFrançois Tigeot static bool i915_clflush_enable_signaling(struct dma_fence *fence)
49a85cb24fSFrançois Tigeot {
50a85cb24fSFrançois Tigeot return true;
51a85cb24fSFrançois Tigeot }
52a85cb24fSFrançois Tigeot
i915_clflush_release(struct dma_fence * fence)53a85cb24fSFrançois Tigeot static void i915_clflush_release(struct dma_fence *fence)
54a85cb24fSFrançois Tigeot {
55a85cb24fSFrançois Tigeot struct clflush *clflush = container_of(fence, typeof(*clflush), dma);
56a85cb24fSFrançois Tigeot
57a85cb24fSFrançois Tigeot i915_sw_fence_fini(&clflush->wait);
58a85cb24fSFrançois Tigeot
59a85cb24fSFrançois Tigeot BUILD_BUG_ON(offsetof(typeof(*clflush), dma));
60a85cb24fSFrançois Tigeot dma_fence_free(&clflush->dma);
61a85cb24fSFrançois Tigeot }
62a85cb24fSFrançois Tigeot
63a85cb24fSFrançois Tigeot static const struct dma_fence_ops i915_clflush_ops = {
64a85cb24fSFrançois Tigeot .get_driver_name = i915_clflush_get_driver_name,
65a85cb24fSFrançois Tigeot .get_timeline_name = i915_clflush_get_timeline_name,
66a85cb24fSFrançois Tigeot .enable_signaling = i915_clflush_enable_signaling,
67a85cb24fSFrançois Tigeot .wait = dma_fence_default_wait,
68a85cb24fSFrançois Tigeot .release = i915_clflush_release,
69a85cb24fSFrançois Tigeot };
70a85cb24fSFrançois Tigeot
__i915_do_clflush(struct drm_i915_gem_object * obj)71a85cb24fSFrançois Tigeot static void __i915_do_clflush(struct drm_i915_gem_object *obj)
72a85cb24fSFrançois Tigeot {
73*3f2dd94aSFrançois Tigeot GEM_BUG_ON(!i915_gem_object_has_pages(obj));
74a85cb24fSFrançois Tigeot drm_clflush_sg(obj->mm.pages);
75a85cb24fSFrançois Tigeot intel_fb_obj_flush(obj, ORIGIN_CPU);
76a85cb24fSFrançois Tigeot }
77a85cb24fSFrançois Tigeot
i915_clflush_work(struct work_struct * work)78a85cb24fSFrançois Tigeot static void i915_clflush_work(struct work_struct *work)
79a85cb24fSFrançois Tigeot {
80a85cb24fSFrançois Tigeot struct clflush *clflush = container_of(work, typeof(*clflush), work);
81a85cb24fSFrançois Tigeot struct drm_i915_gem_object *obj = clflush->obj;
82a85cb24fSFrançois Tigeot
83a85cb24fSFrançois Tigeot if (i915_gem_object_pin_pages(obj)) {
84a85cb24fSFrançois Tigeot DRM_ERROR("Failed to acquire obj->pages for clflushing\n");
85a85cb24fSFrançois Tigeot goto out;
86a85cb24fSFrançois Tigeot }
87a85cb24fSFrançois Tigeot
88a85cb24fSFrançois Tigeot __i915_do_clflush(obj);
89a85cb24fSFrançois Tigeot
90a85cb24fSFrançois Tigeot i915_gem_object_unpin_pages(obj);
91a85cb24fSFrançois Tigeot
92a85cb24fSFrançois Tigeot out:
93a85cb24fSFrançois Tigeot i915_gem_object_put(obj);
94a85cb24fSFrançois Tigeot
95a85cb24fSFrançois Tigeot dma_fence_signal(&clflush->dma);
96a85cb24fSFrançois Tigeot dma_fence_put(&clflush->dma);
97a85cb24fSFrançois Tigeot }
98a85cb24fSFrançois Tigeot
99a85cb24fSFrançois Tigeot static int __i915_sw_fence_call
i915_clflush_notify(struct i915_sw_fence * fence,enum i915_sw_fence_notify state)100a85cb24fSFrançois Tigeot i915_clflush_notify(struct i915_sw_fence *fence,
101a85cb24fSFrançois Tigeot enum i915_sw_fence_notify state)
102a85cb24fSFrançois Tigeot {
103a85cb24fSFrançois Tigeot struct clflush *clflush = container_of(fence, typeof(*clflush), wait);
104a85cb24fSFrançois Tigeot
105a85cb24fSFrançois Tigeot switch (state) {
106a85cb24fSFrançois Tigeot case FENCE_COMPLETE:
107a85cb24fSFrançois Tigeot schedule_work(&clflush->work);
108a85cb24fSFrançois Tigeot break;
109a85cb24fSFrançois Tigeot
110a85cb24fSFrançois Tigeot case FENCE_FREE:
111a85cb24fSFrançois Tigeot dma_fence_put(&clflush->dma);
112a85cb24fSFrançois Tigeot break;
113a85cb24fSFrançois Tigeot }
114a85cb24fSFrançois Tigeot
115a85cb24fSFrançois Tigeot return NOTIFY_DONE;
116a85cb24fSFrançois Tigeot }
117a85cb24fSFrançois Tigeot
i915_gem_clflush_object(struct drm_i915_gem_object * obj,unsigned int flags)118*3f2dd94aSFrançois Tigeot bool i915_gem_clflush_object(struct drm_i915_gem_object *obj,
119a85cb24fSFrançois Tigeot unsigned int flags)
120a85cb24fSFrançois Tigeot {
121a85cb24fSFrançois Tigeot struct clflush *clflush;
122a85cb24fSFrançois Tigeot
123a85cb24fSFrançois Tigeot /*
124a85cb24fSFrançois Tigeot * Stolen memory is always coherent with the GPU as it is explicitly
125a85cb24fSFrançois Tigeot * marked as wc by the system, or the system is cache-coherent.
126a85cb24fSFrançois Tigeot * Similarly, we only access struct pages through the CPU cache, so
127a85cb24fSFrançois Tigeot * anything not backed by physical memory we consider to be always
128a85cb24fSFrançois Tigeot * coherent and not need clflushing.
129a85cb24fSFrançois Tigeot */
130*3f2dd94aSFrançois Tigeot if (!i915_gem_object_has_struct_page(obj)) {
131*3f2dd94aSFrançois Tigeot obj->cache_dirty = false;
132*3f2dd94aSFrançois Tigeot return false;
133*3f2dd94aSFrançois Tigeot }
134a85cb24fSFrançois Tigeot
135a85cb24fSFrançois Tigeot /* If the GPU is snooping the contents of the CPU cache,
136a85cb24fSFrançois Tigeot * we do not need to manually clear the CPU cache lines. However,
137a85cb24fSFrançois Tigeot * the caches are only snooped when the render cache is
138a85cb24fSFrançois Tigeot * flushed/invalidated. As we always have to emit invalidations
139a85cb24fSFrançois Tigeot * and flushes when moving into and out of the RENDER domain, correct
140a85cb24fSFrançois Tigeot * snooping behaviour occurs naturally as the result of our domain
141a85cb24fSFrançois Tigeot * tracking.
142a85cb24fSFrançois Tigeot */
143*3f2dd94aSFrançois Tigeot if (!(flags & I915_CLFLUSH_FORCE) &&
144*3f2dd94aSFrançois Tigeot obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)
145*3f2dd94aSFrançois Tigeot return false;
146a85cb24fSFrançois Tigeot
147a85cb24fSFrançois Tigeot trace_i915_gem_object_clflush(obj);
148a85cb24fSFrançois Tigeot
149a85cb24fSFrançois Tigeot clflush = NULL;
150a85cb24fSFrançois Tigeot if (!(flags & I915_CLFLUSH_SYNC))
151a85cb24fSFrançois Tigeot clflush = kmalloc(sizeof(*clflush), M_DRM, GFP_KERNEL);
152a85cb24fSFrançois Tigeot if (clflush) {
153*3f2dd94aSFrançois Tigeot GEM_BUG_ON(!obj->cache_dirty);
154*3f2dd94aSFrançois Tigeot
155a85cb24fSFrançois Tigeot dma_fence_init(&clflush->dma,
156a85cb24fSFrançois Tigeot &i915_clflush_ops,
157a85cb24fSFrançois Tigeot &clflush_lock,
158*3f2dd94aSFrançois Tigeot to_i915(obj->base.dev)->mm.unordered_timeline,
159a85cb24fSFrançois Tigeot 0);
160a85cb24fSFrançois Tigeot i915_sw_fence_init(&clflush->wait, i915_clflush_notify);
161a85cb24fSFrançois Tigeot
162a85cb24fSFrançois Tigeot clflush->obj = i915_gem_object_get(obj);
163a85cb24fSFrançois Tigeot INIT_WORK(&clflush->work, i915_clflush_work);
164a85cb24fSFrançois Tigeot
165a85cb24fSFrançois Tigeot dma_fence_get(&clflush->dma);
166a85cb24fSFrançois Tigeot
167a85cb24fSFrançois Tigeot i915_sw_fence_await_reservation(&clflush->wait,
168a85cb24fSFrançois Tigeot obj->resv, NULL,
169a85cb24fSFrançois Tigeot true, I915_FENCE_TIMEOUT,
170a85cb24fSFrançois Tigeot GFP_KERNEL);
171a85cb24fSFrançois Tigeot
172a85cb24fSFrançois Tigeot reservation_object_lock(obj->resv, NULL);
173a85cb24fSFrançois Tigeot reservation_object_add_excl_fence(obj->resv, &clflush->dma);
174a85cb24fSFrançois Tigeot reservation_object_unlock(obj->resv);
175a85cb24fSFrançois Tigeot
176a85cb24fSFrançois Tigeot i915_sw_fence_commit(&clflush->wait);
177a85cb24fSFrançois Tigeot } else if (obj->mm.pages) {
178a85cb24fSFrançois Tigeot __i915_do_clflush(obj);
179a85cb24fSFrançois Tigeot } else {
180a85cb24fSFrançois Tigeot GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
181a85cb24fSFrançois Tigeot }
182a85cb24fSFrançois Tigeot
183*3f2dd94aSFrançois Tigeot obj->cache_dirty = false;
184*3f2dd94aSFrançois Tigeot return true;
185a85cb24fSFrançois Tigeot }
186