blob: 4183b0e103246586d04683d672157ee3194478b6 [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 */
Chris Wilson10195b12018-06-28 14:22:06 +010024
Chris Wilson09480072019-07-03 10:17:19 +010025#include <linux/sched/mm.h>
Jani Nikuladf0566a2019-06-13 11:44:16 +030026#include <drm/drm_gem.h>
Chris Wilson112ed2d2019-04-24 18:48:39 +010027
Jani Nikuladf0566a2019-06-13 11:44:16 +030028#include "display/intel_frontbuffer.h"
29
30#include "gt/intel_engine.h"
Tvrtko Ursulina1c8a092019-06-21 08:08:01 +010031#include "gt/intel_gt.h"
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020032
33#include "i915_drv.h"
Chris Wilson103b76ee2019-03-05 21:38:30 +000034#include "i915_globals.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030035#include "i915_trace.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030036#include "i915_vma.h"
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020037
Chris Wilson13f1bfd2019-02-28 10:20:34 +000038static struct i915_global_vma {
Chris Wilson103b76ee2019-03-05 21:38:30 +000039 struct i915_global base;
Chris Wilson13f1bfd2019-02-28 10:20:34 +000040 struct kmem_cache *slab_vmas;
41} global;
42
43struct i915_vma *i915_vma_alloc(void)
44{
45 return kmem_cache_zalloc(global.slab_vmas, GFP_KERNEL);
46}
47
48void i915_vma_free(struct i915_vma *vma)
49{
50 return kmem_cache_free(global.slab_vmas, vma);
51}
52
Chris Wilson1eca65d2018-07-06 07:53:06 +010053#if IS_ENABLED(CONFIG_DRM_I915_ERRLOG_GEM) && IS_ENABLED(CONFIG_DRM_DEBUG_MM)
Chris Wilson10195b12018-06-28 14:22:06 +010054
55#include <linux/stackdepot.h>
56
57static void vma_print_allocator(struct i915_vma *vma, const char *reason)
58{
Thomas Gleixner487f3c72019-04-25 11:45:09 +020059 unsigned long *entries;
60 unsigned int nr_entries;
Chris Wilson10195b12018-06-28 14:22:06 +010061 char buf[512];
62
63 if (!vma->node.stack) {
64 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: unknown owner\n",
65 vma->node.start, vma->node.size, reason);
66 return;
67 }
68
Thomas Gleixner487f3c72019-04-25 11:45:09 +020069 nr_entries = stack_depot_fetch(vma->node.stack, &entries);
70 stack_trace_snprint(buf, sizeof(buf), entries, nr_entries, 0);
Chris Wilson10195b12018-06-28 14:22:06 +010071 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: inserted at %s\n",
72 vma->node.start, vma->node.size, reason, buf);
73}
74
75#else
76
77static void vma_print_allocator(struct i915_vma *vma, const char *reason)
78{
79}
80
81#endif
82
Chris Wilson12c255b2019-06-21 19:38:00 +010083static inline struct i915_vma *active_to_vma(struct i915_active *ref)
84{
85 return container_of(ref, typeof(struct i915_vma), active);
86}
87
88static int __i915_vma_active(struct i915_active *ref)
89{
90 i915_vma_get(active_to_vma(ref));
91 return 0;
92}
93
Chris Wilson64d6c502019-02-05 13:00:02 +000094static void __i915_vma_retire(struct i915_active *ref)
95{
Chris Wilson12c255b2019-06-21 19:38:00 +010096 i915_vma_put(active_to_vma(ref));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020097}
98
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020099static struct i915_vma *
Chris Wilsona01cb37a2017-01-16 15:21:30 +0000100vma_create(struct drm_i915_gem_object *obj,
101 struct i915_address_space *vm,
102 const struct i915_ggtt_view *view)
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200103{
104 struct i915_vma *vma;
105 struct rb_node *rb, **p;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200106
Chris Wilsone1cc3db2017-02-09 11:19:33 +0000107 /* The aliasing_ppgtt should never be used directly! */
Chris Wilsonc082afa2019-07-30 15:32:08 +0100108 GEM_BUG_ON(vm == &vm->i915->ggtt.alias->vm);
Chris Wilsone1cc3db2017-02-09 11:19:33 +0000109
Chris Wilson13f1bfd2019-02-28 10:20:34 +0000110 vma = i915_vma_alloc();
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200111 if (vma == NULL)
112 return ERR_PTR(-ENOMEM);
113
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200114 vma->vm = vm;
Chris Wilson93f2cde2018-06-07 16:40:46 +0100115 vma->ops = &vm->vma_ops;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200116 vma->obj = obj;
Chris Wilsonef78f7b2019-06-18 13:58:58 +0100117 vma->resv = obj->base.resv;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200118 vma->size = obj->base.size;
Chris Wilsonf51455d2017-01-10 14:47:34 +0000119 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200120
Chris Wilson12c255b2019-06-21 19:38:00 +0100121 i915_active_init(vm->i915, &vma->active,
122 __i915_vma_active, __i915_vma_retire);
Chris Wilson155ab882019-06-06 12:23:20 +0100123 INIT_ACTIVE_REQUEST(&vma->last_fence);
124
Chris Wilson09480072019-07-03 10:17:19 +0100125 /* Declare ourselves safe for use inside shrinkers */
126 if (IS_ENABLED(CONFIG_LOCKDEP)) {
127 fs_reclaim_acquire(GFP_KERNEL);
128 might_lock(&vma->active.mutex);
129 fs_reclaim_release(GFP_KERNEL);
130 }
131
Chris Wilson155ab882019-06-06 12:23:20 +0100132 INIT_LIST_HEAD(&vma->closed_link);
133
Chris Wilson7c518462017-01-23 14:52:45 +0000134 if (view && view->type != I915_GGTT_VIEW_NORMAL) {
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200135 vma->ggtt_view = *view;
136 if (view->type == I915_GGTT_VIEW_PARTIAL) {
Chris Wilson07e19ea2016-12-23 14:57:59 +0000137 GEM_BUG_ON(range_overflows_t(u64,
Chris Wilson8bab11932017-01-14 00:28:25 +0000138 view->partial.offset,
139 view->partial.size,
Chris Wilson07e19ea2016-12-23 14:57:59 +0000140 obj->base.size >> PAGE_SHIFT));
Chris Wilson8bab11932017-01-14 00:28:25 +0000141 vma->size = view->partial.size;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200142 vma->size <<= PAGE_SHIFT;
Chris Wilson7e7367d2018-06-30 10:05:09 +0100143 GEM_BUG_ON(vma->size > obj->base.size);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200144 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
Chris Wilson8bab11932017-01-14 00:28:25 +0000145 vma->size = intel_rotation_info_size(&view->rotated);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200146 vma->size <<= PAGE_SHIFT;
Ville Syrjälä1a74fc02019-05-09 15:21:52 +0300147 } else if (view->type == I915_GGTT_VIEW_REMAPPED) {
148 vma->size = intel_remapped_info_size(&view->remapped);
149 vma->size <<= PAGE_SHIFT;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200150 }
151 }
152
Chris Wilson1fcdaa72017-01-19 19:26:56 +0000153 if (unlikely(vma->size > vm->total))
154 goto err_vma;
155
Chris Wilsonb00ddb22017-01-19 19:26:59 +0000156 GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
157
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200158 if (i915_is_ggtt(vm)) {
Chris Wilson1fcdaa72017-01-19 19:26:56 +0000159 if (unlikely(overflows_type(vma->size, u32)))
160 goto err_vma;
161
Chris Wilson91d4e0aa2017-01-09 16:16:13 +0000162 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
163 i915_gem_object_get_tiling(obj),
164 i915_gem_object_get_stride(obj));
Chris Wilson1fcdaa72017-01-19 19:26:56 +0000165 if (unlikely(vma->fence_size < vma->size || /* overflow */
166 vma->fence_size > vm->total))
167 goto err_vma;
168
Chris Wilsonf51455d2017-01-10 14:47:34 +0000169 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
Chris Wilson944397f2017-01-09 16:16:11 +0000170
Chris Wilson91d4e0aa2017-01-09 16:16:13 +0000171 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
172 i915_gem_object_get_tiling(obj),
173 i915_gem_object_get_stride(obj));
Chris Wilson944397f2017-01-09 16:16:11 +0000174 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
175
Chris Wilson528cbd12019-01-28 10:23:54 +0000176 vma->flags |= I915_VMA_GGTT;
177 }
178
179 spin_lock(&obj->vma.lock);
180
181 rb = NULL;
182 p = &obj->vma.tree.rb_node;
183 while (*p) {
184 struct i915_vma *pos;
185 long cmp;
186
187 rb = *p;
188 pos = rb_entry(rb, struct i915_vma, obj_node);
189
190 /*
191 * If the view already exists in the tree, another thread
192 * already created a matching vma, so return the older instance
193 * and dispose of ours.
194 */
195 cmp = i915_vma_compare(pos, vm, view);
196 if (cmp == 0) {
197 spin_unlock(&obj->vma.lock);
Chris Wilson13f1bfd2019-02-28 10:20:34 +0000198 i915_vma_free(vma);
Chris Wilson528cbd12019-01-28 10:23:54 +0000199 return pos;
200 }
201
202 if (cmp < 0)
203 p = &rb->rb_right;
204 else
205 p = &rb->rb_left;
206 }
207 rb_link_node(&vma->obj_node, rb, p);
208 rb_insert_color(&vma->obj_node, &obj->vma.tree);
209
210 if (i915_vma_is_ggtt(vma))
Chris Wilsone2189dd2017-12-07 21:14:07 +0000211 /*
212 * We put the GGTT vma at the start of the vma-list, followed
213 * by the ppGGTT vma. This allows us to break early when
214 * iterating over only the GGTT vma for an object, see
215 * for_each_ggtt_vma()
216 */
Chris Wilson528cbd12019-01-28 10:23:54 +0000217 list_add(&vma->obj_link, &obj->vma.list);
218 else
219 list_add_tail(&vma->obj_link, &obj->vma.list);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200220
Chris Wilson528cbd12019-01-28 10:23:54 +0000221 spin_unlock(&obj->vma.lock);
Chris Wilson09d7e462019-01-28 10:23:53 +0000222
223 mutex_lock(&vm->mutex);
Chris Wilson1fcdaa72017-01-19 19:26:56 +0000224 list_add(&vma->vm_link, &vm->unbound_list);
Chris Wilson09d7e462019-01-28 10:23:53 +0000225 mutex_unlock(&vm->mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200226
227 return vma;
Chris Wilson1fcdaa72017-01-19 19:26:56 +0000228
229err_vma:
Chris Wilson13f1bfd2019-02-28 10:20:34 +0000230 i915_vma_free(vma);
Chris Wilson1fcdaa72017-01-19 19:26:56 +0000231 return ERR_PTR(-E2BIG);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200232}
233
Chris Wilson481a6f72017-01-16 15:21:31 +0000234static struct i915_vma *
235vma_lookup(struct drm_i915_gem_object *obj,
236 struct i915_address_space *vm,
237 const struct i915_ggtt_view *view)
Chris Wilson718659a2017-01-16 15:21:28 +0000238{
239 struct rb_node *rb;
240
Chris Wilson528cbd12019-01-28 10:23:54 +0000241 rb = obj->vma.tree.rb_node;
Chris Wilson718659a2017-01-16 15:21:28 +0000242 while (rb) {
243 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
244 long cmp;
245
246 cmp = i915_vma_compare(vma, vm, view);
247 if (cmp == 0)
248 return vma;
249
250 if (cmp < 0)
251 rb = rb->rb_right;
252 else
253 rb = rb->rb_left;
254 }
255
256 return NULL;
257}
258
259/**
Chris Wilson718659a2017-01-16 15:21:28 +0000260 * i915_vma_instance - return the singleton instance of the VMA
261 * @obj: parent &struct drm_i915_gem_object to be mapped
262 * @vm: address space in which the mapping is located
263 * @view: additional mapping requirements
264 *
265 * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
266 * the same @view characteristics. If a match is not found, one is created.
267 * Once created, the VMA is kept until either the object is freed, or the
268 * address space is closed.
269 *
270 * Must be called with struct_mutex held.
271 *
272 * Returns the vma, or an error pointer.
273 */
274struct i915_vma *
275i915_vma_instance(struct drm_i915_gem_object *obj,
276 struct i915_address_space *vm,
277 const struct i915_ggtt_view *view)
278{
279 struct i915_vma *vma;
280
Chris Wilson718659a2017-01-16 15:21:28 +0000281 GEM_BUG_ON(view && !i915_is_ggtt(vm));
282 GEM_BUG_ON(vm->closed);
283
Chris Wilson528cbd12019-01-28 10:23:54 +0000284 spin_lock(&obj->vma.lock);
Chris Wilson481a6f72017-01-16 15:21:31 +0000285 vma = vma_lookup(obj, vm, view);
Chris Wilson528cbd12019-01-28 10:23:54 +0000286 spin_unlock(&obj->vma.lock);
287
288 /* vma_create() will resolve the race if another creates the vma */
289 if (unlikely(!vma))
Chris Wilsona01cb37a2017-01-16 15:21:30 +0000290 vma = vma_create(obj, vm, view);
Chris Wilson718659a2017-01-16 15:21:28 +0000291
Chris Wilson4ea95272017-01-16 15:21:29 +0000292 GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
Chris Wilson718659a2017-01-16 15:21:28 +0000293 return vma;
294}
295
296/**
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200297 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
298 * @vma: VMA to map
299 * @cache_level: mapping cache level
300 * @flags: flags like global or local mapping
301 *
302 * DMA addresses are taken from the scatter-gather table of this object (or of
303 * this VMA in case of non-default GGTT views) and PTE entries set up.
304 * Note that DMA addresses are also the only part of the SG table we care about.
305 */
306int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
307 u32 flags)
308{
309 u32 bind_flags;
310 u32 vma_flags;
311 int ret;
312
Chris Wilsonaa149432017-02-25 18:11:21 +0000313 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
314 GEM_BUG_ON(vma->size > vma->node.size);
315
Tvrtko Ursulinbbb8a9d2018-10-12 07:31:42 +0100316 if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start,
317 vma->node.size,
318 vma->vm->total)))
Chris Wilsonaa149432017-02-25 18:11:21 +0000319 return -ENODEV;
320
Tvrtko Ursulinbbb8a9d2018-10-12 07:31:42 +0100321 if (GEM_DEBUG_WARN_ON(!flags))
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200322 return -EINVAL;
323
324 bind_flags = 0;
325 if (flags & PIN_GLOBAL)
326 bind_flags |= I915_VMA_GLOBAL_BIND;
327 if (flags & PIN_USER)
328 bind_flags |= I915_VMA_LOCAL_BIND;
329
330 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
331 if (flags & PIN_UPDATE)
332 bind_flags |= vma_flags;
333 else
334 bind_flags &= ~vma_flags;
335 if (bind_flags == 0)
336 return 0;
337
Matthew Auldfa3f46a2017-10-06 23:18:19 +0100338 GEM_BUG_ON(!vma->pages);
339
Daniele Ceraolo Spurio6146e6d2017-01-20 13:51:23 -0800340 trace_i915_vma_bind(vma, bind_flags);
Chris Wilson93f2cde2018-06-07 16:40:46 +0100341 ret = vma->ops->bind_vma(vma, cache_level, bind_flags);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200342 if (ret)
343 return ret;
344
345 vma->flags |= bind_flags;
346 return 0;
347}
348
349void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
350{
351 void __iomem *ptr;
Chris Wilsonb4563f52017-10-09 09:43:55 +0100352 int err;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200353
354 /* Access through the GTT requires the device to be awake. */
Daniele Ceraolo Spurio87b391b92019-06-13 16:21:50 -0700355 assert_rpm_wakelock_held(&vma->vm->i915->runtime_pm);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200356
Chris Wilson49d73912016-11-29 09:50:08 +0000357 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilsonb4563f52017-10-09 09:43:55 +0100358 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
359 err = -ENODEV;
360 goto err;
361 }
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200362
363 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
364 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
365
366 ptr = vma->iomap;
367 if (ptr == NULL) {
Matthew Auld73ebd502017-12-11 15:18:20 +0000368 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200369 vma->node.start,
370 vma->node.size);
Chris Wilsonb4563f52017-10-09 09:43:55 +0100371 if (ptr == NULL) {
372 err = -ENOMEM;
373 goto err;
374 }
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200375
376 vma->iomap = ptr;
377 }
378
379 __i915_vma_pin(vma);
Chris Wilsonb4563f52017-10-09 09:43:55 +0100380
Chris Wilson3bd40732017-10-09 09:43:56 +0100381 err = i915_vma_pin_fence(vma);
Chris Wilsonb4563f52017-10-09 09:43:55 +0100382 if (err)
383 goto err_unpin;
384
Chris Wilson7125397b2017-12-06 12:49:14 +0000385 i915_vma_set_ggtt_write(vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200386 return ptr;
Chris Wilsonb4563f52017-10-09 09:43:55 +0100387
388err_unpin:
389 __i915_vma_unpin(vma);
390err:
391 return IO_ERR_PTR(err);
392}
393
Chris Wilson7125397b2017-12-06 12:49:14 +0000394void i915_vma_flush_writes(struct i915_vma *vma)
395{
396 if (!i915_vma_has_ggtt_write(vma))
397 return;
398
Tvrtko Ursulina1c8a092019-06-21 08:08:01 +0100399 intel_gt_flush_ggtt_writes(vma->vm->gt);
Chris Wilson7125397b2017-12-06 12:49:14 +0000400
401 i915_vma_unset_ggtt_write(vma);
402}
403
Chris Wilsonb4563f52017-10-09 09:43:55 +0100404void i915_vma_unpin_iomap(struct i915_vma *vma)
405{
Chris Wilson520ea7c2018-06-07 16:40:45 +0100406 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilsonb4563f52017-10-09 09:43:55 +0100407
408 GEM_BUG_ON(vma->iomap == NULL);
409
Chris Wilson7125397b2017-12-06 12:49:14 +0000410 i915_vma_flush_writes(vma);
411
Chris Wilsonb4563f52017-10-09 09:43:55 +0100412 i915_vma_unpin_fence(vma);
413 i915_vma_unpin(vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200414}
415
Chris Wilson6a2f59e2018-07-21 13:50:37 +0100416void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags)
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200417{
418 struct i915_vma *vma;
419 struct drm_i915_gem_object *obj;
420
421 vma = fetch_and_zero(p_vma);
422 if (!vma)
423 return;
424
425 obj = vma->obj;
Chris Wilson520ea7c2018-06-07 16:40:45 +0100426 GEM_BUG_ON(!obj);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200427
428 i915_vma_unpin(vma);
429 i915_vma_close(vma);
430
Chris Wilson6a2f59e2018-07-21 13:50:37 +0100431 if (flags & I915_VMA_RELEASE_MAP)
432 i915_gem_object_unpin_map(obj);
433
Chris Wilsonc017cf62019-05-28 10:29:56 +0100434 i915_gem_object_put(obj);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200435}
436
Chris Wilson782a3e92017-02-13 17:15:46 +0000437bool i915_vma_misplaced(const struct i915_vma *vma,
438 u64 size, u64 alignment, u64 flags)
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200439{
440 if (!drm_mm_node_allocated(&vma->node))
441 return false;
442
443 if (vma->node.size < size)
444 return true;
445
Chris Wilsonf51455d2017-01-10 14:47:34 +0000446 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
447 if (alignment && !IS_ALIGNED(vma->node.start, alignment))
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200448 return true;
449
450 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
451 return true;
452
453 if (flags & PIN_OFFSET_BIAS &&
454 vma->node.start < (flags & PIN_OFFSET_MASK))
455 return true;
456
457 if (flags & PIN_OFFSET_FIXED &&
458 vma->node.start != (flags & PIN_OFFSET_MASK))
459 return true;
460
461 return false;
462}
463
464void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
465{
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200466 bool mappable, fenceable;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200467
Chris Wilson944397f2017-01-09 16:16:11 +0000468 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
469 GEM_BUG_ON(!vma->fence_size);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200470
Chris Wilson944397f2017-01-09 16:16:11 +0000471 fenceable = (vma->node.size >= vma->fence_size &&
Chris Wilsonf51455d2017-01-10 14:47:34 +0000472 IS_ALIGNED(vma->node.start, vma->fence_alignment));
Chris Wilson944397f2017-01-09 16:16:11 +0000473
474 mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
475
476 if (mappable && fenceable)
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200477 vma->flags |= I915_VMA_CAN_FENCE;
478 else
479 vma->flags &= ~I915_VMA_CAN_FENCE;
480}
481
Chris Wilson7d1d9ae2016-12-05 14:29:38 +0000482static bool color_differs(struct drm_mm_node *node, unsigned long color)
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200483{
Chris Wilson7d1d9ae2016-12-05 14:29:38 +0000484 return node->allocated && node->color != color;
485}
486
487bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
488{
489 struct drm_mm_node *node = &vma->node;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200490 struct drm_mm_node *other;
491
492 /*
493 * On some machines we have to be careful when putting differing types
494 * of snoopable memory together to avoid the prefetcher crossing memory
495 * domains and dying. During vm initialisation, we decide whether or not
496 * these constraints apply and set the drm_mm.color_adjust
497 * appropriately.
498 */
499 if (vma->vm->mm.color_adjust == NULL)
500 return true;
501
Chris Wilson7d1d9ae2016-12-05 14:29:38 +0000502 /* Only valid to be called on an already inserted vma */
503 GEM_BUG_ON(!drm_mm_node_allocated(node));
504 GEM_BUG_ON(list_empty(&node->node_list));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200505
Chris Wilson7d1d9ae2016-12-05 14:29:38 +0000506 other = list_prev_entry(node, node_list);
Daniel Vetteref426c12017-01-04 11:41:10 +0100507 if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200508 return false;
509
Chris Wilson7d1d9ae2016-12-05 14:29:38 +0000510 other = list_next_entry(node, node_list);
Daniel Vetteref426c12017-01-04 11:41:10 +0100511 if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200512 return false;
513
514 return true;
515}
516
Chris Wilson83d317a2018-06-05 10:41:07 +0100517static void assert_bind_count(const struct drm_i915_gem_object *obj)
518{
519 /*
520 * Combine the assertion that the object is bound and that we have
521 * pinned its pages. But we should never have bound the object
522 * more than we have pinned its pages. (For complete accuracy, we
523 * assume that no else is pinning the pages, but as a rough assertion
524 * that we will not run into problems later, this will do!)
525 */
Chris Wilsonecab9be2019-06-12 11:57:20 +0100526 GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < atomic_read(&obj->bind_count));
Chris Wilson83d317a2018-06-05 10:41:07 +0100527}
528
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200529/**
530 * i915_vma_insert - finds a slot for the vma in its address space
531 * @vma: the vma
532 * @size: requested size in bytes (can be larger than the VMA)
533 * @alignment: required alignment
534 * @flags: mask of PIN_* flags to use
535 *
536 * First we try to allocate some free space that meets the requirements for
537 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
538 * preferrably the oldest idle entry to make room for the new VMA.
539 *
540 * Returns:
541 * 0 on success, negative error code otherwise.
542 */
543static int
544i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
545{
Chris Wilson49d73912016-11-29 09:50:08 +0000546 struct drm_i915_private *dev_priv = vma->vm->i915;
Chris Wilson520ea7c2018-06-07 16:40:45 +0100547 unsigned int cache_level;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200548 u64 start, end;
549 int ret;
550
Chris Wilson010e3e62017-12-06 12:49:13 +0000551 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200552 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
553 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
554
555 size = max(size, vma->size);
Chris Wilson944397f2017-01-09 16:16:11 +0000556 alignment = max(alignment, vma->display_alignment);
557 if (flags & PIN_MAPPABLE) {
558 size = max_t(typeof(size), size, vma->fence_size);
559 alignment = max_t(typeof(alignment),
560 alignment, vma->fence_alignment);
561 }
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200562
Chris Wilsonf51455d2017-01-10 14:47:34 +0000563 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
564 GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
565 GEM_BUG_ON(!is_power_of_2(alignment));
566
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200567 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
Chris Wilsonf51455d2017-01-10 14:47:34 +0000568 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200569
570 end = vma->vm->total;
571 if (flags & PIN_MAPPABLE)
572 end = min_t(u64, end, dev_priv->ggtt.mappable_end);
573 if (flags & PIN_ZONE_4G)
Chris Wilsonf51455d2017-01-10 14:47:34 +0000574 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
575 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200576
577 /* If binding the object/GGTT view requires more space than the entire
578 * aperture has, reject it early before evicting everything in a vain
579 * attempt to find space.
580 */
581 if (size > end) {
Chris Wilson520ea7c2018-06-07 16:40:45 +0100582 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n",
583 size, flags & PIN_MAPPABLE ? "mappable" : "total",
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200584 end);
Chris Wilson2889caa2017-06-16 15:05:19 +0100585 return -ENOSPC;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200586 }
587
Chris Wilson520ea7c2018-06-07 16:40:45 +0100588 if (vma->obj) {
589 ret = i915_gem_object_pin_pages(vma->obj);
590 if (ret)
591 return ret;
592
593 cache_level = vma->obj->cache_level;
594 } else {
595 cache_level = 0;
596 }
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200597
Matthew Auldfa3f46a2017-10-06 23:18:19 +0100598 GEM_BUG_ON(vma->pages);
599
Chris Wilson93f2cde2018-06-07 16:40:46 +0100600 ret = vma->ops->set_pages(vma);
Matthew Auldfa3f46a2017-10-06 23:18:19 +0100601 if (ret)
602 goto err_unpin;
603
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200604 if (flags & PIN_OFFSET_FIXED) {
605 u64 offset = flags & PIN_OFFSET_MASK;
Chris Wilsonf51455d2017-01-10 14:47:34 +0000606 if (!IS_ALIGNED(offset, alignment) ||
Chris Wilsone8f9ae92017-01-06 15:20:12 +0000607 range_overflows(offset, size, end)) {
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200608 ret = -EINVAL;
Matthew Auldfa3f46a2017-10-06 23:18:19 +0100609 goto err_clear;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200610 }
611
Chris Wilson625d9882017-01-11 11:23:11 +0000612 ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
Chris Wilson520ea7c2018-06-07 16:40:45 +0100613 size, offset, cache_level,
Chris Wilson625d9882017-01-11 11:23:11 +0000614 flags);
615 if (ret)
Matthew Auldfa3f46a2017-10-06 23:18:19 +0100616 goto err_clear;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200617 } else {
Matthew Auld74642842017-10-06 23:18:20 +0100618 /*
619 * We only support huge gtt pages through the 48b PPGTT,
620 * however we also don't want to force any alignment for
621 * objects which need to be tightly packed into the low 32bits.
622 *
623 * Note that we assume that GGTT are limited to 4GiB for the
624 * forseeable future. See also i915_ggtt_offset().
625 */
626 if (upper_32_bits(end - 1) &&
627 vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
Matthew Auld855822b2017-10-06 23:18:21 +0100628 /*
629 * We can't mix 64K and 4K PTEs in the same page-table
630 * (2M block), and so to avoid the ugliness and
631 * complexity of coloring we opt for just aligning 64K
632 * objects to 2M.
633 */
Matthew Auld74642842017-10-06 23:18:20 +0100634 u64 page_alignment =
Matthew Auld855822b2017-10-06 23:18:21 +0100635 rounddown_pow_of_two(vma->page_sizes.sg |
636 I915_GTT_PAGE_SIZE_2M);
Matthew Auld74642842017-10-06 23:18:20 +0100637
Chris Wilsonbef27bdb2017-10-09 10:20:19 +0100638 /*
639 * Check we don't expand for the limited Global GTT
640 * (mappable aperture is even more precious!). This
641 * also checks that we exclude the aliasing-ppgtt.
642 */
643 GEM_BUG_ON(i915_vma_is_ggtt(vma));
644
Matthew Auld74642842017-10-06 23:18:20 +0100645 alignment = max(alignment, page_alignment);
Matthew Auld855822b2017-10-06 23:18:21 +0100646
647 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
648 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
Matthew Auld74642842017-10-06 23:18:20 +0100649 }
650
Chris Wilsone007b192017-01-11 11:23:10 +0000651 ret = i915_gem_gtt_insert(vma->vm, &vma->node,
Chris Wilson520ea7c2018-06-07 16:40:45 +0100652 size, alignment, cache_level,
Chris Wilsone007b192017-01-11 11:23:10 +0000653 start, end, flags);
654 if (ret)
Matthew Auldfa3f46a2017-10-06 23:18:19 +0100655 goto err_clear;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200656
657 GEM_BUG_ON(vma->node.start < start);
658 GEM_BUG_ON(vma->node.start + vma->node.size > end);
659 }
Chris Wilson44a0ec02017-01-19 19:26:58 +0000660 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
Chris Wilson520ea7c2018-06-07 16:40:45 +0100661 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, cache_level));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200662
Chris Wilson09d7e462019-01-28 10:23:53 +0000663 mutex_lock(&vma->vm->mutex);
Chris Wilson499197d2019-01-28 10:23:52 +0000664 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
Chris Wilson09d7e462019-01-28 10:23:53 +0000665 mutex_unlock(&vma->vm->mutex);
Chris Wilsonf2123812017-10-16 12:40:37 +0100666
Chris Wilson520ea7c2018-06-07 16:40:45 +0100667 if (vma->obj) {
Chris Wilsonecab9be2019-06-12 11:57:20 +0100668 atomic_inc(&vma->obj->bind_count);
669 assert_bind_count(vma->obj);
Chris Wilson520ea7c2018-06-07 16:40:45 +0100670 }
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200671
672 return 0;
673
Matthew Auldfa3f46a2017-10-06 23:18:19 +0100674err_clear:
Chris Wilson93f2cde2018-06-07 16:40:46 +0100675 vma->ops->clear_pages(vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200676err_unpin:
Chris Wilson520ea7c2018-06-07 16:40:45 +0100677 if (vma->obj)
678 i915_gem_object_unpin_pages(vma->obj);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200679 return ret;
680}
681
Chris Wilson31c7eff2017-02-27 12:26:54 +0000682static void
683i915_vma_remove(struct i915_vma *vma)
684{
Chris Wilson31c7eff2017-02-27 12:26:54 +0000685 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
686 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
687
Chris Wilson93f2cde2018-06-07 16:40:46 +0100688 vma->ops->clear_pages(vma);
Matthew Auldfa3f46a2017-10-06 23:18:19 +0100689
Chris Wilson09d7e462019-01-28 10:23:53 +0000690 mutex_lock(&vma->vm->mutex);
Chris Wilson31c7eff2017-02-27 12:26:54 +0000691 drm_mm_remove_node(&vma->node);
692 list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
Chris Wilson09d7e462019-01-28 10:23:53 +0000693 mutex_unlock(&vma->vm->mutex);
Chris Wilson31c7eff2017-02-27 12:26:54 +0000694
Chris Wilson520ea7c2018-06-07 16:40:45 +0100695 /*
696 * Since the unbound list is global, only move to that list if
Chris Wilson31c7eff2017-02-27 12:26:54 +0000697 * no more VMAs exist.
698 */
Chris Wilson520ea7c2018-06-07 16:40:45 +0100699 if (vma->obj) {
700 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilson31c7eff2017-02-27 12:26:54 +0000701
Chris Wilsonecab9be2019-06-12 11:57:20 +0100702 atomic_dec(&obj->bind_count);
Chris Wilson520ea7c2018-06-07 16:40:45 +0100703
704 /*
705 * And finally now the object is completely decoupled from this
706 * vma, we can drop its hold on the backing storage and allow
707 * it to be reaped by the shrinker.
708 */
709 i915_gem_object_unpin_pages(obj);
710 assert_bind_count(obj);
711 }
Chris Wilson31c7eff2017-02-27 12:26:54 +0000712}
713
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200714int __i915_vma_do_pin(struct i915_vma *vma,
715 u64 size, u64 alignment, u64 flags)
716{
Chris Wilson31c7eff2017-02-27 12:26:54 +0000717 const unsigned int bound = vma->flags;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200718 int ret;
719
Chris Wilson49d73912016-11-29 09:50:08 +0000720 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200721 GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
722 GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
723
724 if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
725 ret = -EBUSY;
Chris Wilson31c7eff2017-02-27 12:26:54 +0000726 goto err_unpin;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200727 }
728
729 if ((bound & I915_VMA_BIND_MASK) == 0) {
730 ret = i915_vma_insert(vma, size, alignment, flags);
731 if (ret)
Chris Wilson31c7eff2017-02-27 12:26:54 +0000732 goto err_unpin;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200733 }
Chris Wilsond36caee2017-11-05 12:45:50 +0000734 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200735
Chris Wilson520ea7c2018-06-07 16:40:45 +0100736 ret = i915_vma_bind(vma, vma->obj ? vma->obj->cache_level : 0, flags);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200737 if (ret)
Chris Wilson31c7eff2017-02-27 12:26:54 +0000738 goto err_remove;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200739
Chris Wilsond36caee2017-11-05 12:45:50 +0000740 GEM_BUG_ON((vma->flags & I915_VMA_BIND_MASK) == 0);
741
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200742 if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
743 __i915_vma_set_map_and_fenceable(vma);
744
745 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
746 return 0;
747
Chris Wilson31c7eff2017-02-27 12:26:54 +0000748err_remove:
749 if ((bound & I915_VMA_BIND_MASK) == 0) {
Chris Wilson31c7eff2017-02-27 12:26:54 +0000750 i915_vma_remove(vma);
Matthew Auldfa3f46a2017-10-06 23:18:19 +0100751 GEM_BUG_ON(vma->pages);
Chris Wilsond36caee2017-11-05 12:45:50 +0000752 GEM_BUG_ON(vma->flags & I915_VMA_BIND_MASK);
Chris Wilson31c7eff2017-02-27 12:26:54 +0000753 }
754err_unpin:
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200755 __i915_vma_unpin(vma);
756 return ret;
757}
758
Chris Wilson3365e222018-05-03 20:51:14 +0100759void i915_vma_close(struct i915_vma *vma)
760{
Chris Wilson155ab882019-06-06 12:23:20 +0100761 struct drm_i915_private *i915 = vma->vm->i915;
762 unsigned long flags;
Chris Wilson3365e222018-05-03 20:51:14 +0100763
764 GEM_BUG_ON(i915_vma_is_closed(vma));
Chris Wilson3365e222018-05-03 20:51:14 +0100765
766 /*
767 * We defer actually closing, unbinding and destroying the VMA until
768 * the next idle point, or if the object is freed in the meantime. By
769 * postponing the unbind, we allow for it to be resurrected by the
770 * client, avoiding the work required to rebind the VMA. This is
771 * advantageous for DRI, where the client/server pass objects
772 * between themselves, temporarily opening a local VMA to the
773 * object, and then closing it again. The same object is then reused
774 * on the next frame (or two, depending on the depth of the swap queue)
775 * causing us to rebind the VMA once more. This ends up being a lot
776 * of wasted work for the steady state.
777 */
Chris Wilson155ab882019-06-06 12:23:20 +0100778 spin_lock_irqsave(&i915->gt.closed_lock, flags);
779 list_add(&vma->closed_link, &i915->gt.closed_vma);
780 spin_unlock_irqrestore(&i915->gt.closed_lock, flags);
781}
782
783static void __i915_vma_remove_closed(struct i915_vma *vma)
784{
785 struct drm_i915_private *i915 = vma->vm->i915;
786
787 if (!i915_vma_is_closed(vma))
788 return;
789
790 spin_lock_irq(&i915->gt.closed_lock);
791 list_del_init(&vma->closed_link);
792 spin_unlock_irq(&i915->gt.closed_lock);
Chris Wilson3365e222018-05-03 20:51:14 +0100793}
794
795void i915_vma_reopen(struct i915_vma *vma)
796{
Chris Wilson155ab882019-06-06 12:23:20 +0100797 __i915_vma_remove_closed(vma);
Chris Wilson3365e222018-05-03 20:51:14 +0100798}
799
800static void __i915_vma_destroy(struct i915_vma *vma)
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200801{
802 GEM_BUG_ON(vma->node.allocated);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200803 GEM_BUG_ON(vma->fence);
804
Chris Wilson21950ee2019-02-05 13:00:05 +0000805 GEM_BUG_ON(i915_active_request_isset(&vma->last_fence));
Chris Wilson7a3bc032017-06-20 13:43:21 +0100806
Chris Wilson09d7e462019-01-28 10:23:53 +0000807 mutex_lock(&vma->vm->mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200808 list_del(&vma->vm_link);
Chris Wilson09d7e462019-01-28 10:23:53 +0000809 mutex_unlock(&vma->vm->mutex);
810
Chris Wilson528cbd12019-01-28 10:23:54 +0000811 if (vma->obj) {
812 struct drm_i915_gem_object *obj = vma->obj;
813
814 spin_lock(&obj->vma.lock);
815 list_del(&vma->obj_link);
816 rb_erase(&vma->obj_node, &vma->obj->vma.tree);
817 spin_unlock(&obj->vma.lock);
818 }
Chris Wilson010e3e62017-12-06 12:49:13 +0000819
Chris Wilson64d6c502019-02-05 13:00:02 +0000820 i915_active_fini(&vma->active);
Chris Wilson5c3f8c22018-07-06 11:39:46 +0100821
Chris Wilson13f1bfd2019-02-28 10:20:34 +0000822 i915_vma_free(vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200823}
824
Chris Wilson3365e222018-05-03 20:51:14 +0100825void i915_vma_destroy(struct i915_vma *vma)
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200826{
Chris Wilson3365e222018-05-03 20:51:14 +0100827 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200828
Chris Wilson3365e222018-05-03 20:51:14 +0100829 GEM_BUG_ON(i915_vma_is_pinned(vma));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200830
Chris Wilson155ab882019-06-06 12:23:20 +0100831 __i915_vma_remove_closed(vma);
Chris Wilson3365e222018-05-03 20:51:14 +0100832
833 WARN_ON(i915_vma_unbind(vma));
Chris Wilson6951e582019-05-28 10:29:51 +0100834 GEM_BUG_ON(i915_vma_is_active(vma));
835
Chris Wilson3365e222018-05-03 20:51:14 +0100836 __i915_vma_destroy(vma);
837}
838
839void i915_vma_parked(struct drm_i915_private *i915)
840{
841 struct i915_vma *vma, *next;
842
Chris Wilson155ab882019-06-06 12:23:20 +0100843 spin_lock_irq(&i915->gt.closed_lock);
Chris Wilson3365e222018-05-03 20:51:14 +0100844 list_for_each_entry_safe(vma, next, &i915->gt.closed_vma, closed_link) {
Chris Wilson155ab882019-06-06 12:23:20 +0100845 list_del_init(&vma->closed_link);
846 spin_unlock_irq(&i915->gt.closed_lock);
Chris Wilson3365e222018-05-03 20:51:14 +0100847
Chris Wilson155ab882019-06-06 12:23:20 +0100848 i915_vma_destroy(vma);
849
850 spin_lock_irq(&i915->gt.closed_lock);
851 }
852 spin_unlock_irq(&i915->gt.closed_lock);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200853}
854
855static void __i915_vma_iounmap(struct i915_vma *vma)
856{
857 GEM_BUG_ON(i915_vma_is_pinned(vma));
858
859 if (vma->iomap == NULL)
860 return;
861
862 io_mapping_unmap(vma->iomap);
863 vma->iomap = NULL;
864}
865
Chris Wilsona65adaf2017-10-09 09:43:57 +0100866void i915_vma_revoke_mmap(struct i915_vma *vma)
867{
868 struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
869 u64 vma_offset;
870
871 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
872
873 if (!i915_vma_has_userfault(vma))
874 return;
875
876 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
877 GEM_BUG_ON(!vma->obj->userfault_count);
878
879 vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
880 unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
881 drm_vma_node_offset_addr(node) + vma_offset,
882 vma->size,
883 1);
884
885 i915_vma_unset_userfault(vma);
886 if (!--vma->obj->userfault_count)
887 list_del(&vma->obj->userfault_link);
888}
889
Chris Wilsone6bb1d72018-07-06 11:39:45 +0100890int i915_vma_move_to_active(struct i915_vma *vma,
891 struct i915_request *rq,
892 unsigned int flags)
893{
894 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsona93615f2019-06-21 19:37:59 +0100895 int err;
Chris Wilsone6bb1d72018-07-06 11:39:45 +0100896
Chris Wilson6951e582019-05-28 10:29:51 +0100897 assert_vma_held(vma);
898 assert_object_held(obj);
Chris Wilsone6bb1d72018-07-06 11:39:45 +0100899 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
900
901 /*
902 * Add a reference if we're newly entering the active list.
903 * The order in which we add operations to the retirement queue is
904 * vital here: mark_active adds to the start of the callback list,
905 * such that subsequent callbacks are called first. Therefore we
906 * add the active reference first and queue for it to be dropped
907 * *last*.
908 */
Chris Wilsona93615f2019-06-21 19:37:59 +0100909 err = i915_active_ref(&vma->active, rq->fence.context, rq);
Chris Wilsona93615f2019-06-21 19:37:59 +0100910 if (unlikely(err))
911 return err;
Chris Wilsone6bb1d72018-07-06 11:39:45 +0100912
Chris Wilsone6bb1d72018-07-06 11:39:45 +0100913 if (flags & EXEC_OBJECT_WRITE) {
Chris Wilsone6bb1d72018-07-06 11:39:45 +0100914 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
Chris Wilson21950ee2019-02-05 13:00:05 +0000915 __i915_active_request_set(&obj->frontbuffer_write, rq);
Chris Wilsone6bb1d72018-07-06 11:39:45 +0100916
Chris Wilsoncd2a4ea2019-07-30 21:58:05 +0100917 reservation_object_add_excl_fence(vma->resv, &rq->fence);
918 obj->write_domain = I915_GEM_DOMAIN_RENDER;
Chris Wilsone6bb1d72018-07-06 11:39:45 +0100919 obj->read_domains = 0;
Chris Wilsoncd2a4ea2019-07-30 21:58:05 +0100920 } else {
921 err = reservation_object_reserve_shared(vma->resv, 1);
922 if (unlikely(err))
923 return err;
924
925 reservation_object_add_shared_fence(vma->resv, &rq->fence);
926 obj->write_domain = 0;
Chris Wilsone6bb1d72018-07-06 11:39:45 +0100927 }
928 obj->read_domains |= I915_GEM_GPU_DOMAINS;
Chris Wilsona93615f2019-06-21 19:37:59 +0100929 obj->mm.dirty = true;
Chris Wilsone6bb1d72018-07-06 11:39:45 +0100930
931 if (flags & EXEC_OBJECT_NEEDS_FENCE)
Chris Wilson21950ee2019-02-05 13:00:05 +0000932 __i915_active_request_set(&vma->last_fence, rq);
Chris Wilsone6bb1d72018-07-06 11:39:45 +0100933
Chris Wilsona93615f2019-06-21 19:37:59 +0100934 GEM_BUG_ON(!i915_vma_is_active(vma));
Chris Wilsone6bb1d72018-07-06 11:39:45 +0100935 return 0;
936}
937
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200938int i915_vma_unbind(struct i915_vma *vma)
939{
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200940 int ret;
941
Chris Wilson520ea7c2018-06-07 16:40:45 +0100942 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200943
Chris Wilson520ea7c2018-06-07 16:40:45 +0100944 /*
945 * First wait upon any activity as retiring the request may
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200946 * have side-effects such as unpinning or even unbinding this vma.
947 */
Chris Wilson7f017b12017-11-09 21:34:50 +0000948 might_sleep();
Chris Wilson5c3f8c22018-07-06 11:39:46 +0100949 if (i915_vma_is_active(vma)) {
Chris Wilson520ea7c2018-06-07 16:40:45 +0100950 /*
951 * When a closed VMA is retired, it is unbound - eek.
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200952 * In order to prevent it from being recursively closed,
953 * take a pin on the vma so that the second unbind is
954 * aborted.
955 *
956 * Even more scary is that the retire callback may free
957 * the object (last active vma). To prevent the explosion
958 * we defer the actual object free to a worker that can
959 * only proceed once it acquires the struct_mutex (which
960 * we currently hold, therefore it cannot free this object
961 * before we are finished).
962 */
963 __i915_vma_pin(vma);
964
Chris Wilson64d6c502019-02-05 13:00:02 +0000965 ret = i915_active_wait(&vma->active);
Chris Wilson8b293eb2018-07-06 13:31:57 +0100966 if (ret)
967 goto unpin;
968
Chris Wilson21950ee2019-02-05 13:00:05 +0000969 ret = i915_active_request_retire(&vma->last_fence,
970 &vma->vm->i915->drm.struct_mutex);
Chris Wilson5c3f8c22018-07-06 11:39:46 +0100971unpin:
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200972 __i915_vma_unpin(vma);
973 if (ret)
974 return ret;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200975 }
Chris Wilson7a3bc032017-06-20 13:43:21 +0100976 GEM_BUG_ON(i915_vma_is_active(vma));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200977
Chris Wilson10195b12018-06-28 14:22:06 +0100978 if (i915_vma_is_pinned(vma)) {
979 vma_print_allocator(vma, "is pinned");
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200980 return -EBUSY;
Chris Wilson10195b12018-06-28 14:22:06 +0100981 }
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200982
983 if (!drm_mm_node_allocated(&vma->node))
Chris Wilson3365e222018-05-03 20:51:14 +0100984 return 0;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200985
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200986 if (i915_vma_is_map_and_fenceable(vma)) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000987 /*
988 * Check that we have flushed all writes through the GGTT
989 * before the unbind, other due to non-strict nature of those
990 * indirect writes they may end up referencing the GGTT PTE
991 * after the unbind.
992 */
993 i915_vma_flush_writes(vma);
994 GEM_BUG_ON(i915_vma_has_ggtt_write(vma));
995
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200996 /* release the fence reg _after_ flushing */
997 ret = i915_vma_put_fence(vma);
998 if (ret)
999 return ret;
1000
1001 /* Force a pagefault for domain tracking on next user access */
Chris Wilsona65adaf2017-10-09 09:43:57 +01001002 i915_vma_revoke_mmap(vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02001003
1004 __i915_vma_iounmap(vma);
1005 vma->flags &= ~I915_VMA_CAN_FENCE;
1006 }
Chris Wilsona65adaf2017-10-09 09:43:57 +01001007 GEM_BUG_ON(vma->fence);
1008 GEM_BUG_ON(i915_vma_has_userfault(vma));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02001009
1010 if (likely(!vma->vm->closed)) {
1011 trace_i915_vma_unbind(vma);
Chris Wilson93f2cde2018-06-07 16:40:46 +01001012 vma->ops->unbind_vma(vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02001013 }
1014 vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
1015
Chris Wilson31c7eff2017-02-27 12:26:54 +00001016 i915_vma_remove(vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02001017
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02001018 return 0;
1019}
1020
Chris Wilson1aff1902019-08-02 22:21:36 +01001021struct i915_vma *i915_vma_make_unshrinkable(struct i915_vma *vma)
1022{
1023 i915_gem_object_make_unshrinkable(vma->obj);
1024 return vma;
1025}
1026
1027void i915_vma_make_shrinkable(struct i915_vma *vma)
1028{
1029 i915_gem_object_make_shrinkable(vma->obj);
1030}
1031
1032void i915_vma_make_purgeable(struct i915_vma *vma)
1033{
1034 i915_gem_object_make_purgeable(vma->obj);
1035}
1036
Chris Wilsone3c7a1c2017-02-13 17:15:45 +00001037#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1038#include "selftests/i915_vma.c"
1039#endif
Chris Wilson13f1bfd2019-02-28 10:20:34 +00001040
Chris Wilson103b76ee2019-03-05 21:38:30 +00001041static void i915_global_vma_shrink(void)
1042{
1043 kmem_cache_shrink(global.slab_vmas);
1044}
1045
1046static void i915_global_vma_exit(void)
1047{
1048 kmem_cache_destroy(global.slab_vmas);
1049}
1050
1051static struct i915_global_vma global = { {
1052 .shrink = i915_global_vma_shrink,
1053 .exit = i915_global_vma_exit,
1054} };
1055
Chris Wilson13f1bfd2019-02-28 10:20:34 +00001056int __init i915_global_vma_init(void)
1057{
1058 global.slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
1059 if (!global.slab_vmas)
1060 return -ENOMEM;
1061
Chris Wilson103b76ee2019-03-05 21:38:30 +00001062 i915_global_register(&global.base);
Chris Wilson13f1bfd2019-02-28 10:20:34 +00001063 return 0;
1064}