i915_gem.c 100.4 KB
Newer Older
1 2 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 28 29 30 31
/*
 * Copyright © 2008 Intel Corporation
 *
 * 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>
 *
 */

#include "drmP.h"
#include "drm.h"
#include "i915_drm.h"
#include "i915_drv.h"
C
Chris Wilson 已提交
32
#include "i915_trace.h"
33
#include "intel_drv.h"
34
#include <linux/slab.h>
35
#include <linux/swap.h>
J
Jesse Barnes 已提交
36
#include <linux/pci.h>
37

38
static void i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
39 40 41
static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
static int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
42
					     bool write);
43
static int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
44 45
						     uint64_t offset,
						     uint64_t size);
46 47
static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
static int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
48
				       unsigned alignment,
49
				       bool map_and_fenceable);
50 51 52
static void i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj);
static int i915_gem_phys_pwrite(struct drm_device *dev,
				struct drm_i915_gem_object *obj,
53
				struct drm_i915_gem_pwrite *args,
54 55
				struct drm_file *file);
static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
56

57 58 59 60
static int i915_gem_inactive_shrink(struct shrinker *shrinker,
				    int nr_to_scan,
				    gfp_t gfp_mask);

61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
/* some bookkeeping */
static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
				  size_t size)
{
	dev_priv->mm.object_count++;
	dev_priv->mm.object_memory += size;
}

static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
				     size_t size)
{
	dev_priv->mm.object_count--;
	dev_priv->mm.object_memory -= size;
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
int
i915_gem_check_is_wedged(struct drm_device *dev)
{
	struct drm_i915_private *dev_priv = dev->dev_private;
	struct completion *x = &dev_priv->error_completion;
	unsigned long flags;
	int ret;

	if (!atomic_read(&dev_priv->mm.wedged))
		return 0;

	ret = wait_for_completion_interruptible(x);
	if (ret)
		return ret;

	/* Success, we reset the GPU! */
	if (!atomic_read(&dev_priv->mm.wedged))
		return 0;

	/* GPU is hung, bump the completion count to account for
	 * the token we just consumed so that we never hit zero and
	 * end up waiting upon a subsequent completion event that
	 * will never happen.
	 */
	spin_lock_irqsave(&x->wait.lock, flags);
	x->done++;
	spin_unlock_irqrestore(&x->wait.lock, flags);
	return -EIO;
}

107
int i915_mutex_lock_interruptible(struct drm_device *dev)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
{
	struct drm_i915_private *dev_priv = dev->dev_private;
	int ret;

	ret = i915_gem_check_is_wedged(dev);
	if (ret)
		return ret;

	ret = mutex_lock_interruptible(&dev->struct_mutex);
	if (ret)
		return ret;

	if (atomic_read(&dev_priv->mm.wedged)) {
		mutex_unlock(&dev->struct_mutex);
		return -EAGAIN;
	}

125
	WARN_ON(i915_verify_lists(dev));
126 127
	return 0;
}
128

129
static inline bool
130
i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
131
{
132
	return obj->gtt_space && !obj->active && obj->pin_count == 0;
133 134
}

135 136 137 138
void i915_gem_do_init(struct drm_device *dev,
		      unsigned long start,
		      unsigned long mappable_end,
		      unsigned long end)
139 140 141
{
	drm_i915_private_t *dev_priv = dev->dev_private;

J
Jesse Barnes 已提交
142 143
	drm_mm_init(&dev_priv->mm.gtt_space, start,
		    end - start);
144

145
	dev_priv->mm.gtt_total = end - start;
146
	dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
D
Daniel Vetter 已提交
147
	dev_priv->mm.gtt_mappable_end = mappable_end;
J
Jesse Barnes 已提交
148
}
149

J
Jesse Barnes 已提交
150 151
int
i915_gem_init_ioctl(struct drm_device *dev, void *data,
152
		    struct drm_file *file)
J
Jesse Barnes 已提交
153 154
{
	struct drm_i915_gem_init *args = data;
155 156 157 158

	if (args->gtt_start >= args->gtt_end ||
	    (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
		return -EINVAL;
J
Jesse Barnes 已提交
159 160

	mutex_lock(&dev->struct_mutex);
161
	i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
162 163
	mutex_unlock(&dev->struct_mutex);

164
	return 0;
165 166
}

167 168
int
i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
169
			    struct drm_file *file)
170
{
171
	struct drm_i915_private *dev_priv = dev->dev_private;
172
	struct drm_i915_gem_get_aperture *args = data;
173 174
	struct drm_i915_gem_object *obj;
	size_t pinned;
175 176 177 178

	if (!(dev->driver->driver_features & DRIVER_GEM))
		return -ENODEV;

179
	pinned = 0;
180
	mutex_lock(&dev->struct_mutex);
181 182
	list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
		pinned += obj->gtt_space->size;
183
	mutex_unlock(&dev->struct_mutex);
184

185 186 187
	args->aper_size = dev_priv->mm.gtt_total;
	args->aper_available_size = args->aper_size -pinned;

188 189 190
	return 0;
}

191 192 193 194 195
/**
 * Creates a new mm object and returns a handle to it.
 */
int
i915_gem_create_ioctl(struct drm_device *dev, void *data,
196
		      struct drm_file *file)
197 198
{
	struct drm_i915_gem_create *args = data;
199
	struct drm_i915_gem_object *obj;
200 201
	int ret;
	u32 handle;
202 203 204 205

	args->size = roundup(args->size, PAGE_SIZE);

	/* Allocate the new object */
206
	obj = i915_gem_alloc_object(dev, args->size);
207 208 209
	if (obj == NULL)
		return -ENOMEM;

210
	ret = drm_gem_handle_create(file, &obj->base, &handle);
211
	if (ret) {
212 213
		drm_gem_object_release(&obj->base);
		i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
214
		kfree(obj);
215
		return ret;
216
	}
217

218
	/* drop reference from allocate - handle holds it now */
219
	drm_gem_object_unreference(&obj->base);
220 221
	trace_i915_gem_object_create(obj);

222
	args->handle = handle;
223 224 225
	return 0;
}

226
static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
227
{
228
	drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
229 230

	return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
231
		obj->tiling_mode != I915_TILING_NONE;
232 233
}

234
static inline void
235 236 237 238 239 240 241 242
slow_shmem_copy(struct page *dst_page,
		int dst_offset,
		struct page *src_page,
		int src_offset,
		int length)
{
	char *dst_vaddr, *src_vaddr;

243 244
	dst_vaddr = kmap(dst_page);
	src_vaddr = kmap(src_page);
245 246 247

	memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);

248 249
	kunmap(src_page);
	kunmap(dst_page);
250 251
}

252
static inline void
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
slow_shmem_bit17_copy(struct page *gpu_page,
		      int gpu_offset,
		      struct page *cpu_page,
		      int cpu_offset,
		      int length,
		      int is_read)
{
	char *gpu_vaddr, *cpu_vaddr;

	/* Use the unswizzled path if this page isn't affected. */
	if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
		if (is_read)
			return slow_shmem_copy(cpu_page, cpu_offset,
					       gpu_page, gpu_offset, length);
		else
			return slow_shmem_copy(gpu_page, gpu_offset,
					       cpu_page, cpu_offset, length);
	}

272 273
	gpu_vaddr = kmap(gpu_page);
	cpu_vaddr = kmap(cpu_page);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

	/* Copy the data, XORing A6 with A17 (1). The user already knows he's
	 * XORing with the other bits (A9 for Y, A9 and A10 for X)
	 */
	while (length > 0) {
		int cacheline_end = ALIGN(gpu_offset + 1, 64);
		int this_length = min(cacheline_end - gpu_offset, length);
		int swizzled_gpu_offset = gpu_offset ^ 64;

		if (is_read) {
			memcpy(cpu_vaddr + cpu_offset,
			       gpu_vaddr + swizzled_gpu_offset,
			       this_length);
		} else {
			memcpy(gpu_vaddr + swizzled_gpu_offset,
			       cpu_vaddr + cpu_offset,
			       this_length);
		}
		cpu_offset += this_length;
		gpu_offset += this_length;
		length -= this_length;
	}

297 298
	kunmap(cpu_page);
	kunmap(gpu_page);
299 300
}

301 302 303 304 305 306
/**
 * This is the fast shmem pread path, which attempts to copy_from_user directly
 * from the backing pages of the object to the user's address space.  On a
 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
 */
static int
307 308
i915_gem_shmem_pread_fast(struct drm_device *dev,
			  struct drm_i915_gem_object *obj,
309
			  struct drm_i915_gem_pread *args,
310
			  struct drm_file *file)
311
{
312
	struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
313
	ssize_t remain;
314
	loff_t offset;
315 316 317 318 319 320 321 322 323
	char __user *user_data;
	int page_offset, page_length;

	user_data = (char __user *) (uintptr_t) args->data_ptr;
	remain = args->size;

	offset = args->offset;

	while (remain > 0) {
324 325 326 327
		struct page *page;
		char *vaddr;
		int ret;

328 329 330 331 332 333 334 335 336 337
		/* Operation in this page
		 *
		 * page_offset = offset within page
		 * page_length = bytes to copy for this page
		 */
		page_offset = offset & (PAGE_SIZE-1);
		page_length = remain;
		if ((page_offset + remain) > PAGE_SIZE)
			page_length = PAGE_SIZE - page_offset;

338 339 340 341 342 343 344 345 346 347 348 349 350 351
		page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
					   GFP_HIGHUSER | __GFP_RECLAIMABLE);
		if (IS_ERR(page))
			return PTR_ERR(page);

		vaddr = kmap_atomic(page);
		ret = __copy_to_user_inatomic(user_data,
					      vaddr + page_offset,
					      page_length);
		kunmap_atomic(vaddr);

		mark_page_accessed(page);
		page_cache_release(page);
		if (ret)
352
			return -EFAULT;
353 354 355 356 357 358

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

359
	return 0;
360 361 362 363 364 365 366 367 368
}

/**
 * This is the fallback shmem pread path, which allocates temporary storage
 * in kernel space to copy_to_user into outside of the struct_mutex, so we
 * can copy out of the object's backing pages while holding the struct mutex
 * and not take page faults.
 */
static int
369 370
i915_gem_shmem_pread_slow(struct drm_device *dev,
			  struct drm_i915_gem_object *obj,
371
			  struct drm_i915_gem_pread *args,
372
			  struct drm_file *file)
373
{
374
	struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
375 376 377 378 379
	struct mm_struct *mm = current->mm;
	struct page **user_pages;
	ssize_t remain;
	loff_t offset, pinned_pages, i;
	loff_t first_data_page, last_data_page, num_pages;
380 381
	int shmem_page_offset;
	int data_page_index, data_page_offset;
382 383 384
	int page_length;
	int ret;
	uint64_t data_ptr = args->data_ptr;
385
	int do_bit17_swizzling;
386 387 388 389 390 391 392 393 394 395 396

	remain = args->size;

	/* Pin the user pages containing the data.  We can't fault while
	 * holding the struct mutex, yet we want to hold it while
	 * dereferencing the user data.
	 */
	first_data_page = data_ptr / PAGE_SIZE;
	last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
	num_pages = last_data_page - first_data_page + 1;

397
	user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
398 399 400
	if (user_pages == NULL)
		return -ENOMEM;

401
	mutex_unlock(&dev->struct_mutex);
402 403
	down_read(&mm->mmap_sem);
	pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
404
				      num_pages, 1, 0, user_pages, NULL);
405
	up_read(&mm->mmap_sem);
406
	mutex_lock(&dev->struct_mutex);
407 408
	if (pinned_pages < num_pages) {
		ret = -EFAULT;
409
		goto out;
410 411
	}

412 413 414
	ret = i915_gem_object_set_cpu_read_domain_range(obj,
							args->offset,
							args->size);
415
	if (ret)
416
		goto out;
417

418
	do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
419 420 421 422

	offset = args->offset;

	while (remain > 0) {
423 424
		struct page *page;

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
		/* Operation in this page
		 *
		 * shmem_page_offset = offset within page in shmem file
		 * data_page_index = page number in get_user_pages return
		 * data_page_offset = offset with data_page_index page.
		 * page_length = bytes to copy for this page
		 */
		shmem_page_offset = offset & ~PAGE_MASK;
		data_page_index = data_ptr / PAGE_SIZE - first_data_page;
		data_page_offset = data_ptr & ~PAGE_MASK;

		page_length = remain;
		if ((shmem_page_offset + page_length) > PAGE_SIZE)
			page_length = PAGE_SIZE - shmem_page_offset;
		if ((data_page_offset + page_length) > PAGE_SIZE)
			page_length = PAGE_SIZE - data_page_offset;

442 443 444 445 446
		page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
					   GFP_HIGHUSER | __GFP_RECLAIMABLE);
		if (IS_ERR(page))
			return PTR_ERR(page);

447
		if (do_bit17_swizzling) {
448
			slow_shmem_bit17_copy(page,
449
					      shmem_page_offset,
450 451 452 453 454 455 456
					      user_pages[data_page_index],
					      data_page_offset,
					      page_length,
					      1);
		} else {
			slow_shmem_copy(user_pages[data_page_index],
					data_page_offset,
457
					page,
458 459
					shmem_page_offset,
					page_length);
460
		}
461

462 463 464
		mark_page_accessed(page);
		page_cache_release(page);

465 466 467 468 469
		remain -= page_length;
		data_ptr += page_length;
		offset += page_length;
	}

470
out:
471 472
	for (i = 0; i < pinned_pages; i++) {
		SetPageDirty(user_pages[i]);
473
		mark_page_accessed(user_pages[i]);
474 475
		page_cache_release(user_pages[i]);
	}
476
	drm_free_large(user_pages);
477 478 479 480

	return ret;
}

481 482 483 484 485 486 487
/**
 * Reads data from the object referenced by handle.
 *
 * On error, the contents of *data are undefined.
 */
int
i915_gem_pread_ioctl(struct drm_device *dev, void *data,
488
		     struct drm_file *file)
489 490
{
	struct drm_i915_gem_pread *args = data;
491
	struct drm_i915_gem_object *obj;
492
	int ret = 0;
493

494 495 496 497 498 499 500 501 502 503 504 505 506
	if (args->size == 0)
		return 0;

	if (!access_ok(VERIFY_WRITE,
		       (char __user *)(uintptr_t)args->data_ptr,
		       args->size))
		return -EFAULT;

	ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
				       args->size);
	if (ret)
		return -EFAULT;

507
	ret = i915_mutex_lock_interruptible(dev);
508
	if (ret)
509
		return ret;
510

511
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
512 513 514
	if (obj == NULL) {
		ret = -ENOENT;
		goto unlock;
515
	}
516

517
	/* Bounds check source.  */
518 519
	if (args->offset > obj->base.size ||
	    args->size > obj->base.size - args->offset) {
C
Chris Wilson 已提交
520
		ret = -EINVAL;
521
		goto out;
C
Chris Wilson 已提交
522 523
	}

524 525 526 527
	ret = i915_gem_object_set_cpu_read_domain_range(obj,
							args->offset,
							args->size);
	if (ret)
528
		goto out;
529 530 531

	ret = -EFAULT;
	if (!i915_gem_object_needs_bit17_swizzle(obj))
532
		ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
533
	if (ret == -EFAULT)
534
		ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
535

536
out:
537
	drm_gem_object_unreference(&obj->base);
538
unlock:
539
	mutex_unlock(&dev->struct_mutex);
540
	return ret;
541 542
}

543 544
/* This is the fast write path which cannot handle
 * page faults in the source data
545
 */
546 547 548 549 550 551

static inline int
fast_user_write(struct io_mapping *mapping,
		loff_t page_base, int page_offset,
		char __user *user_data,
		int length)
552 553
{
	char *vaddr_atomic;
554
	unsigned long unwritten;
555

P
Peter Zijlstra 已提交
556
	vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
557 558
	unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
						      user_data, length);
P
Peter Zijlstra 已提交
559
	io_mapping_unmap_atomic(vaddr_atomic);
560
	return unwritten;
561 562 563 564 565 566
}

/* Here's the write path which can sleep for
 * page faults
 */

567
static inline void
568 569 570 571
slow_kernel_write(struct io_mapping *mapping,
		  loff_t gtt_base, int gtt_offset,
		  struct page *user_page, int user_offset,
		  int length)
572
{
573 574
	char __iomem *dst_vaddr;
	char *src_vaddr;
575

576 577 578 579 580 581 582 583 584
	dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
	src_vaddr = kmap(user_page);

	memcpy_toio(dst_vaddr + gtt_offset,
		    src_vaddr + user_offset,
		    length);

	kunmap(user_page);
	io_mapping_unmap(dst_vaddr);
585 586
}

587 588 589 590
/**
 * This is the fast pwrite path, where we copy the data directly from the
 * user into the GTT, uncached.
 */
591
static int
592 593
i915_gem_gtt_pwrite_fast(struct drm_device *dev,
			 struct drm_i915_gem_object *obj,
594
			 struct drm_i915_gem_pwrite *args,
595
			 struct drm_file *file)
596
{
597
	drm_i915_private_t *dev_priv = dev->dev_private;
598
	ssize_t remain;
599
	loff_t offset, page_base;
600
	char __user *user_data;
601
	int page_offset, page_length;
602 603 604 605

	user_data = (char __user *) (uintptr_t) args->data_ptr;
	remain = args->size;

606
	offset = obj->gtt_offset + args->offset;
607 608 609 610

	while (remain > 0) {
		/* Operation in this page
		 *
611 612 613
		 * page_base = page offset within aperture
		 * page_offset = offset within page
		 * page_length = bytes to copy for this page
614
		 */
615 616 617 618 619 620 621
		page_base = (offset & ~(PAGE_SIZE-1));
		page_offset = offset & (PAGE_SIZE-1);
		page_length = remain;
		if ((page_offset + remain) > PAGE_SIZE)
			page_length = PAGE_SIZE - page_offset;

		/* If we get a fault while copying data, then (presumably) our
622 623
		 * source page isn't available.  Return the error and we'll
		 * retry in the slow path.
624
		 */
625 626 627 628
		if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
				    page_offset, user_data, page_length))

			return -EFAULT;
629

630 631 632
		remain -= page_length;
		user_data += page_length;
		offset += page_length;
633 634
	}

635
	return 0;
636 637
}

638 639 640 641 642 643 644
/**
 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
 * the memory and maps it using kmap_atomic for copying.
 *
 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
 */
645
static int
646 647
i915_gem_gtt_pwrite_slow(struct drm_device *dev,
			 struct drm_i915_gem_object *obj,
648
			 struct drm_i915_gem_pwrite *args,
649
			 struct drm_file *file)
650
{
651 652 653 654 655 656 657 658
	drm_i915_private_t *dev_priv = dev->dev_private;
	ssize_t remain;
	loff_t gtt_page_base, offset;
	loff_t first_data_page, last_data_page, num_pages;
	loff_t pinned_pages, i;
	struct page **user_pages;
	struct mm_struct *mm = current->mm;
	int gtt_page_offset, data_page_offset, data_page_index, page_length;
659
	int ret;
660 661 662 663 664 665 666 667 668 669 670 671
	uint64_t data_ptr = args->data_ptr;

	remain = args->size;

	/* Pin the user pages containing the data.  We can't fault while
	 * holding the struct mutex, and all of the pwrite implementations
	 * want to hold it while dereferencing the user data.
	 */
	first_data_page = data_ptr / PAGE_SIZE;
	last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
	num_pages = last_data_page - first_data_page + 1;

672
	user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
673 674 675
	if (user_pages == NULL)
		return -ENOMEM;

676
	mutex_unlock(&dev->struct_mutex);
677 678 679 680
	down_read(&mm->mmap_sem);
	pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
				      num_pages, 0, 0, user_pages, NULL);
	up_read(&mm->mmap_sem);
681
	mutex_lock(&dev->struct_mutex);
682 683 684 685
	if (pinned_pages < num_pages) {
		ret = -EFAULT;
		goto out_unpin_pages;
	}
686

687 688
	ret = i915_gem_object_set_to_gtt_domain(obj, 1);
	if (ret)
689
		goto out_unpin_pages;
690

691
	offset = obj->gtt_offset + args->offset;
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

	while (remain > 0) {
		/* Operation in this page
		 *
		 * gtt_page_base = page offset within aperture
		 * gtt_page_offset = offset within page in aperture
		 * data_page_index = page number in get_user_pages return
		 * data_page_offset = offset with data_page_index page.
		 * page_length = bytes to copy for this page
		 */
		gtt_page_base = offset & PAGE_MASK;
		gtt_page_offset = offset & ~PAGE_MASK;
		data_page_index = data_ptr / PAGE_SIZE - first_data_page;
		data_page_offset = data_ptr & ~PAGE_MASK;

		page_length = remain;
		if ((gtt_page_offset + page_length) > PAGE_SIZE)
			page_length = PAGE_SIZE - gtt_page_offset;
		if ((data_page_offset + page_length) > PAGE_SIZE)
			page_length = PAGE_SIZE - data_page_offset;

713 714 715 716 717
		slow_kernel_write(dev_priv->mm.gtt_mapping,
				  gtt_page_base, gtt_page_offset,
				  user_pages[data_page_index],
				  data_page_offset,
				  page_length);
718 719 720 721 722 723 724 725 726

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

out_unpin_pages:
	for (i = 0; i < pinned_pages; i++)
		page_cache_release(user_pages[i]);
727
	drm_free_large(user_pages);
728 729 730 731

	return ret;
}

732 733 734 735
/**
 * This is the fast shmem pwrite path, which attempts to directly
 * copy_from_user into the kmapped pages backing the object.
 */
736
static int
737 738
i915_gem_shmem_pwrite_fast(struct drm_device *dev,
			   struct drm_i915_gem_object *obj,
739
			   struct drm_i915_gem_pwrite *args,
740
			   struct drm_file *file)
741
{
742
	struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
743
	ssize_t remain;
744
	loff_t offset;
745 746 747 748 749
	char __user *user_data;
	int page_offset, page_length;

	user_data = (char __user *) (uintptr_t) args->data_ptr;
	remain = args->size;
750

751
	offset = args->offset;
752
	obj->dirty = 1;
753 754

	while (remain > 0) {
755 756 757 758
		struct page *page;
		char *vaddr;
		int ret;

759 760 761 762 763 764 765 766 767 768
		/* Operation in this page
		 *
		 * page_offset = offset within page
		 * page_length = bytes to copy for this page
		 */
		page_offset = offset & (PAGE_SIZE-1);
		page_length = remain;
		if ((page_offset + remain) > PAGE_SIZE)
			page_length = PAGE_SIZE - page_offset;

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
		page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
					   GFP_HIGHUSER | __GFP_RECLAIMABLE);
		if (IS_ERR(page))
			return PTR_ERR(page);

		vaddr = kmap_atomic(page, KM_USER0);
		ret = __copy_from_user_inatomic(vaddr + page_offset,
						user_data,
						page_length);
		kunmap_atomic(vaddr, KM_USER0);

		set_page_dirty(page);
		mark_page_accessed(page);
		page_cache_release(page);

		/* If we get a fault while copying data, then (presumably) our
		 * source page isn't available.  Return the error and we'll
		 * retry in the slow path.
		 */
		if (ret)
789
			return -EFAULT;
790 791 792 793 794 795

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

796
	return 0;
797 798 799 800 801 802 803 804 805 806
}

/**
 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
 * the memory and maps it using kmap_atomic for copying.
 *
 * This avoids taking mmap_sem for faulting on the user's address while the
 * struct_mutex is held.
 */
static int
807 808
i915_gem_shmem_pwrite_slow(struct drm_device *dev,
			   struct drm_i915_gem_object *obj,
809
			   struct drm_i915_gem_pwrite *args,
810
			   struct drm_file *file)
811
{
812
	struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
813 814 815 816 817
	struct mm_struct *mm = current->mm;
	struct page **user_pages;
	ssize_t remain;
	loff_t offset, pinned_pages, i;
	loff_t first_data_page, last_data_page, num_pages;
818
	int shmem_page_offset;
819 820 821 822
	int data_page_index,  data_page_offset;
	int page_length;
	int ret;
	uint64_t data_ptr = args->data_ptr;
823
	int do_bit17_swizzling;
824 825 826 827 828 829 830 831 832 833 834

	remain = args->size;

	/* Pin the user pages containing the data.  We can't fault while
	 * holding the struct mutex, and all of the pwrite implementations
	 * want to hold it while dereferencing the user data.
	 */
	first_data_page = data_ptr / PAGE_SIZE;
	last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
	num_pages = last_data_page - first_data_page + 1;

835
	user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
836 837 838
	if (user_pages == NULL)
		return -ENOMEM;

839
	mutex_unlock(&dev->struct_mutex);
840 841 842 843
	down_read(&mm->mmap_sem);
	pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
				      num_pages, 0, 0, user_pages, NULL);
	up_read(&mm->mmap_sem);
844
	mutex_lock(&dev->struct_mutex);
845 846
	if (pinned_pages < num_pages) {
		ret = -EFAULT;
847
		goto out;
848 849
	}

850
	ret = i915_gem_object_set_to_cpu_domain(obj, 1);
851
	if (ret)
852
		goto out;
853

854
	do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
855

856
	offset = args->offset;
857
	obj->dirty = 1;
858

859
	while (remain > 0) {
860 861
		struct page *page;

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
		/* Operation in this page
		 *
		 * shmem_page_offset = offset within page in shmem file
		 * data_page_index = page number in get_user_pages return
		 * data_page_offset = offset with data_page_index page.
		 * page_length = bytes to copy for this page
		 */
		shmem_page_offset = offset & ~PAGE_MASK;
		data_page_index = data_ptr / PAGE_SIZE - first_data_page;
		data_page_offset = data_ptr & ~PAGE_MASK;

		page_length = remain;
		if ((shmem_page_offset + page_length) > PAGE_SIZE)
			page_length = PAGE_SIZE - shmem_page_offset;
		if ((data_page_offset + page_length) > PAGE_SIZE)
			page_length = PAGE_SIZE - data_page_offset;

879 880 881 882 883 884 885
		page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
					   GFP_HIGHUSER | __GFP_RECLAIMABLE);
		if (IS_ERR(page)) {
			ret = PTR_ERR(page);
			goto out;
		}

886
		if (do_bit17_swizzling) {
887
			slow_shmem_bit17_copy(page,
888 889 890
					      shmem_page_offset,
					      user_pages[data_page_index],
					      data_page_offset,
891 892 893
					      page_length,
					      0);
		} else {
894
			slow_shmem_copy(page,
895 896 897 898
					shmem_page_offset,
					user_pages[data_page_index],
					data_page_offset,
					page_length);
899
		}
900

901 902 903 904
		set_page_dirty(page);
		mark_page_accessed(page);
		page_cache_release(page);

905 906 907
		remain -= page_length;
		data_ptr += page_length;
		offset += page_length;
908 909
	}

910
out:
911 912
	for (i = 0; i < pinned_pages; i++)
		page_cache_release(user_pages[i]);
913
	drm_free_large(user_pages);
914

915
	return ret;
916 917 918 919 920 921 922 923 924
}

/**
 * Writes data to the object referenced by handle.
 *
 * 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,
925
		      struct drm_file *file)
926 927
{
	struct drm_i915_gem_pwrite *args = data;
928
	struct drm_i915_gem_object *obj;
929 930 931 932 933 934 935 936 937 938 939 940 941 942
	int ret;

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

	if (!access_ok(VERIFY_READ,
		       (char __user *)(uintptr_t)args->data_ptr,
		       args->size))
		return -EFAULT;

	ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
				      args->size);
	if (ret)
		return -EFAULT;
943

944
	ret = i915_mutex_lock_interruptible(dev);
945
	if (ret)
946
		return ret;
947

948
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
949 950 951
	if (obj == NULL) {
		ret = -ENOENT;
		goto unlock;
952
	}
953

954
	/* Bounds check destination. */
955 956
	if (args->offset > obj->base.size ||
	    args->size > obj->base.size - args->offset) {
C
Chris Wilson 已提交
957
		ret = -EINVAL;
958
		goto out;
C
Chris Wilson 已提交
959 960
	}

961 962 963 964 965 966
	/* 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.
	 */
967
	if (obj->phys_obj)
968
		ret = i915_gem_phys_pwrite(dev, obj, args, file);
969 970 971
	else if (obj->tiling_mode == I915_TILING_NONE &&
		 obj->gtt_space &&
		 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
972
		ret = i915_gem_object_pin(obj, 0, true);
973 974 975 976 977 978 979 980 981 982 983 984 985
		if (ret)
			goto out;

		ret = i915_gem_object_set_to_gtt_domain(obj, 1);
		if (ret)
			goto out_unpin;

		ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
		if (ret == -EFAULT)
			ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);

out_unpin:
		i915_gem_object_unpin(obj);
986
	} else {
987 988
		ret = i915_gem_object_set_to_cpu_domain(obj, 1);
		if (ret)
989
			goto out;
990

991 992 993 994 995 996
		ret = -EFAULT;
		if (!i915_gem_object_needs_bit17_swizzle(obj))
			ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
		if (ret == -EFAULT)
			ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
	}
997

998
out:
999
	drm_gem_object_unreference(&obj->base);
1000
unlock:
1001
	mutex_unlock(&dev->struct_mutex);
1002 1003 1004 1005
	return ret;
}

/**
1006 1007
 * Called when user space prepares to use an object with the CPU, either
 * through the mmap ioctl's mapping or a GTT mapping.
1008 1009 1010
 */
int
i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1011
			  struct drm_file *file)
1012
{
1013
	struct drm_i915_private *dev_priv = dev->dev_private;
1014
	struct drm_i915_gem_set_domain *args = data;
1015
	struct drm_i915_gem_object *obj;
1016 1017
	uint32_t read_domains = args->read_domains;
	uint32_t write_domain = args->write_domain;
1018 1019 1020 1021 1022
	int ret;

	if (!(dev->driver->driver_features & DRIVER_GEM))
		return -ENODEV;

1023
	/* Only handle setting domains to types used by the CPU. */
1024
	if (write_domain & I915_GEM_GPU_DOMAINS)
1025 1026
		return -EINVAL;

1027
	if (read_domains & I915_GEM_GPU_DOMAINS)
1028 1029 1030 1031 1032 1033 1034 1035
		return -EINVAL;

	/* Having something in the write domain implies it's in the read
	 * domain, and only that read domain.  Enforce that in the request.
	 */
	if (write_domain != 0 && read_domains != write_domain)
		return -EINVAL;

1036
	ret = i915_mutex_lock_interruptible(dev);
1037
	if (ret)
1038
		return ret;
1039

1040
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1041 1042 1043
	if (obj == NULL) {
		ret = -ENOENT;
		goto unlock;
1044
	}
1045

1046 1047
	intel_mark_busy(dev, obj);

1048 1049
	if (read_domains & I915_GEM_DOMAIN_GTT) {
		ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1050

1051 1052 1053
		/* Update the LRU on the fence for the CPU access that's
		 * about to occur.
		 */
1054
		if (obj->fence_reg != I915_FENCE_REG_NONE) {
1055
			struct drm_i915_fence_reg *reg =
1056
				&dev_priv->fence_regs[obj->fence_reg];
1057
			list_move_tail(&reg->lru_list,
1058 1059 1060
				       &dev_priv->mm.fence_list);
		}

1061 1062 1063 1064 1065 1066
		/* Silently promote "you're not bound, there was nothing to do"
		 * to success, since the client was just asking us to
		 * make sure everything was done.
		 */
		if (ret == -EINVAL)
			ret = 0;
1067
	} else {
1068
		ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1069 1070
	}

1071
	/* Maintain LRU order of "inactive" objects */
1072 1073
	if (ret == 0 && i915_gem_object_is_inactive(obj))
		list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1074

1075
	drm_gem_object_unreference(&obj->base);
1076
unlock:
1077 1078 1079 1080 1081 1082 1083 1084 1085
	mutex_unlock(&dev->struct_mutex);
	return ret;
}

/**
 * Called when user space has done writes to this buffer
 */
int
i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1086
			 struct drm_file *file)
1087 1088
{
	struct drm_i915_gem_sw_finish *args = data;
1089
	struct drm_i915_gem_object *obj;
1090 1091 1092 1093 1094
	int ret = 0;

	if (!(dev->driver->driver_features & DRIVER_GEM))
		return -ENODEV;

1095
	ret = i915_mutex_lock_interruptible(dev);
1096
	if (ret)
1097
		return ret;
1098

1099
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1100
	if (obj == NULL) {
1101 1102
		ret = -ENOENT;
		goto unlock;
1103 1104 1105
	}

	/* Pinned buffers may be scanout, so flush the cache */
1106
	if (obj->pin_count)
1107 1108
		i915_gem_object_flush_cpu_write_domain(obj);

1109
	drm_gem_object_unreference(&obj->base);
1110
unlock:
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
	mutex_unlock(&dev->struct_mutex);
	return ret;
}

/**
 * Maps the contents of an object, returning the address it is mapped
 * into.
 *
 * While the mapping holds a reference on the contents of the object, it doesn't
 * imply a ref on the object itself.
 */
int
i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1124
		    struct drm_file *file)
1125
{
1126
	struct drm_i915_private *dev_priv = dev->dev_private;
1127 1128 1129 1130 1131 1132 1133 1134
	struct drm_i915_gem_mmap *args = data;
	struct drm_gem_object *obj;
	loff_t offset;
	unsigned long addr;

	if (!(dev->driver->driver_features & DRIVER_GEM))
		return -ENODEV;

1135
	obj = drm_gem_object_lookup(dev, file, args->handle);
1136
	if (obj == NULL)
1137
		return -ENOENT;
1138

1139 1140 1141 1142 1143
	if (obj->size > dev_priv->mm.gtt_mappable_end) {
		drm_gem_object_unreference_unlocked(obj);
		return -E2BIG;
	}

1144 1145 1146 1147 1148 1149 1150
	offset = args->offset;

	down_write(&current->mm->mmap_sem);
	addr = do_mmap(obj->filp, 0, args->size,
		       PROT_READ | PROT_WRITE, MAP_SHARED,
		       args->offset);
	up_write(&current->mm->mmap_sem);
1151
	drm_gem_object_unreference_unlocked(obj);
1152 1153 1154 1155 1156 1157 1158 1159
	if (IS_ERR((void *)addr))
		return addr;

	args->addr_ptr = (uint64_t) addr;

	return 0;
}

1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
/**
 * i915_gem_fault - fault a page into the GTT
 * vma: VMA in question
 * vmf: fault info
 *
 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
 * from userspace.  The fault handler takes care of binding the object to
 * the GTT (if needed), allocating and programming a fence register (again,
 * only if needed based on whether the old reg is still valid or the object
 * is tiled) and inserting a new PTE into the faulting process.
 *
 * Note that the faulting process may involve evicting existing objects
 * from the GTT and/or fence registers to make room.  So performance may
 * suffer if the GTT working set is large or there are few fence registers
 * left.
 */
int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
{
1178 1179
	struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
	struct drm_device *dev = obj->base.dev;
1180
	drm_i915_private_t *dev_priv = dev->dev_private;
1181 1182 1183
	pgoff_t page_offset;
	unsigned long pfn;
	int ret = 0;
1184
	bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1185 1186 1187 1188 1189 1190 1191

	/* We don't use vmf->pgoff since that has the fake offset */
	page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
		PAGE_SHIFT;

	/* Now bind it into the GTT if needed */
	mutex_lock(&dev->struct_mutex);
1192

1193 1194 1195 1196
	if (!obj->map_and_fenceable) {
		ret = i915_gem_object_unbind(obj);
		if (ret)
			goto unlock;
1197
	}
1198
	if (!obj->gtt_space) {
1199
		ret = i915_gem_object_bind_to_gtt(obj, 0, true);
1200 1201
		if (ret)
			goto unlock;
1202 1203
	}

1204 1205 1206 1207
	ret = i915_gem_object_set_to_gtt_domain(obj, write);
	if (ret)
		goto unlock;

1208
	/* Need a new fence register? */
1209
	if (obj->tiling_mode != I915_TILING_NONE) {
1210
		ret = i915_gem_object_get_fence_reg(obj, true);
1211 1212
		if (ret)
			goto unlock;
1213
	}
1214

1215 1216
	if (i915_gem_object_is_inactive(obj))
		list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1217

1218 1219
	obj->fault_mappable = true;

1220
	pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
1221 1222 1223 1224
		page_offset;

	/* Finally, remap it using the new GTT offset */
	ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1225
unlock:
1226 1227 1228
	mutex_unlock(&dev->struct_mutex);

	switch (ret) {
1229 1230
	case -EAGAIN:
		set_need_resched();
1231 1232 1233
	case 0:
	case -ERESTARTSYS:
		return VM_FAULT_NOPAGE;
1234 1235 1236
	case -ENOMEM:
		return VM_FAULT_OOM;
	default:
1237
		return VM_FAULT_SIGBUS;
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
	}
}

/**
 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
 * @obj: obj in question
 *
 * GEM memory mapping works by handing back to userspace a fake mmap offset
 * it can use in a subsequent mmap(2) call.  The DRM core code then looks
 * up the object based on the offset and sets up the various memory mapping
 * structures.
 *
 * This routine allocates and attaches a fake offset for @obj.
 */
static int
1253
i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
1254
{
1255
	struct drm_device *dev = obj->base.dev;
1256 1257
	struct drm_gem_mm *mm = dev->mm_private;
	struct drm_map_list *list;
1258
	struct drm_local_map *map;
1259 1260 1261
	int ret = 0;

	/* Set the object up for mmap'ing */
1262
	list = &obj->base.map_list;
1263
	list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
1264 1265 1266 1267 1268
	if (!list->map)
		return -ENOMEM;

	map = list->map;
	map->type = _DRM_GEM;
1269
	map->size = obj->base.size;
1270 1271 1272 1273
	map->handle = obj;

	/* Get a DRM GEM mmap offset allocated... */
	list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1274 1275
						    obj->base.size / PAGE_SIZE,
						    0, 0);
1276
	if (!list->file_offset_node) {
1277 1278
		DRM_ERROR("failed to allocate offset for bo %d\n",
			  obj->base.name);
1279
		ret = -ENOSPC;
1280 1281 1282 1283
		goto out_free_list;
	}

	list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1284 1285
						  obj->base.size / PAGE_SIZE,
						  0);
1286 1287 1288 1289 1290 1291
	if (!list->file_offset_node) {
		ret = -ENOMEM;
		goto out_free_list;
	}

	list->hash.key = list->file_offset_node->start;
1292 1293
	ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
	if (ret) {
1294 1295 1296 1297 1298 1299 1300 1301 1302
		DRM_ERROR("failed to add to map hash\n");
		goto out_free_mm;
	}

	return 0;

out_free_mm:
	drm_mm_put_block(list->file_offset_node);
out_free_list:
1303
	kfree(list->map);
C
Chris Wilson 已提交
1304
	list->map = NULL;
1305 1306 1307 1308

	return ret;
}

1309 1310 1311 1312
/**
 * i915_gem_release_mmap - remove physical page mappings
 * @obj: obj in question
 *
1313
 * Preserve the reservation of the mmapping with the DRM core code, but
1314 1315 1316 1317 1318 1319 1320 1321 1322
 * relinquish ownership of the pages back to the system.
 *
 * It is vital that we remove the page mapping if we have mapped a tiled
 * object through the GTT and then lose the fence register due to
 * resource pressure. Similarly if the object has been moved out of the
 * aperture, than pages mapped into userspace must be revoked. Removing the
 * mapping will then trigger a page fault on the next user access, allowing
 * fixup by i915_gem_fault().
 */
1323
void
1324
i915_gem_release_mmap(struct drm_i915_gem_object *obj)
1325
{
1326 1327
	if (!obj->fault_mappable)
		return;
1328

1329 1330 1331
	unmap_mapping_range(obj->base.dev->dev_mapping,
			    (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
			    obj->base.size, 1);
1332

1333
	obj->fault_mappable = false;
1334 1335
}

1336
static void
1337
i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
1338
{
1339
	struct drm_device *dev = obj->base.dev;
1340
	struct drm_gem_mm *mm = dev->mm_private;
1341
	struct drm_map_list *list = &obj->base.map_list;
1342 1343

	drm_ht_remove_item(&mm->offset_hash, &list->hash);
C
Chris Wilson 已提交
1344 1345 1346
	drm_mm_put_block(list->file_offset_node);
	kfree(list->map);
	list->map = NULL;
1347 1348
}

1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
static uint32_t
i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
{
	struct drm_device *dev = obj->base.dev;
	uint32_t size;

	if (INTEL_INFO(dev)->gen >= 4 ||
	    obj->tiling_mode == I915_TILING_NONE)
		return obj->base.size;

	/* Previous chips need a power-of-two fence region when tiling */
	if (INTEL_INFO(dev)->gen == 3)
		size = 1024*1024;
	else
		size = 512*1024;

	while (size < obj->base.size)
		size <<= 1;

	return size;
}

1371 1372 1373 1374 1375
/**
 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
 * @obj: object to check
 *
 * Return the required GTT alignment for an object, taking into account
1376
 * potential fence register mapping.
1377 1378
 */
static uint32_t
1379
i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
1380
{
1381
	struct drm_device *dev = obj->base.dev;
1382 1383 1384 1385 1386

	/*
	 * Minimum alignment is 4k (GTT page size), but might be greater
	 * if a fence register is needed for the object.
	 */
1387
	if (INTEL_INFO(dev)->gen >= 4 ||
1388
	    obj->tiling_mode == I915_TILING_NONE)
1389 1390
		return 4096;

1391 1392 1393 1394
	/*
	 * Previous chips need to be aligned to the size of the smallest
	 * fence register that can contain the object.
	 */
1395
	return i915_gem_get_gtt_size(obj);
1396 1397
}

1398 1399 1400 1401 1402 1403 1404 1405 1406
/**
 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
 *					 unfenced object
 * @obj: object to check
 *
 * Return the required GTT alignment for an object, only taking into account
 * unfenced tiled surface requirements.
 */
static uint32_t
1407
i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
1408
{
1409
	struct drm_device *dev = obj->base.dev;
1410 1411 1412 1413 1414 1415
	int tile_height;

	/*
	 * Minimum alignment is 4k (GTT page size) for sane hw.
	 */
	if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
1416
	    obj->tiling_mode == I915_TILING_NONE)
1417 1418 1419 1420 1421 1422 1423 1424
		return 4096;

	/*
	 * Older chips need unfenced tiled buffers to be aligned to the left
	 * edge of an even tile row (where tile rows are counted as if the bo is
	 * placed in a fenced gtt region).
	 */
	if (IS_GEN2(dev) ||
1425
	    (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
1426 1427 1428 1429
		tile_height = 32;
	else
		tile_height = 8;

1430
	return tile_height * obj->stride * 2;
1431 1432
}

1433 1434 1435 1436
/**
 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
 * @dev: DRM device
 * @data: GTT mapping ioctl data
1437
 * @file: GEM object info
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
 *
 * Simply returns the fake offset to userspace so it can mmap it.
 * The mmap call will end up in drm_gem_mmap(), which will set things
 * up so we can get faults in the handler above.
 *
 * The fault handler will take care of binding the object into the GTT
 * (since it may have been evicted to make room for something), allocating
 * a fence register, and mapping the appropriate aperture address into
 * userspace.
 */
int
i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1450
			struct drm_file *file)
1451
{
1452
	struct drm_i915_private *dev_priv = dev->dev_private;
1453
	struct drm_i915_gem_mmap_gtt *args = data;
1454
	struct drm_i915_gem_object *obj;
1455 1456 1457 1458 1459
	int ret;

	if (!(dev->driver->driver_features & DRIVER_GEM))
		return -ENODEV;

1460
	ret = i915_mutex_lock_interruptible(dev);
1461
	if (ret)
1462
		return ret;
1463

1464
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1465 1466 1467 1468
	if (obj == NULL) {
		ret = -ENOENT;
		goto unlock;
	}
1469

1470
	if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
1471 1472 1473 1474
		ret = -E2BIG;
		goto unlock;
	}

1475
	if (obj->madv != I915_MADV_WILLNEED) {
1476
		DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1477 1478
		ret = -EINVAL;
		goto out;
1479 1480
	}

1481
	if (!obj->base.map_list.map) {
1482
		ret = i915_gem_create_mmap_offset(obj);
1483 1484
		if (ret)
			goto out;
1485 1486
	}

1487
	args->offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
1488

1489
out:
1490
	drm_gem_object_unreference(&obj->base);
1491
unlock:
1492
	mutex_unlock(&dev->struct_mutex);
1493
	return ret;
1494 1495
}

1496
static int
1497
i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
			      gfp_t gfpmask)
{
	int page_count, i;
	struct address_space *mapping;
	struct inode *inode;
	struct page *page;

	/* Get the list of pages out of our struct file.  They'll be pinned
	 * at this point until we release them.
	 */
1508 1509 1510 1511
	page_count = obj->base.size / PAGE_SIZE;
	BUG_ON(obj->pages != NULL);
	obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
	if (obj->pages == NULL)
1512 1513
		return -ENOMEM;

1514
	inode = obj->base.filp->f_path.dentry->d_inode;
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
	mapping = inode->i_mapping;
	for (i = 0; i < page_count; i++) {
		page = read_cache_page_gfp(mapping, i,
					   GFP_HIGHUSER |
					   __GFP_COLD |
					   __GFP_RECLAIMABLE |
					   gfpmask);
		if (IS_ERR(page))
			goto err_pages;

1525
		obj->pages[i] = page;
1526 1527
	}

1528
	if (obj->tiling_mode != I915_TILING_NONE)
1529 1530 1531 1532 1533 1534
		i915_gem_object_do_bit_17_swizzle(obj);

	return 0;

err_pages:
	while (i--)
1535
		page_cache_release(obj->pages[i]);
1536

1537 1538
	drm_free_large(obj->pages);
	obj->pages = NULL;
1539 1540 1541
	return PTR_ERR(page);
}

1542
static void
1543
i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1544
{
1545
	int page_count = obj->base.size / PAGE_SIZE;
1546 1547
	int i;

1548
	BUG_ON(obj->madv == __I915_MADV_PURGED);
1549

1550
	if (obj->tiling_mode != I915_TILING_NONE)
1551 1552
		i915_gem_object_save_bit_17_swizzle(obj);

1553 1554
	if (obj->madv == I915_MADV_DONTNEED)
		obj->dirty = 0;
1555 1556

	for (i = 0; i < page_count; i++) {
1557 1558
		if (obj->dirty)
			set_page_dirty(obj->pages[i]);
1559

1560 1561
		if (obj->madv == I915_MADV_WILLNEED)
			mark_page_accessed(obj->pages[i]);
1562

1563
		page_cache_release(obj->pages[i]);
1564
	}
1565
	obj->dirty = 0;
1566

1567 1568
	drm_free_large(obj->pages);
	obj->pages = NULL;
1569 1570
}

1571
void
1572
i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
1573
			       struct intel_ring_buffer *ring)
1574
{
1575
	struct drm_device *dev = obj->base.dev;
1576
	struct drm_i915_private *dev_priv = dev->dev_private;
1577
	uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
1578

1579
	BUG_ON(ring == NULL);
1580
	obj->ring = ring;
1581 1582

	/* Add a reference if we're newly entering the active list. */
1583 1584 1585
	if (!obj->active) {
		drm_gem_object_reference(&obj->base);
		obj->active = 1;
1586
	}
1587

1588
	/* Move from whatever list we were on to the tail of execution. */
1589 1590
	list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
	list_move_tail(&obj->ring_list, &ring->active_list);
1591

1592
	obj->last_rendering_seqno = seqno;
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
	if (obj->fenced_gpu_access) {
		struct drm_i915_fence_reg *reg;

		BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);

		obj->last_fenced_seqno = seqno;
		obj->last_fenced_ring = ring;

		reg = &dev_priv->fence_regs[obj->fence_reg];
		list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
	}
}

static void
i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
{
	list_del_init(&obj->ring_list);
	obj->last_rendering_seqno = 0;
	obj->last_fenced_seqno = 0;
1612 1613
}

1614
static void
1615
i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
1616
{
1617
	struct drm_device *dev = obj->base.dev;
1618 1619
	drm_i915_private_t *dev_priv = dev->dev_private;

1620 1621
	BUG_ON(!obj->active);
	list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648

	i915_gem_object_move_off_active(obj);
}

static void
i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
{
	struct drm_device *dev = obj->base.dev;
	struct drm_i915_private *dev_priv = dev->dev_private;

	if (obj->pin_count != 0)
		list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
	else
		list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);

	BUG_ON(!list_empty(&obj->gpu_write_list));
	BUG_ON(!obj->active);
	obj->ring = NULL;

	i915_gem_object_move_off_active(obj);
	obj->fenced_gpu_access = false;
	obj->last_fenced_ring = NULL;

	obj->active = 0;
	drm_gem_object_unreference(&obj->base);

	WARN_ON(i915_verify_lists(dev));
1649
}
1650

1651 1652
/* Immediately discard the backing storage */
static void
1653
i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1654
{
C
Chris Wilson 已提交
1655
	struct inode *inode;
1656

1657 1658 1659 1660 1661 1662
	/* Our goal here is to return as much of the memory as
	 * is possible back to the system as we are called from OOM.
	 * To do this we must instruct the shmfs to drop all of its
	 * backing pages, *now*. Here we mirror the actions taken
	 * when by shmem_delete_inode() to release the backing store.
	 */
1663
	inode = obj->base.filp->f_path.dentry->d_inode;
1664 1665 1666
	truncate_inode_pages(inode->i_mapping, 0);
	if (inode->i_op->truncate_range)
		inode->i_op->truncate_range(inode, 0, (loff_t)-1);
C
Chris Wilson 已提交
1667

1668
	obj->madv = __I915_MADV_PURGED;
1669 1670 1671
}

static inline int
1672
i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1673
{
1674
	return obj->madv == I915_MADV_DONTNEED;
1675 1676
}

1677 1678
static void
i915_gem_process_flushing_list(struct drm_device *dev,
1679
			       uint32_t flush_domains,
1680
			       struct intel_ring_buffer *ring)
1681
{
1682
	struct drm_i915_gem_object *obj, *next;
1683

1684
	list_for_each_entry_safe(obj, next,
1685
				 &ring->gpu_write_list,
1686
				 gpu_write_list) {
1687 1688
		if (obj->base.write_domain & flush_domains) {
			uint32_t old_write_domain = obj->base.write_domain;
1689

1690 1691
			obj->base.write_domain = 0;
			list_del_init(&obj->gpu_write_list);
1692
			i915_gem_object_move_to_active(obj, ring);
1693 1694

			trace_i915_gem_object_change_domain(obj,
1695
							    obj->base.read_domains,
1696 1697 1698 1699
							    old_write_domain);
		}
	}
}
1700

1701
int
1702
i915_add_request(struct drm_device *dev,
1703
		 struct drm_file *file,
C
Chris Wilson 已提交
1704
		 struct drm_i915_gem_request *request,
1705
		 struct intel_ring_buffer *ring)
1706 1707
{
	drm_i915_private_t *dev_priv = dev->dev_private;
1708
	struct drm_i915_file_private *file_priv = NULL;
1709 1710
	uint32_t seqno;
	int was_empty;
1711 1712 1713
	int ret;

	BUG_ON(request == NULL);
1714

1715 1716
	if (file != NULL)
		file_priv = file->driver_priv;
1717

1718 1719 1720
	ret = ring->add_request(ring, &seqno);
	if (ret)
	    return ret;
1721

1722
	ring->outstanding_lazy_request = false;
1723 1724

	request->seqno = seqno;
1725
	request->ring = ring;
1726
	request->emitted_jiffies = jiffies;
1727 1728 1729
	was_empty = list_empty(&ring->request_list);
	list_add_tail(&request->list, &ring->request_list);

1730
	if (file_priv) {
1731
		spin_lock(&file_priv->mm.lock);
1732
		request->file_priv = file_priv;
1733
		list_add_tail(&request->client_list,
1734
			      &file_priv->mm.request_list);
1735
		spin_unlock(&file_priv->mm.lock);
1736
	}
1737

B
Ben Gamari 已提交
1738
	if (!dev_priv->mm.suspended) {
1739 1740
		mod_timer(&dev_priv->hangcheck_timer,
			  jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
B
Ben Gamari 已提交
1741
		if (was_empty)
1742 1743
			queue_delayed_work(dev_priv->wq,
					   &dev_priv->mm.retire_work, HZ);
B
Ben Gamari 已提交
1744
	}
1745
	return 0;
1746 1747
}

1748 1749
static inline void
i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1750
{
1751
	struct drm_i915_file_private *file_priv = request->file_priv;
1752

1753 1754
	if (!file_priv)
		return;
C
Chris Wilson 已提交
1755

1756 1757 1758 1759
	spin_lock(&file_priv->mm.lock);
	list_del(&request->client_list);
	request->file_priv = NULL;
	spin_unlock(&file_priv->mm.lock);
1760 1761
}

1762 1763
static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
				      struct intel_ring_buffer *ring)
1764
{
1765 1766
	while (!list_empty(&ring->request_list)) {
		struct drm_i915_gem_request *request;
1767

1768 1769 1770
		request = list_first_entry(&ring->request_list,
					   struct drm_i915_gem_request,
					   list);
1771

1772
		list_del(&request->list);
1773
		i915_gem_request_remove_from_client(request);
1774 1775
		kfree(request);
	}
1776

1777
	while (!list_empty(&ring->active_list)) {
1778
		struct drm_i915_gem_object *obj;
1779

1780 1781 1782
		obj = list_first_entry(&ring->active_list,
				       struct drm_i915_gem_object,
				       ring_list);
1783

1784 1785 1786
		obj->base.write_domain = 0;
		list_del_init(&obj->gpu_write_list);
		i915_gem_object_move_to_inactive(obj);
1787 1788 1789
	}
}

1790 1791 1792 1793 1794 1795 1796
static void i915_gem_reset_fences(struct drm_device *dev)
{
	struct drm_i915_private *dev_priv = dev->dev_private;
	int i;

	for (i = 0; i < 16; i++) {
		struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
1797 1798 1799 1800 1801 1802 1803 1804 1805
		struct drm_i915_gem_object *obj = reg->obj;

		if (!obj)
			continue;

		if (obj->tiling_mode)
			i915_gem_release_mmap(obj);

		i915_gem_clear_fence_reg(obj);
1806 1807 1808
	}
}

1809
void i915_gem_reset(struct drm_device *dev)
1810
{
1811
	struct drm_i915_private *dev_priv = dev->dev_private;
1812
	struct drm_i915_gem_object *obj;
1813

1814
	i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
1815
	i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
1816
	i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
1817 1818 1819 1820 1821 1822

	/* Remove anything from the flushing lists. The GPU cache is likely
	 * to be lost on reset along with the data, so simply move the
	 * lost bo to the inactive list.
	 */
	while (!list_empty(&dev_priv->mm.flushing_list)) {
1823 1824 1825
		obj= list_first_entry(&dev_priv->mm.flushing_list,
				      struct drm_i915_gem_object,
				      mm_list);
1826

1827 1828 1829
		obj->base.write_domain = 0;
		list_del_init(&obj->gpu_write_list);
		i915_gem_object_move_to_inactive(obj);
1830 1831 1832 1833 1834
	}

	/* Move everything out of the GPU domains to ensure we do any
	 * necessary invalidation upon reuse.
	 */
1835
	list_for_each_entry(obj,
1836
			    &dev_priv->mm.inactive_list,
1837
			    mm_list)
1838
	{
1839
		obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1840
	}
1841 1842

	/* The fence registers are invalidated so clear them out */
1843
	i915_gem_reset_fences(dev);
1844 1845 1846 1847 1848
}

/**
 * This function clears the request list as sequence numbers are passed.
 */
1849 1850 1851
static void
i915_gem_retire_requests_ring(struct drm_device *dev,
			      struct intel_ring_buffer *ring)
1852 1853 1854 1855
{
	drm_i915_private_t *dev_priv = dev->dev_private;
	uint32_t seqno;

1856 1857
	if (!ring->status_page.page_addr ||
	    list_empty(&ring->request_list))
1858 1859
		return;

1860
	WARN_ON(i915_verify_lists(dev));
1861

1862
	seqno = ring->get_seqno(ring);
1863
	while (!list_empty(&ring->request_list)) {
1864 1865
		struct drm_i915_gem_request *request;

1866
		request = list_first_entry(&ring->request_list,
1867 1868 1869
					   struct drm_i915_gem_request,
					   list);

1870
		if (!i915_seqno_passed(seqno, request->seqno))
1871 1872 1873 1874 1875
			break;

		trace_i915_gem_request_retire(dev, request->seqno);

		list_del(&request->list);
1876
		i915_gem_request_remove_from_client(request);
1877 1878
		kfree(request);
	}
1879

1880 1881 1882 1883
	/* Move any buffers on the active list that are no longer referenced
	 * by the ringbuffer to the flushing/inactive lists as appropriate.
	 */
	while (!list_empty(&ring->active_list)) {
1884
		struct drm_i915_gem_object *obj;
1885

1886 1887 1888
		obj= list_first_entry(&ring->active_list,
				      struct drm_i915_gem_object,
				      ring_list);
1889

1890
		if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
1891
			break;
1892

1893
		if (obj->base.write_domain != 0)
1894 1895 1896
			i915_gem_object_move_to_flushing(obj);
		else
			i915_gem_object_move_to_inactive(obj);
1897
	}
1898 1899 1900

	if (unlikely (dev_priv->trace_irq_seqno &&
		      i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
1901
		ring->user_irq_put(ring);
1902 1903
		dev_priv->trace_irq_seqno = 0;
	}
1904 1905

	WARN_ON(i915_verify_lists(dev));
1906 1907
}

1908 1909 1910 1911 1912
void
i915_gem_retire_requests(struct drm_device *dev)
{
	drm_i915_private_t *dev_priv = dev->dev_private;

1913
	if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1914
	    struct drm_i915_gem_object *obj, *next;
1915 1916 1917 1918 1919 1920

	    /* We must be careful that during unbind() we do not
	     * accidentally infinitely recurse into retire requests.
	     * Currently:
	     *   retire -> free -> unbind -> wait -> retire_ring
	     */
1921
	    list_for_each_entry_safe(obj, next,
1922
				     &dev_priv->mm.deferred_free_list,
1923
				     mm_list)
1924
		    i915_gem_free_object_tail(obj);
1925 1926
	}

1927
	i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
1928
	i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
1929
	i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
1930 1931
}

1932
static void
1933 1934 1935 1936 1937 1938 1939 1940 1941
i915_gem_retire_work_handler(struct work_struct *work)
{
	drm_i915_private_t *dev_priv;
	struct drm_device *dev;

	dev_priv = container_of(work, drm_i915_private_t,
				mm.retire_work.work);
	dev = dev_priv->dev;

1942 1943 1944 1945 1946 1947
	/* Come back later if the device is busy... */
	if (!mutex_trylock(&dev->struct_mutex)) {
		queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
		return;
	}

1948
	i915_gem_retire_requests(dev);
1949

1950
	if (!dev_priv->mm.suspended &&
1951
		(!list_empty(&dev_priv->render_ring.request_list) ||
1952 1953
		 !list_empty(&dev_priv->bsd_ring.request_list) ||
		 !list_empty(&dev_priv->blt_ring.request_list)))
1954
		queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1955 1956 1957
	mutex_unlock(&dev->struct_mutex);
}

1958
int
1959
i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
1960
		     bool interruptible, struct intel_ring_buffer *ring)
1961 1962
{
	drm_i915_private_t *dev_priv = dev->dev_private;
1963
	u32 ier;
1964 1965 1966 1967
	int ret = 0;

	BUG_ON(seqno == 0);

1968
	if (atomic_read(&dev_priv->mm.wedged))
1969 1970
		return -EAGAIN;

1971
	if (seqno == ring->outstanding_lazy_request) {
1972 1973 1974 1975
		struct drm_i915_gem_request *request;

		request = kzalloc(sizeof(*request), GFP_KERNEL);
		if (request == NULL)
1976
			return -ENOMEM;
1977 1978 1979 1980 1981 1982 1983 1984

		ret = i915_add_request(dev, NULL, request, ring);
		if (ret) {
			kfree(request);
			return ret;
		}

		seqno = request->seqno;
1985
	}
1986

1987
	if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
1988
		if (HAS_PCH_SPLIT(dev))
1989 1990 1991
			ier = I915_READ(DEIER) | I915_READ(GTIER);
		else
			ier = I915_READ(IER);
1992 1993 1994 1995 1996 1997 1998
		if (!ier) {
			DRM_ERROR("something (likely vbetool) disabled "
				  "interrupts, re-enabling\n");
			i915_driver_irq_preinstall(dev);
			i915_driver_irq_postinstall(dev);
		}

C
Chris Wilson 已提交
1999 2000
		trace_i915_gem_request_wait_begin(dev, seqno);

2001
		ring->waiting_seqno = seqno;
2002
		ring->user_irq_get(ring);
2003
		if (interruptible)
2004
			ret = wait_event_interruptible(ring->irq_queue,
2005
				i915_seqno_passed(ring->get_seqno(ring), seqno)
2006
				|| atomic_read(&dev_priv->mm.wedged));
2007
		else
2008
			wait_event(ring->irq_queue,
2009
				i915_seqno_passed(ring->get_seqno(ring), seqno)
2010
				|| atomic_read(&dev_priv->mm.wedged));
2011

2012
		ring->user_irq_put(ring);
2013
		ring->waiting_seqno = 0;
C
Chris Wilson 已提交
2014 2015

		trace_i915_gem_request_wait_end(dev, seqno);
2016
	}
2017
	if (atomic_read(&dev_priv->mm.wedged))
2018
		ret = -EAGAIN;
2019 2020

	if (ret && ret != -ERESTARTSYS)
2021
		DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
2022
			  __func__, ret, seqno, ring->get_seqno(ring),
2023
			  dev_priv->next_seqno);
2024 2025 2026 2027 2028 2029 2030

	/* Directly dispatch request retiring.  While we have the work queue
	 * to handle this, the waiter on a request often wants an associated
	 * buffer to have made it to the inactive list, and we would need
	 * a separate wait queue to handle that.
	 */
	if (ret == 0)
2031
		i915_gem_retire_requests_ring(dev, ring);
2032 2033 2034 2035

	return ret;
}

2036 2037 2038 2039 2040
/**
 * Waits for a sequence number to be signaled, and cleans up the
 * request and object lists appropriately for that event.
 */
static int
2041
i915_wait_request(struct drm_device *dev, uint32_t seqno,
2042
		  struct intel_ring_buffer *ring)
2043
{
2044
	return i915_do_wait_request(dev, seqno, 1, ring);
2045 2046
}

2047 2048 2049 2050
/**
 * Ensures that all rendering to the object has completed and the object is
 * safe to unbind from the GTT or access from the CPU.
 */
2051
int
2052
i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
2053
			       bool interruptible)
2054
{
2055
	struct drm_device *dev = obj->base.dev;
2056 2057
	int ret;

2058 2059
	/* This function only exists to support waiting for existing rendering,
	 * not for emitting required flushes.
2060
	 */
2061
	BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
2062 2063 2064 2065

	/* If there is rendering queued on the buffer being evicted, wait for
	 * it.
	 */
2066
	if (obj->active) {
2067
		ret = i915_do_wait_request(dev,
2068
					   obj->last_rendering_seqno,
2069
					   interruptible,
2070
					   obj->ring);
2071
		if (ret)
2072 2073 2074 2075 2076 2077 2078 2079 2080
			return ret;
	}

	return 0;
}

/**
 * Unbinds an object from the GTT aperture.
 */
2081
int
2082
i915_gem_object_unbind(struct drm_i915_gem_object *obj)
2083 2084 2085
{
	int ret = 0;

2086
	if (obj->gtt_space == NULL)
2087 2088
		return 0;

2089
	if (obj->pin_count != 0) {
2090 2091 2092 2093
		DRM_ERROR("Attempting to unbind pinned buffer\n");
		return -EINVAL;
	}

2094 2095 2096
	/* blow away mappings if mapped through GTT */
	i915_gem_release_mmap(obj);

2097 2098 2099 2100 2101 2102
	/* Move the object to the CPU domain to ensure that
	 * any possible CPU writes while it's not in the GTT
	 * are flushed when we go to remap it. This will
	 * also ensure that all pending GPU writes are finished
	 * before we unbind.
	 */
2103
	ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2104
	if (ret == -ERESTARTSYS)
2105
		return ret;
2106 2107 2108 2109
	/* Continue on if we fail due to EIO, the GPU is hung so we
	 * should be safe and we need to cleanup or else we might
	 * cause memory corruption through use-after-free.
	 */
2110 2111
	if (ret) {
		i915_gem_clflush_object(obj);
2112
		obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2113
	}
2114

2115
	/* release the fence reg _after_ flushing */
2116
	if (obj->fence_reg != I915_FENCE_REG_NONE)
2117 2118
		i915_gem_clear_fence_reg(obj);

2119
	i915_gem_gtt_unbind_object(obj);
2120
	i915_gem_object_put_pages_gtt(obj);
2121

2122
	list_del_init(&obj->gtt_list);
2123
	list_del_init(&obj->mm_list);
2124
	/* Avoid an unnecessary call to unbind on rebind. */
2125
	obj->map_and_fenceable = true;
2126

2127 2128 2129
	drm_mm_put_block(obj->gtt_space);
	obj->gtt_space = NULL;
	obj->gtt_offset = 0;
2130

2131
	if (i915_gem_object_is_purgeable(obj))
2132 2133
		i915_gem_object_truncate(obj);

C
Chris Wilson 已提交
2134 2135
	trace_i915_gem_object_unbind(obj);

2136
	return ret;
2137 2138
}

2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
void
i915_gem_flush_ring(struct drm_device *dev,
		    struct intel_ring_buffer *ring,
		    uint32_t invalidate_domains,
		    uint32_t flush_domains)
{
	ring->flush(ring, invalidate_domains, flush_domains);
	i915_gem_process_flushing_list(dev, flush_domains, ring);
}

2149 2150 2151
static int i915_ring_idle(struct drm_device *dev,
			  struct intel_ring_buffer *ring)
{
2152
	if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
2153 2154
		return 0;

2155
	i915_gem_flush_ring(dev, ring,
2156 2157 2158 2159 2160 2161
			    I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
	return i915_wait_request(dev,
				 i915_gem_next_request_seqno(dev, ring),
				 ring);
}

2162
int
2163 2164 2165 2166
i915_gpu_idle(struct drm_device *dev)
{
	drm_i915_private_t *dev_priv = dev->dev_private;
	bool lists_empty;
2167
	int ret;
2168

2169
	lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2170
		       list_empty(&dev_priv->mm.active_list));
2171 2172 2173 2174
	if (lists_empty)
		return 0;

	/* Flush everything onto the inactive list. */
2175
	ret = i915_ring_idle(dev, &dev_priv->render_ring);
2176 2177
	if (ret)
		return ret;
2178

2179 2180 2181
	ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
	if (ret)
		return ret;
2182

2183 2184 2185
	ret = i915_ring_idle(dev, &dev_priv->blt_ring);
	if (ret)
		return ret;
2186

2187
	return 0;
2188 2189
}

2190 2191
static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
				       struct intel_ring_buffer *pipelined)
2192
{
2193
	struct drm_device *dev = obj->base.dev;
2194
	drm_i915_private_t *dev_priv = dev->dev_private;
2195 2196
	u32 size = obj->gtt_space->size;
	int regnum = obj->fence_reg;
2197 2198
	uint64_t val;

2199
	val = (uint64_t)((obj->gtt_offset + size - 4096) &
2200
			 0xfffff000) << 32;
2201 2202
	val |= obj->gtt_offset & 0xfffff000;
	val |= (uint64_t)((obj->stride / 128) - 1) <<
2203 2204
		SANDYBRIDGE_FENCE_PITCH_SHIFT;

2205
	if (obj->tiling_mode == I915_TILING_Y)
2206 2207 2208
		val |= 1 << I965_FENCE_TILING_Y_SHIFT;
	val |= I965_FENCE_REG_VALID;

2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
	if (pipelined) {
		int ret = intel_ring_begin(pipelined, 6);
		if (ret)
			return ret;

		intel_ring_emit(pipelined, MI_NOOP);
		intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
		intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
		intel_ring_emit(pipelined, (u32)val);
		intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
		intel_ring_emit(pipelined, (u32)(val >> 32));
		intel_ring_advance(pipelined);
	} else
		I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);

	return 0;
2225 2226
}

2227 2228
static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
				struct intel_ring_buffer *pipelined)
2229
{
2230
	struct drm_device *dev = obj->base.dev;
2231
	drm_i915_private_t *dev_priv = dev->dev_private;
2232 2233
	u32 size = obj->gtt_space->size;
	int regnum = obj->fence_reg;
2234 2235
	uint64_t val;

2236
	val = (uint64_t)((obj->gtt_offset + size - 4096) &
2237
		    0xfffff000) << 32;
2238 2239 2240
	val |= obj->gtt_offset & 0xfffff000;
	val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
	if (obj->tiling_mode == I915_TILING_Y)
2241 2242 2243
		val |= 1 << I965_FENCE_TILING_Y_SHIFT;
	val |= I965_FENCE_REG_VALID;

2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259
	if (pipelined) {
		int ret = intel_ring_begin(pipelined, 6);
		if (ret)
			return ret;

		intel_ring_emit(pipelined, MI_NOOP);
		intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
		intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
		intel_ring_emit(pipelined, (u32)val);
		intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
		intel_ring_emit(pipelined, (u32)(val >> 32));
		intel_ring_advance(pipelined);
	} else
		I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);

	return 0;
2260 2261
}

2262 2263
static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
				struct intel_ring_buffer *pipelined)
2264
{
2265
	struct drm_device *dev = obj->base.dev;
2266
	drm_i915_private_t *dev_priv = dev->dev_private;
2267
	u32 size = obj->gtt_space->size;
2268
	u32 fence_reg, val, pitch_val;
2269
	int tile_width;
2270

2271 2272 2273 2274 2275 2276
	if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
		 (size & -size) != size ||
		 (obj->gtt_offset & (size - 1)),
		 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
		 obj->gtt_offset, obj->map_and_fenceable, size))
		return -EINVAL;
2277

2278
	if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2279
		tile_width = 128;
2280
	else
2281 2282 2283
		tile_width = 512;

	/* Note: pitch better be a power of two tile widths */
2284
	pitch_val = obj->stride / tile_width;
2285
	pitch_val = ffs(pitch_val) - 1;
2286

2287 2288
	val = obj->gtt_offset;
	if (obj->tiling_mode == I915_TILING_Y)
2289
		val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2290
	val |= I915_FENCE_SIZE_BITS(size);
2291 2292 2293
	val |= pitch_val << I830_FENCE_PITCH_SHIFT;
	val |= I830_FENCE_REG_VALID;

2294
	fence_reg = obj->fence_reg;
2295 2296
	if (fence_reg < 8)
		fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2297
	else
2298
		fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313

	if (pipelined) {
		int ret = intel_ring_begin(pipelined, 4);
		if (ret)
			return ret;

		intel_ring_emit(pipelined, MI_NOOP);
		intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
		intel_ring_emit(pipelined, fence_reg);
		intel_ring_emit(pipelined, val);
		intel_ring_advance(pipelined);
	} else
		I915_WRITE(fence_reg, val);

	return 0;
2314 2315
}

2316 2317
static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
				struct intel_ring_buffer *pipelined)
2318
{
2319
	struct drm_device *dev = obj->base.dev;
2320
	drm_i915_private_t *dev_priv = dev->dev_private;
2321 2322
	u32 size = obj->gtt_space->size;
	int regnum = obj->fence_reg;
2323 2324 2325
	uint32_t val;
	uint32_t pitch_val;

2326 2327 2328 2329 2330 2331
	if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
		 (size & -size) != size ||
		 (obj->gtt_offset & (size - 1)),
		 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
		 obj->gtt_offset, size))
		return -EINVAL;
2332

2333
	pitch_val = obj->stride / 128;
2334 2335
	pitch_val = ffs(pitch_val) - 1;

2336 2337
	val = obj->gtt_offset;
	if (obj->tiling_mode == I915_TILING_Y)
2338
		val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2339
	val |= I830_FENCE_SIZE_BITS(size);
2340 2341 2342
	val |= pitch_val << I830_FENCE_PITCH_SHIFT;
	val |= I830_FENCE_REG_VALID;

2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356
	if (pipelined) {
		int ret = intel_ring_begin(pipelined, 4);
		if (ret)
			return ret;

		intel_ring_emit(pipelined, MI_NOOP);
		intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
		intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
		intel_ring_emit(pipelined, val);
		intel_ring_advance(pipelined);
	} else
		I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);

	return 0;
2357 2358
}

2359 2360
static int i915_find_fence_reg(struct drm_device *dev,
			       bool interruptible)
2361 2362
{
	struct drm_i915_private *dev_priv = dev->dev_private;
2363
	struct drm_i915_fence_reg *reg;
2364
	struct drm_i915_gem_object *obj = NULL;
2365 2366 2367 2368 2369 2370 2371 2372 2373
	int i, avail, ret;

	/* First try to find a free reg */
	avail = 0;
	for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
		reg = &dev_priv->fence_regs[i];
		if (!reg->obj)
			return i;

2374 2375
		if (!reg->obj->pin_count)
			avail++;
2376 2377 2378 2379 2380 2381
	}

	if (avail == 0)
		return -ENOSPC;

	/* None available, try to steal one or wait for a user to finish */
2382
	avail = I915_FENCE_REG_NONE;
2383 2384
	list_for_each_entry(reg, &dev_priv->mm.fence_list,
			    lru_list) {
2385 2386
		obj = reg->obj;
		if (obj->pin_count)
2387 2388 2389
			continue;

		/* found one! */
2390
		avail = obj->fence_reg;
2391 2392 2393
		break;
	}

2394
	BUG_ON(avail == I915_FENCE_REG_NONE);
2395 2396 2397 2398 2399

	/* We only have a reference on obj from the active list. put_fence_reg
	 * might drop that one, causing a use-after-free in it. So hold a
	 * private reference to obj like the other callers of put_fence_reg
	 * (set_tiling ioctl) do. */
2400 2401 2402
	drm_gem_object_reference(&obj->base);
	ret = i915_gem_object_put_fence_reg(obj, interruptible);
	drm_gem_object_unreference(&obj->base);
2403 2404 2405
	if (ret != 0)
		return ret;

2406
	return avail;
2407 2408
}

2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421
/**
 * i915_gem_object_get_fence_reg - set up a fence reg for an object
 * @obj: object to map through a fence reg
 *
 * When mapping objects through the GTT, userspace wants to be able to write
 * to them without having to worry about swizzling if the object is tiled.
 *
 * This function walks the fence regs looking for a free one for @obj,
 * stealing one if it can't find any.
 *
 * It then sets up the reg based on the object's properties: address, pitch
 * and tiling format.
 */
2422
int
2423
i915_gem_object_get_fence_reg(struct drm_i915_gem_object *obj,
2424
			      bool interruptible)
2425
{
2426
	struct drm_device *dev = obj->base.dev;
J
Jesse Barnes 已提交
2427
	struct drm_i915_private *dev_priv = dev->dev_private;
2428
	struct drm_i915_fence_reg *reg = NULL;
2429
	struct intel_ring_buffer *pipelined = NULL;
2430
	int ret;
2431

2432
	/* Just update our place in the LRU if our fence is getting used. */
2433 2434
	if (obj->fence_reg != I915_FENCE_REG_NONE) {
		reg = &dev_priv->fence_regs[obj->fence_reg];
2435
		list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2436 2437 2438
		return 0;
	}

2439
	switch (obj->tiling_mode) {
2440 2441 2442 2443
	case I915_TILING_NONE:
		WARN(1, "allocating a fence for non-tiled object?\n");
		break;
	case I915_TILING_X:
2444
		if (!obj->stride)
2445
			return -EINVAL;
2446
		WARN((obj->stride & (512 - 1)),
2447
		     "object 0x%08x is X tiled but has non-512B pitch\n",
2448
		     obj->gtt_offset);
2449 2450
		break;
	case I915_TILING_Y:
2451
		if (!obj->stride)
2452
			return -EINVAL;
2453
		WARN((obj->stride & (128 - 1)),
2454
		     "object 0x%08x is Y tiled but has non-128B pitch\n",
2455
		     obj->gtt_offset);
2456 2457 2458
		break;
	}

2459
	ret = i915_find_fence_reg(dev, interruptible);
2460 2461
	if (ret < 0)
		return ret;
2462

2463 2464
	obj->fence_reg = ret;
	reg = &dev_priv->fence_regs[obj->fence_reg];
2465
	list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
2466

2467 2468
	reg->obj = obj;

2469 2470
	switch (INTEL_INFO(dev)->gen) {
	case 6:
2471
		ret = sandybridge_write_fence_reg(obj, pipelined);
2472 2473 2474
		break;
	case 5:
	case 4:
2475
		ret = i965_write_fence_reg(obj, pipelined);
2476 2477
		break;
	case 3:
2478
		ret = i915_write_fence_reg(obj, pipelined);
2479 2480
		break;
	case 2:
2481
		ret = i830_write_fence_reg(obj, pipelined);
2482 2483
		break;
	}
2484

2485
	trace_i915_gem_object_get_fence(obj,
2486 2487
					obj->fence_reg,
					obj->tiling_mode);
2488
	return ret;
2489 2490 2491 2492 2493 2494 2495
}

/**
 * i915_gem_clear_fence_reg - clear out fence register info
 * @obj: object to clear
 *
 * Zeroes out the fence register itself and clears out the associated
2496
 * data structures in dev_priv and obj.
2497 2498
 */
static void
2499
i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj)
2500
{
2501
	struct drm_device *dev = obj->base.dev;
J
Jesse Barnes 已提交
2502
	drm_i915_private_t *dev_priv = dev->dev_private;
2503
	struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[obj->fence_reg];
2504
	uint32_t fence_reg;
2505

2506 2507
	switch (INTEL_INFO(dev)->gen) {
	case 6:
2508
		I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2509
			     (obj->fence_reg * 8), 0);
2510 2511 2512
		break;
	case 5:
	case 4:
2513
		I915_WRITE64(FENCE_REG_965_0 + (obj->fence_reg * 8), 0);
2514 2515
		break;
	case 3:
2516 2517
		if (obj->fence_reg >= 8)
			fence_reg = FENCE_REG_945_8 + (obj->fence_reg - 8) * 4;
2518
		else
2519
	case 2:
2520
			fence_reg = FENCE_REG_830_0 + obj->fence_reg * 4;
2521 2522

		I915_WRITE(fence_reg, 0);
2523
		break;
2524
	}
2525

2526
	reg->obj = NULL;
2527
	obj->fence_reg = I915_FENCE_REG_NONE;
2528
	list_del_init(&reg->lru_list);
2529 2530
}

2531 2532 2533 2534
/**
 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
 * to the buffer to finish, and then resets the fence register.
 * @obj: tiled object holding a fence register.
2535
 * @bool: whether the wait upon the fence is interruptible
2536 2537
 *
 * Zeroes out the fence register itself and clears out the associated
2538
 * data structures in dev_priv and obj.
2539 2540
 */
int
2541
i915_gem_object_put_fence_reg(struct drm_i915_gem_object *obj,
2542
			      bool interruptible)
2543
{
2544
	struct drm_device *dev = obj->base.dev;
2545
	int ret;
2546

2547
	if (obj->fence_reg == I915_FENCE_REG_NONE)
2548 2549
		return 0;

2550 2551 2552 2553 2554 2555
	/* If we've changed tiling, GTT-mappings of the object
	 * need to re-fault to ensure that the correct fence register
	 * setup is in place.
	 */
	i915_gem_release_mmap(obj);

2556 2557 2558 2559
	/* On the i915, GPU access to tiled buffers is via a fence,
	 * therefore we must wait for any outstanding access to complete
	 * before clearing the fence.
	 */
2560
	if (obj->fenced_gpu_access) {
2561
		i915_gem_object_flush_gpu_write_domain(obj);
2562 2563 2564 2565 2566 2567 2568 2569
		obj->fenced_gpu_access = false;
	}

	if (obj->last_fenced_seqno) {
		ret = i915_do_wait_request(dev,
					   obj->last_fenced_seqno,
					   interruptible,
					   obj->last_fenced_ring);
2570
		if (ret)
2571
			return ret;
C
Chris Wilson 已提交
2572

2573
		obj->last_fenced_seqno = false;
2574 2575
	}

2576
	i915_gem_object_flush_gtt_write_domain(obj);
2577
	i915_gem_clear_fence_reg(obj);
2578 2579 2580 2581

	return 0;
}

2582 2583 2584 2585
/**
 * Finds free space in the GTT aperture and binds the object there.
 */
static int
2586
i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
2587
			    unsigned alignment,
2588
			    bool map_and_fenceable)
2589
{
2590
	struct drm_device *dev = obj->base.dev;
2591 2592
	drm_i915_private_t *dev_priv = dev->dev_private;
	struct drm_mm_node *free_space;
2593
	gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2594
	u32 size, fence_size, fence_alignment, unfenced_alignment;
2595
	bool mappable, fenceable;
2596
	int ret;
2597

2598
	if (obj->madv != I915_MADV_WILLNEED) {
2599 2600 2601 2602
		DRM_ERROR("Attempting to bind a purgeable object\n");
		return -EINVAL;
	}

2603 2604 2605
	fence_size = i915_gem_get_gtt_size(obj);
	fence_alignment = i915_gem_get_gtt_alignment(obj);
	unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
2606

2607
	if (alignment == 0)
2608 2609
		alignment = map_and_fenceable ? fence_alignment :
						unfenced_alignment;
2610
	if (map_and_fenceable && alignment & (fence_alignment - 1)) {
2611 2612 2613 2614
		DRM_ERROR("Invalid object alignment requested %u\n", alignment);
		return -EINVAL;
	}

2615
	size = map_and_fenceable ? fence_size : obj->base.size;
2616

2617 2618 2619
	/* If the object is bigger than the entire aperture, reject it early
	 * before evicting everything in a vain attempt to find space.
	 */
2620
	if (obj->base.size >
2621
	    (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
2622 2623 2624 2625
		DRM_ERROR("Attempting to bind an object larger than the aperture\n");
		return -E2BIG;
	}

2626
 search_free:
2627
	if (map_and_fenceable)
2628 2629
		free_space =
			drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
2630
						    size, alignment, 0,
2631 2632 2633 2634
						    dev_priv->mm.gtt_mappable_end,
						    0);
	else
		free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2635
						size, alignment, 0);
2636 2637

	if (free_space != NULL) {
2638
		if (map_and_fenceable)
2639
			obj->gtt_space =
2640
				drm_mm_get_block_range_generic(free_space,
2641
							       size, alignment, 0,
2642 2643 2644
							       dev_priv->mm.gtt_mappable_end,
							       0);
		else
2645
			obj->gtt_space =
2646
				drm_mm_get_block(free_space, size, alignment);
2647
	}
2648
	if (obj->gtt_space == NULL) {
2649 2650 2651
		/* If the gtt is empty and we're still having trouble
		 * fitting our object in, we're out of memory.
		 */
2652 2653
		ret = i915_gem_evict_something(dev, size, alignment,
					       map_and_fenceable);
2654
		if (ret)
2655
			return ret;
2656

2657 2658 2659
		goto search_free;
	}

2660
	ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
2661
	if (ret) {
2662 2663
		drm_mm_put_block(obj->gtt_space);
		obj->gtt_space = NULL;
2664 2665 2666

		if (ret == -ENOMEM) {
			/* first try to clear up some space from the GTT */
2667
			ret = i915_gem_evict_something(dev, size,
2668 2669
						       alignment,
						       map_and_fenceable);
2670 2671
			if (ret) {
				/* now try to shrink everyone else */
2672 2673 2674
				if (gfpmask) {
					gfpmask = 0;
					goto search_free;
2675 2676 2677 2678 2679 2680 2681 2682
				}

				return ret;
			}

			goto search_free;
		}

2683 2684 2685
		return ret;
	}

2686 2687
	ret = i915_gem_gtt_bind_object(obj);
	if (ret) {
2688
		i915_gem_object_put_pages_gtt(obj);
2689 2690
		drm_mm_put_block(obj->gtt_space);
		obj->gtt_space = NULL;
2691

2692
		ret = i915_gem_evict_something(dev, size,
2693
					       alignment, map_and_fenceable);
2694
		if (ret)
2695 2696 2697
			return ret;

		goto search_free;
2698 2699
	}

2700
	list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
2701
	list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2702

2703 2704 2705 2706
	/* Assert that the object is not currently in any GPU domain. As it
	 * wasn't in the GTT, there shouldn't be any way it could have been in
	 * a GPU cache
	 */
2707 2708
	BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
	BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2709

2710
	obj->gtt_offset = obj->gtt_space->start;
C
Chris Wilson 已提交
2711

2712
	fenceable =
2713 2714
		obj->gtt_space->size == fence_size &&
		(obj->gtt_space->start & (fence_alignment -1)) == 0;
2715

2716
	mappable =
2717
		obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
2718

2719
	obj->map_and_fenceable = mappable && fenceable;
2720

2721
	trace_i915_gem_object_bind(obj, obj->gtt_offset, map_and_fenceable);
2722 2723 2724 2725
	return 0;
}

void
2726
i915_gem_clflush_object(struct drm_i915_gem_object *obj)
2727 2728 2729 2730 2731
{
	/* If we don't have a page list set up, then we're not pinned
	 * to GPU, and we can ignore the cache flush because it'll happen
	 * again at bind time.
	 */
2732
	if (obj->pages == NULL)
2733 2734
		return;

C
Chris Wilson 已提交
2735
	trace_i915_gem_object_clflush(obj);
2736

2737
	drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
2738 2739
}

2740
/** Flushes any GPU write domain for the object if it's dirty. */
2741 2742
static void
i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
2743
{
2744
	struct drm_device *dev = obj->base.dev;
2745

2746
	if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
2747
		return;
2748 2749

	/* Queue the GPU write cache flushing we need. */
2750 2751
	i915_gem_flush_ring(dev, obj->ring, 0, obj->base.write_domain);
	BUG_ON(obj->base.write_domain);
2752 2753 2754 2755
}

/** Flushes the GTT write domain for the object if it's dirty. */
static void
2756
i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
2757
{
C
Chris Wilson 已提交
2758 2759
	uint32_t old_write_domain;

2760
	if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
2761 2762 2763 2764 2765 2766
		return;

	/* No actual flushing is required for the GTT write domain.   Writes
	 * to it immediately go to main memory as far as we know, so there's
	 * no chipset flush.  It also doesn't land in render cache.
	 */
2767 2768
	i915_gem_release_mmap(obj);

2769 2770
	old_write_domain = obj->base.write_domain;
	obj->base.write_domain = 0;
C
Chris Wilson 已提交
2771 2772

	trace_i915_gem_object_change_domain(obj,
2773
					    obj->base.read_domains,
C
Chris Wilson 已提交
2774
					    old_write_domain);
2775 2776 2777 2778
}

/** Flushes the CPU write domain for the object if it's dirty. */
static void
2779
i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
2780
{
C
Chris Wilson 已提交
2781
	uint32_t old_write_domain;
2782

2783
	if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
2784 2785 2786
		return;

	i915_gem_clflush_object(obj);
2787
	intel_gtt_chipset_flush();
2788 2789
	old_write_domain = obj->base.write_domain;
	obj->base.write_domain = 0;
C
Chris Wilson 已提交
2790 2791

	trace_i915_gem_object_change_domain(obj,
2792
					    obj->base.read_domains,
C
Chris Wilson 已提交
2793
					    old_write_domain);
2794 2795
}

2796 2797 2798 2799 2800 2801
/**
 * Moves a single object to the GTT read, and possibly write domain.
 *
 * This function returns when the move is complete, including waiting on
 * flushes to occur.
 */
J
Jesse Barnes 已提交
2802
int
2803
i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
2804
{
C
Chris Wilson 已提交
2805
	uint32_t old_write_domain, old_read_domains;
2806
	int ret;
2807

2808
	/* Not valid to be called on unbound objects. */
2809
	if (obj->gtt_space == NULL)
2810 2811
		return -EINVAL;

2812
	i915_gem_object_flush_gpu_write_domain(obj);
2813 2814
	ret = i915_gem_object_wait_rendering(obj, true);
	if (ret)
2815 2816
		return ret;

2817
	i915_gem_object_flush_cpu_write_domain(obj);
C
Chris Wilson 已提交
2818

2819 2820
	old_write_domain = obj->base.write_domain;
	old_read_domains = obj->base.read_domains;
C
Chris Wilson 已提交
2821

2822 2823 2824
	/* It should now be out of any other write domains, and we can update
	 * the domain values for our changes.
	 */
2825 2826
	BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
	obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
2827
	if (write) {
2828 2829 2830
		obj->base.read_domains = I915_GEM_DOMAIN_GTT;
		obj->base.write_domain = I915_GEM_DOMAIN_GTT;
		obj->dirty = 1;
2831 2832
	}

C
Chris Wilson 已提交
2833 2834 2835 2836
	trace_i915_gem_object_change_domain(obj,
					    old_read_domains,
					    old_write_domain);

2837 2838 2839
	return 0;
}

2840 2841 2842 2843 2844
/*
 * Prepare buffer for display plane. Use uninterruptible for possible flush
 * wait, as in modesetting process we're not supposed to be interrupted.
 */
int
2845
i915_gem_object_set_to_display_plane(struct drm_i915_gem_object *obj,
2846
				     struct intel_ring_buffer *pipelined)
2847
{
2848
	uint32_t old_read_domains;
2849 2850 2851
	int ret;

	/* Not valid to be called on unbound objects. */
2852
	if (obj->gtt_space == NULL)
2853 2854
		return -EINVAL;

2855
	i915_gem_object_flush_gpu_write_domain(obj);
2856

2857 2858 2859 2860
	/* Currently, we are always called from an non-interruptible context. */
	if (!pipelined) {
		ret = i915_gem_object_wait_rendering(obj, false);
		if (ret)
2861 2862 2863
			return ret;
	}

2864 2865
	i915_gem_object_flush_cpu_write_domain(obj);

2866 2867
	old_read_domains = obj->base.read_domains;
	obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
2868 2869 2870

	trace_i915_gem_object_change_domain(obj,
					    old_read_domains,
2871
					    obj->base.write_domain);
2872 2873 2874 2875

	return 0;
}

2876 2877 2878 2879 2880 2881 2882 2883
int
i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
			  bool interruptible)
{
	if (!obj->active)
		return 0;

	if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
2884
		i915_gem_flush_ring(obj->base.dev, obj->ring,
2885 2886
				    0, obj->base.write_domain);

2887
	return i915_gem_object_wait_rendering(obj, interruptible);
2888 2889
}

2890 2891 2892 2893 2894 2895 2896
/**
 * Moves a single object to the CPU read, and possibly write domain.
 *
 * This function returns when the move is complete, including waiting on
 * flushes to occur.
 */
static int
2897
i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
2898
{
C
Chris Wilson 已提交
2899
	uint32_t old_write_domain, old_read_domains;
2900 2901
	int ret;

2902
	i915_gem_object_flush_gpu_write_domain(obj);
2903 2904
	ret = i915_gem_object_wait_rendering(obj, true);
	if (ret)
2905
		return ret;
2906

2907
	i915_gem_object_flush_gtt_write_domain(obj);
2908

2909 2910
	/* If we have a partially-valid cache of the object in the CPU,
	 * finish invalidating it and free the per-page flags.
2911
	 */
2912
	i915_gem_object_set_to_full_cpu_read_domain(obj);
2913

2914 2915
	old_write_domain = obj->base.write_domain;
	old_read_domains = obj->base.read_domains;
C
Chris Wilson 已提交
2916

2917
	/* Flush the CPU cache if it's still invalid. */
2918
	if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2919 2920
		i915_gem_clflush_object(obj);

2921
		obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
2922 2923 2924 2925 2926
	}

	/* It should now be out of any other write domains, and we can update
	 * the domain values for our changes.
	 */
2927
	BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2928 2929 2930 2931 2932

	/* If we're writing through the CPU, then the GPU read domains will
	 * need to be invalidated at next use.
	 */
	if (write) {
2933 2934
		obj->base.read_domains = I915_GEM_DOMAIN_CPU;
		obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2935
	}
2936

C
Chris Wilson 已提交
2937 2938 2939 2940
	trace_i915_gem_object_change_domain(obj,
					    old_read_domains,
					    old_write_domain);

2941 2942 2943
	return 0;
}

2944
/**
2945
 * Moves the object from a partially CPU read to a full one.
2946
 *
2947 2948
 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2949
 */
2950
static void
2951
i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
2952
{
2953
	if (!obj->page_cpu_valid)
2954 2955 2956 2957
		return;

	/* If we're partially in the CPU read domain, finish moving it in.
	 */
2958
	if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
2959 2960
		int i;

2961 2962
		for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
			if (obj->page_cpu_valid[i])
2963
				continue;
2964
			drm_clflush_pages(obj->pages + i, 1);
2965 2966 2967 2968 2969 2970
		}
	}

	/* Free the page_cpu_valid mappings which are now stale, whether
	 * or not we've got I915_GEM_DOMAIN_CPU.
	 */
2971 2972
	kfree(obj->page_cpu_valid);
	obj->page_cpu_valid = NULL;
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987
}

/**
 * Set the CPU read domain on a range of the object.
 *
 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
 * not entirely valid.  The page_cpu_valid member of the object flags which
 * pages have been flushed, and will be respected by
 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
 * of the whole object.
 *
 * This function returns when the move is complete, including waiting on
 * flushes to occur.
 */
static int
2988
i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
2989 2990
					  uint64_t offset, uint64_t size)
{
C
Chris Wilson 已提交
2991
	uint32_t old_read_domains;
2992
	int i, ret;
2993

2994
	if (offset == 0 && size == obj->base.size)
2995
		return i915_gem_object_set_to_cpu_domain(obj, 0);
2996

2997
	i915_gem_object_flush_gpu_write_domain(obj);
2998 2999
	ret = i915_gem_object_wait_rendering(obj, true);
	if (ret)
3000
		return ret;
3001

3002 3003 3004
	i915_gem_object_flush_gtt_write_domain(obj);

	/* If we're already fully in the CPU read domain, we're done. */
3005 3006
	if (obj->page_cpu_valid == NULL &&
	    (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
3007
		return 0;
3008

3009 3010 3011
	/* Otherwise, create/clear the per-page CPU read domain flag if we're
	 * newly adding I915_GEM_DOMAIN_CPU
	 */
3012 3013 3014 3015
	if (obj->page_cpu_valid == NULL) {
		obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
					      GFP_KERNEL);
		if (obj->page_cpu_valid == NULL)
3016
			return -ENOMEM;
3017 3018
	} else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
		memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
3019 3020 3021 3022

	/* Flush the cache on any pages that are still invalid from the CPU's
	 * perspective.
	 */
3023 3024
	for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
	     i++) {
3025
		if (obj->page_cpu_valid[i])
3026 3027
			continue;

3028
		drm_clflush_pages(obj->pages + i, 1);
3029

3030
		obj->page_cpu_valid[i] = 1;
3031 3032
	}

3033 3034 3035
	/* It should now be out of any other write domains, and we can update
	 * the domain values for our changes.
	 */
3036
	BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3037

3038 3039
	old_read_domains = obj->base.read_domains;
	obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3040

C
Chris Wilson 已提交
3041 3042
	trace_i915_gem_object_change_domain(obj,
					    old_read_domains,
3043
					    obj->base.write_domain);
C
Chris Wilson 已提交
3044

3045 3046 3047 3048 3049 3050
	return 0;
}

/* Throttle our rendering by waiting until the ring has completed our requests
 * emitted over 20 msec ago.
 *
3051 3052 3053 3054
 * Note that if we were to use the current jiffies each time around the loop,
 * we wouldn't escape the function with any frames outstanding if the time to
 * render a frame was over 20ms.
 *
3055 3056 3057
 * This should get us reasonable parallelism between CPU and GPU but also
 * relatively low latency when blocking on a particular request to finish.
 */
3058
static int
3059
i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3060
{
3061 3062
	struct drm_i915_private *dev_priv = dev->dev_private;
	struct drm_i915_file_private *file_priv = file->driver_priv;
3063
	unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3064 3065 3066 3067
	struct drm_i915_gem_request *request;
	struct intel_ring_buffer *ring = NULL;
	u32 seqno = 0;
	int ret;
3068

3069
	spin_lock(&file_priv->mm.lock);
3070
	list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3071 3072
		if (time_after_eq(request->emitted_jiffies, recent_enough))
			break;
3073

3074 3075
		ring = request->ring;
		seqno = request->seqno;
3076
	}
3077
	spin_unlock(&file_priv->mm.lock);
3078

3079 3080
	if (seqno == 0)
		return 0;
3081

3082
	ret = 0;
3083
	if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
3084 3085 3086 3087 3088
		/* And wait for the seqno passing without holding any locks and
		 * causing extra latency for others. This is safe as the irq
		 * generation is designed to be run atomically and so is
		 * lockless.
		 */
3089
		ring->user_irq_get(ring);
3090
		ret = wait_event_interruptible(ring->irq_queue,
3091
					       i915_seqno_passed(ring->get_seqno(ring), seqno)
3092
					       || atomic_read(&dev_priv->mm.wedged));
3093
		ring->user_irq_put(ring);
3094

3095 3096
		if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
			ret = -EIO;
3097 3098
	}

3099 3100
	if (ret == 0)
		queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3101 3102 3103 3104

	return ret;
}

3105
int
3106 3107
i915_gem_object_pin(struct drm_i915_gem_object *obj,
		    uint32_t alignment,
3108
		    bool map_and_fenceable)
3109
{
3110
	struct drm_device *dev = obj->base.dev;
C
Chris Wilson 已提交
3111
	struct drm_i915_private *dev_priv = dev->dev_private;
3112 3113
	int ret;

3114
	BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
3115
	WARN_ON(i915_verify_lists(dev));
3116

3117 3118 3119 3120
	if (obj->gtt_space != NULL) {
		if ((alignment && obj->gtt_offset & (alignment - 1)) ||
		    (map_and_fenceable && !obj->map_and_fenceable)) {
			WARN(obj->pin_count,
3121
			     "bo is already pinned with incorrect alignment:"
3122 3123
			     " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
			     " obj->map_and_fenceable=%d\n",
3124
			     obj->gtt_offset, alignment,
3125
			     map_and_fenceable,
3126
			     obj->map_and_fenceable);
3127 3128 3129 3130 3131 3132
			ret = i915_gem_object_unbind(obj);
			if (ret)
				return ret;
		}
	}

3133
	if (obj->gtt_space == NULL) {
3134
		ret = i915_gem_object_bind_to_gtt(obj, alignment,
3135
						  map_and_fenceable);
3136
		if (ret)
3137
			return ret;
3138
	}
J
Jesse Barnes 已提交
3139

3140 3141 3142
	if (obj->pin_count++ == 0) {
		if (!obj->active)
			list_move_tail(&obj->mm_list,
C
Chris Wilson 已提交
3143
				       &dev_priv->mm.pinned_list);
3144
	}
3145
	obj->pin_mappable |= map_and_fenceable;
3146

3147
	WARN_ON(i915_verify_lists(dev));
3148 3149 3150 3151
	return 0;
}

void
3152
i915_gem_object_unpin(struct drm_i915_gem_object *obj)
3153
{
3154
	struct drm_device *dev = obj->base.dev;
3155 3156
	drm_i915_private_t *dev_priv = dev->dev_private;

3157
	WARN_ON(i915_verify_lists(dev));
3158 3159
	BUG_ON(obj->pin_count == 0);
	BUG_ON(obj->gtt_space == NULL);
3160

3161 3162 3163
	if (--obj->pin_count == 0) {
		if (!obj->active)
			list_move_tail(&obj->mm_list,
3164
				       &dev_priv->mm.inactive_list);
3165
		obj->pin_mappable = false;
3166
	}
3167
	WARN_ON(i915_verify_lists(dev));
3168 3169 3170 3171
}

int
i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3172
		   struct drm_file *file)
3173 3174
{
	struct drm_i915_gem_pin *args = data;
3175
	struct drm_i915_gem_object *obj;
3176 3177
	int ret;

3178 3179 3180
	ret = i915_mutex_lock_interruptible(dev);
	if (ret)
		return ret;
3181

3182
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3183
	if (obj == NULL) {
3184 3185
		ret = -ENOENT;
		goto unlock;
3186 3187
	}

3188
	if (obj->madv != I915_MADV_WILLNEED) {
C
Chris Wilson 已提交
3189
		DRM_ERROR("Attempting to pin a purgeable buffer\n");
3190 3191
		ret = -EINVAL;
		goto out;
3192 3193
	}

3194
	if (obj->pin_filp != NULL && obj->pin_filp != file) {
J
Jesse Barnes 已提交
3195 3196
		DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
			  args->handle);
3197 3198
		ret = -EINVAL;
		goto out;
J
Jesse Barnes 已提交
3199 3200
	}

3201 3202 3203
	obj->user_pin_count++;
	obj->pin_filp = file;
	if (obj->user_pin_count == 1) {
3204
		ret = i915_gem_object_pin(obj, args->alignment, true);
3205 3206
		if (ret)
			goto out;
3207 3208 3209 3210 3211
	}

	/* XXX - flush the CPU caches for pinned objects
	 * as the X server doesn't manage domains yet
	 */
3212
	i915_gem_object_flush_cpu_write_domain(obj);
3213
	args->offset = obj->gtt_offset;
3214
out:
3215
	drm_gem_object_unreference(&obj->base);
3216
unlock:
3217
	mutex_unlock(&dev->struct_mutex);
3218
	return ret;
3219 3220 3221 3222
}

int
i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3223
		     struct drm_file *file)
3224 3225
{
	struct drm_i915_gem_pin *args = data;
3226
	struct drm_i915_gem_object *obj;
3227
	int ret;
3228

3229 3230 3231
	ret = i915_mutex_lock_interruptible(dev);
	if (ret)
		return ret;
3232

3233
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3234
	if (obj == NULL) {
3235 3236
		ret = -ENOENT;
		goto unlock;
3237
	}
3238

3239
	if (obj->pin_filp != file) {
J
Jesse Barnes 已提交
3240 3241
		DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
			  args->handle);
3242 3243
		ret = -EINVAL;
		goto out;
J
Jesse Barnes 已提交
3244
	}
3245 3246 3247
	obj->user_pin_count--;
	if (obj->user_pin_count == 0) {
		obj->pin_filp = NULL;
J
Jesse Barnes 已提交
3248 3249
		i915_gem_object_unpin(obj);
	}
3250

3251
out:
3252
	drm_gem_object_unreference(&obj->base);
3253
unlock:
3254
	mutex_unlock(&dev->struct_mutex);
3255
	return ret;
3256 3257 3258 3259
}

int
i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3260
		    struct drm_file *file)
3261 3262
{
	struct drm_i915_gem_busy *args = data;
3263
	struct drm_i915_gem_object *obj;
3264 3265
	int ret;

3266
	ret = i915_mutex_lock_interruptible(dev);
3267
	if (ret)
3268
		return ret;
3269

3270
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3271
	if (obj == NULL) {
3272 3273
		ret = -ENOENT;
		goto unlock;
3274
	}
3275

3276 3277 3278 3279
	/* Count all active objects as busy, even if they are currently not used
	 * by the gpu. Users of this interface expect objects to eventually
	 * become non-busy without any further actions, therefore emit any
	 * necessary flushes here.
3280
	 */
3281
	args->busy = obj->active;
3282 3283 3284 3285 3286 3287
	if (args->busy) {
		/* Unconditionally flush objects, even when the gpu still uses this
		 * object. Userspace calling this function indicates that it wants to
		 * use this buffer rather sooner than later, so issuing the required
		 * flush earlier is beneficial.
		 */
3288 3289 3290
		if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
			i915_gem_flush_ring(dev, obj->ring,
					    0, obj->base.write_domain);
3291 3292 3293 3294 3295 3296

		/* Update the active list for the hardware's current position.
		 * Otherwise this only updates on a delayed timer or when irqs
		 * are actually unmasked, and our working set ends up being
		 * larger than required.
		 */
3297
		i915_gem_retire_requests_ring(dev, obj->ring);
3298

3299
		args->busy = obj->active;
3300
	}
3301

3302
	drm_gem_object_unreference(&obj->base);
3303
unlock:
3304
	mutex_unlock(&dev->struct_mutex);
3305
	return ret;
3306 3307 3308 3309 3310 3311 3312 3313 3314
}

int
i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
			struct drm_file *file_priv)
{
    return i915_gem_ring_throttle(dev, file_priv);
}

3315 3316 3317 3318 3319
int
i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
		       struct drm_file *file_priv)
{
	struct drm_i915_gem_madvise *args = data;
3320
	struct drm_i915_gem_object *obj;
3321
	int ret;
3322 3323 3324 3325 3326 3327 3328 3329 3330

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

3331 3332 3333 3334
	ret = i915_mutex_lock_interruptible(dev);
	if (ret)
		return ret;

3335
	obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
3336
	if (obj == NULL) {
3337 3338
		ret = -ENOENT;
		goto unlock;
3339 3340
	}

3341
	if (obj->pin_count) {
3342 3343
		ret = -EINVAL;
		goto out;
3344 3345
	}

3346 3347
	if (obj->madv != __I915_MADV_PURGED)
		obj->madv = args->madv;
3348

3349
	/* if the object is no longer bound, discard its backing storage */
3350 3351
	if (i915_gem_object_is_purgeable(obj) &&
	    obj->gtt_space == NULL)
3352 3353
		i915_gem_object_truncate(obj);

3354
	args->retained = obj->madv != __I915_MADV_PURGED;
C
Chris Wilson 已提交
3355

3356
out:
3357
	drm_gem_object_unreference(&obj->base);
3358
unlock:
3359
	mutex_unlock(&dev->struct_mutex);
3360
	return ret;
3361 3362
}

3363 3364
struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
						  size_t size)
3365
{
3366
	struct drm_i915_private *dev_priv = dev->dev_private;
3367
	struct drm_i915_gem_object *obj;
3368

3369 3370 3371
	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
	if (obj == NULL)
		return NULL;
3372

3373 3374 3375 3376
	if (drm_gem_object_init(dev, &obj->base, size) != 0) {
		kfree(obj);
		return NULL;
	}
3377

3378 3379
	i915_gem_info_add_obj(dev_priv, size);

3380 3381
	obj->base.write_domain = I915_GEM_DOMAIN_CPU;
	obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3382

3383
	obj->agp_type = AGP_USER_MEMORY;
3384
	obj->base.driver_private = NULL;
3385
	obj->fence_reg = I915_FENCE_REG_NONE;
3386
	INIT_LIST_HEAD(&obj->mm_list);
D
Daniel Vetter 已提交
3387
	INIT_LIST_HEAD(&obj->gtt_list);
3388
	INIT_LIST_HEAD(&obj->ring_list);
3389
	INIT_LIST_HEAD(&obj->exec_list);
3390 3391
	INIT_LIST_HEAD(&obj->gpu_write_list);
	obj->madv = I915_MADV_WILLNEED;
3392 3393
	/* Avoid an unnecessary call to unbind on the first bind. */
	obj->map_and_fenceable = true;
3394

3395
	return obj;
3396 3397 3398 3399 3400
}

int i915_gem_init_object(struct drm_gem_object *obj)
{
	BUG();
3401

3402 3403 3404
	return 0;
}

3405
static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
3406
{
3407
	struct drm_device *dev = obj->base.dev;
3408 3409
	drm_i915_private_t *dev_priv = dev->dev_private;
	int ret;
3410

3411 3412
	ret = i915_gem_object_unbind(obj);
	if (ret == -ERESTARTSYS) {
3413
		list_move(&obj->mm_list,
3414 3415 3416
			  &dev_priv->mm.deferred_free_list);
		return;
	}
3417

3418
	if (obj->base.map_list.map)
3419
		i915_gem_free_mmap_offset(obj);
3420

3421 3422
	drm_gem_object_release(&obj->base);
	i915_gem_info_remove_obj(dev_priv, obj->base.size);
3423

3424 3425 3426
	kfree(obj->page_cpu_valid);
	kfree(obj->bit_17);
	kfree(obj);
3427 3428
}

3429
void i915_gem_free_object(struct drm_gem_object *gem_obj)
3430
{
3431 3432
	struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
	struct drm_device *dev = obj->base.dev;
3433 3434 3435

	trace_i915_gem_object_destroy(obj);

3436
	while (obj->pin_count > 0)
3437 3438
		i915_gem_object_unpin(obj);

3439
	if (obj->phys_obj)
3440 3441 3442 3443 3444
		i915_gem_detach_phys_object(dev, obj);

	i915_gem_free_object_tail(obj);
}

3445 3446 3447 3448 3449
int
i915_gem_idle(struct drm_device *dev)
{
	drm_i915_private_t *dev_priv = dev->dev_private;
	int ret;
3450

3451
	mutex_lock(&dev->struct_mutex);
C
Chris Wilson 已提交
3452

3453
	if (dev_priv->mm.suspended) {
3454 3455
		mutex_unlock(&dev->struct_mutex);
		return 0;
3456 3457
	}

3458
	ret = i915_gpu_idle(dev);
3459 3460
	if (ret) {
		mutex_unlock(&dev->struct_mutex);
3461
		return ret;
3462
	}
3463

3464 3465
	/* Under UMS, be paranoid and evict. */
	if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
3466
		ret = i915_gem_evict_inactive(dev, false);
3467 3468 3469 3470 3471 3472
		if (ret) {
			mutex_unlock(&dev->struct_mutex);
			return ret;
		}
	}

3473 3474
	i915_gem_reset_fences(dev);

3475 3476 3477 3478 3479
	/* Hack!  Don't let anybody do execbuf while we don't control the chip.
	 * We need to replace this with a semaphore, or something.
	 * And not confound mm.suspended!
	 */
	dev_priv->mm.suspended = 1;
3480
	del_timer_sync(&dev_priv->hangcheck_timer);
3481 3482

	i915_kernel_lost_context(dev);
3483
	i915_gem_cleanup_ringbuffer(dev);
3484

3485 3486
	mutex_unlock(&dev->struct_mutex);

3487 3488 3489
	/* Cancel the retire work handler, which should be idle now. */
	cancel_delayed_work_sync(&dev_priv->mm.retire_work);

3490 3491 3492
	return 0;
}

3493 3494 3495 3496 3497
int
i915_gem_init_ringbuffer(struct drm_device *dev)
{
	drm_i915_private_t *dev_priv = dev->dev_private;
	int ret;
3498

3499
	ret = intel_init_render_ring_buffer(dev);
3500
	if (ret)
3501
		return ret;
3502 3503

	if (HAS_BSD(dev)) {
3504
		ret = intel_init_bsd_ring_buffer(dev);
3505 3506
		if (ret)
			goto cleanup_render_ring;
3507
	}
3508

3509 3510 3511 3512 3513 3514
	if (HAS_BLT(dev)) {
		ret = intel_init_blt_ring_buffer(dev);
		if (ret)
			goto cleanup_bsd_ring;
	}

3515 3516
	dev_priv->next_seqno = 1;

3517 3518
	return 0;

3519
cleanup_bsd_ring:
3520
	intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
3521
cleanup_render_ring:
3522
	intel_cleanup_ring_buffer(&dev_priv->render_ring);
3523 3524 3525 3526 3527 3528 3529 3530
	return ret;
}

void
i915_gem_cleanup_ringbuffer(struct drm_device *dev)
{
	drm_i915_private_t *dev_priv = dev->dev_private;

3531 3532 3533
	intel_cleanup_ring_buffer(&dev_priv->render_ring);
	intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
	intel_cleanup_ring_buffer(&dev_priv->blt_ring);
3534 3535
}

3536 3537 3538 3539 3540 3541 3542
int
i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
		       struct drm_file *file_priv)
{
	drm_i915_private_t *dev_priv = dev->dev_private;
	int ret;

J
Jesse Barnes 已提交
3543 3544 3545
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return 0;

3546
	if (atomic_read(&dev_priv->mm.wedged)) {
3547
		DRM_ERROR("Reenabling wedged hardware, good luck\n");
3548
		atomic_set(&dev_priv->mm.wedged, 0);
3549 3550 3551
	}

	mutex_lock(&dev->struct_mutex);
3552 3553 3554
	dev_priv->mm.suspended = 0;

	ret = i915_gem_init_ringbuffer(dev);
3555 3556
	if (ret != 0) {
		mutex_unlock(&dev->struct_mutex);
3557
		return ret;
3558
	}
3559

3560
	BUG_ON(!list_empty(&dev_priv->mm.active_list));
3561
	BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
3562
	BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
3563
	BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
3564 3565
	BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
	BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3566
	BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
3567
	BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
3568
	BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
3569
	mutex_unlock(&dev->struct_mutex);
3570

3571 3572 3573
	ret = drm_irq_install(dev);
	if (ret)
		goto cleanup_ringbuffer;
3574

3575
	return 0;
3576 3577 3578 3579 3580 3581 3582 3583

cleanup_ringbuffer:
	mutex_lock(&dev->struct_mutex);
	i915_gem_cleanup_ringbuffer(dev);
	dev_priv->mm.suspended = 1;
	mutex_unlock(&dev->struct_mutex);

	return ret;
3584 3585 3586 3587 3588 3589
}

int
i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
		       struct drm_file *file_priv)
{
J
Jesse Barnes 已提交
3590 3591 3592
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return 0;

3593
	drm_irq_uninstall(dev);
3594
	return i915_gem_idle(dev);
3595 3596 3597 3598 3599 3600 3601
}

void
i915_gem_lastclose(struct drm_device *dev)
{
	int ret;

3602 3603 3604
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return;

3605 3606 3607
	ret = i915_gem_idle(dev);
	if (ret)
		DRM_ERROR("failed to idle hardware: %d\n", ret);
3608 3609
}

3610 3611 3612 3613 3614 3615 3616 3617
static void
init_ring_lists(struct intel_ring_buffer *ring)
{
	INIT_LIST_HEAD(&ring->active_list);
	INIT_LIST_HEAD(&ring->request_list);
	INIT_LIST_HEAD(&ring->gpu_write_list);
}

3618 3619 3620
void
i915_gem_load(struct drm_device *dev)
{
3621
	int i;
3622 3623
	drm_i915_private_t *dev_priv = dev->dev_private;

3624
	INIT_LIST_HEAD(&dev_priv->mm.active_list);
3625 3626
	INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
	INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
C
Chris Wilson 已提交
3627
	INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
3628
	INIT_LIST_HEAD(&dev_priv->mm.fence_list);
3629
	INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
D
Daniel Vetter 已提交
3630
	INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
3631 3632 3633
	init_ring_lists(&dev_priv->render_ring);
	init_ring_lists(&dev_priv->bsd_ring);
	init_ring_lists(&dev_priv->blt_ring);
3634 3635
	for (i = 0; i < 16; i++)
		INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
3636 3637
	INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
			  i915_gem_retire_work_handler);
3638
	init_completion(&dev_priv->error_completion);
3639

3640 3641 3642 3643 3644 3645 3646 3647 3648 3649
	/* On GEN3 we really need to make sure the ARB C3 LP bit is set */
	if (IS_GEN3(dev)) {
		u32 tmp = I915_READ(MI_ARB_STATE);
		if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
			/* arb state is a masked write, so set bit + bit in mask */
			tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
			I915_WRITE(MI_ARB_STATE, tmp);
		}
	}

3650
	/* Old X drivers will take 0-2 for front, back, depth buffers */
3651 3652
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
		dev_priv->fence_reg_start = 3;
3653

3654
	if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3655 3656 3657 3658
		dev_priv->num_fence_regs = 16;
	else
		dev_priv->num_fence_regs = 8;

3659
	/* Initialize fence registers to zero */
3660 3661 3662 3663 3664 3665 3666
	switch (INTEL_INFO(dev)->gen) {
	case 6:
		for (i = 0; i < 16; i++)
			I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
		break;
	case 5:
	case 4:
3667 3668
		for (i = 0; i < 16; i++)
			I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
3669 3670
		break;
	case 3:
3671 3672 3673
		if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
			for (i = 0; i < 8; i++)
				I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
3674 3675 3676 3677
	case 2:
		for (i = 0; i < 8; i++)
			I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
		break;
3678
	}
3679
	i915_gem_detect_bit_6_swizzle(dev);
3680
	init_waitqueue_head(&dev_priv->pending_flip_queue);
3681 3682 3683 3684

	dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
	dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
	register_shrinker(&dev_priv->mm.inactive_shrinker);
3685
}
3686 3687 3688 3689 3690

/*
 * Create a physically contiguous memory object for this object
 * e.g. for cursor + overlay regs
 */
3691 3692
static int i915_gem_init_phys_object(struct drm_device *dev,
				     int id, int size, int align)
3693 3694 3695 3696 3697 3698 3699 3700
{
	drm_i915_private_t *dev_priv = dev->dev_private;
	struct drm_i915_gem_phys_object *phys_obj;
	int ret;

	if (dev_priv->mm.phys_objs[id - 1] || !size)
		return 0;

3701
	phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
3702 3703 3704 3705 3706
	if (!phys_obj)
		return -ENOMEM;

	phys_obj->id = id;

3707
	phys_obj->handle = drm_pci_alloc(dev, size, align);
3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719
	if (!phys_obj->handle) {
		ret = -ENOMEM;
		goto kfree_obj;
	}
#ifdef CONFIG_X86
	set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
#endif

	dev_priv->mm.phys_objs[id - 1] = phys_obj;

	return 0;
kfree_obj:
3720
	kfree(phys_obj);
3721 3722 3723
	return ret;
}

3724
static void i915_gem_free_phys_object(struct drm_device *dev, int id)
3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748
{
	drm_i915_private_t *dev_priv = dev->dev_private;
	struct drm_i915_gem_phys_object *phys_obj;

	if (!dev_priv->mm.phys_objs[id - 1])
		return;

	phys_obj = dev_priv->mm.phys_objs[id - 1];
	if (phys_obj->cur_obj) {
		i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
	}

#ifdef CONFIG_X86
	set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
#endif
	drm_pci_free(dev, phys_obj->handle);
	kfree(phys_obj);
	dev_priv->mm.phys_objs[id - 1] = NULL;
}

void i915_gem_free_all_phys_object(struct drm_device *dev)
{
	int i;

3749
	for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
3750 3751 3752 3753
		i915_gem_free_phys_object(dev, i);
}

void i915_gem_detach_phys_object(struct drm_device *dev,
3754
				 struct drm_i915_gem_object *obj)
3755
{
3756
	struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3757
	char *vaddr;
3758 3759 3760
	int i;
	int page_count;

3761
	if (!obj->phys_obj)
3762
		return;
3763
	vaddr = obj->phys_obj->handle->vaddr;
3764

3765
	page_count = obj->base.size / PAGE_SIZE;
3766
	for (i = 0; i < page_count; i++) {
3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779
		struct page *page = read_cache_page_gfp(mapping, i,
							GFP_HIGHUSER | __GFP_RECLAIMABLE);
		if (!IS_ERR(page)) {
			char *dst = kmap_atomic(page);
			memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
			kunmap_atomic(dst);

			drm_clflush_pages(&page, 1);

			set_page_dirty(page);
			mark_page_accessed(page);
			page_cache_release(page);
		}
3780
	}
3781
	intel_gtt_chipset_flush();
3782

3783 3784
	obj->phys_obj->cur_obj = NULL;
	obj->phys_obj = NULL;
3785 3786 3787 3788
}

int
i915_gem_attach_phys_object(struct drm_device *dev,
3789
			    struct drm_i915_gem_object *obj,
3790 3791
			    int id,
			    int align)
3792
{
3793
	struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3794 3795 3796 3797 3798 3799 3800 3801
	drm_i915_private_t *dev_priv = dev->dev_private;
	int ret = 0;
	int page_count;
	int i;

	if (id > I915_MAX_PHYS_OBJECT)
		return -EINVAL;

3802 3803
	if (obj->phys_obj) {
		if (obj->phys_obj->id == id)
3804 3805 3806 3807 3808 3809 3810
			return 0;
		i915_gem_detach_phys_object(dev, obj);
	}

	/* create a new object */
	if (!dev_priv->mm.phys_objs[id - 1]) {
		ret = i915_gem_init_phys_object(dev, id,
3811
						obj->base.size, align);
3812
		if (ret) {
3813 3814
			DRM_ERROR("failed to init phys object %d size: %zu\n",
				  id, obj->base.size);
3815
			return ret;
3816 3817 3818 3819
		}
	}

	/* bind to the object */
3820 3821
	obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
	obj->phys_obj->cur_obj = obj;
3822

3823
	page_count = obj->base.size / PAGE_SIZE;
3824 3825

	for (i = 0; i < page_count; i++) {
3826 3827 3828 3829 3830 3831 3832
		struct page *page;
		char *dst, *src;

		page = read_cache_page_gfp(mapping, i,
					   GFP_HIGHUSER | __GFP_RECLAIMABLE);
		if (IS_ERR(page))
			return PTR_ERR(page);
3833

3834
		src = kmap_atomic(page);
3835
		dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
3836
		memcpy(dst, src, PAGE_SIZE);
P
Peter Zijlstra 已提交
3837
		kunmap_atomic(src);
3838

3839 3840 3841
		mark_page_accessed(page);
		page_cache_release(page);
	}
3842

3843 3844 3845 3846
	return 0;
}

static int
3847 3848
i915_gem_phys_pwrite(struct drm_device *dev,
		     struct drm_i915_gem_object *obj,
3849 3850 3851
		     struct drm_i915_gem_pwrite *args,
		     struct drm_file *file_priv)
{
3852
	void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
3853
	char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
3854

3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867
	if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
		unsigned long unwritten;

		/* The physical object once assigned is fixed for the lifetime
		 * of the obj, so we can safely drop the lock and continue
		 * to access vaddr.
		 */
		mutex_unlock(&dev->struct_mutex);
		unwritten = copy_from_user(vaddr, user_data, args->size);
		mutex_lock(&dev->struct_mutex);
		if (unwritten)
			return -EFAULT;
	}
3868

3869
	intel_gtt_chipset_flush();
3870 3871
	return 0;
}
3872

3873
void i915_gem_release(struct drm_device *dev, struct drm_file *file)
3874
{
3875
	struct drm_i915_file_private *file_priv = file->driver_priv;
3876 3877 3878 3879 3880

	/* 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.
	 */
3881
	spin_lock(&file_priv->mm.lock);
3882 3883 3884 3885 3886 3887 3888 3889 3890
	while (!list_empty(&file_priv->mm.request_list)) {
		struct drm_i915_gem_request *request;

		request = list_first_entry(&file_priv->mm.request_list,
					   struct drm_i915_gem_request,
					   client_list);
		list_del(&request->client_list);
		request->file_priv = NULL;
	}
3891
	spin_unlock(&file_priv->mm.lock);
3892
}
3893

3894 3895 3896 3897 3898 3899 3900
static int
i915_gpu_is_active(struct drm_device *dev)
{
	drm_i915_private_t *dev_priv = dev->dev_private;
	int lists_empty;

	lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
3901
		      list_empty(&dev_priv->mm.active_list);
3902 3903 3904 3905

	return !lists_empty;
}

3906
static int
3907 3908 3909
i915_gem_inactive_shrink(struct shrinker *shrinker,
			 int nr_to_scan,
			 gfp_t gfp_mask)
3910
{
3911 3912 3913 3914 3915 3916 3917 3918 3919
	struct drm_i915_private *dev_priv =
		container_of(shrinker,
			     struct drm_i915_private,
			     mm.inactive_shrinker);
	struct drm_device *dev = dev_priv->dev;
	struct drm_i915_gem_object *obj, *next;
	int cnt;

	if (!mutex_trylock(&dev->struct_mutex))
3920
		return 0;
3921 3922 3923

	/* "fast-path" to count number of available objects */
	if (nr_to_scan == 0) {
3924 3925 3926 3927 3928 3929 3930
		cnt = 0;
		list_for_each_entry(obj,
				    &dev_priv->mm.inactive_list,
				    mm_list)
			cnt++;
		mutex_unlock(&dev->struct_mutex);
		return cnt / 100 * sysctl_vfs_cache_pressure;
3931 3932
	}

3933
rescan:
3934
	/* first scan for clean buffers */
3935
	i915_gem_retire_requests(dev);
3936

3937 3938 3939 3940
	list_for_each_entry_safe(obj, next,
				 &dev_priv->mm.inactive_list,
				 mm_list) {
		if (i915_gem_object_is_purgeable(obj)) {
3941 3942
			if (i915_gem_object_unbind(obj) == 0 &&
			    --nr_to_scan == 0)
3943
				break;
3944 3945 3946 3947
		}
	}

	/* second pass, evict/count anything still on the inactive list */
3948 3949 3950 3951
	cnt = 0;
	list_for_each_entry_safe(obj, next,
				 &dev_priv->mm.inactive_list,
				 mm_list) {
3952 3953
		if (nr_to_scan &&
		    i915_gem_object_unbind(obj) == 0)
3954
			nr_to_scan--;
3955
		else
3956 3957 3958 3959
			cnt++;
	}

	if (nr_to_scan && i915_gpu_is_active(dev)) {
3960 3961 3962 3963 3964 3965
		/*
		 * We are desperate for pages, so as a last resort, wait
		 * for the GPU to finish and discard whatever we can.
		 * This has a dramatic impact to reduce the number of
		 * OOM-killer events whilst running the GPU aggressively.
		 */
3966
		if (i915_gpu_idle(dev) == 0)
3967 3968
			goto rescan;
	}
3969 3970
	mutex_unlock(&dev->struct_mutex);
	return cnt / 100 * sysctl_vfs_cache_pressure;
3971
}