Home | History | Annotate | Line # | Download | only in i915
i915_gem.c revision 1.62
      1 /*	$NetBSD: i915_gem.c,v 1.62 2021/12/18 23:45:28 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright  2008-2015 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  * Authors:
     26  *    Eric Anholt <eric (at) anholt.net>
     27  *
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: i915_gem.c,v 1.62 2021/12/18 23:45:28 riastradh Exp $");
     32 
     33 #ifdef __NetBSD__
     34 #if 0				/* XXX uvmhist option?  */
     35 #include "opt_uvmhist.h"
     36 #endif
     37 
     38 #include <sys/types.h>
     39 #include <sys/param.h>
     40 
     41 #include <uvm/uvm.h>
     42 #include <uvm/uvm_extern.h>
     43 #include <uvm/uvm_fault.h>
     44 #include <uvm/uvm_page.h>
     45 #include <uvm/uvm_pmap.h>
     46 #include <uvm/uvm_prot.h>
     47 
     48 #include <drm/bus_dma_hacks.h>
     49 #endif
     50 
     51 #include <drm/drm_vma_manager.h>
     52 #include <drm/i915_drm.h>
     53 #include <linux/dma-fence-array.h>
     54 #include <linux/kthread.h>
     55 #include <linux/dma-resv.h>
     56 #include <linux/shmem_fs.h>
     57 #include <linux/slab.h>
     58 #include <linux/stop_machine.h>
     59 #include <linux/swap.h>
     60 #include <linux/pci.h>
     61 #include <linux/dma-buf.h>
     62 #include <linux/mman.h>
     63 #include <linux/nbsd-namespace.h>
     64 
     65 #include "display/intel_display.h"
     66 #include "display/intel_frontbuffer.h"
     67 
     68 #include "gem/i915_gem_clflush.h"
     69 #include "gem/i915_gem_context.h"
     70 #include "gem/i915_gem_ioctls.h"
     71 #include "gem/i915_gem_mman.h"
     72 #include "gem/i915_gem_region.h"
     73 #include "gt/intel_engine_user.h"
     74 #include "gt/intel_gt.h"
     75 #include "gt/intel_gt_pm.h"
     76 #include "gt/intel_workarounds.h"
     77 
     78 #include "i915_drv.h"
     79 #include "i915_trace.h"
     80 #include "i915_vgpu.h"
     81 
     82 #include "intel_pm.h"
     83 
     84 static int
     85 insert_mappable_node(struct i915_ggtt *ggtt, struct drm_mm_node *node, u32 size)
     86 {
     87 	int err;
     88 
     89 	err = mutex_lock_interruptible(&ggtt->vm.mutex);
     90 	if (err)
     91 		return err;
     92 
     93 	memset(node, 0, sizeof(*node));
     94 	err = drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
     95 					  size, 0, I915_COLOR_UNEVICTABLE,
     96 					  0, ggtt->mappable_end,
     97 					  DRM_MM_INSERT_LOW);
     98 
     99 	mutex_unlock(&ggtt->vm.mutex);
    100 
    101 	return err;
    102 }
    103 
    104 static void
    105 remove_mappable_node(struct i915_ggtt *ggtt, struct drm_mm_node *node)
    106 {
    107 	mutex_lock(&ggtt->vm.mutex);
    108 	drm_mm_remove_node(node);
    109 	mutex_unlock(&ggtt->vm.mutex);
    110 }
    111 
    112 int
    113 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
    114 			    struct drm_file *file)
    115 {
    116 	struct i915_ggtt *ggtt = &to_i915(dev)->ggtt;
    117 	struct drm_i915_gem_get_aperture *args = data;
    118 	struct i915_vma *vma;
    119 	u64 pinned;
    120 
    121 	if (mutex_lock_interruptible(&ggtt->vm.mutex))
    122 		return -EINTR;
    123 
    124 	pinned = ggtt->vm.reserved;
    125 	list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
    126 		if (i915_vma_is_pinned(vma))
    127 			pinned += vma->node.size;
    128 
    129 	mutex_unlock(&ggtt->vm.mutex);
    130 
    131 	args->aper_size = ggtt->vm.total;
    132 	args->aper_available_size = args->aper_size - pinned;
    133 
    134 	return 0;
    135 }
    136 
    137 int i915_gem_object_unbind(struct drm_i915_gem_object *obj,
    138 			   unsigned long flags)
    139 {
    140 	struct intel_runtime_pm *rpm = &to_i915(obj->base.dev)->runtime_pm;
    141 	LIST_HEAD(still_in_list);
    142 	intel_wakeref_t wakeref;
    143 	struct i915_vma *vma;
    144 	int ret;
    145 
    146 	if (!atomic_read(&obj->bind_count))
    147 		return 0;
    148 
    149 	/*
    150 	 * As some machines use ACPI to handle runtime-resume callbacks, and
    151 	 * ACPI is quite kmalloc happy, we cannot resume beneath the vm->mutex
    152 	 * as they are required by the shrinker. Ergo, we wake the device up
    153 	 * first just in case.
    154 	 */
    155 	wakeref = intel_runtime_pm_get(rpm);
    156 
    157 try_again:
    158 	ret = 0;
    159 	spin_lock(&obj->vma.lock);
    160 	while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
    161 						       struct i915_vma,
    162 						       obj_link))) {
    163 		struct i915_address_space *vm = vma->vm;
    164 
    165 		list_move_tail(&vma->obj_link, &still_in_list);
    166 		if (!i915_vma_is_bound(vma, I915_VMA_BIND_MASK))
    167 			continue;
    168 
    169 		ret = -EAGAIN;
    170 		if (!i915_vm_tryopen(vm))
    171 			break;
    172 
    173 		/* Prevent vma being freed by i915_vma_parked as we unbind */
    174 		vma = __i915_vma_get(vma);
    175 		spin_unlock(&obj->vma.lock);
    176 
    177 		if (vma) {
    178 			ret = -EBUSY;
    179 			if (flags & I915_GEM_OBJECT_UNBIND_ACTIVE ||
    180 			    !i915_vma_is_active(vma))
    181 				ret = i915_vma_unbind(vma);
    182 
    183 			__i915_vma_put(vma);
    184 		}
    185 
    186 		i915_vm_close(vm);
    187 		spin_lock(&obj->vma.lock);
    188 	}
    189 	list_splice_init(&still_in_list, &obj->vma.list);
    190 	spin_unlock(&obj->vma.lock);
    191 
    192 	if (ret == -EAGAIN && flags & I915_GEM_OBJECT_UNBIND_BARRIER) {
    193 		rcu_barrier(); /* flush the i915_vm_release() */
    194 		goto try_again;
    195 	}
    196 
    197 	intel_runtime_pm_put(rpm, wakeref);
    198 
    199 	return ret;
    200 }
    201 
    202 static int
    203 i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
    204 		     struct drm_i915_gem_pwrite *args,
    205 		     struct drm_file *file)
    206 {
    207 	void *vaddr = sg_page(obj->mm.pages->sgl) + args->offset;
    208 	char __user *user_data = u64_to_user_ptr(args->data_ptr);
    209 
    210 	/*
    211 	 * We manually control the domain here and pretend that it
    212 	 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
    213 	 */
    214 	i915_gem_object_invalidate_frontbuffer(obj, ORIGIN_CPU);
    215 
    216 	if (copy_from_user(vaddr, user_data, args->size))
    217 		return -EFAULT;
    218 
    219 	drm_clflush_virt_range(vaddr, args->size);
    220 	intel_gt_chipset_flush(&to_i915(obj->base.dev)->gt);
    221 
    222 	i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
    223 	return 0;
    224 }
    225 
    226 static int
    227 i915_gem_create(struct drm_file *file,
    228 		struct intel_memory_region *mr,
    229 		u64 *size_p,
    230 		u32 *handle_p)
    231 {
    232 	struct drm_i915_gem_object *obj;
    233 	u32 handle;
    234 	u64 size;
    235 	int ret;
    236 
    237 	GEM_BUG_ON(!is_power_of_2(mr->min_page_size));
    238 	size = round_up(*size_p, mr->min_page_size);
    239 	if (size == 0)
    240 		return -EINVAL;
    241 
    242 	/* For most of the ABI (e.g. mmap) we think in system pages */
    243 	GEM_BUG_ON(!IS_ALIGNED(size, PAGE_SIZE));
    244 
    245 	/* Allocate the new object */
    246 	obj = i915_gem_object_create_region(mr, size, 0);
    247 	if (IS_ERR(obj))
    248 		return PTR_ERR(obj);
    249 
    250 	ret = drm_gem_handle_create(file, &obj->base, &handle);
    251 	/* drop reference from allocate - handle holds it now */
    252 	i915_gem_object_put(obj);
    253 	if (ret)
    254 		return ret;
    255 
    256 	*handle_p = handle;
    257 	*size_p = size;
    258 	return 0;
    259 }
    260 
    261 int
    262 i915_gem_dumb_create(struct drm_file *file,
    263 		     struct drm_device *dev,
    264 		     struct drm_mode_create_dumb *args)
    265 {
    266 	enum intel_memory_type mem_type;
    267 	int cpp = DIV_ROUND_UP(args->bpp, 8);
    268 	u32 format;
    269 
    270 	switch (cpp) {
    271 	case 1:
    272 		format = DRM_FORMAT_C8;
    273 		break;
    274 	case 2:
    275 		format = DRM_FORMAT_RGB565;
    276 		break;
    277 	case 4:
    278 		format = DRM_FORMAT_XRGB8888;
    279 		break;
    280 	default:
    281 		return -EINVAL;
    282 	}
    283 
    284 	/* have to work out size/pitch and return them */
    285 	args->pitch = ALIGN(args->width * cpp, 64);
    286 
    287 	/* align stride to page size so that we can remap */
    288 	if (args->pitch > intel_plane_fb_max_stride(to_i915(dev), format,
    289 						    DRM_FORMAT_MOD_LINEAR))
    290 		args->pitch = ALIGN(args->pitch, 4096);
    291 
    292 	if (args->pitch < args->width)
    293 		return -EINVAL;
    294 
    295 	args->size = mul_u32_u32(args->pitch, args->height);
    296 
    297 	mem_type = INTEL_MEMORY_SYSTEM;
    298 	if (HAS_LMEM(to_i915(dev)))
    299 		mem_type = INTEL_MEMORY_LOCAL;
    300 
    301 	return i915_gem_create(file,
    302 			       intel_memory_region_by_type(to_i915(dev),
    303 							   mem_type),
    304 			       &args->size, &args->handle);
    305 }
    306 
    307 /**
    308  * Creates a new mm object and returns a handle to it.
    309  * @dev: drm device pointer
    310  * @data: ioctl data blob
    311  * @file: drm file pointer
    312  */
    313 int
    314 i915_gem_create_ioctl(struct drm_device *dev, void *data,
    315 		      struct drm_file *file)
    316 {
    317 	struct drm_i915_private *i915 = to_i915(dev);
    318 	struct drm_i915_gem_create *args = data;
    319 
    320 	i915_gem_flush_free_objects(i915);
    321 
    322 	return i915_gem_create(file,
    323 			       intel_memory_region_by_type(i915,
    324 							   INTEL_MEMORY_SYSTEM),
    325 			       &args->size, &args->handle);
    326 }
    327 
    328 static int
    329 shmem_pread(struct page *page, int offset, int len, char __user *user_data,
    330 	    bool needs_clflush)
    331 {
    332 	char *vaddr;
    333 	int ret;
    334 
    335 	vaddr = kmap(page);
    336 
    337 	if (needs_clflush)
    338 		drm_clflush_virt_range(vaddr + offset, len);
    339 
    340 	ret = __copy_to_user(user_data, vaddr + offset, len);
    341 
    342 	kunmap(page);
    343 
    344 	return ret ? -EFAULT : 0;
    345 }
    346 
    347 static int
    348 i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
    349 		     struct drm_i915_gem_pread *args)
    350 {
    351 	unsigned int needs_clflush;
    352 	unsigned int idx, offset;
    353 	struct dma_fence *fence;
    354 	char __user *user_data;
    355 	u64 remain;
    356 	int ret;
    357 
    358 	ret = i915_gem_object_prepare_read(obj, &needs_clflush);
    359 	if (ret)
    360 		return ret;
    361 
    362 	fence = i915_gem_object_lock_fence(obj);
    363 	i915_gem_object_finish_access(obj);
    364 	if (!fence)
    365 		return -ENOMEM;
    366 
    367 	remain = args->size;
    368 	user_data = u64_to_user_ptr(args->data_ptr);
    369 	offset = offset_in_page(args->offset);
    370 	for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
    371 		struct page *page = i915_gem_object_get_page(obj, idx);
    372 		unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
    373 
    374 		ret = shmem_pread(page, offset, length, user_data,
    375 				  needs_clflush);
    376 		if (ret)
    377 			break;
    378 
    379 		remain -= length;
    380 		user_data += length;
    381 		offset = 0;
    382 	}
    383 
    384 	i915_gem_object_unlock_fence(obj, fence);
    385 	return ret;
    386 }
    387 
    388 static inline bool
    389 gtt_user_read(struct io_mapping *mapping,
    390 	      loff_t base, int offset,
    391 	      char __user *user_data, int length)
    392 {
    393 	void __iomem *vaddr;
    394 	unsigned long unwritten;
    395 
    396 	/* We can use the cpu mem copy function because this is X86. */
    397 	vaddr = io_mapping_map_atomic_wc(mapping, base);
    398 	unwritten = __copy_to_user_inatomic(user_data,
    399 					    (void __force *)vaddr + offset,
    400 					    length);
    401 	io_mapping_unmap_atomic(vaddr);
    402 	if (unwritten) {
    403 		vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
    404 		unwritten = copy_to_user(user_data,
    405 					 (void __force *)vaddr + offset,
    406 					 length);
    407 		io_mapping_unmap(vaddr);
    408 	}
    409 	return unwritten;
    410 }
    411 
    412 static int
    413 i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
    414 		   const struct drm_i915_gem_pread *args)
    415 {
    416 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
    417 	struct i915_ggtt *ggtt = &i915->ggtt;
    418 	intel_wakeref_t wakeref;
    419 	struct drm_mm_node node;
    420 	struct dma_fence *fence;
    421 	void __user *user_data;
    422 	struct i915_vma *vma;
    423 	u64 remain, offset;
    424 	int ret;
    425 
    426 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
    427 	vma = ERR_PTR(-ENODEV);
    428 	if (!i915_gem_object_is_tiled(obj))
    429 		vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
    430 					       PIN_MAPPABLE |
    431 					       PIN_NONBLOCK /* NOWARN */ |
    432 					       PIN_NOEVICT);
    433 	if (!IS_ERR(vma)) {
    434 		node.start = i915_ggtt_offset(vma);
    435 		node.flags = 0;
    436 	} else {
    437 		ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
    438 		if (ret)
    439 			goto out_rpm;
    440 		GEM_BUG_ON(!drm_mm_node_allocated(&node));
    441 	}
    442 
    443 	ret = i915_gem_object_lock_interruptible(obj);
    444 	if (ret)
    445 		goto out_unpin;
    446 
    447 	ret = i915_gem_object_set_to_gtt_domain(obj, false);
    448 	if (ret) {
    449 		i915_gem_object_unlock(obj);
    450 		goto out_unpin;
    451 	}
    452 
    453 	fence = i915_gem_object_lock_fence(obj);
    454 	i915_gem_object_unlock(obj);
    455 	if (!fence) {
    456 		ret = -ENOMEM;
    457 		goto out_unpin;
    458 	}
    459 
    460 	user_data = u64_to_user_ptr(args->data_ptr);
    461 	remain = args->size;
    462 	offset = args->offset;
    463 
    464 	while (remain > 0) {
    465 		/* Operation in this page
    466 		 *
    467 		 * page_base = page offset within aperture
    468 		 * page_offset = offset within page
    469 		 * page_length = bytes to copy for this page
    470 		 */
    471 		u32 page_base = node.start;
    472 		unsigned page_offset = offset_in_page(offset);
    473 		unsigned page_length = PAGE_SIZE - page_offset;
    474 		page_length = remain < page_length ? remain : page_length;
    475 		if (drm_mm_node_allocated(&node)) {
    476 			ggtt->vm.insert_page(&ggtt->vm,
    477 					     i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
    478 					     node.start, I915_CACHE_NONE, 0);
    479 		} else {
    480 			page_base += offset & PAGE_MASK;
    481 		}
    482 
    483 		if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
    484 				  user_data, page_length)) {
    485 			ret = -EFAULT;
    486 			break;
    487 		}
    488 
    489 		remain -= page_length;
    490 		user_data += page_length;
    491 		offset += page_length;
    492 	}
    493 
    494 	i915_gem_object_unlock_fence(obj, fence);
    495 out_unpin:
    496 	if (drm_mm_node_allocated(&node)) {
    497 		ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
    498 		remove_mappable_node(ggtt, &node);
    499 	} else {
    500 		i915_vma_unpin(vma);
    501 	}
    502 out_rpm:
    503 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
    504 	return ret;
    505 }
    506 
    507 /**
    508  * Reads data from the object referenced by handle.
    509  * @dev: drm device pointer
    510  * @data: ioctl data blob
    511  * @file: drm file pointer
    512  *
    513  * On error, the contents of *data are undefined.
    514  */
    515 int
    516 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
    517 		     struct drm_file *file)
    518 {
    519 	struct drm_i915_gem_pread *args = data;
    520 	struct drm_i915_gem_object *obj;
    521 	int ret;
    522 
    523 	if (args->size == 0)
    524 		return 0;
    525 
    526 	if (!access_ok(u64_to_user_ptr(args->data_ptr),
    527 		       args->size))
    528 		return -EFAULT;
    529 
    530 	obj = i915_gem_object_lookup(file, args->handle);
    531 	if (!obj)
    532 		return -ENOENT;
    533 
    534 	/* Bounds check source.  */
    535 	if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
    536 		ret = -EINVAL;
    537 		goto out;
    538 	}
    539 
    540 	trace_i915_gem_object_pread(obj, args->offset, args->size);
    541 
    542 	ret = i915_gem_object_wait(obj,
    543 				   I915_WAIT_INTERRUPTIBLE,
    544 				   MAX_SCHEDULE_TIMEOUT);
    545 	if (ret)
    546 		goto out;
    547 
    548 	ret = i915_gem_object_pin_pages(obj);
    549 	if (ret)
    550 		goto out;
    551 
    552 	ret = i915_gem_shmem_pread(obj, args);
    553 	if (ret == -EFAULT || ret == -ENODEV)
    554 		ret = i915_gem_gtt_pread(obj, args);
    555 
    556 	i915_gem_object_unpin_pages(obj);
    557 out:
    558 	i915_gem_object_put(obj);
    559 	return ret;
    560 }
    561 
    562 /* This is the fast write path which cannot handle
    563  * page faults in the source data
    564  */
    565 
    566 static inline bool
    567 ggtt_write(struct io_mapping *mapping,
    568 	   loff_t base, int offset,
    569 	   char __user *user_data, int length)
    570 {
    571 	void __iomem *vaddr;
    572 	unsigned long unwritten;
    573 
    574 	/* We can use the cpu mem copy function because this is X86. */
    575 	vaddr = io_mapping_map_atomic_wc(mapping, base);
    576 	unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
    577 						      user_data, length);
    578 	io_mapping_unmap_atomic(vaddr);
    579 	if (unwritten) {
    580 		vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
    581 		unwritten = copy_from_user((void __force *)vaddr + offset,
    582 					   user_data, length);
    583 		io_mapping_unmap(vaddr);
    584 	}
    585 
    586 	return unwritten;
    587 }
    588 
    589 /**
    590  * This is the fast pwrite path, where we copy the data directly from the
    591  * user into the GTT, uncached.
    592  * @obj: i915 GEM object
    593  * @args: pwrite arguments structure
    594  */
    595 static int
    596 i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
    597 			 const struct drm_i915_gem_pwrite *args)
    598 {
    599 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
    600 	struct i915_ggtt *ggtt = &i915->ggtt;
    601 	struct intel_runtime_pm *rpm = &i915->runtime_pm;
    602 	intel_wakeref_t wakeref;
    603 	struct drm_mm_node node;
    604 	struct dma_fence *fence;
    605 	struct i915_vma *vma;
    606 	u64 remain, offset;
    607 	void __user *user_data;
    608 	int ret;
    609 
    610 	if (i915_gem_object_has_struct_page(obj)) {
    611 		/*
    612 		 * Avoid waking the device up if we can fallback, as
    613 		 * waking/resuming is very slow (worst-case 10-100 ms
    614 		 * depending on PCI sleeps and our own resume time).
    615 		 * This easily dwarfs any performance advantage from
    616 		 * using the cache bypass of indirect GGTT access.
    617 		 */
    618 		wakeref = intel_runtime_pm_get_if_in_use(rpm);
    619 		if (!wakeref)
    620 			return -EFAULT;
    621 	} else {
    622 		/* No backing pages, no fallback, we must force GGTT access */
    623 		wakeref = intel_runtime_pm_get(rpm);
    624 	}
    625 
    626 	vma = ERR_PTR(-ENODEV);
    627 	if (!i915_gem_object_is_tiled(obj))
    628 		vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
    629 					       PIN_MAPPABLE |
    630 					       PIN_NONBLOCK /* NOWARN */ |
    631 					       PIN_NOEVICT);
    632 	if (!IS_ERR(vma)) {
    633 		node.start = i915_ggtt_offset(vma);
    634 		node.flags = 0;
    635 	} else {
    636 		ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
    637 		if (ret)
    638 			goto out_rpm;
    639 		GEM_BUG_ON(!drm_mm_node_allocated(&node));
    640 	}
    641 
    642 	ret = i915_gem_object_lock_interruptible(obj);
    643 	if (ret)
    644 		goto out_unpin;
    645 
    646 	ret = i915_gem_object_set_to_gtt_domain(obj, true);
    647 	if (ret) {
    648 		i915_gem_object_unlock(obj);
    649 		goto out_unpin;
    650 	}
    651 
    652 	fence = i915_gem_object_lock_fence(obj);
    653 	i915_gem_object_unlock(obj);
    654 	if (!fence) {
    655 		ret = -ENOMEM;
    656 		goto out_unpin;
    657 	}
    658 
    659 	i915_gem_object_invalidate_frontbuffer(obj, ORIGIN_CPU);
    660 
    661 	user_data = u64_to_user_ptr(args->data_ptr);
    662 	offset = args->offset;
    663 	remain = args->size;
    664 	while (remain) {
    665 		/* Operation in this page
    666 		 *
    667 		 * page_base = page offset within aperture
    668 		 * page_offset = offset within page
    669 		 * page_length = bytes to copy for this page
    670 		 */
    671 		u32 page_base = node.start;
    672 		unsigned int page_offset = offset_in_page(offset);
    673 		unsigned int page_length = PAGE_SIZE - page_offset;
    674 		page_length = remain < page_length ? remain : page_length;
    675 		if (drm_mm_node_allocated(&node)) {
    676 			/* flush the write before we modify the GGTT */
    677 			intel_gt_flush_ggtt_writes(ggtt->vm.gt);
    678 			ggtt->vm.insert_page(&ggtt->vm,
    679 					     i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
    680 					     node.start, I915_CACHE_NONE, 0);
    681 			wmb(); /* flush modifications to the GGTT (insert_page) */
    682 		} else {
    683 			page_base += offset & PAGE_MASK;
    684 		}
    685 		/* If we get a fault while copying data, then (presumably) our
    686 		 * source page isn't available.  Return the error and we'll
    687 		 * retry in the slow path.
    688 		 * If the object is non-shmem backed, we retry again with the
    689 		 * path that handles page fault.
    690 		 */
    691 		if (ggtt_write(&ggtt->iomap, page_base, page_offset,
    692 			       user_data, page_length)) {
    693 			ret = -EFAULT;
    694 			break;
    695 		}
    696 
    697 		remain -= page_length;
    698 		user_data += page_length;
    699 		offset += page_length;
    700 	}
    701 
    702 	intel_gt_flush_ggtt_writes(ggtt->vm.gt);
    703 	i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
    704 
    705 	i915_gem_object_unlock_fence(obj, fence);
    706 out_unpin:
    707 	if (drm_mm_node_allocated(&node)) {
    708 		ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
    709 		remove_mappable_node(ggtt, &node);
    710 	} else {
    711 		i915_vma_unpin(vma);
    712 	}
    713 out_rpm:
    714 	intel_runtime_pm_put(rpm, wakeref);
    715 	return ret;
    716 }
    717 
    718 /* Per-page copy function for the shmem pwrite fastpath.
    719  * Flushes invalid cachelines before writing to the target if
    720  * needs_clflush_before is set and flushes out any written cachelines after
    721  * writing if needs_clflush is set.
    722  */
    723 static int
    724 shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
    725 	     bool needs_clflush_before,
    726 	     bool needs_clflush_after)
    727 {
    728 #ifdef __NetBSD__
    729 	return -EFAULT;
    730 #else
    731 	char *vaddr;
    732 	int ret;
    733 
    734 	vaddr = kmap(page);
    735 
    736 	if (needs_clflush_before)
    737 		drm_clflush_virt_range(vaddr + offset, len);
    738 
    739 	ret = __copy_from_user(vaddr + offset, user_data, len);
    740 	if (!ret && needs_clflush_after)
    741 		drm_clflush_virt_range(vaddr + offset, len);
    742 
    743 	kunmap(page);
    744 
    745 	return ret ? -EFAULT : 0;
    746 #endif
    747 }
    748 
    749 static int
    750 i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
    751 		      const struct drm_i915_gem_pwrite *args)
    752 {
    753 	unsigned int partial_cacheline_write;
    754 	unsigned int needs_clflush;
    755 	unsigned int offset, idx;
    756 	struct dma_fence *fence;
    757 	void __user *user_data;
    758 	u64 remain;
    759 	int ret;
    760 
    761 	ret = i915_gem_object_prepare_write(obj, &needs_clflush);
    762 	if (ret)
    763 		return ret;
    764 
    765 	fence = i915_gem_object_lock_fence(obj);
    766 	i915_gem_object_finish_access(obj);
    767 	if (!fence)
    768 		return -ENOMEM;
    769 
    770 	/* If we don't overwrite a cacheline completely we need to be
    771 	 * careful to have up-to-date data by first clflushing. Don't
    772 	 * overcomplicate things and flush the entire patch.
    773 	 */
    774 	partial_cacheline_write = 0;
    775 	if (needs_clflush & CLFLUSH_BEFORE)
    776 		partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
    777 
    778 	user_data = u64_to_user_ptr(args->data_ptr);
    779 	remain = args->size;
    780 	offset = offset_in_page(args->offset);
    781 	for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
    782 		struct page *page = i915_gem_object_get_page(obj, idx);
    783 		unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
    784 
    785 		ret = shmem_pwrite(page, offset, length, user_data,
    786 				   (offset | length) & partial_cacheline_write,
    787 				   needs_clflush & CLFLUSH_AFTER);
    788 		if (ret)
    789 			break;
    790 
    791 		remain -= length;
    792 		user_data += length;
    793 		offset = 0;
    794 	}
    795 
    796 	i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
    797 	i915_gem_object_unlock_fence(obj, fence);
    798 
    799 	return ret;
    800 }
    801 
    802 /**
    803  * Writes data to the object referenced by handle.
    804  * @dev: drm device
    805  * @data: ioctl data blob
    806  * @file: drm file
    807  *
    808  * On error, the contents of the buffer that were to be modified are undefined.
    809  */
    810 int
    811 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
    812 		      struct drm_file *file)
    813 {
    814 	struct drm_i915_gem_pwrite *args = data;
    815 	struct drm_i915_gem_object *obj;
    816 	int ret;
    817 
    818 	if (args->size == 0)
    819 		return 0;
    820 
    821 	if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
    822 		return -EFAULT;
    823 
    824 	obj = i915_gem_object_lookup(file, args->handle);
    825 	if (!obj)
    826 		return -ENOENT;
    827 
    828 	/* Bounds check destination. */
    829 	if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
    830 		ret = -EINVAL;
    831 		goto err;
    832 	}
    833 
    834 	/* Writes not allowed into this read-only object */
    835 	if (i915_gem_object_is_readonly(obj)) {
    836 		ret = -EINVAL;
    837 		goto err;
    838 	}
    839 
    840 	trace_i915_gem_object_pwrite(obj, args->offset, args->size);
    841 
    842 	ret = -ENODEV;
    843 	if (obj->ops->pwrite)
    844 		ret = obj->ops->pwrite(obj, args);
    845 	if (ret != -ENODEV)
    846 		goto err;
    847 
    848 	ret = i915_gem_object_wait(obj,
    849 				   I915_WAIT_INTERRUPTIBLE |
    850 				   I915_WAIT_ALL,
    851 				   MAX_SCHEDULE_TIMEOUT);
    852 	if (ret)
    853 		goto err;
    854 
    855 	ret = i915_gem_object_pin_pages(obj);
    856 	if (ret)
    857 		goto err;
    858 
    859 	ret = -EFAULT;
    860 	/* We can only do the GTT pwrite on untiled buffers, as otherwise
    861 	 * it would end up going through the fenced access, and we'll get
    862 	 * different detiling behavior between reading and writing.
    863 	 * pread/pwrite currently are reading and writing from the CPU
    864 	 * perspective, requiring manual detiling by the client.
    865 	 */
    866 	if (!i915_gem_object_has_struct_page(obj) ||
    867 	    cpu_write_needs_clflush(obj))
    868 		/* Note that the gtt paths might fail with non-page-backed user
    869 		 * pointers (e.g. gtt mappings when moving data between
    870 		 * textures). Fallback to the shmem path in that case.
    871 		 */
    872 		ret = i915_gem_gtt_pwrite_fast(obj, args);
    873 
    874 	if (ret == -EFAULT || ret == -ENOSPC) {
    875 		if (i915_gem_object_has_struct_page(obj))
    876 			ret = i915_gem_shmem_pwrite(obj, args);
    877 		else
    878 			ret = i915_gem_phys_pwrite(obj, args, file);
    879 	}
    880 
    881 	i915_gem_object_unpin_pages(obj);
    882 err:
    883 	i915_gem_object_put(obj);
    884 	return ret;
    885 }
    886 
    887 /**
    888  * Called when user space has done writes to this buffer
    889  * @dev: drm device
    890  * @data: ioctl data blob
    891  * @file: drm file
    892  */
    893 int
    894 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
    895 			 struct drm_file *file)
    896 {
    897 	struct drm_i915_gem_sw_finish *args = data;
    898 	struct drm_i915_gem_object *obj;
    899 
    900 	obj = i915_gem_object_lookup(file, args->handle);
    901 	if (!obj)
    902 		return -ENOENT;
    903 
    904 	/*
    905 	 * Proxy objects are barred from CPU access, so there is no
    906 	 * need to ban sw_finish as it is a nop.
    907 	 */
    908 
    909 	/* Pinned buffers may be scanout, so flush the cache */
    910 	i915_gem_object_flush_if_display(obj);
    911 	i915_gem_object_put(obj);
    912 
    913 	return 0;
    914 }
    915 
    916 void i915_gem_runtime_suspend(struct drm_i915_private *i915)
    917 {
    918 	struct drm_i915_gem_object *obj, *on;
    919 	int i;
    920 
    921 	/*
    922 	 * Only called during RPM suspend. All users of the userfault_list
    923 	 * must be holding an RPM wakeref to ensure that this can not
    924 	 * run concurrently with themselves (and use the struct_mutex for
    925 	 * protection between themselves).
    926 	 */
    927 
    928 	list_for_each_entry_safe(obj, on,
    929 				 &i915->ggtt.userfault_list, userfault_link)
    930 		__i915_gem_object_release_mmap_gtt(obj);
    931 
    932 	/*
    933 	 * The fence will be lost when the device powers down. If any were
    934 	 * in use by hardware (i.e. they are pinned), we should not be powering
    935 	 * down! All other fences will be reacquired by the user upon waking.
    936 	 */
    937 	for (i = 0; i < i915->ggtt.num_fences; i++) {
    938 		struct i915_fence_reg *reg = &i915->ggtt.fence_regs[i];
    939 
    940 		/*
    941 		 * Ideally we want to assert that the fence register is not
    942 		 * live at this point (i.e. that no piece of code will be
    943 		 * trying to write through fence + GTT, as that both violates
    944 		 * our tracking of activity and associated locking/barriers,
    945 		 * but also is illegal given that the hw is powered down).
    946 		 *
    947 		 * Previously we used reg->pin_count as a "liveness" indicator.
    948 		 * That is not sufficient, and we need a more fine-grained
    949 		 * tool if we want to have a sanity check here.
    950 		 */
    951 
    952 		if (!reg->vma)
    953 			continue;
    954 
    955 		GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
    956 		reg->dirty = true;
    957 	}
    958 }
    959 
    960 struct i915_vma *
    961 i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
    962 			 const struct i915_ggtt_view *view,
    963 			 u64 size,
    964 			 u64 alignment,
    965 			 u64 flags)
    966 {
    967 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
    968 	struct i915_ggtt *ggtt = &i915->ggtt;
    969 	struct i915_vma *vma;
    970 	int ret;
    971 
    972 	if (i915_gem_object_never_bind_ggtt(obj))
    973 		return ERR_PTR(-ENODEV);
    974 
    975 	if (flags & PIN_MAPPABLE &&
    976 	    (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
    977 		/*
    978 		 * If the required space is larger than the available
    979 		 * aperture, we will not able to find a slot for the
    980 		 * object and unbinding the object now will be in
    981 		 * vain. Worse, doing so may cause us to ping-pong
    982 		 * the object in and out of the Global GTT and
    983 		 * waste a lot of cycles under the mutex.
    984 		 */
    985 		if (obj->base.size > ggtt->mappable_end)
    986 			return ERR_PTR(-E2BIG);
    987 
    988 		/*
    989 		 * If NONBLOCK is set the caller is optimistically
    990 		 * trying to cache the full object within the mappable
    991 		 * aperture, and *must* have a fallback in place for
    992 		 * situations where we cannot bind the object. We
    993 		 * can be a little more lax here and use the fallback
    994 		 * more often to avoid costly migrations of ourselves
    995 		 * and other objects within the aperture.
    996 		 *
    997 		 * Half-the-aperture is used as a simple heuristic.
    998 		 * More interesting would to do search for a free
    999 		 * block prior to making the commitment to unbind.
   1000 		 * That caters for the self-harm case, and with a
   1001 		 * little more heuristics (e.g. NOFAULT, NOEVICT)
   1002 		 * we could try to minimise harm to others.
   1003 		 */
   1004 		if (flags & PIN_NONBLOCK &&
   1005 		    obj->base.size > ggtt->mappable_end / 2)
   1006 			return ERR_PTR(-ENOSPC);
   1007 	}
   1008 
   1009 	vma = i915_vma_instance(obj, &ggtt->vm, view);
   1010 	if (IS_ERR(vma))
   1011 		return vma;
   1012 
   1013 	if (i915_vma_misplaced(vma, size, alignment, flags)) {
   1014 		if (flags & PIN_NONBLOCK) {
   1015 			if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
   1016 				return ERR_PTR(-ENOSPC);
   1017 
   1018 			if (flags & PIN_MAPPABLE &&
   1019 			    vma->fence_size > ggtt->mappable_end / 2)
   1020 				return ERR_PTR(-ENOSPC);
   1021 		}
   1022 
   1023 		ret = i915_vma_unbind(vma);
   1024 		if (ret)
   1025 			return ERR_PTR(ret);
   1026 	}
   1027 
   1028 	if (vma->fence && !i915_gem_object_is_tiled(obj)) {
   1029 		mutex_lock(&ggtt->vm.mutex);
   1030 		ret = i915_vma_revoke_fence(vma);
   1031 		mutex_unlock(&ggtt->vm.mutex);
   1032 		if (ret)
   1033 			return ERR_PTR(ret);
   1034 	}
   1035 
   1036 	ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
   1037 	if (ret)
   1038 		return ERR_PTR(ret);
   1039 
   1040 	return vma;
   1041 }
   1042 
   1043 int
   1044 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
   1045 		       struct drm_file *file_priv)
   1046 {
   1047 	struct drm_i915_private *i915 = to_i915(dev);
   1048 	struct drm_i915_gem_madvise *args = data;
   1049 	struct drm_i915_gem_object *obj;
   1050 	int err;
   1051 
   1052 	switch (args->madv) {
   1053 	case I915_MADV_DONTNEED:
   1054 	case I915_MADV_WILLNEED:
   1055 	    break;
   1056 	default:
   1057 	    return -EINVAL;
   1058 	}
   1059 
   1060 	obj = i915_gem_object_lookup(file_priv, args->handle);
   1061 	if (!obj)
   1062 		return -ENOENT;
   1063 
   1064 	err = mutex_lock_interruptible(&obj->mm.lock);
   1065 	if (err)
   1066 		goto out;
   1067 
   1068 	if (i915_gem_object_has_pages(obj) &&
   1069 	    i915_gem_object_is_tiled(obj) &&
   1070 	    i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
   1071 		if (obj->mm.madv == I915_MADV_WILLNEED) {
   1072 			GEM_BUG_ON(!obj->mm.quirked);
   1073 			__i915_gem_object_unpin_pages(obj);
   1074 			obj->mm.quirked = false;
   1075 		}
   1076 		if (args->madv == I915_MADV_WILLNEED) {
   1077 			GEM_BUG_ON(obj->mm.quirked);
   1078 			__i915_gem_object_pin_pages(obj);
   1079 			obj->mm.quirked = true;
   1080 		}
   1081 	}
   1082 
   1083 	if (obj->mm.madv != __I915_MADV_PURGED)
   1084 		obj->mm.madv = args->madv;
   1085 
   1086 	if (i915_gem_object_has_pages(obj)) {
   1087 		struct list_head *list;
   1088 
   1089 		if (i915_gem_object_is_shrinkable(obj)) {
   1090 			unsigned long flags;
   1091 
   1092 			spin_lock_irqsave(&i915->mm.obj_lock, flags);
   1093 
   1094 			if (obj->mm.madv != I915_MADV_WILLNEED)
   1095 				list = &i915->mm.purge_list;
   1096 			else
   1097 				list = &i915->mm.shrink_list;
   1098 			list_move_tail(&obj->mm.link, list);
   1099 
   1100 			spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
   1101 		}
   1102 	}
   1103 
   1104 	/* if the object is no longer attached, discard its backing storage */
   1105 	if (obj->mm.madv == I915_MADV_DONTNEED &&
   1106 	    !i915_gem_object_has_pages(obj))
   1107 		i915_gem_object_truncate(obj);
   1108 
   1109 	args->retained = obj->mm.madv != __I915_MADV_PURGED;
   1110 	mutex_unlock(&obj->mm.lock);
   1111 
   1112 out:
   1113 	i915_gem_object_put(obj);
   1114 	return err;
   1115 }
   1116 
   1117 int i915_gem_init(struct drm_i915_private *dev_priv)
   1118 {
   1119 	int ret;
   1120 
   1121 	/* We need to fallback to 4K pages if host doesn't support huge gtt. */
   1122 	if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
   1123 		mkwrite_device_info(dev_priv)->page_sizes =
   1124 			I915_GTT_PAGE_SIZE_4K;
   1125 
   1126 	ret = i915_gem_init_userptr(dev_priv);
   1127 	if (ret)
   1128 		return ret;
   1129 
   1130 	intel_uc_fetch_firmwares(&dev_priv->gt.uc);
   1131 	intel_wopcm_init(&dev_priv->wopcm);
   1132 
   1133 	ret = i915_init_ggtt(dev_priv);
   1134 	if (ret) {
   1135 		GEM_BUG_ON(ret == -EIO);
   1136 		goto err_unlock;
   1137 	}
   1138 
   1139 	/*
   1140 	 * Despite its name intel_init_clock_gating applies both display
   1141 	 * clock gating workarounds; GT mmio workarounds and the occasional
   1142 	 * GT power context workaround. Worse, sometimes it includes a context
   1143 	 * register workaround which we need to apply before we record the
   1144 	 * default HW state for all contexts.
   1145 	 *
   1146 	 * FIXME: break up the workarounds and apply them at the right time!
   1147 	 */
   1148 	intel_init_clock_gating(dev_priv);
   1149 
   1150 	ret = intel_gt_init(&dev_priv->gt);
   1151 	if (ret)
   1152 		goto err_unlock;
   1153 
   1154 	return 0;
   1155 
   1156 	/*
   1157 	 * Unwinding is complicated by that we want to handle -EIO to mean
   1158 	 * disable GPU submission but keep KMS alive. We want to mark the
   1159 	 * HW as irrevisibly wedged, but keep enough state around that the
   1160 	 * driver doesn't explode during runtime.
   1161 	 */
   1162 err_unlock:
   1163 	i915_gem_drain_workqueue(dev_priv);
   1164 
   1165 	if (ret != -EIO) {
   1166 		intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
   1167 		i915_gem_cleanup_userptr(dev_priv);
   1168 	}
   1169 
   1170 	if (ret == -EIO) {
   1171 		/*
   1172 		 * Allow engines or uC initialisation to fail by marking the GPU
   1173 		 * as wedged. But we only want to do this when the GPU is angry,
   1174 		 * for all other failure, such as an allocation failure, bail.
   1175 		 */
   1176 		if (!intel_gt_is_wedged(&dev_priv->gt)) {
   1177 			i915_probe_error(dev_priv,
   1178 					 "Failed to initialize GPU, declaring it wedged!\n");
   1179 			intel_gt_set_wedged(&dev_priv->gt);
   1180 		}
   1181 
   1182 		/* Minimal basic recovery for KMS */
   1183 		ret = i915_ggtt_enable_hw(dev_priv);
   1184 		i915_gem_restore_gtt_mappings(dev_priv);
   1185 		i915_gem_restore_fences(&dev_priv->ggtt);
   1186 		intel_init_clock_gating(dev_priv);
   1187 	}
   1188 
   1189 	i915_gem_drain_freed_objects(dev_priv);
   1190 	return ret;
   1191 }
   1192 
   1193 void i915_gem_driver_register(struct drm_i915_private *i915)
   1194 {
   1195 	i915_gem_driver_register__shrinker(i915);
   1196 
   1197 	intel_engines_driver_register(i915);
   1198 }
   1199 
   1200 void i915_gem_driver_unregister(struct drm_i915_private *i915)
   1201 {
   1202 	i915_gem_driver_unregister__shrinker(i915);
   1203 }
   1204 
   1205 void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
   1206 {
   1207 	intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
   1208 
   1209 	i915_gem_suspend_late(dev_priv);
   1210 	intel_gt_driver_remove(&dev_priv->gt);
   1211 	dev_priv->uabi_engines = RB_ROOT;
   1212 
   1213 	/* Flush any outstanding unpin_work. */
   1214 	i915_gem_drain_workqueue(dev_priv);
   1215 
   1216 	i915_gem_drain_freed_objects(dev_priv);
   1217 }
   1218 
   1219 void i915_gem_driver_release(struct drm_i915_private *dev_priv)
   1220 {
   1221 	i915_gem_driver_release__contexts(dev_priv);
   1222 
   1223 	intel_gt_driver_release(&dev_priv->gt);
   1224 
   1225 	intel_wa_list_free(&dev_priv->gt_wa_list);
   1226 
   1227 	intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
   1228 	i915_gem_cleanup_userptr(dev_priv);
   1229 
   1230 	i915_gem_drain_freed_objects(dev_priv);
   1231 
   1232 	WARN_ON(!list_empty(&dev_priv->gem.contexts.list));
   1233 }
   1234 
   1235 static void i915_gem_init__mm(struct drm_i915_private *i915)
   1236 {
   1237 	spin_lock_init(&i915->mm.obj_lock);
   1238 
   1239 	init_llist_head(&i915->mm.free_list);
   1240 
   1241 	INIT_LIST_HEAD(&i915->mm.purge_list);
   1242 	INIT_LIST_HEAD(&i915->mm.shrink_list);
   1243 
   1244 	i915_gem_init__objects(i915);
   1245 }
   1246 
   1247 void i915_gem_init_early(struct drm_i915_private *dev_priv)
   1248 {
   1249 	i915_gem_init__mm(dev_priv);
   1250 	i915_gem_init__contexts(dev_priv);
   1251 
   1252 	spin_lock_init(&dev_priv->fb_tracking.lock);
   1253 }
   1254 
   1255 void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
   1256 {
   1257 	i915_gem_drain_freed_objects(dev_priv);
   1258 	GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
   1259 	GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
   1260 	WARN_ON(dev_priv->mm.shrink_count);
   1261 }
   1262 
   1263 int i915_gem_freeze(struct drm_i915_private *dev_priv)
   1264 {
   1265 	/* Discard all purgeable objects, let userspace recover those as
   1266 	 * required after resuming.
   1267 	 */
   1268 	i915_gem_shrink_all(dev_priv);
   1269 
   1270 	return 0;
   1271 }
   1272 
   1273 int i915_gem_freeze_late(struct drm_i915_private *i915)
   1274 {
   1275 	struct drm_i915_gem_object *obj;
   1276 	intel_wakeref_t wakeref;
   1277 
   1278 	/*
   1279 	 * Called just before we write the hibernation image.
   1280 	 *
   1281 	 * We need to update the domain tracking to reflect that the CPU
   1282 	 * will be accessing all the pages to create and restore from the
   1283 	 * hibernation, and so upon restoration those pages will be in the
   1284 	 * CPU domain.
   1285 	 *
   1286 	 * To make sure the hibernation image contains the latest state,
   1287 	 * we update that state just before writing out the image.
   1288 	 *
   1289 	 * To try and reduce the hibernation image, we manually shrink
   1290 	 * the objects as well, see i915_gem_freeze()
   1291 	 */
   1292 
   1293 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
   1294 
   1295 	i915_gem_shrink(i915, -1UL, NULL, ~0);
   1296 	i915_gem_drain_freed_objects(i915);
   1297 
   1298 	list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
   1299 		i915_gem_object_lock(obj);
   1300 		WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
   1301 		i915_gem_object_unlock(obj);
   1302 	}
   1303 
   1304 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
   1305 
   1306 	return 0;
   1307 }
   1308 
   1309 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
   1310 {
   1311 	struct drm_i915_file_private *file_priv = file->driver_priv;
   1312 	struct i915_request *request;
   1313 
   1314 	/* Clean up our request list when the client is going away, so that
   1315 	 * later retire_requests won't dereference our soon-to-be-gone
   1316 	 * file_priv.
   1317 	 */
   1318 	spin_lock(&file_priv->mm.lock);
   1319 	list_for_each_entry(request, &file_priv->mm.request_list, client_link)
   1320 		request->file_priv = NULL;
   1321 	spin_unlock(&file_priv->mm.lock);
   1322 }
   1323 
   1324 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
   1325 {
   1326 	struct drm_i915_file_private *file_priv;
   1327 	int ret;
   1328 
   1329 	DRM_DEBUG("\n");
   1330 
   1331 	file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
   1332 	if (!file_priv)
   1333 		return -ENOMEM;
   1334 
   1335 	file->driver_priv = file_priv;
   1336 	file_priv->dev_priv = i915;
   1337 	file_priv->file = file;
   1338 
   1339 	spin_lock_init(&file_priv->mm.lock);
   1340 	INIT_LIST_HEAD(&file_priv->mm.request_list);
   1341 
   1342 	file_priv->bsd_engine = -1;
   1343 	file_priv->hang_timestamp = jiffies;
   1344 
   1345 	ret = i915_gem_context_open(i915, file);
   1346 	if (ret)
   1347 		kfree(file_priv);
   1348 
   1349 	return ret;
   1350 }
   1351 
   1352 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
   1353 #include "selftests/mock_gem_device.c"
   1354 #include "selftests/i915_gem.c"
   1355 #endif
   1356