i915_gem.c 39.6 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 48
#include "gem/i915_gem_pm.h"
#include "gem/i915_gemfs.h"
49
#include "gt/intel_engine_user.h"
50
#include "gt/intel_gt.h"
51
#include "gt/intel_gt_pm.h"
52 53
#include "gt/intel_mocs.h"
#include "gt/intel_reset.h"
54
#include "gt/intel_renderstate.h"
55 56
#include "gt/intel_workarounds.h"

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

62
#include "intel_pm.h"
63

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

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

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

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

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

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

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

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

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

114 115 116
	return 0;
}

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

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

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

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

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

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

	return ret;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

253
	i915_gem_flush_free_objects(dev_priv);
254

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

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

	vaddr = kmap(page);

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

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

273
	kunmap(page);
274

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return unwritten;
}

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

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

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

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

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

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

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

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

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

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

661
	vaddr = kmap(page);
662

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

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

670 671 672
	kunmap(page);

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

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

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

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

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

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

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

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

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

725
	return ret;
726 727 728 729
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return 0;
840 841
}

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

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

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

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

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

		if (!reg->vma)
			continue;

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

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

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

901 902
	if (flags & PIN_MAPPABLE &&
	    (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
		/* If the required space is larger than the available
		 * 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.
		 */
		if (obj->base.size > dev_priv->ggtt.mappable_end)
			return ERR_PTR(-E2BIG);

		/* If NONBLOCK is set the caller is optimistically
		 * 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 &&
		    obj->base.size > dev_priv->ggtt.mappable_end / 2)
			return ERR_PTR(-ENOSPC);
	}

933
	vma = i915_vma_instance(obj, vm, view);
934
	if (IS_ERR(vma))
C
Chris Wilson 已提交
935
		return vma;
936 937

	if (i915_vma_misplaced(vma, size, alignment, flags)) {
938 939 940
		if (flags & PIN_NONBLOCK) {
			if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
				return ERR_PTR(-ENOSPC);
941

942
			if (flags & PIN_MAPPABLE &&
943
			    vma->fence_size > dev_priv->ggtt.mappable_end / 2)
944 945 946
				return ERR_PTR(-ENOSPC);
		}

947 948
		ret = i915_vma_unbind(vma);
		if (ret)
C
Chris Wilson 已提交
949
			return ERR_PTR(ret);
950 951
	}

952 953 954 955 956 957 958 959
	if (vma->fence && !i915_gem_object_is_tiled(obj)) {
		mutex_lock(&vma->vm->mutex);
		ret = i915_vma_revoke_fence(vma);
		mutex_unlock(&vma->vm->mutex);
		if (ret)
			return ERR_PTR(ret);
	}

C
Chris Wilson 已提交
960 961 962
	ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
	if (ret)
		return ERR_PTR(ret);
963

C
Chris Wilson 已提交
964
	return vma;
965 966
}

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

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

984
	obj = i915_gem_object_lookup(file_priv, args->handle);
985 986 987 988 989 990
	if (!obj)
		return -ENOENT;

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

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

C
Chris Wilson 已提交
1007 1008
	if (obj->mm.madv != __I915_MADV_PURGED)
		obj->mm.madv = args->madv;
1009

1010 1011 1012
	if (i915_gem_object_has_pages(obj)) {
		struct list_head *list;

1013
		if (i915_gem_object_is_shrinkable(obj)) {
1014 1015 1016 1017
			unsigned long flags;

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

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

			spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
1025
		}
1026 1027
	}

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

C
Chris Wilson 已提交
1033
	args->retained = obj->mm.madv != __I915_MADV_PURGED;
1034
	mutex_unlock(&obj->mm.lock);
C
Chris Wilson 已提交
1035

1036
out:
1037
	i915_gem_object_put(obj);
1038
	return err;
1039 1040
}

1041 1042
void i915_gem_sanitize(struct drm_i915_private *i915)
{
1043 1044
	intel_wakeref_t wakeref;

1045 1046
	GEM_TRACE("\n");

1047
	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1048
	intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL);
1049 1050 1051 1052 1053 1054 1055

	/*
	 * As we have just resumed the machine and woken the device up from
	 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
	 * back to defaults, recovering from whatever wedged state we left it
	 * in and so worth trying to use the device once more.
	 */
1056 1057
	if (intel_gt_is_wedged(&i915->gt))
		intel_gt_unset_wedged(&i915->gt);
1058

1059 1060 1061 1062 1063 1064
	/*
	 * If we inherit context state from the BIOS or earlier occupants
	 * of the GPU, the GPU may be in an inconsistent state when we
	 * try to take over. The only way to remove the earlier state
	 * is by resetting. However, resetting on earlier gen is tricky as
	 * it may impact the display and we are uncertain about the stability
1065
	 * of the reset, so this could be applied to even earlier gen.
1066
	 */
1067
	intel_gt_sanitize(&i915->gt, false);
1068

1069
	intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL);
1070
	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1071 1072
}

1073 1074
static int __intel_engines_record_defaults(struct drm_i915_private *i915)
{
1075
	struct i915_request *requests[I915_NUM_ENGINES] = {};
1076 1077
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
1078
	int err = 0;
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089

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

	for_each_engine(engine, i915, id) {
1090
		struct intel_context *ce;
1091
		struct i915_request *rq;
1092

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
		/* We must be able to switch to something! */
		GEM_BUG_ON(!engine->kernel_context);
		engine->serial++; /* force the kernel context switch */

		ce = intel_context_create(i915->kernel_context, engine);
		if (IS_ERR(ce)) {
			err = PTR_ERR(ce);
			goto out;
		}

1103
		rq = intel_context_create_request(ce);
1104 1105
		if (IS_ERR(rq)) {
			err = PTR_ERR(rq);
1106 1107
			intel_context_put(ce);
			goto out;
1108 1109
		}

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
		err = intel_engine_emit_ctx_wa(rq);
		if (err)
			goto err_rq;

		/*
		 * Failing to program the MOCS is non-fatal.The system will not
		 * run at peak performance. So warn the user and carry on.
		 */
		err = intel_mocs_emit(rq);
		if (err)
			dev_notice(i915->drm.dev,
				   "Failed to program MOCS registers; expect performance issues.\n");

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

1127
err_rq:
1128
		requests[id] = i915_request_get(rq);
1129
		i915_request_add(rq);
1130
		if (err)
1131
			goto out;
1132 1133
	}

1134
	/* Flush the default context image to memory, and enable powersaving. */
1135
	if (!i915_gem_load_power_context(i915)) {
1136
		err = -EIO;
1137
		goto out;
1138
	}
1139

1140 1141 1142
	for (id = 0; id < ARRAY_SIZE(requests); id++) {
		struct i915_request *rq;
		struct i915_vma *state;
1143
		void *vaddr;
1144

1145 1146
		rq = requests[id];
		if (!rq)
1147 1148
			continue;

1149 1150 1151 1152 1153 1154
		/* 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;
1155

1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
		/*
		 * 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)
1166
			goto out;
1167

1168
		i915_gem_object_lock(state->obj);
1169
		err = i915_gem_object_set_to_cpu_domain(state->obj, false);
1170
		i915_gem_object_unlock(state->obj);
1171
		if (err)
1172
			goto out;
1173

1174
		i915_gem_object_set_cache_coherency(state->obj, I915_CACHE_LLC);
1175 1176

		/* Check we can acquire the image of the context state */
1177
		vaddr = i915_gem_object_pin_map(state->obj, I915_MAP_FORCE_WB);
1178 1179
		if (IS_ERR(vaddr)) {
			err = PTR_ERR(vaddr);
1180
			goto out;
1181 1182
		}

1183 1184
		rq->engine->default_state = i915_gem_object_get(state->obj);
		i915_gem_object_unpin_map(state->obj);
1185 1186
	}

1187
out:
1188 1189
	/*
	 * If we have to abandon now, we expect the engines to be idle
1190 1191
	 * and ready to be torn-down. The quickest way we can accomplish
	 * this is by declaring ourselves wedged.
1192
	 */
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
	if (err)
		intel_gt_set_wedged(&i915->gt);

	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;
1209 1210
}

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
static int intel_engines_verify_workarounds(struct drm_i915_private *i915)
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	int err = 0;

	if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
		return 0;

	for_each_engine(engine, i915, id) {
		if (intel_engine_verify_workarounds(engine, "load"))
			err = -EIO;
	}

	return err;
}

1228
int i915_gem_init(struct drm_i915_private *dev_priv)
1229 1230 1231
{
	int ret;

1232 1233
	/* 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))
1234 1235 1236
		mkwrite_device_info(dev_priv)->page_sizes =
			I915_GTT_PAGE_SIZE_4K;

1237
	intel_timelines_init(dev_priv);
1238

1239 1240 1241 1242
	ret = i915_gem_init_userptr(dev_priv);
	if (ret)
		return ret;

1243
	intel_uc_fetch_firmwares(&dev_priv->gt.uc);
1244
	intel_wopcm_init(&dev_priv->wopcm);
1245

1246 1247 1248 1249 1250 1251
	/* 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.
	 */
1252
	mutex_lock(&dev_priv->drm.struct_mutex);
1253
	intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
1254

1255
	ret = i915_init_ggtt(dev_priv);
1256 1257 1258 1259
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_unlock;
	}
1260

1261
	intel_gt_init(&dev_priv->gt);
1262

1263 1264 1265 1266 1267 1268
	ret = intel_engines_setup(dev_priv);
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_unlock;
	}

1269 1270 1271 1272 1273 1274
	ret = i915_gem_contexts_init(dev_priv);
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_scratch;
	}

1275
	ret = intel_engines_init(dev_priv);
1276 1277 1278 1279
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_context;
	}
1280

1281 1282
	intel_init_gt_powersave(dev_priv);

1283
	intel_uc_init(&dev_priv->gt.uc);
1284

1285
	ret = intel_gt_init_hw(&dev_priv->gt);
1286 1287 1288
	if (ret)
		goto err_uc_init;

1289 1290 1291 1292 1293
	/* 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;

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
	/*
	 * 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);

1305 1306
	ret = intel_engines_verify_workarounds(dev_priv);
	if (ret)
1307
		goto err_gt;
1308

1309
	ret = __intel_engines_record_defaults(dev_priv);
1310
	if (ret)
1311
		goto err_gt;
1312

1313 1314
	ret = i915_inject_load_error(dev_priv, -ENODEV);
	if (ret)
1315
		goto err_gt;
1316

1317 1318
	ret = i915_inject_load_error(dev_priv, -EIO);
	if (ret)
1319
		goto err_gt;
1320

1321
	intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
	mutex_unlock(&dev_priv->drm.struct_mutex);

	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.
	 */
1332
err_gt:
1333 1334
	mutex_unlock(&dev_priv->drm.struct_mutex);

1335
	intel_gt_set_wedged_on_init(&dev_priv->gt);
1336
	i915_gem_suspend(dev_priv);
1337 1338
	i915_gem_suspend_late(dev_priv);

1339 1340
	i915_gem_drain_workqueue(dev_priv);

1341
	mutex_lock(&dev_priv->drm.struct_mutex);
1342
err_init_hw:
1343
	intel_uc_fini_hw(&dev_priv->gt.uc);
1344
err_uc_init:
1345
	if (ret != -EIO) {
1346
		intel_uc_fini(&dev_priv->gt.uc);
1347
		intel_engines_cleanup(dev_priv);
1348 1349 1350 1351
	}
err_context:
	if (ret != -EIO)
		i915_gem_contexts_fini(dev_priv);
1352
err_scratch:
1353
	intel_gt_driver_release(&dev_priv->gt);
1354
err_unlock:
1355
	intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
1356 1357
	mutex_unlock(&dev_priv->drm.struct_mutex);

1358
	if (ret != -EIO) {
1359
		intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1360
		i915_gem_cleanup_userptr(dev_priv);
1361
		intel_timelines_fini(dev_priv);
1362
	}
1363

1364
	if (ret == -EIO) {
1365
		/*
1366 1367
		 * 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,
1368 1369
		 * for all other failure, such as an allocation failure, bail.
		 */
1370
		if (!intel_gt_is_wedged(&dev_priv->gt)) {
1371 1372
			i915_probe_error(dev_priv,
					 "Failed to initialize GPU, declaring it wedged!\n");
1373
			intel_gt_set_wedged(&dev_priv->gt);
1374
		}
1375 1376 1377 1378 1379 1380

		/* Minimal basic recovery for KMS */
		ret = i915_ggtt_enable_hw(dev_priv);
		i915_gem_restore_gtt_mappings(dev_priv);
		i915_gem_restore_fences(dev_priv);
		intel_init_clock_gating(dev_priv);
1381 1382
	}

1383
	i915_gem_drain_freed_objects(dev_priv);
1384
	return ret;
1385 1386
}

1387 1388 1389
void i915_gem_driver_register(struct drm_i915_private *i915)
{
	i915_gem_driver_register__shrinker(i915);
1390 1391

	intel_engines_driver_register(i915);
1392 1393 1394 1395 1396 1397 1398
}

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

1399
void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
1400
{
1401
	intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
1402

1403
	i915_gem_suspend_late(dev_priv);
1404
	intel_gt_driver_remove(&dev_priv->gt);
1405 1406 1407 1408 1409

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

	mutex_lock(&dev_priv->drm.struct_mutex);
1410 1411
	intel_uc_fini_hw(&dev_priv->gt.uc);
	intel_uc_fini(&dev_priv->gt.uc);
1412 1413 1414 1415 1416
	mutex_unlock(&dev_priv->drm.struct_mutex);

	i915_gem_drain_freed_objects(dev_priv);
}

1417
void i915_gem_driver_release(struct drm_i915_private *dev_priv)
1418 1419
{
	mutex_lock(&dev_priv->drm.struct_mutex);
1420
	intel_engines_cleanup(dev_priv);
1421
	i915_gem_contexts_fini(dev_priv);
1422
	intel_gt_driver_release(&dev_priv->gt);
1423 1424
	mutex_unlock(&dev_priv->drm.struct_mutex);

1425 1426
	intel_wa_list_free(&dev_priv->gt_wa_list);

1427
	intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1428
	i915_gem_cleanup_userptr(dev_priv);
1429
	intel_timelines_fini(dev_priv);
1430 1431 1432 1433 1434 1435

	i915_gem_drain_freed_objects(dev_priv);

	WARN_ON(!list_empty(&dev_priv->contexts.list));
}

1436 1437 1438 1439 1440
void i915_gem_init_mmio(struct drm_i915_private *i915)
{
	i915_gem_sanitize(i915);
}

1441 1442 1443 1444 1445 1446
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);

1447
	INIT_LIST_HEAD(&i915->mm.purge_list);
1448
	INIT_LIST_HEAD(&i915->mm.shrink_list);
1449

1450
	i915_gem_init__objects(i915);
1451 1452
}

1453
void i915_gem_init_early(struct drm_i915_private *dev_priv)
1454
{
1455
	int err;
1456

1457
	i915_gem_init__mm(dev_priv);
1458
	i915_gem_init__pm(dev_priv);
1459

1460
	spin_lock_init(&dev_priv->fb_tracking.lock);
1461

M
Matthew Auld 已提交
1462 1463 1464
	err = i915_gemfs_init(dev_priv);
	if (err)
		DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
1465
}
1466

1467
void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
1468
{
1469
	i915_gem_drain_freed_objects(dev_priv);
1470 1471
	GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
	GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
1472
	WARN_ON(dev_priv->mm.shrink_count);
1473

M
Matthew Auld 已提交
1474
	i915_gemfs_fini(dev_priv);
1475 1476
}

1477 1478
int i915_gem_freeze(struct drm_i915_private *dev_priv)
{
1479 1480 1481
	/* Discard all purgeable objects, let userspace recover those as
	 * required after resuming.
	 */
1482 1483 1484 1485 1486
	i915_gem_shrink_all(dev_priv);

	return 0;
}

1487
int i915_gem_freeze_late(struct drm_i915_private *i915)
1488 1489
{
	struct drm_i915_gem_object *obj;
1490
	intel_wakeref_t wakeref;
1491

1492 1493
	/*
	 * Called just before we write the hibernation image.
1494 1495 1496 1497 1498 1499 1500 1501
	 *
	 * 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.
1502 1503
	 *
	 * To try and reduce the hibernation image, we manually shrink
1504
	 * the objects as well, see i915_gem_freeze()
1505 1506
	 */

1507
	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1508 1509

	i915_gem_shrink(i915, -1UL, NULL, ~0);
1510
	i915_gem_drain_freed_objects(i915);
1511

1512 1513 1514 1515
	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);
1516
	}
1517

1518
	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1519 1520 1521 1522

	return 0;
}

1523
void i915_gem_release(struct drm_device *dev, struct drm_file *file)
1524
{
1525
	struct drm_i915_file_private *file_priv = file->driver_priv;
1526
	struct i915_request *request;
1527 1528 1529 1530 1531

	/* 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.
	 */
1532
	spin_lock(&file_priv->mm.lock);
1533
	list_for_each_entry(request, &file_priv->mm.request_list, client_link)
1534
		request->file_priv = NULL;
1535
	spin_unlock(&file_priv->mm.lock);
1536 1537
}

1538
int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
1539 1540
{
	struct drm_i915_file_private *file_priv;
1541
	int ret;
1542

1543
	DRM_DEBUG("\n");
1544 1545 1546 1547 1548 1549

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

	file->driver_priv = file_priv;
1550
	file_priv->dev_priv = i915;
1551
	file_priv->file = file;
1552 1553 1554 1555

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

1556
	file_priv->bsd_engine = -1;
1557
	file_priv->hang_timestamp = jiffies;
1558

1559
	ret = i915_gem_context_open(i915, file);
1560 1561
	if (ret)
		kfree(file_priv);
1562

1563
	return ret;
1564 1565
}

1566
#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1567
#include "selftests/mock_gem_device.c"
1568
#include "selftests/i915_gem.c"
1569
#endif