2 * SPDX-License-Identifier: MIT
4 * Copyright © 2016-2018 Intel Corporation
9 #include "i915_timeline.h"
10 #include "i915_syncmap.h"
12 struct i915_timeline_hwsp {
14 struct list_head free_link;
18 static inline struct i915_timeline_hwsp *
19 i915_timeline_hwsp(const struct i915_timeline *tl)
21 return tl->hwsp_ggtt->private;
24 static struct i915_vma *__hwsp_alloc(struct drm_i915_private *i915)
26 struct drm_i915_gem_object *obj;
29 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
33 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
35 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
37 i915_gem_object_put(obj);
42 static struct i915_vma *
43 hwsp_alloc(struct i915_timeline *timeline, unsigned int *cacheline)
45 struct drm_i915_private *i915 = timeline->i915;
46 struct i915_gt_timelines *gt = &i915->gt.timelines;
47 struct i915_timeline_hwsp *hwsp;
49 BUILD_BUG_ON(BITS_PER_TYPE(u64) * CACHELINE_BYTES > PAGE_SIZE);
51 spin_lock(>->hwsp_lock);
53 /* hwsp_free_list only contains HWSP that have available cachelines */
54 hwsp = list_first_entry_or_null(>->hwsp_free_list,
55 typeof(*hwsp), free_link);
59 spin_unlock(>->hwsp_lock);
61 hwsp = kmalloc(sizeof(*hwsp), GFP_KERNEL);
63 return ERR_PTR(-ENOMEM);
65 vma = __hwsp_alloc(i915);
73 hwsp->free_bitmap = ~0ull;
75 spin_lock(>->hwsp_lock);
76 list_add(&hwsp->free_link, >->hwsp_free_list);
79 GEM_BUG_ON(!hwsp->free_bitmap);
80 *cacheline = __ffs64(hwsp->free_bitmap);
81 hwsp->free_bitmap &= ~BIT_ULL(*cacheline);
82 if (!hwsp->free_bitmap)
83 list_del(&hwsp->free_link);
85 spin_unlock(>->hwsp_lock);
87 GEM_BUG_ON(hwsp->vma->private != hwsp);
91 static void hwsp_free(struct i915_timeline *timeline)
93 struct i915_gt_timelines *gt = &timeline->i915->gt.timelines;
94 struct i915_timeline_hwsp *hwsp;
96 hwsp = i915_timeline_hwsp(timeline);
97 if (!hwsp) /* leave global HWSP alone! */
100 spin_lock(>->hwsp_lock);
102 /* As a cacheline becomes available, publish the HWSP on the freelist */
103 if (!hwsp->free_bitmap)
104 list_add_tail(&hwsp->free_link, >->hwsp_free_list);
106 hwsp->free_bitmap |= BIT_ULL(timeline->hwsp_offset / CACHELINE_BYTES);
108 /* And if no one is left using it, give the page back to the system */
109 if (hwsp->free_bitmap == ~0ull) {
110 i915_vma_put(hwsp->vma);
111 list_del(&hwsp->free_link);
115 spin_unlock(>->hwsp_lock);
118 int i915_timeline_init(struct drm_i915_private *i915,
119 struct i915_timeline *timeline,
121 struct i915_vma *hwsp)
126 * Ideally we want a set of engines on a single leaf as we expect
127 * to mostly be tracking synchronisation between engines. It is not
128 * a huge issue if this is not the case, but we may want to mitigate
129 * any page crossing penalties if they become an issue.
131 * Called during early_init before we know how many engines there are.
133 BUILD_BUG_ON(KSYNCMAP < I915_NUM_ENGINES);
135 timeline->i915 = i915;
136 timeline->name = name;
137 timeline->pin_count = 0;
138 timeline->has_initial_breadcrumb = !hwsp;
140 timeline->hwsp_offset = I915_GEM_HWS_SEQNO_ADDR;
142 unsigned int cacheline;
144 hwsp = hwsp_alloc(timeline, &cacheline);
146 return PTR_ERR(hwsp);
148 timeline->hwsp_offset = cacheline * CACHELINE_BYTES;
150 timeline->hwsp_ggtt = i915_vma_get(hwsp);
152 vaddr = i915_gem_object_pin_map(hwsp->obj, I915_MAP_WB);
156 return PTR_ERR(vaddr);
159 timeline->hwsp_seqno =
160 memset(vaddr + timeline->hwsp_offset, 0, CACHELINE_BYTES);
162 timeline->fence_context = dma_fence_context_alloc(1);
164 spin_lock_init(&timeline->lock);
166 INIT_ACTIVE_REQUEST(&timeline->barrier);
167 INIT_ACTIVE_REQUEST(&timeline->last_request);
168 INIT_LIST_HEAD(&timeline->requests);
170 i915_syncmap_init(&timeline->sync);
175 void i915_timelines_init(struct drm_i915_private *i915)
177 struct i915_gt_timelines *gt = &i915->gt.timelines;
179 mutex_init(>->mutex);
180 INIT_LIST_HEAD(>->active_list);
182 spin_lock_init(>->hwsp_lock);
183 INIT_LIST_HEAD(>->hwsp_free_list);
185 /* via i915_gem_wait_for_idle() */
186 i915_gem_shrinker_taints_mutex(i915, >->mutex);
189 static void timeline_add_to_active(struct i915_timeline *tl)
191 struct i915_gt_timelines *gt = &tl->i915->gt.timelines;
193 mutex_lock(>->mutex);
194 list_add(&tl->link, >->active_list);
195 mutex_unlock(>->mutex);
198 static void timeline_remove_from_active(struct i915_timeline *tl)
200 struct i915_gt_timelines *gt = &tl->i915->gt.timelines;
202 mutex_lock(>->mutex);
204 mutex_unlock(>->mutex);
208 * i915_timelines_park - called when the driver idles
209 * @i915: the drm_i915_private device
211 * When the driver is completely idle, we know that all of our sync points
212 * have been signaled and our tracking is then entirely redundant. Any request
213 * to wait upon an older sync point will be completed instantly as we know
214 * the fence is signaled and therefore we will not even look them up in the
217 void i915_timelines_park(struct drm_i915_private *i915)
219 struct i915_gt_timelines *gt = &i915->gt.timelines;
220 struct i915_timeline *timeline;
222 mutex_lock(>->mutex);
223 list_for_each_entry(timeline, >->active_list, link) {
225 * All known fences are completed so we can scrap
226 * the current sync point tracking and start afresh,
227 * any attempt to wait upon a previous sync point
228 * will be skipped as the fence was signaled.
230 i915_syncmap_free(&timeline->sync);
232 mutex_unlock(>->mutex);
235 void i915_timeline_fini(struct i915_timeline *timeline)
237 GEM_BUG_ON(timeline->pin_count);
238 GEM_BUG_ON(!list_empty(&timeline->requests));
239 GEM_BUG_ON(i915_active_request_isset(&timeline->barrier));
241 i915_syncmap_free(&timeline->sync);
244 i915_gem_object_unpin_map(timeline->hwsp_ggtt->obj);
245 i915_vma_put(timeline->hwsp_ggtt);
248 struct i915_timeline *
249 i915_timeline_create(struct drm_i915_private *i915,
251 struct i915_vma *global_hwsp)
253 struct i915_timeline *timeline;
256 timeline = kzalloc(sizeof(*timeline), GFP_KERNEL);
258 return ERR_PTR(-ENOMEM);
260 err = i915_timeline_init(i915, timeline, name, global_hwsp);
266 kref_init(&timeline->kref);
271 int i915_timeline_pin(struct i915_timeline *tl)
277 GEM_BUG_ON(!tl->pin_count);
279 err = i915_vma_pin(tl->hwsp_ggtt, 0, 0, PIN_GLOBAL | PIN_HIGH);
284 i915_ggtt_offset(tl->hwsp_ggtt) +
285 offset_in_page(tl->hwsp_offset);
287 timeline_add_to_active(tl);
296 void i915_timeline_unpin(struct i915_timeline *tl)
298 GEM_BUG_ON(!tl->pin_count);
302 timeline_remove_from_active(tl);
305 * Since this timeline is idle, all bariers upon which we were waiting
306 * must also be complete and so we can discard the last used barriers
307 * without loss of information.
309 i915_syncmap_free(&tl->sync);
311 __i915_vma_unpin(tl->hwsp_ggtt);
314 void __i915_timeline_free(struct kref *kref)
316 struct i915_timeline *timeline =
317 container_of(kref, typeof(*timeline), kref);
319 i915_timeline_fini(timeline);
323 void i915_timelines_fini(struct drm_i915_private *i915)
325 struct i915_gt_timelines *gt = &i915->gt.timelines;
327 GEM_BUG_ON(!list_empty(>->active_list));
328 GEM_BUG_ON(!list_empty(>->hwsp_free_list));
330 mutex_destroy(>->mutex);
333 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
334 #include "selftests/mock_timeline.c"
335 #include "selftests/i915_timeline.c"