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

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

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

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

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

61
#include "intel_pm.h"
62

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

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

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

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

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

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

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

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

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

113 114 115
	return 0;
}

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

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

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

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

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

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

	return ret;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

252
	i915_gem_flush_free_objects(dev_priv);
253

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

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

	vaddr = kmap(page);

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

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

272
	kunmap(page);
273

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return unwritten;
}

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

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

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

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

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

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

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

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

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

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

660
	vaddr = kmap(page);
661

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

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

669 670 671
	kunmap(page);

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

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

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

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

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

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

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

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

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

724
	return ret;
725 726 727 728
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return 0;
839 840
}

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

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

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

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

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

		if (!reg->vma)
			continue;

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

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

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

900 901
	if (flags & PIN_MAPPABLE &&
	    (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
902 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
		/* 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);
	}

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

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

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

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

951 952 953 954 955 956 957 958
	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 已提交
959 960 961
	ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
	if (ret)
		return ERR_PTR(ret);
962

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1044 1045
	GEM_TRACE("\n");

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

	/*
	 * 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.
	 */
1055 1056
	if (intel_gt_is_wedged(&i915->gt))
		intel_gt_unset_wedged(&i915->gt);
1057

1058 1059 1060 1061 1062 1063
	/*
	 * 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
1064
	 * of the reset, so this could be applied to even earlier gen.
1065
	 */
1066
	intel_gt_sanitize(&i915->gt, false);
1067

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

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

	/*
	 * 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) {
1089
		struct intel_context *ce;
1090
		struct i915_request *rq;
1091

1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
		/* 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;
		}

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

1109 1110 1111 1112 1113 1114 1115
		err = intel_engine_emit_ctx_wa(rq);
		if (err)
			goto err_rq;

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

1117
err_rq:
1118
		requests[id] = i915_request_get(rq);
1119
		i915_request_add(rq);
1120
		if (err)
1121
			goto out;
1122 1123
	}

1124
	/* Flush the default context image to memory, and enable powersaving. */
1125
	if (!i915_gem_load_power_context(i915)) {
1126
		err = -EIO;
1127
		goto out;
1128
	}
1129

1130 1131 1132
	for (id = 0; id < ARRAY_SIZE(requests); id++) {
		struct i915_request *rq;
		struct i915_vma *state;
1133
		void *vaddr;
1134

1135 1136
		rq = requests[id];
		if (!rq)
1137 1138
			continue;

1139 1140 1141 1142 1143 1144
		/* 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;
1145

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
		/*
		 * 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)
1156
			goto out;
1157

1158
		i915_gem_object_lock(state->obj);
1159
		err = i915_gem_object_set_to_cpu_domain(state->obj, false);
1160
		i915_gem_object_unlock(state->obj);
1161
		if (err)
1162
			goto out;
1163

1164
		i915_gem_object_set_cache_coherency(state->obj, I915_CACHE_LLC);
1165 1166

		/* Check we can acquire the image of the context state */
1167
		vaddr = i915_gem_object_pin_map(state->obj, I915_MAP_FORCE_WB);
1168 1169
		if (IS_ERR(vaddr)) {
			err = PTR_ERR(vaddr);
1170
			goto out;
1171 1172
		}

1173 1174
		rq->engine->default_state = i915_gem_object_get(state->obj);
		i915_gem_object_unpin_map(state->obj);
1175 1176
	}

1177
out:
1178 1179
	/*
	 * If we have to abandon now, we expect the engines to be idle
1180 1181
	 * and ready to be torn-down. The quickest way we can accomplish
	 * this is by declaring ourselves wedged.
1182
	 */
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
	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;
1199 1200
}

1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
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;
}

1218
int i915_gem_init(struct drm_i915_private *dev_priv)
1219 1220 1221
{
	int ret;

1222 1223
	/* 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))
1224 1225 1226
		mkwrite_device_info(dev_priv)->page_sizes =
			I915_GTT_PAGE_SIZE_4K;

1227
	intel_timelines_init(dev_priv);
1228

1229 1230 1231 1232
	ret = i915_gem_init_userptr(dev_priv);
	if (ret)
		return ret;

1233
	intel_uc_fetch_firmwares(&dev_priv->gt.uc);
1234
	intel_wopcm_init(&dev_priv->wopcm);
1235

1236 1237 1238 1239 1240 1241
	/* 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.
	 */
1242
	intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
1243

1244
	ret = i915_init_ggtt(dev_priv);
1245 1246 1247 1248
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_unlock;
	}
1249

1250
	intel_gt_init(&dev_priv->gt);
1251

1252
	ret = intel_engines_setup(&dev_priv->gt);
1253 1254 1255 1256 1257
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_unlock;
	}

1258
	ret = i915_gem_init_contexts(dev_priv);
1259 1260 1261 1262 1263
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_scratch;
	}

1264
	ret = intel_engines_init(&dev_priv->gt);
1265 1266 1267 1268
	if (ret) {
		GEM_BUG_ON(ret == -EIO);
		goto err_context;
	}
1269

1270 1271
	intel_init_gt_powersave(dev_priv);

1272
	intel_uc_init(&dev_priv->gt.uc);
1273

1274
	ret = intel_gt_init_hw(&dev_priv->gt);
1275 1276 1277
	if (ret)
		goto err_uc_init;

1278 1279 1280 1281 1282
	/* 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;

1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
	/*
	 * 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);

1294 1295
	ret = intel_engines_verify_workarounds(dev_priv);
	if (ret)
1296
		goto err_gt;
1297

1298
	ret = __intel_engines_record_defaults(dev_priv);
1299
	if (ret)
1300
		goto err_gt;
1301

1302 1303
	ret = i915_inject_load_error(dev_priv, -ENODEV);
	if (ret)
1304
		goto err_gt;
1305

1306 1307
	ret = i915_inject_load_error(dev_priv, -EIO);
	if (ret)
1308
		goto err_gt;
1309

1310
	intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
1311 1312 1313 1314 1315 1316 1317 1318 1319

	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.
	 */
1320
err_gt:
1321
	intel_gt_set_wedged_on_init(&dev_priv->gt);
1322
	i915_gem_suspend(dev_priv);
1323 1324
	i915_gem_suspend_late(dev_priv);

1325
	i915_gem_drain_workqueue(dev_priv);
1326
err_init_hw:
1327
	intel_uc_fini_hw(&dev_priv->gt.uc);
1328
err_uc_init:
1329
	if (ret != -EIO) {
1330
		intel_uc_fini(&dev_priv->gt.uc);
1331
		intel_engines_cleanup(&dev_priv->gt);
1332 1333 1334
	}
err_context:
	if (ret != -EIO)
1335
		i915_gem_driver_release__contexts(dev_priv);
1336
err_scratch:
1337
	intel_gt_driver_release(&dev_priv->gt);
1338
err_unlock:
1339
	intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
1340

1341
	if (ret != -EIO) {
1342
		intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1343
		i915_gem_cleanup_userptr(dev_priv);
1344
		intel_timelines_fini(dev_priv);
1345
	}
1346

1347
	if (ret == -EIO) {
1348
		/*
1349 1350
		 * 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,
1351 1352
		 * for all other failure, such as an allocation failure, bail.
		 */
1353
		if (!intel_gt_is_wedged(&dev_priv->gt)) {
1354 1355
			i915_probe_error(dev_priv,
					 "Failed to initialize GPU, declaring it wedged!\n");
1356
			intel_gt_set_wedged(&dev_priv->gt);
1357
		}
1358 1359 1360 1361

		/* Minimal basic recovery for KMS */
		ret = i915_ggtt_enable_hw(dev_priv);
		i915_gem_restore_gtt_mappings(dev_priv);
1362
		i915_gem_restore_fences(&dev_priv->ggtt);
1363
		intel_init_clock_gating(dev_priv);
1364 1365
	}

1366
	i915_gem_drain_freed_objects(dev_priv);
1367
	return ret;
1368 1369
}

1370 1371 1372
void i915_gem_driver_register(struct drm_i915_private *i915)
{
	i915_gem_driver_register__shrinker(i915);
1373 1374

	intel_engines_driver_register(i915);
1375 1376 1377 1378 1379 1380 1381
}

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

1382
void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
1383
{
1384
	intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
1385

1386
	i915_gem_suspend_late(dev_priv);
1387
	intel_gt_driver_remove(&dev_priv->gt);
1388 1389 1390 1391

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

1392 1393
	intel_uc_fini_hw(&dev_priv->gt.uc);
	intel_uc_fini(&dev_priv->gt.uc);
1394 1395 1396 1397

	i915_gem_drain_freed_objects(dev_priv);
}

1398
void i915_gem_driver_release(struct drm_i915_private *dev_priv)
1399
{
1400
	intel_engines_cleanup(&dev_priv->gt);
1401
	i915_gem_driver_release__contexts(dev_priv);
1402
	intel_gt_driver_release(&dev_priv->gt);
1403

1404 1405
	intel_wa_list_free(&dev_priv->gt_wa_list);

1406
	intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1407
	i915_gem_cleanup_userptr(dev_priv);
1408
	intel_timelines_fini(dev_priv);
1409 1410 1411

	i915_gem_drain_freed_objects(dev_priv);

1412
	WARN_ON(!list_empty(&dev_priv->gem.contexts.list));
1413 1414
}

1415 1416 1417 1418 1419
void i915_gem_init_mmio(struct drm_i915_private *i915)
{
	i915_gem_sanitize(i915);
}

1420 1421 1422 1423 1424 1425
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);

1426
	INIT_LIST_HEAD(&i915->mm.purge_list);
1427
	INIT_LIST_HEAD(&i915->mm.shrink_list);
1428

1429
	i915_gem_init__objects(i915);
1430 1431
}

1432
void i915_gem_init_early(struct drm_i915_private *dev_priv)
1433
{
1434
	i915_gem_init__mm(dev_priv);
1435

1436
	spin_lock_init(&dev_priv->fb_tracking.lock);
1437
}
1438

1439
void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
1440
{
1441
	i915_gem_drain_freed_objects(dev_priv);
1442 1443
	GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
	GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
1444
	WARN_ON(dev_priv->mm.shrink_count);
1445 1446
}

1447 1448
int i915_gem_freeze(struct drm_i915_private *dev_priv)
{
1449 1450 1451
	/* Discard all purgeable objects, let userspace recover those as
	 * required after resuming.
	 */
1452 1453 1454 1455 1456
	i915_gem_shrink_all(dev_priv);

	return 0;
}

1457
int i915_gem_freeze_late(struct drm_i915_private *i915)
1458 1459
{
	struct drm_i915_gem_object *obj;
1460
	intel_wakeref_t wakeref;
1461

1462 1463
	/*
	 * Called just before we write the hibernation image.
1464 1465 1466 1467 1468 1469 1470 1471
	 *
	 * 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.
1472 1473
	 *
	 * To try and reduce the hibernation image, we manually shrink
1474
	 * the objects as well, see i915_gem_freeze()
1475 1476
	 */

1477
	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1478 1479

	i915_gem_shrink(i915, -1UL, NULL, ~0);
1480
	i915_gem_drain_freed_objects(i915);
1481

1482 1483 1484 1485
	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);
1486
	}
1487

1488
	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1489 1490 1491 1492

	return 0;
}

1493
void i915_gem_release(struct drm_device *dev, struct drm_file *file)
1494
{
1495
	struct drm_i915_file_private *file_priv = file->driver_priv;
1496
	struct i915_request *request;
1497 1498 1499 1500 1501

	/* 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.
	 */
1502
	spin_lock(&file_priv->mm.lock);
1503
	list_for_each_entry(request, &file_priv->mm.request_list, client_link)
1504
		request->file_priv = NULL;
1505
	spin_unlock(&file_priv->mm.lock);
1506 1507
}

1508
int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
1509 1510
{
	struct drm_i915_file_private *file_priv;
1511
	int ret;
1512

1513
	DRM_DEBUG("\n");
1514 1515 1516 1517 1518 1519

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

	file->driver_priv = file_priv;
1520
	file_priv->dev_priv = i915;
1521
	file_priv->file = file;
1522 1523 1524 1525

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

1526
	file_priv->bsd_engine = -1;
1527
	file_priv->hang_timestamp = jiffies;
1528

1529
	ret = i915_gem_context_open(i915, file);
1530 1531
	if (ret)
		kfree(file_priv);
1532

1533
	return ret;
1534 1535
}

1536
#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1537
#include "selftests/mock_gem_device.c"
1538
#include "selftests/i915_gem.c"
1539
#endif