i915_gem.c revision 1.1.1.1.2.7 1 /*
2 * Copyright 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric (at) anholt.net>
25 *
26 */
27
28 #include <drm/drmP.h>
29 #include <drm/i915_drm.h>
30 #include "i915_drv.h"
31 #include "i915_trace.h"
32 #include "intel_drv.h"
33 #include <linux/shmem_fs.h>
34 #include <linux/slab.h>
35 #include <linux/swap.h>
36 #include <linux/pci.h>
37 #include <linux/dma-buf.h>
38 #include <linux/errno.h>
39 #include <linux/time.h>
40 #include <linux/err.h>
41 #include <asm/param.h>
42
43 static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
44 static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
45 static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
46 unsigned alignment,
47 bool map_and_fenceable,
48 bool nonblocking);
49 static int i915_gem_phys_pwrite(struct drm_device *dev,
50 struct drm_i915_gem_object *obj,
51 struct drm_i915_gem_pwrite *args,
52 struct drm_file *file);
53
54 static void i915_gem_write_fence(struct drm_device *dev, int reg,
55 struct drm_i915_gem_object *obj);
56 static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
57 struct drm_i915_fence_reg *fence,
58 bool enable);
59
60 static int i915_gem_inactive_shrink(struct shrinker *shrinker,
61 struct shrink_control *sc);
62 static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
63 static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
64 static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
65
66 static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
67 {
68 if (obj->tiling_mode)
69 i915_gem_release_mmap(obj);
70
71 /* As we do not have an associated fence register, we will force
72 * a tiling change if we ever need to acquire one.
73 */
74 obj->fence_dirty = false;
75 obj->fence_reg = I915_FENCE_REG_NONE;
76 }
77
78 /* some bookkeeping */
79 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
80 size_t size)
81 {
82 dev_priv->mm.object_count++;
83 dev_priv->mm.object_memory += size;
84 }
85
86 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
87 size_t size)
88 {
89 dev_priv->mm.object_count--;
90 dev_priv->mm.object_memory -= size;
91 }
92
93 static int
94 i915_gem_wait_for_error(struct drm_device *dev)
95 {
96 struct drm_i915_private *dev_priv = dev->dev_private;
97 struct completion *x = &dev_priv->error_completion;
98 #ifndef __NetBSD__
99 unsigned long flags;
100 #endif
101 int ret;
102
103 if (!atomic_read(&dev_priv->mm.wedged))
104 return 0;
105
106 /*
107 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
108 * userspace. If it takes that long something really bad is going on and
109 * we should simply try to bail out and fail as gracefully as possible.
110 */
111 ret = wait_for_completion_interruptible_timeout(x, 10*HZ);
112 if (ret == 0) {
113 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
114 return -EIO;
115 } else if (ret < 0) {
116 return ret;
117 }
118
119 if (atomic_read(&dev_priv->mm.wedged)) {
120 /* GPU is hung, bump the completion count to account for
121 * the token we just consumed so that we never hit zero and
122 * end up waiting upon a subsequent completion event that
123 * will never happen.
124 */
125 #ifdef __NetBSD__
126 /* XXX Hope it's not a problem that we might wake someone. */
127 complete(x);
128 #else
129 spin_lock_irqsave(&x->wait.lock, flags);
130 x->done++;
131 spin_unlock_irqrestore(&x->wait.lock, flags);
132 #endif
133 }
134 return 0;
135 }
136
137 int i915_mutex_lock_interruptible(struct drm_device *dev)
138 {
139 int ret;
140
141 ret = i915_gem_wait_for_error(dev);
142 if (ret)
143 return ret;
144
145 ret = mutex_lock_interruptible(&dev->struct_mutex);
146 if (ret)
147 return ret;
148
149 WARN_ON(i915_verify_lists(dev));
150 return 0;
151 }
152
153 static inline bool
154 i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
155 {
156 return obj->gtt_space && !obj->active;
157 }
158
159 int
160 i915_gem_init_ioctl(struct drm_device *dev, void *data,
161 struct drm_file *file)
162 {
163 struct drm_i915_gem_init *args = data;
164
165 if (drm_core_check_feature(dev, DRIVER_MODESET))
166 return -ENODEV;
167
168 if (args->gtt_start >= args->gtt_end ||
169 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
170 return -EINVAL;
171
172 /* GEM with user mode setting was never supported on ilk and later. */
173 if (INTEL_INFO(dev)->gen >= 5)
174 return -ENODEV;
175
176 mutex_lock(&dev->struct_mutex);
177 i915_gem_init_global_gtt(dev, args->gtt_start,
178 args->gtt_end, args->gtt_end);
179 mutex_unlock(&dev->struct_mutex);
180
181 return 0;
182 }
183
184 int
185 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
186 struct drm_file *file)
187 {
188 struct drm_i915_private *dev_priv = dev->dev_private;
189 struct drm_i915_gem_get_aperture *args = data;
190 struct drm_i915_gem_object *obj;
191 size_t pinned;
192
193 pinned = 0;
194 mutex_lock(&dev->struct_mutex);
195 list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
196 if (obj->pin_count)
197 pinned += obj->gtt_space->size;
198 mutex_unlock(&dev->struct_mutex);
199
200 args->aper_size = dev_priv->mm.gtt_total;
201 args->aper_available_size = args->aper_size - pinned;
202
203 return 0;
204 }
205
206 static int
207 i915_gem_create(struct drm_file *file,
208 struct drm_device *dev,
209 uint64_t size,
210 uint32_t *handle_p)
211 {
212 struct drm_i915_gem_object *obj;
213 int ret;
214 u32 handle;
215
216 size = roundup(size, PAGE_SIZE);
217 if (size == 0)
218 return -EINVAL;
219
220 /* Allocate the new object */
221 obj = i915_gem_alloc_object(dev, size);
222 if (obj == NULL)
223 return -ENOMEM;
224
225 ret = drm_gem_handle_create(file, &obj->base, &handle);
226 if (ret) {
227 drm_gem_object_release(&obj->base);
228 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
229 kfree(obj);
230 return ret;
231 }
232
233 /* drop reference from allocate - handle holds it now */
234 drm_gem_object_unreference(&obj->base);
235 trace_i915_gem_object_create(obj);
236
237 *handle_p = handle;
238 return 0;
239 }
240
241 int
242 i915_gem_dumb_create(struct drm_file *file,
243 struct drm_device *dev,
244 struct drm_mode_create_dumb *args)
245 {
246 /* have to work out size/pitch and return them */
247 #ifdef __NetBSD__ /* ALIGN already means something. */
248 args->pitch = round_up(args->width * ((args->bpp + 7) / 8), 64);
249 #else
250 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
251 #endif
252 args->size = args->pitch * args->height;
253 return i915_gem_create(file, dev,
254 args->size, &args->handle);
255 }
256
257 int i915_gem_dumb_destroy(struct drm_file *file,
258 struct drm_device *dev,
259 uint32_t handle)
260 {
261 return drm_gem_handle_delete(file, handle);
262 }
263
264 /**
265 * Creates a new mm object and returns a handle to it.
266 */
267 int
268 i915_gem_create_ioctl(struct drm_device *dev, void *data,
269 struct drm_file *file)
270 {
271 struct drm_i915_gem_create *args = data;
272
273 return i915_gem_create(file, dev,
274 args->size, &args->handle);
275 }
276
277 static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
278 {
279 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
280
281 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
282 obj->tiling_mode != I915_TILING_NONE;
283 }
284
285 static inline int
286 __copy_to_user_swizzled(char __user *cpu_vaddr,
287 const char *gpu_vaddr, int gpu_offset,
288 int length)
289 {
290 int ret, cpu_offset = 0;
291
292 while (length > 0) {
293 #ifdef __NetBSD__
294 int cacheline_end = round_up(gpu_offset + 1, 64);
295 #else
296 int cacheline_end = ALIGN(gpu_offset + 1, 64);
297 #endif
298 int this_length = min(cacheline_end - gpu_offset, length);
299 int swizzled_gpu_offset = gpu_offset ^ 64;
300
301 ret = __copy_to_user(cpu_vaddr + cpu_offset,
302 gpu_vaddr + swizzled_gpu_offset,
303 this_length);
304 if (ret)
305 return ret + length;
306
307 cpu_offset += this_length;
308 gpu_offset += this_length;
309 length -= this_length;
310 }
311
312 return 0;
313 }
314
315 static inline int
316 __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
317 const char __user *cpu_vaddr,
318 int length)
319 {
320 int ret, cpu_offset = 0;
321
322 while (length > 0) {
323 #ifdef __NetBSD__
324 int cacheline_end = round_up(gpu_offset + 1, 64);
325 #else
326 int cacheline_end = ALIGN(gpu_offset + 1, 64);
327 #endif
328 int this_length = min(cacheline_end - gpu_offset, length);
329 int swizzled_gpu_offset = gpu_offset ^ 64;
330
331 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
332 cpu_vaddr + cpu_offset,
333 this_length);
334 if (ret)
335 return ret + length;
336
337 cpu_offset += this_length;
338 gpu_offset += this_length;
339 length -= this_length;
340 }
341
342 return 0;
343 }
344
345 /* Per-page copy function for the shmem pread fastpath.
346 * Flushes invalid cachelines before reading the target if
347 * needs_clflush is set. */
348 static int
349 shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
350 char __user *user_data,
351 bool page_do_bit17_swizzling, bool needs_clflush)
352 {
353 char *vaddr;
354 int ret;
355
356 if (unlikely(page_do_bit17_swizzling))
357 return -EINVAL;
358
359 vaddr = kmap_atomic(page);
360 if (needs_clflush)
361 drm_clflush_virt_range(vaddr + shmem_page_offset,
362 page_length);
363 ret = __copy_to_user_inatomic(user_data,
364 vaddr + shmem_page_offset,
365 page_length);
366 kunmap_atomic(vaddr);
367
368 return ret ? -EFAULT : 0;
369 }
370
371 static void
372 shmem_clflush_swizzled_range(char *addr, unsigned long length,
373 bool swizzled)
374 {
375 if (unlikely(swizzled)) {
376 unsigned long start = (unsigned long) addr;
377 unsigned long end = (unsigned long) addr + length;
378
379 /* For swizzling simply ensure that we always flush both
380 * channels. Lame, but simple and it works. Swizzled
381 * pwrite/pread is far from a hotpath - current userspace
382 * doesn't use it at all. */
383 start = round_down(start, 128);
384 end = round_up(end, 128);
385
386 drm_clflush_virt_range((void *)start, end - start);
387 } else {
388 drm_clflush_virt_range(addr, length);
389 }
390
391 }
392
393 /* Only difference to the fast-path function is that this can handle bit17
394 * and uses non-atomic copy and kmap functions. */
395 static int
396 shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
397 char __user *user_data,
398 bool page_do_bit17_swizzling, bool needs_clflush)
399 {
400 char *vaddr;
401 int ret;
402
403 vaddr = kmap(page);
404 if (needs_clflush)
405 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
406 page_length,
407 page_do_bit17_swizzling);
408
409 if (page_do_bit17_swizzling)
410 ret = __copy_to_user_swizzled(user_data,
411 vaddr, shmem_page_offset,
412 page_length);
413 else
414 ret = __copy_to_user(user_data,
415 vaddr + shmem_page_offset,
416 page_length);
417 kunmap(page);
418
419 return ret ? - EFAULT : 0;
420 }
421
422 static int
423 i915_gem_shmem_pread(struct drm_device *dev,
424 struct drm_i915_gem_object *obj,
425 struct drm_i915_gem_pread *args,
426 struct drm_file *file)
427 {
428 char __user *user_data;
429 ssize_t remain;
430 loff_t offset;
431 int shmem_page_offset, page_length, ret = 0;
432 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
433 int hit_slowpath = 0;
434 int prefaulted = 0;
435 int needs_clflush = 0;
436 struct scatterlist *sg;
437 int i;
438
439 user_data = (char __user *) (uintptr_t) args->data_ptr;
440 remain = args->size;
441
442 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
443
444 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
445 /* If we're not in the cpu read domain, set ourself into the gtt
446 * read domain and manually flush cachelines (if required). This
447 * optimizes for the case when the gpu will dirty the data
448 * anyway again before the next pread happens. */
449 if (obj->cache_level == I915_CACHE_NONE)
450 needs_clflush = 1;
451 if (obj->gtt_space) {
452 ret = i915_gem_object_set_to_gtt_domain(obj, false);
453 if (ret)
454 return ret;
455 }
456 }
457
458 ret = i915_gem_object_get_pages(obj);
459 if (ret)
460 return ret;
461
462 i915_gem_object_pin_pages(obj);
463
464 offset = args->offset;
465
466 for_each_sg(obj->pages->sgl, sg, obj->pages->nents, i) {
467 struct page *page;
468
469 if (i < offset >> PAGE_SHIFT)
470 continue;
471
472 if (remain <= 0)
473 break;
474
475 /* Operation in this page
476 *
477 * shmem_page_offset = offset within page in shmem file
478 * page_length = bytes to copy for this page
479 */
480 shmem_page_offset = offset_in_page(offset);
481 page_length = remain;
482 if ((shmem_page_offset + page_length) > PAGE_SIZE)
483 page_length = PAGE_SIZE - shmem_page_offset;
484
485 page = sg_page(sg);
486 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
487 (page_to_phys(page) & (1 << 17)) != 0;
488
489 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
490 user_data, page_do_bit17_swizzling,
491 needs_clflush);
492 if (ret == 0)
493 goto next_page;
494
495 hit_slowpath = 1;
496 mutex_unlock(&dev->struct_mutex);
497
498 if (!prefaulted) {
499 ret = fault_in_multipages_writeable(user_data, remain);
500 /* Userspace is tricking us, but we've already clobbered
501 * its pages with the prefault and promised to write the
502 * data up to the first fault. Hence ignore any errors
503 * and just continue. */
504 (void)ret;
505 prefaulted = 1;
506 }
507
508 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
509 user_data, page_do_bit17_swizzling,
510 needs_clflush);
511
512 mutex_lock(&dev->struct_mutex);
513
514 next_page:
515 mark_page_accessed(page);
516
517 if (ret)
518 goto out;
519
520 remain -= page_length;
521 user_data += page_length;
522 offset += page_length;
523 }
524
525 out:
526 i915_gem_object_unpin_pages(obj);
527
528 if (hit_slowpath) {
529 /* Fixup: Kill any reinstated backing storage pages */
530 if (obj->madv == __I915_MADV_PURGED)
531 i915_gem_object_truncate(obj);
532 }
533
534 return ret;
535 }
536
537 /**
538 * Reads data from the object referenced by handle.
539 *
540 * On error, the contents of *data are undefined.
541 */
542 int
543 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
544 struct drm_file *file)
545 {
546 struct drm_i915_gem_pread *args = data;
547 struct drm_i915_gem_object *obj;
548 int ret = 0;
549
550 if (args->size == 0)
551 return 0;
552
553 if (!access_ok(VERIFY_WRITE,
554 (char __user *)(uintptr_t)args->data_ptr,
555 args->size))
556 return -EFAULT;
557
558 ret = i915_mutex_lock_interruptible(dev);
559 if (ret)
560 return ret;
561
562 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
563 if (&obj->base == NULL) {
564 ret = -ENOENT;
565 goto unlock;
566 }
567
568 /* Bounds check source. */
569 if (args->offset > obj->base.size ||
570 args->size > obj->base.size - args->offset) {
571 ret = -EINVAL;
572 goto out;
573 }
574
575 /* prime objects have no backing filp to GEM pread/pwrite
576 * pages from.
577 */
578 if (!obj->base.filp) {
579 ret = -EINVAL;
580 goto out;
581 }
582
583 trace_i915_gem_object_pread(obj, args->offset, args->size);
584
585 ret = i915_gem_shmem_pread(dev, obj, args, file);
586
587 out:
588 drm_gem_object_unreference(&obj->base);
589 unlock:
590 mutex_unlock(&dev->struct_mutex);
591 return ret;
592 }
593
594 /* This is the fast write path which cannot handle
595 * page faults in the source data
596 */
597
598 static inline int
599 fast_user_write(struct io_mapping *mapping,
600 loff_t page_base, int page_offset,
601 char __user *user_data,
602 int length)
603 {
604 void __iomem *vaddr_atomic;
605 void *vaddr;
606 unsigned long unwritten;
607
608 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
609 /* We can use the cpu mem copy function because this is X86. */
610 vaddr = (void __force*)vaddr_atomic + page_offset;
611 unwritten = __copy_from_user_inatomic_nocache(vaddr,
612 user_data, length);
613 io_mapping_unmap_atomic(vaddr_atomic);
614 return unwritten;
615 }
616
617 /**
618 * This is the fast pwrite path, where we copy the data directly from the
619 * user into the GTT, uncached.
620 */
621 static int
622 i915_gem_gtt_pwrite_fast(struct drm_device *dev,
623 struct drm_i915_gem_object *obj,
624 struct drm_i915_gem_pwrite *args,
625 struct drm_file *file)
626 {
627 drm_i915_private_t *dev_priv = dev->dev_private;
628 ssize_t remain;
629 loff_t offset, page_base;
630 char __user *user_data;
631 int page_offset, page_length, ret;
632
633 ret = i915_gem_object_pin(obj, 0, true, true);
634 if (ret)
635 goto out;
636
637 ret = i915_gem_object_set_to_gtt_domain(obj, true);
638 if (ret)
639 goto out_unpin;
640
641 ret = i915_gem_object_put_fence(obj);
642 if (ret)
643 goto out_unpin;
644
645 user_data = (char __user *) (uintptr_t) args->data_ptr;
646 remain = args->size;
647
648 offset = obj->gtt_offset + args->offset;
649
650 while (remain > 0) {
651 /* Operation in this page
652 *
653 * page_base = page offset within aperture
654 * page_offset = offset within page
655 * page_length = bytes to copy for this page
656 */
657 page_base = offset & PAGE_MASK;
658 page_offset = offset_in_page(offset);
659 page_length = remain;
660 if ((page_offset + remain) > PAGE_SIZE)
661 page_length = PAGE_SIZE - page_offset;
662
663 /* If we get a fault while copying data, then (presumably) our
664 * source page isn't available. Return the error and we'll
665 * retry in the slow path.
666 */
667 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
668 page_offset, user_data, page_length)) {
669 ret = -EFAULT;
670 goto out_unpin;
671 }
672
673 remain -= page_length;
674 user_data += page_length;
675 offset += page_length;
676 }
677
678 out_unpin:
679 i915_gem_object_unpin(obj);
680 out:
681 return ret;
682 }
683
684 /* Per-page copy function for the shmem pwrite fastpath.
685 * Flushes invalid cachelines before writing to the target if
686 * needs_clflush_before is set and flushes out any written cachelines after
687 * writing if needs_clflush is set. */
688 static int
689 shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
690 char __user *user_data,
691 bool page_do_bit17_swizzling,
692 bool needs_clflush_before,
693 bool needs_clflush_after)
694 {
695 char *vaddr;
696 int ret;
697
698 if (unlikely(page_do_bit17_swizzling))
699 return -EINVAL;
700
701 vaddr = kmap_atomic(page);
702 if (needs_clflush_before)
703 drm_clflush_virt_range(vaddr + shmem_page_offset,
704 page_length);
705 ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
706 user_data,
707 page_length);
708 if (needs_clflush_after)
709 drm_clflush_virt_range(vaddr + shmem_page_offset,
710 page_length);
711 kunmap_atomic(vaddr);
712
713 return ret ? -EFAULT : 0;
714 }
715
716 /* Only difference to the fast-path function is that this can handle bit17
717 * and uses non-atomic copy and kmap functions. */
718 static int
719 shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
720 char __user *user_data,
721 bool page_do_bit17_swizzling,
722 bool needs_clflush_before,
723 bool needs_clflush_after)
724 {
725 char *vaddr;
726 int ret;
727
728 vaddr = kmap(page);
729 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
730 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
731 page_length,
732 page_do_bit17_swizzling);
733 if (page_do_bit17_swizzling)
734 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
735 user_data,
736 page_length);
737 else
738 ret = __copy_from_user(vaddr + shmem_page_offset,
739 user_data,
740 page_length);
741 if (needs_clflush_after)
742 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
743 page_length,
744 page_do_bit17_swizzling);
745 kunmap(page);
746
747 return ret ? -EFAULT : 0;
748 }
749
750 static int
751 i915_gem_shmem_pwrite(struct drm_device *dev,
752 struct drm_i915_gem_object *obj,
753 struct drm_i915_gem_pwrite *args,
754 struct drm_file *file)
755 {
756 ssize_t remain;
757 loff_t offset;
758 char __user *user_data;
759 int shmem_page_offset, page_length, ret = 0;
760 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
761 int hit_slowpath = 0;
762 int needs_clflush_after = 0;
763 int needs_clflush_before = 0;
764 int i;
765 struct scatterlist *sg;
766
767 user_data = (char __user *) (uintptr_t) args->data_ptr;
768 remain = args->size;
769
770 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
771
772 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
773 /* If we're not in the cpu write domain, set ourself into the gtt
774 * write domain and manually flush cachelines (if required). This
775 * optimizes for the case when the gpu will use the data
776 * right away and we therefore have to clflush anyway. */
777 if (obj->cache_level == I915_CACHE_NONE)
778 needs_clflush_after = 1;
779 if (obj->gtt_space) {
780 ret = i915_gem_object_set_to_gtt_domain(obj, true);
781 if (ret)
782 return ret;
783 }
784 }
785 /* Same trick applies for invalidate partially written cachelines before
786 * writing. */
787 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
788 && obj->cache_level == I915_CACHE_NONE)
789 needs_clflush_before = 1;
790
791 ret = i915_gem_object_get_pages(obj);
792 if (ret)
793 return ret;
794
795 i915_gem_object_pin_pages(obj);
796
797 offset = args->offset;
798 obj->dirty = 1;
799
800 for_each_sg(obj->pages->sgl, sg, obj->pages->nents, i) {
801 struct page *page;
802 int partial_cacheline_write;
803
804 if (i < offset >> PAGE_SHIFT)
805 continue;
806
807 if (remain <= 0)
808 break;
809
810 /* Operation in this page
811 *
812 * shmem_page_offset = offset within page in shmem file
813 * page_length = bytes to copy for this page
814 */
815 shmem_page_offset = offset_in_page(offset);
816
817 page_length = remain;
818 if ((shmem_page_offset + page_length) > PAGE_SIZE)
819 page_length = PAGE_SIZE - shmem_page_offset;
820
821 /* If we don't overwrite a cacheline completely we need to be
822 * careful to have up-to-date data by first clflushing. Don't
823 * overcomplicate things and flush the entire patch. */
824 partial_cacheline_write = needs_clflush_before &&
825 ((shmem_page_offset | page_length)
826 & (boot_cpu_data.x86_clflush_size - 1));
827
828 page = sg_page(sg);
829 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
830 (page_to_phys(page) & (1 << 17)) != 0;
831
832 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
833 user_data, page_do_bit17_swizzling,
834 partial_cacheline_write,
835 needs_clflush_after);
836 if (ret == 0)
837 goto next_page;
838
839 hit_slowpath = 1;
840 mutex_unlock(&dev->struct_mutex);
841 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
842 user_data, page_do_bit17_swizzling,
843 partial_cacheline_write,
844 needs_clflush_after);
845
846 mutex_lock(&dev->struct_mutex);
847
848 next_page:
849 set_page_dirty(page);
850 mark_page_accessed(page);
851
852 if (ret)
853 goto out;
854
855 remain -= page_length;
856 user_data += page_length;
857 offset += page_length;
858 }
859
860 out:
861 i915_gem_object_unpin_pages(obj);
862
863 if (hit_slowpath) {
864 /* Fixup: Kill any reinstated backing storage pages */
865 if (obj->madv == __I915_MADV_PURGED)
866 i915_gem_object_truncate(obj);
867 /* and flush dirty cachelines in case the object isn't in the cpu write
868 * domain anymore. */
869 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
870 i915_gem_clflush_object(obj);
871 i915_gem_chipset_flush(dev);
872 }
873 }
874
875 if (needs_clflush_after)
876 i915_gem_chipset_flush(dev);
877
878 return ret;
879 }
880
881 /**
882 * Writes data to the object referenced by handle.
883 *
884 * On error, the contents of the buffer that were to be modified are undefined.
885 */
886 int
887 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
888 struct drm_file *file)
889 {
890 struct drm_i915_gem_pwrite *args = data;
891 struct drm_i915_gem_object *obj;
892 int ret;
893
894 if (args->size == 0)
895 return 0;
896
897 if (!access_ok(VERIFY_READ,
898 (char __user *)(uintptr_t)args->data_ptr,
899 args->size))
900 return -EFAULT;
901
902 ret = fault_in_multipages_readable((char __user *)(uintptr_t)args->data_ptr,
903 args->size);
904 if (ret)
905 return -EFAULT;
906
907 ret = i915_mutex_lock_interruptible(dev);
908 if (ret)
909 return ret;
910
911 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
912 if (&obj->base == NULL) {
913 ret = -ENOENT;
914 goto unlock;
915 }
916
917 /* Bounds check destination. */
918 if (args->offset > obj->base.size ||
919 args->size > obj->base.size - args->offset) {
920 ret = -EINVAL;
921 goto out;
922 }
923
924 /* prime objects have no backing filp to GEM pread/pwrite
925 * pages from.
926 */
927 if (!obj->base.filp) {
928 ret = -EINVAL;
929 goto out;
930 }
931
932 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
933
934 ret = -EFAULT;
935 /* We can only do the GTT pwrite on untiled buffers, as otherwise
936 * it would end up going through the fenced access, and we'll get
937 * different detiling behavior between reading and writing.
938 * pread/pwrite currently are reading and writing from the CPU
939 * perspective, requiring manual detiling by the client.
940 */
941 if (obj->phys_obj) {
942 ret = i915_gem_phys_pwrite(dev, obj, args, file);
943 goto out;
944 }
945
946 if (obj->cache_level == I915_CACHE_NONE &&
947 obj->tiling_mode == I915_TILING_NONE &&
948 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
949 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
950 /* Note that the gtt paths might fail with non-page-backed user
951 * pointers (e.g. gtt mappings when moving data between
952 * textures). Fallback to the shmem path in that case. */
953 }
954
955 if (ret == -EFAULT || ret == -ENOSPC)
956 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
957
958 out:
959 drm_gem_object_unreference(&obj->base);
960 unlock:
961 mutex_unlock(&dev->struct_mutex);
962 return ret;
963 }
964
965 int
966 i915_gem_check_wedge(struct drm_i915_private *dev_priv,
967 bool interruptible)
968 {
969 if (atomic_read(&dev_priv->mm.wedged)) {
970 struct completion *x = &dev_priv->error_completion;
971 bool recovery_complete;
972 unsigned long flags;
973
974 /* Give the error handler a chance to run. */
975 spin_lock_irqsave(&x->wait.lock, flags);
976 recovery_complete = x->done > 0;
977 spin_unlock_irqrestore(&x->wait.lock, flags);
978
979 /* Non-interruptible callers can't handle -EAGAIN, hence return
980 * -EIO unconditionally for these. */
981 if (!interruptible)
982 return -EIO;
983
984 /* Recovery complete, but still wedged means reset failure. */
985 if (recovery_complete)
986 return -EIO;
987
988 return -EAGAIN;
989 }
990
991 return 0;
992 }
993
994 /*
995 * Compare seqno against outstanding lazy request. Emit a request if they are
996 * equal.
997 */
998 static int
999 i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
1000 {
1001 int ret;
1002
1003 BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
1004
1005 ret = 0;
1006 if (seqno == ring->outstanding_lazy_request)
1007 ret = i915_add_request(ring, NULL, NULL);
1008
1009 return ret;
1010 }
1011
1012 /**
1013 * __wait_seqno - wait until execution of seqno has finished
1014 * @ring: the ring expected to report seqno
1015 * @seqno: duh!
1016 * @interruptible: do an interruptible wait (normally yes)
1017 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
1018 *
1019 * Returns 0 if the seqno was found within the alloted time. Else returns the
1020 * errno with remaining time filled in timeout argument.
1021 */
1022 static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
1023 bool interruptible, struct timespec *timeout)
1024 {
1025 drm_i915_private_t *dev_priv = ring->dev->dev_private;
1026 struct timespec before, now, wait_time={1,0};
1027 unsigned long timeout_jiffies;
1028 long end;
1029 bool wait_forever = true;
1030 int ret;
1031
1032 if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
1033 return 0;
1034
1035 trace_i915_gem_request_wait_begin(ring, seqno);
1036
1037 if (timeout != NULL) {
1038 wait_time = *timeout;
1039 wait_forever = false;
1040 }
1041
1042 timeout_jiffies = timespec_to_jiffies(&wait_time);
1043
1044 if (WARN_ON(!ring->irq_get(ring)))
1045 return -ENODEV;
1046
1047 /* Record current time in case interrupted by signal, or wedged * */
1048 getrawmonotonic(&before);
1049
1050 #define EXIT_COND \
1051 (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
1052 atomic_read(&dev_priv->mm.wedged))
1053 do {
1054 #ifdef __NetBSD__
1055 /*
1056 * XXX This wait is always interruptible; we should
1057 * heed the flag `interruptible'.
1058 */
1059 DRM_TIMED_WAIT_UNTIL(end, &ring->irq_queue, &drm_global_mutex,
1060 timeout_jiffies,
1061 EXIT_COND);
1062 #else
1063 if (interruptible)
1064 end = wait_event_interruptible_timeout(ring->irq_queue,
1065 EXIT_COND,
1066 timeout_jiffies);
1067 else
1068 end = wait_event_timeout(ring->irq_queue, EXIT_COND,
1069 timeout_jiffies);
1070
1071 #endif
1072 ret = i915_gem_check_wedge(dev_priv, interruptible);
1073 if (ret)
1074 end = ret;
1075 } while (end == 0 && wait_forever);
1076
1077 getrawmonotonic(&now);
1078
1079 ring->irq_put(ring);
1080 trace_i915_gem_request_wait_end(ring, seqno);
1081 #undef EXIT_COND
1082
1083 if (timeout) {
1084 struct timespec sleep_time = timespec_sub(now, before);
1085 *timeout = timespec_sub(*timeout, sleep_time);
1086 }
1087
1088 switch (end) {
1089 case -EIO:
1090 case -EAGAIN: /* Wedged */
1091 case -ERESTARTSYS: /* Signal */
1092 return (int)end;
1093 case 0: /* Timeout */
1094 if (timeout)
1095 set_normalized_timespec(timeout, 0, 0);
1096 return -ETIME;
1097 default: /* Completed */
1098 WARN_ON(end < 0); /* We're not aware of other errors */
1099 return 0;
1100 }
1101 }
1102
1103 /**
1104 * Waits for a sequence number to be signaled, and cleans up the
1105 * request and object lists appropriately for that event.
1106 */
1107 int
1108 i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
1109 {
1110 struct drm_device *dev = ring->dev;
1111 struct drm_i915_private *dev_priv = dev->dev_private;
1112 bool interruptible = dev_priv->mm.interruptible;
1113 int ret;
1114
1115 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1116 BUG_ON(seqno == 0);
1117
1118 ret = i915_gem_check_wedge(dev_priv, interruptible);
1119 if (ret)
1120 return ret;
1121
1122 ret = i915_gem_check_olr(ring, seqno);
1123 if (ret)
1124 return ret;
1125
1126 return __wait_seqno(ring, seqno, interruptible, NULL);
1127 }
1128
1129 /**
1130 * Ensures that all rendering to the object has completed and the object is
1131 * safe to unbind from the GTT or access from the CPU.
1132 */
1133 static __must_check int
1134 i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1135 bool readonly)
1136 {
1137 struct intel_ring_buffer *ring = obj->ring;
1138 u32 seqno;
1139 int ret;
1140
1141 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1142 if (seqno == 0)
1143 return 0;
1144
1145 ret = i915_wait_seqno(ring, seqno);
1146 if (ret)
1147 return ret;
1148
1149 i915_gem_retire_requests_ring(ring);
1150
1151 /* Manually manage the write flush as we may have not yet
1152 * retired the buffer.
1153 */
1154 if (obj->last_write_seqno &&
1155 i915_seqno_passed(seqno, obj->last_write_seqno)) {
1156 obj->last_write_seqno = 0;
1157 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1158 }
1159
1160 return 0;
1161 }
1162
1163 /* A nonblocking variant of the above wait. This is a highly dangerous routine
1164 * as the object state may change during this call.
1165 */
1166 static __must_check int
1167 i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
1168 bool readonly)
1169 {
1170 struct drm_device *dev = obj->base.dev;
1171 struct drm_i915_private *dev_priv = dev->dev_private;
1172 struct intel_ring_buffer *ring = obj->ring;
1173 u32 seqno;
1174 int ret;
1175
1176 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1177 BUG_ON(!dev_priv->mm.interruptible);
1178
1179 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1180 if (seqno == 0)
1181 return 0;
1182
1183 ret = i915_gem_check_wedge(dev_priv, true);
1184 if (ret)
1185 return ret;
1186
1187 ret = i915_gem_check_olr(ring, seqno);
1188 if (ret)
1189 return ret;
1190
1191 mutex_unlock(&dev->struct_mutex);
1192 ret = __wait_seqno(ring, seqno, true, NULL);
1193 mutex_lock(&dev->struct_mutex);
1194
1195 i915_gem_retire_requests_ring(ring);
1196
1197 /* Manually manage the write flush as we may have not yet
1198 * retired the buffer.
1199 */
1200 if (obj->last_write_seqno &&
1201 i915_seqno_passed(seqno, obj->last_write_seqno)) {
1202 obj->last_write_seqno = 0;
1203 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1204 }
1205
1206 return ret;
1207 }
1208
1209 /**
1210 * Called when user space prepares to use an object with the CPU, either
1211 * through the mmap ioctl's mapping or a GTT mapping.
1212 */
1213 int
1214 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1215 struct drm_file *file)
1216 {
1217 struct drm_i915_gem_set_domain *args = data;
1218 struct drm_i915_gem_object *obj;
1219 uint32_t read_domains = args->read_domains;
1220 uint32_t write_domain = args->write_domain;
1221 int ret;
1222
1223 /* Only handle setting domains to types used by the CPU. */
1224 if (write_domain & I915_GEM_GPU_DOMAINS)
1225 return -EINVAL;
1226
1227 if (read_domains & I915_GEM_GPU_DOMAINS)
1228 return -EINVAL;
1229
1230 /* Having something in the write domain implies it's in the read
1231 * domain, and only that read domain. Enforce that in the request.
1232 */
1233 if (write_domain != 0 && read_domains != write_domain)
1234 return -EINVAL;
1235
1236 ret = i915_mutex_lock_interruptible(dev);
1237 if (ret)
1238 return ret;
1239
1240 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1241 if (&obj->base == NULL) {
1242 ret = -ENOENT;
1243 goto unlock;
1244 }
1245
1246 /* Try to flush the object off the GPU without holding the lock.
1247 * We will repeat the flush holding the lock in the normal manner
1248 * to catch cases where we are gazumped.
1249 */
1250 ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain);
1251 if (ret)
1252 goto unref;
1253
1254 if (read_domains & I915_GEM_DOMAIN_GTT) {
1255 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1256
1257 /* Silently promote "you're not bound, there was nothing to do"
1258 * to success, since the client was just asking us to
1259 * make sure everything was done.
1260 */
1261 if (ret == -EINVAL)
1262 ret = 0;
1263 } else {
1264 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1265 }
1266
1267 unref:
1268 drm_gem_object_unreference(&obj->base);
1269 unlock:
1270 mutex_unlock(&dev->struct_mutex);
1271 return ret;
1272 }
1273
1274 /**
1275 * Called when user space has done writes to this buffer
1276 */
1277 int
1278 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1279 struct drm_file *file)
1280 {
1281 struct drm_i915_gem_sw_finish *args = data;
1282 struct drm_i915_gem_object *obj;
1283 int ret = 0;
1284
1285 ret = i915_mutex_lock_interruptible(dev);
1286 if (ret)
1287 return ret;
1288
1289 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1290 if (&obj->base == NULL) {
1291 ret = -ENOENT;
1292 goto unlock;
1293 }
1294
1295 /* Pinned buffers may be scanout, so flush the cache */
1296 if (obj->pin_count)
1297 i915_gem_object_flush_cpu_write_domain(obj);
1298
1299 drm_gem_object_unreference(&obj->base);
1300 unlock:
1301 mutex_unlock(&dev->struct_mutex);
1302 return ret;
1303 }
1304
1305 /**
1306 * Maps the contents of an object, returning the address it is mapped
1307 * into.
1308 *
1309 * While the mapping holds a reference on the contents of the object, it doesn't
1310 * imply a ref on the object itself.
1311 */
1312 int
1313 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1314 struct drm_file *file)
1315 {
1316 struct drm_i915_gem_mmap *args = data;
1317 struct drm_gem_object *obj;
1318 unsigned long addr;
1319
1320 obj = drm_gem_object_lookup(dev, file, args->handle);
1321 if (obj == NULL)
1322 return -ENOENT;
1323
1324 /* prime objects have no backing filp to GEM mmap
1325 * pages from.
1326 */
1327 if (!obj->filp) {
1328 drm_gem_object_unreference_unlocked(obj);
1329 return -EINVAL;
1330 }
1331
1332 addr = vm_mmap(obj->filp, 0, args->size,
1333 PROT_READ | PROT_WRITE, MAP_SHARED,
1334 args->offset);
1335 drm_gem_object_unreference_unlocked(obj);
1336 if (IS_ERR((void *)addr))
1337 return addr;
1338
1339 args->addr_ptr = (uint64_t) addr;
1340
1341 return 0;
1342 }
1343
1344 /**
1345 * i915_gem_fault - fault a page into the GTT
1346 * vma: VMA in question
1347 * vmf: fault info
1348 *
1349 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1350 * from userspace. The fault handler takes care of binding the object to
1351 * the GTT (if needed), allocating and programming a fence register (again,
1352 * only if needed based on whether the old reg is still valid or the object
1353 * is tiled) and inserting a new PTE into the faulting process.
1354 *
1355 * Note that the faulting process may involve evicting existing objects
1356 * from the GTT and/or fence registers to make room. So performance may
1357 * suffer if the GTT working set is large or there are few fence registers
1358 * left.
1359 */
1360 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1361 {
1362 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1363 struct drm_device *dev = obj->base.dev;
1364 drm_i915_private_t *dev_priv = dev->dev_private;
1365 pgoff_t page_offset;
1366 unsigned long pfn;
1367 int ret = 0;
1368 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1369
1370 /* We don't use vmf->pgoff since that has the fake offset */
1371 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1372 PAGE_SHIFT;
1373
1374 ret = i915_mutex_lock_interruptible(dev);
1375 if (ret)
1376 goto out;
1377
1378 trace_i915_gem_object_fault(obj, page_offset, true, write);
1379
1380 /* Now bind it into the GTT if needed */
1381 ret = i915_gem_object_pin(obj, 0, true, false);
1382 if (ret)
1383 goto unlock;
1384
1385 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1386 if (ret)
1387 goto unpin;
1388
1389 ret = i915_gem_object_get_fence(obj);
1390 if (ret)
1391 goto unpin;
1392
1393 obj->fault_mappable = true;
1394
1395 pfn = ((dev_priv->mm.gtt_base_addr + obj->gtt_offset) >> PAGE_SHIFT) +
1396 page_offset;
1397
1398 /* Finally, remap it using the new GTT offset */
1399 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1400 unpin:
1401 i915_gem_object_unpin(obj);
1402 unlock:
1403 mutex_unlock(&dev->struct_mutex);
1404 out:
1405 switch (ret) {
1406 case -EIO:
1407 /* If this -EIO is due to a gpu hang, give the reset code a
1408 * chance to clean up the mess. Otherwise return the proper
1409 * SIGBUS. */
1410 if (!atomic_read(&dev_priv->mm.wedged))
1411 return VM_FAULT_SIGBUS;
1412 case -EAGAIN:
1413 /* Give the error handler a chance to run and move the
1414 * objects off the GPU active list. Next time we service the
1415 * fault, we should be able to transition the page into the
1416 * GTT without touching the GPU (and so avoid further
1417 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1418 * with coherency, just lost writes.
1419 */
1420 set_need_resched();
1421 case 0:
1422 case -ERESTARTSYS:
1423 case -EINTR:
1424 case -EBUSY:
1425 /*
1426 * EBUSY is ok: this just means that another thread
1427 * already did the job.
1428 */
1429 return VM_FAULT_NOPAGE;
1430 case -ENOMEM:
1431 return VM_FAULT_OOM;
1432 case -ENOSPC:
1433 return VM_FAULT_SIGBUS;
1434 default:
1435 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
1436 return VM_FAULT_SIGBUS;
1437 }
1438 }
1439
1440 /**
1441 * i915_gem_release_mmap - remove physical page mappings
1442 * @obj: obj in question
1443 *
1444 * Preserve the reservation of the mmapping with the DRM core code, but
1445 * relinquish ownership of the pages back to the system.
1446 *
1447 * It is vital that we remove the page mapping if we have mapped a tiled
1448 * object through the GTT and then lose the fence register due to
1449 * resource pressure. Similarly if the object has been moved out of the
1450 * aperture, than pages mapped into userspace must be revoked. Removing the
1451 * mapping will then trigger a page fault on the next user access, allowing
1452 * fixup by i915_gem_fault().
1453 */
1454 void
1455 i915_gem_release_mmap(struct drm_i915_gem_object *obj)
1456 {
1457 if (!obj->fault_mappable)
1458 return;
1459
1460 if (obj->base.dev->dev_mapping)
1461 unmap_mapping_range(obj->base.dev->dev_mapping,
1462 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1463 obj->base.size, 1);
1464
1465 obj->fault_mappable = false;
1466 }
1467
1468 static uint32_t
1469 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
1470 {
1471 uint32_t gtt_size;
1472
1473 if (INTEL_INFO(dev)->gen >= 4 ||
1474 tiling_mode == I915_TILING_NONE)
1475 return size;
1476
1477 /* Previous chips need a power-of-two fence region when tiling */
1478 if (INTEL_INFO(dev)->gen == 3)
1479 gtt_size = 1024*1024;
1480 else
1481 gtt_size = 512*1024;
1482
1483 while (gtt_size < size)
1484 gtt_size <<= 1;
1485
1486 return gtt_size;
1487 }
1488
1489 /**
1490 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1491 * @obj: object to check
1492 *
1493 * Return the required GTT alignment for an object, taking into account
1494 * potential fence register mapping.
1495 */
1496 static uint32_t
1497 i915_gem_get_gtt_alignment(struct drm_device *dev,
1498 uint32_t size,
1499 int tiling_mode)
1500 {
1501 /*
1502 * Minimum alignment is 4k (GTT page size), but might be greater
1503 * if a fence register is needed for the object.
1504 */
1505 if (INTEL_INFO(dev)->gen >= 4 ||
1506 tiling_mode == I915_TILING_NONE)
1507 return 4096;
1508
1509 /*
1510 * Previous chips need to be aligned to the size of the smallest
1511 * fence register that can contain the object.
1512 */
1513 return i915_gem_get_gtt_size(dev, size, tiling_mode);
1514 }
1515
1516 /**
1517 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1518 * unfenced object
1519 * @dev: the device
1520 * @size: size of the object
1521 * @tiling_mode: tiling mode of the object
1522 *
1523 * Return the required GTT alignment for an object, only taking into account
1524 * unfenced tiled surface requirements.
1525 */
1526 uint32_t
1527 i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1528 uint32_t size,
1529 int tiling_mode)
1530 {
1531 /*
1532 * Minimum alignment is 4k (GTT page size) for sane hw.
1533 */
1534 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
1535 tiling_mode == I915_TILING_NONE)
1536 return 4096;
1537
1538 /* Previous hardware however needs to be aligned to a power-of-two
1539 * tile height. The simplest method for determining this is to reuse
1540 * the power-of-tile object size.
1541 */
1542 return i915_gem_get_gtt_size(dev, size, tiling_mode);
1543 }
1544
1545 static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
1546 {
1547 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1548 int ret;
1549
1550 if (obj->base.map_list.map)
1551 return 0;
1552
1553 dev_priv->mm.shrinker_no_lock_stealing = true;
1554
1555 ret = drm_gem_create_mmap_offset(&obj->base);
1556 if (ret != -ENOSPC)
1557 goto out;
1558
1559 /* Badly fragmented mmap space? The only way we can recover
1560 * space is by destroying unwanted objects. We can't randomly release
1561 * mmap_offsets as userspace expects them to be persistent for the
1562 * lifetime of the objects. The closest we can is to release the
1563 * offsets on purgeable objects by truncating it and marking it purged,
1564 * which prevents userspace from ever using that object again.
1565 */
1566 i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
1567 ret = drm_gem_create_mmap_offset(&obj->base);
1568 if (ret != -ENOSPC)
1569 goto out;
1570
1571 i915_gem_shrink_all(dev_priv);
1572 ret = drm_gem_create_mmap_offset(&obj->base);
1573 out:
1574 dev_priv->mm.shrinker_no_lock_stealing = false;
1575
1576 return ret;
1577 }
1578
1579 static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
1580 {
1581 if (!obj->base.map_list.map)
1582 return;
1583
1584 drm_gem_free_mmap_offset(&obj->base);
1585 }
1586
1587 int
1588 i915_gem_mmap_gtt(struct drm_file *file,
1589 struct drm_device *dev,
1590 uint32_t handle,
1591 uint64_t *offset)
1592 {
1593 struct drm_i915_private *dev_priv = dev->dev_private;
1594 struct drm_i915_gem_object *obj;
1595 int ret;
1596
1597 ret = i915_mutex_lock_interruptible(dev);
1598 if (ret)
1599 return ret;
1600
1601 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
1602 if (&obj->base == NULL) {
1603 ret = -ENOENT;
1604 goto unlock;
1605 }
1606
1607 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
1608 ret = -E2BIG;
1609 goto out;
1610 }
1611
1612 if (obj->madv != I915_MADV_WILLNEED) {
1613 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1614 ret = -EINVAL;
1615 goto out;
1616 }
1617
1618 ret = i915_gem_object_create_mmap_offset(obj);
1619 if (ret)
1620 goto out;
1621
1622 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
1623
1624 out:
1625 drm_gem_object_unreference(&obj->base);
1626 unlock:
1627 mutex_unlock(&dev->struct_mutex);
1628 return ret;
1629 }
1630
1631 /**
1632 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1633 * @dev: DRM device
1634 * @data: GTT mapping ioctl data
1635 * @file: GEM object info
1636 *
1637 * Simply returns the fake offset to userspace so it can mmap it.
1638 * The mmap call will end up in drm_gem_mmap(), which will set things
1639 * up so we can get faults in the handler above.
1640 *
1641 * The fault handler will take care of binding the object into the GTT
1642 * (since it may have been evicted to make room for something), allocating
1643 * a fence register, and mapping the appropriate aperture address into
1644 * userspace.
1645 */
1646 int
1647 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1648 struct drm_file *file)
1649 {
1650 struct drm_i915_gem_mmap_gtt *args = data;
1651
1652 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1653 }
1654
1655 /* Immediately discard the backing storage */
1656 static void
1657 i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1658 {
1659 struct inode *inode;
1660
1661 i915_gem_object_free_mmap_offset(obj);
1662
1663 if (obj->base.filp == NULL)
1664 return;
1665
1666 /* Our goal here is to return as much of the memory as
1667 * is possible back to the system as we are called from OOM.
1668 * To do this we must instruct the shmfs to drop all of its
1669 * backing pages, *now*.
1670 */
1671 inode = obj->base.filp->f_path.dentry->d_inode;
1672 shmem_truncate_range(inode, 0, (loff_t)-1);
1673
1674 obj->madv = __I915_MADV_PURGED;
1675 }
1676
1677 static inline int
1678 i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1679 {
1680 return obj->madv == I915_MADV_DONTNEED;
1681 }
1682
1683 static void
1684 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1685 {
1686 int page_count = obj->base.size / PAGE_SIZE;
1687 struct scatterlist *sg;
1688 int ret, i;
1689
1690 BUG_ON(obj->madv == __I915_MADV_PURGED);
1691
1692 ret = i915_gem_object_set_to_cpu_domain(obj, true);
1693 if (ret) {
1694 /* In the event of a disaster, abandon all caches and
1695 * hope for the best.
1696 */
1697 WARN_ON(ret != -EIO);
1698 i915_gem_clflush_object(obj);
1699 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
1700 }
1701
1702 if (i915_gem_object_needs_bit17_swizzle(obj))
1703 i915_gem_object_save_bit_17_swizzle(obj);
1704
1705 if (obj->madv == I915_MADV_DONTNEED)
1706 obj->dirty = 0;
1707
1708 for_each_sg(obj->pages->sgl, sg, page_count, i) {
1709 struct page *page = sg_page(sg);
1710
1711 if (obj->dirty)
1712 set_page_dirty(page);
1713
1714 if (obj->madv == I915_MADV_WILLNEED)
1715 mark_page_accessed(page);
1716
1717 page_cache_release(page);
1718 }
1719 obj->dirty = 0;
1720
1721 sg_free_table(obj->pages);
1722 kfree(obj->pages);
1723 }
1724
1725 static int
1726 i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
1727 {
1728 const struct drm_i915_gem_object_ops *ops = obj->ops;
1729
1730 if (obj->pages == NULL)
1731 return 0;
1732
1733 BUG_ON(obj->gtt_space);
1734
1735 if (obj->pages_pin_count)
1736 return -EBUSY;
1737
1738 /* ->put_pages might need to allocate memory for the bit17 swizzle
1739 * array, hence protect them from being reaped by removing them from gtt
1740 * lists early. */
1741 list_del(&obj->gtt_list);
1742
1743 ops->put_pages(obj);
1744 obj->pages = NULL;
1745
1746 if (i915_gem_object_is_purgeable(obj))
1747 i915_gem_object_truncate(obj);
1748
1749 return 0;
1750 }
1751
1752 static long
1753 __i915_gem_shrink(struct drm_i915_private *dev_priv, long target,
1754 bool purgeable_only)
1755 {
1756 struct drm_i915_gem_object *obj, *next;
1757 long count = 0;
1758
1759 list_for_each_entry_safe(obj, next,
1760 &dev_priv->mm.unbound_list,
1761 gtt_list) {
1762 if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
1763 i915_gem_object_put_pages(obj) == 0) {
1764 count += obj->base.size >> PAGE_SHIFT;
1765 if (count >= target)
1766 return count;
1767 }
1768 }
1769
1770 list_for_each_entry_safe(obj, next,
1771 &dev_priv->mm.inactive_list,
1772 mm_list) {
1773 if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
1774 i915_gem_object_unbind(obj) == 0 &&
1775 i915_gem_object_put_pages(obj) == 0) {
1776 count += obj->base.size >> PAGE_SHIFT;
1777 if (count >= target)
1778 return count;
1779 }
1780 }
1781
1782 return count;
1783 }
1784
1785 static long
1786 i915_gem_purge(struct drm_i915_private *dev_priv, long target)
1787 {
1788 return __i915_gem_shrink(dev_priv, target, true);
1789 }
1790
1791 static void
1792 i915_gem_shrink_all(struct drm_i915_private *dev_priv)
1793 {
1794 struct drm_i915_gem_object *obj, *next;
1795
1796 i915_gem_evict_everything(dev_priv->dev);
1797
1798 list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list, gtt_list)
1799 i915_gem_object_put_pages(obj);
1800 }
1801
1802 static int
1803 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
1804 {
1805 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1806 int page_count, i;
1807 struct address_space *mapping;
1808 struct sg_table *st;
1809 struct scatterlist *sg;
1810 struct page *page;
1811 gfp_t gfp;
1812
1813 /* Assert that the object is not currently in any GPU domain. As it
1814 * wasn't in the GTT, there shouldn't be any way it could have been in
1815 * a GPU cache
1816 */
1817 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
1818 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
1819
1820 st = kmalloc(sizeof(*st), GFP_KERNEL);
1821 if (st == NULL)
1822 return -ENOMEM;
1823
1824 page_count = obj->base.size / PAGE_SIZE;
1825 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
1826 sg_free_table(st);
1827 kfree(st);
1828 return -ENOMEM;
1829 }
1830
1831 /* Get the list of pages out of our struct file. They'll be pinned
1832 * at this point until we release them.
1833 *
1834 * Fail silently without starting the shrinker
1835 */
1836 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
1837 gfp = mapping_gfp_mask(mapping);
1838 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
1839 gfp &= ~(__GFP_IO | __GFP_WAIT);
1840 for_each_sg(st->sgl, sg, page_count, i) {
1841 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1842 if (IS_ERR(page)) {
1843 i915_gem_purge(dev_priv, page_count);
1844 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1845 }
1846 if (IS_ERR(page)) {
1847 /* We've tried hard to allocate the memory by reaping
1848 * our own buffer, now let the real VM do its job and
1849 * go down in flames if truly OOM.
1850 */
1851 gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
1852 gfp |= __GFP_IO | __GFP_WAIT;
1853
1854 i915_gem_shrink_all(dev_priv);
1855 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1856 if (IS_ERR(page))
1857 goto err_pages;
1858
1859 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
1860 gfp &= ~(__GFP_IO | __GFP_WAIT);
1861 }
1862
1863 sg_set_page(sg, page, PAGE_SIZE, 0);
1864 }
1865
1866 obj->pages = st;
1867
1868 if (i915_gem_object_needs_bit17_swizzle(obj))
1869 i915_gem_object_do_bit_17_swizzle(obj);
1870
1871 return 0;
1872
1873 err_pages:
1874 for_each_sg(st->sgl, sg, i, page_count)
1875 page_cache_release(sg_page(sg));
1876 sg_free_table(st);
1877 kfree(st);
1878 return PTR_ERR(page);
1879 }
1880
1881 /* Ensure that the associated pages are gathered from the backing storage
1882 * and pinned into our object. i915_gem_object_get_pages() may be called
1883 * multiple times before they are released by a single call to
1884 * i915_gem_object_put_pages() - once the pages are no longer referenced
1885 * either as a result of memory pressure (reaping pages under the shrinker)
1886 * or as the object is itself released.
1887 */
1888 int
1889 i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
1890 {
1891 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1892 const struct drm_i915_gem_object_ops *ops = obj->ops;
1893 int ret;
1894
1895 if (obj->pages)
1896 return 0;
1897
1898 BUG_ON(obj->pages_pin_count);
1899
1900 ret = ops->get_pages(obj);
1901 if (ret)
1902 return ret;
1903
1904 list_add_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
1905 return 0;
1906 }
1907
1908 void
1909 i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
1910 struct intel_ring_buffer *ring)
1911 {
1912 struct drm_device *dev = obj->base.dev;
1913 struct drm_i915_private *dev_priv = dev->dev_private;
1914 u32 seqno = intel_ring_get_seqno(ring);
1915
1916 BUG_ON(ring == NULL);
1917 obj->ring = ring;
1918
1919 /* Add a reference if we're newly entering the active list. */
1920 if (!obj->active) {
1921 drm_gem_object_reference(&obj->base);
1922 obj->active = 1;
1923 }
1924
1925 /* Move from whatever list we were on to the tail of execution. */
1926 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1927 list_move_tail(&obj->ring_list, &ring->active_list);
1928
1929 obj->last_read_seqno = seqno;
1930
1931 if (obj->fenced_gpu_access) {
1932 obj->last_fenced_seqno = seqno;
1933
1934 /* Bump MRU to take account of the delayed flush */
1935 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1936 struct drm_i915_fence_reg *reg;
1937
1938 reg = &dev_priv->fence_regs[obj->fence_reg];
1939 list_move_tail(®->lru_list,
1940 &dev_priv->mm.fence_list);
1941 }
1942 }
1943 }
1944
1945 static void
1946 i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1947 {
1948 struct drm_device *dev = obj->base.dev;
1949 struct drm_i915_private *dev_priv = dev->dev_private;
1950
1951 BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
1952 BUG_ON(!obj->active);
1953
1954 if (obj->pin_count) /* are we a framebuffer? */
1955 intel_mark_fb_idle(obj);
1956
1957 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1958
1959 list_del_init(&obj->ring_list);
1960 obj->ring = NULL;
1961
1962 obj->last_read_seqno = 0;
1963 obj->last_write_seqno = 0;
1964 obj->base.write_domain = 0;
1965
1966 obj->last_fenced_seqno = 0;
1967 obj->fenced_gpu_access = false;
1968
1969 obj->active = 0;
1970 drm_gem_object_unreference(&obj->base);
1971
1972 WARN_ON(i915_verify_lists(dev));
1973 }
1974
1975 static int
1976 i915_gem_handle_seqno_wrap(struct drm_device *dev)
1977 {
1978 struct drm_i915_private *dev_priv = dev->dev_private;
1979 struct intel_ring_buffer *ring;
1980 int ret, i, j;
1981
1982 /* The hardware uses various monotonic 32-bit counters, if we
1983 * detect that they will wraparound we need to idle the GPU
1984 * and reset those counters.
1985 */
1986 ret = 0;
1987 for_each_ring(ring, dev_priv, i) {
1988 for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
1989 ret |= ring->sync_seqno[j] != 0;
1990 }
1991 if (ret == 0)
1992 return ret;
1993
1994 ret = i915_gpu_idle(dev);
1995 if (ret)
1996 return ret;
1997
1998 i915_gem_retire_requests(dev);
1999 for_each_ring(ring, dev_priv, i) {
2000 for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
2001 ring->sync_seqno[j] = 0;
2002 }
2003
2004 return 0;
2005 }
2006
2007 int
2008 i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
2009 {
2010 struct drm_i915_private *dev_priv = dev->dev_private;
2011
2012 /* reserve 0 for non-seqno */
2013 if (dev_priv->next_seqno == 0) {
2014 int ret = i915_gem_handle_seqno_wrap(dev);
2015 if (ret)
2016 return ret;
2017
2018 dev_priv->next_seqno = 1;
2019 }
2020
2021 *seqno = dev_priv->next_seqno++;
2022 return 0;
2023 }
2024
2025 int
2026 i915_add_request(struct intel_ring_buffer *ring,
2027 struct drm_file *file,
2028 u32 *out_seqno)
2029 {
2030 drm_i915_private_t *dev_priv = ring->dev->dev_private;
2031 struct drm_i915_gem_request *request;
2032 u32 request_ring_position;
2033 int was_empty;
2034 int ret;
2035
2036 /*
2037 * Emit any outstanding flushes - execbuf can fail to emit the flush
2038 * after having emitted the batchbuffer command. Hence we need to fix
2039 * things up similar to emitting the lazy request. The difference here
2040 * is that the flush _must_ happen before the next request, no matter
2041 * what.
2042 */
2043 ret = intel_ring_flush_all_caches(ring);
2044 if (ret)
2045 return ret;
2046
2047 request = kmalloc(sizeof(*request), GFP_KERNEL);
2048 if (request == NULL)
2049 return -ENOMEM;
2050
2051
2052 /* Record the position of the start of the request so that
2053 * should we detect the updated seqno part-way through the
2054 * GPU processing the request, we never over-estimate the
2055 * position of the head.
2056 */
2057 request_ring_position = intel_ring_get_tail(ring);
2058
2059 ret = ring->add_request(ring);
2060 if (ret) {
2061 kfree(request);
2062 return ret;
2063 }
2064
2065 request->seqno = intel_ring_get_seqno(ring);
2066 request->ring = ring;
2067 request->tail = request_ring_position;
2068 request->emitted_jiffies = jiffies;
2069 was_empty = list_empty(&ring->request_list);
2070 list_add_tail(&request->list, &ring->request_list);
2071 request->file_priv = NULL;
2072
2073 if (file) {
2074 struct drm_i915_file_private *file_priv = file->driver_priv;
2075
2076 spin_lock(&file_priv->mm.lock);
2077 request->file_priv = file_priv;
2078 list_add_tail(&request->client_list,
2079 &file_priv->mm.request_list);
2080 spin_unlock(&file_priv->mm.lock);
2081 }
2082
2083 trace_i915_gem_request_add(ring, request->seqno);
2084 ring->outstanding_lazy_request = 0;
2085
2086 if (!dev_priv->mm.suspended) {
2087 if (i915_enable_hangcheck) {
2088 mod_timer(&dev_priv->hangcheck_timer,
2089 round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2090 }
2091 if (was_empty) {
2092 queue_delayed_work(dev_priv->wq,
2093 &dev_priv->mm.retire_work,
2094 round_jiffies_up_relative(HZ));
2095 intel_mark_busy(dev_priv->dev);
2096 }
2097 }
2098
2099 if (out_seqno)
2100 *out_seqno = request->seqno;
2101 return 0;
2102 }
2103
2104 static inline void
2105 i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
2106 {
2107 struct drm_i915_file_private *file_priv = request->file_priv;
2108
2109 if (!file_priv)
2110 return;
2111
2112 spin_lock(&file_priv->mm.lock);
2113 if (request->file_priv) {
2114 list_del(&request->client_list);
2115 request->file_priv = NULL;
2116 }
2117 spin_unlock(&file_priv->mm.lock);
2118 }
2119
2120 static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
2121 struct intel_ring_buffer *ring)
2122 {
2123 while (!list_empty(&ring->request_list)) {
2124 struct drm_i915_gem_request *request;
2125
2126 request = list_first_entry(&ring->request_list,
2127 struct drm_i915_gem_request,
2128 list);
2129
2130 list_del(&request->list);
2131 i915_gem_request_remove_from_client(request);
2132 kfree(request);
2133 }
2134
2135 while (!list_empty(&ring->active_list)) {
2136 struct drm_i915_gem_object *obj;
2137
2138 obj = list_first_entry(&ring->active_list,
2139 struct drm_i915_gem_object,
2140 ring_list);
2141
2142 i915_gem_object_move_to_inactive(obj);
2143 }
2144 }
2145
2146 static void i915_gem_reset_fences(struct drm_device *dev)
2147 {
2148 struct drm_i915_private *dev_priv = dev->dev_private;
2149 int i;
2150
2151 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2152 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2153
2154 i915_gem_write_fence(dev, i, NULL);
2155
2156 if (reg->obj)
2157 i915_gem_object_fence_lost(reg->obj);
2158
2159 reg->pin_count = 0;
2160 reg->obj = NULL;
2161 INIT_LIST_HEAD(®->lru_list);
2162 }
2163
2164 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
2165 }
2166
2167 void i915_gem_reset(struct drm_device *dev)
2168 {
2169 struct drm_i915_private *dev_priv = dev->dev_private;
2170 struct drm_i915_gem_object *obj;
2171 struct intel_ring_buffer *ring;
2172 int i;
2173
2174 for_each_ring(ring, dev_priv, i)
2175 i915_gem_reset_ring_lists(dev_priv, ring);
2176
2177 /* Move everything out of the GPU domains to ensure we do any
2178 * necessary invalidation upon reuse.
2179 */
2180 list_for_each_entry(obj,
2181 &dev_priv->mm.inactive_list,
2182 mm_list)
2183 {
2184 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
2185 }
2186
2187 /* The fence registers are invalidated so clear them out */
2188 i915_gem_reset_fences(dev);
2189 }
2190
2191 /**
2192 * This function clears the request list as sequence numbers are passed.
2193 */
2194 void
2195 i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
2196 {
2197 uint32_t seqno;
2198
2199 if (list_empty(&ring->request_list))
2200 return;
2201
2202 WARN_ON(i915_verify_lists(ring->dev));
2203
2204 seqno = ring->get_seqno(ring, true);
2205
2206 while (!list_empty(&ring->request_list)) {
2207 struct drm_i915_gem_request *request;
2208
2209 request = list_first_entry(&ring->request_list,
2210 struct drm_i915_gem_request,
2211 list);
2212
2213 if (!i915_seqno_passed(seqno, request->seqno))
2214 break;
2215
2216 trace_i915_gem_request_retire(ring, request->seqno);
2217 /* We know the GPU must have read the request to have
2218 * sent us the seqno + interrupt, so use the position
2219 * of tail of the request to update the last known position
2220 * of the GPU head.
2221 */
2222 ring->last_retired_head = request->tail;
2223
2224 list_del(&request->list);
2225 i915_gem_request_remove_from_client(request);
2226 kfree(request);
2227 }
2228
2229 /* Move any buffers on the active list that are no longer referenced
2230 * by the ringbuffer to the flushing/inactive lists as appropriate.
2231 */
2232 while (!list_empty(&ring->active_list)) {
2233 struct drm_i915_gem_object *obj;
2234
2235 obj = list_first_entry(&ring->active_list,
2236 struct drm_i915_gem_object,
2237 ring_list);
2238
2239 if (!i915_seqno_passed(seqno, obj->last_read_seqno))
2240 break;
2241
2242 i915_gem_object_move_to_inactive(obj);
2243 }
2244
2245 if (unlikely(ring->trace_irq_seqno &&
2246 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
2247 ring->irq_put(ring);
2248 ring->trace_irq_seqno = 0;
2249 }
2250
2251 WARN_ON(i915_verify_lists(ring->dev));
2252 }
2253
2254 void
2255 i915_gem_retire_requests(struct drm_device *dev)
2256 {
2257 drm_i915_private_t *dev_priv = dev->dev_private;
2258 struct intel_ring_buffer *ring;
2259 int i;
2260
2261 for_each_ring(ring, dev_priv, i)
2262 i915_gem_retire_requests_ring(ring);
2263 }
2264
2265 static void
2266 i915_gem_retire_work_handler(struct work_struct *work)
2267 {
2268 drm_i915_private_t *dev_priv;
2269 struct drm_device *dev;
2270 struct intel_ring_buffer *ring;
2271 bool idle;
2272 int i;
2273
2274 dev_priv = container_of(work, drm_i915_private_t,
2275 mm.retire_work.work);
2276 dev = dev_priv->dev;
2277
2278 /* Come back later if the device is busy... */
2279 if (!mutex_trylock(&dev->struct_mutex)) {
2280 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2281 round_jiffies_up_relative(HZ));
2282 return;
2283 }
2284
2285 i915_gem_retire_requests(dev);
2286
2287 /* Send a periodic flush down the ring so we don't hold onto GEM
2288 * objects indefinitely.
2289 */
2290 idle = true;
2291 for_each_ring(ring, dev_priv, i) {
2292 if (ring->gpu_caches_dirty)
2293 i915_add_request(ring, NULL, NULL);
2294
2295 idle &= list_empty(&ring->request_list);
2296 }
2297
2298 if (!dev_priv->mm.suspended && !idle)
2299 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2300 round_jiffies_up_relative(HZ));
2301 if (idle)
2302 intel_mark_idle(dev);
2303
2304 mutex_unlock(&dev->struct_mutex);
2305 }
2306
2307 /**
2308 * Ensures that an object will eventually get non-busy by flushing any required
2309 * write domains, emitting any outstanding lazy request and retiring and
2310 * completed requests.
2311 */
2312 static int
2313 i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2314 {
2315 int ret;
2316
2317 if (obj->active) {
2318 ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
2319 if (ret)
2320 return ret;
2321
2322 i915_gem_retire_requests_ring(obj->ring);
2323 }
2324
2325 return 0;
2326 }
2327
2328 /**
2329 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2330 * @DRM_IOCTL_ARGS: standard ioctl arguments
2331 *
2332 * Returns 0 if successful, else an error is returned with the remaining time in
2333 * the timeout parameter.
2334 * -ETIME: object is still busy after timeout
2335 * -ERESTARTSYS: signal interrupted the wait
2336 * -ENONENT: object doesn't exist
2337 * Also possible, but rare:
2338 * -EAGAIN: GPU wedged
2339 * -ENOMEM: damn
2340 * -ENODEV: Internal IRQ fail
2341 * -E?: The add request failed
2342 *
2343 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2344 * non-zero timeout parameter the wait ioctl will wait for the given number of
2345 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2346 * without holding struct_mutex the object may become re-busied before this
2347 * function completes. A similar but shorter * race condition exists in the busy
2348 * ioctl
2349 */
2350 int
2351 i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2352 {
2353 struct drm_i915_gem_wait *args = data;
2354 struct drm_i915_gem_object *obj;
2355 struct intel_ring_buffer *ring = NULL;
2356 struct timespec timeout_stack, *timeout = NULL;
2357 u32 seqno = 0;
2358 int ret = 0;
2359
2360 if (args->timeout_ns >= 0) {
2361 timeout_stack = ns_to_timespec(args->timeout_ns);
2362 timeout = &timeout_stack;
2363 }
2364
2365 ret = i915_mutex_lock_interruptible(dev);
2366 if (ret)
2367 return ret;
2368
2369 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
2370 if (&obj->base == NULL) {
2371 mutex_unlock(&dev->struct_mutex);
2372 return -ENOENT;
2373 }
2374
2375 /* Need to make sure the object gets inactive eventually. */
2376 ret = i915_gem_object_flush_active(obj);
2377 if (ret)
2378 goto out;
2379
2380 if (obj->active) {
2381 seqno = obj->last_read_seqno;
2382 ring = obj->ring;
2383 }
2384
2385 if (seqno == 0)
2386 goto out;
2387
2388 /* Do this after OLR check to make sure we make forward progress polling
2389 * on this IOCTL with a 0 timeout (like busy ioctl)
2390 */
2391 if (!args->timeout_ns) {
2392 ret = -ETIME;
2393 goto out;
2394 }
2395
2396 drm_gem_object_unreference(&obj->base);
2397 mutex_unlock(&dev->struct_mutex);
2398
2399 ret = __wait_seqno(ring, seqno, true, timeout);
2400 if (timeout) {
2401 WARN_ON(!timespec_valid(timeout));
2402 args->timeout_ns = timespec_to_ns(timeout);
2403 }
2404 return ret;
2405
2406 out:
2407 drm_gem_object_unreference(&obj->base);
2408 mutex_unlock(&dev->struct_mutex);
2409 return ret;
2410 }
2411
2412 /**
2413 * i915_gem_object_sync - sync an object to a ring.
2414 *
2415 * @obj: object which may be in use on another ring.
2416 * @to: ring we wish to use the object on. May be NULL.
2417 *
2418 * This code is meant to abstract object synchronization with the GPU.
2419 * Calling with NULL implies synchronizing the object with the CPU
2420 * rather than a particular GPU ring.
2421 *
2422 * Returns 0 if successful, else propagates up the lower layer error.
2423 */
2424 int
2425 i915_gem_object_sync(struct drm_i915_gem_object *obj,
2426 struct intel_ring_buffer *to)
2427 {
2428 struct intel_ring_buffer *from = obj->ring;
2429 u32 seqno;
2430 int ret, idx;
2431
2432 if (from == NULL || to == from)
2433 return 0;
2434
2435 if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
2436 return i915_gem_object_wait_rendering(obj, false);
2437
2438 idx = intel_ring_sync_index(from, to);
2439
2440 seqno = obj->last_read_seqno;
2441 if (seqno <= from->sync_seqno[idx])
2442 return 0;
2443
2444 ret = i915_gem_check_olr(obj->ring, seqno);
2445 if (ret)
2446 return ret;
2447
2448 ret = to->sync_to(to, from, seqno);
2449 if (!ret)
2450 /* We use last_read_seqno because sync_to()
2451 * might have just caused seqno wrap under
2452 * the radar.
2453 */
2454 from->sync_seqno[idx] = obj->last_read_seqno;
2455
2456 return ret;
2457 }
2458
2459 static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2460 {
2461 u32 old_write_domain, old_read_domains;
2462
2463 /* Act a barrier for all accesses through the GTT */
2464 mb();
2465
2466 /* Force a pagefault for domain tracking on next user access */
2467 i915_gem_release_mmap(obj);
2468
2469 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2470 return;
2471
2472 old_read_domains = obj->base.read_domains;
2473 old_write_domain = obj->base.write_domain;
2474
2475 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2476 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2477
2478 trace_i915_gem_object_change_domain(obj,
2479 old_read_domains,
2480 old_write_domain);
2481 }
2482
2483 /**
2484 * Unbinds an object from the GTT aperture.
2485 */
2486 int
2487 i915_gem_object_unbind(struct drm_i915_gem_object *obj)
2488 {
2489 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
2490 int ret = 0;
2491
2492 if (obj->gtt_space == NULL)
2493 return 0;
2494
2495 if (obj->pin_count)
2496 return -EBUSY;
2497
2498 BUG_ON(obj->pages == NULL);
2499
2500 ret = i915_gem_object_finish_gpu(obj);
2501 if (ret)
2502 return ret;
2503 /* Continue on if we fail due to EIO, the GPU is hung so we
2504 * should be safe and we need to cleanup or else we might
2505 * cause memory corruption through use-after-free.
2506 */
2507
2508 i915_gem_object_finish_gtt(obj);
2509
2510 /* release the fence reg _after_ flushing */
2511 ret = i915_gem_object_put_fence(obj);
2512 if (ret)
2513 return ret;
2514
2515 trace_i915_gem_object_unbind(obj);
2516
2517 if (obj->has_global_gtt_mapping)
2518 i915_gem_gtt_unbind_object(obj);
2519 if (obj->has_aliasing_ppgtt_mapping) {
2520 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2521 obj->has_aliasing_ppgtt_mapping = 0;
2522 }
2523 i915_gem_gtt_finish_object(obj);
2524
2525 list_del(&obj->mm_list);
2526 list_move_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
2527 /* Avoid an unnecessary call to unbind on rebind. */
2528 obj->map_and_fenceable = true;
2529
2530 drm_mm_put_block(obj->gtt_space);
2531 obj->gtt_space = NULL;
2532 obj->gtt_offset = 0;
2533
2534 return 0;
2535 }
2536
2537 int i915_gpu_idle(struct drm_device *dev)
2538 {
2539 drm_i915_private_t *dev_priv = dev->dev_private;
2540 struct intel_ring_buffer *ring;
2541 int ret, i;
2542
2543 /* Flush everything onto the inactive list. */
2544 for_each_ring(ring, dev_priv, i) {
2545 ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
2546 if (ret)
2547 return ret;
2548
2549 ret = intel_ring_idle(ring);
2550 if (ret)
2551 return ret;
2552 }
2553
2554 return 0;
2555 }
2556
2557 static void sandybridge_write_fence_reg(struct drm_device *dev, int reg,
2558 struct drm_i915_gem_object *obj)
2559 {
2560 drm_i915_private_t *dev_priv = dev->dev_private;
2561 uint64_t val;
2562
2563 if (obj) {
2564 u32 size = obj->gtt_space->size;
2565
2566 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2567 0xfffff000) << 32;
2568 val |= obj->gtt_offset & 0xfffff000;
2569 val |= (uint64_t)((obj->stride / 128) - 1) <<
2570 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2571
2572 if (obj->tiling_mode == I915_TILING_Y)
2573 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2574 val |= I965_FENCE_REG_VALID;
2575 } else
2576 val = 0;
2577
2578 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val);
2579 POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8);
2580 }
2581
2582 static void i965_write_fence_reg(struct drm_device *dev, int reg,
2583 struct drm_i915_gem_object *obj)
2584 {
2585 drm_i915_private_t *dev_priv = dev->dev_private;
2586 uint64_t val;
2587
2588 if (obj) {
2589 u32 size = obj->gtt_space->size;
2590
2591 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2592 0xfffff000) << 32;
2593 val |= obj->gtt_offset & 0xfffff000;
2594 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2595 if (obj->tiling_mode == I915_TILING_Y)
2596 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2597 val |= I965_FENCE_REG_VALID;
2598 } else
2599 val = 0;
2600
2601 I915_WRITE64(FENCE_REG_965_0 + reg * 8, val);
2602 POSTING_READ(FENCE_REG_965_0 + reg * 8);
2603 }
2604
2605 static void i915_write_fence_reg(struct drm_device *dev, int reg,
2606 struct drm_i915_gem_object *obj)
2607 {
2608 drm_i915_private_t *dev_priv = dev->dev_private;
2609 u32 val;
2610
2611 if (obj) {
2612 u32 size = obj->gtt_space->size;
2613 int pitch_val;
2614 int tile_width;
2615
2616 WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2617 (size & -size) != size ||
2618 (obj->gtt_offset & (size - 1)),
2619 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2620 obj->gtt_offset, obj->map_and_fenceable, size);
2621
2622 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2623 tile_width = 128;
2624 else
2625 tile_width = 512;
2626
2627 /* Note: pitch better be a power of two tile widths */
2628 pitch_val = obj->stride / tile_width;
2629 pitch_val = ffs(pitch_val) - 1;
2630
2631 val = obj->gtt_offset;
2632 if (obj->tiling_mode == I915_TILING_Y)
2633 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2634 val |= I915_FENCE_SIZE_BITS(size);
2635 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2636 val |= I830_FENCE_REG_VALID;
2637 } else
2638 val = 0;
2639
2640 if (reg < 8)
2641 reg = FENCE_REG_830_0 + reg * 4;
2642 else
2643 reg = FENCE_REG_945_8 + (reg - 8) * 4;
2644
2645 I915_WRITE(reg, val);
2646 POSTING_READ(reg);
2647 }
2648
2649 static void i830_write_fence_reg(struct drm_device *dev, int reg,
2650 struct drm_i915_gem_object *obj)
2651 {
2652 drm_i915_private_t *dev_priv = dev->dev_private;
2653 uint32_t val;
2654
2655 if (obj) {
2656 u32 size = obj->gtt_space->size;
2657 uint32_t pitch_val;
2658
2659 WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2660 (size & -size) != size ||
2661 (obj->gtt_offset & (size - 1)),
2662 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2663 obj->gtt_offset, size);
2664
2665 pitch_val = obj->stride / 128;
2666 pitch_val = ffs(pitch_val) - 1;
2667
2668 val = obj->gtt_offset;
2669 if (obj->tiling_mode == I915_TILING_Y)
2670 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2671 val |= I830_FENCE_SIZE_BITS(size);
2672 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2673 val |= I830_FENCE_REG_VALID;
2674 } else
2675 val = 0;
2676
2677 I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
2678 POSTING_READ(FENCE_REG_830_0 + reg * 4);
2679 }
2680
2681 static void i915_gem_write_fence(struct drm_device *dev, int reg,
2682 struct drm_i915_gem_object *obj)
2683 {
2684 switch (INTEL_INFO(dev)->gen) {
2685 case 7:
2686 case 6: sandybridge_write_fence_reg(dev, reg, obj); break;
2687 case 5:
2688 case 4: i965_write_fence_reg(dev, reg, obj); break;
2689 case 3: i915_write_fence_reg(dev, reg, obj); break;
2690 case 2: i830_write_fence_reg(dev, reg, obj); break;
2691 default: break;
2692 }
2693 }
2694
2695 static inline int fence_number(struct drm_i915_private *dev_priv,
2696 struct drm_i915_fence_reg *fence)
2697 {
2698 return fence - dev_priv->fence_regs;
2699 }
2700
2701 static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
2702 struct drm_i915_fence_reg *fence,
2703 bool enable)
2704 {
2705 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2706 int reg = fence_number(dev_priv, fence);
2707
2708 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
2709
2710 if (enable) {
2711 obj->fence_reg = reg;
2712 fence->obj = obj;
2713 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
2714 } else {
2715 obj->fence_reg = I915_FENCE_REG_NONE;
2716 fence->obj = NULL;
2717 list_del_init(&fence->lru_list);
2718 }
2719 }
2720
2721 static int
2722 i915_gem_object_flush_fence(struct drm_i915_gem_object *obj)
2723 {
2724 if (obj->last_fenced_seqno) {
2725 int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
2726 if (ret)
2727 return ret;
2728
2729 obj->last_fenced_seqno = 0;
2730 }
2731
2732 /* Ensure that all CPU reads are completed before installing a fence
2733 * and all writes before removing the fence.
2734 */
2735 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2736 mb();
2737
2738 obj->fenced_gpu_access = false;
2739 return 0;
2740 }
2741
2742 int
2743 i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2744 {
2745 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2746 int ret;
2747
2748 ret = i915_gem_object_flush_fence(obj);
2749 if (ret)
2750 return ret;
2751
2752 if (obj->fence_reg == I915_FENCE_REG_NONE)
2753 return 0;
2754
2755 i915_gem_object_update_fence(obj,
2756 &dev_priv->fence_regs[obj->fence_reg],
2757 false);
2758 i915_gem_object_fence_lost(obj);
2759
2760 return 0;
2761 }
2762
2763 static struct drm_i915_fence_reg *
2764 i915_find_fence_reg(struct drm_device *dev)
2765 {
2766 struct drm_i915_private *dev_priv = dev->dev_private;
2767 struct drm_i915_fence_reg *reg, *avail;
2768 int i;
2769
2770 /* First try to find a free reg */
2771 avail = NULL;
2772 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2773 reg = &dev_priv->fence_regs[i];
2774 if (!reg->obj)
2775 return reg;
2776
2777 if (!reg->pin_count)
2778 avail = reg;
2779 }
2780
2781 if (avail == NULL)
2782 return NULL;
2783
2784 /* None available, try to steal one or wait for a user to finish */
2785 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2786 if (reg->pin_count)
2787 continue;
2788
2789 return reg;
2790 }
2791
2792 return NULL;
2793 }
2794
2795 /**
2796 * i915_gem_object_get_fence - set up fencing for an object
2797 * @obj: object to map through a fence reg
2798 *
2799 * When mapping objects through the GTT, userspace wants to be able to write
2800 * to them without having to worry about swizzling if the object is tiled.
2801 * This function walks the fence regs looking for a free one for @obj,
2802 * stealing one if it can't find any.
2803 *
2804 * It then sets up the reg based on the object's properties: address, pitch
2805 * and tiling format.
2806 *
2807 * For an untiled surface, this removes any existing fence.
2808 */
2809 int
2810 i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
2811 {
2812 struct drm_device *dev = obj->base.dev;
2813 struct drm_i915_private *dev_priv = dev->dev_private;
2814 bool enable = obj->tiling_mode != I915_TILING_NONE;
2815 struct drm_i915_fence_reg *reg;
2816 int ret;
2817
2818 /* Have we updated the tiling parameters upon the object and so
2819 * will need to serialise the write to the associated fence register?
2820 */
2821 if (obj->fence_dirty) {
2822 ret = i915_gem_object_flush_fence(obj);
2823 if (ret)
2824 return ret;
2825 }
2826
2827 /* Just update our place in the LRU if our fence is getting reused. */
2828 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2829 reg = &dev_priv->fence_regs[obj->fence_reg];
2830 if (!obj->fence_dirty) {
2831 list_move_tail(®->lru_list,
2832 &dev_priv->mm.fence_list);
2833 return 0;
2834 }
2835 } else if (enable) {
2836 reg = i915_find_fence_reg(dev);
2837 if (reg == NULL)
2838 return -EDEADLK;
2839
2840 if (reg->obj) {
2841 struct drm_i915_gem_object *old = reg->obj;
2842
2843 ret = i915_gem_object_flush_fence(old);
2844 if (ret)
2845 return ret;
2846
2847 i915_gem_object_fence_lost(old);
2848 }
2849 } else
2850 return 0;
2851
2852 i915_gem_object_update_fence(obj, reg, enable);
2853 obj->fence_dirty = false;
2854
2855 return 0;
2856 }
2857
2858 static bool i915_gem_valid_gtt_space(struct drm_device *dev,
2859 struct drm_mm_node *gtt_space,
2860 unsigned long cache_level)
2861 {
2862 struct drm_mm_node *other;
2863
2864 /* On non-LLC machines we have to be careful when putting differing
2865 * types of snoopable memory together to avoid the prefetcher
2866 * crossing memory domains and dieing.
2867 */
2868 if (HAS_LLC(dev))
2869 return true;
2870
2871 if (gtt_space == NULL)
2872 return true;
2873
2874 if (list_empty(>t_space->node_list))
2875 return true;
2876
2877 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
2878 if (other->allocated && !other->hole_follows && other->color != cache_level)
2879 return false;
2880
2881 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
2882 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
2883 return false;
2884
2885 return true;
2886 }
2887
2888 static void i915_gem_verify_gtt(struct drm_device *dev)
2889 {
2890 #if WATCH_GTT
2891 struct drm_i915_private *dev_priv = dev->dev_private;
2892 struct drm_i915_gem_object *obj;
2893 int err = 0;
2894
2895 list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
2896 if (obj->gtt_space == NULL) {
2897 printk(KERN_ERR "object found on GTT list with no space reserved\n");
2898 err++;
2899 continue;
2900 }
2901
2902 if (obj->cache_level != obj->gtt_space->color) {
2903 printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
2904 obj->gtt_space->start,
2905 obj->gtt_space->start + obj->gtt_space->size,
2906 obj->cache_level,
2907 obj->gtt_space->color);
2908 err++;
2909 continue;
2910 }
2911
2912 if (!i915_gem_valid_gtt_space(dev,
2913 obj->gtt_space,
2914 obj->cache_level)) {
2915 printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
2916 obj->gtt_space->start,
2917 obj->gtt_space->start + obj->gtt_space->size,
2918 obj->cache_level);
2919 err++;
2920 continue;
2921 }
2922 }
2923
2924 WARN_ON(err);
2925 #endif
2926 }
2927
2928 /**
2929 * Finds free space in the GTT aperture and binds the object there.
2930 */
2931 static int
2932 i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
2933 unsigned alignment,
2934 bool map_and_fenceable,
2935 bool nonblocking)
2936 {
2937 struct drm_device *dev = obj->base.dev;
2938 drm_i915_private_t *dev_priv = dev->dev_private;
2939 struct drm_mm_node *node;
2940 u32 size, fence_size, fence_alignment, unfenced_alignment;
2941 bool mappable, fenceable;
2942 int ret;
2943
2944 if (obj->madv != I915_MADV_WILLNEED) {
2945 DRM_ERROR("Attempting to bind a purgeable object\n");
2946 return -EINVAL;
2947 }
2948
2949 fence_size = i915_gem_get_gtt_size(dev,
2950 obj->base.size,
2951 obj->tiling_mode);
2952 fence_alignment = i915_gem_get_gtt_alignment(dev,
2953 obj->base.size,
2954 obj->tiling_mode);
2955 unfenced_alignment =
2956 i915_gem_get_unfenced_gtt_alignment(dev,
2957 obj->base.size,
2958 obj->tiling_mode);
2959
2960 if (alignment == 0)
2961 alignment = map_and_fenceable ? fence_alignment :
2962 unfenced_alignment;
2963 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
2964 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2965 return -EINVAL;
2966 }
2967
2968 size = map_and_fenceable ? fence_size : obj->base.size;
2969
2970 /* If the object is bigger than the entire aperture, reject it early
2971 * before evicting everything in a vain attempt to find space.
2972 */
2973 if (obj->base.size >
2974 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
2975 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2976 return -E2BIG;
2977 }
2978
2979 ret = i915_gem_object_get_pages(obj);
2980 if (ret)
2981 return ret;
2982
2983 i915_gem_object_pin_pages(obj);
2984
2985 node = kzalloc(sizeof(*node), GFP_KERNEL);
2986 if (node == NULL) {
2987 i915_gem_object_unpin_pages(obj);
2988 return -ENOMEM;
2989 }
2990
2991 search_free:
2992 if (map_and_fenceable)
2993 ret = drm_mm_insert_node_in_range_generic(&dev_priv->mm.gtt_space, node,
2994 size, alignment, obj->cache_level,
2995 0, dev_priv->mm.gtt_mappable_end);
2996 else
2997 ret = drm_mm_insert_node_generic(&dev_priv->mm.gtt_space, node,
2998 size, alignment, obj->cache_level);
2999 if (ret) {
3000 ret = i915_gem_evict_something(dev, size, alignment,
3001 obj->cache_level,
3002 map_and_fenceable,
3003 nonblocking);
3004 if (ret == 0)
3005 goto search_free;
3006
3007 i915_gem_object_unpin_pages(obj);
3008 kfree(node);
3009 return ret;
3010 }
3011 if (WARN_ON(!i915_gem_valid_gtt_space(dev, node, obj->cache_level))) {
3012 i915_gem_object_unpin_pages(obj);
3013 drm_mm_put_block(node);
3014 return -EINVAL;
3015 }
3016
3017 ret = i915_gem_gtt_prepare_object(obj);
3018 if (ret) {
3019 i915_gem_object_unpin_pages(obj);
3020 drm_mm_put_block(node);
3021 return ret;
3022 }
3023
3024 list_move_tail(&obj->gtt_list, &dev_priv->mm.bound_list);
3025 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
3026
3027 obj->gtt_space = node;
3028 obj->gtt_offset = node->start;
3029
3030 fenceable =
3031 node->size == fence_size &&
3032 (node->start & (fence_alignment - 1)) == 0;
3033
3034 mappable =
3035 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
3036
3037 obj->map_and_fenceable = mappable && fenceable;
3038
3039 i915_gem_object_unpin_pages(obj);
3040 trace_i915_gem_object_bind(obj, map_and_fenceable);
3041 i915_gem_verify_gtt(dev);
3042 return 0;
3043 }
3044
3045 void
3046 i915_gem_clflush_object(struct drm_i915_gem_object *obj)
3047 {
3048 /* If we don't have a page list set up, then we're not pinned
3049 * to GPU, and we can ignore the cache flush because it'll happen
3050 * again at bind time.
3051 */
3052 if (obj->pages == NULL)
3053 return;
3054
3055 /* If the GPU is snooping the contents of the CPU cache,
3056 * we do not need to manually clear the CPU cache lines. However,
3057 * the caches are only snooped when the render cache is
3058 * flushed/invalidated. As we always have to emit invalidations
3059 * and flushes when moving into and out of the RENDER domain, correct
3060 * snooping behaviour occurs naturally as the result of our domain
3061 * tracking.
3062 */
3063 if (obj->cache_level != I915_CACHE_NONE)
3064 return;
3065
3066 trace_i915_gem_object_clflush(obj);
3067
3068 drm_clflush_sg(obj->pages);
3069 }
3070
3071 /** Flushes the GTT write domain for the object if it's dirty. */
3072 static void
3073 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
3074 {
3075 uint32_t old_write_domain;
3076
3077 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
3078 return;
3079
3080 /* No actual flushing is required for the GTT write domain. Writes
3081 * to it immediately go to main memory as far as we know, so there's
3082 * no chipset flush. It also doesn't land in render cache.
3083 *
3084 * However, we do have to enforce the order so that all writes through
3085 * the GTT land before any writes to the device, such as updates to
3086 * the GATT itself.
3087 */
3088 wmb();
3089
3090 old_write_domain = obj->base.write_domain;
3091 obj->base.write_domain = 0;
3092
3093 trace_i915_gem_object_change_domain(obj,
3094 obj->base.read_domains,
3095 old_write_domain);
3096 }
3097
3098 /** Flushes the CPU write domain for the object if it's dirty. */
3099 static void
3100 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
3101 {
3102 uint32_t old_write_domain;
3103
3104 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
3105 return;
3106
3107 i915_gem_clflush_object(obj);
3108 i915_gem_chipset_flush(obj->base.dev);
3109 old_write_domain = obj->base.write_domain;
3110 obj->base.write_domain = 0;
3111
3112 trace_i915_gem_object_change_domain(obj,
3113 obj->base.read_domains,
3114 old_write_domain);
3115 }
3116
3117 /**
3118 * Moves a single object to the GTT read, and possibly write domain.
3119 *
3120 * This function returns when the move is complete, including waiting on
3121 * flushes to occur.
3122 */
3123 int
3124 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
3125 {
3126 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
3127 uint32_t old_write_domain, old_read_domains;
3128 int ret;
3129
3130 /* Not valid to be called on unbound objects. */
3131 if (obj->gtt_space == NULL)
3132 return -EINVAL;
3133
3134 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3135 return 0;
3136
3137 ret = i915_gem_object_wait_rendering(obj, !write);
3138 if (ret)
3139 return ret;
3140
3141 i915_gem_object_flush_cpu_write_domain(obj);
3142
3143 old_write_domain = obj->base.write_domain;
3144 old_read_domains = obj->base.read_domains;
3145
3146 /* It should now be out of any other write domains, and we can update
3147 * the domain values for our changes.
3148 */
3149 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3150 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3151 if (write) {
3152 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3153 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3154 obj->dirty = 1;
3155 }
3156
3157 trace_i915_gem_object_change_domain(obj,
3158 old_read_domains,
3159 old_write_domain);
3160
3161 /* And bump the LRU for this access */
3162 if (i915_gem_object_is_inactive(obj))
3163 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
3164
3165 return 0;
3166 }
3167
3168 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3169 enum i915_cache_level cache_level)
3170 {
3171 struct drm_device *dev = obj->base.dev;
3172 drm_i915_private_t *dev_priv = dev->dev_private;
3173 int ret;
3174
3175 if (obj->cache_level == cache_level)
3176 return 0;
3177
3178 if (obj->pin_count) {
3179 DRM_DEBUG("can not change the cache level of pinned objects\n");
3180 return -EBUSY;
3181 }
3182
3183 if (!i915_gem_valid_gtt_space(dev, obj->gtt_space, cache_level)) {
3184 ret = i915_gem_object_unbind(obj);
3185 if (ret)
3186 return ret;
3187 }
3188
3189 if (obj->gtt_space) {
3190 ret = i915_gem_object_finish_gpu(obj);
3191 if (ret)
3192 return ret;
3193
3194 i915_gem_object_finish_gtt(obj);
3195
3196 /* Before SandyBridge, you could not use tiling or fence
3197 * registers with snooped memory, so relinquish any fences
3198 * currently pointing to our region in the aperture.
3199 */
3200 if (INTEL_INFO(dev)->gen < 6) {
3201 ret = i915_gem_object_put_fence(obj);
3202 if (ret)
3203 return ret;
3204 }
3205
3206 if (obj->has_global_gtt_mapping)
3207 i915_gem_gtt_bind_object(obj, cache_level);
3208 if (obj->has_aliasing_ppgtt_mapping)
3209 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
3210 obj, cache_level);
3211
3212 obj->gtt_space->color = cache_level;
3213 }
3214
3215 if (cache_level == I915_CACHE_NONE) {
3216 u32 old_read_domains, old_write_domain;
3217
3218 /* If we're coming from LLC cached, then we haven't
3219 * actually been tracking whether the data is in the
3220 * CPU cache or not, since we only allow one bit set
3221 * in obj->write_domain and have been skipping the clflushes.
3222 * Just set it to the CPU cache for now.
3223 */
3224 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3225 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3226
3227 old_read_domains = obj->base.read_domains;
3228 old_write_domain = obj->base.write_domain;
3229
3230 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3231 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3232
3233 trace_i915_gem_object_change_domain(obj,
3234 old_read_domains,
3235 old_write_domain);
3236 }
3237
3238 obj->cache_level = cache_level;
3239 i915_gem_verify_gtt(dev);
3240 return 0;
3241 }
3242
3243 int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3244 struct drm_file *file)
3245 {
3246 struct drm_i915_gem_caching *args = data;
3247 struct drm_i915_gem_object *obj;
3248 int ret;
3249
3250 ret = i915_mutex_lock_interruptible(dev);
3251 if (ret)
3252 return ret;
3253
3254 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3255 if (&obj->base == NULL) {
3256 ret = -ENOENT;
3257 goto unlock;
3258 }
3259
3260 args->caching = obj->cache_level != I915_CACHE_NONE;
3261
3262 drm_gem_object_unreference(&obj->base);
3263 unlock:
3264 mutex_unlock(&dev->struct_mutex);
3265 return ret;
3266 }
3267
3268 int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3269 struct drm_file *file)
3270 {
3271 struct drm_i915_gem_caching *args = data;
3272 struct drm_i915_gem_object *obj;
3273 enum i915_cache_level level;
3274 int ret;
3275
3276 switch (args->caching) {
3277 case I915_CACHING_NONE:
3278 level = I915_CACHE_NONE;
3279 break;
3280 case I915_CACHING_CACHED:
3281 level = I915_CACHE_LLC;
3282 break;
3283 default:
3284 return -EINVAL;
3285 }
3286
3287 ret = i915_mutex_lock_interruptible(dev);
3288 if (ret)
3289 return ret;
3290
3291 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3292 if (&obj->base == NULL) {
3293 ret = -ENOENT;
3294 goto unlock;
3295 }
3296
3297 ret = i915_gem_object_set_cache_level(obj, level);
3298
3299 drm_gem_object_unreference(&obj->base);
3300 unlock:
3301 mutex_unlock(&dev->struct_mutex);
3302 return ret;
3303 }
3304
3305 /*
3306 * Prepare buffer for display plane (scanout, cursors, etc).
3307 * Can be called from an uninterruptible phase (modesetting) and allows
3308 * any flushes to be pipelined (for pageflips).
3309 */
3310 int
3311 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3312 u32 alignment,
3313 struct intel_ring_buffer *pipelined)
3314 {
3315 u32 old_read_domains, old_write_domain;
3316 int ret;
3317
3318 if (pipelined != obj->ring) {
3319 ret = i915_gem_object_sync(obj, pipelined);
3320 if (ret)
3321 return ret;
3322 }
3323
3324 /* The display engine is not coherent with the LLC cache on gen6. As
3325 * a result, we make sure that the pinning that is about to occur is
3326 * done with uncached PTEs. This is lowest common denominator for all
3327 * chipsets.
3328 *
3329 * However for gen6+, we could do better by using the GFDT bit instead
3330 * of uncaching, which would allow us to flush all the LLC-cached data
3331 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3332 */
3333 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3334 if (ret)
3335 return ret;
3336
3337 /* As the user may map the buffer once pinned in the display plane
3338 * (e.g. libkms for the bootup splash), we have to ensure that we
3339 * always use map_and_fenceable for all scanout buffers.
3340 */
3341 ret = i915_gem_object_pin(obj, alignment, true, false);
3342 if (ret)
3343 return ret;
3344
3345 i915_gem_object_flush_cpu_write_domain(obj);
3346
3347 old_write_domain = obj->base.write_domain;
3348 old_read_domains = obj->base.read_domains;
3349
3350 /* It should now be out of any other write domains, and we can update
3351 * the domain values for our changes.
3352 */
3353 obj->base.write_domain = 0;
3354 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3355
3356 trace_i915_gem_object_change_domain(obj,
3357 old_read_domains,
3358 old_write_domain);
3359
3360 return 0;
3361 }
3362
3363 int
3364 i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
3365 {
3366 int ret;
3367
3368 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
3369 return 0;
3370
3371 ret = i915_gem_object_wait_rendering(obj, false);
3372 if (ret)
3373 return ret;
3374
3375 /* Ensure that we invalidate the GPU's caches and TLBs. */
3376 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3377 return 0;
3378 }
3379
3380 /**
3381 * Moves a single object to the CPU read, and possibly write domain.
3382 *
3383 * This function returns when the move is complete, including waiting on
3384 * flushes to occur.
3385 */
3386 int
3387 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
3388 {
3389 uint32_t old_write_domain, old_read_domains;
3390 int ret;
3391
3392 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3393 return 0;
3394
3395 ret = i915_gem_object_wait_rendering(obj, !write);
3396 if (ret)
3397 return ret;
3398
3399 i915_gem_object_flush_gtt_write_domain(obj);
3400
3401 old_write_domain = obj->base.write_domain;
3402 old_read_domains = obj->base.read_domains;
3403
3404 /* Flush the CPU cache if it's still invalid. */
3405 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3406 i915_gem_clflush_object(obj);
3407
3408 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3409 }
3410
3411 /* It should now be out of any other write domains, and we can update
3412 * the domain values for our changes.
3413 */
3414 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3415
3416 /* If we're writing through the CPU, then the GPU read domains will
3417 * need to be invalidated at next use.
3418 */
3419 if (write) {
3420 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3421 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3422 }
3423
3424 trace_i915_gem_object_change_domain(obj,
3425 old_read_domains,
3426 old_write_domain);
3427
3428 return 0;
3429 }
3430
3431 /* Throttle our rendering by waiting until the ring has completed our requests
3432 * emitted over 20 msec ago.
3433 *
3434 * Note that if we were to use the current jiffies each time around the loop,
3435 * we wouldn't escape the function with any frames outstanding if the time to
3436 * render a frame was over 20ms.
3437 *
3438 * This should get us reasonable parallelism between CPU and GPU but also
3439 * relatively low latency when blocking on a particular request to finish.
3440 */
3441 static int
3442 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3443 {
3444 struct drm_i915_private *dev_priv = dev->dev_private;
3445 struct drm_i915_file_private *file_priv = file->driver_priv;
3446 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3447 struct drm_i915_gem_request *request;
3448 struct intel_ring_buffer *ring = NULL;
3449 u32 seqno = 0;
3450 int ret;
3451
3452 if (atomic_read(&dev_priv->mm.wedged))
3453 return -EIO;
3454
3455 spin_lock(&file_priv->mm.lock);
3456 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3457 if (time_after_eq(request->emitted_jiffies, recent_enough))
3458 break;
3459
3460 ring = request->ring;
3461 seqno = request->seqno;
3462 }
3463 spin_unlock(&file_priv->mm.lock);
3464
3465 if (seqno == 0)
3466 return 0;
3467
3468 ret = __wait_seqno(ring, seqno, true, NULL);
3469 if (ret == 0)
3470 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3471
3472 return ret;
3473 }
3474
3475 int
3476 i915_gem_object_pin(struct drm_i915_gem_object *obj,
3477 uint32_t alignment,
3478 bool map_and_fenceable,
3479 bool nonblocking)
3480 {
3481 int ret;
3482
3483 if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
3484 return -EBUSY;
3485
3486 if (obj->gtt_space != NULL) {
3487 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3488 (map_and_fenceable && !obj->map_and_fenceable)) {
3489 WARN(obj->pin_count,
3490 "bo is already pinned with incorrect alignment:"
3491 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3492 " obj->map_and_fenceable=%d\n",
3493 obj->gtt_offset, alignment,
3494 map_and_fenceable,
3495 obj->map_and_fenceable);
3496 ret = i915_gem_object_unbind(obj);
3497 if (ret)
3498 return ret;
3499 }
3500 }
3501
3502 if (obj->gtt_space == NULL) {
3503 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3504
3505 ret = i915_gem_object_bind_to_gtt(obj, alignment,
3506 map_and_fenceable,
3507 nonblocking);
3508 if (ret)
3509 return ret;
3510
3511 if (!dev_priv->mm.aliasing_ppgtt)
3512 i915_gem_gtt_bind_object(obj, obj->cache_level);
3513 }
3514
3515 if (!obj->has_global_gtt_mapping && map_and_fenceable)
3516 i915_gem_gtt_bind_object(obj, obj->cache_level);
3517
3518 obj->pin_count++;
3519 obj->pin_mappable |= map_and_fenceable;
3520
3521 return 0;
3522 }
3523
3524 void
3525 i915_gem_object_unpin(struct drm_i915_gem_object *obj)
3526 {
3527 BUG_ON(obj->pin_count == 0);
3528 BUG_ON(obj->gtt_space == NULL);
3529
3530 if (--obj->pin_count == 0)
3531 obj->pin_mappable = false;
3532 }
3533
3534 int
3535 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3536 struct drm_file *file)
3537 {
3538 struct drm_i915_gem_pin *args = data;
3539 struct drm_i915_gem_object *obj;
3540 int ret;
3541
3542 ret = i915_mutex_lock_interruptible(dev);
3543 if (ret)
3544 return ret;
3545
3546 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3547 if (&obj->base == NULL) {
3548 ret = -ENOENT;
3549 goto unlock;
3550 }
3551
3552 if (obj->madv != I915_MADV_WILLNEED) {
3553 DRM_ERROR("Attempting to pin a purgeable buffer\n");
3554 ret = -EINVAL;
3555 goto out;
3556 }
3557
3558 if (obj->pin_filp != NULL && obj->pin_filp != file) {
3559 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3560 args->handle);
3561 ret = -EINVAL;
3562 goto out;
3563 }
3564
3565 if (obj->user_pin_count == 0) {
3566 ret = i915_gem_object_pin(obj, args->alignment, true, false);
3567 if (ret)
3568 goto out;
3569 }
3570
3571 obj->user_pin_count++;
3572 obj->pin_filp = file;
3573
3574 /* XXX - flush the CPU caches for pinned objects
3575 * as the X server doesn't manage domains yet
3576 */
3577 i915_gem_object_flush_cpu_write_domain(obj);
3578 args->offset = obj->gtt_offset;
3579 out:
3580 drm_gem_object_unreference(&obj->base);
3581 unlock:
3582 mutex_unlock(&dev->struct_mutex);
3583 return ret;
3584 }
3585
3586 int
3587 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3588 struct drm_file *file)
3589 {
3590 struct drm_i915_gem_pin *args = data;
3591 struct drm_i915_gem_object *obj;
3592 int ret;
3593
3594 ret = i915_mutex_lock_interruptible(dev);
3595 if (ret)
3596 return ret;
3597
3598 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3599 if (&obj->base == NULL) {
3600 ret = -ENOENT;
3601 goto unlock;
3602 }
3603
3604 if (obj->pin_filp != file) {
3605 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3606 args->handle);
3607 ret = -EINVAL;
3608 goto out;
3609 }
3610 obj->user_pin_count--;
3611 if (obj->user_pin_count == 0) {
3612 obj->pin_filp = NULL;
3613 i915_gem_object_unpin(obj);
3614 }
3615
3616 out:
3617 drm_gem_object_unreference(&obj->base);
3618 unlock:
3619 mutex_unlock(&dev->struct_mutex);
3620 return ret;
3621 }
3622
3623 int
3624 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3625 struct drm_file *file)
3626 {
3627 struct drm_i915_gem_busy *args = data;
3628 struct drm_i915_gem_object *obj;
3629 int ret;
3630
3631 ret = i915_mutex_lock_interruptible(dev);
3632 if (ret)
3633 return ret;
3634
3635 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3636 if (&obj->base == NULL) {
3637 ret = -ENOENT;
3638 goto unlock;
3639 }
3640
3641 /* Count all active objects as busy, even if they are currently not used
3642 * by the gpu. Users of this interface expect objects to eventually
3643 * become non-busy without any further actions, therefore emit any
3644 * necessary flushes here.
3645 */
3646 ret = i915_gem_object_flush_active(obj);
3647
3648 args->busy = obj->active;
3649 if (obj->ring) {
3650 BUILD_BUG_ON(I915_NUM_RINGS > 16);
3651 args->busy |= intel_ring_flag(obj->ring) << 16;
3652 }
3653
3654 drm_gem_object_unreference(&obj->base);
3655 unlock:
3656 mutex_unlock(&dev->struct_mutex);
3657 return ret;
3658 }
3659
3660 int
3661 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3662 struct drm_file *file_priv)
3663 {
3664 return i915_gem_ring_throttle(dev, file_priv);
3665 }
3666
3667 int
3668 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3669 struct drm_file *file_priv)
3670 {
3671 struct drm_i915_gem_madvise *args = data;
3672 struct drm_i915_gem_object *obj;
3673 int ret;
3674
3675 switch (args->madv) {
3676 case I915_MADV_DONTNEED:
3677 case I915_MADV_WILLNEED:
3678 break;
3679 default:
3680 return -EINVAL;
3681 }
3682
3683 ret = i915_mutex_lock_interruptible(dev);
3684 if (ret)
3685 return ret;
3686
3687 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
3688 if (&obj->base == NULL) {
3689 ret = -ENOENT;
3690 goto unlock;
3691 }
3692
3693 if (obj->pin_count) {
3694 ret = -EINVAL;
3695 goto out;
3696 }
3697
3698 if (obj->madv != __I915_MADV_PURGED)
3699 obj->madv = args->madv;
3700
3701 /* if the object is no longer attached, discard its backing storage */
3702 if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
3703 i915_gem_object_truncate(obj);
3704
3705 args->retained = obj->madv != __I915_MADV_PURGED;
3706
3707 out:
3708 drm_gem_object_unreference(&obj->base);
3709 unlock:
3710 mutex_unlock(&dev->struct_mutex);
3711 return ret;
3712 }
3713
3714 void i915_gem_object_init(struct drm_i915_gem_object *obj,
3715 const struct drm_i915_gem_object_ops *ops)
3716 {
3717 INIT_LIST_HEAD(&obj->mm_list);
3718 INIT_LIST_HEAD(&obj->gtt_list);
3719 INIT_LIST_HEAD(&obj->ring_list);
3720 INIT_LIST_HEAD(&obj->exec_list);
3721
3722 obj->ops = ops;
3723
3724 obj->fence_reg = I915_FENCE_REG_NONE;
3725 obj->madv = I915_MADV_WILLNEED;
3726 /* Avoid an unnecessary call to unbind on the first bind. */
3727 obj->map_and_fenceable = true;
3728
3729 i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
3730 }
3731
3732 static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
3733 .get_pages = i915_gem_object_get_pages_gtt,
3734 .put_pages = i915_gem_object_put_pages_gtt,
3735 };
3736
3737 struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3738 size_t size)
3739 {
3740 struct drm_i915_gem_object *obj;
3741 struct address_space *mapping;
3742 u32 mask;
3743
3744 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3745 if (obj == NULL)
3746 return NULL;
3747
3748 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3749 kfree(obj);
3750 return NULL;
3751 }
3752
3753 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
3754 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
3755 /* 965gm cannot relocate objects above 4GiB. */
3756 mask &= ~__GFP_HIGHMEM;
3757 mask |= __GFP_DMA32;
3758 }
3759
3760 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3761 mapping_set_gfp_mask(mapping, mask);
3762
3763 i915_gem_object_init(obj, &i915_gem_object_ops);
3764
3765 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3766 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3767
3768 if (HAS_LLC(dev)) {
3769 /* On some devices, we can have the GPU use the LLC (the CPU
3770 * cache) for about a 10% performance improvement
3771 * compared to uncached. Graphics requests other than
3772 * display scanout are coherent with the CPU in
3773 * accessing this cache. This means in this mode we
3774 * don't need to clflush on the CPU side, and on the
3775 * GPU side we only need to flush internal caches to
3776 * get data visible to the CPU.
3777 *
3778 * However, we maintain the display planes as UC, and so
3779 * need to rebind when first used as such.
3780 */
3781 obj->cache_level = I915_CACHE_LLC;
3782 } else
3783 obj->cache_level = I915_CACHE_NONE;
3784
3785 return obj;
3786 }
3787
3788 int i915_gem_init_object(struct drm_gem_object *obj)
3789 {
3790 BUG();
3791
3792 return 0;
3793 }
3794
3795 void i915_gem_free_object(struct drm_gem_object *gem_obj)
3796 {
3797 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3798 struct drm_device *dev = obj->base.dev;
3799 drm_i915_private_t *dev_priv = dev->dev_private;
3800
3801 trace_i915_gem_object_destroy(obj);
3802
3803 if (obj->phys_obj)
3804 i915_gem_detach_phys_object(dev, obj);
3805
3806 obj->pin_count = 0;
3807 if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
3808 bool was_interruptible;
3809
3810 was_interruptible = dev_priv->mm.interruptible;
3811 dev_priv->mm.interruptible = false;
3812
3813 WARN_ON(i915_gem_object_unbind(obj));
3814
3815 dev_priv->mm.interruptible = was_interruptible;
3816 }
3817
3818 obj->pages_pin_count = 0;
3819 i915_gem_object_put_pages(obj);
3820 i915_gem_object_free_mmap_offset(obj);
3821
3822 BUG_ON(obj->pages);
3823
3824 if (obj->base.import_attach)
3825 drm_prime_gem_destroy(&obj->base, NULL);
3826
3827 drm_gem_object_release(&obj->base);
3828 i915_gem_info_remove_obj(dev_priv, obj->base.size);
3829
3830 kfree(obj->bit_17);
3831 kfree(obj);
3832 }
3833
3834 int
3835 i915_gem_idle(struct drm_device *dev)
3836 {
3837 drm_i915_private_t *dev_priv = dev->dev_private;
3838 int ret;
3839
3840 mutex_lock(&dev->struct_mutex);
3841
3842 if (dev_priv->mm.suspended) {
3843 mutex_unlock(&dev->struct_mutex);
3844 return 0;
3845 }
3846
3847 ret = i915_gpu_idle(dev);
3848 if (ret) {
3849 mutex_unlock(&dev->struct_mutex);
3850 return ret;
3851 }
3852 i915_gem_retire_requests(dev);
3853
3854 /* Under UMS, be paranoid and evict. */
3855 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3856 i915_gem_evict_everything(dev);
3857
3858 i915_gem_reset_fences(dev);
3859
3860 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3861 * We need to replace this with a semaphore, or something.
3862 * And not confound mm.suspended!
3863 */
3864 dev_priv->mm.suspended = 1;
3865 del_timer_sync(&dev_priv->hangcheck_timer);
3866
3867 i915_kernel_lost_context(dev);
3868 i915_gem_cleanup_ringbuffer(dev);
3869
3870 mutex_unlock(&dev->struct_mutex);
3871
3872 /* Cancel the retire work handler, which should be idle now. */
3873 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3874
3875 return 0;
3876 }
3877
3878 void i915_gem_l3_remap(struct drm_device *dev)
3879 {
3880 drm_i915_private_t *dev_priv = dev->dev_private;
3881 u32 misccpctl;
3882 int i;
3883
3884 if (!IS_IVYBRIDGE(dev))
3885 return;
3886
3887 if (!dev_priv->l3_parity.remap_info)
3888 return;
3889
3890 misccpctl = I915_READ(GEN7_MISCCPCTL);
3891 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
3892 POSTING_READ(GEN7_MISCCPCTL);
3893
3894 for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
3895 u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
3896 if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
3897 DRM_DEBUG("0x%x was already programmed to %x\n",
3898 GEN7_L3LOG_BASE + i, remap);
3899 if (remap && !dev_priv->l3_parity.remap_info[i/4])
3900 DRM_DEBUG_DRIVER("Clearing remapped register\n");
3901 I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
3902 }
3903
3904 /* Make sure all the writes land before disabling dop clock gating */
3905 POSTING_READ(GEN7_L3LOG_BASE);
3906
3907 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
3908 }
3909
3910 void i915_gem_init_swizzling(struct drm_device *dev)
3911 {
3912 drm_i915_private_t *dev_priv = dev->dev_private;
3913
3914 if (INTEL_INFO(dev)->gen < 5 ||
3915 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3916 return;
3917
3918 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3919 DISP_TILE_SURFACE_SWIZZLING);
3920
3921 if (IS_GEN5(dev))
3922 return;
3923
3924 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3925 if (IS_GEN6(dev))
3926 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
3927 else
3928 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
3929 }
3930
3931 static bool
3932 intel_enable_blt(struct drm_device *dev)
3933 {
3934 if (!HAS_BLT(dev))
3935 return false;
3936
3937 /* The blitter was dysfunctional on early prototypes */
3938 if (IS_GEN6(dev) && dev->pdev->revision < 8) {
3939 DRM_INFO("BLT not supported on this pre-production hardware;"
3940 " graphics performance will be degraded.\n");
3941 return false;
3942 }
3943
3944 return true;
3945 }
3946
3947 int
3948 i915_gem_init_hw(struct drm_device *dev)
3949 {
3950 drm_i915_private_t *dev_priv = dev->dev_private;
3951 int ret;
3952
3953 if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
3954 return -EIO;
3955
3956 if (IS_HASWELL(dev) && (I915_READ(0x120010) == 1))
3957 I915_WRITE(0x9008, I915_READ(0x9008) | 0xf0000);
3958
3959 i915_gem_l3_remap(dev);
3960
3961 i915_gem_init_swizzling(dev);
3962
3963 ret = intel_init_render_ring_buffer(dev);
3964 if (ret)
3965 return ret;
3966
3967 if (HAS_BSD(dev)) {
3968 ret = intel_init_bsd_ring_buffer(dev);
3969 if (ret)
3970 goto cleanup_render_ring;
3971 }
3972
3973 if (intel_enable_blt(dev)) {
3974 ret = intel_init_blt_ring_buffer(dev);
3975 if (ret)
3976 goto cleanup_bsd_ring;
3977 }
3978
3979 dev_priv->next_seqno = 1;
3980
3981 /*
3982 * XXX: There was some w/a described somewhere suggesting loading
3983 * contexts before PPGTT.
3984 */
3985 i915_gem_context_init(dev);
3986 i915_gem_init_ppgtt(dev);
3987
3988 return 0;
3989
3990 cleanup_bsd_ring:
3991 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
3992 cleanup_render_ring:
3993 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
3994 return ret;
3995 }
3996
3997 static bool
3998 intel_enable_ppgtt(struct drm_device *dev)
3999 {
4000 if (i915_enable_ppgtt >= 0)
4001 return i915_enable_ppgtt;
4002
4003 #ifdef CONFIG_INTEL_IOMMU
4004 /* Disable ppgtt on SNB if VT-d is on. */
4005 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
4006 return false;
4007 #endif
4008
4009 return true;
4010 }
4011
4012 int i915_gem_init(struct drm_device *dev)
4013 {
4014 struct drm_i915_private *dev_priv = dev->dev_private;
4015 unsigned long gtt_size, mappable_size;
4016 int ret;
4017
4018 gtt_size = dev_priv->mm.gtt->gtt_total_entries << PAGE_SHIFT;
4019 mappable_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
4020
4021 mutex_lock(&dev->struct_mutex);
4022 if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
4023 /* PPGTT pdes are stolen from global gtt ptes, so shrink the
4024 * aperture accordingly when using aliasing ppgtt. */
4025 gtt_size -= I915_PPGTT_PD_ENTRIES*PAGE_SIZE;
4026
4027 i915_gem_init_global_gtt(dev, 0, mappable_size, gtt_size);
4028
4029 ret = i915_gem_init_aliasing_ppgtt(dev);
4030 if (ret) {
4031 mutex_unlock(&dev->struct_mutex);
4032 return ret;
4033 }
4034 } else {
4035 /* Let GEM Manage all of the aperture.
4036 *
4037 * However, leave one page at the end still bound to the scratch
4038 * page. There are a number of places where the hardware
4039 * apparently prefetches past the end of the object, and we've
4040 * seen multiple hangs with the GPU head pointer stuck in a
4041 * batchbuffer bound at the last page of the aperture. One page
4042 * should be enough to keep any prefetching inside of the
4043 * aperture.
4044 */
4045 i915_gem_init_global_gtt(dev, 0, mappable_size,
4046 gtt_size);
4047 }
4048
4049 ret = i915_gem_init_hw(dev);
4050 mutex_unlock(&dev->struct_mutex);
4051 if (ret) {
4052 i915_gem_cleanup_aliasing_ppgtt(dev);
4053 return ret;
4054 }
4055
4056 /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
4057 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4058 dev_priv->dri1.allow_batchbuffer = 1;
4059 return 0;
4060 }
4061
4062 void
4063 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4064 {
4065 drm_i915_private_t *dev_priv = dev->dev_private;
4066 struct intel_ring_buffer *ring;
4067 int i;
4068
4069 for_each_ring(ring, dev_priv, i)
4070 intel_cleanup_ring_buffer(ring);
4071 }
4072
4073 int
4074 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4075 struct drm_file *file_priv)
4076 {
4077 drm_i915_private_t *dev_priv = dev->dev_private;
4078 int ret;
4079
4080 if (drm_core_check_feature(dev, DRIVER_MODESET))
4081 return 0;
4082
4083 if (atomic_read(&dev_priv->mm.wedged)) {
4084 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4085 atomic_set(&dev_priv->mm.wedged, 0);
4086 }
4087
4088 mutex_lock(&dev->struct_mutex);
4089 dev_priv->mm.suspended = 0;
4090
4091 ret = i915_gem_init_hw(dev);
4092 if (ret != 0) {
4093 mutex_unlock(&dev->struct_mutex);
4094 return ret;
4095 }
4096
4097 BUG_ON(!list_empty(&dev_priv->mm.active_list));
4098 mutex_unlock(&dev->struct_mutex);
4099
4100 ret = drm_irq_install(dev);
4101 if (ret)
4102 goto cleanup_ringbuffer;
4103
4104 return 0;
4105
4106 cleanup_ringbuffer:
4107 mutex_lock(&dev->struct_mutex);
4108 i915_gem_cleanup_ringbuffer(dev);
4109 dev_priv->mm.suspended = 1;
4110 mutex_unlock(&dev->struct_mutex);
4111
4112 return ret;
4113 }
4114
4115 int
4116 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4117 struct drm_file *file_priv)
4118 {
4119 if (drm_core_check_feature(dev, DRIVER_MODESET))
4120 return 0;
4121
4122 drm_irq_uninstall(dev);
4123 return i915_gem_idle(dev);
4124 }
4125
4126 void
4127 i915_gem_lastclose(struct drm_device *dev)
4128 {
4129 int ret;
4130
4131 if (drm_core_check_feature(dev, DRIVER_MODESET))
4132 return;
4133
4134 ret = i915_gem_idle(dev);
4135 if (ret)
4136 DRM_ERROR("failed to idle hardware: %d\n", ret);
4137 }
4138
4139 static void
4140 init_ring_lists(struct intel_ring_buffer *ring)
4141 {
4142 INIT_LIST_HEAD(&ring->active_list);
4143 INIT_LIST_HEAD(&ring->request_list);
4144 }
4145
4146 void
4147 i915_gem_load(struct drm_device *dev)
4148 {
4149 int i;
4150 drm_i915_private_t *dev_priv = dev->dev_private;
4151
4152 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4153 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4154 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4155 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
4156 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4157 for (i = 0; i < I915_NUM_RINGS; i++)
4158 init_ring_lists(&dev_priv->ring[i]);
4159 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
4160 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
4161 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4162 i915_gem_retire_work_handler);
4163 init_completion(&dev_priv->error_completion);
4164
4165 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4166 if (IS_GEN3(dev)) {
4167 I915_WRITE(MI_ARB_STATE,
4168 _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
4169 }
4170
4171 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4172
4173 /* Old X drivers will take 0-2 for front, back, depth buffers */
4174 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4175 dev_priv->fence_reg_start = 3;
4176
4177 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4178 dev_priv->num_fence_regs = 16;
4179 else
4180 dev_priv->num_fence_regs = 8;
4181
4182 /* Initialize fence registers to zero */
4183 i915_gem_reset_fences(dev);
4184
4185 i915_gem_detect_bit_6_swizzle(dev);
4186 init_waitqueue_head(&dev_priv->pending_flip_queue);
4187
4188 dev_priv->mm.interruptible = true;
4189
4190 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4191 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4192 register_shrinker(&dev_priv->mm.inactive_shrinker);
4193 }
4194
4195 /*
4196 * Create a physically contiguous memory object for this object
4197 * e.g. for cursor + overlay regs
4198 */
4199 static int i915_gem_init_phys_object(struct drm_device *dev,
4200 int id, int size, int align)
4201 {
4202 drm_i915_private_t *dev_priv = dev->dev_private;
4203 struct drm_i915_gem_phys_object *phys_obj;
4204 int ret;
4205
4206 if (dev_priv->mm.phys_objs[id - 1] || !size)
4207 return 0;
4208
4209 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
4210 if (!phys_obj)
4211 return -ENOMEM;
4212
4213 phys_obj->id = id;
4214
4215 phys_obj->handle = drm_pci_alloc(dev, size, align);
4216 if (!phys_obj->handle) {
4217 ret = -ENOMEM;
4218 goto kfree_obj;
4219 }
4220 #ifdef CONFIG_X86
4221 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4222 #endif
4223
4224 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4225
4226 return 0;
4227 kfree_obj:
4228 kfree(phys_obj);
4229 return ret;
4230 }
4231
4232 static void i915_gem_free_phys_object(struct drm_device *dev, int id)
4233 {
4234 drm_i915_private_t *dev_priv = dev->dev_private;
4235 struct drm_i915_gem_phys_object *phys_obj;
4236
4237 if (!dev_priv->mm.phys_objs[id - 1])
4238 return;
4239
4240 phys_obj = dev_priv->mm.phys_objs[id - 1];
4241 if (phys_obj->cur_obj) {
4242 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4243 }
4244
4245 #ifdef CONFIG_X86
4246 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4247 #endif
4248 drm_pci_free(dev, phys_obj->handle);
4249 kfree(phys_obj);
4250 dev_priv->mm.phys_objs[id - 1] = NULL;
4251 }
4252
4253 void i915_gem_free_all_phys_object(struct drm_device *dev)
4254 {
4255 int i;
4256
4257 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
4258 i915_gem_free_phys_object(dev, i);
4259 }
4260
4261 void i915_gem_detach_phys_object(struct drm_device *dev,
4262 struct drm_i915_gem_object *obj)
4263 {
4264 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4265 char *vaddr;
4266 int i;
4267 int page_count;
4268
4269 if (!obj->phys_obj)
4270 return;
4271 vaddr = obj->phys_obj->handle->vaddr;
4272
4273 page_count = obj->base.size / PAGE_SIZE;
4274 for (i = 0; i < page_count; i++) {
4275 struct page *page = shmem_read_mapping_page(mapping, i);
4276 if (!IS_ERR(page)) {
4277 char *dst = kmap_atomic(page);
4278 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4279 kunmap_atomic(dst);
4280
4281 drm_clflush_pages(&page, 1);
4282
4283 set_page_dirty(page);
4284 mark_page_accessed(page);
4285 page_cache_release(page);
4286 }
4287 }
4288 i915_gem_chipset_flush(dev);
4289
4290 obj->phys_obj->cur_obj = NULL;
4291 obj->phys_obj = NULL;
4292 }
4293
4294 int
4295 i915_gem_attach_phys_object(struct drm_device *dev,
4296 struct drm_i915_gem_object *obj,
4297 int id,
4298 int align)
4299 {
4300 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4301 drm_i915_private_t *dev_priv = dev->dev_private;
4302 int ret = 0;
4303 int page_count;
4304 int i;
4305
4306 if (id > I915_MAX_PHYS_OBJECT)
4307 return -EINVAL;
4308
4309 if (obj->phys_obj) {
4310 if (obj->phys_obj->id == id)
4311 return 0;
4312 i915_gem_detach_phys_object(dev, obj);
4313 }
4314
4315 /* create a new object */
4316 if (!dev_priv->mm.phys_objs[id - 1]) {
4317 ret = i915_gem_init_phys_object(dev, id,
4318 obj->base.size, align);
4319 if (ret) {
4320 DRM_ERROR("failed to init phys object %d size: %zu\n",
4321 id, obj->base.size);
4322 return ret;
4323 }
4324 }
4325
4326 /* bind to the object */
4327 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4328 obj->phys_obj->cur_obj = obj;
4329
4330 page_count = obj->base.size / PAGE_SIZE;
4331
4332 for (i = 0; i < page_count; i++) {
4333 struct page *page;
4334 char *dst, *src;
4335
4336 page = shmem_read_mapping_page(mapping, i);
4337 if (IS_ERR(page))
4338 return PTR_ERR(page);
4339
4340 src = kmap_atomic(page);
4341 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4342 memcpy(dst, src, PAGE_SIZE);
4343 kunmap_atomic(src);
4344
4345 mark_page_accessed(page);
4346 page_cache_release(page);
4347 }
4348
4349 return 0;
4350 }
4351
4352 static int
4353 i915_gem_phys_pwrite(struct drm_device *dev,
4354 struct drm_i915_gem_object *obj,
4355 struct drm_i915_gem_pwrite *args,
4356 struct drm_file *file_priv)
4357 {
4358 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
4359 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
4360
4361 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4362 unsigned long unwritten;
4363
4364 /* The physical object once assigned is fixed for the lifetime
4365 * of the obj, so we can safely drop the lock and continue
4366 * to access vaddr.
4367 */
4368 mutex_unlock(&dev->struct_mutex);
4369 unwritten = copy_from_user(vaddr, user_data, args->size);
4370 mutex_lock(&dev->struct_mutex);
4371 if (unwritten)
4372 return -EFAULT;
4373 }
4374
4375 i915_gem_chipset_flush(dev);
4376 return 0;
4377 }
4378
4379 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
4380 {
4381 struct drm_i915_file_private *file_priv = file->driver_priv;
4382
4383 /* Clean up our request list when the client is going away, so that
4384 * later retire_requests won't dereference our soon-to-be-gone
4385 * file_priv.
4386 */
4387 spin_lock(&file_priv->mm.lock);
4388 while (!list_empty(&file_priv->mm.request_list)) {
4389 struct drm_i915_gem_request *request;
4390
4391 request = list_first_entry(&file_priv->mm.request_list,
4392 struct drm_i915_gem_request,
4393 client_list);
4394 list_del(&request->client_list);
4395 request->file_priv = NULL;
4396 }
4397 spin_unlock(&file_priv->mm.lock);
4398 }
4399
4400 static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
4401 {
4402 if (!mutex_is_locked(mutex))
4403 return false;
4404
4405 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
4406 return mutex->owner == task;
4407 #else
4408 /* Since UP may be pre-empted, we cannot assume that we own the lock */
4409 return false;
4410 #endif
4411 }
4412
4413 static int
4414 i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
4415 {
4416 struct drm_i915_private *dev_priv =
4417 container_of(shrinker,
4418 struct drm_i915_private,
4419 mm.inactive_shrinker);
4420 struct drm_device *dev = dev_priv->dev;
4421 struct drm_i915_gem_object *obj;
4422 int nr_to_scan = sc->nr_to_scan;
4423 bool unlock = true;
4424 int cnt;
4425
4426 if (!mutex_trylock(&dev->struct_mutex)) {
4427 if (!mutex_is_locked_by(&dev->struct_mutex, current))
4428 return 0;
4429
4430 if (dev_priv->mm.shrinker_no_lock_stealing)
4431 return 0;
4432
4433 unlock = false;
4434 }
4435
4436 if (nr_to_scan) {
4437 nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
4438 if (nr_to_scan > 0)
4439 nr_to_scan -= __i915_gem_shrink(dev_priv, nr_to_scan,
4440 false);
4441 if (nr_to_scan > 0)
4442 i915_gem_shrink_all(dev_priv);
4443 }
4444
4445 cnt = 0;
4446 list_for_each_entry(obj, &dev_priv->mm.unbound_list, gtt_list)
4447 if (obj->pages_pin_count == 0)
4448 cnt += obj->base.size >> PAGE_SHIFT;
4449 list_for_each_entry(obj, &dev_priv->mm.inactive_list, gtt_list)
4450 if (obj->pin_count == 0 && obj->pages_pin_count == 0)
4451 cnt += obj->base.size >> PAGE_SHIFT;
4452
4453 if (unlock)
4454 mutex_unlock(&dev->struct_mutex);
4455 return cnt;
4456 }
4457