blob: fe93ed1e012f343a04cde5bf7c13891876bb257e [file] [log] [blame]
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02001/*
2 * Copyright © 2016 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 */
24
25#include "i915_vma.h"
26
27#include "i915_drv.h"
28#include "intel_ringbuffer.h"
29#include "intel_frontbuffer.h"
30
31#include <drm/drm_gem.h>
32
33static void
34i915_vma_retire(struct i915_gem_active *active,
35 struct drm_i915_gem_request *rq)
36{
37 const unsigned int idx = rq->engine->id;
38 struct i915_vma *vma =
39 container_of(active, struct i915_vma, last_read[idx]);
40 struct drm_i915_gem_object *obj = vma->obj;
41
42 GEM_BUG_ON(!i915_vma_has_active_engine(vma, idx));
43
44 i915_vma_clear_active(vma, idx);
45 if (i915_vma_is_active(vma))
46 return;
47
48 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
49 if (unlikely(i915_vma_is_closed(vma) && !i915_vma_is_pinned(vma)))
50 WARN_ON(i915_vma_unbind(vma));
51
52 GEM_BUG_ON(!i915_gem_object_is_active(obj));
53 if (--obj->active_count)
54 return;
55
56 /* Bump our place on the bound list to keep it roughly in LRU order
57 * so that we don't steal from recently used but inactive objects
58 * (unless we are forced to ofc!)
59 */
60 if (obj->bind_count)
61 list_move_tail(&obj->global_link, &rq->i915->mm.bound_list);
62
63 obj->mm.dirty = true; /* be paranoid */
64
65 if (i915_gem_object_has_active_reference(obj)) {
66 i915_gem_object_clear_active_reference(obj);
67 i915_gem_object_put(obj);
68 }
69}
70
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020071static struct i915_vma *
72__i915_vma_create(struct drm_i915_gem_object *obj,
73 struct i915_address_space *vm,
74 const struct i915_ggtt_view *view)
75{
76 struct i915_vma *vma;
77 struct rb_node *rb, **p;
78 int i;
79
80 GEM_BUG_ON(vm->closed);
81
82 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
83 if (vma == NULL)
84 return ERR_PTR(-ENOMEM);
85
86 INIT_LIST_HEAD(&vma->exec_list);
87 for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
88 init_request_active(&vma->last_read[i], i915_vma_retire);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020089 init_request_active(&vma->last_fence, NULL);
90 list_add(&vma->vm_link, &vm->unbound_list);
91 vma->vm = vm;
92 vma->obj = obj;
93 vma->size = obj->base.size;
Chris Wilsonf51455d2017-01-10 14:47:34 +000094 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020095
96 if (view) {
97 vma->ggtt_view = *view;
98 if (view->type == I915_GGTT_VIEW_PARTIAL) {
Chris Wilson07e19ea2016-12-23 14:57:59 +000099 GEM_BUG_ON(range_overflows_t(u64,
Chris Wilson8bab11932017-01-14 00:28:25 +0000100 view->partial.offset,
101 view->partial.size,
Chris Wilson07e19ea2016-12-23 14:57:59 +0000102 obj->base.size >> PAGE_SHIFT));
Chris Wilson8bab11932017-01-14 00:28:25 +0000103 vma->size = view->partial.size;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200104 vma->size <<= PAGE_SHIFT;
Chris Wilson07e19ea2016-12-23 14:57:59 +0000105 GEM_BUG_ON(vma->size >= obj->base.size);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200106 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
Chris Wilson8bab11932017-01-14 00:28:25 +0000107 vma->size = intel_rotation_info_size(&view->rotated);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200108 vma->size <<= PAGE_SHIFT;
109 }
110 }
111
112 if (i915_is_ggtt(vm)) {
Chris Wilson944397f2017-01-09 16:16:11 +0000113 GEM_BUG_ON(overflows_type(vma->size, u32));
Chris Wilson91d4e0aa2017-01-09 16:16:13 +0000114 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
115 i915_gem_object_get_tiling(obj),
116 i915_gem_object_get_stride(obj));
Chris Wilsonf51455d2017-01-10 14:47:34 +0000117 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
Chris Wilson944397f2017-01-09 16:16:11 +0000118
Chris Wilson91d4e0aa2017-01-09 16:16:13 +0000119 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
120 i915_gem_object_get_tiling(obj),
121 i915_gem_object_get_stride(obj));
Chris Wilson944397f2017-01-09 16:16:11 +0000122 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
123
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200124 vma->flags |= I915_VMA_GGTT;
125 list_add(&vma->obj_link, &obj->vma_list);
126 } else {
127 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
128 list_add_tail(&vma->obj_link, &obj->vma_list);
129 }
130
131 rb = NULL;
132 p = &obj->vma_tree.rb_node;
133 while (*p) {
134 struct i915_vma *pos;
135
136 rb = *p;
137 pos = rb_entry(rb, struct i915_vma, obj_node);
138 if (i915_vma_compare(pos, vm, view) < 0)
139 p = &rb->rb_right;
140 else
141 p = &rb->rb_left;
142 }
143 rb_link_node(&vma->obj_node, rb, p);
144 rb_insert_color(&vma->obj_node, &obj->vma_tree);
145
146 return vma;
147}
148
149struct i915_vma *
150i915_vma_create(struct drm_i915_gem_object *obj,
151 struct i915_address_space *vm,
152 const struct i915_ggtt_view *view)
153{
154 lockdep_assert_held(&obj->base.dev->struct_mutex);
155 GEM_BUG_ON(view && !i915_is_ggtt(vm));
156 GEM_BUG_ON(i915_gem_obj_to_vma(obj, vm, view));
157
158 return __i915_vma_create(obj, vm, view);
159}
160
161/**
162 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
163 * @vma: VMA to map
164 * @cache_level: mapping cache level
165 * @flags: flags like global or local mapping
166 *
167 * DMA addresses are taken from the scatter-gather table of this object (or of
168 * this VMA in case of non-default GGTT views) and PTE entries set up.
169 * Note that DMA addresses are also the only part of the SG table we care about.
170 */
171int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
172 u32 flags)
173{
174 u32 bind_flags;
175 u32 vma_flags;
176 int ret;
177
178 if (WARN_ON(flags == 0))
179 return -EINVAL;
180
181 bind_flags = 0;
182 if (flags & PIN_GLOBAL)
183 bind_flags |= I915_VMA_GLOBAL_BIND;
184 if (flags & PIN_USER)
185 bind_flags |= I915_VMA_LOCAL_BIND;
186
187 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
188 if (flags & PIN_UPDATE)
189 bind_flags |= vma_flags;
190 else
191 bind_flags &= ~vma_flags;
192 if (bind_flags == 0)
193 return 0;
194
Matthew Auld966d5bf2016-12-13 20:32:22 +0000195 if (GEM_WARN_ON(range_overflows(vma->node.start,
196 vma->node.size,
197 vma->vm->total)))
Matthew Auld7a0499a2016-12-13 20:32:20 +0000198 return -ENODEV;
199
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200200 if (vma_flags == 0 && vma->vm->allocate_va_range) {
201 trace_i915_va_alloc(vma);
202 ret = vma->vm->allocate_va_range(vma->vm,
203 vma->node.start,
204 vma->node.size);
205 if (ret)
206 return ret;
207 }
208
209 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
210 if (ret)
211 return ret;
212
213 vma->flags |= bind_flags;
214 return 0;
215}
216
217void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
218{
219 void __iomem *ptr;
220
221 /* Access through the GTT requires the device to be awake. */
Chris Wilson49d73912016-11-29 09:50:08 +0000222 assert_rpm_wakelock_held(vma->vm->i915);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200223
Chris Wilson49d73912016-11-29 09:50:08 +0000224 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200225 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma)))
226 return IO_ERR_PTR(-ENODEV);
227
228 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
229 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
230
231 ptr = vma->iomap;
232 if (ptr == NULL) {
233 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->mappable,
234 vma->node.start,
235 vma->node.size);
236 if (ptr == NULL)
237 return IO_ERR_PTR(-ENOMEM);
238
239 vma->iomap = ptr;
240 }
241
242 __i915_vma_pin(vma);
243 return ptr;
244}
245
246void i915_vma_unpin_and_release(struct i915_vma **p_vma)
247{
248 struct i915_vma *vma;
249 struct drm_i915_gem_object *obj;
250
251 vma = fetch_and_zero(p_vma);
252 if (!vma)
253 return;
254
255 obj = vma->obj;
256
257 i915_vma_unpin(vma);
258 i915_vma_close(vma);
259
260 __i915_gem_object_release_unless_active(obj);
261}
262
263bool
264i915_vma_misplaced(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
265{
266 if (!drm_mm_node_allocated(&vma->node))
267 return false;
268
269 if (vma->node.size < size)
270 return true;
271
Chris Wilsonf51455d2017-01-10 14:47:34 +0000272 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
273 if (alignment && !IS_ALIGNED(vma->node.start, alignment))
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200274 return true;
275
276 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
277 return true;
278
279 if (flags & PIN_OFFSET_BIAS &&
280 vma->node.start < (flags & PIN_OFFSET_MASK))
281 return true;
282
283 if (flags & PIN_OFFSET_FIXED &&
284 vma->node.start != (flags & PIN_OFFSET_MASK))
285 return true;
286
287 return false;
288}
289
290void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
291{
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200292 bool mappable, fenceable;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200293
Chris Wilson944397f2017-01-09 16:16:11 +0000294 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
295 GEM_BUG_ON(!vma->fence_size);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200296
297 /*
298 * Explicitly disable for rotated VMA since the display does not
299 * need the fence and the VMA is not accessible to other users.
300 */
Chris Wilson944397f2017-01-09 16:16:11 +0000301 if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
302 return;
303
304 fenceable = (vma->node.size >= vma->fence_size &&
Chris Wilsonf51455d2017-01-10 14:47:34 +0000305 IS_ALIGNED(vma->node.start, vma->fence_alignment));
Chris Wilson944397f2017-01-09 16:16:11 +0000306
307 mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
308
309 if (mappable && fenceable)
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200310 vma->flags |= I915_VMA_CAN_FENCE;
311 else
312 vma->flags &= ~I915_VMA_CAN_FENCE;
313}
314
Chris Wilson7d1d9ae2016-12-05 14:29:38 +0000315static bool color_differs(struct drm_mm_node *node, unsigned long color)
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200316{
Chris Wilson7d1d9ae2016-12-05 14:29:38 +0000317 return node->allocated && node->color != color;
318}
319
320bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
321{
322 struct drm_mm_node *node = &vma->node;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200323 struct drm_mm_node *other;
324
325 /*
326 * On some machines we have to be careful when putting differing types
327 * of snoopable memory together to avoid the prefetcher crossing memory
328 * domains and dying. During vm initialisation, we decide whether or not
329 * these constraints apply and set the drm_mm.color_adjust
330 * appropriately.
331 */
332 if (vma->vm->mm.color_adjust == NULL)
333 return true;
334
Chris Wilson7d1d9ae2016-12-05 14:29:38 +0000335 /* Only valid to be called on an already inserted vma */
336 GEM_BUG_ON(!drm_mm_node_allocated(node));
337 GEM_BUG_ON(list_empty(&node->node_list));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200338
Chris Wilson7d1d9ae2016-12-05 14:29:38 +0000339 other = list_prev_entry(node, node_list);
Daniel Vetteref426c12017-01-04 11:41:10 +0100340 if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200341 return false;
342
Chris Wilson7d1d9ae2016-12-05 14:29:38 +0000343 other = list_next_entry(node, node_list);
Daniel Vetteref426c12017-01-04 11:41:10 +0100344 if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200345 return false;
346
347 return true;
348}
349
350/**
351 * i915_vma_insert - finds a slot for the vma in its address space
352 * @vma: the vma
353 * @size: requested size in bytes (can be larger than the VMA)
354 * @alignment: required alignment
355 * @flags: mask of PIN_* flags to use
356 *
357 * First we try to allocate some free space that meets the requirements for
358 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
359 * preferrably the oldest idle entry to make room for the new VMA.
360 *
361 * Returns:
362 * 0 on success, negative error code otherwise.
363 */
364static int
365i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
366{
Chris Wilson49d73912016-11-29 09:50:08 +0000367 struct drm_i915_private *dev_priv = vma->vm->i915;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200368 struct drm_i915_gem_object *obj = vma->obj;
369 u64 start, end;
370 int ret;
371
372 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
373 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
374
375 size = max(size, vma->size);
Chris Wilson944397f2017-01-09 16:16:11 +0000376 alignment = max(alignment, vma->display_alignment);
377 if (flags & PIN_MAPPABLE) {
378 size = max_t(typeof(size), size, vma->fence_size);
379 alignment = max_t(typeof(alignment),
380 alignment, vma->fence_alignment);
381 }
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200382
Chris Wilsonf51455d2017-01-10 14:47:34 +0000383 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
384 GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
385 GEM_BUG_ON(!is_power_of_2(alignment));
386
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200387 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
Chris Wilsonf51455d2017-01-10 14:47:34 +0000388 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200389
390 end = vma->vm->total;
391 if (flags & PIN_MAPPABLE)
392 end = min_t(u64, end, dev_priv->ggtt.mappable_end);
393 if (flags & PIN_ZONE_4G)
Chris Wilsonf51455d2017-01-10 14:47:34 +0000394 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
395 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200396
397 /* If binding the object/GGTT view requires more space than the entire
398 * aperture has, reject it early before evicting everything in a vain
399 * attempt to find space.
400 */
401 if (size > end) {
402 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n",
403 size, obj->base.size,
404 flags & PIN_MAPPABLE ? "mappable" : "total",
405 end);
406 return -E2BIG;
407 }
408
409 ret = i915_gem_object_pin_pages(obj);
410 if (ret)
411 return ret;
412
413 if (flags & PIN_OFFSET_FIXED) {
414 u64 offset = flags & PIN_OFFSET_MASK;
Chris Wilsonf51455d2017-01-10 14:47:34 +0000415 if (!IS_ALIGNED(offset, alignment) ||
Chris Wilsone8f9ae92017-01-06 15:20:12 +0000416 range_overflows(offset, size, end)) {
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200417 ret = -EINVAL;
418 goto err_unpin;
419 }
420
Chris Wilson625d9882017-01-11 11:23:11 +0000421 ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
422 size, offset, obj->cache_level,
423 flags);
424 if (ret)
425 goto err_unpin;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200426 } else {
Chris Wilsone007b192017-01-11 11:23:10 +0000427 ret = i915_gem_gtt_insert(vma->vm, &vma->node,
428 size, alignment, obj->cache_level,
429 start, end, flags);
430 if (ret)
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200431 goto err_unpin;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200432
433 GEM_BUG_ON(vma->node.start < start);
434 GEM_BUG_ON(vma->node.start + vma->node.size > end);
435 }
436 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level));
437
438 list_move_tail(&obj->global_link, &dev_priv->mm.bound_list);
439 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
440 obj->bind_count++;
441 GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
442
443 return 0;
444
445err_unpin:
446 i915_gem_object_unpin_pages(obj);
447 return ret;
448}
449
450int __i915_vma_do_pin(struct i915_vma *vma,
451 u64 size, u64 alignment, u64 flags)
452{
453 unsigned int bound = vma->flags;
454 int ret;
455
Chris Wilson49d73912016-11-29 09:50:08 +0000456 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200457 GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
458 GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
459
460 if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
461 ret = -EBUSY;
462 goto err;
463 }
464
465 if ((bound & I915_VMA_BIND_MASK) == 0) {
466 ret = i915_vma_insert(vma, size, alignment, flags);
467 if (ret)
468 goto err;
469 }
470
471 ret = i915_vma_bind(vma, vma->obj->cache_level, flags);
472 if (ret)
473 goto err;
474
475 if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
476 __i915_vma_set_map_and_fenceable(vma);
477
Chris Wilson03257012017-01-11 21:09:26 +0000478 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200479 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
480 return 0;
481
482err:
483 __i915_vma_unpin(vma);
484 return ret;
485}
486
487void i915_vma_destroy(struct i915_vma *vma)
488{
489 GEM_BUG_ON(vma->node.allocated);
490 GEM_BUG_ON(i915_vma_is_active(vma));
491 GEM_BUG_ON(!i915_vma_is_closed(vma));
492 GEM_BUG_ON(vma->fence);
493
494 list_del(&vma->vm_link);
495 if (!i915_vma_is_ggtt(vma))
496 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
497
498 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
499}
500
501void i915_vma_close(struct i915_vma *vma)
502{
503 GEM_BUG_ON(i915_vma_is_closed(vma));
504 vma->flags |= I915_VMA_CLOSED;
505
506 list_del(&vma->obj_link);
507 rb_erase(&vma->obj_node, &vma->obj->vma_tree);
508
509 if (!i915_vma_is_active(vma) && !i915_vma_is_pinned(vma))
510 WARN_ON(i915_vma_unbind(vma));
511}
512
513static void __i915_vma_iounmap(struct i915_vma *vma)
514{
515 GEM_BUG_ON(i915_vma_is_pinned(vma));
516
517 if (vma->iomap == NULL)
518 return;
519
520 io_mapping_unmap(vma->iomap);
521 vma->iomap = NULL;
522}
523
524int i915_vma_unbind(struct i915_vma *vma)
525{
526 struct drm_i915_gem_object *obj = vma->obj;
527 unsigned long active;
528 int ret;
529
530 lockdep_assert_held(&obj->base.dev->struct_mutex);
531
532 /* First wait upon any activity as retiring the request may
533 * have side-effects such as unpinning or even unbinding this vma.
534 */
535 active = i915_vma_get_active(vma);
536 if (active) {
537 int idx;
538
539 /* When a closed VMA is retired, it is unbound - eek.
540 * In order to prevent it from being recursively closed,
541 * take a pin on the vma so that the second unbind is
542 * aborted.
543 *
544 * Even more scary is that the retire callback may free
545 * the object (last active vma). To prevent the explosion
546 * we defer the actual object free to a worker that can
547 * only proceed once it acquires the struct_mutex (which
548 * we currently hold, therefore it cannot free this object
549 * before we are finished).
550 */
551 __i915_vma_pin(vma);
552
553 for_each_active(active, idx) {
554 ret = i915_gem_active_retire(&vma->last_read[idx],
Chris Wilson49d73912016-11-29 09:50:08 +0000555 &vma->vm->i915->drm.struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200556 if (ret)
557 break;
558 }
559
560 __i915_vma_unpin(vma);
561 if (ret)
562 return ret;
563
564 GEM_BUG_ON(i915_vma_is_active(vma));
565 }
566
567 if (i915_vma_is_pinned(vma))
568 return -EBUSY;
569
570 if (!drm_mm_node_allocated(&vma->node))
571 goto destroy;
572
573 GEM_BUG_ON(obj->bind_count == 0);
574 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
575
576 if (i915_vma_is_map_and_fenceable(vma)) {
577 /* release the fence reg _after_ flushing */
578 ret = i915_vma_put_fence(vma);
579 if (ret)
580 return ret;
581
582 /* Force a pagefault for domain tracking on next user access */
583 i915_gem_release_mmap(obj);
584
585 __i915_vma_iounmap(vma);
586 vma->flags &= ~I915_VMA_CAN_FENCE;
587 }
588
589 if (likely(!vma->vm->closed)) {
590 trace_i915_vma_unbind(vma);
591 vma->vm->unbind_vma(vma);
592 }
593 vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
594
595 drm_mm_remove_node(&vma->node);
596 list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
597
598 if (vma->pages != obj->mm.pages) {
599 GEM_BUG_ON(!vma->pages);
600 sg_free_table(vma->pages);
601 kfree(vma->pages);
602 }
603 vma->pages = NULL;
604
605 /* Since the unbound list is global, only move to that list if
606 * no more VMAs exist. */
607 if (--obj->bind_count == 0)
608 list_move_tail(&obj->global_link,
609 &to_i915(obj->base.dev)->mm.unbound_list);
610
611 /* And finally now the object is completely decoupled from this vma,
612 * we can drop its hold on the backing storage and allow it to be
613 * reaped by the shrinker.
614 */
615 i915_gem_object_unpin_pages(obj);
Chris Wilson7a5580a2016-12-31 11:20:09 +0000616 GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200617
618destroy:
619 if (unlikely(i915_vma_is_closed(vma)))
620 i915_vma_destroy(vma);
621
622 return 0;
623}
624