1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2019 Intel Corporation
5  */
6 
7 #include "gem/i915_gem_context.h"
8 #include "gem/i915_gem_pm.h"
9 
10 #include "i915_drv.h"
11 #include "i915_globals.h"
12 
13 #include "intel_context.h"
14 #include "intel_engine.h"
15 #include "intel_engine_pm.h"
16 
17 static struct i915_global_context {
18 	struct i915_global base;
19 	struct kmem_cache *slab_ce;
20 } global;
21 
22 static struct intel_context *intel_context_alloc(void)
23 {
24 	return kmem_cache_zalloc(global.slab_ce, GFP_KERNEL);
25 }
26 
27 void intel_context_free(struct intel_context *ce)
28 {
29 	kmem_cache_free(global.slab_ce, ce);
30 }
31 
32 struct intel_context *
33 intel_context_create(struct i915_gem_context *ctx,
34 		     struct intel_engine_cs *engine)
35 {
36 	struct intel_context *ce;
37 
38 	ce = intel_context_alloc();
39 	if (!ce)
40 		return ERR_PTR(-ENOMEM);
41 
42 	intel_context_init(ce, ctx, engine);
43 	return ce;
44 }
45 
46 int __intel_context_do_pin(struct intel_context *ce)
47 {
48 	int err;
49 
50 	if (mutex_lock_interruptible(&ce->pin_mutex))
51 		return -EINTR;
52 
53 	if (likely(!atomic_read(&ce->pin_count))) {
54 		intel_wakeref_t wakeref;
55 
56 		err = 0;
57 		with_intel_runtime_pm(&ce->engine->i915->runtime_pm, wakeref)
58 			err = ce->ops->pin(ce);
59 		if (err)
60 			goto err;
61 
62 		GEM_TRACE("%s context:%llx pin ring:{head:%04x, tail:%04x}\n",
63 			  ce->engine->name, ce->ring->timeline->fence_context,
64 			  ce->ring->head, ce->ring->tail);
65 
66 		i915_gem_context_get(ce->gem_context); /* for ctx->ppgtt */
67 
68 		smp_mb__before_atomic(); /* flush pin before it is visible */
69 	}
70 
71 	atomic_inc(&ce->pin_count);
72 	GEM_BUG_ON(!intel_context_is_pinned(ce)); /* no overflow! */
73 
74 	mutex_unlock(&ce->pin_mutex);
75 	return 0;
76 
77 err:
78 	mutex_unlock(&ce->pin_mutex);
79 	return err;
80 }
81 
82 void intel_context_unpin(struct intel_context *ce)
83 {
84 	if (likely(atomic_add_unless(&ce->pin_count, -1, 1)))
85 		return;
86 
87 	/* We may be called from inside intel_context_pin() to evict another */
88 	intel_context_get(ce);
89 	mutex_lock_nested(&ce->pin_mutex, SINGLE_DEPTH_NESTING);
90 
91 	if (likely(atomic_dec_and_test(&ce->pin_count))) {
92 		GEM_TRACE("%s context:%llx retire\n",
93 			  ce->engine->name, ce->ring->timeline->fence_context);
94 
95 		ce->ops->unpin(ce);
96 
97 		i915_gem_context_put(ce->gem_context);
98 		intel_context_active_release(ce);
99 	}
100 
101 	mutex_unlock(&ce->pin_mutex);
102 	intel_context_put(ce);
103 }
104 
105 static int __context_pin_state(struct i915_vma *vma)
106 {
107 	u64 flags;
108 	int err;
109 
110 	flags = i915_ggtt_pin_bias(vma) | PIN_OFFSET_BIAS;
111 	flags |= PIN_HIGH | PIN_GLOBAL;
112 
113 	err = i915_vma_pin(vma, 0, 0, flags);
114 	if (err)
115 		return err;
116 
117 	/*
118 	 * And mark it as a globally pinned object to let the shrinker know
119 	 * it cannot reclaim the object until we release it.
120 	 */
121 	vma->obj->pin_global++;
122 	vma->obj->mm.dirty = true;
123 
124 	return 0;
125 }
126 
127 static void __context_unpin_state(struct i915_vma *vma)
128 {
129 	vma->obj->pin_global--;
130 	__i915_vma_unpin(vma);
131 }
132 
133 static void __intel_context_retire(struct i915_active *active)
134 {
135 	struct intel_context *ce = container_of(active, typeof(*ce), active);
136 
137 	GEM_TRACE("%s context:%llx retire\n",
138 		  ce->engine->name, ce->ring->timeline->fence_context);
139 
140 	if (ce->state)
141 		__context_unpin_state(ce->state);
142 
143 	intel_ring_unpin(ce->ring);
144 	intel_context_put(ce);
145 }
146 
147 static int __intel_context_active(struct i915_active *active)
148 {
149 	struct intel_context *ce = container_of(active, typeof(*ce), active);
150 	int err;
151 
152 	intel_context_get(ce);
153 
154 	err = intel_ring_pin(ce->ring);
155 	if (err)
156 		goto err_put;
157 
158 	if (!ce->state)
159 		return 0;
160 
161 	err = __context_pin_state(ce->state);
162 	if (err)
163 		goto err_ring;
164 
165 	/* Preallocate tracking nodes */
166 	if (!i915_gem_context_is_kernel(ce->gem_context)) {
167 		err = i915_active_acquire_preallocate_barrier(&ce->active,
168 							      ce->engine);
169 		if (err)
170 			goto err_state;
171 	}
172 
173 	return 0;
174 
175 err_state:
176 	__context_unpin_state(ce->state);
177 err_ring:
178 	intel_ring_unpin(ce->ring);
179 err_put:
180 	intel_context_put(ce);
181 	return err;
182 }
183 
184 void
185 intel_context_init(struct intel_context *ce,
186 		   struct i915_gem_context *ctx,
187 		   struct intel_engine_cs *engine)
188 {
189 	GEM_BUG_ON(!engine->cops);
190 
191 	kref_init(&ce->ref);
192 
193 	ce->gem_context = ctx;
194 	ce->vm = i915_vm_get(ctx->vm ?: &engine->gt->ggtt->vm);
195 
196 	ce->engine = engine;
197 	ce->ops = engine->cops;
198 	ce->sseu = engine->sseu;
199 
200 	INIT_LIST_HEAD(&ce->signal_link);
201 	INIT_LIST_HEAD(&ce->signals);
202 
203 	mutex_init(&ce->pin_mutex);
204 
205 	i915_active_init(ctx->i915, &ce->active,
206 			 __intel_context_active, __intel_context_retire);
207 }
208 
209 void intel_context_fini(struct intel_context *ce)
210 {
211 	i915_vm_put(ce->vm);
212 
213 	mutex_destroy(&ce->pin_mutex);
214 	i915_active_fini(&ce->active);
215 }
216 
217 static void i915_global_context_shrink(void)
218 {
219 	kmem_cache_shrink(global.slab_ce);
220 }
221 
222 static void i915_global_context_exit(void)
223 {
224 	kmem_cache_destroy(global.slab_ce);
225 }
226 
227 static struct i915_global_context global = { {
228 	.shrink = i915_global_context_shrink,
229 	.exit = i915_global_context_exit,
230 } };
231 
232 int __init i915_global_context_init(void)
233 {
234 	global.slab_ce = KMEM_CACHE(intel_context, SLAB_HWCACHE_ALIGN);
235 	if (!global.slab_ce)
236 		return -ENOMEM;
237 
238 	i915_global_register(&global.base);
239 	return 0;
240 }
241 
242 void intel_context_enter_engine(struct intel_context *ce)
243 {
244 	intel_engine_pm_get(ce->engine);
245 }
246 
247 void intel_context_exit_engine(struct intel_context *ce)
248 {
249 	intel_engine_pm_put(ce->engine);
250 }
251 
252 int intel_context_prepare_remote_request(struct intel_context *ce,
253 					 struct i915_request *rq)
254 {
255 	struct intel_timeline *tl = ce->ring->timeline;
256 	int err;
257 
258 	/* Only suitable for use in remotely modifying this context */
259 	GEM_BUG_ON(rq->hw_context == ce);
260 
261 	if (rq->timeline != tl) { /* beware timeline sharing */
262 		err = mutex_lock_interruptible_nested(&tl->mutex,
263 						      SINGLE_DEPTH_NESTING);
264 		if (err)
265 			return err;
266 
267 		/* Queue this switch after current activity by this context. */
268 		err = i915_active_request_set(&tl->last_request, rq);
269 		if (err)
270 			goto unlock;
271 	}
272 	lockdep_assert_held(&tl->mutex);
273 
274 	/*
275 	 * Guarantee context image and the timeline remains pinned until the
276 	 * modifying request is retired by setting the ce activity tracker.
277 	 *
278 	 * But we only need to take one pin on the account of it. Or in other
279 	 * words transfer the pinned ce object to tracked active request.
280 	 */
281 	GEM_BUG_ON(i915_active_is_idle(&ce->active));
282 	err = i915_active_ref(&ce->active, rq->fence.context, rq);
283 
284 unlock:
285 	if (rq->timeline != tl)
286 		mutex_unlock(&tl->mutex);
287 	return err;
288 }
289 
290 struct i915_request *intel_context_create_request(struct intel_context *ce)
291 {
292 	struct i915_request *rq;
293 	int err;
294 
295 	err = intel_context_pin(ce);
296 	if (unlikely(err))
297 		return ERR_PTR(err);
298 
299 	rq = i915_request_create(ce);
300 	intel_context_unpin(ce);
301 
302 	return rq;
303 }
304