OSDN Git Service

4d0bc1478ccd4cb848cf936946288c073d933444
[tomoyo/tomoyo-test1.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_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                 if (unlikely(!test_bit(CONTEXT_ALLOC_BIT, &ce->flags))) {
57                         err = ce->ops->alloc(ce);
58                         if (unlikely(err))
59                                 goto err;
60
61                         __set_bit(CONTEXT_ALLOC_BIT, &ce->flags);
62                 }
63
64                 err = 0;
65                 with_intel_runtime_pm(ce->engine->uncore->rpm, wakeref)
66                         err = ce->ops->pin(ce);
67                 if (err)
68                         goto err;
69
70                 CE_TRACE(ce, "pin ring:{head:%04x, tail:%04x}\n",
71                          ce->ring->head, ce->ring->tail);
72
73                 smp_mb__before_atomic(); /* flush pin before it is visible */
74         }
75
76         atomic_inc(&ce->pin_count);
77         GEM_BUG_ON(!intel_context_is_pinned(ce)); /* no overflow! */
78
79         mutex_unlock(&ce->pin_mutex);
80         return 0;
81
82 err:
83         mutex_unlock(&ce->pin_mutex);
84         return err;
85 }
86
87 void intel_context_unpin(struct intel_context *ce)
88 {
89         if (likely(atomic_add_unless(&ce->pin_count, -1, 1)))
90                 return;
91
92         /* We may be called from inside intel_context_pin() to evict another */
93         intel_context_get(ce);
94         mutex_lock_nested(&ce->pin_mutex, SINGLE_DEPTH_NESTING);
95
96         if (likely(atomic_dec_and_test(&ce->pin_count))) {
97                 CE_TRACE(ce, "retire\n");
98
99                 ce->ops->unpin(ce);
100
101                 intel_context_active_release(ce);
102         }
103
104         mutex_unlock(&ce->pin_mutex);
105         intel_context_put(ce);
106 }
107
108 static int __context_pin_state(struct i915_vma *vma)
109 {
110         unsigned int bias = i915_ggtt_pin_bias(vma) | PIN_OFFSET_BIAS;
111         int err;
112
113         err = i915_ggtt_pin(vma, 0, bias | PIN_HIGH);
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         i915_vma_make_unshrinkable(vma);
122         vma->obj->mm.dirty = true;
123
124         return 0;
125 }
126
127 static void __context_unpin_state(struct i915_vma *vma)
128 {
129         i915_vma_make_shrinkable(vma);
130         __i915_vma_unpin(vma);
131 }
132
133 __i915_active_call
134 static void __intel_context_retire(struct i915_active *active)
135 {
136         struct intel_context *ce = container_of(active, typeof(*ce), active);
137
138         CE_TRACE(ce, "retire\n");
139
140         set_bit(CONTEXT_VALID_BIT, &ce->flags);
141         if (ce->state)
142                 __context_unpin_state(ce->state);
143
144         intel_timeline_unpin(ce->timeline);
145         intel_ring_unpin(ce->ring);
146
147         intel_context_put(ce);
148 }
149
150 static int __intel_context_active(struct i915_active *active)
151 {
152         struct intel_context *ce = container_of(active, typeof(*ce), active);
153         int err;
154
155         CE_TRACE(ce, "active\n");
156
157         intel_context_get(ce);
158
159         err = intel_ring_pin(ce->ring);
160         if (err)
161                 goto err_put;
162
163         err = intel_timeline_pin(ce->timeline);
164         if (err)
165                 goto err_ring;
166
167         if (!ce->state)
168                 return 0;
169
170         err = __context_pin_state(ce->state);
171         if (err)
172                 goto err_timeline;
173
174         return 0;
175
176 err_timeline:
177         intel_timeline_unpin(ce->timeline);
178 err_ring:
179         intel_ring_unpin(ce->ring);
180 err_put:
181         intel_context_put(ce);
182         return err;
183 }
184
185 int intel_context_active_acquire(struct intel_context *ce)
186 {
187         int err;
188
189         err = i915_active_acquire(&ce->active);
190         if (err)
191                 return err;
192
193         /* Preallocate tracking nodes */
194         if (!intel_context_is_barrier(ce)) {
195                 err = i915_active_acquire_preallocate_barrier(&ce->active,
196                                                               ce->engine);
197                 if (err) {
198                         i915_active_release(&ce->active);
199                         return err;
200                 }
201         }
202
203         return 0;
204 }
205
206 void intel_context_active_release(struct intel_context *ce)
207 {
208         /* Nodes preallocated in intel_context_active() */
209         i915_active_acquire_barrier(&ce->active);
210         i915_active_release(&ce->active);
211 }
212
213 void
214 intel_context_init(struct intel_context *ce,
215                    struct intel_engine_cs *engine)
216 {
217         GEM_BUG_ON(!engine->cops);
218         GEM_BUG_ON(!engine->gt->vm);
219
220         kref_init(&ce->ref);
221
222         ce->engine = engine;
223         ce->ops = engine->cops;
224         ce->sseu = engine->sseu;
225         ce->ring = __intel_context_ring_size(SZ_4K);
226
227         ce->vm = i915_vm_get(engine->gt->vm);
228
229         INIT_LIST_HEAD(&ce->signal_link);
230         INIT_LIST_HEAD(&ce->signals);
231
232         mutex_init(&ce->pin_mutex);
233
234         i915_active_init(&ce->active,
235                          __intel_context_active, __intel_context_retire);
236 }
237
238 void intel_context_fini(struct intel_context *ce)
239 {
240         if (ce->timeline)
241                 intel_timeline_put(ce->timeline);
242         i915_vm_put(ce->vm);
243
244         mutex_destroy(&ce->pin_mutex);
245         i915_active_fini(&ce->active);
246 }
247
248 static void i915_global_context_shrink(void)
249 {
250         kmem_cache_shrink(global.slab_ce);
251 }
252
253 static void i915_global_context_exit(void)
254 {
255         kmem_cache_destroy(global.slab_ce);
256 }
257
258 static struct i915_global_context global = { {
259         .shrink = i915_global_context_shrink,
260         .exit = i915_global_context_exit,
261 } };
262
263 int __init i915_global_context_init(void)
264 {
265         global.slab_ce = KMEM_CACHE(intel_context, SLAB_HWCACHE_ALIGN);
266         if (!global.slab_ce)
267                 return -ENOMEM;
268
269         i915_global_register(&global.base);
270         return 0;
271 }
272
273 void intel_context_enter_engine(struct intel_context *ce)
274 {
275         intel_engine_pm_get(ce->engine);
276         intel_timeline_enter(ce->timeline);
277 }
278
279 void intel_context_exit_engine(struct intel_context *ce)
280 {
281         intel_timeline_exit(ce->timeline);
282         intel_engine_pm_put(ce->engine);
283 }
284
285 int intel_context_prepare_remote_request(struct intel_context *ce,
286                                          struct i915_request *rq)
287 {
288         struct intel_timeline *tl = ce->timeline;
289         int err;
290
291         /* Only suitable for use in remotely modifying this context */
292         GEM_BUG_ON(rq->context == ce);
293
294         if (rcu_access_pointer(rq->timeline) != tl) { /* timeline sharing! */
295                 /* Queue this switch after current activity by this context. */
296                 err = i915_active_fence_set(&tl->last_request, rq);
297                 if (err)
298                         return err;
299         }
300
301         /*
302          * Guarantee context image and the timeline remains pinned until the
303          * modifying request is retired by setting the ce activity tracker.
304          *
305          * But we only need to take one pin on the account of it. Or in other
306          * words transfer the pinned ce object to tracked active request.
307          */
308         GEM_BUG_ON(i915_active_is_idle(&ce->active));
309         return i915_active_add_request(&ce->active, rq);
310 }
311
312 struct i915_request *intel_context_create_request(struct intel_context *ce)
313 {
314         struct i915_request *rq;
315         int err;
316
317         err = intel_context_pin(ce);
318         if (unlikely(err))
319                 return ERR_PTR(err);
320
321         rq = i915_request_create(ce);
322         intel_context_unpin(ce);
323
324         return rq;
325 }
326
327 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
328 #include "selftest_context.c"
329 #endif