]>
Commit | Line | Data |
---|---|---|
254f965c BW |
1 | /* |
2 | * Copyright © 2011-2012 Intel Corporation | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
5 | * copy of this software and associated documentation files (the "Software"), | |
6 | * to deal in the Software without restriction, including without limitation | |
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
8 | * and/or sell copies of the Software, and to permit persons to whom the | |
9 | * Software is furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice (including the next | |
12 | * paragraph) shall be included in all copies or substantial portions of the | |
13 | * Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 | * IN THE SOFTWARE. | |
22 | * | |
23 | * Authors: | |
24 | * Ben Widawsky <ben@bwidawsk.net> | |
25 | * | |
26 | */ | |
27 | ||
28 | /* | |
29 | * This file implements HW context support. On gen5+ a HW context consists of an | |
30 | * opaque GPU object which is referenced at times of context saves and restores. | |
31 | * With RC6 enabled, the context is also referenced as the GPU enters and exists | |
32 | * from RC6 (GPU has it's own internal power context, except on gen5). Though | |
33 | * something like a context does exist for the media ring, the code only | |
34 | * supports contexts for the render ring. | |
35 | * | |
36 | * In software, there is a distinction between contexts created by the user, | |
37 | * and the default HW context. The default HW context is used by GPU clients | |
38 | * that do not request setup of their own hardware context. The default | |
39 | * context's state is never restored to help prevent programming errors. This | |
40 | * would happen if a client ran and piggy-backed off another clients GPU state. | |
41 | * The default context only exists to give the GPU some offset to load as the | |
42 | * current to invoke a save of the context we actually care about. In fact, the | |
43 | * code could likely be constructed, albeit in a more complicated fashion, to | |
44 | * never use the default context, though that limits the driver's ability to | |
45 | * swap out, and/or destroy other contexts. | |
46 | * | |
47 | * All other contexts are created as a request by the GPU client. These contexts | |
48 | * store GPU state, and thus allow GPU clients to not re-emit state (and | |
49 | * potentially query certain state) at any time. The kernel driver makes | |
50 | * certain that the appropriate commands are inserted. | |
51 | * | |
52 | * The context life cycle is semi-complicated in that context BOs may live | |
53 | * longer than the context itself because of the way the hardware, and object | |
54 | * tracking works. Below is a very crude representation of the state machine | |
55 | * describing the context life. | |
56 | * refcount pincount active | |
57 | * S0: initial state 0 0 0 | |
58 | * S1: context created 1 0 0 | |
59 | * S2: context is currently running 2 1 X | |
60 | * S3: GPU referenced, but not current 2 0 1 | |
61 | * S4: context is current, but destroyed 1 1 0 | |
62 | * S5: like S3, but destroyed 1 0 1 | |
63 | * | |
64 | * The most common (but not all) transitions: | |
65 | * S0->S1: client creates a context | |
66 | * S1->S2: client submits execbuf with context | |
67 | * S2->S3: other clients submits execbuf with context | |
68 | * S3->S1: context object was retired | |
69 | * S3->S2: clients submits another execbuf | |
70 | * S2->S4: context destroy called with current context | |
71 | * S3->S5->S0: destroy path | |
72 | * S4->S5->S0: destroy path on current context | |
73 | * | |
74 | * There are two confusing terms used above: | |
75 | * The "current context" means the context which is currently running on the | |
508842a0 | 76 | * GPU. The GPU has loaded its state already and has stored away the gtt |
254f965c BW |
77 | * offset of the BO. The GPU is not actively referencing the data at this |
78 | * offset, but it will on the next context switch. The only way to avoid this | |
79 | * is to do a GPU reset. | |
80 | * | |
81 | * An "active context' is one which was previously the "current context" and is | |
82 | * on the active list waiting for the next context switch to occur. Until this | |
83 | * happens, the object must remain at the same gtt offset. It is therefore | |
84 | * possible to destroy a context, but it is still active. | |
85 | * | |
86 | */ | |
87 | ||
760285e7 DH |
88 | #include <drm/drmP.h> |
89 | #include <drm/i915_drm.h> | |
254f965c BW |
90 | #include "i915_drv.h" |
91 | ||
40521054 BW |
92 | /* This is a HW constraint. The value below is the largest known requirement |
93 | * I've seen in a spec to date, and that was a workaround for a non-shipping | |
94 | * part. It should be safe to decrease this, but it's more future proof as is. | |
95 | */ | |
b731d33d BW |
96 | #define GEN6_CONTEXT_ALIGN (64<<10) |
97 | #define GEN7_CONTEXT_ALIGN 4096 | |
40521054 | 98 | |
b18b6bde | 99 | static void do_ppgtt_cleanup(struct i915_hw_ppgtt *ppgtt) |
321f2ada | 100 | { |
321f2ada BW |
101 | struct drm_device *dev = ppgtt->base.dev; |
102 | struct drm_i915_private *dev_priv = dev->dev_private; | |
103 | struct i915_address_space *vm = &ppgtt->base; | |
104 | ||
105 | if (ppgtt == dev_priv->mm.aliasing_ppgtt || | |
106 | (list_empty(&vm->active_list) && list_empty(&vm->inactive_list))) { | |
107 | ppgtt->base.cleanup(&ppgtt->base); | |
108 | return; | |
109 | } | |
110 | ||
111 | /* | |
112 | * Make sure vmas are unbound before we take down the drm_mm | |
113 | * | |
114 | * FIXME: Proper refcounting should take care of this, this shouldn't be | |
115 | * needed at all. | |
116 | */ | |
117 | if (!list_empty(&vm->active_list)) { | |
118 | struct i915_vma *vma; | |
119 | ||
120 | list_for_each_entry(vma, &vm->active_list, mm_list) | |
121 | if (WARN_ON(list_empty(&vma->vma_link) || | |
122 | list_is_singular(&vma->vma_link))) | |
123 | break; | |
124 | ||
125 | i915_gem_evict_vm(&ppgtt->base, true); | |
126 | } else { | |
127 | i915_gem_retire_requests(dev); | |
128 | i915_gem_evict_vm(&ppgtt->base, false); | |
129 | } | |
130 | ||
131 | ppgtt->base.cleanup(&ppgtt->base); | |
132 | } | |
133 | ||
b18b6bde BW |
134 | static void ppgtt_release(struct kref *kref) |
135 | { | |
136 | struct i915_hw_ppgtt *ppgtt = | |
137 | container_of(kref, struct i915_hw_ppgtt, ref); | |
138 | ||
139 | do_ppgtt_cleanup(ppgtt); | |
140 | kfree(ppgtt); | |
141 | } | |
142 | ||
b731d33d BW |
143 | static size_t get_context_alignment(struct drm_device *dev) |
144 | { | |
145 | if (IS_GEN6(dev)) | |
146 | return GEN6_CONTEXT_ALIGN; | |
147 | ||
148 | return GEN7_CONTEXT_ALIGN; | |
149 | } | |
150 | ||
254f965c BW |
151 | static int get_context_size(struct drm_device *dev) |
152 | { | |
153 | struct drm_i915_private *dev_priv = dev->dev_private; | |
154 | int ret; | |
155 | u32 reg; | |
156 | ||
157 | switch (INTEL_INFO(dev)->gen) { | |
158 | case 6: | |
159 | reg = I915_READ(CXT_SIZE); | |
160 | ret = GEN6_CXT_TOTAL_SIZE(reg) * 64; | |
161 | break; | |
162 | case 7: | |
4f91dd6f | 163 | reg = I915_READ(GEN7_CXT_SIZE); |
2e4291e0 | 164 | if (IS_HASWELL(dev)) |
a0de80a0 | 165 | ret = HSW_CXT_TOTAL_SIZE; |
2e4291e0 BW |
166 | else |
167 | ret = GEN7_CXT_TOTAL_SIZE(reg) * 64; | |
254f965c | 168 | break; |
8897644a BW |
169 | case 8: |
170 | ret = GEN8_CXT_TOTAL_SIZE; | |
171 | break; | |
254f965c BW |
172 | default: |
173 | BUG(); | |
174 | } | |
175 | ||
176 | return ret; | |
177 | } | |
178 | ||
dce3271b | 179 | void i915_gem_context_free(struct kref *ctx_ref) |
40521054 | 180 | { |
273497e5 | 181 | struct intel_context *ctx = container_of(ctx_ref, |
dce3271b | 182 | typeof(*ctx), ref); |
c7c48dfd | 183 | struct i915_hw_ppgtt *ppgtt = NULL; |
40521054 | 184 | |
691e6415 CW |
185 | if (ctx->obj) { |
186 | /* We refcount even the aliasing PPGTT to keep the code symmetric */ | |
187 | if (USES_PPGTT(ctx->obj->base.dev)) | |
188 | ppgtt = ctx_to_ppgtt(ctx); | |
c7c48dfd | 189 | |
691e6415 CW |
190 | /* XXX: Free up the object before tearing down the address space, in |
191 | * case we're bound in the PPGTT */ | |
192 | drm_gem_object_unreference(&ctx->obj->base); | |
193 | } | |
c7c48dfd BW |
194 | |
195 | if (ppgtt) | |
196 | kref_put(&ppgtt->ref, ppgtt_release); | |
197 | list_del(&ctx->link); | |
40521054 BW |
198 | kfree(ctx); |
199 | } | |
200 | ||
aa0c13da OM |
201 | static struct drm_i915_gem_object * |
202 | i915_gem_alloc_context_obj(struct drm_device *dev, size_t size) | |
203 | { | |
204 | struct drm_i915_gem_object *obj; | |
205 | int ret; | |
206 | ||
207 | obj = i915_gem_alloc_object(dev, size); | |
208 | if (obj == NULL) | |
209 | return ERR_PTR(-ENOMEM); | |
210 | ||
211 | /* | |
212 | * Try to make the context utilize L3 as well as LLC. | |
213 | * | |
214 | * On VLV we don't have L3 controls in the PTEs so we | |
215 | * shouldn't touch the cache level, especially as that | |
216 | * would make the object snooped which might have a | |
217 | * negative performance impact. | |
218 | */ | |
219 | if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev)) { | |
220 | ret = i915_gem_object_set_cache_level(obj, I915_CACHE_L3_LLC); | |
221 | /* Failure shouldn't ever happen this early */ | |
222 | if (WARN_ON(ret)) { | |
223 | drm_gem_object_unreference(&obj->base); | |
224 | return ERR_PTR(ret); | |
225 | } | |
226 | } | |
227 | ||
228 | return obj; | |
229 | } | |
230 | ||
bdf4fd7e | 231 | static struct i915_hw_ppgtt * |
273497e5 | 232 | create_vm_for_ctx(struct drm_device *dev, struct intel_context *ctx) |
bdf4fd7e BW |
233 | { |
234 | struct i915_hw_ppgtt *ppgtt; | |
235 | int ret; | |
236 | ||
237 | ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); | |
238 | if (!ppgtt) | |
239 | return ERR_PTR(-ENOMEM); | |
240 | ||
241 | ret = i915_gem_init_ppgtt(dev, ppgtt); | |
242 | if (ret) { | |
243 | kfree(ppgtt); | |
244 | return ERR_PTR(ret); | |
245 | } | |
246 | ||
6313c204 | 247 | ppgtt->ctx = ctx; |
bdf4fd7e BW |
248 | return ppgtt; |
249 | } | |
250 | ||
273497e5 | 251 | static struct intel_context * |
0eea67eb | 252 | __create_hw_context(struct drm_device *dev, |
146937e5 | 253 | struct drm_i915_file_private *file_priv) |
40521054 BW |
254 | { |
255 | struct drm_i915_private *dev_priv = dev->dev_private; | |
273497e5 | 256 | struct intel_context *ctx; |
c8c470af | 257 | int ret; |
40521054 | 258 | |
f94982b0 | 259 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
146937e5 BW |
260 | if (ctx == NULL) |
261 | return ERR_PTR(-ENOMEM); | |
40521054 | 262 | |
dce3271b | 263 | kref_init(&ctx->ref); |
691e6415 | 264 | list_add_tail(&ctx->link, &dev_priv->context_list); |
40521054 | 265 | |
691e6415 | 266 | if (dev_priv->hw_context_size) { |
aa0c13da OM |
267 | struct drm_i915_gem_object *obj = |
268 | i915_gem_alloc_context_obj(dev, dev_priv->hw_context_size); | |
269 | if (IS_ERR(obj)) { | |
270 | ret = PTR_ERR(obj); | |
4615d4c9 | 271 | goto err_out; |
691e6415 | 272 | } |
aa0c13da | 273 | ctx->obj = obj; |
691e6415 | 274 | } |
40521054 BW |
275 | |
276 | /* Default context will never have a file_priv */ | |
691e6415 CW |
277 | if (file_priv != NULL) { |
278 | ret = idr_alloc(&file_priv->context_idr, ctx, | |
279 | DEFAULT_CONTEXT_ID, 0, GFP_KERNEL); | |
280 | if (ret < 0) | |
281 | goto err_out; | |
282 | } else | |
283 | ret = DEFAULT_CONTEXT_ID; | |
dce3271b MK |
284 | |
285 | ctx->file_priv = file_priv; | |
c8c470af | 286 | ctx->id = ret; |
3ccfd19d BW |
287 | /* NB: Mark all slices as needing a remap so that when the context first |
288 | * loads it will restore whatever remap state already exists. If there | |
289 | * is no remap info, it will be a NOP. */ | |
290 | ctx->remap_slice = (1 << NUM_L3_SLICES(dev)) - 1; | |
40521054 | 291 | |
146937e5 | 292 | return ctx; |
40521054 BW |
293 | |
294 | err_out: | |
dce3271b | 295 | i915_gem_context_unreference(ctx); |
146937e5 | 296 | return ERR_PTR(ret); |
40521054 BW |
297 | } |
298 | ||
254f965c BW |
299 | /** |
300 | * The default context needs to exist per ring that uses contexts. It stores the | |
301 | * context state of the GPU for applications that don't utilize HW contexts, as | |
302 | * well as an idle case. | |
303 | */ | |
273497e5 | 304 | static struct intel_context * |
0eea67eb BW |
305 | i915_gem_create_context(struct drm_device *dev, |
306 | struct drm_i915_file_private *file_priv, | |
307 | bool create_vm) | |
254f965c | 308 | { |
42c3b603 | 309 | const bool is_global_default_ctx = file_priv == NULL; |
bdf4fd7e | 310 | struct drm_i915_private *dev_priv = dev->dev_private; |
273497e5 | 311 | struct intel_context *ctx; |
bdf4fd7e | 312 | int ret = 0; |
40521054 | 313 | |
b731d33d | 314 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
40521054 | 315 | |
0eea67eb | 316 | ctx = __create_hw_context(dev, file_priv); |
146937e5 | 317 | if (IS_ERR(ctx)) |
a45d0f6a | 318 | return ctx; |
40521054 | 319 | |
691e6415 | 320 | if (is_global_default_ctx && ctx->obj) { |
42c3b603 CW |
321 | /* We may need to do things with the shrinker which |
322 | * require us to immediately switch back to the default | |
323 | * context. This can cause a problem as pinning the | |
324 | * default context also requires GTT space which may not | |
325 | * be available. To avoid this we always pin the default | |
326 | * context. | |
327 | */ | |
328 | ret = i915_gem_obj_ggtt_pin(ctx->obj, | |
1ec9e26d | 329 | get_context_alignment(dev), 0); |
42c3b603 CW |
330 | if (ret) { |
331 | DRM_DEBUG_DRIVER("Couldn't pin %d\n", ret); | |
332 | goto err_destroy; | |
333 | } | |
334 | } | |
335 | ||
bdf4fd7e BW |
336 | if (create_vm) { |
337 | struct i915_hw_ppgtt *ppgtt = create_vm_for_ctx(dev, ctx); | |
338 | ||
339 | if (IS_ERR_OR_NULL(ppgtt)) { | |
0eea67eb BW |
340 | DRM_DEBUG_DRIVER("PPGTT setup failed (%ld)\n", |
341 | PTR_ERR(ppgtt)); | |
bdf4fd7e | 342 | ret = PTR_ERR(ppgtt); |
42c3b603 | 343 | goto err_unpin; |
bdf4fd7e BW |
344 | } else |
345 | ctx->vm = &ppgtt->base; | |
346 | ||
347 | /* This case is reserved for the global default context and | |
348 | * should only happen once. */ | |
42c3b603 | 349 | if (is_global_default_ctx) { |
bdf4fd7e BW |
350 | if (WARN_ON(dev_priv->mm.aliasing_ppgtt)) { |
351 | ret = -EEXIST; | |
42c3b603 | 352 | goto err_unpin; |
bdf4fd7e BW |
353 | } |
354 | ||
355 | dev_priv->mm.aliasing_ppgtt = ppgtt; | |
bdf4fd7e | 356 | } |
c5dc5cec | 357 | } else if (USES_PPGTT(dev)) { |
bdf4fd7e BW |
358 | /* For platforms which only have aliasing PPGTT, we fake the |
359 | * address space and refcounting. */ | |
bdf4fd7e | 360 | ctx->vm = &dev_priv->mm.aliasing_ppgtt->base; |
7e0d96bc BW |
361 | kref_get(&dev_priv->mm.aliasing_ppgtt->ref); |
362 | } else | |
bdf4fd7e BW |
363 | ctx->vm = &dev_priv->gtt.base; |
364 | ||
a45d0f6a | 365 | return ctx; |
9a3b5304 | 366 | |
42c3b603 | 367 | err_unpin: |
691e6415 | 368 | if (is_global_default_ctx && ctx->obj) |
42c3b603 | 369 | i915_gem_object_ggtt_unpin(ctx->obj); |
9a3b5304 | 370 | err_destroy: |
dce3271b | 371 | i915_gem_context_unreference(ctx); |
a45d0f6a | 372 | return ERR_PTR(ret); |
254f965c BW |
373 | } |
374 | ||
acce9ffa BW |
375 | void i915_gem_context_reset(struct drm_device *dev) |
376 | { | |
377 | struct drm_i915_private *dev_priv = dev->dev_private; | |
acce9ffa BW |
378 | int i; |
379 | ||
acce9ffa BW |
380 | /* Prevent the hardware from restoring the last context (which hung) on |
381 | * the next switch */ | |
382 | for (i = 0; i < I915_NUM_RINGS; i++) { | |
a4872ba6 | 383 | struct intel_engine_cs *ring = &dev_priv->ring[i]; |
273497e5 | 384 | struct intel_context *dctx = ring->default_context; |
acce9ffa BW |
385 | |
386 | /* Do a fake switch to the default context */ | |
691e6415 | 387 | if (ring->last_context == dctx) |
acce9ffa BW |
388 | continue; |
389 | ||
390 | if (!ring->last_context) | |
391 | continue; | |
392 | ||
691e6415 | 393 | if (dctx->obj && i == RCS) { |
acce9ffa | 394 | WARN_ON(i915_gem_obj_ggtt_pin(dctx->obj, |
1ec9e26d | 395 | get_context_alignment(dev), 0)); |
acce9ffa BW |
396 | /* Fake a finish/inactive */ |
397 | dctx->obj->base.write_domain = 0; | |
398 | dctx->obj->active = 0; | |
399 | } | |
400 | ||
4bfad3dd VS |
401 | if (ring->last_context->obj && i == RCS) |
402 | i915_gem_object_ggtt_unpin(ring->last_context->obj); | |
403 | ||
acce9ffa BW |
404 | i915_gem_context_unreference(ring->last_context); |
405 | i915_gem_context_reference(dctx); | |
406 | ring->last_context = dctx; | |
407 | } | |
408 | } | |
409 | ||
8245be31 | 410 | int i915_gem_context_init(struct drm_device *dev) |
254f965c BW |
411 | { |
412 | struct drm_i915_private *dev_priv = dev->dev_private; | |
273497e5 | 413 | struct intel_context *ctx; |
a45d0f6a | 414 | int i; |
254f965c | 415 | |
2fa48d8d BW |
416 | /* Init should only be called once per module load. Eventually the |
417 | * restriction on the context_disabled check can be loosened. */ | |
418 | if (WARN_ON(dev_priv->ring[RCS].default_context)) | |
8245be31 | 419 | return 0; |
254f965c | 420 | |
691e6415 CW |
421 | if (HAS_HW_CONTEXTS(dev)) { |
422 | dev_priv->hw_context_size = round_up(get_context_size(dev), 4096); | |
423 | if (dev_priv->hw_context_size > (1<<20)) { | |
424 | DRM_DEBUG_DRIVER("Disabling HW Contexts; invalid size %d\n", | |
425 | dev_priv->hw_context_size); | |
426 | dev_priv->hw_context_size = 0; | |
427 | } | |
254f965c BW |
428 | } |
429 | ||
691e6415 CW |
430 | ctx = i915_gem_create_context(dev, NULL, USES_PPGTT(dev)); |
431 | if (IS_ERR(ctx)) { | |
432 | DRM_ERROR("Failed to create default global context (error %ld)\n", | |
433 | PTR_ERR(ctx)); | |
434 | return PTR_ERR(ctx); | |
254f965c BW |
435 | } |
436 | ||
691e6415 CW |
437 | /* NB: RCS will hold a ref for all rings */ |
438 | for (i = 0; i < I915_NUM_RINGS; i++) | |
439 | dev_priv->ring[i].default_context = ctx; | |
67e3d297 | 440 | |
691e6415 | 441 | DRM_DEBUG_DRIVER("%s context support initialized\n", dev_priv->hw_context_size ? "HW" : "fake"); |
8245be31 | 442 | return 0; |
254f965c BW |
443 | } |
444 | ||
445 | void i915_gem_context_fini(struct drm_device *dev) | |
446 | { | |
447 | struct drm_i915_private *dev_priv = dev->dev_private; | |
273497e5 | 448 | struct intel_context *dctx = dev_priv->ring[RCS].default_context; |
67e3d297 | 449 | int i; |
254f965c | 450 | |
691e6415 CW |
451 | if (dctx->obj) { |
452 | /* The only known way to stop the gpu from accessing the hw context is | |
453 | * to reset it. Do this as the very last operation to avoid confusing | |
454 | * other code, leading to spurious errors. */ | |
455 | intel_gpu_reset(dev); | |
456 | ||
457 | /* When default context is created and switched to, base object refcount | |
458 | * will be 2 (+1 from object creation and +1 from do_switch()). | |
459 | * i915_gem_context_fini() will be called after gpu_idle() has switched | |
460 | * to default context. So we need to unreference the base object once | |
461 | * to offset the do_switch part, so that i915_gem_context_unreference() | |
462 | * can then free the base object correctly. */ | |
463 | WARN_ON(!dev_priv->ring[RCS].last_context); | |
464 | if (dev_priv->ring[RCS].last_context == dctx) { | |
465 | /* Fake switch to NULL context */ | |
466 | WARN_ON(dctx->obj->active); | |
467 | i915_gem_object_ggtt_unpin(dctx->obj); | |
468 | i915_gem_context_unreference(dctx); | |
469 | dev_priv->ring[RCS].last_context = NULL; | |
470 | } | |
d3b448d9 CW |
471 | |
472 | i915_gem_object_ggtt_unpin(dctx->obj); | |
67e3d297 BW |
473 | } |
474 | ||
475 | for (i = 0; i < I915_NUM_RINGS; i++) { | |
a4872ba6 | 476 | struct intel_engine_cs *ring = &dev_priv->ring[i]; |
67e3d297 BW |
477 | |
478 | if (ring->last_context) | |
479 | i915_gem_context_unreference(ring->last_context); | |
480 | ||
481 | ring->default_context = NULL; | |
0009e46c | 482 | ring->last_context = NULL; |
71b76d00 BW |
483 | } |
484 | ||
dce3271b | 485 | i915_gem_context_unreference(dctx); |
254f965c BW |
486 | } |
487 | ||
2fa48d8d BW |
488 | int i915_gem_context_enable(struct drm_i915_private *dev_priv) |
489 | { | |
a4872ba6 | 490 | struct intel_engine_cs *ring; |
2fa48d8d BW |
491 | int ret, i; |
492 | ||
bdf4fd7e BW |
493 | /* This is the only place the aliasing PPGTT gets enabled, which means |
494 | * it has to happen before we bail on reset */ | |
495 | if (dev_priv->mm.aliasing_ppgtt) { | |
496 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; | |
497 | ppgtt->enable(ppgtt); | |
498 | } | |
499 | ||
2fa48d8d BW |
500 | /* FIXME: We should make this work, even in reset */ |
501 | if (i915_reset_in_progress(&dev_priv->gpu_error)) | |
502 | return 0; | |
503 | ||
504 | BUG_ON(!dev_priv->ring[RCS].default_context); | |
bdf4fd7e | 505 | |
2fa48d8d | 506 | for_each_ring(ring, dev_priv, i) { |
691e6415 | 507 | ret = i915_switch_context(ring, ring->default_context); |
2fa48d8d BW |
508 | if (ret) |
509 | return ret; | |
510 | } | |
511 | ||
512 | return 0; | |
513 | } | |
514 | ||
40521054 BW |
515 | static int context_idr_cleanup(int id, void *p, void *data) |
516 | { | |
273497e5 | 517 | struct intel_context *ctx = p; |
40521054 | 518 | |
dce3271b | 519 | i915_gem_context_unreference(ctx); |
40521054 | 520 | return 0; |
254f965c BW |
521 | } |
522 | ||
e422b888 BW |
523 | int i915_gem_context_open(struct drm_device *dev, struct drm_file *file) |
524 | { | |
525 | struct drm_i915_file_private *file_priv = file->driver_priv; | |
f83d6518 | 526 | struct intel_context *ctx; |
e422b888 BW |
527 | |
528 | idr_init(&file_priv->context_idr); | |
529 | ||
0eea67eb | 530 | mutex_lock(&dev->struct_mutex); |
f83d6518 | 531 | ctx = i915_gem_create_context(dev, file_priv, USES_FULL_PPGTT(dev)); |
0eea67eb BW |
532 | mutex_unlock(&dev->struct_mutex); |
533 | ||
f83d6518 | 534 | if (IS_ERR(ctx)) { |
0eea67eb | 535 | idr_destroy(&file_priv->context_idr); |
f83d6518 | 536 | return PTR_ERR(ctx); |
0eea67eb BW |
537 | } |
538 | ||
e422b888 BW |
539 | return 0; |
540 | } | |
541 | ||
254f965c BW |
542 | void i915_gem_context_close(struct drm_device *dev, struct drm_file *file) |
543 | { | |
40521054 | 544 | struct drm_i915_file_private *file_priv = file->driver_priv; |
254f965c | 545 | |
73c273eb | 546 | idr_for_each(&file_priv->context_idr, context_idr_cleanup, NULL); |
40521054 | 547 | idr_destroy(&file_priv->context_idr); |
40521054 BW |
548 | } |
549 | ||
273497e5 | 550 | struct intel_context * |
40521054 BW |
551 | i915_gem_context_get(struct drm_i915_file_private *file_priv, u32 id) |
552 | { | |
273497e5 | 553 | struct intel_context *ctx; |
72ad5c45 | 554 | |
273497e5 | 555 | ctx = (struct intel_context *)idr_find(&file_priv->context_idr, id); |
72ad5c45 BW |
556 | if (!ctx) |
557 | return ERR_PTR(-ENOENT); | |
558 | ||
559 | return ctx; | |
254f965c | 560 | } |
e0556841 BW |
561 | |
562 | static inline int | |
a4872ba6 | 563 | mi_set_context(struct intel_engine_cs *ring, |
273497e5 | 564 | struct intel_context *new_context, |
e0556841 BW |
565 | u32 hw_flags) |
566 | { | |
567 | int ret; | |
568 | ||
12b0286f BW |
569 | /* w/a: If Flush TLB Invalidation Mode is enabled, driver must do a TLB |
570 | * invalidation prior to MI_SET_CONTEXT. On GEN6 we don't set the value | |
571 | * explicitly, so we rely on the value at ring init, stored in | |
572 | * itlb_before_ctx_switch. | |
573 | */ | |
057f6a8a | 574 | if (IS_GEN6(ring->dev)) { |
ac82ea2e | 575 | ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, 0); |
12b0286f BW |
576 | if (ret) |
577 | return ret; | |
578 | } | |
579 | ||
e37ec39b | 580 | ret = intel_ring_begin(ring, 6); |
e0556841 BW |
581 | if (ret) |
582 | return ret; | |
583 | ||
b3f797ac | 584 | /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */ |
64bed788 | 585 | if (INTEL_INFO(ring->dev)->gen >= 7) |
e37ec39b BW |
586 | intel_ring_emit(ring, MI_ARB_ON_OFF | MI_ARB_DISABLE); |
587 | else | |
588 | intel_ring_emit(ring, MI_NOOP); | |
589 | ||
e0556841 BW |
590 | intel_ring_emit(ring, MI_NOOP); |
591 | intel_ring_emit(ring, MI_SET_CONTEXT); | |
f343c5f6 | 592 | intel_ring_emit(ring, i915_gem_obj_ggtt_offset(new_context->obj) | |
e0556841 BW |
593 | MI_MM_SPACE_GTT | |
594 | MI_SAVE_EXT_STATE_EN | | |
595 | MI_RESTORE_EXT_STATE_EN | | |
596 | hw_flags); | |
2b7e8082 VS |
597 | /* |
598 | * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP | |
599 | * WaMiSetContext_Hang:snb,ivb,vlv | |
600 | */ | |
e0556841 BW |
601 | intel_ring_emit(ring, MI_NOOP); |
602 | ||
64bed788 | 603 | if (INTEL_INFO(ring->dev)->gen >= 7) |
e37ec39b BW |
604 | intel_ring_emit(ring, MI_ARB_ON_OFF | MI_ARB_ENABLE); |
605 | else | |
606 | intel_ring_emit(ring, MI_NOOP); | |
607 | ||
e0556841 BW |
608 | intel_ring_advance(ring); |
609 | ||
610 | return ret; | |
611 | } | |
612 | ||
a4872ba6 | 613 | static int do_switch(struct intel_engine_cs *ring, |
273497e5 | 614 | struct intel_context *to) |
e0556841 | 615 | { |
6f65e29a | 616 | struct drm_i915_private *dev_priv = ring->dev->dev_private; |
273497e5 | 617 | struct intel_context *from = ring->last_context; |
7e0d96bc | 618 | struct i915_hw_ppgtt *ppgtt = ctx_to_ppgtt(to); |
e0556841 | 619 | u32 hw_flags = 0; |
967ab6b1 | 620 | bool uninitialized = false; |
3ccfd19d | 621 | int ret, i; |
e0556841 | 622 | |
67e3d297 BW |
623 | if (from != NULL && ring == &dev_priv->ring[RCS]) { |
624 | BUG_ON(from->obj == NULL); | |
625 | BUG_ON(!i915_gem_obj_is_pinned(from->obj)); | |
626 | } | |
e0556841 | 627 | |
14d8ec54 | 628 | if (from == to && !to->remap_slice) |
9a3b5304 CW |
629 | return 0; |
630 | ||
7e0d96bc BW |
631 | /* Trying to pin first makes error handling easier. */ |
632 | if (ring == &dev_priv->ring[RCS]) { | |
633 | ret = i915_gem_obj_ggtt_pin(to->obj, | |
1ec9e26d | 634 | get_context_alignment(ring->dev), 0); |
7e0d96bc BW |
635 | if (ret) |
636 | return ret; | |
67e3d297 BW |
637 | } |
638 | ||
acc240d4 DV |
639 | /* |
640 | * Pin can switch back to the default context if we end up calling into | |
641 | * evict_everything - as a last ditch gtt defrag effort that also | |
642 | * switches to the default context. Hence we need to reload from here. | |
643 | */ | |
644 | from = ring->last_context; | |
645 | ||
7e0d96bc BW |
646 | if (USES_FULL_PPGTT(ring->dev)) { |
647 | ret = ppgtt->switch_mm(ppgtt, ring, false); | |
648 | if (ret) | |
649 | goto unpin_out; | |
650 | } | |
651 | ||
652 | if (ring != &dev_priv->ring[RCS]) { | |
653 | if (from) | |
654 | i915_gem_context_unreference(from); | |
655 | goto done; | |
656 | } | |
657 | ||
acc240d4 DV |
658 | /* |
659 | * Clear this page out of any CPU caches for coherent swap-in/out. Note | |
d3373a24 CW |
660 | * that thanks to write = false in this call and us not setting any gpu |
661 | * write domains when putting a context object onto the active list | |
662 | * (when switching away from it), this won't block. | |
acc240d4 DV |
663 | * |
664 | * XXX: We need a real interface to do this instead of trickery. | |
665 | */ | |
d3373a24 | 666 | ret = i915_gem_object_set_to_gtt_domain(to->obj, false); |
7e0d96bc BW |
667 | if (ret) |
668 | goto unpin_out; | |
d3373a24 | 669 | |
6f65e29a BW |
670 | if (!to->obj->has_global_gtt_mapping) { |
671 | struct i915_vma *vma = i915_gem_obj_to_vma(to->obj, | |
672 | &dev_priv->gtt.base); | |
673 | vma->bind_vma(vma, to->obj->cache_level, GLOBAL_BIND); | |
674 | } | |
3af7b857 | 675 | |
3fac8978 | 676 | if (!to->is_initialized || i915_gem_context_is_default(to)) |
e0556841 | 677 | hw_flags |= MI_RESTORE_INHIBIT; |
e0556841 | 678 | |
e0556841 | 679 | ret = mi_set_context(ring, to, hw_flags); |
7e0d96bc BW |
680 | if (ret) |
681 | goto unpin_out; | |
e0556841 | 682 | |
3ccfd19d BW |
683 | for (i = 0; i < MAX_L3_SLICES; i++) { |
684 | if (!(to->remap_slice & (1<<i))) | |
685 | continue; | |
686 | ||
687 | ret = i915_gem_l3_remap(ring, i); | |
688 | /* If it failed, try again next round */ | |
689 | if (ret) | |
690 | DRM_DEBUG_DRIVER("L3 remapping failed\n"); | |
691 | else | |
692 | to->remap_slice &= ~(1<<i); | |
693 | } | |
694 | ||
e0556841 BW |
695 | /* The backing object for the context is done after switching to the |
696 | * *next* context. Therefore we cannot retire the previous context until | |
697 | * the next context has already started running. In fact, the below code | |
698 | * is a bit suboptimal because the retiring can occur simply after the | |
699 | * MI_SET_CONTEXT instead of when the next seqno has completed. | |
700 | */ | |
112522f6 CW |
701 | if (from != NULL) { |
702 | from->obj->base.read_domains = I915_GEM_DOMAIN_INSTRUCTION; | |
e2d05a8b | 703 | i915_vma_move_to_active(i915_gem_obj_to_ggtt(from->obj), ring); |
e0556841 BW |
704 | /* As long as MI_SET_CONTEXT is serializing, ie. it flushes the |
705 | * whole damn pipeline, we don't need to explicitly mark the | |
706 | * object dirty. The only exception is that the context must be | |
707 | * correct in case the object gets swapped out. Ideally we'd be | |
708 | * able to defer doing this until we know the object would be | |
709 | * swapped, but there is no way to do that yet. | |
710 | */ | |
112522f6 CW |
711 | from->obj->dirty = 1; |
712 | BUG_ON(from->obj->ring != ring); | |
713 | ||
c0321e2c | 714 | /* obj is kept alive until the next request by its active ref */ |
d7f46fc4 | 715 | i915_gem_object_ggtt_unpin(from->obj); |
112522f6 | 716 | i915_gem_context_unreference(from); |
e0556841 BW |
717 | } |
718 | ||
967ab6b1 CW |
719 | uninitialized = !to->is_initialized && from == NULL; |
720 | to->is_initialized = true; | |
721 | ||
67e3d297 | 722 | done: |
112522f6 CW |
723 | i915_gem_context_reference(to); |
724 | ring->last_context = to; | |
e0556841 | 725 | |
967ab6b1 | 726 | if (uninitialized) { |
46470fc9 MK |
727 | ret = i915_gem_render_state_init(ring); |
728 | if (ret) | |
729 | DRM_ERROR("init render state: %d\n", ret); | |
730 | } | |
731 | ||
e0556841 | 732 | return 0; |
7e0d96bc BW |
733 | |
734 | unpin_out: | |
735 | if (ring->id == RCS) | |
736 | i915_gem_object_ggtt_unpin(to->obj); | |
737 | return ret; | |
e0556841 BW |
738 | } |
739 | ||
740 | /** | |
741 | * i915_switch_context() - perform a GPU context switch. | |
742 | * @ring: ring for which we'll execute the context switch | |
96a6f0f1 | 743 | * @to: the context to switch to |
e0556841 BW |
744 | * |
745 | * The context life cycle is simple. The context refcount is incremented and | |
746 | * decremented by 1 and create and destroy. If the context is in use by the GPU, | |
747 | * it will have a refoucnt > 1. This allows us to destroy the context abstract | |
748 | * object while letting the normal object tracking destroy the backing BO. | |
749 | */ | |
a4872ba6 | 750 | int i915_switch_context(struct intel_engine_cs *ring, |
273497e5 | 751 | struct intel_context *to) |
e0556841 BW |
752 | { |
753 | struct drm_i915_private *dev_priv = ring->dev->dev_private; | |
e0556841 | 754 | |
0eea67eb BW |
755 | WARN_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex)); |
756 | ||
691e6415 CW |
757 | if (to->obj == NULL) { /* We have the fake context */ |
758 | if (to != ring->last_context) { | |
759 | i915_gem_context_reference(to); | |
760 | if (ring->last_context) | |
761 | i915_gem_context_unreference(ring->last_context); | |
762 | ring->last_context = to; | |
763 | } | |
c482972a | 764 | return 0; |
a95f6a00 | 765 | } |
c482972a | 766 | |
67e3d297 | 767 | return do_switch(ring, to); |
e0556841 | 768 | } |
84624813 | 769 | |
691e6415 CW |
770 | static bool hw_context_enabled(struct drm_device *dev) |
771 | { | |
772 | return to_i915(dev)->hw_context_size; | |
773 | } | |
774 | ||
84624813 BW |
775 | int i915_gem_context_create_ioctl(struct drm_device *dev, void *data, |
776 | struct drm_file *file) | |
777 | { | |
84624813 BW |
778 | struct drm_i915_gem_context_create *args = data; |
779 | struct drm_i915_file_private *file_priv = file->driver_priv; | |
273497e5 | 780 | struct intel_context *ctx; |
84624813 BW |
781 | int ret; |
782 | ||
691e6415 | 783 | if (!hw_context_enabled(dev)) |
5fa8be65 DV |
784 | return -ENODEV; |
785 | ||
84624813 BW |
786 | ret = i915_mutex_lock_interruptible(dev); |
787 | if (ret) | |
788 | return ret; | |
789 | ||
7e0d96bc | 790 | ctx = i915_gem_create_context(dev, file_priv, USES_FULL_PPGTT(dev)); |
84624813 | 791 | mutex_unlock(&dev->struct_mutex); |
be636387 DC |
792 | if (IS_ERR(ctx)) |
793 | return PTR_ERR(ctx); | |
84624813 BW |
794 | |
795 | args->ctx_id = ctx->id; | |
796 | DRM_DEBUG_DRIVER("HW context %d created\n", args->ctx_id); | |
797 | ||
be636387 | 798 | return 0; |
84624813 BW |
799 | } |
800 | ||
801 | int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data, | |
802 | struct drm_file *file) | |
803 | { | |
804 | struct drm_i915_gem_context_destroy *args = data; | |
805 | struct drm_i915_file_private *file_priv = file->driver_priv; | |
273497e5 | 806 | struct intel_context *ctx; |
84624813 BW |
807 | int ret; |
808 | ||
0eea67eb | 809 | if (args->ctx_id == DEFAULT_CONTEXT_ID) |
c2cf2416 | 810 | return -ENOENT; |
0eea67eb | 811 | |
84624813 BW |
812 | ret = i915_mutex_lock_interruptible(dev); |
813 | if (ret) | |
814 | return ret; | |
815 | ||
816 | ctx = i915_gem_context_get(file_priv, args->ctx_id); | |
72ad5c45 | 817 | if (IS_ERR(ctx)) { |
84624813 | 818 | mutex_unlock(&dev->struct_mutex); |
72ad5c45 | 819 | return PTR_ERR(ctx); |
84624813 BW |
820 | } |
821 | ||
dce3271b MK |
822 | idr_remove(&ctx->file_priv->context_idr, ctx->id); |
823 | i915_gem_context_unreference(ctx); | |
84624813 BW |
824 | mutex_unlock(&dev->struct_mutex); |
825 | ||
826 | DRM_DEBUG_DRIVER("HW context %d destroyed\n", args->ctx_id); | |
827 | return 0; | |
828 | } |