i915_gem.c 38.0 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_context.h"
49
#include "gt/intel_engine_user.h"
50
#include "gt/intel_gt.h"
51
#include "gt/intel_gt_pm.h"
52
#include "gt/intel_gt_requests.h"
53 54
#include "gt/intel_mocs.h"
#include "gt/intel_reset.h"
55
#include "gt/intel_renderstate.h"
56
#include "gt/intel_rps.h"
57 58
#include "gt/intel_workarounds.h"

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

64
#include "intel_pm.h"
65

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

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

75
	memset(node, 0, sizeof(*node));
76 77 78 79 80 81 82 83
	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;
84 85 86
}

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

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

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

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

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

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

116 117 118
	return 0;
}

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

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

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

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

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

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

	return ret;
}

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

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

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

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

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

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

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

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

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

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

207 208 209 210 211
int
i915_gem_dumb_create(struct drm_file *file,
		     struct drm_device *dev,
		     struct drm_mode_create_dumb *args)
{
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	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;
	}

229
	/* have to work out size/pitch and return them */
230 231 232 233 234 235 236
	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);

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

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

255
	i915_gem_flush_free_objects(dev_priv);
256

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

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

	vaddr = kmap(page);

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

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

275
	kunmap(page);
276

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

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;
286 287 288
	struct dma_fence *fence;
	char __user *user_data;
	u64 remain;
289 290
	int ret;

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

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

300 301 302 303 304
	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);
305
		unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
306 307 308 309 310 311 312 313 314 315 316

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

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

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

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

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

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

359
	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
360 361 362 363 364 365
	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);
366 367
	if (!IS_ERR(vma)) {
		node.start = i915_ggtt_offset(vma);
368
		node.flags = 0;
369
	} else {
370
		ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
371
		if (ret)
372
			goto out_rpm;
373
		GEM_BUG_ON(!drm_mm_node_allocated(&node));
374 375
	}

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

380 381 382 383 384 385 386 387 388 389 390 391
	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;
	}
392

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

	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;
408
		if (drm_mm_node_allocated(&node)) {
409 410 411
			ggtt->vm.insert_page(&ggtt->vm,
					     i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
					     node.start, I915_CACHE_NONE, 0);
412 413 414
		} else {
			page_base += offset & PAGE_MASK;
		}
415

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return unwritten;
}

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

543 544 545 546 547 548 549 550
	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.
		 */
551
		wakeref = intel_runtime_pm_get_if_in_use(rpm);
552 553
		if (!wakeref)
			return -EFAULT;
554 555
	} else {
		/* No backing pages, no fallback, we must force GGTT access */
556
		wakeref = intel_runtime_pm_get(rpm);
557 558
	}

559 560 561 562 563 564
	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);
565 566
	if (!IS_ERR(vma)) {
		node.start = i915_ggtt_offset(vma);
567
		node.flags = 0;
568
	} else {
569
		ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
570
		if (ret)
571
			goto out_rpm;
572
		GEM_BUG_ON(!drm_mm_node_allocated(&node));
573
	}
D
Daniel Vetter 已提交
574

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

579 580 581 582 583 584 585 586 587 588 589 590
	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;
	}
591

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

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

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

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

650 651 652 653 654
/* 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.
 */
655
static int
656 657 658
shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
	     bool needs_clflush_before,
	     bool needs_clflush_after)
659
{
660
	char *vaddr;
661 662
	int ret;

663
	vaddr = kmap(page);
664

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

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

672 673 674
	kunmap(page);

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

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

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

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

698 699 700 701 702 703 704
	/* 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;
705

706 707 708 709 710
	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);
711
		unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
712

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

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

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

727
	return ret;
728 729 730 731
}

/**
 * Writes data to the object referenced by handle.
732 733 734
 * @dev: drm device
 * @data: ioctl data blob
 * @file: drm file
735 736 737 738 739
 *
 * 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,
740
		      struct drm_file *file)
741 742
{
	struct drm_i915_gem_pwrite *args = data;
743
	struct drm_i915_gem_object *obj;
744 745 746 747 748
	int ret;

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

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

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

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

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

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

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

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

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

D
Daniel Vetter 已提交
787
	ret = -EFAULT;
788 789 790 791 792 793
	/* 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.
	 */
794
	if (!i915_gem_object_has_struct_page(obj) ||
795
	    cpu_write_needs_clflush(obj))
D
Daniel Vetter 已提交
796 797
		/* Note that the gtt paths might fail with non-page-backed user
		 * pointers (e.g. gtt mappings when moving data between
798 799
		 * textures). Fallback to the shmem path in that case.
		 */
800
		ret = i915_gem_gtt_pwrite_fast(obj, args);
801

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

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

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

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

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

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

	return 0;
842 843
}

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

849 850 851 852 853 854
	/*
	 * 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).
	 */
855

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

860 861
	/*
	 * The fence will be lost when the device powers down. If any were
862 863 864
	 * 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.
	 */
865 866
	for (i = 0; i < i915->ggtt.num_fences; i++) {
		struct i915_fence_reg *reg = &i915->ggtt.fence_regs[i];
867

868 869
		/*
		 * Ideally we want to assert that the fence register is not
870 871 872 873 874 875 876 877 878
		 * 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.
		 */
879 880 881 882

		if (!reg->vma)
			continue;

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

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

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

903 904
	if (flags & PIN_MAPPABLE &&
	    (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
905 906
		/*
		 * If the required space is larger than the available
907 908 909 910 911 912
		 * 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.
		 */
913
		if (obj->base.size > ggtt->mappable_end)
914 915
			return ERR_PTR(-E2BIG);

916 917
		/*
		 * If NONBLOCK is set the caller is optimistically
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
		 * 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 &&
933
		    obj->base.size > ggtt->mappable_end / 2)
934 935 936
			return ERR_PTR(-ENOSPC);
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
static int __intel_context_flush_retire(struct intel_context *ce)
{
	struct intel_timeline *tl;

	tl = intel_context_timeline_lock(ce);
	if (IS_ERR(tl))
		return PTR_ERR(tl);

	intel_context_timeline_unlock(tl);
	return 0;
}

1057
static int __intel_engines_record_defaults(struct intel_gt *gt)
1058
{
1059
	struct i915_request *requests[I915_NUM_ENGINES] = {};
1060 1061
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
1062
	int err = 0;
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

	/*
	 * 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.
	 */

1073
	for_each_engine(engine, gt, id) {
1074
		struct intel_context *ce;
1075
		struct i915_request *rq;
1076

1077 1078 1079 1080
		/* We must be able to switch to something! */
		GEM_BUG_ON(!engine->kernel_context);
		engine->serial++; /* force the kernel context switch */

1081 1082
		ce = intel_context_create(engine->kernel_context->gem_context,
					  engine);
1083 1084 1085 1086 1087
		if (IS_ERR(ce)) {
			err = PTR_ERR(ce);
			goto out;
		}

1088
		rq = intel_context_create_request(ce);
1089 1090
		if (IS_ERR(rq)) {
			err = PTR_ERR(rq);
1091 1092
			intel_context_put(ce);
			goto out;
1093 1094
		}

1095 1096 1097 1098 1099 1100 1101
		err = intel_engine_emit_ctx_wa(rq);
		if (err)
			goto err_rq;

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

1103
err_rq:
1104
		requests[id] = i915_request_get(rq);
1105
		i915_request_add(rq);
1106
		if (err)
1107
			goto out;
1108 1109
	}

1110
	/* Flush the default context image to memory, and enable powersaving. */
1111
	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
1112
		err = -EIO;
1113
		goto out;
1114
	}
1115

1116 1117 1118
	for (id = 0; id < ARRAY_SIZE(requests); id++) {
		struct i915_request *rq;
		struct i915_vma *state;
1119
		void *vaddr;
1120

1121 1122
		rq = requests[id];
		if (!rq)
1123 1124
			continue;

1125 1126
		GEM_BUG_ON(!test_bit(CONTEXT_ALLOC_BIT,
				     &rq->hw_context->flags));
1127 1128 1129
		state = rq->hw_context->state;
		if (!state)
			continue;
1130

1131 1132 1133 1134 1135 1136 1137 1138
		/* Serialise with retirement on another CPU */
		err = __intel_context_flush_retire(rq->hw_context);
		if (err)
			goto out;

		/* We want to be able to unbind the state from the GGTT */
		GEM_BUG_ON(intel_context_is_pinned(rq->hw_context));

1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
		/*
		 * 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)
1149
			goto out;
1150

1151
		i915_gem_object_lock(state->obj);
1152
		err = i915_gem_object_set_to_cpu_domain(state->obj, false);
1153
		i915_gem_object_unlock(state->obj);
1154
		if (err)
1155
			goto out;
1156

1157
		i915_gem_object_set_cache_coherency(state->obj, I915_CACHE_LLC);
1158 1159

		/* Check we can acquire the image of the context state */
1160
		vaddr = i915_gem_object_pin_map(state->obj, I915_MAP_FORCE_WB);
1161 1162
		if (IS_ERR(vaddr)) {
			err = PTR_ERR(vaddr);
1163
			goto out;
1164 1165
		}

1166 1167
		rq->engine->default_state = i915_gem_object_get(state->obj);
		i915_gem_object_unpin_map(state->obj);
1168 1169
	}

1170
out:
1171 1172
	/*
	 * If we have to abandon now, we expect the engines to be idle
1173 1174
	 * and ready to be torn-down. The quickest way we can accomplish
	 * this is by declaring ourselves wedged.
1175
	 */
1176
	if (err)
1177
		intel_gt_set_wedged(gt);
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

	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;
1192 1193
}

1194
static int intel_engines_verify_workarounds(struct intel_gt *gt)
1195 1196 1197 1198 1199 1200 1201 1202
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	int err = 0;

	if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
		return 0;

1203
	for_each_engine(engine, gt, id) {
1204 1205 1206 1207 1208 1209 1210
		if (intel_engine_verify_workarounds(engine, "load"))
			err = -EIO;
	}

	return err;
}

1211
int i915_gem_init(struct drm_i915_private *dev_priv)
1212 1213 1214
{
	int ret;

1215 1216
	/* 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))
1217 1218 1219
		mkwrite_device_info(dev_priv)->page_sizes =
			I915_GTT_PAGE_SIZE_4K;

1220 1221 1222 1223
	ret = i915_gem_init_userptr(dev_priv);
	if (ret)
		return ret;

1224
	intel_uc_fetch_firmwares(&dev_priv->gt.uc);
1225
	intel_wopcm_init(&dev_priv->wopcm);
1226

1227 1228 1229 1230 1231 1232
	/* 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.
	 */
1233
	intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
1234

1235
	ret = i915_init_ggtt(dev_priv);
1236 1237 1238 1239
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_unlock;
	}
1240

1241
	intel_gt_init(&dev_priv->gt);
1242

1243
	ret = intel_engines_setup(&dev_priv->gt);
1244 1245 1246 1247 1248
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_unlock;
	}

1249
	ret = i915_gem_init_contexts(dev_priv);
1250 1251 1252 1253 1254
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_scratch;
	}

1255
	ret = intel_engines_init(&dev_priv->gt);
1256 1257 1258 1259
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_context;
	}
1260

1261
	intel_uc_init(&dev_priv->gt.uc);
1262

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

1267 1268 1269 1270 1271
	/* 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;

1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
	/*
	 * 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);

1283
	ret = intel_engines_verify_workarounds(&dev_priv->gt);
1284
	if (ret)
1285
		goto err_gt;
1286

1287
	ret = __intel_engines_record_defaults(&dev_priv->gt);
1288
	if (ret)
1289
		goto err_gt;
1290

1291
	ret = i915_inject_probe_error(dev_priv, -ENODEV);
1292
	if (ret)
1293
		goto err_gt;
1294

1295
	ret = i915_inject_probe_error(dev_priv, -EIO);
1296
	if (ret)
1297
		goto err_gt;
1298

1299
	intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
1300 1301 1302 1303 1304 1305 1306 1307 1308

	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.
	 */
1309
err_gt:
1310
	intel_gt_set_wedged_on_init(&dev_priv->gt);
1311
	i915_gem_suspend(dev_priv);
1312 1313
	i915_gem_suspend_late(dev_priv);

1314
	i915_gem_drain_workqueue(dev_priv);
1315
err_init_hw:
1316
	intel_uc_fini_hw(&dev_priv->gt.uc);
1317
err_uc_init:
1318
	if (ret != -EIO) {
1319
		intel_uc_fini(&dev_priv->gt.uc);
1320
		intel_engines_cleanup(&dev_priv->gt);
1321 1322 1323
	}
err_context:
	if (ret != -EIO)
1324
		i915_gem_driver_release__contexts(dev_priv);
1325
err_scratch:
1326
	intel_gt_driver_release(&dev_priv->gt);
1327
err_unlock:
1328
	intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
1329

1330
	if (ret != -EIO) {
1331
		intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1332
		i915_gem_cleanup_userptr(dev_priv);
1333
	}
1334

1335
	if (ret == -EIO) {
1336
		/*
1337 1338
		 * 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,
1339 1340
		 * for all other failure, such as an allocation failure, bail.
		 */
1341
		if (!intel_gt_is_wedged(&dev_priv->gt)) {
1342 1343
			i915_probe_error(dev_priv,
					 "Failed to initialize GPU, declaring it wedged!\n");
1344
			intel_gt_set_wedged(&dev_priv->gt);
1345
		}
1346 1347 1348 1349

		/* Minimal basic recovery for KMS */
		ret = i915_ggtt_enable_hw(dev_priv);
		i915_gem_restore_gtt_mappings(dev_priv);
1350
		i915_gem_restore_fences(&dev_priv->ggtt);
1351
		intel_init_clock_gating(dev_priv);
1352 1353
	}

1354
	i915_gem_drain_freed_objects(dev_priv);
1355
	return ret;
1356 1357
}

1358 1359 1360
void i915_gem_driver_register(struct drm_i915_private *i915)
{
	i915_gem_driver_register__shrinker(i915);
1361 1362

	intel_engines_driver_register(i915);
1363 1364 1365 1366 1367 1368 1369
}

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

1370
void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
1371
{
1372
	intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
1373

1374
	i915_gem_suspend_late(dev_priv);
1375
	intel_gt_driver_remove(&dev_priv->gt);
1376 1377 1378 1379

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

1380 1381
	intel_uc_fini_hw(&dev_priv->gt.uc);
	intel_uc_fini(&dev_priv->gt.uc);
1382 1383 1384 1385

	i915_gem_drain_freed_objects(dev_priv);
}

1386
void i915_gem_driver_release(struct drm_i915_private *dev_priv)
1387
{
1388
	intel_engines_cleanup(&dev_priv->gt);
1389
	i915_gem_driver_release__contexts(dev_priv);
1390
	intel_gt_driver_release(&dev_priv->gt);
1391

1392 1393
	intel_wa_list_free(&dev_priv->gt_wa_list);

1394
	intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1395 1396 1397 1398
	i915_gem_cleanup_userptr(dev_priv);

	i915_gem_drain_freed_objects(dev_priv);

1399
	WARN_ON(!list_empty(&dev_priv->gem.contexts.list));
1400 1401
}

1402 1403 1404 1405 1406 1407
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);

1408
	INIT_LIST_HEAD(&i915->mm.purge_list);
1409
	INIT_LIST_HEAD(&i915->mm.shrink_list);
1410

1411
	i915_gem_init__objects(i915);
1412 1413
}

1414
void i915_gem_init_early(struct drm_i915_private *dev_priv)
1415
{
1416
	i915_gem_init__mm(dev_priv);
1417

1418
	spin_lock_init(&dev_priv->fb_tracking.lock);
1419
}
1420

1421
void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
1422
{
1423
	i915_gem_drain_freed_objects(dev_priv);
1424 1425
	GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
	GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
1426
	WARN_ON(dev_priv->mm.shrink_count);
1427 1428
}

1429 1430
int i915_gem_freeze(struct drm_i915_private *dev_priv)
{
1431 1432 1433
	/* Discard all purgeable objects, let userspace recover those as
	 * required after resuming.
	 */
1434 1435 1436 1437 1438
	i915_gem_shrink_all(dev_priv);

	return 0;
}

1439
int i915_gem_freeze_late(struct drm_i915_private *i915)
1440 1441
{
	struct drm_i915_gem_object *obj;
1442
	intel_wakeref_t wakeref;
1443

1444 1445
	/*
	 * Called just before we write the hibernation image.
1446 1447 1448 1449 1450 1451 1452 1453
	 *
	 * 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.
1454 1455
	 *
	 * To try and reduce the hibernation image, we manually shrink
1456
	 * the objects as well, see i915_gem_freeze()
1457 1458
	 */

1459
	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1460 1461

	i915_gem_shrink(i915, -1UL, NULL, ~0);
1462
	i915_gem_drain_freed_objects(i915);
1463

1464 1465 1466 1467
	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);
1468
	}
1469

1470
	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1471 1472 1473 1474

	return 0;
}

1475
void i915_gem_release(struct drm_device *dev, struct drm_file *file)
1476
{
1477
	struct drm_i915_file_private *file_priv = file->driver_priv;
1478
	struct i915_request *request;
1479 1480 1481 1482 1483

	/* 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.
	 */
1484
	spin_lock(&file_priv->mm.lock);
1485
	list_for_each_entry(request, &file_priv->mm.request_list, client_link)
1486
		request->file_priv = NULL;
1487
	spin_unlock(&file_priv->mm.lock);
1488 1489
}

1490
int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
1491 1492
{
	struct drm_i915_file_private *file_priv;
1493
	int ret;
1494

1495
	DRM_DEBUG("\n");
1496 1497 1498 1499 1500 1501

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

	file->driver_priv = file_priv;
1502
	file_priv->dev_priv = i915;
1503
	file_priv->file = file;
1504 1505 1506 1507

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

1508
	file_priv->bsd_engine = -1;
1509
	file_priv->hang_timestamp = jiffies;
1510

1511
	ret = i915_gem_context_open(i915, file);
1512 1513
	if (ret)
		kfree(file_priv);
1514

1515
	return ret;
1516 1517
}

1518
#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1519
#include "selftests/mock_gem_device.c"
1520
#include "selftests/i915_gem.c"
1521
#endif