1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2016-2018 Intel Corporation
5  */
6 
7 #include "i915_drv.h"
8 
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"
14 
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))
17 
18 #define CACHELINE_BITS 6
19 #define CACHELINE_FREE CACHELINE_BITS
20 
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;
27 };
28 
29 static struct i915_vma *__hwsp_alloc(struct intel_gt *gt)
30 {
31 	struct drm_i915_private *i915 = gt->i915;
32 	struct drm_i915_gem_object *obj;
33 	struct i915_vma *vma;
34 
35 	obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
36 	if (IS_ERR(obj))
37 		return ERR_CAST(obj);
38 
39 	i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
40 
41 	vma = i915_vma_instance(obj, &gt->ggtt->vm, NULL);
42 	if (IS_ERR(vma))
43 		i915_gem_object_put(obj);
44 
45 	return vma;
46 }
47 
48 static struct i915_vma *
49 hwsp_alloc(struct intel_timeline *timeline, unsigned int *cacheline)
50 {
51 	struct intel_gt_timelines *gt = &timeline->gt->timelines;
52 	struct intel_timeline_hwsp *hwsp;
53 
54 	BUILD_BUG_ON(BITS_PER_TYPE(u64) * CACHELINE_BYTES > PAGE_SIZE);
55 
56 	spin_lock_irq(&gt->hwsp_lock);
57 
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;
63 
64 		spin_unlock_irq(&gt->hwsp_lock);
65 
66 		hwsp = kmalloc(sizeof(*hwsp), GFP_KERNEL);
67 		if (!hwsp)
68 			return ERR_PTR(-ENOMEM);
69 
70 		vma = __hwsp_alloc(timeline->gt);
71 		if (IS_ERR(vma)) {
72 			kfree(hwsp);
73 			return vma;
74 		}
75 
76 		GT_TRACE(timeline->gt, "new HWSP allocated\n");
77 
78 		vma->private = hwsp;
79 		hwsp->gt = timeline->gt;
80 		hwsp->vma = vma;
81 		hwsp->free_bitmap = ~0ull;
82 		hwsp->gt_timelines = gt;
83 
84 		spin_lock_irq(&gt->hwsp_lock);
85 		list_add(&hwsp->free_link, &gt->hwsp_free_list);
86 	}
87 
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);
93 
94 	spin_unlock_irq(&gt->hwsp_lock);
95 
96 	GEM_BUG_ON(hwsp->vma->private != hwsp);
97 	return hwsp->vma;
98 }
99 
100 static void __idle_hwsp_free(struct intel_timeline_hwsp *hwsp, int cacheline)
101 {
102 	struct intel_gt_timelines *gt = hwsp->gt_timelines;
103 	unsigned long flags;
104 
105 	spin_lock_irqsave(&gt->hwsp_lock, flags);
106 
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);
110 
111 	GEM_BUG_ON(cacheline >= BITS_PER_TYPE(hwsp->free_bitmap));
112 	hwsp->free_bitmap |= BIT_ULL(cacheline);
113 
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);
119 	}
120 
121 	spin_unlock_irqrestore(&gt->hwsp_lock, flags);
122 }
123 
124 static void __rcu_cacheline_free(struct rcu_head *rcu)
125 {
126 	struct intel_timeline_cacheline *cl =
127 		container_of(rcu, typeof(*cl), rcu);
128 
129 	i915_active_fini(&cl->active);
130 	kfree(cl);
131 }
132 
133 static void __idle_cacheline_free(struct intel_timeline_cacheline *cl)
134 {
135 	GEM_BUG_ON(!i915_active_is_idle(&cl->active));
136 
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));
140 
141 	call_rcu(&cl->rcu, __rcu_cacheline_free);
142 }
143 
144 __i915_active_call
145 static void __cacheline_retire(struct i915_active *active)
146 {
147 	struct intel_timeline_cacheline *cl =
148 		container_of(active, typeof(*cl), active);
149 
150 	i915_vma_unpin(cl->hwsp->vma);
151 	if (ptr_test_bit(cl->vaddr, CACHELINE_FREE))
152 		__idle_cacheline_free(cl);
153 }
154 
155 static int __cacheline_active(struct i915_active *active)
156 {
157 	struct intel_timeline_cacheline *cl =
158 		container_of(active, typeof(*cl), active);
159 
160 	__i915_vma_pin(cl->hwsp->vma);
161 	return 0;
162 }
163 
164 static struct intel_timeline_cacheline *
165 cacheline_alloc(struct intel_timeline_hwsp *hwsp, unsigned int cacheline)
166 {
167 	struct intel_timeline_cacheline *cl;
168 	void *vaddr;
169 
170 	GEM_BUG_ON(cacheline >= BIT(CACHELINE_BITS));
171 
172 	cl = kmalloc(sizeof(*cl), GFP_KERNEL);
173 	if (!cl)
174 		return ERR_PTR(-ENOMEM);
175 
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);
180 	}
181 
182 	i915_vma_get(hwsp->vma);
183 	cl->hwsp = hwsp;
184 	cl->vaddr = page_pack_bits(vaddr, cacheline);
185 
186 	i915_active_init(&cl->active, __cacheline_active, __cacheline_retire);
187 
188 	return cl;
189 }
190 
191 static void cacheline_acquire(struct intel_timeline_cacheline *cl)
192 {
193 	if (cl)
194 		i915_active_acquire(&cl->active);
195 }
196 
197 static void cacheline_release(struct intel_timeline_cacheline *cl)
198 {
199 	if (cl)
200 		i915_active_release(&cl->active);
201 }
202 
203 static void cacheline_free(struct intel_timeline_cacheline *cl)
204 {
205 	if (!i915_active_acquire_if_busy(&cl->active)) {
206 		__idle_cacheline_free(cl);
207 		return;
208 	}
209 
210 	GEM_BUG_ON(ptr_test_bit(cl->vaddr, CACHELINE_FREE));
211 	cl->vaddr = ptr_set_bit(cl->vaddr, CACHELINE_FREE);
212 
213 	i915_active_release(&cl->active);
214 }
215 
216 static int intel_timeline_init(struct intel_timeline *timeline,
217 			       struct intel_gt *gt,
218 			       struct i915_vma *hwsp,
219 			       unsigned int offset)
220 {
221 	void *vaddr;
222 
223 	kref_init(&timeline->kref);
224 	atomic_set(&timeline->pin_count, 0);
225 
226 	timeline->gt = gt;
227 
228 	timeline->has_initial_breadcrumb = !hwsp;
229 	timeline->hwsp_cacheline = NULL;
230 
231 	if (!hwsp) {
232 		struct intel_timeline_cacheline *cl;
233 		unsigned int cacheline;
234 
235 		hwsp = hwsp_alloc(timeline, &cacheline);
236 		if (IS_ERR(hwsp))
237 			return PTR_ERR(hwsp);
238 
239 		cl = cacheline_alloc(hwsp->private, cacheline);
240 		if (IS_ERR(cl)) {
241 			__idle_hwsp_free(hwsp->private, cacheline);
242 			return PTR_ERR(cl);
243 		}
244 
245 		timeline->hwsp_cacheline = cl;
246 		timeline->hwsp_offset = cacheline * CACHELINE_BYTES;
247 
248 		vaddr = page_mask_bits(cl->vaddr);
249 	} else {
250 		timeline->hwsp_offset = offset;
251 		vaddr = i915_gem_object_pin_map(hwsp->obj, I915_MAP_WB);
252 		if (IS_ERR(vaddr))
253 			return PTR_ERR(vaddr);
254 	}
255 
256 	timeline->hwsp_seqno =
257 		memset(vaddr + timeline->hwsp_offset, 0, CACHELINE_BYTES);
258 
259 	timeline->hwsp_ggtt = i915_vma_get(hwsp);
260 	GEM_BUG_ON(timeline->hwsp_offset >= hwsp->size);
261 
262 	timeline->fence_context = dma_fence_context_alloc(1);
263 
264 	mutex_init(&timeline->mutex);
265 
266 	INIT_ACTIVE_FENCE(&timeline->last_request);
267 	INIT_LIST_HEAD(&timeline->requests);
268 
269 	i915_syncmap_init(&timeline->sync);
270 
271 	return 0;
272 }
273 
274 void intel_gt_init_timelines(struct intel_gt *gt)
275 {
276 	struct intel_gt_timelines *timelines = &gt->timelines;
277 
278 	spin_lock_init(&timelines->lock);
279 	INIT_LIST_HEAD(&timelines->active_list);
280 
281 	spin_lock_init(&timelines->hwsp_lock);
282 	INIT_LIST_HEAD(&timelines->hwsp_free_list);
283 }
284 
285 static void intel_timeline_fini(struct intel_timeline *timeline)
286 {
287 	GEM_BUG_ON(atomic_read(&timeline->pin_count));
288 	GEM_BUG_ON(!list_empty(&timeline->requests));
289 	GEM_BUG_ON(timeline->retire);
290 
291 	if (timeline->hwsp_cacheline)
292 		cacheline_free(timeline->hwsp_cacheline);
293 	else
294 		i915_gem_object_unpin_map(timeline->hwsp_ggtt->obj);
295 
296 	i915_vma_put(timeline->hwsp_ggtt);
297 }
298 
299 struct intel_timeline *
300 __intel_timeline_create(struct intel_gt *gt,
301 			struct i915_vma *global_hwsp,
302 			unsigned int offset)
303 {
304 	struct intel_timeline *timeline;
305 	int err;
306 
307 	timeline = kzalloc(sizeof(*timeline), GFP_KERNEL);
308 	if (!timeline)
309 		return ERR_PTR(-ENOMEM);
310 
311 	err = intel_timeline_init(timeline, gt, global_hwsp, offset);
312 	if (err) {
313 		kfree(timeline);
314 		return ERR_PTR(err);
315 	}
316 
317 	return timeline;
318 }
319 
320 void __intel_timeline_pin(struct intel_timeline *tl)
321 {
322 	GEM_BUG_ON(!atomic_read(&tl->pin_count));
323 	atomic_inc(&tl->pin_count);
324 }
325 
326 int intel_timeline_pin(struct intel_timeline *tl, struct i915_gem_ww_ctx *ww)
327 {
328 	int err;
329 
330 	if (atomic_add_unless(&tl->pin_count, 1, 0))
331 		return 0;
332 
333 	err = i915_ggtt_pin(tl->hwsp_ggtt, ww, 0, PIN_HIGH);
334 	if (err)
335 		return err;
336 
337 	tl->hwsp_offset =
338 		i915_ggtt_offset(tl->hwsp_ggtt) +
339 		offset_in_page(tl->hwsp_offset);
340 	GT_TRACE(tl->gt, "timeline:%llx using HWSP offset:%x\n",
341 		 tl->fence_context, tl->hwsp_offset);
342 
343 	cacheline_acquire(tl->hwsp_cacheline);
344 	if (atomic_fetch_inc(&tl->pin_count)) {
345 		cacheline_release(tl->hwsp_cacheline);
346 		__i915_vma_unpin(tl->hwsp_ggtt);
347 	}
348 
349 	return 0;
350 }
351 
352 void intel_timeline_reset_seqno(const struct intel_timeline *tl)
353 {
354 	/* Must be pinned to be writable, and no requests in flight. */
355 	GEM_BUG_ON(!atomic_read(&tl->pin_count));
356 	WRITE_ONCE(*(u32 *)tl->hwsp_seqno, tl->seqno);
357 }
358 
359 void intel_timeline_enter(struct intel_timeline *tl)
360 {
361 	struct intel_gt_timelines *timelines = &tl->gt->timelines;
362 
363 	/*
364 	 * Pretend we are serialised by the timeline->mutex.
365 	 *
366 	 * While generally true, there are a few exceptions to the rule
367 	 * for the engine->kernel_context being used to manage power
368 	 * transitions. As the engine_park may be called from under any
369 	 * timeline, it uses the power mutex as a global serialisation
370 	 * lock to prevent any other request entering its timeline.
371 	 *
372 	 * The rule is generally tl->mutex, otherwise engine->wakeref.mutex.
373 	 *
374 	 * However, intel_gt_retire_request() does not know which engine
375 	 * it is retiring along and so cannot partake in the engine-pm
376 	 * barrier, and there we use the tl->active_count as a means to
377 	 * pin the timeline in the active_list while the locks are dropped.
378 	 * Ergo, as that is outside of the engine-pm barrier, we need to
379 	 * use atomic to manipulate tl->active_count.
380 	 */
381 	lockdep_assert_held(&tl->mutex);
382 
383 	if (atomic_add_unless(&tl->active_count, 1, 0))
384 		return;
385 
386 	spin_lock(&timelines->lock);
387 	if (!atomic_fetch_inc(&tl->active_count)) {
388 		/*
389 		 * The HWSP is volatile, and may have been lost while inactive,
390 		 * e.g. across suspend/resume. Be paranoid, and ensure that
391 		 * the HWSP value matches our seqno so we don't proclaim
392 		 * the next request as already complete.
393 		 */
394 		intel_timeline_reset_seqno(tl);
395 		list_add_tail(&tl->link, &timelines->active_list);
396 	}
397 	spin_unlock(&timelines->lock);
398 }
399 
400 void intel_timeline_exit(struct intel_timeline *tl)
401 {
402 	struct intel_gt_timelines *timelines = &tl->gt->timelines;
403 
404 	/* See intel_timeline_enter() */
405 	lockdep_assert_held(&tl->mutex);
406 
407 	GEM_BUG_ON(!atomic_read(&tl->active_count));
408 	if (atomic_add_unless(&tl->active_count, -1, 1))
409 		return;
410 
411 	spin_lock(&timelines->lock);
412 	if (atomic_dec_and_test(&tl->active_count))
413 		list_del(&tl->link);
414 	spin_unlock(&timelines->lock);
415 
416 	/*
417 	 * Since this timeline is idle, all bariers upon which we were waiting
418 	 * must also be complete and so we can discard the last used barriers
419 	 * without loss of information.
420 	 */
421 	i915_syncmap_free(&tl->sync);
422 }
423 
424 static u32 timeline_advance(struct intel_timeline *tl)
425 {
426 	GEM_BUG_ON(!atomic_read(&tl->pin_count));
427 	GEM_BUG_ON(tl->seqno & tl->has_initial_breadcrumb);
428 
429 	return tl->seqno += 1 + tl->has_initial_breadcrumb;
430 }
431 
432 static void timeline_rollback(struct intel_timeline *tl)
433 {
434 	tl->seqno -= 1 + tl->has_initial_breadcrumb;
435 }
436 
437 static noinline int
438 __intel_timeline_get_seqno(struct intel_timeline *tl,
439 			   struct i915_request *rq,
440 			   u32 *seqno)
441 {
442 	struct intel_timeline_cacheline *cl;
443 	unsigned int cacheline;
444 	struct i915_vma *vma;
445 	void *vaddr;
446 	int err;
447 
448 	might_lock(&tl->gt->ggtt->vm.mutex);
449 	GT_TRACE(tl->gt, "timeline:%llx wrapped\n", tl->fence_context);
450 
451 	/*
452 	 * If there is an outstanding GPU reference to this cacheline,
453 	 * such as it being sampled by a HW semaphore on another timeline,
454 	 * we cannot wraparound our seqno value (the HW semaphore does
455 	 * a strict greater-than-or-equals compare, not i915_seqno_passed).
456 	 * So if the cacheline is still busy, we must detach ourselves
457 	 * from it and leave it inflight alongside its users.
458 	 *
459 	 * However, if nobody is watching and we can guarantee that nobody
460 	 * will, we could simply reuse the same cacheline.
461 	 *
462 	 * if (i915_active_request_is_signaled(&tl->last_request) &&
463 	 *     i915_active_is_signaled(&tl->hwsp_cacheline->active))
464 	 *	return 0;
465 	 *
466 	 * That seems unlikely for a busy timeline that needed to wrap in
467 	 * the first place, so just replace the cacheline.
468 	 */
469 
470 	vma = hwsp_alloc(tl, &cacheline);
471 	if (IS_ERR(vma)) {
472 		err = PTR_ERR(vma);
473 		goto err_rollback;
474 	}
475 
476 	err = i915_ggtt_pin(vma, NULL, 0, PIN_HIGH);
477 	if (err) {
478 		__idle_hwsp_free(vma->private, cacheline);
479 		goto err_rollback;
480 	}
481 
482 	cl = cacheline_alloc(vma->private, cacheline);
483 	if (IS_ERR(cl)) {
484 		err = PTR_ERR(cl);
485 		__idle_hwsp_free(vma->private, cacheline);
486 		goto err_unpin;
487 	}
488 	GEM_BUG_ON(cl->hwsp->vma != vma);
489 
490 	/*
491 	 * Attach the old cacheline to the current request, so that we only
492 	 * free it after the current request is retired, which ensures that
493 	 * all writes into the cacheline from previous requests are complete.
494 	 */
495 	err = i915_active_ref(&tl->hwsp_cacheline->active,
496 			      tl->fence_context,
497 			      &rq->fence);
498 	if (err)
499 		goto err_cacheline;
500 
501 	cacheline_release(tl->hwsp_cacheline); /* ownership now xfered to rq */
502 	cacheline_free(tl->hwsp_cacheline);
503 
504 	i915_vma_unpin(tl->hwsp_ggtt); /* binding kept alive by old cacheline */
505 	i915_vma_put(tl->hwsp_ggtt);
506 
507 	tl->hwsp_ggtt = i915_vma_get(vma);
508 
509 	vaddr = page_mask_bits(cl->vaddr);
510 	tl->hwsp_offset = cacheline * CACHELINE_BYTES;
511 	tl->hwsp_seqno =
512 		memset(vaddr + tl->hwsp_offset, 0, CACHELINE_BYTES);
513 
514 	tl->hwsp_offset += i915_ggtt_offset(vma);
515 	GT_TRACE(tl->gt, "timeline:%llx using HWSP offset:%x\n",
516 		 tl->fence_context, tl->hwsp_offset);
517 
518 	cacheline_acquire(cl);
519 	tl->hwsp_cacheline = cl;
520 
521 	*seqno = timeline_advance(tl);
522 	GEM_BUG_ON(i915_seqno_passed(*tl->hwsp_seqno, *seqno));
523 	return 0;
524 
525 err_cacheline:
526 	cacheline_free(cl);
527 err_unpin:
528 	i915_vma_unpin(vma);
529 err_rollback:
530 	timeline_rollback(tl);
531 	return err;
532 }
533 
534 int intel_timeline_get_seqno(struct intel_timeline *tl,
535 			     struct i915_request *rq,
536 			     u32 *seqno)
537 {
538 	*seqno = timeline_advance(tl);
539 
540 	/* Replace the HWSP on wraparound for HW semaphores */
541 	if (unlikely(!*seqno && tl->hwsp_cacheline))
542 		return __intel_timeline_get_seqno(tl, rq, seqno);
543 
544 	return 0;
545 }
546 
547 static int cacheline_ref(struct intel_timeline_cacheline *cl,
548 			 struct i915_request *rq)
549 {
550 	return i915_active_add_request(&cl->active, rq);
551 }
552 
553 int intel_timeline_read_hwsp(struct i915_request *from,
554 			     struct i915_request *to,
555 			     u32 *hwsp)
556 {
557 	struct intel_timeline_cacheline *cl;
558 	int err;
559 
560 	GEM_BUG_ON(!rcu_access_pointer(from->hwsp_cacheline));
561 
562 	rcu_read_lock();
563 	cl = rcu_dereference(from->hwsp_cacheline);
564 	if (i915_request_completed(from)) /* confirm cacheline is valid */
565 		goto unlock;
566 	if (unlikely(!i915_active_acquire_if_busy(&cl->active)))
567 		goto unlock; /* seqno wrapped and completed! */
568 	if (unlikely(i915_request_completed(from)))
569 		goto release;
570 	rcu_read_unlock();
571 
572 	err = cacheline_ref(cl, to);
573 	if (err)
574 		goto out;
575 
576 	*hwsp = i915_ggtt_offset(cl->hwsp->vma) +
577 		ptr_unmask_bits(cl->vaddr, CACHELINE_BITS) * CACHELINE_BYTES;
578 
579 out:
580 	i915_active_release(&cl->active);
581 	return err;
582 
583 release:
584 	i915_active_release(&cl->active);
585 unlock:
586 	rcu_read_unlock();
587 	return 1;
588 }
589 
590 void intel_timeline_unpin(struct intel_timeline *tl)
591 {
592 	GEM_BUG_ON(!atomic_read(&tl->pin_count));
593 	if (!atomic_dec_and_test(&tl->pin_count))
594 		return;
595 
596 	cacheline_release(tl->hwsp_cacheline);
597 
598 	__i915_vma_unpin(tl->hwsp_ggtt);
599 }
600 
601 void __intel_timeline_free(struct kref *kref)
602 {
603 	struct intel_timeline *timeline =
604 		container_of(kref, typeof(*timeline), kref);
605 
606 	intel_timeline_fini(timeline);
607 	kfree_rcu(timeline, rcu);
608 }
609 
610 void intel_gt_fini_timelines(struct intel_gt *gt)
611 {
612 	struct intel_gt_timelines *timelines = &gt->timelines;
613 
614 	GEM_BUG_ON(!list_empty(&timelines->active_list));
615 	GEM_BUG_ON(!list_empty(&timelines->hwsp_free_list));
616 }
617 
618 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
619 #include "gt/selftests/mock_timeline.c"
620 #include "gt/selftest_timeline.c"
621 #endif
622