drm/i915/gt: Push context state allocation earlier
[linux-2.6-microblaze.git] / drivers / gpu / drm / i915 / gt / intel_context.c
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 #include "intel_ring.h"
17
18 static struct i915_global_context {
19         struct i915_global base;
20         struct kmem_cache *slab_ce;
21 } global;
22
23 static struct intel_context *intel_context_alloc(void)
24 {
25         return kmem_cache_zalloc(global.slab_ce, GFP_KERNEL);
26 }
27
28 void intel_context_free(struct intel_context *ce)
29 {
30         kmem_cache_free(global.slab_ce, ce);
31 }
32
33 struct intel_context *
34 intel_context_create(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, engine);
43         return ce;
44 }
45
46 int intel_context_alloc_state(struct intel_context *ce)
47 {
48         int err = 0;
49
50         if (mutex_lock_interruptible(&ce->pin_mutex))
51                 return -EINTR;
52
53         if (!test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) {
54                 err = ce->ops->alloc(ce);
55                 if (unlikely(err))
56                         goto unlock;
57
58                 set_bit(CONTEXT_ALLOC_BIT, &ce->flags);
59         }
60
61 unlock:
62         mutex_unlock(&ce->pin_mutex);
63         return err;
64 }
65
66 int __intel_context_do_pin(struct intel_context *ce)
67 {
68         int err;
69
70         if (unlikely(!test_bit(CONTEXT_ALLOC_BIT, &ce->flags))) {
71                 err = intel_context_alloc_state(ce);
72                 if (err)
73                         return err;
74         }
75
76         if (mutex_lock_interruptible(&ce->pin_mutex))
77                 return -EINTR;
78
79         if (likely(!atomic_read(&ce->pin_count))) {
80                 intel_wakeref_t wakeref;
81
82                 err = 0;
83                 with_intel_runtime_pm(ce->engine->uncore->rpm, wakeref)
84                         err = ce->ops->pin(ce);
85                 if (err)
86                         goto err;
87
88                 CE_TRACE(ce, "pin ring:{head:%04x, tail:%04x}\n",
89                          ce->ring->head, ce->ring->tail);
90
91                 smp_mb__before_atomic(); /* flush pin before it is visible */
92         }
93
94         atomic_inc(&ce->pin_count);
95         GEM_BUG_ON(!intel_context_is_pinned(ce)); /* no overflow! */
96
97         mutex_unlock(&ce->pin_mutex);
98         return 0;
99
100 err:
101         mutex_unlock(&ce->pin_mutex);
102         return err;
103 }
104
105 void intel_context_unpin(struct intel_context *ce)
106 {
107         if (!atomic_dec_and_test(&ce->pin_count))
108                 return;
109
110         CE_TRACE(ce, "unpin\n");
111         ce->ops->unpin(ce);
112
113         /*
114          * Once released, we may asynchronously drop the active reference.
115          * As that may be the only reference keeping the context alive,
116          * take an extra now so that it is not freed before we finish
117          * dereferencing it.
118          */
119         intel_context_get(ce);
120         intel_context_active_release(ce);
121         intel_context_put(ce);
122 }
123
124 static int __context_pin_state(struct i915_vma *vma)
125 {
126         unsigned int bias = i915_ggtt_pin_bias(vma) | PIN_OFFSET_BIAS;
127         int err;
128
129         err = i915_ggtt_pin(vma, 0, bias | PIN_HIGH);
130         if (err)
131                 return err;
132
133         /*
134          * And mark it as a globally pinned object to let the shrinker know
135          * it cannot reclaim the object until we release it.
136          */
137         i915_vma_make_unshrinkable(vma);
138         vma->obj->mm.dirty = true;
139
140         return 0;
141 }
142
143 static void __context_unpin_state(struct i915_vma *vma)
144 {
145         i915_vma_make_shrinkable(vma);
146         __i915_vma_unpin(vma);
147 }
148
149 __i915_active_call
150 static void __intel_context_retire(struct i915_active *active)
151 {
152         struct intel_context *ce = container_of(active, typeof(*ce), active);
153
154         CE_TRACE(ce, "retire\n");
155
156         set_bit(CONTEXT_VALID_BIT, &ce->flags);
157         if (ce->state)
158                 __context_unpin_state(ce->state);
159
160         intel_timeline_unpin(ce->timeline);
161         intel_ring_unpin(ce->ring);
162
163         intel_context_put(ce);
164 }
165
166 static int __intel_context_active(struct i915_active *active)
167 {
168         struct intel_context *ce = container_of(active, typeof(*ce), active);
169         int err;
170
171         CE_TRACE(ce, "active\n");
172
173         intel_context_get(ce);
174
175         err = intel_ring_pin(ce->ring);
176         if (err)
177                 goto err_put;
178
179         err = intel_timeline_pin(ce->timeline);
180         if (err)
181                 goto err_ring;
182
183         if (!ce->state)
184                 return 0;
185
186         err = __context_pin_state(ce->state);
187         if (err)
188                 goto err_timeline;
189
190         return 0;
191
192 err_timeline:
193         intel_timeline_unpin(ce->timeline);
194 err_ring:
195         intel_ring_unpin(ce->ring);
196 err_put:
197         intel_context_put(ce);
198         return err;
199 }
200
201 int intel_context_active_acquire(struct intel_context *ce)
202 {
203         int err;
204
205         err = i915_active_acquire(&ce->active);
206         if (err)
207                 return err;
208
209         /* Preallocate tracking nodes */
210         if (!intel_context_is_barrier(ce)) {
211                 err = i915_active_acquire_preallocate_barrier(&ce->active,
212                                                               ce->engine);
213                 if (err) {
214                         i915_active_release(&ce->active);
215                         return err;
216                 }
217         }
218
219         return 0;
220 }
221
222 void intel_context_active_release(struct intel_context *ce)
223 {
224         /* Nodes preallocated in intel_context_active() */
225         i915_active_acquire_barrier(&ce->active);
226         i915_active_release(&ce->active);
227 }
228
229 void
230 intel_context_init(struct intel_context *ce,
231                    struct intel_engine_cs *engine)
232 {
233         GEM_BUG_ON(!engine->cops);
234         GEM_BUG_ON(!engine->gt->vm);
235
236         kref_init(&ce->ref);
237
238         ce->engine = engine;
239         ce->ops = engine->cops;
240         ce->sseu = engine->sseu;
241         ce->ring = __intel_context_ring_size(SZ_4K);
242
243         ce->vm = i915_vm_get(engine->gt->vm);
244
245         INIT_LIST_HEAD(&ce->signal_link);
246         INIT_LIST_HEAD(&ce->signals);
247
248         mutex_init(&ce->pin_mutex);
249
250         i915_active_init(&ce->active,
251                          __intel_context_active, __intel_context_retire);
252 }
253
254 void intel_context_fini(struct intel_context *ce)
255 {
256         if (ce->timeline)
257                 intel_timeline_put(ce->timeline);
258         i915_vm_put(ce->vm);
259
260         mutex_destroy(&ce->pin_mutex);
261         i915_active_fini(&ce->active);
262 }
263
264 static void i915_global_context_shrink(void)
265 {
266         kmem_cache_shrink(global.slab_ce);
267 }
268
269 static void i915_global_context_exit(void)
270 {
271         kmem_cache_destroy(global.slab_ce);
272 }
273
274 static struct i915_global_context global = { {
275         .shrink = i915_global_context_shrink,
276         .exit = i915_global_context_exit,
277 } };
278
279 int __init i915_global_context_init(void)
280 {
281         global.slab_ce = KMEM_CACHE(intel_context, SLAB_HWCACHE_ALIGN);
282         if (!global.slab_ce)
283                 return -ENOMEM;
284
285         i915_global_register(&global.base);
286         return 0;
287 }
288
289 void intel_context_enter_engine(struct intel_context *ce)
290 {
291         intel_engine_pm_get(ce->engine);
292         intel_timeline_enter(ce->timeline);
293 }
294
295 void intel_context_exit_engine(struct intel_context *ce)
296 {
297         intel_timeline_exit(ce->timeline);
298         intel_engine_pm_put(ce->engine);
299 }
300
301 int intel_context_prepare_remote_request(struct intel_context *ce,
302                                          struct i915_request *rq)
303 {
304         struct intel_timeline *tl = ce->timeline;
305         int err;
306
307         /* Only suitable for use in remotely modifying this context */
308         GEM_BUG_ON(rq->context == ce);
309
310         if (rcu_access_pointer(rq->timeline) != tl) { /* timeline sharing! */
311                 /* Queue this switch after current activity by this context. */
312                 err = i915_active_fence_set(&tl->last_request, rq);
313                 if (err)
314                         return err;
315         }
316
317         /*
318          * Guarantee context image and the timeline remains pinned until the
319          * modifying request is retired by setting the ce activity tracker.
320          *
321          * But we only need to take one pin on the account of it. Or in other
322          * words transfer the pinned ce object to tracked active request.
323          */
324         GEM_BUG_ON(i915_active_is_idle(&ce->active));
325         return i915_active_add_request(&ce->active, rq);
326 }
327
328 struct i915_request *intel_context_create_request(struct intel_context *ce)
329 {
330         struct i915_request *rq;
331         int err;
332
333         err = intel_context_pin(ce);
334         if (unlikely(err))
335                 return ERR_PTR(err);
336
337         rq = i915_request_create(ce);
338         intel_context_unpin(ce);
339
340         return rq;
341 }
342
343 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
344 #include "selftest_context.c"
345 #endif