Home | History | Annotate | Line # | Download | only in gem
      1 /*	$NetBSD: i915_gem_object.c,v 1.9 2021/12/19 12:12:31 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright  2017 Intel Corporation
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the next
     14  * paragraph) shall be included in all copies or substantial portions of the
     15  * Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     23  * IN THE SOFTWARE.
     24  *
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: i915_gem_object.c,v 1.9 2021/12/19 12:12:31 riastradh Exp $");
     29 
     30 #include <linux/bitmap.h>
     31 #include <linux/sched/mm.h>
     32 
     33 #include "display/intel_frontbuffer.h"
     34 #include "gt/intel_gt.h"
     35 #include "i915_drv.h"
     36 #include "i915_gem_clflush.h"
     37 #include "i915_gem_context.h"
     38 #include "i915_gem_mman.h"
     39 #include "i915_gem_object.h"
     40 #include "i915_globals.h"
     41 #include "i915_trace.h"
     42 
     43 #include <linux/nbsd-namespace.h>
     44 
     45 static struct i915_global_object {
     46 	struct i915_global base;
     47 	struct kmem_cache *slab_objects;
     48 } global;
     49 
     50 struct drm_i915_gem_object *i915_gem_object_alloc(void)
     51 {
     52 	return kmem_cache_zalloc(global.slab_objects, GFP_KERNEL);
     53 }
     54 
     55 void i915_gem_object_free(struct drm_i915_gem_object *obj)
     56 {
     57 	return kmem_cache_free(global.slab_objects, obj);
     58 }
     59 
     60 void i915_gem_object_init(struct drm_i915_gem_object *obj,
     61 			  const struct drm_i915_gem_object_ops *ops,
     62 			  struct lock_class_key *key)
     63 {
     64 	__mutex_init(&obj->mm.lock, "obj->mm.lock", key);
     65 
     66 	spin_lock_init(&obj->vma.lock);
     67 	INIT_LIST_HEAD(&obj->vma.list);
     68 	i915_vma_tree_init(obj);
     69 
     70 	INIT_LIST_HEAD(&obj->mm.link);
     71 
     72 	INIT_LIST_HEAD(&obj->lut_list);
     73 
     74 	spin_lock_init(&obj->mmo.lock);
     75 #ifdef __NetBSD__
     76 	memset(obj->mmo.offsets, 0, sizeof(obj->mmo.offsets));
     77 #else
     78 	obj->mmo.offsets = RB_ROOT;
     79 #endif
     80 
     81 	init_rcu_head(&obj->rcu);
     82 
     83 	obj->ops = ops;
     84 
     85 	obj->mm.madv = I915_MADV_WILLNEED;
     86 #ifndef __NetBSD__
     87 	INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
     88 	mutex_init(&obj->mm.get_page.lock);
     89 #endif
     90 }
     91 
     92 /**
     93  * Mark up the object's coherency levels for a given cache_level
     94  * @obj: #drm_i915_gem_object
     95  * @cache_level: cache level
     96  */
     97 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
     98 					 unsigned int cache_level)
     99 {
    100 	obj->cache_level = cache_level;
    101 
    102 	if (cache_level != I915_CACHE_NONE)
    103 		obj->cache_coherent = (I915_BO_CACHE_COHERENT_FOR_READ |
    104 				       I915_BO_CACHE_COHERENT_FOR_WRITE);
    105 	else if (HAS_LLC(to_i915(obj->base.dev)))
    106 		obj->cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ;
    107 	else
    108 		obj->cache_coherent = 0;
    109 
    110 	obj->cache_dirty =
    111 		!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE);
    112 }
    113 
    114 void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
    115 {
    116 	struct drm_i915_gem_object *obj = to_intel_bo(gem);
    117 	struct drm_i915_file_private *fpriv = file->driver_priv;
    118 	struct i915_mmap_offset *mmo, *mn;
    119 	struct i915_lut_handle *lut, *ln;
    120 	LIST_HEAD(close);
    121 
    122 	i915_gem_object_lock(obj);
    123 	list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
    124 		struct i915_gem_context *ctx = lut->ctx;
    125 
    126 		if (ctx->file_priv != fpriv)
    127 			continue;
    128 
    129 		i915_gem_context_get(ctx);
    130 		list_move(&lut->obj_link, &close);
    131 	}
    132 	i915_gem_object_unlock(obj);
    133 
    134 #ifdef __NetBSD__
    135 	__USE(mn);
    136 	for (enum i915_mmap_type t = 0; t < I915_MMAP_NTYPES; t++) {
    137 		if ((mmo = obj->mmo.offsets[t]) == NULL)
    138 			continue;
    139 		drm_vma_node_revoke(&mmo->vma_node, file);
    140 	}
    141 #else
    142 	spin_lock(&obj->mmo.lock);
    143 	rbtree_postorder_for_each_entry_safe(mmo, mn, &obj->mmo.offsets, offset)
    144 		drm_vma_node_revoke(&mmo->vma_node, file);
    145 	spin_unlock(&obj->mmo.lock);
    146 #endif
    147 
    148 	list_for_each_entry_safe(lut, ln, &close, obj_link) {
    149 		struct i915_gem_context *ctx = lut->ctx;
    150 		struct i915_vma *vma;
    151 
    152 		/*
    153 		 * We allow the process to have multiple handles to the same
    154 		 * vma, in the same fd namespace, by virtue of flink/open.
    155 		 */
    156 
    157 		mutex_lock(&ctx->mutex);
    158 		vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
    159 		if (vma) {
    160 			GEM_BUG_ON(vma->obj != obj);
    161 			GEM_BUG_ON(!atomic_read(&vma->open_count));
    162 			if (atomic_dec_and_test(&vma->open_count) &&
    163 			    !i915_vma_is_ggtt(vma))
    164 				i915_vma_close(vma);
    165 		}
    166 		mutex_unlock(&ctx->mutex);
    167 
    168 		i915_gem_context_put(lut->ctx);
    169 		i915_lut_handle_free(lut);
    170 		i915_gem_object_put(obj);
    171 	}
    172 }
    173 
    174 static void __i915_gem_free_object_rcu(struct rcu_head *head)
    175 {
    176 	struct drm_i915_gem_object *obj =
    177 		container_of(head, typeof(*obj), rcu);
    178 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
    179 
    180 	/* i915_gem_object_init */
    181 	spin_lock_destroy(&obj->mmo.lock);
    182 	spin_lock_destroy(&obj->vma.lock);
    183 	mutex_destroy(&obj->mm.lock);
    184 
    185 	dma_resv_fini(&obj->base._resv);
    186 	drm_vma_node_destroy(&obj->base.vma_node);
    187 	i915_gem_object_free(obj);
    188 
    189 	GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
    190 	atomic_dec(&i915->mm.free_count);
    191 }
    192 
    193 static void __i915_gem_free_objects(struct drm_i915_private *i915,
    194 				    struct llist_node *freed)
    195 {
    196 	struct drm_i915_gem_object *obj, *on;
    197 	intel_wakeref_t wakeref;
    198 
    199 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
    200 	llist_for_each_entry_safe(obj, on, freed, freed) {
    201 		struct i915_mmap_offset *mmo, *mn;
    202 
    203 		trace_i915_gem_object_destroy(obj);
    204 
    205 		if (!list_empty(&obj->vma.list)) {
    206 			struct i915_vma *vma;
    207 
    208 			/*
    209 			 * Note that the vma keeps an object reference while
    210 			 * it is active, so it *should* not sleep while we
    211 			 * destroy it. Our debug code errs insits it *might*.
    212 			 * For the moment, play along.
    213 			 */
    214 			spin_lock(&obj->vma.lock);
    215 			while ((vma = list_first_entry_or_null(&obj->vma.list,
    216 							       struct i915_vma,
    217 							       obj_link))) {
    218 				GEM_BUG_ON(vma->obj != obj);
    219 				spin_unlock(&obj->vma.lock);
    220 
    221 				__i915_vma_put(vma);
    222 
    223 				spin_lock(&obj->vma.lock);
    224 			}
    225 			spin_unlock(&obj->vma.lock);
    226 		}
    227 
    228 		i915_gem_object_release_mmap(obj);
    229 
    230 #ifdef __NetBSD__
    231 		__USE(mn);
    232 		for (enum i915_mmap_type t = 0; t < I915_MMAP_NTYPES; t++) {
    233 			if ((mmo = obj->mmo.offsets[t]) == NULL)
    234 				continue;
    235 			drm_vma_offset_remove(obj->base.dev->vma_offset_manager,
    236 					      &mmo->vma_node);
    237 			drm_vma_node_destroy(&mmo->vma_node);
    238 			kfree(mmo);
    239 		}
    240 		memset(obj->mmo.offsets, 0, sizeof(obj->mmo.offsets));
    241 #else
    242 		rbtree_postorder_for_each_entry_safe(mmo, mn,
    243 						     &obj->mmo.offsets,
    244 						     offset) {
    245 			drm_vma_offset_remove(obj->base.dev->vma_offset_manager,
    246 					      &mmo->vma_node);
    247 			kfree(mmo);
    248 		}
    249 		obj->mmo.offsets = RB_ROOT;
    250 #endif
    251 
    252 		GEM_BUG_ON(atomic_read(&obj->bind_count));
    253 		GEM_BUG_ON(obj->userfault_count);
    254 		GEM_BUG_ON(!list_empty(&obj->lut_list));
    255 
    256 		atomic_set(&obj->mm.pages_pin_count, 0);
    257 		__i915_gem_object_put_pages(obj);
    258 		GEM_BUG_ON(i915_gem_object_has_pages(obj));
    259 		bitmap_free(obj->bit_17);
    260 
    261 		if (obj->base.import_attach)
    262 			drm_prime_gem_destroy(&obj->base, NULL);
    263 
    264 		drm_gem_free_mmap_offset(&obj->base);
    265 
    266 		if (obj->ops->release)
    267 			obj->ops->release(obj);
    268 
    269 		/* But keep the pointer alive for RCU-protected lookups */
    270 		call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
    271 	}
    272 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
    273 }
    274 
    275 void i915_gem_flush_free_objects(struct drm_i915_private *i915)
    276 {
    277 	struct llist_node *freed = llist_del_all(&i915->mm.free_list);
    278 
    279 	if (unlikely(freed))
    280 		__i915_gem_free_objects(i915, freed);
    281 }
    282 
    283 static void __i915_gem_free_work(struct work_struct *work)
    284 {
    285 	struct drm_i915_private *i915 =
    286 		container_of(work, struct drm_i915_private, mm.free_work);
    287 
    288 	i915_gem_flush_free_objects(i915);
    289 }
    290 
    291 void i915_gem_free_object(struct drm_gem_object *gem_obj)
    292 {
    293 	struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
    294 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
    295 
    296 	GEM_BUG_ON(i915_gem_object_is_framebuffer(obj));
    297 
    298 	/*
    299 	 * Before we free the object, make sure any pure RCU-only
    300 	 * read-side critical sections are complete, e.g.
    301 	 * i915_gem_busy_ioctl(). For the corresponding synchronized
    302 	 * lookup see i915_gem_object_lookup_rcu().
    303 	 */
    304 	atomic_inc(&i915->mm.free_count);
    305 
    306 	/*
    307 	 * This serializes freeing with the shrinker. Since the free
    308 	 * is delayed, first by RCU then by the workqueue, we want the
    309 	 * shrinker to be able to free pages of unreferenced objects,
    310 	 * or else we may oom whilst there are plenty of deferred
    311 	 * freed objects.
    312 	 */
    313 	i915_gem_object_make_unshrinkable(obj);
    314 
    315 	/*
    316 	 * Since we require blocking on struct_mutex to unbind the freed
    317 	 * object from the GPU before releasing resources back to the
    318 	 * system, we can not do that directly from the RCU callback (which may
    319 	 * be a softirq context), but must instead then defer that work onto a
    320 	 * kthread. We use the RCU callback rather than move the freed object
    321 	 * directly onto the work queue so that we can mix between using the
    322 	 * worker and performing frees directly from subsequent allocations for
    323 	 * crude but effective memory throttling.
    324 	 */
    325 	if (llist_add(&obj->freed, &i915->mm.free_list))
    326 		queue_work(i915->wq, &i915->mm.free_work);
    327 }
    328 
    329 static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
    330 {
    331 	return !(obj->cache_level == I915_CACHE_NONE ||
    332 		 obj->cache_level == I915_CACHE_WT);
    333 }
    334 
    335 void
    336 i915_gem_object_flush_write_domain(struct drm_i915_gem_object *obj,
    337 				   unsigned int flush_domains)
    338 {
    339 	struct i915_vma *vma;
    340 
    341 	assert_object_held(obj);
    342 
    343 	if (!(obj->write_domain & flush_domains))
    344 		return;
    345 
    346 	switch (obj->write_domain) {
    347 	case I915_GEM_DOMAIN_GTT:
    348 		spin_lock(&obj->vma.lock);
    349 		for_each_ggtt_vma(vma, obj) {
    350 			if (i915_vma_unset_ggtt_write(vma))
    351 				intel_gt_flush_ggtt_writes(vma->vm->gt);
    352 		}
    353 		spin_unlock(&obj->vma.lock);
    354 
    355 		i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
    356 		break;
    357 
    358 	case I915_GEM_DOMAIN_WC:
    359 		wmb();
    360 		break;
    361 
    362 	case I915_GEM_DOMAIN_CPU:
    363 		i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
    364 		break;
    365 
    366 	case I915_GEM_DOMAIN_RENDER:
    367 		if (gpu_write_needs_clflush(obj))
    368 			obj->cache_dirty = true;
    369 		break;
    370 	}
    371 
    372 	obj->write_domain = 0;
    373 }
    374 
    375 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
    376 					 enum fb_op_origin origin)
    377 {
    378 	struct intel_frontbuffer *front;
    379 
    380 	front = __intel_frontbuffer_get(obj);
    381 	if (front) {
    382 		intel_frontbuffer_flush(front, origin);
    383 		intel_frontbuffer_put(front);
    384 	}
    385 }
    386 
    387 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
    388 					      enum fb_op_origin origin)
    389 {
    390 	struct intel_frontbuffer *front;
    391 
    392 	front = __intel_frontbuffer_get(obj);
    393 	if (front) {
    394 		intel_frontbuffer_invalidate(front, origin);
    395 		intel_frontbuffer_put(front);
    396 	}
    397 }
    398 
    399 void i915_gem_init__objects(struct drm_i915_private *i915)
    400 {
    401 	INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
    402 }
    403 
    404 static void i915_global_objects_shrink(void)
    405 {
    406 	kmem_cache_shrink(global.slab_objects);
    407 }
    408 
    409 static void i915_global_objects_exit(void)
    410 {
    411 	kmem_cache_destroy(global.slab_objects);
    412 }
    413 
    414 static struct i915_global_object global = { {
    415 	.shrink = i915_global_objects_shrink,
    416 	.exit = i915_global_objects_exit,
    417 } };
    418 
    419 int __init i915_global_objects_init(void)
    420 {
    421 	global.slab_objects =
    422 		KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
    423 	if (!global.slab_objects)
    424 		return -ENOMEM;
    425 
    426 	i915_global_register(&global.base);
    427 	return 0;
    428 }
    429 
    430 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
    431 #include "selftests/huge_gem_object.c"
    432 #include "selftests/huge_pages.c"
    433 #include "selftests/i915_gem_object.c"
    434 #include "selftests/i915_gem_coherency.c"
    435 #endif
    436