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

28
#include <drm/drmP.h>
29
#include <drm/drm_vma_manager.h>
30
#include <drm/i915_drm.h>
31
#include "i915_drv.h"
32
#include "i915_vgpu.h"
Chris Wilson's avatar
Chris Wilson committed
33
#include "i915_trace.h"
34
#include "intel_drv.h"
35
#include "intel_frontbuffer.h"
36
#include "intel_mocs.h"
37
#include <linux/dma-fence-array.h>
38
#include <linux/reservation.h>
39
#include <linux/shmem_fs.h>
40
#include <linux/slab.h>
41
#include <linux/stop_machine.h>
42
#include <linux/swap.h>
43
#include <linux/pci.h>
44
#include <linux/dma-buf.h>
45

46
static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
47
static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
48
static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
49

50 51 52
static bool cpu_cache_is_coherent(struct drm_device *dev,
				  enum i915_cache_level level)
{
53
	return HAS_LLC(to_i915(dev)) || level != I915_CACHE_NONE;
54 55
}

56 57
static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
{
58 59 60
	if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
		return false;

61 62 63 64 65 66
	if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
		return true;

	return obj->pin_display;
}

67
static int
68
insert_mappable_node(struct i915_ggtt *ggtt,
69 70 71
                     struct drm_mm_node *node, u32 size)
{
	memset(node, 0, sizeof(*node));
72
	return drm_mm_insert_node_in_range_generic(&ggtt->base.mm, node,
73 74
						   size, 0,
						   I915_COLOR_UNEVICTABLE,
75
						   0, ggtt->mappable_end,
76 77 78 79 80 81 82 83 84 85
						   DRM_MM_SEARCH_DEFAULT,
						   DRM_MM_CREATE_DEFAULT);
}

static void
remove_mappable_node(struct drm_mm_node *node)
{
	drm_mm_remove_node(node);
}

86 87
/* some bookkeeping */
static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
88
				  u64 size)
89
{
90
	spin_lock(&dev_priv->mm.object_stat_lock);
91 92
	dev_priv->mm.object_count++;
	dev_priv->mm.object_memory += size;
93
	spin_unlock(&dev_priv->mm.object_stat_lock);
94 95 96
}

static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
97
				     u64 size)
98
{
99
	spin_lock(&dev_priv->mm.object_stat_lock);
100 101
	dev_priv->mm.object_count--;
	dev_priv->mm.object_memory -= size;
102
	spin_unlock(&dev_priv->mm.object_stat_lock);
103 104
}

105
static int
106
i915_gem_wait_for_error(struct i915_gpu_error *error)
107 108 109
{
	int ret;

110 111
	might_sleep();

112
	if (!i915_reset_in_progress(error))
113 114
		return 0;

115 116 117 118 119
	/*
	 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
	 * userspace. If it takes that long something really bad is going on and
	 * we should simply try to bail out and fail as gracefully as possible.
	 */
120
	ret = wait_event_interruptible_timeout(error->reset_queue,
121
					       !i915_reset_in_progress(error),
122
					       I915_RESET_TIMEOUT);
123 124 125 126
	if (ret == 0) {
		DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
		return -EIO;
	} else if (ret < 0) {
127
		return ret;
128 129
	} else {
		return 0;
130
	}
131 132
}

133
int i915_mutex_lock_interruptible(struct drm_device *dev)
134
{
135
	struct drm_i915_private *dev_priv = to_i915(dev);
136 137
	int ret;

138
	ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
139 140 141 142 143 144 145 146 147
	if (ret)
		return ret;

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

	return 0;
}
148

149 150
int
i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
151
			    struct drm_file *file)
152
{
153
	struct drm_i915_private *dev_priv = to_i915(dev);
154
	struct i915_ggtt *ggtt = &dev_priv->ggtt;
155
	struct drm_i915_gem_get_aperture *args = data;
156
	struct i915_vma *vma;
157
	size_t pinned;
158

159
	pinned = 0;
160
	mutex_lock(&dev->struct_mutex);
161
	list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
162
		if (i915_vma_is_pinned(vma))
163
			pinned += vma->node.size;
164
	list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
165
		if (i915_vma_is_pinned(vma))
166
			pinned += vma->node.size;
167
	mutex_unlock(&dev->struct_mutex);
168

169
	args->aper_size = ggtt->base.total;
170
	args->aper_available_size = args->aper_size - pinned;
171

172 173 174
	return 0;
}

175
static struct sg_table *
176
i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
177
{
178
	struct address_space *mapping = obj->base.filp->f_mapping;
179 180 181 182
	char *vaddr = obj->phys_handle->vaddr;
	struct sg_table *st;
	struct scatterlist *sg;
	int i;
183

184
	if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
185
		return ERR_PTR(-EINVAL);
186 187 188 189 190 191 192

	for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
		struct page *page;
		char *src;

		page = shmem_read_mapping_page(mapping, i);
		if (IS_ERR(page))
193
			return ERR_CAST(page);
194 195 196 197 198 199

		src = kmap_atomic(page);
		memcpy(vaddr, src, PAGE_SIZE);
		drm_clflush_virt_range(vaddr, PAGE_SIZE);
		kunmap_atomic(src);

200
		put_page(page);
201 202 203
		vaddr += PAGE_SIZE;
	}

204
	i915_gem_chipset_flush(to_i915(obj->base.dev));
205 206 207

	st = kmalloc(sizeof(*st), GFP_KERNEL);
	if (st == NULL)
208
		return ERR_PTR(-ENOMEM);
209 210 211

	if (sg_alloc_table(st, 1, GFP_KERNEL)) {
		kfree(st);
212
		return ERR_PTR(-ENOMEM);
213 214 215 216 217
	}

	sg = st->sgl;
	sg->offset = 0;
	sg->length = obj->base.size;
218

219 220 221
	sg_dma_address(sg) = obj->phys_handle->busaddr;
	sg_dma_len(sg) = obj->base.size;

222
	return st;
223 224 225
}

static void
226 227
__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
				struct sg_table *pages)
228
{
229
	GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
230

231 232
	if (obj->mm.madv == I915_MADV_DONTNEED)
		obj->mm.dirty = false;
233

234 235
	if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
	    !cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
236
		drm_clflush_sg(pages);
237 238 239 240 241 242 243 244 245

	obj->base.read_domains = I915_GEM_DOMAIN_CPU;
	obj->base.write_domain = I915_GEM_DOMAIN_CPU;
}

static void
i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
			       struct sg_table *pages)
{
246
	__i915_gem_object_release_shmem(obj, pages);
247

248
	if (obj->mm.dirty) {
249
		struct address_space *mapping = obj->base.filp->f_mapping;
250
		char *vaddr = obj->phys_handle->vaddr;
251 252 253
		int i;

		for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
254 255 256 257 258 259 260 261 262 263 264 265 266
			struct page *page;
			char *dst;

			page = shmem_read_mapping_page(mapping, i);
			if (IS_ERR(page))
				continue;

			dst = kmap_atomic(page);
			drm_clflush_virt_range(vaddr, PAGE_SIZE);
			memcpy(dst, vaddr, PAGE_SIZE);
			kunmap_atomic(dst);

			set_page_dirty(page);
267
			if (obj->mm.madv == I915_MADV_WILLNEED)
268
				mark_page_accessed(page);
269
			put_page(page);
270 271
			vaddr += PAGE_SIZE;
		}
272
		obj->mm.dirty = false;
273 274
	}

275 276
	sg_free_table(pages);
	kfree(pages);
277 278 279 280 281 282
}

static void
i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
{
	drm_pci_free(obj->base.dev, obj->phys_handle);
283
	i915_gem_object_unpin_pages(obj);
284 285 286 287 288 289 290 291
}

static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
	.get_pages = i915_gem_object_get_pages_phys,
	.put_pages = i915_gem_object_put_pages_phys,
	.release = i915_gem_object_release_phys,
};

292
int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
293 294 295
{
	struct i915_vma *vma;
	LIST_HEAD(still_in_list);
296 297 298
	int ret;

	lockdep_assert_held(&obj->base.dev->struct_mutex);
299

300 301 302 303
	/* Closed vma are removed from the obj->vma_list - but they may
	 * still have an active binding on the object. To remove those we
	 * must wait for all rendering to complete to the object (as unbinding
	 * must anyway), and retire the requests.
304
	 */
305 306 307 308 309 310
	ret = i915_gem_object_wait(obj,
				   I915_WAIT_INTERRUPTIBLE |
				   I915_WAIT_LOCKED |
				   I915_WAIT_ALL,
				   MAX_SCHEDULE_TIMEOUT,
				   NULL);
311 312 313 314 315
	if (ret)
		return ret;

	i915_gem_retire_requests(to_i915(obj->base.dev));

316 317 318 319 320 321 322 323 324 325 326 327 328
	while ((vma = list_first_entry_or_null(&obj->vma_list,
					       struct i915_vma,
					       obj_link))) {
		list_move_tail(&vma->obj_link, &still_in_list);
		ret = i915_vma_unbind(vma);
		if (ret)
			break;
	}
	list_splice(&still_in_list, &obj->vma_list);

	return ret;
}

329 330 331 332 333
static long
i915_gem_object_wait_fence(struct dma_fence *fence,
			   unsigned int flags,
			   long timeout,
			   struct intel_rps_client *rps)
334
{
335
	struct drm_i915_gem_request *rq;
336

337
	BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
338

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
		return timeout;

	if (!dma_fence_is_i915(fence))
		return dma_fence_wait_timeout(fence,
					      flags & I915_WAIT_INTERRUPTIBLE,
					      timeout);

	rq = to_request(fence);
	if (i915_gem_request_completed(rq))
		goto out;

	/* This client is about to stall waiting for the GPU. In many cases
	 * this is undesirable and limits the throughput of the system, as
	 * many clients cannot continue processing user input/output whilst
	 * blocked. RPS autotuning may take tens of milliseconds to respond
	 * to the GPU load and thus incurs additional latency for the client.
	 * We can circumvent that by promoting the GPU frequency to maximum
	 * before we wait. This makes the GPU throttle up much more quickly
	 * (good for benchmarks and user experience, e.g. window animations),
	 * but at a cost of spending more power processing the workload
	 * (bad for battery). Not all clients even want their results
	 * immediately and for them we should just let the GPU select its own
	 * frequency to maximise efficiency. To prevent a single client from
	 * forcing the clocks too high for the whole system, we only allow
	 * each client to waitboost once in a busy period.
	 */
	if (rps) {
		if (INTEL_GEN(rq->i915) >= 6)
			gen6_rps_boost(rq->i915, rps, rq->emitted_jiffies);
		else
			rps = NULL;
371 372
	}

373 374 375 376 377 378
	timeout = i915_wait_request(rq, flags, timeout);

out:
	if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
		i915_gem_request_retire_upto(rq);

379
	if (rps && rq->global_seqno == intel_engine_last_submit(rq->engine)) {
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
		/* The GPU is now idle and this client has stalled.
		 * Since no other client has submitted a request in the
		 * meantime, assume that this client is the only one
		 * supplying work to the GPU but is unable to keep that
		 * work supplied because it is waiting. Since the GPU is
		 * then never kept fully busy, RPS autoclocking will
		 * keep the clocks relatively low, causing further delays.
		 * Compensate by giving the synchronous client credit for
		 * a waitboost next time.
		 */
		spin_lock(&rq->i915->rps.client_lock);
		list_del_init(&rps->link);
		spin_unlock(&rq->i915->rps.client_lock);
	}

	return timeout;
}

static long
i915_gem_object_wait_reservation(struct reservation_object *resv,
				 unsigned int flags,
				 long timeout,
				 struct intel_rps_client *rps)
{
	struct dma_fence *excl;

	if (flags & I915_WAIT_ALL) {
		struct dma_fence **shared;
		unsigned int count, i;
409 410
		int ret;

411 412
		ret = reservation_object_get_fences_rcu(resv,
							&excl, &count, &shared);
413 414 415
		if (ret)
			return ret;

416 417 418 419 420 421
		for (i = 0; i < count; i++) {
			timeout = i915_gem_object_wait_fence(shared[i],
							     flags, timeout,
							     rps);
			if (timeout <= 0)
				break;
422

423 424 425 426 427 428 429 430
			dma_fence_put(shared[i]);
		}

		for (; i < count; i++)
			dma_fence_put(shared[i]);
		kfree(shared);
	} else {
		excl = reservation_object_get_excl_rcu(resv);
431 432
	}

433 434 435 436 437 438
	if (excl && timeout > 0)
		timeout = i915_gem_object_wait_fence(excl, flags, timeout, rps);

	dma_fence_put(excl);

	return timeout;
439 440
}

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
static void __fence_set_priority(struct dma_fence *fence, int prio)
{
	struct drm_i915_gem_request *rq;
	struct intel_engine_cs *engine;

	if (!dma_fence_is_i915(fence))
		return;

	rq = to_request(fence);
	engine = rq->engine;
	if (!engine->schedule)
		return;

	engine->schedule(rq, prio);
}

static void fence_set_priority(struct dma_fence *fence, int prio)
{
	/* Recurse once into a fence-array */
	if (dma_fence_is_array(fence)) {
		struct dma_fence_array *array = to_dma_fence_array(fence);
		int i;

		for (i = 0; i < array->num_fences; i++)
			__fence_set_priority(array->fences[i], prio);
	} else {
		__fence_set_priority(fence, prio);
	}
}

int
i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
			      unsigned int flags,
			      int prio)
{
	struct dma_fence *excl;

	if (flags & I915_WAIT_ALL) {
		struct dma_fence **shared;
		unsigned int count, i;
		int ret;

		ret = reservation_object_get_fences_rcu(obj->resv,
							&excl, &count, &shared);
		if (ret)
			return ret;

		for (i = 0; i < count; i++) {
			fence_set_priority(shared[i], prio);
			dma_fence_put(shared[i]);
		}

		kfree(shared);
	} else {
		excl = reservation_object_get_excl_rcu(obj->resv);
	}

	if (excl) {
		fence_set_priority(excl, prio);
		dma_fence_put(excl);
	}
	return 0;
}

505 506 507 508 509 510
/**
 * Waits for rendering to the object to be completed
 * @obj: i915 gem object
 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
 * @timeout: how long to wait
 * @rps: client (user process) to charge for any waitboosting
511
 */
512 513 514 515 516
int
i915_gem_object_wait(struct drm_i915_gem_object *obj,
		     unsigned int flags,
		     long timeout,
		     struct intel_rps_client *rps)
517
{
518 519 520 521 522 523 524
	might_sleep();
#if IS_ENABLED(CONFIG_LOCKDEP)
	GEM_BUG_ON(debug_locks &&
		   !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
		   !!(flags & I915_WAIT_LOCKED));
#endif
	GEM_BUG_ON(timeout < 0);
525

526 527 528
	timeout = i915_gem_object_wait_reservation(obj->resv,
						   flags, timeout,
						   rps);
529
	return timeout < 0 ? timeout : 0;
530 531 532 533 534 535 536 537 538
}

static struct intel_rps_client *to_rps_client(struct drm_file *file)
{
	struct drm_i915_file_private *fpriv = file->driver_priv;

	return &fpriv->rps;
}

539 540 541 542 543
int
i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
			    int align)
{
	drm_dma_handle_t *phys;
544
	int ret;
545 546 547 548 549 550 551 552

	if (obj->phys_handle) {
		if ((unsigned long)obj->phys_handle->vaddr & (align -1))
			return -EBUSY;

		return 0;
	}

553
	if (obj->mm.madv != I915_MADV_WILLNEED)
554 555 556 557 558
		return -EFAULT;

	if (obj->base.filp == NULL)
		return -EINVAL;

Chris Wilson's avatar
Chris Wilson committed
559 560 561 562
	ret = i915_gem_object_unbind(obj);
	if (ret)
		return ret;

563
	__i915_gem_object_put_pages(obj, I915_MM_NORMAL);
564 565
	if (obj->mm.pages)
		return -EBUSY;
566

567 568 569 570 571 572
	/* create a new object */
	phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
	if (!phys)
		return -ENOMEM;

	obj->phys_handle = phys;
573 574
	obj->ops = &i915_gem_phys_ops;

575
	return i915_gem_object_pin_pages(obj);
576 577 578 579 580
}

static int
i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
		     struct drm_i915_gem_pwrite *args,
581
		     struct drm_file *file)
582 583 584
{
	struct drm_device *dev = obj->base.dev;
	void *vaddr = obj->phys_handle->vaddr + args->offset;
585
	char __user *user_data = u64_to_user_ptr(args->data_ptr);
586
	int ret;
587 588 589 590

	/* We manually control the domain here and pretend that it
	 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
	 */
591 592 593 594 595 596
	lockdep_assert_held(&obj->base.dev->struct_mutex);
	ret = i915_gem_object_wait(obj,
				   I915_WAIT_INTERRUPTIBLE |
				   I915_WAIT_LOCKED |
				   I915_WAIT_ALL,
				   MAX_SCHEDULE_TIMEOUT,
597
				   to_rps_client(file));
598 599
	if (ret)
		return ret;
600

601
	intel_fb_obj_invalidate(obj, ORIGIN_CPU);
602 603 604 605 606 607 608 609 610 611
	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);
612 613 614 615
		if (unwritten) {
			ret = -EFAULT;
			goto out;
		}
616 617
	}

618
	drm_clflush_virt_range(vaddr, args->size);
619
	i915_gem_chipset_flush(to_i915(dev));
620 621

out:
622
	intel_fb_obj_flush(obj, false, ORIGIN_CPU);
623
	return ret;
624 625
}

626
void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
627
{
628
	return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
629 630 631 632
}

void i915_gem_object_free(struct drm_i915_gem_object *obj)
{
633
	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
634
	kmem_cache_free(dev_priv->objects, obj);
635 636
}

637 638
static int
i915_gem_create(struct drm_file *file,
639
		struct drm_i915_private *dev_priv,
640 641
		uint64_t size,
		uint32_t *handle_p)
642
{
643
	struct drm_i915_gem_object *obj;
644 645
	int ret;
	u32 handle;
646

647
	size = roundup(size, PAGE_SIZE);
648 649
	if (size == 0)
		return -EINVAL;
650 651

	/* Allocate the new object */
652
	obj = i915_gem_object_create(dev_priv, size);
653 654
	if (IS_ERR(obj))
		return PTR_ERR(obj);
655

656
	ret = drm_gem_handle_create(file, &obj->base, &handle);
657
	/* drop reference from allocate - handle holds it now */
658
	i915_gem_object_put(obj);
659 660
	if (ret)
		return ret;
661

662
	*handle_p = handle;
663 664 665
	return 0;
}

666 667 668 669 670 671
int
i915_gem_dumb_create(struct drm_file *file,
		     struct drm_device *dev,
		     struct drm_mode_create_dumb *args)
{
	/* have to work out size/pitch and return them */
672
	args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
673
	args->size = args->pitch * args->height;
674
	return i915_gem_create(file, to_i915(dev),
675
			       args->size, &args->handle);
676 677 678 679
}

/**
 * Creates a new mm object and returns a handle to it.
680 681 682
 * @dev: drm device pointer
 * @data: ioctl data blob
 * @file: drm file pointer
683 684 685 686 687
 */
int
i915_gem_create_ioctl(struct drm_device *dev, void *data,
		      struct drm_file *file)
{
688
	struct drm_i915_private *dev_priv = to_i915(dev);
689
	struct drm_i915_gem_create *args = data;
690

691
	i915_gem_flush_free_objects(dev_priv);
692

693
	return i915_gem_create(file, dev_priv,
694
			       args->size, &args->handle);
695 696
}

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
static inline int
__copy_to_user_swizzled(char __user *cpu_vaddr,
			const char *gpu_vaddr, int gpu_offset,
			int length)
{
	int ret, cpu_offset = 0;

	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;

		ret = __copy_to_user(cpu_vaddr + cpu_offset,
				     gpu_vaddr + swizzled_gpu_offset,
				     this_length);
		if (ret)
			return ret + length;

		cpu_offset += this_length;
		gpu_offset += this_length;
		length -= this_length;
	}

	return 0;
}

723
static inline int
724 725
__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
			  const char __user *cpu_vaddr,
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
			  int length)
{
	int ret, cpu_offset = 0;

	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;

		ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
				       cpu_vaddr + cpu_offset,
				       this_length);
		if (ret)
			return ret + length;

		cpu_offset += this_length;
		gpu_offset += this_length;
		length -= this_length;
	}

	return 0;
}

749 750 751 752 753 754
/*
 * Pins the specified object's pages and synchronizes the object with
 * GPU accesses. Sets needs_clflush to non-zero if the caller should
 * flush the object from the CPU cache.
 */
int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
755
				    unsigned int *needs_clflush)
756 757 758
{
	int ret;

759
	lockdep_assert_held(&obj->base.dev->struct_mutex);
760

761
	*needs_clflush = 0;
762 763
	if (!i915_gem_object_has_struct_page(obj))
		return -ENODEV;
764

765 766 767 768 769
	ret = i915_gem_object_wait(obj,
				   I915_WAIT_INTERRUPTIBLE |
				   I915_WAIT_LOCKED,
				   MAX_SCHEDULE_TIMEOUT,
				   NULL);
770 771 772
	if (ret)
		return ret;

773
	ret = i915_gem_object_pin_pages(obj);
774 775 776
	if (ret)
		return ret;

777 778
	i915_gem_object_flush_gtt_write_domain(obj);

779 780 781 782 783 784
	/* If we're not in the cpu read domain, set ourself into the gtt
	 * read domain and manually flush cachelines (if required). This
	 * optimizes for the case when the gpu will dirty the data
	 * anyway again before the next pread happens.
	 */
	if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
785 786
		*needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
							obj->cache_level);
787 788 789

	if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
		ret = i915_gem_object_set_to_cpu_domain(obj, false);
790 791 792
		if (ret)
			goto err_unpin;

793
		*needs_clflush = 0;
794 795
	}

796
	/* return with the pages pinned */
797
	return 0;
798 799 800 801

err_unpin:
	i915_gem_object_unpin_pages(obj);
	return ret;
802 803 804 805 806 807 808
}

int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
				     unsigned int *needs_clflush)
{
	int ret;

809 810
	lockdep_assert_held(&obj->base.dev->struct_mutex);

811 812 813 814
	*needs_clflush = 0;
	if (!i915_gem_object_has_struct_page(obj))
		return -ENODEV;

815 816 817 818 819 820
	ret = i915_gem_object_wait(obj,
				   I915_WAIT_INTERRUPTIBLE |
				   I915_WAIT_LOCKED |
				   I915_WAIT_ALL,
				   MAX_SCHEDULE_TIMEOUT,
				   NULL);
821 822 823
	if (ret)
		return ret;

824
	ret = i915_gem_object_pin_pages(obj);
825 826 827
	if (ret)
		return ret;

828 829
	i915_gem_object_flush_gtt_write_domain(obj);

830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
	/* If we're not in the cpu write domain, set ourself into the
	 * gtt write domain and manually flush cachelines (as required).
	 * This optimizes for the case when the gpu will use the data
	 * right away and we therefore have to clflush anyway.
	 */
	if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
		*needs_clflush |= cpu_write_needs_clflush(obj) << 1;

	/* Same trick applies to invalidate partially written cachelines read
	 * before writing.
	 */
	if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
		*needs_clflush |= !cpu_cache_is_coherent(obj->base.dev,
							 obj->cache_level);

	if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
		ret = i915_gem_object_set_to_cpu_domain(obj, true);
847 848 849
		if (ret)
			goto err_unpin;

850 851 852 853 854 855 856
		*needs_clflush = 0;
	}

	if ((*needs_clflush & CLFLUSH_AFTER) == 0)
		obj->cache_dirty = true;

	intel_fb_obj_invalidate(obj, ORIGIN_CPU);
857
	obj->mm.dirty = true;
858
	/* return with the pages pinned */
859
	return 0;
860 861 862 863

err_unpin:
	i915_gem_object_unpin_pages(obj);
	return ret;
864 865
}

866 867 868 869
static void
shmem_clflush_swizzled_range(char *addr, unsigned long length,
			     bool swizzled)
{
870
	if (unlikely(swizzled)) {
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
		unsigned long start = (unsigned long) addr;
		unsigned long end = (unsigned long) addr + length;

		/* For swizzling simply ensure that we always flush both
		 * channels. Lame, but simple and it works. Swizzled
		 * pwrite/pread is far from a hotpath - current userspace
		 * doesn't use it at all. */
		start = round_down(start, 128);
		end = round_up(end, 128);

		drm_clflush_virt_range((void *)start, end - start);
	} else {
		drm_clflush_virt_range(addr, length);
	}

}

888 889 890
/* Only difference to the fast-path function is that this can handle bit17
 * and uses non-atomic copy and kmap functions. */
static int
891
shmem_pread_slow(struct page *page, int offset, int length,
892 893 894 895 896 897 898 899
		 char __user *user_data,
		 bool page_do_bit17_swizzling, bool needs_clflush)
{
	char *vaddr;
	int ret;

	vaddr = kmap(page);
	if (needs_clflush)
900
		shmem_clflush_swizzled_range(vaddr + offset, length,
901
					     page_do_bit17_swizzling);
902 903

	if (page_do_bit17_swizzling)
904
		ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
905
	else
906
		ret = __copy_to_user(user_data, vaddr + offset, length);
907 908
	kunmap(page);

909
	return ret ? - EFAULT : 0;
910 911
}

912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
static int
shmem_pread(struct page *page, int offset, int length, char __user *user_data,
	    bool page_do_bit17_swizzling, bool needs_clflush)
{
	int ret;

	ret = -ENODEV;
	if (!page_do_bit17_swizzling) {
		char *vaddr = kmap_atomic(page);

		if (needs_clflush)
			drm_clflush_virt_range(vaddr + offset, length);
		ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
		kunmap_atomic(vaddr);
	}
	if (ret == 0)
		return 0;

	return shmem_pread_slow(page, offset, length, user_data,
				page_do_bit17_swizzling, needs_clflush);
}

static int
i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
		     struct drm_i915_gem_pread *args)
{
	char __user *user_data;
	u64 remain;
	unsigned int obj_do_bit17_swizzling;
	unsigned int needs_clflush;
	unsigned int idx, offset;
	int ret;

	obj_do_bit17_swizzling = 0;
	if (i915_gem_object_needs_bit17_swizzle(obj))
		obj_do_bit17_swizzling = BIT(17);

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

	ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
	mutex_unlock(&obj->base.dev->struct_mutex);
	if (ret)
		return ret;

	remain = args->size;
	user_data = u64_to_user_ptr(args->data_ptr);
	offset = offset_in_page(args->offset);
	for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
		struct page *page = i915_gem_object_get_page(obj, idx);
		int length;

		length = remain;
		if (offset + length > PAGE_SIZE)
			length = PAGE_SIZE - offset;

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

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

	i915_gem_obj_finish_shmem_access(obj);
	return ret;
}

static inline bool
gtt_user_read(struct io_mapping *mapping,
	      loff_t base, int offset,
	      char __user *user_data, int length)
988 989
{
	void *vaddr;
990
	unsigned long unwritten;
991 992

	/* We can use the cpu mem copy function because this is X86. */
993 994 995 996 997 998 999 1000 1001
	vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
	unwritten = __copy_to_user_inatomic(user_data, vaddr + offset, length);
	io_mapping_unmap_atomic(vaddr);
	if (unwritten) {
		vaddr = (void __force *)
			io_mapping_map_wc(mapping, base, PAGE_SIZE);
		unwritten = copy_to_user(user_data, vaddr + offset, length);
		io_mapping_unmap(vaddr);
	}
1002 1003 1004 1005
	return unwritten;
}

static int
1006 1007
i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
		   const struct drm_i915_gem_pread *args)
1008
{
1009 1010
	struct drm_i915_private *i915 = to_i915(obj->base.dev);
	struct i915_ggtt *ggtt = &i915->ggtt;
1011
	struct drm_mm_node node;
1012 1013 1014
	struct i915_vma *vma;
	void __user *user_data;
	u64 remain, offset;
1015 1016
	int ret;

1017 1018 1019 1020 1021 1022 1023
	ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
	if (ret)
		return ret;

	intel_runtime_pm_get(i915);
	vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
				       PIN_MAPPABLE | PIN_NONBLOCK);
1024 1025 1026
	if (!IS_ERR(vma)) {
		node.start = i915_ggtt_offset(vma);
		node.allocated = false;
1027
		ret = i915_vma_put_fence(vma);
1028 1029 1030 1031 1032
		if (ret) {
			i915_vma_unpin(vma);
			vma = ERR_PTR(ret);
		}
	}
Chris Wilson's avatar
Chris Wilson committed
1033
	if (IS_ERR(vma)) {
1034
		ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
1035
		if (ret)
1036 1037
			goto out_unlock;
		GEM_BUG_ON(!node.allocated);
1038 1039 1040 1041 1042 1043
	}

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

1044
	mutex_unlock(&i915->drm.struct_mutex);
1045

1046 1047 1048
	user_data = u64_to_user_ptr(args->data_ptr);
	remain = args->size;
	offset = args->offset;
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

	while (remain > 0) {
		/* Operation in this page
		 *
		 * page_base = page offset within aperture
		 * page_offset = offset within page
		 * page_length = bytes to copy for this page
		 */
		u32 page_base = node.start;
		unsigned page_offset = offset_in_page(offset);
		unsigned page_length = PAGE_SIZE - page_offset;
		page_length = remain < page_length ? remain : page_length;
		if (node.allocated) {
			wmb();
			ggtt->base.insert_page(&ggtt->base,
					       i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1065
					       node.start, I915_CACHE_NONE, 0);
1066 1067 1068 1069
			wmb();
		} else {
			page_base += offset & PAGE_MASK;
		}
1070 1071 1072

		if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
				  user_data, page_length)) {
1073 1074 1075 1076 1077 1078 1079 1080 1081
			ret = -EFAULT;
			break;
		}

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

1082
	mutex_lock(&i915->drm.struct_mutex);
1083 1084 1085 1086
out_unpin:
	if (node.allocated) {
		wmb();
		ggtt->base.clear_range(&ggtt->base,
1087
				       node.start, node.size);
1088 1089
		remove_mappable_node(&node);
	} else {
Chris Wilson's avatar
Chris Wilson committed
1090
		i915_vma_unpin(vma);
1091
	}
1092 1093 1094
out_unlock:
	intel_runtime_pm_put(i915);
	mutex_unlock(&i915->drm.struct_mutex);
1095

1096 1097 1098
	return ret;
}

1099 1100
/**
 * Reads data from the object referenced by handle.
1101 1102 1103
 * @dev: drm device pointer
 * @data: ioctl data blob
 * @file: drm file pointer
1104 1105 1106 1107 1108
 *
 * On error, the contents of *data are undefined.
 */
int
i915_gem_pread_ioctl(struct drm_device *dev, void *data,
1109
		     struct drm_file *file)
1110 1111
{
	struct drm_i915_gem_pread *args = data;
1112
	struct drm_i915_gem_object *obj;
1113
	int ret;
1114

1115 1116 1117 1118
	if (args->size == 0)
		return 0;

	if (!access_ok(VERIFY_WRITE,
1119
		       u64_to_user_ptr(args->data_ptr),
1120 1121 1122
		       args->size))
		return -EFAULT;

1123
	obj = i915_gem_object_lookup(file, args->handle);
1124 1125
	if (!obj)
		return -ENOENT;
1126

1127
	/* Bounds check source.  */
1128 1129
	if (args->offset > obj->base.size ||
	    args->size > obj->base.size - args->offset) {
1130
		ret = -EINVAL;
1131
		goto out;
1132 1133
	}

Chris Wilson's avatar
Chris Wilson committed
1134 1135
	trace_i915_gem_object_pread(obj, args->offset, args->size);

1136 1137 1138 1139
	ret = i915_gem_object_wait(obj,
				   I915_WAIT_INTERRUPTIBLE,
				   MAX_SCHEDULE_TIMEOUT,
				   to_rps_client(file));
1140
	if (ret)
1141
		goto out;
1142

1143
	ret = i915_gem_object_pin_pages(obj);
1144
	if (ret)
1145
		goto out;
1146

1147
	ret = i915_gem_shmem_pread(obj, args);
1148
	if (ret == -EFAULT || ret == -ENODEV)
1149
		ret = i915_gem_gtt_pread(obj, args);
1150

1151 1152
	i915_gem_object_unpin_pages(obj);
out:
1153
	i915_gem_object_put(obj);
1154
	return ret;
1155 1156
}

1157 1158
/* This is the fast write path which cannot handle
 * page faults in the source data
1159
 */
1160

1161 1162 1163 1164
static inline bool
ggtt_write(struct io_mapping *mapping,
	   loff_t base, int offset,
	   char __user *user_data, int length)
1165
{
1166
	void *vaddr;
1167
	unsigned long unwritten;
1168

1169
	/* We can use the cpu mem copy function because this is X86. */
1170 1171
	vaddr = (void __force *)io_mapping_map_atomic_wc(mapping, base);
	unwritten = __copy_from_user_inatomic_nocache(vaddr + offset,
1172
						      user_data, length);
1173 1174 1175 1176 1177 1178 1179
	io_mapping_unmap_atomic(vaddr);
	if (unwritten) {
		vaddr = (void __force *)
			io_mapping_map_wc(mapping, base, PAGE_SIZE);
		unwritten = copy_from_user(vaddr + offset, user_data, length);
		io_mapping_unmap(vaddr);
	}
1180 1181 1182 1183

	return unwritten;
}

1184 1185 1186
/**
 * This is the fast pwrite path, where we copy the data directly from the
 * user into the GTT, uncached.
1187
 * @obj: i915 GEM object
1188
 * @args: pwrite arguments structure
1189
 */
1190
static int
1191 1192
i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
			 const struct drm_i915_gem_pwrite *args)
1193
{
1194
	struct drm_i915_private *i915 = to_i915(obj->base.dev);
1195 1196
	struct i915_ggtt *ggtt = &i915->ggtt;
	struct drm_mm_node node;
1197 1198 1199
	struct i915_vma *vma;
	u64 remain, offset;
	void __user *user_data;
1200
	int ret;
1201

1202 1203 1204
	ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
	if (ret)
		return ret;
Daniel Vetter's avatar
Daniel Vetter committed
1205

1206
	intel_runtime_pm_get(i915);
Chris Wilson's avatar
Chris Wilson committed
1207
	vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
1208
				       PIN_MAPPABLE | PIN_NONBLOCK);
1209 1210 1211
	if (!IS_ERR(vma)) {
		node.start = i915_ggtt_offset(vma);
		node.allocated = false;
1212
		ret = i915_vma_put_fence(vma);
1213 1214 1215 1216 1217
		if (ret) {
			i915_vma_unpin(vma);
			vma = ERR_PTR(ret);
		}
	}
Chris Wilson's avatar
Chris Wilson committed
1218
	if (IS_ERR(vma)) {
1219
		ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
1220
		if (ret)
1221 1222
			goto out_unlock;
		GEM_BUG_ON(!node.allocated);
1223
	}
Daniel Vetter's avatar
Daniel Vetter committed
1224 1225 1226 1227 1228

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

1229 1230
	mutex_unlock(&