drm/i915/gt: Use the local HWSP offset during submission
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / gt / intel_timeline.c
blobee505cb4710f442658bb2b4510de9cb4076ba3e0
1 /*
2 * SPDX-License-Identifier: MIT
4 * Copyright © 2016-2018 Intel Corporation
5 */
7 #include "i915_drv.h"
9 #include "i915_active.h"
10 #include "i915_syncmap.h"
11 #include "intel_gt.h"
12 #include "intel_ring.h"
13 #include "intel_timeline.h"
15 #define ptr_set_bit(ptr, bit) ((typeof(ptr))((unsigned long)(ptr) | BIT(bit)))
16 #define ptr_test_bit(ptr, bit) ((unsigned long)(ptr) & BIT(bit))
18 #define CACHELINE_BITS 6
19 #define CACHELINE_FREE CACHELINE_BITS
21 struct intel_timeline_hwsp {
22 struct intel_gt *gt;
23 struct intel_gt_timelines *gt_timelines;
24 struct list_head free_link;
25 struct i915_vma *vma;
26 u64 free_bitmap;
29 static struct i915_vma *__hwsp_alloc(struct intel_gt *gt)
31 struct drm_i915_private *i915 = gt->i915;
32 struct drm_i915_gem_object *obj;
33 struct i915_vma *vma;
35 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
36 if (IS_ERR(obj))
37 return ERR_CAST(obj);
39 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
41 vma = i915_vma_instance(obj, &gt->ggtt->vm, NULL);
42 if (IS_ERR(vma))
43 i915_gem_object_put(obj);
45 return vma;
48 static struct i915_vma *
49 hwsp_alloc(struct intel_timeline *timeline, unsigned int *cacheline)
51 struct intel_gt_timelines *gt = &timeline->gt->timelines;
52 struct intel_timeline_hwsp *hwsp;
54 BUILD_BUG_ON(BITS_PER_TYPE(u64) * CACHELINE_BYTES > PAGE_SIZE);
56 spin_lock_irq(&gt->hwsp_lock);
58 /* hwsp_free_list only contains HWSP that have available cachelines */
59 hwsp = list_first_entry_or_null(&gt->hwsp_free_list,
60 typeof(*hwsp), free_link);
61 if (!hwsp) {
62 struct i915_vma *vma;
64 spin_unlock_irq(&gt->hwsp_lock);
66 hwsp = kmalloc(sizeof(*hwsp), GFP_KERNEL);
67 if (!hwsp)
68 return ERR_PTR(-ENOMEM);
70 vma = __hwsp_alloc(timeline->gt);
71 if (IS_ERR(vma)) {
72 kfree(hwsp);
73 return vma;
76 GT_TRACE(timeline->gt, "new HWSP allocated\n");
78 vma->private = hwsp;
79 hwsp->gt = timeline->gt;
80 hwsp->vma = vma;
81 hwsp->free_bitmap = ~0ull;
82 hwsp->gt_timelines = gt;
84 spin_lock_irq(&gt->hwsp_lock);
85 list_add(&hwsp->free_link, &gt->hwsp_free_list);
88 GEM_BUG_ON(!hwsp->free_bitmap);
89 *cacheline = __ffs64(hwsp->free_bitmap);
90 hwsp->free_bitmap &= ~BIT_ULL(*cacheline);
91 if (!hwsp->free_bitmap)
92 list_del(&hwsp->free_link);
94 spin_unlock_irq(&gt->hwsp_lock);
96 GEM_BUG_ON(hwsp->vma->private != hwsp);
97 return hwsp->vma;
100 static void __idle_hwsp_free(struct intel_timeline_hwsp *hwsp, int cacheline)
102 struct intel_gt_timelines *gt = hwsp->gt_timelines;
103 unsigned long flags;
105 spin_lock_irqsave(&gt->hwsp_lock, flags);
107 /* As a cacheline becomes available, publish the HWSP on the freelist */
108 if (!hwsp->free_bitmap)
109 list_add_tail(&hwsp->free_link, &gt->hwsp_free_list);
111 GEM_BUG_ON(cacheline >= BITS_PER_TYPE(hwsp->free_bitmap));
112 hwsp->free_bitmap |= BIT_ULL(cacheline);
114 /* And if no one is left using it, give the page back to the system */
115 if (hwsp->free_bitmap == ~0ull) {
116 i915_vma_put(hwsp->vma);
117 list_del(&hwsp->free_link);
118 kfree(hwsp);
121 spin_unlock_irqrestore(&gt->hwsp_lock, flags);
124 static void __rcu_cacheline_free(struct rcu_head *rcu)
126 struct intel_timeline_cacheline *cl =
127 container_of(rcu, typeof(*cl), rcu);
129 i915_active_fini(&cl->active);
130 kfree(cl);
133 static void __idle_cacheline_free(struct intel_timeline_cacheline *cl)
135 GEM_BUG_ON(!i915_active_is_idle(&cl->active));
137 i915_gem_object_unpin_map(cl->hwsp->vma->obj);
138 i915_vma_put(cl->hwsp->vma);
139 __idle_hwsp_free(cl->hwsp, ptr_unmask_bits(cl->vaddr, CACHELINE_BITS));
141 call_rcu(&cl->rcu, __rcu_cacheline_free);
144 __i915_active_call
145 static void __cacheline_retire(struct i915_active *active)
147 struct intel_timeline_cacheline *cl =
148 container_of(active, typeof(*cl), active);
150 i915_vma_unpin(cl->hwsp->vma);
151 if (ptr_test_bit(cl->vaddr, CACHELINE_FREE))
152 __idle_cacheline_free(cl);
155 static int __cacheline_active(struct i915_active *active)
157 struct intel_timeline_cacheline *cl =
158 container_of(active, typeof(*cl), active);
160 __i915_vma_pin(cl->hwsp->vma);
161 return 0;
164 static struct intel_timeline_cacheline *
165 cacheline_alloc(struct intel_timeline_hwsp *hwsp, unsigned int cacheline)
167 struct intel_timeline_cacheline *cl;
168 void *vaddr;
170 GEM_BUG_ON(cacheline >= BIT(CACHELINE_BITS));
172 cl = kmalloc(sizeof(*cl), GFP_KERNEL);
173 if (!cl)
174 return ERR_PTR(-ENOMEM);
176 vaddr = i915_gem_object_pin_map(hwsp->vma->obj, I915_MAP_WB);
177 if (IS_ERR(vaddr)) {
178 kfree(cl);
179 return ERR_CAST(vaddr);
182 i915_vma_get(hwsp->vma);
183 cl->hwsp = hwsp;
184 cl->vaddr = page_pack_bits(vaddr, cacheline);
186 i915_active_init(&cl->active, __cacheline_active, __cacheline_retire);
188 return cl;
191 static void cacheline_acquire(struct intel_timeline_cacheline *cl,
192 u32 ggtt_offset)
194 if (!cl)
195 return;
197 cl->ggtt_offset = ggtt_offset;
198 i915_active_acquire(&cl->active);
201 static void cacheline_release(struct intel_timeline_cacheline *cl)
203 if (cl)
204 i915_active_release(&cl->active);
207 static void cacheline_free(struct intel_timeline_cacheline *cl)
209 if (!i915_active_acquire_if_busy(&cl->active)) {
210 __idle_cacheline_free(cl);
211 return;
214 GEM_BUG_ON(ptr_test_bit(cl->vaddr, CACHELINE_FREE));
215 cl->vaddr = ptr_set_bit(cl->vaddr, CACHELINE_FREE);
217 i915_active_release(&cl->active);
220 static int intel_timeline_init(struct intel_timeline *timeline,
221 struct intel_gt *gt,
222 struct i915_vma *hwsp)
224 void *vaddr;
226 kref_init(&timeline->kref);
227 atomic_set(&timeline->pin_count, 0);
229 timeline->gt = gt;
231 timeline->has_initial_breadcrumb = !hwsp;
232 timeline->hwsp_cacheline = NULL;
234 if (!hwsp) {
235 struct intel_timeline_cacheline *cl;
236 unsigned int cacheline;
238 hwsp = hwsp_alloc(timeline, &cacheline);
239 if (IS_ERR(hwsp))
240 return PTR_ERR(hwsp);
242 cl = cacheline_alloc(hwsp->private, cacheline);
243 if (IS_ERR(cl)) {
244 __idle_hwsp_free(hwsp->private, cacheline);
245 return PTR_ERR(cl);
248 timeline->hwsp_cacheline = cl;
249 timeline->hwsp_offset = cacheline * CACHELINE_BYTES;
251 vaddr = page_mask_bits(cl->vaddr);
252 } else {
253 timeline->hwsp_offset = I915_GEM_HWS_SEQNO_ADDR;
255 vaddr = i915_gem_object_pin_map(hwsp->obj, I915_MAP_WB);
256 if (IS_ERR(vaddr))
257 return PTR_ERR(vaddr);
260 timeline->hwsp_seqno =
261 memset(vaddr + timeline->hwsp_offset, 0, CACHELINE_BYTES);
263 timeline->hwsp_ggtt = i915_vma_get(hwsp);
264 GEM_BUG_ON(timeline->hwsp_offset >= hwsp->size);
266 timeline->fence_context = dma_fence_context_alloc(1);
268 mutex_init(&timeline->mutex);
270 INIT_ACTIVE_FENCE(&timeline->last_request);
271 INIT_LIST_HEAD(&timeline->requests);
273 i915_syncmap_init(&timeline->sync);
275 return 0;
278 void intel_gt_init_timelines(struct intel_gt *gt)
280 struct intel_gt_timelines *timelines = &gt->timelines;
282 spin_lock_init(&timelines->lock);
283 INIT_LIST_HEAD(&timelines->active_list);
285 spin_lock_init(&timelines->hwsp_lock);
286 INIT_LIST_HEAD(&timelines->hwsp_free_list);
289 static void intel_timeline_fini(struct intel_timeline *timeline)
291 GEM_BUG_ON(atomic_read(&timeline->pin_count));
292 GEM_BUG_ON(!list_empty(&timeline->requests));
293 GEM_BUG_ON(timeline->retire);
295 if (timeline->hwsp_cacheline)
296 cacheline_free(timeline->hwsp_cacheline);
297 else
298 i915_gem_object_unpin_map(timeline->hwsp_ggtt->obj);
300 i915_vma_put(timeline->hwsp_ggtt);
303 struct intel_timeline *
304 intel_timeline_create(struct intel_gt *gt, struct i915_vma *global_hwsp)
306 struct intel_timeline *timeline;
307 int err;
309 timeline = kzalloc(sizeof(*timeline), GFP_KERNEL);
310 if (!timeline)
311 return ERR_PTR(-ENOMEM);
313 err = intel_timeline_init(timeline, gt, global_hwsp);
314 if (err) {
315 kfree(timeline);
316 return ERR_PTR(err);
319 return timeline;
322 int intel_timeline_pin(struct intel_timeline *tl)
324 int err;
326 if (atomic_add_unless(&tl->pin_count, 1, 0))
327 return 0;
329 err = i915_ggtt_pin(tl->hwsp_ggtt, 0, PIN_HIGH);
330 if (err)
331 return err;
333 tl->hwsp_offset =
334 i915_ggtt_offset(tl->hwsp_ggtt) +
335 offset_in_page(tl->hwsp_offset);
336 GT_TRACE(tl->gt, "timeline:%llx using HWSP offset:%x\n",
337 tl->fence_context, tl->hwsp_offset);
339 cacheline_acquire(tl->hwsp_cacheline, tl->hwsp_offset);
340 if (atomic_fetch_inc(&tl->pin_count)) {
341 cacheline_release(tl->hwsp_cacheline);
342 __i915_vma_unpin(tl->hwsp_ggtt);
345 return 0;
348 void intel_timeline_reset_seqno(const struct intel_timeline *tl)
350 /* Must be pinned to be writable, and no requests in flight. */
351 GEM_BUG_ON(!atomic_read(&tl->pin_count));
352 WRITE_ONCE(*(u32 *)tl->hwsp_seqno, tl->seqno);
355 void intel_timeline_enter(struct intel_timeline *tl)
357 struct intel_gt_timelines *timelines = &tl->gt->timelines;
360 * Pretend we are serialised by the timeline->mutex.
362 * While generally true, there are a few exceptions to the rule
363 * for the engine->kernel_context being used to manage power
364 * transitions. As the engine_park may be called from under any
365 * timeline, it uses the power mutex as a global serialisation
366 * lock to prevent any other request entering its timeline.
368 * The rule is generally tl->mutex, otherwise engine->wakeref.mutex.
370 * However, intel_gt_retire_request() does not know which engine
371 * it is retiring along and so cannot partake in the engine-pm
372 * barrier, and there we use the tl->active_count as a means to
373 * pin the timeline in the active_list while the locks are dropped.
374 * Ergo, as that is outside of the engine-pm barrier, we need to
375 * use atomic to manipulate tl->active_count.
377 lockdep_assert_held(&tl->mutex);
379 if (atomic_add_unless(&tl->active_count, 1, 0))
380 return;
382 spin_lock(&timelines->lock);
383 if (!atomic_fetch_inc(&tl->active_count)) {
385 * The HWSP is volatile, and may have been lost while inactive,
386 * e.g. across suspend/resume. Be paranoid, and ensure that
387 * the HWSP value matches our seqno so we don't proclaim
388 * the next request as already complete.
390 intel_timeline_reset_seqno(tl);
391 list_add_tail(&tl->link, &timelines->active_list);
393 spin_unlock(&timelines->lock);
396 void intel_timeline_exit(struct intel_timeline *tl)
398 struct intel_gt_timelines *timelines = &tl->gt->timelines;
400 /* See intel_timeline_enter() */
401 lockdep_assert_held(&tl->mutex);
403 GEM_BUG_ON(!atomic_read(&tl->active_count));
404 if (atomic_add_unless(&tl->active_count, -1, 1))
405 return;
407 spin_lock(&timelines->lock);
408 if (atomic_dec_and_test(&tl->active_count))
409 list_del(&tl->link);
410 spin_unlock(&timelines->lock);
413 * Since this timeline is idle, all bariers upon which we were waiting
414 * must also be complete and so we can discard the last used barriers
415 * without loss of information.
417 i915_syncmap_free(&tl->sync);
420 static u32 timeline_advance(struct intel_timeline *tl)
422 GEM_BUG_ON(!atomic_read(&tl->pin_count));
423 GEM_BUG_ON(tl->seqno & tl->has_initial_breadcrumb);
425 return tl->seqno += 1 + tl->has_initial_breadcrumb;
428 static void timeline_rollback(struct intel_timeline *tl)
430 tl->seqno -= 1 + tl->has_initial_breadcrumb;
433 static noinline int
434 __intel_timeline_get_seqno(struct intel_timeline *tl,
435 struct i915_request *rq,
436 u32 *seqno)
438 struct intel_timeline_cacheline *cl;
439 unsigned int cacheline;
440 struct i915_vma *vma;
441 void *vaddr;
442 int err;
444 might_lock(&tl->gt->ggtt->vm.mutex);
445 GT_TRACE(tl->gt, "timeline:%llx wrapped\n", tl->fence_context);
448 * If there is an outstanding GPU reference to this cacheline,
449 * such as it being sampled by a HW semaphore on another timeline,
450 * we cannot wraparound our seqno value (the HW semaphore does
451 * a strict greater-than-or-equals compare, not i915_seqno_passed).
452 * So if the cacheline is still busy, we must detach ourselves
453 * from it and leave it inflight alongside its users.
455 * However, if nobody is watching and we can guarantee that nobody
456 * will, we could simply reuse the same cacheline.
458 * if (i915_active_request_is_signaled(&tl->last_request) &&
459 * i915_active_is_signaled(&tl->hwsp_cacheline->active))
460 * return 0;
462 * That seems unlikely for a busy timeline that needed to wrap in
463 * the first place, so just replace the cacheline.
466 vma = hwsp_alloc(tl, &cacheline);
467 if (IS_ERR(vma)) {
468 err = PTR_ERR(vma);
469 goto err_rollback;
472 err = i915_ggtt_pin(vma, 0, PIN_HIGH);
473 if (err) {
474 __idle_hwsp_free(vma->private, cacheline);
475 goto err_rollback;
478 cl = cacheline_alloc(vma->private, cacheline);
479 if (IS_ERR(cl)) {
480 err = PTR_ERR(cl);
481 __idle_hwsp_free(vma->private, cacheline);
482 goto err_unpin;
484 GEM_BUG_ON(cl->hwsp->vma != vma);
487 * Attach the old cacheline to the current request, so that we only
488 * free it after the current request is retired, which ensures that
489 * all writes into the cacheline from previous requests are complete.
491 err = i915_active_ref(&tl->hwsp_cacheline->active, tl, &rq->fence);
492 if (err)
493 goto err_cacheline;
495 cacheline_release(tl->hwsp_cacheline); /* ownership now xfered to rq */
496 cacheline_free(tl->hwsp_cacheline);
498 i915_vma_unpin(tl->hwsp_ggtt); /* binding kept alive by old cacheline */
499 i915_vma_put(tl->hwsp_ggtt);
501 tl->hwsp_ggtt = i915_vma_get(vma);
503 vaddr = page_mask_bits(cl->vaddr);
504 tl->hwsp_offset = cacheline * CACHELINE_BYTES;
505 tl->hwsp_seqno =
506 memset(vaddr + tl->hwsp_offset, 0, CACHELINE_BYTES);
508 tl->hwsp_offset += i915_ggtt_offset(vma);
509 GT_TRACE(tl->gt, "timeline:%llx using HWSP offset:%x\n",
510 tl->fence_context, tl->hwsp_offset);
512 cacheline_acquire(cl, tl->hwsp_offset);
513 tl->hwsp_cacheline = cl;
515 *seqno = timeline_advance(tl);
516 GEM_BUG_ON(i915_seqno_passed(*tl->hwsp_seqno, *seqno));
517 return 0;
519 err_cacheline:
520 cacheline_free(cl);
521 err_unpin:
522 i915_vma_unpin(vma);
523 err_rollback:
524 timeline_rollback(tl);
525 return err;
528 int intel_timeline_get_seqno(struct intel_timeline *tl,
529 struct i915_request *rq,
530 u32 *seqno)
532 *seqno = timeline_advance(tl);
534 /* Replace the HWSP on wraparound for HW semaphores */
535 if (unlikely(!*seqno && tl->hwsp_cacheline))
536 return __intel_timeline_get_seqno(tl, rq, seqno);
538 return 0;
541 static int cacheline_ref(struct intel_timeline_cacheline *cl,
542 struct i915_request *rq)
544 return i915_active_add_request(&cl->active, rq);
547 int intel_timeline_read_hwsp(struct i915_request *from,
548 struct i915_request *to,
549 u32 *hwsp)
551 struct intel_timeline_cacheline *cl;
552 int err;
554 GEM_BUG_ON(!rcu_access_pointer(from->hwsp_cacheline));
556 rcu_read_lock();
557 cl = rcu_dereference(from->hwsp_cacheline);
558 if (i915_request_completed(from)) /* confirm cacheline is valid */
559 goto unlock;
560 if (unlikely(!i915_active_acquire_if_busy(&cl->active)))
561 goto unlock; /* seqno wrapped and completed! */
562 if (unlikely(i915_request_completed(from)))
563 goto release;
564 rcu_read_unlock();
566 err = cacheline_ref(cl, to);
567 if (err)
568 goto out;
570 *hwsp = cl->ggtt_offset;
571 out:
572 i915_active_release(&cl->active);
573 return err;
575 release:
576 i915_active_release(&cl->active);
577 unlock:
578 rcu_read_unlock();
579 return 1;
582 void intel_timeline_unpin(struct intel_timeline *tl)
584 GEM_BUG_ON(!atomic_read(&tl->pin_count));
585 if (!atomic_dec_and_test(&tl->pin_count))
586 return;
588 cacheline_release(tl->hwsp_cacheline);
590 __i915_vma_unpin(tl->hwsp_ggtt);
593 void __intel_timeline_free(struct kref *kref)
595 struct intel_timeline *timeline =
596 container_of(kref, typeof(*timeline), kref);
598 intel_timeline_fini(timeline);
599 kfree_rcu(timeline, rcu);
602 void intel_gt_fini_timelines(struct intel_gt *gt)
604 struct intel_gt_timelines *timelines = &gt->timelines;
606 GEM_BUG_ON(!list_empty(&timelines->active_list));
607 GEM_BUG_ON(!list_empty(&timelines->hwsp_free_list));
610 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
611 #include "gt/selftests/mock_timeline.c"
612 #include "gt/selftest_timeline.c"
613 #endif