i915_gem.c 37.5 KB
Newer Older
1
/*
2
 * Copyright © 2008-2015 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *
 */

28
#include <drm/drm_vma_manager.h>
29
#include <drm/i915_drm.h>
30
#include <linux/dma-fence-array.h>
31
#include <linux/kthread.h>
32
#include <linux/dma-resv.h>
33
#include <linux/shmem_fs.h>
34
#include <linux/slab.h>
35
#include <linux/stop_machine.h>
36
#include <linux/swap.h>
J
Jesse Barnes 已提交
37
#include <linux/pci.h>
38
#include <linux/dma-buf.h>
39
#include <linux/mman.h>
40

41 42 43
#include "display/intel_display.h"
#include "display/intel_frontbuffer.h"

44 45
#include "gem/i915_gem_clflush.h"
#include "gem/i915_gem_context.h"
46
#include "gem/i915_gem_ioctls.h"
47
#include "gem/i915_gem_pm.h"
48
#include "gt/intel_engine_user.h"
49
#include "gt/intel_gt.h"
50
#include "gt/intel_gt_pm.h"
51
#include "gt/intel_gt_requests.h"
52 53
#include "gt/intel_mocs.h"
#include "gt/intel_reset.h"
54
#include "gt/intel_renderstate.h"
55
#include "gt/intel_rps.h"
56 57
#include "gt/intel_workarounds.h"

58
#include "i915_drv.h"
59
#include "i915_scatterlist.h"
60 61 62
#include "i915_trace.h"
#include "i915_vgpu.h"

63
#include "intel_pm.h"
64

65
static int
66
insert_mappable_node(struct i915_ggtt *ggtt, struct drm_mm_node *node, u32 size)
67
{
68 69 70 71 72 73
	int err;

	err = mutex_lock_interruptible(&ggtt->vm.mutex);
	if (err)
		return err;

74
	memset(node, 0, sizeof(*node));
75 76 77 78 79 80 81 82
	err = drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
					  size, 0, I915_COLOR_UNEVICTABLE,
					  0, ggtt->mappable_end,
					  DRM_MM_INSERT_LOW);

	mutex_unlock(&ggtt->vm.mutex);

	return err;
83 84 85
}

static void
86
remove_mappable_node(struct i915_ggtt *ggtt, struct drm_mm_node *node)
87
{
88
	mutex_lock(&ggtt->vm.mutex);
89
	drm_mm_remove_node(node);
90
	mutex_unlock(&ggtt->vm.mutex);
91 92
}

93 94
int
i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
95
			    struct drm_file *file)
96
{
97
	struct i915_ggtt *ggtt = &to_i915(dev)->ggtt;
98
	struct drm_i915_gem_get_aperture *args = data;
99
	struct i915_vma *vma;
100
	u64 pinned;
101

102 103
	if (mutex_lock_interruptible(&ggtt->vm.mutex))
		return -EINTR;
104

105
	pinned = ggtt->vm.reserved;
106
	list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
107
		if (i915_vma_is_pinned(vma))
108
			pinned += vma->node.size;
109 110

	mutex_unlock(&ggtt->vm.mutex);
111

112
	args->aper_size = ggtt->vm.total;
113
	args->aper_available_size = args->aper_size - pinned;
114

115 116 117
	return 0;
}

118 119
int i915_gem_object_unbind(struct drm_i915_gem_object *obj,
			   unsigned long flags)
120 121 122
{
	struct i915_vma *vma;
	LIST_HEAD(still_in_list);
123
	int ret = 0;
124

125 126 127 128
	spin_lock(&obj->vma.lock);
	while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
						       struct i915_vma,
						       obj_link))) {
129 130 131 132 133 134
		struct i915_address_space *vm = vma->vm;

		ret = -EBUSY;
		if (!i915_vm_tryopen(vm))
			break;

135
		list_move_tail(&vma->obj_link, &still_in_list);
136 137
		spin_unlock(&obj->vma.lock);

138 139 140
		if (flags & I915_GEM_OBJECT_UNBIND_ACTIVE ||
		    !i915_vma_is_active(vma))
			ret = i915_vma_unbind(vma);
141

142
		i915_vm_close(vm);
143
		spin_lock(&obj->vma.lock);
144
	}
145 146
	list_splice(&still_in_list, &obj->vma.list);
	spin_unlock(&obj->vma.lock);
147 148 149 150

	return ret;
}

151 152 153
static int
i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
		     struct drm_i915_gem_pwrite *args,
154
		     struct drm_file *file)
155 156
{
	void *vaddr = obj->phys_handle->vaddr + args->offset;
157
	char __user *user_data = u64_to_user_ptr(args->data_ptr);
158

159 160
	/*
	 * We manually control the domain here and pretend that it
161 162
	 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
	 */
163 164
	intel_frontbuffer_invalidate(obj->frontbuffer, ORIGIN_CPU);

165 166
	if (copy_from_user(vaddr, user_data, args->size))
		return -EFAULT;
167

168
	drm_clflush_virt_range(vaddr, args->size);
169
	intel_gt_chipset_flush(&to_i915(obj->base.dev)->gt);
170

171
	intel_frontbuffer_flush(obj->frontbuffer, ORIGIN_CPU);
172
	return 0;
173 174
}

175 176
static int
i915_gem_create(struct drm_file *file,
177
		struct drm_i915_private *dev_priv,
178
		u64 *size_p,
179
		u32 *handle_p)
180
{
181
	struct drm_i915_gem_object *obj;
182
	u32 handle;
183 184
	u64 size;
	int ret;
185

186
	size = round_up(*size_p, PAGE_SIZE);
187 188
	if (size == 0)
		return -EINVAL;
189 190

	/* Allocate the new object */
191
	obj = i915_gem_object_create_shmem(dev_priv, size);
192 193
	if (IS_ERR(obj))
		return PTR_ERR(obj);
194

195
	ret = drm_gem_handle_create(file, &obj->base, &handle);
196
	/* drop reference from allocate - handle holds it now */
C
Chris Wilson 已提交
197
	i915_gem_object_put(obj);
198 199
	if (ret)
		return ret;
200

201
	*handle_p = handle;
202
	*size_p = size;
203 204 205
	return 0;
}

206 207 208 209 210
int
i915_gem_dumb_create(struct drm_file *file,
		     struct drm_device *dev,
		     struct drm_mode_create_dumb *args)
{
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	int cpp = DIV_ROUND_UP(args->bpp, 8);
	u32 format;

	switch (cpp) {
	case 1:
		format = DRM_FORMAT_C8;
		break;
	case 2:
		format = DRM_FORMAT_RGB565;
		break;
	case 4:
		format = DRM_FORMAT_XRGB8888;
		break;
	default:
		return -EINVAL;
	}

228
	/* have to work out size/pitch and return them */
229 230 231 232 233 234 235
	args->pitch = ALIGN(args->width * cpp, 64);

	/* align stride to page size so that we can remap */
	if (args->pitch > intel_plane_fb_max_stride(to_i915(dev), format,
						    DRM_FORMAT_MOD_LINEAR))
		args->pitch = ALIGN(args->pitch, 4096);

236
	args->size = args->pitch * args->height;
237
	return i915_gem_create(file, to_i915(dev),
238
			       &args->size, &args->handle);
239 240 241 242
}

/**
 * Creates a new mm object and returns a handle to it.
243 244 245
 * @dev: drm device pointer
 * @data: ioctl data blob
 * @file: drm file pointer
246 247 248 249 250
 */
int
i915_gem_create_ioctl(struct drm_device *dev, void *data,
		      struct drm_file *file)
{
251
	struct drm_i915_private *dev_priv = to_i915(dev);
252
	struct drm_i915_gem_create *args = data;
253

254
	i915_gem_flush_free_objects(dev_priv);
255

256
	return i915_gem_create(file, dev_priv,
257
			       &args->size, &args->handle);
258 259
}

260
static int
261 262
shmem_pread(struct page *page, int offset, int len, char __user *user_data,
	    bool needs_clflush)
263 264 265 266 267 268
{
	char *vaddr;
	int ret;

	vaddr = kmap(page);

269 270
	if (needs_clflush)
		drm_clflush_virt_range(vaddr + offset, len);
271

272
	ret = __copy_to_user(user_data, vaddr + offset, len);
273

274
	kunmap(page);
275

276
	return ret ? -EFAULT : 0;
277 278 279 280 281 282 283 284
}

static int
i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
		     struct drm_i915_gem_pread *args)
{
	unsigned int needs_clflush;
	unsigned int idx, offset;
285 286 287
	struct dma_fence *fence;
	char __user *user_data;
	u64 remain;
288 289
	int ret;

290
	ret = i915_gem_object_prepare_read(obj, &needs_clflush);
291 292 293
	if (ret)
		return ret;

294 295 296 297 298
	fence = i915_gem_object_lock_fence(obj);
	i915_gem_object_finish_access(obj);
	if (!fence)
		return -ENOMEM;

299 300 301 302 303
	remain = args->size;
	user_data = u64_to_user_ptr(args->data_ptr);
	offset = offset_in_page(args->offset);
	for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
		struct page *page = i915_gem_object_get_page(obj, idx);
304
		unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
305 306 307 308 309 310 311 312 313 314 315

		ret = shmem_pread(page, offset, length, user_data,
				  needs_clflush);
		if (ret)
			break;

		remain -= length;
		user_data += length;
		offset = 0;
	}

316
	i915_gem_object_unlock_fence(obj, fence);
317 318 319 320 321 322 323
	return ret;
}

static inline bool
gtt_user_read(struct io_mapping *mapping,
	      loff_t base, int offset,
	      char __user *user_data, int length)
324
{
325
	void __iomem *vaddr;
326
	unsigned long unwritten;
327 328

	/* We can use the cpu mem copy function because this is X86. */
329 330 331 332
	vaddr = io_mapping_map_atomic_wc(mapping, base);
	unwritten = __copy_to_user_inatomic(user_data,
					    (void __force *)vaddr + offset,
					    length);
333 334
	io_mapping_unmap_atomic(vaddr);
	if (unwritten) {
335 336 337 338
		vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
		unwritten = copy_to_user(user_data,
					 (void __force *)vaddr + offset,
					 length);
339 340
		io_mapping_unmap(vaddr);
	}
341 342 343 344
	return unwritten;
}

static int
345 346
i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
		   const struct drm_i915_gem_pread *args)
347
{
348 349
	struct drm_i915_private *i915 = to_i915(obj->base.dev);
	struct i915_ggtt *ggtt = &i915->ggtt;
350
	intel_wakeref_t wakeref;
351
	struct drm_mm_node node;
352
	struct dma_fence *fence;
353
	void __user *user_data;
354
	struct i915_vma *vma;
355
	u64 remain, offset;
356 357
	int ret;

358
	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
359 360 361 362 363 364
	vma = ERR_PTR(-ENODEV);
	if (!i915_gem_object_is_tiled(obj))
		vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
					       PIN_MAPPABLE |
					       PIN_NONBLOCK /* NOWARN */ |
					       PIN_NOEVICT);
365 366
	if (!IS_ERR(vma)) {
		node.start = i915_ggtt_offset(vma);
367
		node.flags = 0;
368
	} else {
369
		ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
370
		if (ret)
371
			goto out_rpm;
372
		GEM_BUG_ON(!drm_mm_node_allocated(&node));
373 374
	}

375
	ret = i915_gem_object_lock_interruptible(obj);
376 377 378
	if (ret)
		goto out_unpin;

379 380 381 382 383 384 385 386 387 388 389 390
	ret = i915_gem_object_set_to_gtt_domain(obj, false);
	if (ret) {
		i915_gem_object_unlock(obj);
		goto out_unpin;
	}

	fence = i915_gem_object_lock_fence(obj);
	i915_gem_object_unlock(obj);
	if (!fence) {
		ret = -ENOMEM;
		goto out_unpin;
	}
391

392 393 394
	user_data = u64_to_user_ptr(args->data_ptr);
	remain = args->size;
	offset = args->offset;
395 396 397 398 399 400 401 402 403 404 405 406

	while (remain > 0) {
		/* Operation in this page
		 *
		 * page_base = page offset within aperture
		 * page_offset = offset within page
		 * page_length = bytes to copy for this page
		 */
		u32 page_base = node.start;
		unsigned page_offset = offset_in_page(offset);
		unsigned page_length = PAGE_SIZE - page_offset;
		page_length = remain < page_length ? remain : page_length;
407
		if (drm_mm_node_allocated(&node)) {
408 409 410
			ggtt->vm.insert_page(&ggtt->vm,
					     i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
					     node.start, I915_CACHE_NONE, 0);
411 412 413
		} else {
			page_base += offset & PAGE_MASK;
		}
414

415
		if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
416
				  user_data, page_length)) {
417 418 419 420 421 422 423 424 425
			ret = -EFAULT;
			break;
		}

		remain -= page_length;
		user_data += page_length;
		offset += page_length;
	}

426
	i915_gem_object_unlock_fence(obj, fence);
427
out_unpin:
428
	if (drm_mm_node_allocated(&node)) {
429
		ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
430
		remove_mappable_node(ggtt, &node);
431
	} else {
C
Chris Wilson 已提交
432
		i915_vma_unpin(vma);
433
	}
434
out_rpm:
435
	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
436 437 438
	return ret;
}

439 440
/**
 * Reads data from the object referenced by handle.
441 442 443
 * @dev: drm device pointer
 * @data: ioctl data blob
 * @file: drm file pointer
444 445 446 447 448
 *
 * On error, the contents of *data are undefined.
 */
int
i915_gem_pread_ioctl(struct drm_device *dev, void *data,
449
		     struct drm_file *file)
450 451
{
	struct drm_i915_gem_pread *args = data;
452
	struct drm_i915_gem_object *obj;
453
	int ret;
454

455 456 457
	if (args->size == 0)
		return 0;

458
	if (!access_ok(u64_to_user_ptr(args->data_ptr),
459 460 461
		       args->size))
		return -EFAULT;

462
	obj = i915_gem_object_lookup(file, args->handle);
463 464
	if (!obj)
		return -ENOENT;
465

466
	/* Bounds check source.  */
467
	if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
C
Chris Wilson 已提交
468
		ret = -EINVAL;
469
		goto out;
C
Chris Wilson 已提交
470 471
	}

C
Chris Wilson 已提交
472 473
	trace_i915_gem_object_pread(obj, args->offset, args->size);

474 475
	ret = i915_gem_object_wait(obj,
				   I915_WAIT_INTERRUPTIBLE,
476
				   MAX_SCHEDULE_TIMEOUT);
477
	if (ret)
478
		goto out;
479

480
	ret = i915_gem_object_pin_pages(obj);
481
	if (ret)
482
		goto out;
483

484
	ret = i915_gem_shmem_pread(obj, args);
485
	if (ret == -EFAULT || ret == -ENODEV)
486
		ret = i915_gem_gtt_pread(obj, args);
487

488 489
	i915_gem_object_unpin_pages(obj);
out:
C
Chris Wilson 已提交
490
	i915_gem_object_put(obj);
491
	return ret;
492 493
}

494 495
/* This is the fast write path which cannot handle
 * page faults in the source data
496
 */
497

498 499 500 501
static inline bool
ggtt_write(struct io_mapping *mapping,
	   loff_t base, int offset,
	   char __user *user_data, int length)
502
{
503
	void __iomem *vaddr;
504
	unsigned long unwritten;
505

506
	/* We can use the cpu mem copy function because this is X86. */
507 508
	vaddr = io_mapping_map_atomic_wc(mapping, base);
	unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
509
						      user_data, length);
510 511
	io_mapping_unmap_atomic(vaddr);
	if (unwritten) {
512 513 514
		vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
		unwritten = copy_from_user((void __force *)vaddr + offset,
					   user_data, length);
515 516
		io_mapping_unmap(vaddr);
	}
517 518 519 520

	return unwritten;
}

521 522 523
/**
 * This is the fast pwrite path, where we copy the data directly from the
 * user into the GTT, uncached.
524
 * @obj: i915 GEM object
525
 * @args: pwrite arguments structure
526
 */
527
static int
528 529
i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
			 const struct drm_i915_gem_pwrite *args)
530
{
531
	struct drm_i915_private *i915 = to_i915(obj->base.dev);
532
	struct i915_ggtt *ggtt = &i915->ggtt;
533
	struct intel_runtime_pm *rpm = &i915->runtime_pm;
534
	intel_wakeref_t wakeref;
535
	struct drm_mm_node node;
536
	struct dma_fence *fence;
537 538 539
	struct i915_vma *vma;
	u64 remain, offset;
	void __user *user_data;
540
	int ret;
541

542 543 544 545 546 547 548 549
	if (i915_gem_object_has_struct_page(obj)) {
		/*
		 * Avoid waking the device up if we can fallback, as
		 * waking/resuming is very slow (worst-case 10-100 ms
		 * depending on PCI sleeps and our own resume time).
		 * This easily dwarfs any performance advantage from
		 * using the cache bypass of indirect GGTT access.
		 */
550
		wakeref = intel_runtime_pm_get_if_in_use(rpm);
551 552
		if (!wakeref)
			return -EFAULT;
553 554
	} else {
		/* No backing pages, no fallback, we must force GGTT access */
555
		wakeref = intel_runtime_pm_get(rpm);
556 557
	}

558 559 560 561 562 563
	vma = ERR_PTR(-ENODEV);
	if (!i915_gem_object_is_tiled(obj))
		vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
					       PIN_MAPPABLE |
					       PIN_NONBLOCK /* NOWARN */ |
					       PIN_NOEVICT);
564 565
	if (!IS_ERR(vma)) {
		node.start = i915_ggtt_offset(vma);
566
		node.flags = 0;
567
	} else {
568
		ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
569
		if (ret)
570
			goto out_rpm;
571
		GEM_BUG_ON(!drm_mm_node_allocated(&node));
572
	}
D
Daniel Vetter 已提交
573

574
	ret = i915_gem_object_lock_interruptible(obj);
D
Daniel Vetter 已提交
575 576 577
	if (ret)
		goto out_unpin;

578 579 580 581 582 583 584 585 586 587 588 589
	ret = i915_gem_object_set_to_gtt_domain(obj, true);
	if (ret) {
		i915_gem_object_unlock(obj);
		goto out_unpin;
	}

	fence = i915_gem_object_lock_fence(obj);
	i915_gem_object_unlock(obj);
	if (!fence) {
		ret = -ENOMEM;
		goto out_unpin;
	}
590

591
	intel_frontbuffer_invalidate(obj->frontbuffer, ORIGIN_CPU);
592

593 594 595 596
	user_data = u64_to_user_ptr(args->data_ptr);
	offset = args->offset;
	remain = args->size;
	while (remain) {
597 598
		/* Operation in this page
		 *
599 600 601
		 * page_base = page offset within aperture
		 * page_offset = offset within page
		 * page_length = bytes to copy for this page
602
		 */
603
		u32 page_base = node.start;
604 605
		unsigned int page_offset = offset_in_page(offset);
		unsigned int page_length = PAGE_SIZE - page_offset;
606
		page_length = remain < page_length ? remain : page_length;
607
		if (drm_mm_node_allocated(&node)) {
608 609
			/* flush the write before we modify the GGTT */
			intel_gt_flush_ggtt_writes(ggtt->vm.gt);
610 611 612
			ggtt->vm.insert_page(&ggtt->vm,
					     i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
					     node.start, I915_CACHE_NONE, 0);
613 614 615 616
			wmb(); /* flush modifications to the GGTT (insert_page) */
		} else {
			page_base += offset & PAGE_MASK;
		}
617
		/* If we get a fault while copying data, then (presumably) our
618 619
		 * source page isn't available.  Return the error and we'll
		 * retry in the slow path.
620 621
		 * If the object is non-shmem backed, we retry again with the
		 * path that handles page fault.
622
		 */
623
		if (ggtt_write(&ggtt->iomap, page_base, page_offset,
624 625 626
			       user_data, page_length)) {
			ret = -EFAULT;
			break;
D
Daniel Vetter 已提交
627
		}
628

629 630 631
		remain -= page_length;
		user_data += page_length;
		offset += page_length;
632
	}
633
	intel_frontbuffer_flush(obj->frontbuffer, ORIGIN_CPU);
634

635
	i915_gem_object_unlock_fence(obj, fence);
D
Daniel Vetter 已提交
636
out_unpin:
637
	intel_gt_flush_ggtt_writes(ggtt->vm.gt);
638
	if (drm_mm_node_allocated(&node)) {
639
		ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
640
		remove_mappable_node(ggtt, &node);
641
	} else {
C
Chris Wilson 已提交
642
		i915_vma_unpin(vma);
643
	}
644
out_rpm:
645
	intel_runtime_pm_put(rpm, wakeref);
646
	return ret;
647 648
}

649 650 651 652 653
/* Per-page copy function for the shmem pwrite fastpath.
 * Flushes invalid cachelines before writing to the target if
 * needs_clflush_before is set and flushes out any written cachelines after
 * writing if needs_clflush is set.
 */
654
static int
655 656 657
shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
	     bool needs_clflush_before,
	     bool needs_clflush_after)
658
{
659
	char *vaddr;
660 661
	int ret;

662
	vaddr = kmap(page);
663

664 665
	if (needs_clflush_before)
		drm_clflush_virt_range(vaddr + offset, len);
666

667 668 669
	ret = __copy_from_user(vaddr + offset, user_data, len);
	if (!ret && needs_clflush_after)
		drm_clflush_virt_range(vaddr + offset, len);
670

671 672 673
	kunmap(page);

	return ret ? -EFAULT : 0;
674 675 676 677 678 679 680
}

static int
i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
		      const struct drm_i915_gem_pwrite *args)
{
	unsigned int partial_cacheline_write;
681
	unsigned int needs_clflush;
682
	unsigned int offset, idx;
683 684 685
	struct dma_fence *fence;
	void __user *user_data;
	u64 remain;
686
	int ret;
687

688
	ret = i915_gem_object_prepare_write(obj, &needs_clflush);
689 690
	if (ret)
		return ret;
691

692 693 694 695 696
	fence = i915_gem_object_lock_fence(obj);
	i915_gem_object_finish_access(obj);
	if (!fence)
		return -ENOMEM;

697 698 699 700 701 702 703
	/* If we don't overwrite a cacheline completely we need to be
	 * careful to have up-to-date data by first clflushing. Don't
	 * overcomplicate things and flush the entire patch.
	 */
	partial_cacheline_write = 0;
	if (needs_clflush & CLFLUSH_BEFORE)
		partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
704

705 706 707 708 709
	user_data = u64_to_user_ptr(args->data_ptr);
	remain = args->size;
	offset = offset_in_page(args->offset);
	for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
		struct page *page = i915_gem_object_get_page(obj, idx);
710
		unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
711

712 713 714
		ret = shmem_pwrite(page, offset, length, user_data,
				   (offset | length) & partial_cacheline_write,
				   needs_clflush & CLFLUSH_AFTER);
715
		if (ret)
716
			break;
717

718 719 720
		remain -= length;
		user_data += length;
		offset = 0;
721
	}
722

723
	intel_frontbuffer_flush(obj->frontbuffer, ORIGIN_CPU);
724 725
	i915_gem_object_unlock_fence(obj, fence);

726
	return ret;
727 728 729 730
}

/**
 * Writes data to the object referenced by handle.
731 732 733
 * @dev: drm device
 * @data: ioctl data blob
 * @file: drm file
734 735 736 737 738
 *
 * On error, the contents of the buffer that were to be modified are undefined.
 */
int
i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
739
		      struct drm_file *file)
740 741
{
	struct drm_i915_gem_pwrite *args = data;
742
	struct drm_i915_gem_object *obj;
743 744 745 746 747
	int ret;

	if (args->size == 0)
		return 0;

748
	if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
749 750
		return -EFAULT;

751
	obj = i915_gem_object_lookup(file, args->handle);
752 753
	if (!obj)
		return -ENOENT;
754

755
	/* Bounds check destination. */
756
	if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
C
Chris Wilson 已提交
757
		ret = -EINVAL;
758
		goto err;
C
Chris Wilson 已提交
759 760
	}

761 762 763 764 765 766
	/* Writes not allowed into this read-only object */
	if (i915_gem_object_is_readonly(obj)) {
		ret = -EINVAL;
		goto err;
	}

C
Chris Wilson 已提交
767 768
	trace_i915_gem_object_pwrite(obj, args->offset, args->size);

769 770 771 772 773 774
	ret = -ENODEV;
	if (obj->ops->pwrite)
		ret = obj->ops->pwrite(obj, args);
	if (ret != -ENODEV)
		goto err;

775 776 777
	ret = i915_gem_object_wait(obj,
				   I915_WAIT_INTERRUPTIBLE |
				   I915_WAIT_ALL,
778
				   MAX_SCHEDULE_TIMEOUT);
779 780 781
	if (ret)
		goto err;

782
	ret = i915_gem_object_pin_pages(obj);
783
	if (ret)
784
		goto err;
785

D
Daniel Vetter 已提交
786
	ret = -EFAULT;
787 788 789 790 791 792
	/* We can only do the GTT pwrite on untiled buffers, as otherwise
	 * it would end up going through the fenced access, and we'll get
	 * different detiling behavior between reading and writing.
	 * pread/pwrite currently are reading and writing from the CPU
	 * perspective, requiring manual detiling by the client.
	 */
793
	if (!i915_gem_object_has_struct_page(obj) ||
794
	    cpu_write_needs_clflush(obj))
D
Daniel Vetter 已提交
795 796
		/* Note that the gtt paths might fail with non-page-backed user
		 * pointers (e.g. gtt mappings when moving data between
797 798
		 * textures). Fallback to the shmem path in that case.
		 */
799
		ret = i915_gem_gtt_pwrite_fast(obj, args);
800

801
	if (ret == -EFAULT || ret == -ENOSPC) {
802 803
		if (obj->phys_handle)
			ret = i915_gem_phys_pwrite(obj, args, file);
804
		else
805
			ret = i915_gem_shmem_pwrite(obj, args);
806
	}
807

808
	i915_gem_object_unpin_pages(obj);
809
err:
C
Chris Wilson 已提交
810
	i915_gem_object_put(obj);
811
	return ret;
812 813 814 815
}

/**
 * Called when user space has done writes to this buffer
816 817 818
 * @dev: drm device
 * @data: ioctl data blob
 * @file: drm file
819 820 821
 */
int
i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
822
			 struct drm_file *file)
823 824
{
	struct drm_i915_gem_sw_finish *args = data;
825
	struct drm_i915_gem_object *obj;
826

827
	obj = i915_gem_object_lookup(file, args->handle);
828 829
	if (!obj)
		return -ENOENT;
830

T
Tina Zhang 已提交
831 832 833 834 835
	/*
	 * Proxy objects are barred from CPU access, so there is no
	 * need to ban sw_finish as it is a nop.
	 */

836
	/* Pinned buffers may be scanout, so flush the cache */
837
	i915_gem_object_flush_if_display(obj);
C
Chris Wilson 已提交
838
	i915_gem_object_put(obj);
839 840

	return 0;
841 842
}

843
void i915_gem_runtime_suspend(struct drm_i915_private *i915)
844
{
845
	struct drm_i915_gem_object *obj, *on;
846
	int i;
847

848 849 850 851 852 853
	/*
	 * Only called during RPM suspend. All users of the userfault_list
	 * must be holding an RPM wakeref to ensure that this can not
	 * run concurrently with themselves (and use the struct_mutex for
	 * protection between themselves).
	 */
854

855
	list_for_each_entry_safe(obj, on,
856
				 &i915->ggtt.userfault_list, userfault_link)
857
		__i915_gem_object_release_mmap(obj);
858

859 860
	/*
	 * The fence will be lost when the device powers down. If any were
861 862 863
	 * in use by hardware (i.e. they are pinned), we should not be powering
	 * down! All other fences will be reacquired by the user upon waking.
	 */
864 865
	for (i = 0; i < i915->ggtt.num_fences; i++) {
		struct i915_fence_reg *reg = &i915->ggtt.fence_regs[i];
866

867 868
		/*
		 * Ideally we want to assert that the fence register is not
869 870 871 872 873 874 875 876 877
		 * live at this point (i.e. that no piece of code will be
		 * trying to write through fence + GTT, as that both violates
		 * our tracking of activity and associated locking/barriers,
		 * but also is illegal given that the hw is powered down).
		 *
		 * Previously we used reg->pin_count as a "liveness" indicator.
		 * That is not sufficient, and we need a more fine-grained
		 * tool if we want to have a sanity check here.
		 */
878 879 880 881

		if (!reg->vma)
			continue;

882
		GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
883 884
		reg->dirty = true;
	}
885 886
}

C
Chris Wilson 已提交
887
struct i915_vma *
888 889
i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
			 const struct i915_ggtt_view *view,
890
			 u64 size,
891 892
			 u64 alignment,
			 u64 flags)
893
{
894 895
	struct drm_i915_private *i915 = to_i915(obj->base.dev);
	struct i915_ggtt *ggtt = &i915->ggtt;
896 897
	struct i915_vma *vma;
	int ret;
898

899 900 901
	if (i915_gem_object_never_bind_ggtt(obj))
		return ERR_PTR(-ENODEV);

902 903
	if (flags & PIN_MAPPABLE &&
	    (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
904 905
		/*
		 * If the required space is larger than the available
906 907 908 909 910 911
		 * aperture, we will not able to find a slot for the
		 * object and unbinding the object now will be in
		 * vain. Worse, doing so may cause us to ping-pong
		 * the object in and out of the Global GTT and
		 * waste a lot of cycles under the mutex.
		 */
912
		if (obj->base.size > ggtt->mappable_end)
913 914
			return ERR_PTR(-E2BIG);

915 916
		/*
		 * If NONBLOCK is set the caller is optimistically
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
		 * trying to cache the full object within the mappable
		 * aperture, and *must* have a fallback in place for
		 * situations where we cannot bind the object. We
		 * can be a little more lax here and use the fallback
		 * more often to avoid costly migrations of ourselves
		 * and other objects within the aperture.
		 *
		 * Half-the-aperture is used as a simple heuristic.
		 * More interesting would to do search for a free
		 * block prior to making the commitment to unbind.
		 * That caters for the self-harm case, and with a
		 * little more heuristics (e.g. NOFAULT, NOEVICT)
		 * we could try to minimise harm to others.
		 */
		if (flags & PIN_NONBLOCK &&
932
		    obj->base.size > ggtt->mappable_end / 2)
933 934 935
			return ERR_PTR(-ENOSPC);
	}

936
	vma = i915_vma_instance(obj, &ggtt->vm, view);
937
	if (IS_ERR(vma))
C
Chris Wilson 已提交
938
		return vma;
939 940

	if (i915_vma_misplaced(vma, size, alignment, flags)) {
941 942 943
		if (flags & PIN_NONBLOCK) {
			if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
				return ERR_PTR(-ENOSPC);
944

945
			if (flags & PIN_MAPPABLE &&
946
			    vma->fence_size > ggtt->mappable_end / 2)
947 948 949
				return ERR_PTR(-ENOSPC);
		}

950 951
		ret = i915_vma_unbind(vma);
		if (ret)
C
Chris Wilson 已提交
952
			return ERR_PTR(ret);
953 954
	}

955
	if (vma->fence && !i915_gem_object_is_tiled(obj)) {
956
		mutex_lock(&ggtt->vm.mutex);
957
		ret = i915_vma_revoke_fence(vma);
958
		mutex_unlock(&ggtt->vm.mutex);
959 960 961 962
		if (ret)
			return ERR_PTR(ret);
	}

963
	ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
C
Chris Wilson 已提交
964 965
	if (ret)
		return ERR_PTR(ret);
966

C
Chris Wilson 已提交
967
	return vma;
968 969
}

970 971 972 973
int
i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
		       struct drm_file *file_priv)
{
974
	struct drm_i915_private *i915 = to_i915(dev);
975
	struct drm_i915_gem_madvise *args = data;
976
	struct drm_i915_gem_object *obj;
977
	int err;
978 979 980 981 982 983 984 985 986

	switch (args->madv) {
	case I915_MADV_DONTNEED:
	case I915_MADV_WILLNEED:
	    break;
	default:
	    return -EINVAL;
	}

987
	obj = i915_gem_object_lookup(file_priv, args->handle);
988 989 990 991 992 993
	if (!obj)
		return -ENOENT;

	err = mutex_lock_interruptible(&obj->mm.lock);
	if (err)
		goto out;
994

995
	if (i915_gem_object_has_pages(obj) &&
996
	    i915_gem_object_is_tiled(obj) &&
997
	    i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
998 999
		if (obj->mm.madv == I915_MADV_WILLNEED) {
			GEM_BUG_ON(!obj->mm.quirked);
C
Chris Wilson 已提交
1000
			__i915_gem_object_unpin_pages(obj);
1001 1002 1003
			obj->mm.quirked = false;
		}
		if (args->madv == I915_MADV_WILLNEED) {
1004
			GEM_BUG_ON(obj->mm.quirked);
C
Chris Wilson 已提交
1005
			__i915_gem_object_pin_pages(obj);
1006 1007
			obj->mm.quirked = true;
		}
1008 1009
	}

C
Chris Wilson 已提交
1010 1011
	if (obj->mm.madv != __I915_MADV_PURGED)
		obj->mm.madv = args->madv;
1012

1013 1014 1015
	if (i915_gem_object_has_pages(obj)) {
		struct list_head *list;

1016
		if (i915_gem_object_is_shrinkable(obj)) {
1017 1018 1019 1020
			unsigned long flags;

			spin_lock_irqsave(&i915->mm.obj_lock, flags);

1021 1022 1023
			if (obj->mm.madv != I915_MADV_WILLNEED)
				list = &i915->mm.purge_list;
			else
1024
				list = &i915->mm.shrink_list;
1025
			list_move_tail(&obj->mm.link, list);
1026 1027

			spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
1028
		}
1029 1030
	}

C
Chris Wilson 已提交
1031
	/* if the object is no longer attached, discard its backing storage */
1032 1033
	if (obj->mm.madv == I915_MADV_DONTNEED &&
	    !i915_gem_object_has_pages(obj))
1034
		i915_gem_object_truncate(obj);
1035

C
Chris Wilson 已提交
1036
	args->retained = obj->mm.madv != __I915_MADV_PURGED;
1037
	mutex_unlock(&obj->mm.lock);
C
Chris Wilson 已提交
1038

1039
out:
1040
	i915_gem_object_put(obj);
1041
	return err;
1042 1043
}

1044
static int __intel_engines_record_defaults(struct intel_gt *gt)
1045
{
1046
	struct i915_request *requests[I915_NUM_ENGINES] = {};
1047 1048
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
1049
	int err = 0;
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059

	/*
	 * As we reset the gpu during very early sanitisation, the current
	 * register state on the GPU should reflect its defaults values.
	 * We load a context onto the hw (with restore-inhibit), then switch
	 * over to a second context to save that default register state. We
	 * can then prime every new context with that state so they all start
	 * from the same default HW values.
	 */

1060
	for_each_engine(engine, gt, id) {
1061
		struct intel_context *ce;
1062
		struct i915_request *rq;
1063

1064 1065 1066 1067
		/* We must be able to switch to something! */
		GEM_BUG_ON(!engine->kernel_context);
		engine->serial++; /* force the kernel context switch */

1068 1069
		ce = intel_context_create(engine->kernel_context->gem_context,
					  engine);
1070 1071 1072 1073 1074
		if (IS_ERR(ce)) {
			err = PTR_ERR(ce);
			goto out;
		}

1075
		rq = intel_context_create_request(ce);
1076 1077
		if (IS_ERR(rq)) {
			err = PTR_ERR(rq);
1078 1079
			intel_context_put(ce);
			goto out;
1080 1081
		}

1082 1083 1084 1085 1086 1087 1088
		err = intel_engine_emit_ctx_wa(rq);
		if (err)
			goto err_rq;

		err = intel_renderstate_emit(rq);
		if (err)
			goto err_rq;
1089

1090
err_rq:
1091
		requests[id] = i915_request_get(rq);
1092
		i915_request_add(rq);
1093
		if (err)
1094
			goto out;
1095 1096
	}

1097
	/* Flush the default context image to memory, and enable powersaving. */
1098
	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
1099
		err = -EIO;
1100
		goto out;
1101
	}
1102

1103 1104 1105
	for (id = 0; id < ARRAY_SIZE(requests); id++) {
		struct i915_request *rq;
		struct i915_vma *state;
1106
		void *vaddr;
1107

1108 1109
		rq = requests[id];
		if (!rq)
1110 1111
			continue;

1112 1113 1114 1115 1116 1117
		/* We want to be able to unbind the state from the GGTT */
		GEM_BUG_ON(intel_context_is_pinned(rq->hw_context));

		state = rq->hw_context->state;
		if (!state)
			continue;
1118

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
		/*
		 * As we will hold a reference to the logical state, it will
		 * not be torn down with the context, and importantly the
		 * object will hold onto its vma (making it possible for a
		 * stray GTT write to corrupt our defaults). Unmap the vma
		 * from the GTT to prevent such accidents and reclaim the
		 * space.
		 */
		err = i915_vma_unbind(state);
		if (err)
1129
			goto out;
1130

1131
		i915_gem_object_lock(state->obj);
1132
		err = i915_gem_object_set_to_cpu_domain(state->obj, false);
1133
		i915_gem_object_unlock(state->obj);
1134
		if (err)
1135
			goto out;
1136

1137
		i915_gem_object_set_cache_coherency(state->obj, I915_CACHE_LLC);
1138 1139

		/* Check we can acquire the image of the context state */
1140
		vaddr = i915_gem_object_pin_map(state->obj, I915_MAP_FORCE_WB);
1141 1142
		if (IS_ERR(vaddr)) {
			err = PTR_ERR(vaddr);
1143
			goto out;
1144 1145
		}

1146 1147
		rq->engine->default_state = i915_gem_object_get(state->obj);
		i915_gem_object_unpin_map(state->obj);
1148 1149
	}

1150
out:
1151 1152
	/*
	 * If we have to abandon now, we expect the engines to be idle
1153 1154
	 * and ready to be torn-down. The quickest way we can accomplish
	 * this is by declaring ourselves wedged.
1155
	 */
1156
	if (err)
1157
		intel_gt_set_wedged(gt);
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171

	for (id = 0; id < ARRAY_SIZE(requests); id++) {
		struct intel_context *ce;
		struct i915_request *rq;

		rq = requests[id];
		if (!rq)
			continue;

		ce = rq->hw_context;
		i915_request_put(rq);
		intel_context_put(ce);
	}
	return err;
1172 1173
}

1174
static int intel_engines_verify_workarounds(struct intel_gt *gt)
1175 1176 1177 1178 1179 1180 1181 1182
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	int err = 0;

	if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
		return 0;

1183
	for_each_engine(engine, gt, id) {
1184 1185 1186 1187 1188 1189 1190
		if (intel_engine_verify_workarounds(engine, "load"))
			err = -EIO;
	}

	return err;
}

1191
int i915_gem_init(struct drm_i915_private *dev_priv)
1192 1193 1194
{
	int ret;

1195 1196
	/* We need to fallback to 4K pages if host doesn't support huge gtt. */
	if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
1197 1198 1199
		mkwrite_device_info(dev_priv)->page_sizes =
			I915_GTT_PAGE_SIZE_4K;

1200 1201 1202 1203
	ret = i915_gem_init_userptr(dev_priv);
	if (ret)
		return ret;

1204
	intel_uc_fetch_firmwares(&dev_priv->gt.uc);
1205
	intel_wopcm_init(&dev_priv->wopcm);
1206

1207 1208 1209 1210 1211 1212
	/* This is just a security blanket to placate dragons.
	 * On some systems, we very sporadically observe that the first TLBs
	 * used by the CS may be stale, despite us poking the TLB reset. If
	 * we hold the forcewake during initialisation these problems
	 * just magically go away.
	 */
1213
	intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
1214

1215
	ret = i915_init_ggtt(dev_priv);
1216 1217 1218 1219
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_unlock;
	}
1220

1221
	intel_gt_init(&dev_priv->gt);
1222

1223
	ret = intel_engines_setup(&dev_priv->gt);
1224 1225 1226 1227 1228
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_unlock;
	}

1229
	ret = i915_gem_init_contexts(dev_priv);
1230 1231 1232 1233 1234
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_scratch;
	}

1235
	ret = intel_engines_init(&dev_priv->gt);
1236 1237 1238 1239
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_context;
	}
1240

1241
	intel_uc_init(&dev_priv->gt.uc);
1242

1243
	ret = intel_gt_init_hw(&dev_priv->gt);
1244 1245 1246
	if (ret)
		goto err_uc_init;

1247 1248 1249 1250 1251
	/* Only when the HW is re-initialised, can we replay the requests */
	ret = intel_gt_resume(&dev_priv->gt);
	if (ret)
		goto err_init_hw;

1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
	/*
	 * Despite its name intel_init_clock_gating applies both display
	 * clock gating workarounds; GT mmio workarounds and the occasional
	 * GT power context workaround. Worse, sometimes it includes a context
	 * register workaround which we need to apply before we record the
	 * default HW state for all contexts.
	 *
	 * FIXME: break up the workarounds and apply them at the right time!
	 */
	intel_init_clock_gating(dev_priv);

1263
	ret = intel_engines_verify_workarounds(&dev_priv->gt);
1264
	if (ret)
1265
		goto err_gt;
1266

1267
	ret = __intel_engines_record_defaults(&dev_priv->gt);
1268
	if (ret)
1269
		goto err_gt;
1270

1271
	ret = i915_inject_probe_error(dev_priv, -ENODEV);
1272
	if (ret)
1273
		goto err_gt;
1274

1275
	ret = i915_inject_probe_error(dev_priv, -EIO);
1276
	if (ret)
1277
		goto err_gt;
1278

1279
	intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
1280 1281 1282 1283 1284 1285 1286 1287 1288

	return 0;

	/*
	 * Unwinding is complicated by that we want to handle -EIO to mean
	 * disable GPU submission but keep KMS alive. We want to mark the
	 * HW as irrevisibly wedged, but keep enough state around that the
	 * driver doesn't explode during runtime.
	 */
1289
err_gt:
1290
	intel_gt_set_wedged_on_init(&dev_priv->gt);
1291
	i915_gem_suspend(dev_priv);
1292 1293
	i915_gem_suspend_late(dev_priv);

1294
	i915_gem_drain_workqueue(dev_priv);
1295
err_init_hw:
1296
	intel_uc_fini_hw(&dev_priv->gt.uc);
1297
err_uc_init:
1298
	if (ret != -EIO) {
1299
		intel_uc_fini(&dev_priv->gt.uc);
1300
		intel_engines_cleanup(&dev_priv->gt);
1301 1302 1303
	}
err_context:
	if (ret != -EIO)
1304
		i915_gem_driver_release__contexts(dev_priv);
1305
err_scratch:
1306
	intel_gt_driver_release(&dev_priv->gt);
1307
err_unlock:
1308
	intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
1309

1310
	if (ret != -EIO) {
1311
		intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1312
		i915_gem_cleanup_userptr(dev_priv);
1313
	}
1314

1315
	if (ret == -EIO) {
1316
		/*
1317 1318
		 * Allow engines or uC initialisation to fail by marking the GPU
		 * as wedged. But we only want to do this when the GPU is angry,
1319 1320
		 * for all other failure, such as an allocation failure, bail.
		 */
1321
		if (!intel_gt_is_wedged(&dev_priv->gt)) {
1322 1323
			i915_probe_error(dev_priv,
					 "Failed to initialize GPU, declaring it wedged!\n");
1324
			intel_gt_set_wedged(&dev_priv->gt);
1325
		}
1326 1327 1328 1329

		/* Minimal basic recovery for KMS */
		ret = i915_ggtt_enable_hw(dev_priv);
		i915_gem_restore_gtt_mappings(dev_priv);
1330
		i915_gem_restore_fences(&dev_priv->ggtt);
1331
		intel_init_clock_gating(dev_priv);
1332 1333
	}

1334
	i915_gem_drain_freed_objects(dev_priv);
1335
	return ret;
1336 1337
}

1338 1339 1340
void i915_gem_driver_register(struct drm_i915_private *i915)
{
	i915_gem_driver_register__shrinker(i915);
1341 1342

	intel_engines_driver_register(i915);
1343 1344 1345 1346 1347 1348 1349
}

void i915_gem_driver_unregister(struct drm_i915_private *i915)
{
	i915_gem_driver_unregister__shrinker(i915);
}

1350
void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
1351
{
1352
	intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
1353

1354
	i915_gem_suspend_late(dev_priv);
1355
	intel_gt_driver_remove(&dev_priv->gt);
1356 1357 1358 1359

	/* Flush any outstanding unpin_work. */
	i915_gem_drain_workqueue(dev_priv);

1360 1361
	intel_uc_fini_hw(&dev_priv->gt.uc);
	intel_uc_fini(&dev_priv->gt.uc);
1362 1363 1364 1365

	i915_gem_drain_freed_objects(dev_priv);
}

1366
void i915_gem_driver_release(struct drm_i915_private *dev_priv)
1367
{
1368
	intel_engines_cleanup(&dev_priv->gt);
1369
	i915_gem_driver_release__contexts(dev_priv);
1370
	intel_gt_driver_release(&dev_priv->gt);
1371

1372 1373
	intel_wa_list_free(&dev_priv->gt_wa_list);

1374
	intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1375 1376 1377 1378
	i915_gem_cleanup_userptr(dev_priv);

	i915_gem_drain_freed_objects(dev_priv);

1379
	WARN_ON(!list_empty(&dev_priv->gem.contexts.list));
1380 1381
}

1382 1383 1384 1385 1386 1387
static void i915_gem_init__mm(struct drm_i915_private *i915)
{
	spin_lock_init(&i915->mm.obj_lock);

	init_llist_head(&i915->mm.free_list);

1388
	INIT_LIST_HEAD(&i915->mm.purge_list);
1389
	INIT_LIST_HEAD(&i915->mm.shrink_list);
1390

1391
	i915_gem_init__objects(i915);
1392 1393
}

1394
void i915_gem_init_early(struct drm_i915_private *dev_priv)
1395
{
1396
	i915_gem_init__mm(dev_priv);
1397

1398
	spin_lock_init(&dev_priv->fb_tracking.lock);
1399
}
1400

1401
void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
1402
{
1403
	i915_gem_drain_freed_objects(dev_priv);
1404 1405
	GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
	GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
1406
	WARN_ON(dev_priv->mm.shrink_count);
1407 1408
}

1409 1410
int i915_gem_freeze(struct drm_i915_private *dev_priv)
{
1411 1412 1413
	/* Discard all purgeable objects, let userspace recover those as
	 * required after resuming.
	 */
1414 1415 1416 1417 1418
	i915_gem_shrink_all(dev_priv);

	return 0;
}

1419
int i915_gem_freeze_late(struct drm_i915_private *i915)
1420 1421
{
	struct drm_i915_gem_object *obj;
1422
	intel_wakeref_t wakeref;
1423

1424 1425
	/*
	 * Called just before we write the hibernation image.
1426 1427 1428 1429 1430 1431 1432 1433
	 *
	 * We need to update the domain tracking to reflect that the CPU
	 * will be accessing all the pages to create and restore from the
	 * hibernation, and so upon restoration those pages will be in the
	 * CPU domain.
	 *
	 * To make sure the hibernation image contains the latest state,
	 * we update that state just before writing out the image.
1434 1435
	 *
	 * To try and reduce the hibernation image, we manually shrink
1436
	 * the objects as well, see i915_gem_freeze()
1437 1438
	 */

1439
	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1440 1441

	i915_gem_shrink(i915, -1UL, NULL, ~0);
1442
	i915_gem_drain_freed_objects(i915);
1443

1444 1445 1446 1447
	list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
		i915_gem_object_lock(obj);
		WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
		i915_gem_object_unlock(obj);
1448
	}
1449

1450
	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1451 1452 1453 1454

	return 0;
}

1455
void i915_gem_release(struct drm_device *dev, struct drm_file *file)
1456
{
1457
	struct drm_i915_file_private *file_priv = file->driver_priv;
1458
	struct i915_request *request;
1459 1460 1461 1462 1463

	/* Clean up our request list when the client is going away, so that
	 * later retire_requests won't dereference our soon-to-be-gone
	 * file_priv.
	 */
1464
	spin_lock(&file_priv->mm.lock);
1465
	list_for_each_entry(request, &file_priv->mm.request_list, client_link)
1466
		request->file_priv = NULL;
1467
	spin_unlock(&file_priv->mm.lock);
1468 1469
}

1470
int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
1471 1472
{
	struct drm_i915_file_private *file_priv;
1473
	int ret;
1474

1475
	DRM_DEBUG("\n");
1476 1477 1478 1479 1480 1481

	file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
	if (!file_priv)
		return -ENOMEM;

	file->driver_priv = file_priv;
1482
	file_priv->dev_priv = i915;
1483
	file_priv->file = file;
1484 1485 1486 1487

	spin_lock_init(&file_priv->mm.lock);
	INIT_LIST_HEAD(&file_priv->mm.request_list);

1488
	file_priv->bsd_engine = -1;
1489
	file_priv->hang_timestamp = jiffies;
1490

1491
	ret = i915_gem_context_open(i915, file);
1492 1493
	if (ret)
		kfree(file_priv);
1494

1495
	return ret;
1496 1497
}

1498
#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1499
#include "selftests/mock_gem_device.c"
1500
#include "selftests/i915_gem.c"
1501
#endif