i915_gem.c 101 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"
Chris Wilson's avatar
Chris Wilson committed
32
#include "i915_trace.h"
33
#include "intel_drv.h"
34
#include <linux/shmem_fs.h>
35
#include <linux/slab.h>
36
#include <linux/swap.h>
37
#include <linux/pci.h>
38

39
static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
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);
42
43
44
static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
						    unsigned alignment,
						    bool map_and_fenceable);
45
46
static int i915_gem_phys_pwrite(struct drm_device *dev,
				struct drm_i915_gem_object *obj,
47
				struct drm_i915_gem_pwrite *args,
48
49
				struct drm_file *file);
static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
50

51
52
53
54
55
56
static void i915_gem_write_fence(struct drm_device *dev, int reg,
				 struct drm_i915_gem_object *obj);
static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
					 struct drm_i915_fence_reg *fence,
					 bool enable);

57
static int i915_gem_inactive_shrink(struct shrinker *shrinker,
58
				    struct shrink_control *sc);
59
static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
60

61
62
63
64
65
66
67
68
static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
{
	if (obj->tiling_mode)
		i915_gem_release_mmap(obj);

	/* As we do not have an associated fence register, we will force
	 * a tiling change if we ever need to acquire one.
	 */
69
	obj->fence_dirty = false;
70
71
72
	obj->fence_reg = I915_FENCE_REG_NONE;
}

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* 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;
}

88
89
static int
i915_gem_wait_for_error(struct drm_device *dev)
90
91
92
93
94
95
96
97
98
99
100
101
102
{
	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;

103
104
105
106
107
108
109
110
111
112
113
	if (atomic_read(&dev_priv->mm.wedged)) {
		/* 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 0;
114
115
}

116
int i915_mutex_lock_interruptible(struct drm_device *dev)
117
118
119
{
	int ret;

120
	ret = i915_gem_wait_for_error(dev);
121
122
123
124
125
126
127
	if (ret)
		return ret;

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

128
	WARN_ON(i915_verify_lists(dev));
129
130
	return 0;
}
131

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

138
139
int
i915_gem_init_ioctl(struct drm_device *dev, void *data,
140
		    struct drm_file *file)
141
142
{
	struct drm_i915_gem_init *args = data;
143
144
145
146

	if (args->gtt_start >= args->gtt_end ||
	    (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
		return -EINVAL;
147

148
149
150
151
	/* GEM with user mode setting was never supported on ilk and later. */
	if (INTEL_INFO(dev)->gen >= 5)
		return -ENODEV;

152
	mutex_lock(&dev->struct_mutex);
153
154
	i915_gem_init_global_gtt(dev, args->gtt_start,
				 args->gtt_end, args->gtt_end);
155
156
	mutex_unlock(&dev->struct_mutex);

157
	return 0;
158
159
}

160
161
int
i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
162
			    struct drm_file *file)
163
{
164
	struct drm_i915_private *dev_priv = dev->dev_private;
165
	struct drm_i915_gem_get_aperture *args = data;
166
167
	struct drm_i915_gem_object *obj;
	size_t pinned;
168
169
170
171

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

172
	pinned = 0;
173
	mutex_lock(&dev->struct_mutex);
174
175
	list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
		pinned += obj->gtt_space->size;
176
	mutex_unlock(&dev->struct_mutex);
177

178
	args->aper_size = dev_priv->mm.gtt_total;
179
	args->aper_available_size = args->aper_size - pinned;
180

181
182
183
	return 0;
}

184
185
186
187
188
static int
i915_gem_create(struct drm_file *file,
		struct drm_device *dev,
		uint64_t size,
		uint32_t *handle_p)
189
{
190
	struct drm_i915_gem_object *obj;
191
192
	int ret;
	u32 handle;
193

194
	size = roundup(size, PAGE_SIZE);
195
196
	if (size == 0)
		return -EINVAL;
197
198

	/* Allocate the new object */
199
	obj = i915_gem_alloc_object(dev, size);
200
201
202
	if (obj == NULL)
		return -ENOMEM;

203
	ret = drm_gem_handle_create(file, &obj->base, &handle);
204
	if (ret) {
205
206
		drm_gem_object_release(&obj->base);
		i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
207
		kfree(obj);
208
		return ret;
209
	}
210

211
	/* drop reference from allocate - handle holds it now */
212
	drm_gem_object_unreference(&obj->base);
213
214
	trace_i915_gem_object_create(obj);

215
	*handle_p = handle;
216
217
218
	return 0;
}

219
220
221
222
223
224
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 */
225
	args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
	args->size = args->pitch * args->height;
	return i915_gem_create(file, dev,
			       args->size, &args->handle);
}

int i915_gem_dumb_destroy(struct drm_file *file,
			  struct drm_device *dev,
			  uint32_t handle)
{
	return drm_gem_handle_delete(file, handle);
}

/**
 * Creates a new mm object and returns a handle to it.
 */
int
i915_gem_create_ioctl(struct drm_device *dev, void *data,
		      struct drm_file *file)
{
	struct drm_i915_gem_create *args = data;
	return i915_gem_create(file, dev,
			       args->size, &args->handle);
}

250
static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
251
{
252
	drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
253
254

	return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
255
		obj->tiling_mode != I915_TILING_NONE;
256
257
}

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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;
}

284
static inline int
285
286
__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
			  const char __user *cpu_vaddr,
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
			  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;
}

310
311
312
/* Per-page copy function for the shmem pread fastpath.
 * Flushes invalid cachelines before reading the target if
 * needs_clflush is set. */
313
static int
314
315
316
317
318
319
320
shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
		 char __user *user_data,
		 bool page_do_bit17_swizzling, bool needs_clflush)
{
	char *vaddr;
	int ret;

321
	if (unlikely(page_do_bit17_swizzling))
322
323
324
325
326
327
328
329
330
331
332
333
334
335
		return -EINVAL;

	vaddr = kmap_atomic(page);
	if (needs_clflush)
		drm_clflush_virt_range(vaddr + shmem_page_offset,
				       page_length);
	ret = __copy_to_user_inatomic(user_data,
				      vaddr + shmem_page_offset,
				      page_length);
	kunmap_atomic(vaddr);

	return ret;
}

336
337
338
339
static void
shmem_clflush_swizzled_range(char *addr, unsigned long length,
			     bool swizzled)
{
340
	if (unlikely(swizzled)) {
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
		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);
	}

}

358
359
360
361
362
363
364
365
366
367
368
369
/* Only difference to the fast-path function is that this can handle bit17
 * and uses non-atomic copy and kmap functions. */
static int
shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
		 char __user *user_data,
		 bool page_do_bit17_swizzling, bool needs_clflush)
{
	char *vaddr;
	int ret;

	vaddr = kmap(page);
	if (needs_clflush)
370
371
372
		shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
					     page_length,
					     page_do_bit17_swizzling);
373
374
375
376
377
378
379
380
381
382
383
384
385
386

	if (page_do_bit17_swizzling)
		ret = __copy_to_user_swizzled(user_data,
					      vaddr, shmem_page_offset,
					      page_length);
	else
		ret = __copy_to_user(user_data,
				     vaddr + shmem_page_offset,
				     page_length);
	kunmap(page);

	return ret;
}

387
static int
388
389
390
391
i915_gem_shmem_pread(struct drm_device *dev,
		     struct drm_i915_gem_object *obj,
		     struct drm_i915_gem_pread *args,
		     struct drm_file *file)
392
{
393
	struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
394
	char __user *user_data;
395
	ssize_t remain;
396
	loff_t offset;
397
	int shmem_page_offset, page_length, ret = 0;
398
	int obj_do_bit17_swizzling, page_do_bit17_swizzling;
399
	int hit_slowpath = 0;
400
	int prefaulted = 0;
401
	int needs_clflush = 0;
402
	int release_page;
403

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

407
	obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
408

409
410
411
412
413
414
415
416
417
418
419
	if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
		/* 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->cache_level == I915_CACHE_NONE)
			needs_clflush = 1;
		ret = i915_gem_object_set_to_gtt_domain(obj, false);
		if (ret)
			return ret;
	}
420

421
	offset = args->offset;
422
423

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

426
427
428
429
430
		/* Operation in this page
		 *
		 * shmem_page_offset = offset within page in shmem file
		 * page_length = bytes to copy for this page
		 */
431
		shmem_page_offset = offset_in_page(offset);
432
433
434
435
		page_length = remain;
		if ((shmem_page_offset + page_length) > PAGE_SIZE)
			page_length = PAGE_SIZE - shmem_page_offset;

436
437
438
439
440
441
442
443
444
445
		if (obj->pages) {
			page = obj->pages[offset >> PAGE_SHIFT];
			release_page = 0;
		} else {
			page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
			if (IS_ERR(page)) {
				ret = PTR_ERR(page);
				goto out;
			}
			release_page = 1;
446
		}
447

448
449
450
		page_do_bit17_swizzling = obj_do_bit17_swizzling &&
			(page_to_phys(page) & (1 << 17)) != 0;

451
452
453
454
455
		ret = shmem_pread_fast(page, shmem_page_offset, page_length,
				       user_data, page_do_bit17_swizzling,
				       needs_clflush);
		if (ret == 0)
			goto next_page;
456
457

		hit_slowpath = 1;
458
		page_cache_get(page);
459
460
		mutex_unlock(&dev->struct_mutex);

461
		if (!prefaulted) {
462
			ret = fault_in_multipages_writeable(user_data, remain);
463
464
465
466
467
468
469
			/* Userspace is tricking us, but we've already clobbered
			 * its pages with the prefault and promised to write the
			 * data up to the first fault. Hence ignore any errors
			 * and just continue. */
			(void)ret;
			prefaulted = 1;
		}
470

471
472
473
		ret = shmem_pread_slow(page, shmem_page_offset, page_length,
				       user_data, page_do_bit17_swizzling,
				       needs_clflush);
474

475
		mutex_lock(&dev->struct_mutex);
476
		page_cache_release(page);
477
next_page:
478
		mark_page_accessed(page);
479
480
		if (release_page)
			page_cache_release(page);
481

482
483
484
485
486
		if (ret) {
			ret = -EFAULT;
			goto out;
		}

487
		remain -= page_length;
488
		user_data += page_length;
489
490
491
		offset += page_length;
	}

492
out:
493
494
495
496
497
	if (hit_slowpath) {
		/* Fixup: Kill any reinstated backing storage pages */
		if (obj->madv == __I915_MADV_PURGED)
			i915_gem_object_truncate(obj);
	}
498
499
500
501

	return ret;
}

502
503
504
505
506
507
508
/**
 * 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,
509
		     struct drm_file *file)
510
511
{
	struct drm_i915_gem_pread *args = data;
512
	struct drm_i915_gem_object *obj;
513
	int ret = 0;
514

515
516
517
518
519
520
521
522
	if (args->size == 0)
		return 0;

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

523
	ret = i915_mutex_lock_interruptible(dev);
524
	if (ret)
525
		return ret;
526

527
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
528
	if (&obj->base == NULL) {
529
530
		ret = -ENOENT;
		goto unlock;
531
	}
532

533
	/* Bounds check source.  */
534
535
	if (args->offset > obj->base.size ||
	    args->size > obj->base.size - args->offset) {
536
		ret = -EINVAL;
537
		goto out;
538
539
	}

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

542
	ret = i915_gem_shmem_pread(dev, obj, args, file);
543

544
out:
545
	drm_gem_object_unreference(&obj->base);
546
unlock:
547
	mutex_unlock(&dev->struct_mutex);
548
	return ret;
549
550
}

551
552
/* This is the fast write path which cannot handle
 * page faults in the source data
553
 */
554
555
556
557
558
559

static inline int
fast_user_write(struct io_mapping *mapping,
		loff_t page_base, int page_offset,
		char __user *user_data,
		int length)
560
{
561
562
	void __iomem *vaddr_atomic;
	void *vaddr;
563
	unsigned long unwritten;
564

Peter Zijlstra's avatar
Peter Zijlstra committed
565
	vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
566
567
568
	/* We can use the cpu mem copy function because this is X86. */
	vaddr = (void __force*)vaddr_atomic + page_offset;
	unwritten = __copy_from_user_inatomic_nocache(vaddr,
569
						      user_data, length);
Peter Zijlstra's avatar
Peter Zijlstra committed
570
	io_mapping_unmap_atomic(vaddr_atomic);
571
	return unwritten;
572
573
}

574
575
576
577
/**
 * This is the fast pwrite path, where we copy the data directly from the
 * user into the GTT, uncached.
 */
578
static int
579
580
i915_gem_gtt_pwrite_fast(struct drm_device *dev,
			 struct drm_i915_gem_object *obj,
581
			 struct drm_i915_gem_pwrite *args,
582
			 struct drm_file *file)
583
{
584
	drm_i915_private_t *dev_priv = dev->dev_private;
585
	ssize_t remain;
586
	loff_t offset, page_base;
587
	char __user *user_data;
Daniel Vetter's avatar
Daniel Vetter committed
588
589
590
591
592
593
594
595
596
597
598
599
600
	int page_offset, page_length, ret;

	ret = i915_gem_object_pin(obj, 0, true);
	if (ret)
		goto out;

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

	ret = i915_gem_object_put_fence(obj);
	if (ret)
		goto out_unpin;
601
602
603
604

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

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

	while (remain > 0) {
		/* Operation in this page
		 *
610
611
612
		 * page_base = page offset within aperture
		 * page_offset = offset within page
		 * page_length = bytes to copy for this page
613
		 */
614
615
		page_base = offset & PAGE_MASK;
		page_offset = offset_in_page(offset);
616
617
618
619
620
		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
621
622
		 * source page isn't available.  Return the error and we'll
		 * retry in the slow path.
623
		 */
624
		if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
Daniel Vetter's avatar
Daniel Vetter committed
625
626
627
628
				    page_offset, user_data, page_length)) {
			ret = -EFAULT;
			goto out_unpin;
		}
629

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

Daniel Vetter's avatar
Daniel Vetter committed
635
636
637
out_unpin:
	i915_gem_object_unpin(obj);
out:
638
	return ret;
639
640
}

641
642
643
644
/* Per-page copy function for the shmem pwrite fastpath.
 * Flushes invalid cachelines before writing to the target if
 * needs_clflush_before is set and flushes out any written cachelines after
 * writing if needs_clflush is set. */
645
static int
646
647
648
649
650
shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
		  char __user *user_data,
		  bool page_do_bit17_swizzling,
		  bool needs_clflush_before,
		  bool needs_clflush_after)
651
{
652
	char *vaddr;
653
	int ret;
654

655
	if (unlikely(page_do_bit17_swizzling))
656
		return -EINVAL;
657

658
659
660
661
662
663
664
665
666
667
668
	vaddr = kmap_atomic(page);
	if (needs_clflush_before)
		drm_clflush_virt_range(vaddr + shmem_page_offset,
				       page_length);
	ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
						user_data,
						page_length);
	if (needs_clflush_after)
		drm_clflush_virt_range(vaddr + shmem_page_offset,
				       page_length);
	kunmap_atomic(vaddr);
669
670
671
672

	return ret;
}

673
674
/* Only difference to the fast-path function is that this can handle bit17
 * and uses non-atomic copy and kmap functions. */
675
static int
676
677
678
679
680
shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
		  char __user *user_data,
		  bool page_do_bit17_swizzling,
		  bool needs_clflush_before,
		  bool needs_clflush_after)
681
{
682
683
	char *vaddr;
	int ret;
684

685
	vaddr = kmap(page);
686
	if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
687
688
689
		shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
					     page_length,
					     page_do_bit17_swizzling);
690
691
	if (page_do_bit17_swizzling)
		ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
692
693
						user_data,
						page_length);
694
695
696
697
698
	else
		ret = __copy_from_user(vaddr + shmem_page_offset,
				       user_data,
				       page_length);
	if (needs_clflush_after)
699
700
701
		shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
					     page_length,
					     page_do_bit17_swizzling);
702
	kunmap(page);
703

704
	return ret;
705
706
707
}

static int
708
709
710
711
i915_gem_shmem_pwrite(struct drm_device *dev,
		      struct drm_i915_gem_object *obj,
		      struct drm_i915_gem_pwrite *args,
		      struct drm_file *file)
712
{
713
	struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
714
	ssize_t remain;
715
716
	loff_t offset;
	char __user *user_data;
717
	int shmem_page_offset, page_length, ret = 0;
718
	int obj_do_bit17_swizzling, page_do_bit17_swizzling;
719
	int hit_slowpath = 0;
720
721
	int needs_clflush_after = 0;
	int needs_clflush_before = 0;
722
	int release_page;
723

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

727
	obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
728

729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
	if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
		/* If we're not in the cpu write domain, set ourself into the gtt
		 * write domain and manually flush cachelines (if required). This
		 * optimizes for the case when the gpu will use the data
		 * right away and we therefore have to clflush anyway. */
		if (obj->cache_level == I915_CACHE_NONE)
			needs_clflush_after = 1;
		ret = i915_gem_object_set_to_gtt_domain(obj, true);
		if (ret)
			return ret;
	}
	/* Same trick applies for invalidate partially written cachelines before
	 * writing.  */
	if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
	    && obj->cache_level == I915_CACHE_NONE)
		needs_clflush_before = 1;

746
	offset = args->offset;
747
	obj->dirty = 1;
748

749
	while (remain > 0) {
750
		struct page *page;
751
		int partial_cacheline_write;
752

753
754
755
756
757
		/* Operation in this page
		 *
		 * shmem_page_offset = offset within page in shmem file
		 * page_length = bytes to copy for this page
		 */
758
		shmem_page_offset = offset_in_page(offset);
759
760
761
762
763

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

764
765
766
767
768
769
770
		/* If we don't overwrite a cacheline completely we need to be
		 * careful to have up-to-date data by first clflushing. Don't
		 * overcomplicate things and flush the entire patch. */
		partial_cacheline_write = needs_clflush_before &&
			((shmem_page_offset | page_length)
				& (boot_cpu_data.x86_clflush_size - 1));

771
772
773
774
775
776
777
778
779
780
		if (obj->pages) {
			page = obj->pages[offset >> PAGE_SHIFT];
			release_page = 0;
		} else {
			page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
			if (IS_ERR(page)) {
				ret = PTR_ERR(page);
				goto out;
			}
			release_page = 1;
781
782
		}

783
784
785
		page_do_bit17_swizzling = obj_do_bit17_swizzling &&
			(page_to_phys(page) & (1 << 17)) != 0;

786
787
788
789
790
791
		ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
					user_data, page_do_bit17_swizzling,
					partial_cacheline_write,
					needs_clflush_after);
		if (ret == 0)
			goto next_page;
792
793

		hit_slowpath = 1;
794
		page_cache_get(page);
795
796
		mutex_unlock(&dev->struct_mutex);

797
798
799
800
		ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
					user_data, page_do_bit17_swizzling,
					partial_cacheline_write,
					needs_clflush_after);
801

802
		mutex_lock(&dev->struct_mutex);
803
		page_cache_release(page);
804
next_page:
805
806
		set_page_dirty(page);
		mark_page_accessed(page);
807
808
		if (release_page)
			page_cache_release(page);
809

810
811
812
813
814
		if (ret) {
			ret = -EFAULT;
			goto out;
		}

815
		remain -= page_length;
816
		user_data += page_length;
817
		offset += page_length;
818
819
	}

820
out:
821
822
823
824
825
826
827
828
829
830
	if (hit_slowpath) {
		/* Fixup: Kill any reinstated backing storage pages */
		if (obj->madv == __I915_MADV_PURGED)
			i915_gem_object_truncate(obj);
		/* and flush dirty cachelines in case the object isn't in the cpu write
		 * domain anymore. */
		if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
			i915_gem_clflush_object(obj);
			intel_gtt_chipset_flush();
		}
831
	}
832

833
834
835
	if (needs_clflush_after)
		intel_gtt_chipset_flush();

836
	return ret;
837
838
839
840
841
842
843
844
845
}

/**
 * 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,
846
		      struct drm_file *file)
847
848
{
	struct drm_i915_gem_pwrite *args = data;
849
	struct drm_i915_gem_object *obj;
850
851
852
853
854
855
856
857
858
859
	int ret;

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

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

860
861
	ret = fault_in_multipages_readable((char __user *)(uintptr_t)args->data_ptr,
					   args->size);
862
863
	if (ret)
		return -EFAULT;
864

865
	ret = i915_mutex_lock_interruptible(dev);
866
	if (ret)
867
		return ret;
868

869
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
870
	if (&obj->base == NULL) {
871
872
		ret = -ENOENT;
		goto unlock;
873
	}
874

875
	/* Bounds check destination. */
876
877
	if (args->offset > obj->base.size ||
	    args->size > obj->base.size - args->offset) {
878
		ret = -EINVAL;
879
		goto out;
880
881
	}

Chris Wilson's avatar
Chris Wilson committed
882
883
	trace_i915_gem_object_pwrite(obj, args->offset, args->size);

Daniel Vetter's avatar
Daniel Vetter committed
884
	ret = -EFAULT;
885
886
887
888
889
890
	/* 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.
	 */
891
	if (obj->phys_obj) {
892
		ret = i915_gem_phys_pwrite(dev, obj, args, file);
893
894
895
896
		goto out;
	}

	if (obj->gtt_space &&
897
	    obj->cache_level == I915_CACHE_NONE &&
898
	    obj->tiling_mode == I915_TILING_NONE &&
899
	    obj->map_and_fenceable &&
900
	    obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
901
		ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter's avatar
Daniel Vetter committed
902
903
904
		/* Note that the gtt paths might fail with non-page-backed user
		 * pointers (e.g. gtt mappings when moving data between
		 * textures). Fallback to the shmem path in that case. */
905
	}
906

907
	if (ret == -EFAULT)
Daniel Vetter's avatar
Daniel Vetter committed
908
		ret = i915_gem_shmem_pwrite(dev, obj, args, file);
909

910
out:
911
	drm_gem_object_unreference(&obj->base);
912
unlock:
913
	mutex_unlock(&dev->struct_mutex);
914
915
916
917
	return ret;
}

/**
918
919
 * Called when user space prepares to use an object with the CPU, either
 * through the mmap ioctl's mapping or a GTT mapping.
920
921
922
 */
int
i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
923
			  struct drm_file *file)
924
925
{
	struct drm_i915_gem_set_domain *args = data;
926
	struct drm_i915_gem_object *obj;
927
928
	uint32_t read_domains = args->read_domains;
	uint32_t write_domain = args->write_domain;
929
930
931
932
933
	int ret;

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

934
	/* Only handle setting domains to types used by the CPU. */
935
	if (write_domain & I915_GEM_GPU_DOMAINS)
936
937
		return -EINVAL;

938
	if (read_domains & I915_GEM_GPU_DOMAINS)
939
940
941
942
943
944
945
946
		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;

947
	ret = i915_mutex_lock_interruptible(dev);
948
	if (ret)
949
		return ret;
950

951
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
952
	if (&obj->base == NULL) {
953
954
		ret = -ENOENT;
		goto unlock;
955
	}
956

957
958
	if (read_domains & I915_GEM_DOMAIN_GTT) {
		ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
959
960
961
962
963
964
965

		/* 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;
966
	} else {
967
		ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
968
969
	}

970
	drm_gem_object_unreference(&obj->base);
971
unlock:
972
973
974
975
976
977
978
979
980
	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,
981
			 struct drm_file *file)
982
983
{
	struct drm_i915_gem_sw_finish *args = data;
984
	struct drm_i915_gem_object *obj;
985
986
987
988
989
	int ret = 0;

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

990
	ret = i915_mutex_lock_interruptible(dev);
991
	if (ret)
992
		return ret;
993

994
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
995
	if (&obj->base == NULL) {
996
997
		ret = -ENOENT;
		goto unlock;
998
999
1000
	}

	/* Pinned buffers may be scanout, so flush the cache */