drm/i915: Also perform gpu reset under execlist mode.
[deliverable/linux.git] / drivers / gpu / drm / i915 / i915_gem.c
1 /*
2 * Copyright © 2008-2015 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@anholt.net>
25 *
26 */
27
28 #include <drm/drmP.h>
29 #include <drm/drm_vma_manager.h>
30 #include <drm/i915_drm.h>
31 #include "i915_drv.h"
32 #include "i915_vgpu.h"
33 #include "i915_trace.h"
34 #include "intel_drv.h"
35 #include <linux/shmem_fs.h>
36 #include <linux/slab.h>
37 #include <linux/swap.h>
38 #include <linux/pci.h>
39 #include <linux/dma-buf.h>
40
41 #define RQ_BUG_ON(expr)
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 void
46 i915_gem_object_retire__write(struct drm_i915_gem_object *obj);
47 static void
48 i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring);
49 static void i915_gem_write_fence(struct drm_device *dev, int reg,
50 struct drm_i915_gem_object *obj);
51 static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
52 struct drm_i915_fence_reg *fence,
53 bool enable);
54
55 static bool cpu_cache_is_coherent(struct drm_device *dev,
56 enum i915_cache_level level)
57 {
58 return HAS_LLC(dev) || level != I915_CACHE_NONE;
59 }
60
61 static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
62 {
63 if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
64 return true;
65
66 return obj->pin_display;
67 }
68
69 static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
70 {
71 if (obj->tiling_mode)
72 i915_gem_release_mmap(obj);
73
74 /* As we do not have an associated fence register, we will force
75 * a tiling change if we ever need to acquire one.
76 */
77 obj->fence_dirty = false;
78 obj->fence_reg = I915_FENCE_REG_NONE;
79 }
80
81 /* some bookkeeping */
82 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
83 size_t size)
84 {
85 spin_lock(&dev_priv->mm.object_stat_lock);
86 dev_priv->mm.object_count++;
87 dev_priv->mm.object_memory += size;
88 spin_unlock(&dev_priv->mm.object_stat_lock);
89 }
90
91 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
92 size_t size)
93 {
94 spin_lock(&dev_priv->mm.object_stat_lock);
95 dev_priv->mm.object_count--;
96 dev_priv->mm.object_memory -= size;
97 spin_unlock(&dev_priv->mm.object_stat_lock);
98 }
99
100 static int
101 i915_gem_wait_for_error(struct i915_gpu_error *error)
102 {
103 int ret;
104
105 #define EXIT_COND (!i915_reset_in_progress(error) || \
106 i915_terminally_wedged(error))
107 if (EXIT_COND)
108 return 0;
109
110 /*
111 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
112 * userspace. If it takes that long something really bad is going on and
113 * we should simply try to bail out and fail as gracefully as possible.
114 */
115 ret = wait_event_interruptible_timeout(error->reset_queue,
116 EXIT_COND,
117 10*HZ);
118 if (ret == 0) {
119 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
120 return -EIO;
121 } else if (ret < 0) {
122 return ret;
123 }
124 #undef EXIT_COND
125
126 return 0;
127 }
128
129 int i915_mutex_lock_interruptible(struct drm_device *dev)
130 {
131 struct drm_i915_private *dev_priv = dev->dev_private;
132 int ret;
133
134 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
135 if (ret)
136 return ret;
137
138 ret = mutex_lock_interruptible(&dev->struct_mutex);
139 if (ret)
140 return ret;
141
142 WARN_ON(i915_verify_lists(dev));
143 return 0;
144 }
145
146 int
147 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
148 struct drm_file *file)
149 {
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 struct drm_i915_gem_get_aperture *args = data;
152 struct i915_gtt *ggtt = &dev_priv->gtt;
153 struct i915_vma *vma;
154 size_t pinned;
155
156 pinned = 0;
157 mutex_lock(&dev->struct_mutex);
158 list_for_each_entry(vma, &ggtt->base.active_list, mm_list)
159 if (vma->pin_count)
160 pinned += vma->node.size;
161 list_for_each_entry(vma, &ggtt->base.inactive_list, mm_list)
162 if (vma->pin_count)
163 pinned += vma->node.size;
164 mutex_unlock(&dev->struct_mutex);
165
166 args->aper_size = dev_priv->gtt.base.total;
167 args->aper_available_size = args->aper_size - pinned;
168
169 return 0;
170 }
171
172 static int
173 i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
174 {
175 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
176 char *vaddr = obj->phys_handle->vaddr;
177 struct sg_table *st;
178 struct scatterlist *sg;
179 int i;
180
181 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
182 return -EINVAL;
183
184 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
185 struct page *page;
186 char *src;
187
188 page = shmem_read_mapping_page(mapping, i);
189 if (IS_ERR(page))
190 return PTR_ERR(page);
191
192 src = kmap_atomic(page);
193 memcpy(vaddr, src, PAGE_SIZE);
194 drm_clflush_virt_range(vaddr, PAGE_SIZE);
195 kunmap_atomic(src);
196
197 page_cache_release(page);
198 vaddr += PAGE_SIZE;
199 }
200
201 i915_gem_chipset_flush(obj->base.dev);
202
203 st = kmalloc(sizeof(*st), GFP_KERNEL);
204 if (st == NULL)
205 return -ENOMEM;
206
207 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
208 kfree(st);
209 return -ENOMEM;
210 }
211
212 sg = st->sgl;
213 sg->offset = 0;
214 sg->length = obj->base.size;
215
216 sg_dma_address(sg) = obj->phys_handle->busaddr;
217 sg_dma_len(sg) = obj->base.size;
218
219 obj->pages = st;
220 obj->has_dma_mapping = true;
221 return 0;
222 }
223
224 static void
225 i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
226 {
227 int ret;
228
229 BUG_ON(obj->madv == __I915_MADV_PURGED);
230
231 ret = i915_gem_object_set_to_cpu_domain(obj, true);
232 if (ret) {
233 /* In the event of a disaster, abandon all caches and
234 * hope for the best.
235 */
236 WARN_ON(ret != -EIO);
237 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
238 }
239
240 if (obj->madv == I915_MADV_DONTNEED)
241 obj->dirty = 0;
242
243 if (obj->dirty) {
244 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
245 char *vaddr = obj->phys_handle->vaddr;
246 int i;
247
248 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
249 struct page *page;
250 char *dst;
251
252 page = shmem_read_mapping_page(mapping, i);
253 if (IS_ERR(page))
254 continue;
255
256 dst = kmap_atomic(page);
257 drm_clflush_virt_range(vaddr, PAGE_SIZE);
258 memcpy(dst, vaddr, PAGE_SIZE);
259 kunmap_atomic(dst);
260
261 set_page_dirty(page);
262 if (obj->madv == I915_MADV_WILLNEED)
263 mark_page_accessed(page);
264 page_cache_release(page);
265 vaddr += PAGE_SIZE;
266 }
267 obj->dirty = 0;
268 }
269
270 sg_free_table(obj->pages);
271 kfree(obj->pages);
272
273 obj->has_dma_mapping = false;
274 }
275
276 static void
277 i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
278 {
279 drm_pci_free(obj->base.dev, obj->phys_handle);
280 }
281
282 static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
283 .get_pages = i915_gem_object_get_pages_phys,
284 .put_pages = i915_gem_object_put_pages_phys,
285 .release = i915_gem_object_release_phys,
286 };
287
288 static int
289 drop_pages(struct drm_i915_gem_object *obj)
290 {
291 struct i915_vma *vma, *next;
292 int ret;
293
294 drm_gem_object_reference(&obj->base);
295 list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link)
296 if (i915_vma_unbind(vma))
297 break;
298
299 ret = i915_gem_object_put_pages(obj);
300 drm_gem_object_unreference(&obj->base);
301
302 return ret;
303 }
304
305 int
306 i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
307 int align)
308 {
309 drm_dma_handle_t *phys;
310 int ret;
311
312 if (obj->phys_handle) {
313 if ((unsigned long)obj->phys_handle->vaddr & (align -1))
314 return -EBUSY;
315
316 return 0;
317 }
318
319 if (obj->madv != I915_MADV_WILLNEED)
320 return -EFAULT;
321
322 if (obj->base.filp == NULL)
323 return -EINVAL;
324
325 ret = drop_pages(obj);
326 if (ret)
327 return ret;
328
329 /* create a new object */
330 phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
331 if (!phys)
332 return -ENOMEM;
333
334 obj->phys_handle = phys;
335 obj->ops = &i915_gem_phys_ops;
336
337 return i915_gem_object_get_pages(obj);
338 }
339
340 static int
341 i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
342 struct drm_i915_gem_pwrite *args,
343 struct drm_file *file_priv)
344 {
345 struct drm_device *dev = obj->base.dev;
346 void *vaddr = obj->phys_handle->vaddr + args->offset;
347 char __user *user_data = to_user_ptr(args->data_ptr);
348 int ret = 0;
349
350 /* We manually control the domain here and pretend that it
351 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
352 */
353 ret = i915_gem_object_wait_rendering(obj, false);
354 if (ret)
355 return ret;
356
357 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
358 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
359 unsigned long unwritten;
360
361 /* The physical object once assigned is fixed for the lifetime
362 * of the obj, so we can safely drop the lock and continue
363 * to access vaddr.
364 */
365 mutex_unlock(&dev->struct_mutex);
366 unwritten = copy_from_user(vaddr, user_data, args->size);
367 mutex_lock(&dev->struct_mutex);
368 if (unwritten) {
369 ret = -EFAULT;
370 goto out;
371 }
372 }
373
374 drm_clflush_virt_range(vaddr, args->size);
375 i915_gem_chipset_flush(dev);
376
377 out:
378 intel_fb_obj_flush(obj, false);
379 return ret;
380 }
381
382 void *i915_gem_object_alloc(struct drm_device *dev)
383 {
384 struct drm_i915_private *dev_priv = dev->dev_private;
385 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
386 }
387
388 void i915_gem_object_free(struct drm_i915_gem_object *obj)
389 {
390 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
391 kmem_cache_free(dev_priv->objects, obj);
392 }
393
394 static int
395 i915_gem_create(struct drm_file *file,
396 struct drm_device *dev,
397 uint64_t size,
398 uint32_t *handle_p)
399 {
400 struct drm_i915_gem_object *obj;
401 int ret;
402 u32 handle;
403
404 size = roundup(size, PAGE_SIZE);
405 if (size == 0)
406 return -EINVAL;
407
408 /* Allocate the new object */
409 obj = i915_gem_alloc_object(dev, size);
410 if (obj == NULL)
411 return -ENOMEM;
412
413 ret = drm_gem_handle_create(file, &obj->base, &handle);
414 /* drop reference from allocate - handle holds it now */
415 drm_gem_object_unreference_unlocked(&obj->base);
416 if (ret)
417 return ret;
418
419 *handle_p = handle;
420 return 0;
421 }
422
423 int
424 i915_gem_dumb_create(struct drm_file *file,
425 struct drm_device *dev,
426 struct drm_mode_create_dumb *args)
427 {
428 /* have to work out size/pitch and return them */
429 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
430 args->size = args->pitch * args->height;
431 return i915_gem_create(file, dev,
432 args->size, &args->handle);
433 }
434
435 /**
436 * Creates a new mm object and returns a handle to it.
437 */
438 int
439 i915_gem_create_ioctl(struct drm_device *dev, void *data,
440 struct drm_file *file)
441 {
442 struct drm_i915_gem_create *args = data;
443
444 return i915_gem_create(file, dev,
445 args->size, &args->handle);
446 }
447
448 static inline int
449 __copy_to_user_swizzled(char __user *cpu_vaddr,
450 const char *gpu_vaddr, int gpu_offset,
451 int length)
452 {
453 int ret, cpu_offset = 0;
454
455 while (length > 0) {
456 int cacheline_end = ALIGN(gpu_offset + 1, 64);
457 int this_length = min(cacheline_end - gpu_offset, length);
458 int swizzled_gpu_offset = gpu_offset ^ 64;
459
460 ret = __copy_to_user(cpu_vaddr + cpu_offset,
461 gpu_vaddr + swizzled_gpu_offset,
462 this_length);
463 if (ret)
464 return ret + length;
465
466 cpu_offset += this_length;
467 gpu_offset += this_length;
468 length -= this_length;
469 }
470
471 return 0;
472 }
473
474 static inline int
475 __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
476 const char __user *cpu_vaddr,
477 int length)
478 {
479 int ret, cpu_offset = 0;
480
481 while (length > 0) {
482 int cacheline_end = ALIGN(gpu_offset + 1, 64);
483 int this_length = min(cacheline_end - gpu_offset, length);
484 int swizzled_gpu_offset = gpu_offset ^ 64;
485
486 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
487 cpu_vaddr + cpu_offset,
488 this_length);
489 if (ret)
490 return ret + length;
491
492 cpu_offset += this_length;
493 gpu_offset += this_length;
494 length -= this_length;
495 }
496
497 return 0;
498 }
499
500 /*
501 * Pins the specified object's pages and synchronizes the object with
502 * GPU accesses. Sets needs_clflush to non-zero if the caller should
503 * flush the object from the CPU cache.
504 */
505 int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
506 int *needs_clflush)
507 {
508 int ret;
509
510 *needs_clflush = 0;
511
512 if (!obj->base.filp)
513 return -EINVAL;
514
515 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
516 /* If we're not in the cpu read domain, set ourself into the gtt
517 * read domain and manually flush cachelines (if required). This
518 * optimizes for the case when the gpu will dirty the data
519 * anyway again before the next pread happens. */
520 *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
521 obj->cache_level);
522 ret = i915_gem_object_wait_rendering(obj, true);
523 if (ret)
524 return ret;
525 }
526
527 ret = i915_gem_object_get_pages(obj);
528 if (ret)
529 return ret;
530
531 i915_gem_object_pin_pages(obj);
532
533 return ret;
534 }
535
536 /* Per-page copy function for the shmem pread fastpath.
537 * Flushes invalid cachelines before reading the target if
538 * needs_clflush is set. */
539 static int
540 shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
541 char __user *user_data,
542 bool page_do_bit17_swizzling, bool needs_clflush)
543 {
544 char *vaddr;
545 int ret;
546
547 if (unlikely(page_do_bit17_swizzling))
548 return -EINVAL;
549
550 vaddr = kmap_atomic(page);
551 if (needs_clflush)
552 drm_clflush_virt_range(vaddr + shmem_page_offset,
553 page_length);
554 ret = __copy_to_user_inatomic(user_data,
555 vaddr + shmem_page_offset,
556 page_length);
557 kunmap_atomic(vaddr);
558
559 return ret ? -EFAULT : 0;
560 }
561
562 static void
563 shmem_clflush_swizzled_range(char *addr, unsigned long length,
564 bool swizzled)
565 {
566 if (unlikely(swizzled)) {
567 unsigned long start = (unsigned long) addr;
568 unsigned long end = (unsigned long) addr + length;
569
570 /* For swizzling simply ensure that we always flush both
571 * channels. Lame, but simple and it works. Swizzled
572 * pwrite/pread is far from a hotpath - current userspace
573 * doesn't use it at all. */
574 start = round_down(start, 128);
575 end = round_up(end, 128);
576
577 drm_clflush_virt_range((void *)start, end - start);
578 } else {
579 drm_clflush_virt_range(addr, length);
580 }
581
582 }
583
584 /* Only difference to the fast-path function is that this can handle bit17
585 * and uses non-atomic copy and kmap functions. */
586 static int
587 shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
588 char __user *user_data,
589 bool page_do_bit17_swizzling, bool needs_clflush)
590 {
591 char *vaddr;
592 int ret;
593
594 vaddr = kmap(page);
595 if (needs_clflush)
596 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
597 page_length,
598 page_do_bit17_swizzling);
599
600 if (page_do_bit17_swizzling)
601 ret = __copy_to_user_swizzled(user_data,
602 vaddr, shmem_page_offset,
603 page_length);
604 else
605 ret = __copy_to_user(user_data,
606 vaddr + shmem_page_offset,
607 page_length);
608 kunmap(page);
609
610 return ret ? - EFAULT : 0;
611 }
612
613 static int
614 i915_gem_shmem_pread(struct drm_device *dev,
615 struct drm_i915_gem_object *obj,
616 struct drm_i915_gem_pread *args,
617 struct drm_file *file)
618 {
619 char __user *user_data;
620 ssize_t remain;
621 loff_t offset;
622 int shmem_page_offset, page_length, ret = 0;
623 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
624 int prefaulted = 0;
625 int needs_clflush = 0;
626 struct sg_page_iter sg_iter;
627
628 user_data = to_user_ptr(args->data_ptr);
629 remain = args->size;
630
631 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
632
633 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
634 if (ret)
635 return ret;
636
637 offset = args->offset;
638
639 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
640 offset >> PAGE_SHIFT) {
641 struct page *page = sg_page_iter_page(&sg_iter);
642
643 if (remain <= 0)
644 break;
645
646 /* Operation in this page
647 *
648 * shmem_page_offset = offset within page in shmem file
649 * page_length = bytes to copy for this page
650 */
651 shmem_page_offset = offset_in_page(offset);
652 page_length = remain;
653 if ((shmem_page_offset + page_length) > PAGE_SIZE)
654 page_length = PAGE_SIZE - shmem_page_offset;
655
656 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
657 (page_to_phys(page) & (1 << 17)) != 0;
658
659 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
660 user_data, page_do_bit17_swizzling,
661 needs_clflush);
662 if (ret == 0)
663 goto next_page;
664
665 mutex_unlock(&dev->struct_mutex);
666
667 if (likely(!i915.prefault_disable) && !prefaulted) {
668 ret = fault_in_multipages_writeable(user_data, remain);
669 /* Userspace is tricking us, but we've already clobbered
670 * its pages with the prefault and promised to write the
671 * data up to the first fault. Hence ignore any errors
672 * and just continue. */
673 (void)ret;
674 prefaulted = 1;
675 }
676
677 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
678 user_data, page_do_bit17_swizzling,
679 needs_clflush);
680
681 mutex_lock(&dev->struct_mutex);
682
683 if (ret)
684 goto out;
685
686 next_page:
687 remain -= page_length;
688 user_data += page_length;
689 offset += page_length;
690 }
691
692 out:
693 i915_gem_object_unpin_pages(obj);
694
695 return ret;
696 }
697
698 /**
699 * Reads data from the object referenced by handle.
700 *
701 * On error, the contents of *data are undefined.
702 */
703 int
704 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
705 struct drm_file *file)
706 {
707 struct drm_i915_gem_pread *args = data;
708 struct drm_i915_gem_object *obj;
709 int ret = 0;
710
711 if (args->size == 0)
712 return 0;
713
714 if (!access_ok(VERIFY_WRITE,
715 to_user_ptr(args->data_ptr),
716 args->size))
717 return -EFAULT;
718
719 ret = i915_mutex_lock_interruptible(dev);
720 if (ret)
721 return ret;
722
723 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
724 if (&obj->base == NULL) {
725 ret = -ENOENT;
726 goto unlock;
727 }
728
729 /* Bounds check source. */
730 if (args->offset > obj->base.size ||
731 args->size > obj->base.size - args->offset) {
732 ret = -EINVAL;
733 goto out;
734 }
735
736 /* prime objects have no backing filp to GEM pread/pwrite
737 * pages from.
738 */
739 if (!obj->base.filp) {
740 ret = -EINVAL;
741 goto out;
742 }
743
744 trace_i915_gem_object_pread(obj, args->offset, args->size);
745
746 ret = i915_gem_shmem_pread(dev, obj, args, file);
747
748 out:
749 drm_gem_object_unreference(&obj->base);
750 unlock:
751 mutex_unlock(&dev->struct_mutex);
752 return ret;
753 }
754
755 /* This is the fast write path which cannot handle
756 * page faults in the source data
757 */
758
759 static inline int
760 fast_user_write(struct io_mapping *mapping,
761 loff_t page_base, int page_offset,
762 char __user *user_data,
763 int length)
764 {
765 void __iomem *vaddr_atomic;
766 void *vaddr;
767 unsigned long unwritten;
768
769 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
770 /* We can use the cpu mem copy function because this is X86. */
771 vaddr = (void __force*)vaddr_atomic + page_offset;
772 unwritten = __copy_from_user_inatomic_nocache(vaddr,
773 user_data, length);
774 io_mapping_unmap_atomic(vaddr_atomic);
775 return unwritten;
776 }
777
778 /**
779 * This is the fast pwrite path, where we copy the data directly from the
780 * user into the GTT, uncached.
781 */
782 static int
783 i915_gem_gtt_pwrite_fast(struct drm_device *dev,
784 struct drm_i915_gem_object *obj,
785 struct drm_i915_gem_pwrite *args,
786 struct drm_file *file)
787 {
788 struct drm_i915_private *dev_priv = dev->dev_private;
789 ssize_t remain;
790 loff_t offset, page_base;
791 char __user *user_data;
792 int page_offset, page_length, ret;
793
794 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
795 if (ret)
796 goto out;
797
798 ret = i915_gem_object_set_to_gtt_domain(obj, true);
799 if (ret)
800 goto out_unpin;
801
802 ret = i915_gem_object_put_fence(obj);
803 if (ret)
804 goto out_unpin;
805
806 user_data = to_user_ptr(args->data_ptr);
807 remain = args->size;
808
809 offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
810
811 intel_fb_obj_invalidate(obj, ORIGIN_GTT);
812
813 while (remain > 0) {
814 /* Operation in this page
815 *
816 * page_base = page offset within aperture
817 * page_offset = offset within page
818 * page_length = bytes to copy for this page
819 */
820 page_base = offset & PAGE_MASK;
821 page_offset = offset_in_page(offset);
822 page_length = remain;
823 if ((page_offset + remain) > PAGE_SIZE)
824 page_length = PAGE_SIZE - page_offset;
825
826 /* If we get a fault while copying data, then (presumably) our
827 * source page isn't available. Return the error and we'll
828 * retry in the slow path.
829 */
830 if (fast_user_write(dev_priv->gtt.mappable, page_base,
831 page_offset, user_data, page_length)) {
832 ret = -EFAULT;
833 goto out_flush;
834 }
835
836 remain -= page_length;
837 user_data += page_length;
838 offset += page_length;
839 }
840
841 out_flush:
842 intel_fb_obj_flush(obj, false);
843 out_unpin:
844 i915_gem_object_ggtt_unpin(obj);
845 out:
846 return ret;
847 }
848
849 /* Per-page copy function for the shmem pwrite fastpath.
850 * Flushes invalid cachelines before writing to the target if
851 * needs_clflush_before is set and flushes out any written cachelines after
852 * writing if needs_clflush is set. */
853 static int
854 shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
855 char __user *user_data,
856 bool page_do_bit17_swizzling,
857 bool needs_clflush_before,
858 bool needs_clflush_after)
859 {
860 char *vaddr;
861 int ret;
862
863 if (unlikely(page_do_bit17_swizzling))
864 return -EINVAL;
865
866 vaddr = kmap_atomic(page);
867 if (needs_clflush_before)
868 drm_clflush_virt_range(vaddr + shmem_page_offset,
869 page_length);
870 ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
871 user_data, page_length);
872 if (needs_clflush_after)
873 drm_clflush_virt_range(vaddr + shmem_page_offset,
874 page_length);
875 kunmap_atomic(vaddr);
876
877 return ret ? -EFAULT : 0;
878 }
879
880 /* Only difference to the fast-path function is that this can handle bit17
881 * and uses non-atomic copy and kmap functions. */
882 static int
883 shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
884 char __user *user_data,
885 bool page_do_bit17_swizzling,
886 bool needs_clflush_before,
887 bool needs_clflush_after)
888 {
889 char *vaddr;
890 int ret;
891
892 vaddr = kmap(page);
893 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
894 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
895 page_length,
896 page_do_bit17_swizzling);
897 if (page_do_bit17_swizzling)
898 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
899 user_data,
900 page_length);
901 else
902 ret = __copy_from_user(vaddr + shmem_page_offset,
903 user_data,
904 page_length);
905 if (needs_clflush_after)
906 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
907 page_length,
908 page_do_bit17_swizzling);
909 kunmap(page);
910
911 return ret ? -EFAULT : 0;
912 }
913
914 static int
915 i915_gem_shmem_pwrite(struct drm_device *dev,
916 struct drm_i915_gem_object *obj,
917 struct drm_i915_gem_pwrite *args,
918 struct drm_file *file)
919 {
920 ssize_t remain;
921 loff_t offset;
922 char __user *user_data;
923 int shmem_page_offset, page_length, ret = 0;
924 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
925 int hit_slowpath = 0;
926 int needs_clflush_after = 0;
927 int needs_clflush_before = 0;
928 struct sg_page_iter sg_iter;
929
930 user_data = to_user_ptr(args->data_ptr);
931 remain = args->size;
932
933 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
934
935 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
936 /* If we're not in the cpu write domain, set ourself into the gtt
937 * write domain and manually flush cachelines (if required). This
938 * optimizes for the case when the gpu will use the data
939 * right away and we therefore have to clflush anyway. */
940 needs_clflush_after = cpu_write_needs_clflush(obj);
941 ret = i915_gem_object_wait_rendering(obj, false);
942 if (ret)
943 return ret;
944 }
945 /* Same trick applies to invalidate partially written cachelines read
946 * before writing. */
947 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
948 needs_clflush_before =
949 !cpu_cache_is_coherent(dev, obj->cache_level);
950
951 ret = i915_gem_object_get_pages(obj);
952 if (ret)
953 return ret;
954
955 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
956
957 i915_gem_object_pin_pages(obj);
958
959 offset = args->offset;
960 obj->dirty = 1;
961
962 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
963 offset >> PAGE_SHIFT) {
964 struct page *page = sg_page_iter_page(&sg_iter);
965 int partial_cacheline_write;
966
967 if (remain <= 0)
968 break;
969
970 /* Operation in this page
971 *
972 * shmem_page_offset = offset within page in shmem file
973 * page_length = bytes to copy for this page
974 */
975 shmem_page_offset = offset_in_page(offset);
976
977 page_length = remain;
978 if ((shmem_page_offset + page_length) > PAGE_SIZE)
979 page_length = PAGE_SIZE - shmem_page_offset;
980
981 /* If we don't overwrite a cacheline completely we need to be
982 * careful to have up-to-date data by first clflushing. Don't
983 * overcomplicate things and flush the entire patch. */
984 partial_cacheline_write = needs_clflush_before &&
985 ((shmem_page_offset | page_length)
986 & (boot_cpu_data.x86_clflush_size - 1));
987
988 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
989 (page_to_phys(page) & (1 << 17)) != 0;
990
991 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
992 user_data, page_do_bit17_swizzling,
993 partial_cacheline_write,
994 needs_clflush_after);
995 if (ret == 0)
996 goto next_page;
997
998 hit_slowpath = 1;
999 mutex_unlock(&dev->struct_mutex);
1000 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
1001 user_data, page_do_bit17_swizzling,
1002 partial_cacheline_write,
1003 needs_clflush_after);
1004
1005 mutex_lock(&dev->struct_mutex);
1006
1007 if (ret)
1008 goto out;
1009
1010 next_page:
1011 remain -= page_length;
1012 user_data += page_length;
1013 offset += page_length;
1014 }
1015
1016 out:
1017 i915_gem_object_unpin_pages(obj);
1018
1019 if (hit_slowpath) {
1020 /*
1021 * Fixup: Flush cpu caches in case we didn't flush the dirty
1022 * cachelines in-line while writing and the object moved
1023 * out of the cpu write domain while we've dropped the lock.
1024 */
1025 if (!needs_clflush_after &&
1026 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
1027 if (i915_gem_clflush_object(obj, obj->pin_display))
1028 i915_gem_chipset_flush(dev);
1029 }
1030 }
1031
1032 if (needs_clflush_after)
1033 i915_gem_chipset_flush(dev);
1034
1035 intel_fb_obj_flush(obj, false);
1036 return ret;
1037 }
1038
1039 /**
1040 * Writes data to the object referenced by handle.
1041 *
1042 * On error, the contents of the buffer that were to be modified are undefined.
1043 */
1044 int
1045 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
1046 struct drm_file *file)
1047 {
1048 struct drm_i915_private *dev_priv = dev->dev_private;
1049 struct drm_i915_gem_pwrite *args = data;
1050 struct drm_i915_gem_object *obj;
1051 int ret;
1052
1053 if (args->size == 0)
1054 return 0;
1055
1056 if (!access_ok(VERIFY_READ,
1057 to_user_ptr(args->data_ptr),
1058 args->size))
1059 return -EFAULT;
1060
1061 if (likely(!i915.prefault_disable)) {
1062 ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
1063 args->size);
1064 if (ret)
1065 return -EFAULT;
1066 }
1067
1068 intel_runtime_pm_get(dev_priv);
1069
1070 ret = i915_mutex_lock_interruptible(dev);
1071 if (ret)
1072 goto put_rpm;
1073
1074 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1075 if (&obj->base == NULL) {
1076 ret = -ENOENT;
1077 goto unlock;
1078 }
1079
1080 /* Bounds check destination. */
1081 if (args->offset > obj->base.size ||
1082 args->size > obj->base.size - args->offset) {
1083 ret = -EINVAL;
1084 goto out;
1085 }
1086
1087 /* prime objects have no backing filp to GEM pread/pwrite
1088 * pages from.
1089 */
1090 if (!obj->base.filp) {
1091 ret = -EINVAL;
1092 goto out;
1093 }
1094
1095 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1096
1097 ret = -EFAULT;
1098 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1099 * it would end up going through the fenced access, and we'll get
1100 * different detiling behavior between reading and writing.
1101 * pread/pwrite currently are reading and writing from the CPU
1102 * perspective, requiring manual detiling by the client.
1103 */
1104 if (obj->tiling_mode == I915_TILING_NONE &&
1105 obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
1106 cpu_write_needs_clflush(obj)) {
1107 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1108 /* Note that the gtt paths might fail with non-page-backed user
1109 * pointers (e.g. gtt mappings when moving data between
1110 * textures). Fallback to the shmem path in that case. */
1111 }
1112
1113 if (ret == -EFAULT || ret == -ENOSPC) {
1114 if (obj->phys_handle)
1115 ret = i915_gem_phys_pwrite(obj, args, file);
1116 else
1117 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
1118 }
1119
1120 out:
1121 drm_gem_object_unreference(&obj->base);
1122 unlock:
1123 mutex_unlock(&dev->struct_mutex);
1124 put_rpm:
1125 intel_runtime_pm_put(dev_priv);
1126
1127 return ret;
1128 }
1129
1130 int
1131 i915_gem_check_wedge(struct i915_gpu_error *error,
1132 bool interruptible)
1133 {
1134 if (i915_reset_in_progress(error)) {
1135 /* Non-interruptible callers can't handle -EAGAIN, hence return
1136 * -EIO unconditionally for these. */
1137 if (!interruptible)
1138 return -EIO;
1139
1140 /* Recovery complete, but the reset failed ... */
1141 if (i915_terminally_wedged(error))
1142 return -EIO;
1143
1144 /*
1145 * Check if GPU Reset is in progress - we need intel_ring_begin
1146 * to work properly to reinit the hw state while the gpu is
1147 * still marked as reset-in-progress. Handle this with a flag.
1148 */
1149 if (!error->reload_in_reset)
1150 return -EAGAIN;
1151 }
1152
1153 return 0;
1154 }
1155
1156 static void fake_irq(unsigned long data)
1157 {
1158 wake_up_process((struct task_struct *)data);
1159 }
1160
1161 static bool missed_irq(struct drm_i915_private *dev_priv,
1162 struct intel_engine_cs *ring)
1163 {
1164 return test_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings);
1165 }
1166
1167 static int __i915_spin_request(struct drm_i915_gem_request *req)
1168 {
1169 unsigned long timeout;
1170
1171 if (i915_gem_request_get_ring(req)->irq_refcount)
1172 return -EBUSY;
1173
1174 timeout = jiffies + 1;
1175 while (!need_resched()) {
1176 if (i915_gem_request_completed(req, true))
1177 return 0;
1178
1179 if (time_after_eq(jiffies, timeout))
1180 break;
1181
1182 cpu_relax_lowlatency();
1183 }
1184 if (i915_gem_request_completed(req, false))
1185 return 0;
1186
1187 return -EAGAIN;
1188 }
1189
1190 /**
1191 * __i915_wait_request - wait until execution of request has finished
1192 * @req: duh!
1193 * @reset_counter: reset sequence associated with the given request
1194 * @interruptible: do an interruptible wait (normally yes)
1195 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
1196 *
1197 * Note: It is of utmost importance that the passed in seqno and reset_counter
1198 * values have been read by the caller in an smp safe manner. Where read-side
1199 * locks are involved, it is sufficient to read the reset_counter before
1200 * unlocking the lock that protects the seqno. For lockless tricks, the
1201 * reset_counter _must_ be read before, and an appropriate smp_rmb must be
1202 * inserted.
1203 *
1204 * Returns 0 if the request was found within the alloted time. Else returns the
1205 * errno with remaining time filled in timeout argument.
1206 */
1207 int __i915_wait_request(struct drm_i915_gem_request *req,
1208 unsigned reset_counter,
1209 bool interruptible,
1210 s64 *timeout,
1211 struct intel_rps_client *rps)
1212 {
1213 struct intel_engine_cs *ring = i915_gem_request_get_ring(req);
1214 struct drm_device *dev = ring->dev;
1215 struct drm_i915_private *dev_priv = dev->dev_private;
1216 const bool irq_test_in_progress =
1217 ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_ring_flag(ring);
1218 DEFINE_WAIT(wait);
1219 unsigned long timeout_expire;
1220 s64 before, now;
1221 int ret;
1222
1223 WARN(!intel_irqs_enabled(dev_priv), "IRQs disabled");
1224
1225 if (list_empty(&req->list))
1226 return 0;
1227
1228 if (i915_gem_request_completed(req, true))
1229 return 0;
1230
1231 timeout_expire = timeout ?
1232 jiffies + nsecs_to_jiffies_timeout((u64)*timeout) : 0;
1233
1234 if (INTEL_INFO(dev_priv)->gen >= 6)
1235 gen6_rps_boost(dev_priv, rps, req->emitted_jiffies);
1236
1237 /* Record current time in case interrupted by signal, or wedged */
1238 trace_i915_gem_request_wait_begin(req);
1239 before = ktime_get_raw_ns();
1240
1241 /* Optimistic spin for the next jiffie before touching IRQs */
1242 ret = __i915_spin_request(req);
1243 if (ret == 0)
1244 goto out;
1245
1246 if (!irq_test_in_progress && WARN_ON(!ring->irq_get(ring))) {
1247 ret = -ENODEV;
1248 goto out;
1249 }
1250
1251 for (;;) {
1252 struct timer_list timer;
1253
1254 prepare_to_wait(&ring->irq_queue, &wait,
1255 interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
1256
1257 /* We need to check whether any gpu reset happened in between
1258 * the caller grabbing the seqno and now ... */
1259 if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) {
1260 /* ... but upgrade the -EAGAIN to an -EIO if the gpu
1261 * is truely gone. */
1262 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
1263 if (ret == 0)
1264 ret = -EAGAIN;
1265 break;
1266 }
1267
1268 if (i915_gem_request_completed(req, false)) {
1269 ret = 0;
1270 break;
1271 }
1272
1273 if (interruptible && signal_pending(current)) {
1274 ret = -ERESTARTSYS;
1275 break;
1276 }
1277
1278 if (timeout && time_after_eq(jiffies, timeout_expire)) {
1279 ret = -ETIME;
1280 break;
1281 }
1282
1283 timer.function = NULL;
1284 if (timeout || missed_irq(dev_priv, ring)) {
1285 unsigned long expire;
1286
1287 setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
1288 expire = missed_irq(dev_priv, ring) ? jiffies + 1 : timeout_expire;
1289 mod_timer(&timer, expire);
1290 }
1291
1292 io_schedule();
1293
1294 if (timer.function) {
1295 del_singleshot_timer_sync(&timer);
1296 destroy_timer_on_stack(&timer);
1297 }
1298 }
1299 if (!irq_test_in_progress)
1300 ring->irq_put(ring);
1301
1302 finish_wait(&ring->irq_queue, &wait);
1303
1304 out:
1305 now = ktime_get_raw_ns();
1306 trace_i915_gem_request_wait_end(req);
1307
1308 if (timeout) {
1309 s64 tres = *timeout - (now - before);
1310
1311 *timeout = tres < 0 ? 0 : tres;
1312
1313 /*
1314 * Apparently ktime isn't accurate enough and occasionally has a
1315 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
1316 * things up to make the test happy. We allow up to 1 jiffy.
1317 *
1318 * This is a regrssion from the timespec->ktime conversion.
1319 */
1320 if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
1321 *timeout = 0;
1322 }
1323
1324 return ret;
1325 }
1326
1327 int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
1328 struct drm_file *file)
1329 {
1330 struct drm_i915_private *dev_private;
1331 struct drm_i915_file_private *file_priv;
1332
1333 WARN_ON(!req || !file || req->file_priv);
1334
1335 if (!req || !file)
1336 return -EINVAL;
1337
1338 if (req->file_priv)
1339 return -EINVAL;
1340
1341 dev_private = req->ring->dev->dev_private;
1342 file_priv = file->driver_priv;
1343
1344 spin_lock(&file_priv->mm.lock);
1345 req->file_priv = file_priv;
1346 list_add_tail(&req->client_list, &file_priv->mm.request_list);
1347 spin_unlock(&file_priv->mm.lock);
1348
1349 req->pid = get_pid(task_pid(current));
1350
1351 return 0;
1352 }
1353
1354 static inline void
1355 i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1356 {
1357 struct drm_i915_file_private *file_priv = request->file_priv;
1358
1359 if (!file_priv)
1360 return;
1361
1362 spin_lock(&file_priv->mm.lock);
1363 list_del(&request->client_list);
1364 request->file_priv = NULL;
1365 spin_unlock(&file_priv->mm.lock);
1366
1367 put_pid(request->pid);
1368 request->pid = NULL;
1369 }
1370
1371 static void i915_gem_request_retire(struct drm_i915_gem_request *request)
1372 {
1373 trace_i915_gem_request_retire(request);
1374
1375 /* We know the GPU must have read the request to have
1376 * sent us the seqno + interrupt, so use the position
1377 * of tail of the request to update the last known position
1378 * of the GPU head.
1379 *
1380 * Note this requires that we are always called in request
1381 * completion order.
1382 */
1383 request->ringbuf->last_retired_head = request->postfix;
1384
1385 list_del_init(&request->list);
1386 i915_gem_request_remove_from_client(request);
1387
1388 i915_gem_request_unreference(request);
1389 }
1390
1391 static void
1392 __i915_gem_request_retire__upto(struct drm_i915_gem_request *req)
1393 {
1394 struct intel_engine_cs *engine = req->ring;
1395 struct drm_i915_gem_request *tmp;
1396
1397 lockdep_assert_held(&engine->dev->struct_mutex);
1398
1399 if (list_empty(&req->list))
1400 return;
1401
1402 do {
1403 tmp = list_first_entry(&engine->request_list,
1404 typeof(*tmp), list);
1405
1406 i915_gem_request_retire(tmp);
1407 } while (tmp != req);
1408
1409 WARN_ON(i915_verify_lists(engine->dev));
1410 }
1411
1412 /**
1413 * Waits for a request to be signaled, and cleans up the
1414 * request and object lists appropriately for that event.
1415 */
1416 int
1417 i915_wait_request(struct drm_i915_gem_request *req)
1418 {
1419 struct drm_device *dev;
1420 struct drm_i915_private *dev_priv;
1421 bool interruptible;
1422 int ret;
1423
1424 BUG_ON(req == NULL);
1425
1426 dev = req->ring->dev;
1427 dev_priv = dev->dev_private;
1428 interruptible = dev_priv->mm.interruptible;
1429
1430 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1431
1432 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
1433 if (ret)
1434 return ret;
1435
1436 ret = __i915_wait_request(req,
1437 atomic_read(&dev_priv->gpu_error.reset_counter),
1438 interruptible, NULL, NULL);
1439 if (ret)
1440 return ret;
1441
1442 __i915_gem_request_retire__upto(req);
1443 return 0;
1444 }
1445
1446 /**
1447 * Ensures that all rendering to the object has completed and the object is
1448 * safe to unbind from the GTT or access from the CPU.
1449 */
1450 int
1451 i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1452 bool readonly)
1453 {
1454 int ret, i;
1455
1456 if (!obj->active)
1457 return 0;
1458
1459 if (readonly) {
1460 if (obj->last_write_req != NULL) {
1461 ret = i915_wait_request(obj->last_write_req);
1462 if (ret)
1463 return ret;
1464
1465 i = obj->last_write_req->ring->id;
1466 if (obj->last_read_req[i] == obj->last_write_req)
1467 i915_gem_object_retire__read(obj, i);
1468 else
1469 i915_gem_object_retire__write(obj);
1470 }
1471 } else {
1472 for (i = 0; i < I915_NUM_RINGS; i++) {
1473 if (obj->last_read_req[i] == NULL)
1474 continue;
1475
1476 ret = i915_wait_request(obj->last_read_req[i]);
1477 if (ret)
1478 return ret;
1479
1480 i915_gem_object_retire__read(obj, i);
1481 }
1482 RQ_BUG_ON(obj->active);
1483 }
1484
1485 return 0;
1486 }
1487
1488 static void
1489 i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
1490 struct drm_i915_gem_request *req)
1491 {
1492 int ring = req->ring->id;
1493
1494 if (obj->last_read_req[ring] == req)
1495 i915_gem_object_retire__read(obj, ring);
1496 else if (obj->last_write_req == req)
1497 i915_gem_object_retire__write(obj);
1498
1499 __i915_gem_request_retire__upto(req);
1500 }
1501
1502 /* A nonblocking variant of the above wait. This is a highly dangerous routine
1503 * as the object state may change during this call.
1504 */
1505 static __must_check int
1506 i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
1507 struct intel_rps_client *rps,
1508 bool readonly)
1509 {
1510 struct drm_device *dev = obj->base.dev;
1511 struct drm_i915_private *dev_priv = dev->dev_private;
1512 struct drm_i915_gem_request *requests[I915_NUM_RINGS];
1513 unsigned reset_counter;
1514 int ret, i, n = 0;
1515
1516 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1517 BUG_ON(!dev_priv->mm.interruptible);
1518
1519 if (!obj->active)
1520 return 0;
1521
1522 ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
1523 if (ret)
1524 return ret;
1525
1526 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
1527
1528 if (readonly) {
1529 struct drm_i915_gem_request *req;
1530
1531 req = obj->last_write_req;
1532 if (req == NULL)
1533 return 0;
1534
1535 requests[n++] = i915_gem_request_reference(req);
1536 } else {
1537 for (i = 0; i < I915_NUM_RINGS; i++) {
1538 struct drm_i915_gem_request *req;
1539
1540 req = obj->last_read_req[i];
1541 if (req == NULL)
1542 continue;
1543
1544 requests[n++] = i915_gem_request_reference(req);
1545 }
1546 }
1547
1548 mutex_unlock(&dev->struct_mutex);
1549 for (i = 0; ret == 0 && i < n; i++)
1550 ret = __i915_wait_request(requests[i], reset_counter, true,
1551 NULL, rps);
1552 mutex_lock(&dev->struct_mutex);
1553
1554 for (i = 0; i < n; i++) {
1555 if (ret == 0)
1556 i915_gem_object_retire_request(obj, requests[i]);
1557 i915_gem_request_unreference(requests[i]);
1558 }
1559
1560 return ret;
1561 }
1562
1563 static struct intel_rps_client *to_rps_client(struct drm_file *file)
1564 {
1565 struct drm_i915_file_private *fpriv = file->driver_priv;
1566 return &fpriv->rps;
1567 }
1568
1569 /**
1570 * Called when user space prepares to use an object with the CPU, either
1571 * through the mmap ioctl's mapping or a GTT mapping.
1572 */
1573 int
1574 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1575 struct drm_file *file)
1576 {
1577 struct drm_i915_gem_set_domain *args = data;
1578 struct drm_i915_gem_object *obj;
1579 uint32_t read_domains = args->read_domains;
1580 uint32_t write_domain = args->write_domain;
1581 int ret;
1582
1583 /* Only handle setting domains to types used by the CPU. */
1584 if (write_domain & I915_GEM_GPU_DOMAINS)
1585 return -EINVAL;
1586
1587 if (read_domains & I915_GEM_GPU_DOMAINS)
1588 return -EINVAL;
1589
1590 /* Having something in the write domain implies it's in the read
1591 * domain, and only that read domain. Enforce that in the request.
1592 */
1593 if (write_domain != 0 && read_domains != write_domain)
1594 return -EINVAL;
1595
1596 ret = i915_mutex_lock_interruptible(dev);
1597 if (ret)
1598 return ret;
1599
1600 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1601 if (&obj->base == NULL) {
1602 ret = -ENOENT;
1603 goto unlock;
1604 }
1605
1606 /* Try to flush the object off the GPU without holding the lock.
1607 * We will repeat the flush holding the lock in the normal manner
1608 * to catch cases where we are gazumped.
1609 */
1610 ret = i915_gem_object_wait_rendering__nonblocking(obj,
1611 to_rps_client(file),
1612 !write_domain);
1613 if (ret)
1614 goto unref;
1615
1616 if (read_domains & I915_GEM_DOMAIN_GTT)
1617 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1618 else
1619 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1620
1621 if (write_domain != 0)
1622 intel_fb_obj_invalidate(obj,
1623 write_domain == I915_GEM_DOMAIN_GTT ?
1624 ORIGIN_GTT : ORIGIN_CPU);
1625
1626 unref:
1627 drm_gem_object_unreference(&obj->base);
1628 unlock:
1629 mutex_unlock(&dev->struct_mutex);
1630 return ret;
1631 }
1632
1633 /**
1634 * Called when user space has done writes to this buffer
1635 */
1636 int
1637 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1638 struct drm_file *file)
1639 {
1640 struct drm_i915_gem_sw_finish *args = data;
1641 struct drm_i915_gem_object *obj;
1642 int ret = 0;
1643
1644 ret = i915_mutex_lock_interruptible(dev);
1645 if (ret)
1646 return ret;
1647
1648 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1649 if (&obj->base == NULL) {
1650 ret = -ENOENT;
1651 goto unlock;
1652 }
1653
1654 /* Pinned buffers may be scanout, so flush the cache */
1655 if (obj->pin_display)
1656 i915_gem_object_flush_cpu_write_domain(obj);
1657
1658 drm_gem_object_unreference(&obj->base);
1659 unlock:
1660 mutex_unlock(&dev->struct_mutex);
1661 return ret;
1662 }
1663
1664 /**
1665 * Maps the contents of an object, returning the address it is mapped
1666 * into.
1667 *
1668 * While the mapping holds a reference on the contents of the object, it doesn't
1669 * imply a ref on the object itself.
1670 *
1671 * IMPORTANT:
1672 *
1673 * DRM driver writers who look a this function as an example for how to do GEM
1674 * mmap support, please don't implement mmap support like here. The modern way
1675 * to implement DRM mmap support is with an mmap offset ioctl (like
1676 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1677 * That way debug tooling like valgrind will understand what's going on, hiding
1678 * the mmap call in a driver private ioctl will break that. The i915 driver only
1679 * does cpu mmaps this way because we didn't know better.
1680 */
1681 int
1682 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1683 struct drm_file *file)
1684 {
1685 struct drm_i915_gem_mmap *args = data;
1686 struct drm_gem_object *obj;
1687 unsigned long addr;
1688
1689 if (args->flags & ~(I915_MMAP_WC))
1690 return -EINVAL;
1691
1692 if (args->flags & I915_MMAP_WC && !cpu_has_pat)
1693 return -ENODEV;
1694
1695 obj = drm_gem_object_lookup(dev, file, args->handle);
1696 if (obj == NULL)
1697 return -ENOENT;
1698
1699 /* prime objects have no backing filp to GEM mmap
1700 * pages from.
1701 */
1702 if (!obj->filp) {
1703 drm_gem_object_unreference_unlocked(obj);
1704 return -EINVAL;
1705 }
1706
1707 addr = vm_mmap(obj->filp, 0, args->size,
1708 PROT_READ | PROT_WRITE, MAP_SHARED,
1709 args->offset);
1710 if (args->flags & I915_MMAP_WC) {
1711 struct mm_struct *mm = current->mm;
1712 struct vm_area_struct *vma;
1713
1714 down_write(&mm->mmap_sem);
1715 vma = find_vma(mm, addr);
1716 if (vma)
1717 vma->vm_page_prot =
1718 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1719 else
1720 addr = -ENOMEM;
1721 up_write(&mm->mmap_sem);
1722 }
1723 drm_gem_object_unreference_unlocked(obj);
1724 if (IS_ERR((void *)addr))
1725 return addr;
1726
1727 args->addr_ptr = (uint64_t) addr;
1728
1729 return 0;
1730 }
1731
1732 /**
1733 * i915_gem_fault - fault a page into the GTT
1734 * vma: VMA in question
1735 * vmf: fault info
1736 *
1737 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1738 * from userspace. The fault handler takes care of binding the object to
1739 * the GTT (if needed), allocating and programming a fence register (again,
1740 * only if needed based on whether the old reg is still valid or the object
1741 * is tiled) and inserting a new PTE into the faulting process.
1742 *
1743 * Note that the faulting process may involve evicting existing objects
1744 * from the GTT and/or fence registers to make room. So performance may
1745 * suffer if the GTT working set is large or there are few fence registers
1746 * left.
1747 */
1748 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1749 {
1750 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1751 struct drm_device *dev = obj->base.dev;
1752 struct drm_i915_private *dev_priv = dev->dev_private;
1753 struct i915_ggtt_view view = i915_ggtt_view_normal;
1754 pgoff_t page_offset;
1755 unsigned long pfn;
1756 int ret = 0;
1757 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1758
1759 intel_runtime_pm_get(dev_priv);
1760
1761 /* We don't use vmf->pgoff since that has the fake offset */
1762 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1763 PAGE_SHIFT;
1764
1765 ret = i915_mutex_lock_interruptible(dev);
1766 if (ret)
1767 goto out;
1768
1769 trace_i915_gem_object_fault(obj, page_offset, true, write);
1770
1771 /* Try to flush the object off the GPU first without holding the lock.
1772 * Upon reacquiring the lock, we will perform our sanity checks and then
1773 * repeat the flush holding the lock in the normal manner to catch cases
1774 * where we are gazumped.
1775 */
1776 ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
1777 if (ret)
1778 goto unlock;
1779
1780 /* Access to snoopable pages through the GTT is incoherent. */
1781 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
1782 ret = -EFAULT;
1783 goto unlock;
1784 }
1785
1786 /* Use a partial view if the object is bigger than the aperture. */
1787 if (obj->base.size >= dev_priv->gtt.mappable_end &&
1788 obj->tiling_mode == I915_TILING_NONE) {
1789 static const unsigned int chunk_size = 256; // 1 MiB
1790
1791 memset(&view, 0, sizeof(view));
1792 view.type = I915_GGTT_VIEW_PARTIAL;
1793 view.params.partial.offset = rounddown(page_offset, chunk_size);
1794 view.params.partial.size =
1795 min_t(unsigned int,
1796 chunk_size,
1797 (vma->vm_end - vma->vm_start)/PAGE_SIZE -
1798 view.params.partial.offset);
1799 }
1800
1801 /* Now pin it into the GTT if needed */
1802 ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
1803 if (ret)
1804 goto unlock;
1805
1806 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1807 if (ret)
1808 goto unpin;
1809
1810 ret = i915_gem_object_get_fence(obj);
1811 if (ret)
1812 goto unpin;
1813
1814 /* Finally, remap it using the new GTT offset */
1815 pfn = dev_priv->gtt.mappable_base +
1816 i915_gem_obj_ggtt_offset_view(obj, &view);
1817 pfn >>= PAGE_SHIFT;
1818
1819 if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
1820 /* Overriding existing pages in partial view does not cause
1821 * us any trouble as TLBs are still valid because the fault
1822 * is due to userspace losing part of the mapping or never
1823 * having accessed it before (at this partials' range).
1824 */
1825 unsigned long base = vma->vm_start +
1826 (view.params.partial.offset << PAGE_SHIFT);
1827 unsigned int i;
1828
1829 for (i = 0; i < view.params.partial.size; i++) {
1830 ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
1831 if (ret)
1832 break;
1833 }
1834
1835 obj->fault_mappable = true;
1836 } else {
1837 if (!obj->fault_mappable) {
1838 unsigned long size = min_t(unsigned long,
1839 vma->vm_end - vma->vm_start,
1840 obj->base.size);
1841 int i;
1842
1843 for (i = 0; i < size >> PAGE_SHIFT; i++) {
1844 ret = vm_insert_pfn(vma,
1845 (unsigned long)vma->vm_start + i * PAGE_SIZE,
1846 pfn + i);
1847 if (ret)
1848 break;
1849 }
1850
1851 obj->fault_mappable = true;
1852 } else
1853 ret = vm_insert_pfn(vma,
1854 (unsigned long)vmf->virtual_address,
1855 pfn + page_offset);
1856 }
1857 unpin:
1858 i915_gem_object_ggtt_unpin_view(obj, &view);
1859 unlock:
1860 mutex_unlock(&dev->struct_mutex);
1861 out:
1862 switch (ret) {
1863 case -EIO:
1864 /*
1865 * We eat errors when the gpu is terminally wedged to avoid
1866 * userspace unduly crashing (gl has no provisions for mmaps to
1867 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1868 * and so needs to be reported.
1869 */
1870 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
1871 ret = VM_FAULT_SIGBUS;
1872 break;
1873 }
1874 case -EAGAIN:
1875 /*
1876 * EAGAIN means the gpu is hung and we'll wait for the error
1877 * handler to reset everything when re-faulting in
1878 * i915_mutex_lock_interruptible.
1879 */
1880 case 0:
1881 case -ERESTARTSYS:
1882 case -EINTR:
1883 case -EBUSY:
1884 /*
1885 * EBUSY is ok: this just means that another thread
1886 * already did the job.
1887 */
1888 ret = VM_FAULT_NOPAGE;
1889 break;
1890 case -ENOMEM:
1891 ret = VM_FAULT_OOM;
1892 break;
1893 case -ENOSPC:
1894 case -EFAULT:
1895 ret = VM_FAULT_SIGBUS;
1896 break;
1897 default:
1898 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
1899 ret = VM_FAULT_SIGBUS;
1900 break;
1901 }
1902
1903 intel_runtime_pm_put(dev_priv);
1904 return ret;
1905 }
1906
1907 /**
1908 * i915_gem_release_mmap - remove physical page mappings
1909 * @obj: obj in question
1910 *
1911 * Preserve the reservation of the mmapping with the DRM core code, but
1912 * relinquish ownership of the pages back to the system.
1913 *
1914 * It is vital that we remove the page mapping if we have mapped a tiled
1915 * object through the GTT and then lose the fence register due to
1916 * resource pressure. Similarly if the object has been moved out of the
1917 * aperture, than pages mapped into userspace must be revoked. Removing the
1918 * mapping will then trigger a page fault on the next user access, allowing
1919 * fixup by i915_gem_fault().
1920 */
1921 void
1922 i915_gem_release_mmap(struct drm_i915_gem_object *obj)
1923 {
1924 if (!obj->fault_mappable)
1925 return;
1926
1927 drm_vma_node_unmap(&obj->base.vma_node,
1928 obj->base.dev->anon_inode->i_mapping);
1929 obj->fault_mappable = false;
1930 }
1931
1932 void
1933 i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
1934 {
1935 struct drm_i915_gem_object *obj;
1936
1937 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
1938 i915_gem_release_mmap(obj);
1939 }
1940
1941 uint32_t
1942 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
1943 {
1944 uint32_t gtt_size;
1945
1946 if (INTEL_INFO(dev)->gen >= 4 ||
1947 tiling_mode == I915_TILING_NONE)
1948 return size;
1949
1950 /* Previous chips need a power-of-two fence region when tiling */
1951 if (INTEL_INFO(dev)->gen == 3)
1952 gtt_size = 1024*1024;
1953 else
1954 gtt_size = 512*1024;
1955
1956 while (gtt_size < size)
1957 gtt_size <<= 1;
1958
1959 return gtt_size;
1960 }
1961
1962 /**
1963 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1964 * @obj: object to check
1965 *
1966 * Return the required GTT alignment for an object, taking into account
1967 * potential fence register mapping.
1968 */
1969 uint32_t
1970 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
1971 int tiling_mode, bool fenced)
1972 {
1973 /*
1974 * Minimum alignment is 4k (GTT page size), but might be greater
1975 * if a fence register is needed for the object.
1976 */
1977 if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
1978 tiling_mode == I915_TILING_NONE)
1979 return 4096;
1980
1981 /*
1982 * Previous chips need to be aligned to the size of the smallest
1983 * fence register that can contain the object.
1984 */
1985 return i915_gem_get_gtt_size(dev, size, tiling_mode);
1986 }
1987
1988 static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
1989 {
1990 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1991 int ret;
1992
1993 if (drm_vma_node_has_offset(&obj->base.vma_node))
1994 return 0;
1995
1996 dev_priv->mm.shrinker_no_lock_stealing = true;
1997
1998 ret = drm_gem_create_mmap_offset(&obj->base);
1999 if (ret != -ENOSPC)
2000 goto out;
2001
2002 /* Badly fragmented mmap space? The only way we can recover
2003 * space is by destroying unwanted objects. We can't randomly release
2004 * mmap_offsets as userspace expects them to be persistent for the
2005 * lifetime of the objects. The closest we can is to release the
2006 * offsets on purgeable objects by truncating it and marking it purged,
2007 * which prevents userspace from ever using that object again.
2008 */
2009 i915_gem_shrink(dev_priv,
2010 obj->base.size >> PAGE_SHIFT,
2011 I915_SHRINK_BOUND |
2012 I915_SHRINK_UNBOUND |
2013 I915_SHRINK_PURGEABLE);
2014 ret = drm_gem_create_mmap_offset(&obj->base);
2015 if (ret != -ENOSPC)
2016 goto out;
2017
2018 i915_gem_shrink_all(dev_priv);
2019 ret = drm_gem_create_mmap_offset(&obj->base);
2020 out:
2021 dev_priv->mm.shrinker_no_lock_stealing = false;
2022
2023 return ret;
2024 }
2025
2026 static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2027 {
2028 drm_gem_free_mmap_offset(&obj->base);
2029 }
2030
2031 int
2032 i915_gem_mmap_gtt(struct drm_file *file,
2033 struct drm_device *dev,
2034 uint32_t handle,
2035 uint64_t *offset)
2036 {
2037 struct drm_i915_gem_object *obj;
2038 int ret;
2039
2040 ret = i915_mutex_lock_interruptible(dev);
2041 if (ret)
2042 return ret;
2043
2044 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
2045 if (&obj->base == NULL) {
2046 ret = -ENOENT;
2047 goto unlock;
2048 }
2049
2050 if (obj->madv != I915_MADV_WILLNEED) {
2051 DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
2052 ret = -EFAULT;
2053 goto out;
2054 }
2055
2056 ret = i915_gem_object_create_mmap_offset(obj);
2057 if (ret)
2058 goto out;
2059
2060 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
2061
2062 out:
2063 drm_gem_object_unreference(&obj->base);
2064 unlock:
2065 mutex_unlock(&dev->struct_mutex);
2066 return ret;
2067 }
2068
2069 /**
2070 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2071 * @dev: DRM device
2072 * @data: GTT mapping ioctl data
2073 * @file: GEM object info
2074 *
2075 * Simply returns the fake offset to userspace so it can mmap it.
2076 * The mmap call will end up in drm_gem_mmap(), which will set things
2077 * up so we can get faults in the handler above.
2078 *
2079 * The fault handler will take care of binding the object into the GTT
2080 * (since it may have been evicted to make room for something), allocating
2081 * a fence register, and mapping the appropriate aperture address into
2082 * userspace.
2083 */
2084 int
2085 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2086 struct drm_file *file)
2087 {
2088 struct drm_i915_gem_mmap_gtt *args = data;
2089
2090 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
2091 }
2092
2093 /* Immediately discard the backing storage */
2094 static void
2095 i915_gem_object_truncate(struct drm_i915_gem_object *obj)
2096 {
2097 i915_gem_object_free_mmap_offset(obj);
2098
2099 if (obj->base.filp == NULL)
2100 return;
2101
2102 /* Our goal here is to return as much of the memory as
2103 * is possible back to the system as we are called from OOM.
2104 * To do this we must instruct the shmfs to drop all of its
2105 * backing pages, *now*.
2106 */
2107 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
2108 obj->madv = __I915_MADV_PURGED;
2109 }
2110
2111 /* Try to discard unwanted pages */
2112 static void
2113 i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
2114 {
2115 struct address_space *mapping;
2116
2117 switch (obj->madv) {
2118 case I915_MADV_DONTNEED:
2119 i915_gem_object_truncate(obj);
2120 case __I915_MADV_PURGED:
2121 return;
2122 }
2123
2124 if (obj->base.filp == NULL)
2125 return;
2126
2127 mapping = file_inode(obj->base.filp)->i_mapping,
2128 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
2129 }
2130
2131 static void
2132 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
2133 {
2134 struct sg_page_iter sg_iter;
2135 int ret;
2136
2137 BUG_ON(obj->madv == __I915_MADV_PURGED);
2138
2139 ret = i915_gem_object_set_to_cpu_domain(obj, true);
2140 if (ret) {
2141 /* In the event of a disaster, abandon all caches and
2142 * hope for the best.
2143 */
2144 WARN_ON(ret != -EIO);
2145 i915_gem_clflush_object(obj, true);
2146 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2147 }
2148
2149 if (i915_gem_object_needs_bit17_swizzle(obj))
2150 i915_gem_object_save_bit_17_swizzle(obj);
2151
2152 if (obj->madv == I915_MADV_DONTNEED)
2153 obj->dirty = 0;
2154
2155 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
2156 struct page *page = sg_page_iter_page(&sg_iter);
2157
2158 if (obj->dirty)
2159 set_page_dirty(page);
2160
2161 if (obj->madv == I915_MADV_WILLNEED)
2162 mark_page_accessed(page);
2163
2164 page_cache_release(page);
2165 }
2166 obj->dirty = 0;
2167
2168 sg_free_table(obj->pages);
2169 kfree(obj->pages);
2170 }
2171
2172 int
2173 i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
2174 {
2175 const struct drm_i915_gem_object_ops *ops = obj->ops;
2176
2177 if (obj->pages == NULL)
2178 return 0;
2179
2180 if (obj->pages_pin_count)
2181 return -EBUSY;
2182
2183 BUG_ON(i915_gem_obj_bound_any(obj));
2184
2185 /* ->put_pages might need to allocate memory for the bit17 swizzle
2186 * array, hence protect them from being reaped by removing them from gtt
2187 * lists early. */
2188 list_del(&obj->global_list);
2189
2190 ops->put_pages(obj);
2191 obj->pages = NULL;
2192
2193 i915_gem_object_invalidate(obj);
2194
2195 return 0;
2196 }
2197
2198 static int
2199 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
2200 {
2201 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2202 int page_count, i;
2203 struct address_space *mapping;
2204 struct sg_table *st;
2205 struct scatterlist *sg;
2206 struct sg_page_iter sg_iter;
2207 struct page *page;
2208 unsigned long last_pfn = 0; /* suppress gcc warning */
2209 gfp_t gfp;
2210
2211 /* Assert that the object is not currently in any GPU domain. As it
2212 * wasn't in the GTT, there shouldn't be any way it could have been in
2213 * a GPU cache
2214 */
2215 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2216 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2217
2218 st = kmalloc(sizeof(*st), GFP_KERNEL);
2219 if (st == NULL)
2220 return -ENOMEM;
2221
2222 page_count = obj->base.size / PAGE_SIZE;
2223 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
2224 kfree(st);
2225 return -ENOMEM;
2226 }
2227
2228 /* Get the list of pages out of our struct file. They'll be pinned
2229 * at this point until we release them.
2230 *
2231 * Fail silently without starting the shrinker
2232 */
2233 mapping = file_inode(obj->base.filp)->i_mapping;
2234 gfp = mapping_gfp_mask(mapping);
2235 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
2236 gfp &= ~(__GFP_IO | __GFP_WAIT);
2237 sg = st->sgl;
2238 st->nents = 0;
2239 for (i = 0; i < page_count; i++) {
2240 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2241 if (IS_ERR(page)) {
2242 i915_gem_shrink(dev_priv,
2243 page_count,
2244 I915_SHRINK_BOUND |
2245 I915_SHRINK_UNBOUND |
2246 I915_SHRINK_PURGEABLE);
2247 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2248 }
2249 if (IS_ERR(page)) {
2250 /* We've tried hard to allocate the memory by reaping
2251 * our own buffer, now let the real VM do its job and
2252 * go down in flames if truly OOM.
2253 */
2254 i915_gem_shrink_all(dev_priv);
2255 page = shmem_read_mapping_page(mapping, i);
2256 if (IS_ERR(page))
2257 goto err_pages;
2258 }
2259 #ifdef CONFIG_SWIOTLB
2260 if (swiotlb_nr_tbl()) {
2261 st->nents++;
2262 sg_set_page(sg, page, PAGE_SIZE, 0);
2263 sg = sg_next(sg);
2264 continue;
2265 }
2266 #endif
2267 if (!i || page_to_pfn(page) != last_pfn + 1) {
2268 if (i)
2269 sg = sg_next(sg);
2270 st->nents++;
2271 sg_set_page(sg, page, PAGE_SIZE, 0);
2272 } else {
2273 sg->length += PAGE_SIZE;
2274 }
2275 last_pfn = page_to_pfn(page);
2276
2277 /* Check that the i965g/gm workaround works. */
2278 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
2279 }
2280 #ifdef CONFIG_SWIOTLB
2281 if (!swiotlb_nr_tbl())
2282 #endif
2283 sg_mark_end(sg);
2284 obj->pages = st;
2285
2286 if (i915_gem_object_needs_bit17_swizzle(obj))
2287 i915_gem_object_do_bit_17_swizzle(obj);
2288
2289 if (obj->tiling_mode != I915_TILING_NONE &&
2290 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
2291 i915_gem_object_pin_pages(obj);
2292
2293 return 0;
2294
2295 err_pages:
2296 sg_mark_end(sg);
2297 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
2298 page_cache_release(sg_page_iter_page(&sg_iter));
2299 sg_free_table(st);
2300 kfree(st);
2301
2302 /* shmemfs first checks if there is enough memory to allocate the page
2303 * and reports ENOSPC should there be insufficient, along with the usual
2304 * ENOMEM for a genuine allocation failure.
2305 *
2306 * We use ENOSPC in our driver to mean that we have run out of aperture
2307 * space and so want to translate the error from shmemfs back to our
2308 * usual understanding of ENOMEM.
2309 */
2310 if (PTR_ERR(page) == -ENOSPC)
2311 return -ENOMEM;
2312 else
2313 return PTR_ERR(page);
2314 }
2315
2316 /* Ensure that the associated pages are gathered from the backing storage
2317 * and pinned into our object. i915_gem_object_get_pages() may be called
2318 * multiple times before they are released by a single call to
2319 * i915_gem_object_put_pages() - once the pages are no longer referenced
2320 * either as a result of memory pressure (reaping pages under the shrinker)
2321 * or as the object is itself released.
2322 */
2323 int
2324 i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2325 {
2326 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2327 const struct drm_i915_gem_object_ops *ops = obj->ops;
2328 int ret;
2329
2330 if (obj->pages)
2331 return 0;
2332
2333 if (obj->madv != I915_MADV_WILLNEED) {
2334 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2335 return -EFAULT;
2336 }
2337
2338 BUG_ON(obj->pages_pin_count);
2339
2340 ret = ops->get_pages(obj);
2341 if (ret)
2342 return ret;
2343
2344 list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
2345
2346 obj->get_page.sg = obj->pages->sgl;
2347 obj->get_page.last = 0;
2348
2349 return 0;
2350 }
2351
2352 void i915_vma_move_to_active(struct i915_vma *vma,
2353 struct drm_i915_gem_request *req)
2354 {
2355 struct drm_i915_gem_object *obj = vma->obj;
2356 struct intel_engine_cs *ring;
2357
2358 ring = i915_gem_request_get_ring(req);
2359
2360 /* Add a reference if we're newly entering the active list. */
2361 if (obj->active == 0)
2362 drm_gem_object_reference(&obj->base);
2363 obj->active |= intel_ring_flag(ring);
2364
2365 list_move_tail(&obj->ring_list[ring->id], &ring->active_list);
2366 i915_gem_request_assign(&obj->last_read_req[ring->id], req);
2367
2368 list_move_tail(&vma->mm_list, &vma->vm->active_list);
2369 }
2370
2371 static void
2372 i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
2373 {
2374 RQ_BUG_ON(obj->last_write_req == NULL);
2375 RQ_BUG_ON(!(obj->active & intel_ring_flag(obj->last_write_req->ring)));
2376
2377 i915_gem_request_assign(&obj->last_write_req, NULL);
2378 intel_fb_obj_flush(obj, true);
2379 }
2380
2381 static void
2382 i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring)
2383 {
2384 struct i915_vma *vma;
2385
2386 RQ_BUG_ON(obj->last_read_req[ring] == NULL);
2387 RQ_BUG_ON(!(obj->active & (1 << ring)));
2388
2389 list_del_init(&obj->ring_list[ring]);
2390 i915_gem_request_assign(&obj->last_read_req[ring], NULL);
2391
2392 if (obj->last_write_req && obj->last_write_req->ring->id == ring)
2393 i915_gem_object_retire__write(obj);
2394
2395 obj->active &= ~(1 << ring);
2396 if (obj->active)
2397 return;
2398
2399 list_for_each_entry(vma, &obj->vma_list, vma_link) {
2400 if (!list_empty(&vma->mm_list))
2401 list_move_tail(&vma->mm_list, &vma->vm->inactive_list);
2402 }
2403
2404 i915_gem_request_assign(&obj->last_fenced_req, NULL);
2405 drm_gem_object_unreference(&obj->base);
2406 }
2407
2408 static int
2409 i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
2410 {
2411 struct drm_i915_private *dev_priv = dev->dev_private;
2412 struct intel_engine_cs *ring;
2413 int ret, i, j;
2414
2415 /* Carefully retire all requests without writing to the rings */
2416 for_each_ring(ring, dev_priv, i) {
2417 ret = intel_ring_idle(ring);
2418 if (ret)
2419 return ret;
2420 }
2421 i915_gem_retire_requests(dev);
2422
2423 /* Finally reset hw state */
2424 for_each_ring(ring, dev_priv, i) {
2425 intel_ring_init_seqno(ring, seqno);
2426
2427 for (j = 0; j < ARRAY_SIZE(ring->semaphore.sync_seqno); j++)
2428 ring->semaphore.sync_seqno[j] = 0;
2429 }
2430
2431 return 0;
2432 }
2433
2434 int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
2435 {
2436 struct drm_i915_private *dev_priv = dev->dev_private;
2437 int ret;
2438
2439 if (seqno == 0)
2440 return -EINVAL;
2441
2442 /* HWS page needs to be set less than what we
2443 * will inject to ring
2444 */
2445 ret = i915_gem_init_seqno(dev, seqno - 1);
2446 if (ret)
2447 return ret;
2448
2449 /* Carefully set the last_seqno value so that wrap
2450 * detection still works
2451 */
2452 dev_priv->next_seqno = seqno;
2453 dev_priv->last_seqno = seqno - 1;
2454 if (dev_priv->last_seqno == 0)
2455 dev_priv->last_seqno--;
2456
2457 return 0;
2458 }
2459
2460 int
2461 i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
2462 {
2463 struct drm_i915_private *dev_priv = dev->dev_private;
2464
2465 /* reserve 0 for non-seqno */
2466 if (dev_priv->next_seqno == 0) {
2467 int ret = i915_gem_init_seqno(dev, 0);
2468 if (ret)
2469 return ret;
2470
2471 dev_priv->next_seqno = 1;
2472 }
2473
2474 *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
2475 return 0;
2476 }
2477
2478 /*
2479 * NB: This function is not allowed to fail. Doing so would mean the the
2480 * request is not being tracked for completion but the work itself is
2481 * going to happen on the hardware. This would be a Bad Thing(tm).
2482 */
2483 void __i915_add_request(struct drm_i915_gem_request *request,
2484 struct drm_i915_gem_object *obj,
2485 bool flush_caches)
2486 {
2487 struct intel_engine_cs *ring;
2488 struct drm_i915_private *dev_priv;
2489 struct intel_ringbuffer *ringbuf;
2490 u32 request_start;
2491 int ret;
2492
2493 if (WARN_ON(request == NULL))
2494 return;
2495
2496 ring = request->ring;
2497 dev_priv = ring->dev->dev_private;
2498 ringbuf = request->ringbuf;
2499
2500 /*
2501 * To ensure that this call will not fail, space for its emissions
2502 * should already have been reserved in the ring buffer. Let the ring
2503 * know that it is time to use that space up.
2504 */
2505 intel_ring_reserved_space_use(ringbuf);
2506
2507 request_start = intel_ring_get_tail(ringbuf);
2508 /*
2509 * Emit any outstanding flushes - execbuf can fail to emit the flush
2510 * after having emitted the batchbuffer command. Hence we need to fix
2511 * things up similar to emitting the lazy request. The difference here
2512 * is that the flush _must_ happen before the next request, no matter
2513 * what.
2514 */
2515 if (flush_caches) {
2516 if (i915.enable_execlists)
2517 ret = logical_ring_flush_all_caches(request);
2518 else
2519 ret = intel_ring_flush_all_caches(request);
2520 /* Not allowed to fail! */
2521 WARN(ret, "*_ring_flush_all_caches failed: %d!\n", ret);
2522 }
2523
2524 /* Record the position of the start of the request so that
2525 * should we detect the updated seqno part-way through the
2526 * GPU processing the request, we never over-estimate the
2527 * position of the head.
2528 */
2529 request->postfix = intel_ring_get_tail(ringbuf);
2530
2531 if (i915.enable_execlists)
2532 ret = ring->emit_request(request);
2533 else {
2534 ret = ring->add_request(request);
2535
2536 request->tail = intel_ring_get_tail(ringbuf);
2537 }
2538 /* Not allowed to fail! */
2539 WARN(ret, "emit|add_request failed: %d!\n", ret);
2540
2541 request->head = request_start;
2542
2543 /* Whilst this request exists, batch_obj will be on the
2544 * active_list, and so will hold the active reference. Only when this
2545 * request is retired will the the batch_obj be moved onto the
2546 * inactive_list and lose its active reference. Hence we do not need
2547 * to explicitly hold another reference here.
2548 */
2549 request->batch_obj = obj;
2550
2551 request->emitted_jiffies = jiffies;
2552 list_add_tail(&request->list, &ring->request_list);
2553
2554 trace_i915_gem_request_add(request);
2555
2556 i915_queue_hangcheck(ring->dev);
2557
2558 queue_delayed_work(dev_priv->wq,
2559 &dev_priv->mm.retire_work,
2560 round_jiffies_up_relative(HZ));
2561 intel_mark_busy(dev_priv->dev);
2562
2563 /* Sanity check that the reserved size was large enough. */
2564 intel_ring_reserved_space_end(ringbuf);
2565 }
2566
2567 static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
2568 const struct intel_context *ctx)
2569 {
2570 unsigned long elapsed;
2571
2572 elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
2573
2574 if (ctx->hang_stats.banned)
2575 return true;
2576
2577 if (ctx->hang_stats.ban_period_seconds &&
2578 elapsed <= ctx->hang_stats.ban_period_seconds) {
2579 if (!i915_gem_context_is_default(ctx)) {
2580 DRM_DEBUG("context hanging too fast, banning!\n");
2581 return true;
2582 } else if (i915_stop_ring_allow_ban(dev_priv)) {
2583 if (i915_stop_ring_allow_warn(dev_priv))
2584 DRM_ERROR("gpu hanging too fast, banning!\n");
2585 return true;
2586 }
2587 }
2588
2589 return false;
2590 }
2591
2592 static void i915_set_reset_status(struct drm_i915_private *dev_priv,
2593 struct intel_context *ctx,
2594 const bool guilty)
2595 {
2596 struct i915_ctx_hang_stats *hs;
2597
2598 if (WARN_ON(!ctx))
2599 return;
2600
2601 hs = &ctx->hang_stats;
2602
2603 if (guilty) {
2604 hs->banned = i915_context_is_banned(dev_priv, ctx);
2605 hs->batch_active++;
2606 hs->guilty_ts = get_seconds();
2607 } else {
2608 hs->batch_pending++;
2609 }
2610 }
2611
2612 void i915_gem_request_free(struct kref *req_ref)
2613 {
2614 struct drm_i915_gem_request *req = container_of(req_ref,
2615 typeof(*req), ref);
2616 struct intel_context *ctx = req->ctx;
2617
2618 if (req->file_priv)
2619 i915_gem_request_remove_from_client(req);
2620
2621 if (ctx) {
2622 if (i915.enable_execlists) {
2623 struct intel_engine_cs *ring = req->ring;
2624
2625 if (ctx != ring->default_context)
2626 intel_lr_context_unpin(ring, ctx);
2627 }
2628
2629 i915_gem_context_unreference(ctx);
2630 }
2631
2632 kmem_cache_free(req->i915->requests, req);
2633 }
2634
2635 int i915_gem_request_alloc(struct intel_engine_cs *ring,
2636 struct intel_context *ctx,
2637 struct drm_i915_gem_request **req_out)
2638 {
2639 struct drm_i915_private *dev_priv = to_i915(ring->dev);
2640 struct drm_i915_gem_request *req;
2641 int ret;
2642
2643 if (!req_out)
2644 return -EINVAL;
2645
2646 *req_out = NULL;
2647
2648 req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
2649 if (req == NULL)
2650 return -ENOMEM;
2651
2652 ret = i915_gem_get_seqno(ring->dev, &req->seqno);
2653 if (ret)
2654 goto err;
2655
2656 kref_init(&req->ref);
2657 req->i915 = dev_priv;
2658 req->ring = ring;
2659 req->ctx = ctx;
2660 i915_gem_context_reference(req->ctx);
2661
2662 if (i915.enable_execlists)
2663 ret = intel_logical_ring_alloc_request_extras(req);
2664 else
2665 ret = intel_ring_alloc_request_extras(req);
2666 if (ret) {
2667 i915_gem_context_unreference(req->ctx);
2668 goto err;
2669 }
2670
2671 /*
2672 * Reserve space in the ring buffer for all the commands required to
2673 * eventually emit this request. This is to guarantee that the
2674 * i915_add_request() call can't fail. Note that the reserve may need
2675 * to be redone if the request is not actually submitted straight
2676 * away, e.g. because a GPU scheduler has deferred it.
2677 */
2678 if (i915.enable_execlists)
2679 ret = intel_logical_ring_reserve_space(req);
2680 else
2681 ret = intel_ring_reserve_space(req);
2682 if (ret) {
2683 /*
2684 * At this point, the request is fully allocated even if not
2685 * fully prepared. Thus it can be cleaned up using the proper
2686 * free code.
2687 */
2688 i915_gem_request_cancel(req);
2689 return ret;
2690 }
2691
2692 *req_out = req;
2693 return 0;
2694
2695 err:
2696 kmem_cache_free(dev_priv->requests, req);
2697 return ret;
2698 }
2699
2700 void i915_gem_request_cancel(struct drm_i915_gem_request *req)
2701 {
2702 intel_ring_reserved_space_cancel(req->ringbuf);
2703
2704 i915_gem_request_unreference(req);
2705 }
2706
2707 struct drm_i915_gem_request *
2708 i915_gem_find_active_request(struct intel_engine_cs *ring)
2709 {
2710 struct drm_i915_gem_request *request;
2711
2712 list_for_each_entry(request, &ring->request_list, list) {
2713 if (i915_gem_request_completed(request, false))
2714 continue;
2715
2716 return request;
2717 }
2718
2719 return NULL;
2720 }
2721
2722 static void i915_gem_reset_ring_status(struct drm_i915_private *dev_priv,
2723 struct intel_engine_cs *ring)
2724 {
2725 struct drm_i915_gem_request *request;
2726 bool ring_hung;
2727
2728 request = i915_gem_find_active_request(ring);
2729
2730 if (request == NULL)
2731 return;
2732
2733 ring_hung = ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
2734
2735 i915_set_reset_status(dev_priv, request->ctx, ring_hung);
2736
2737 list_for_each_entry_continue(request, &ring->request_list, list)
2738 i915_set_reset_status(dev_priv, request->ctx, false);
2739 }
2740
2741 static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv,
2742 struct intel_engine_cs *ring)
2743 {
2744 while (!list_empty(&ring->active_list)) {
2745 struct drm_i915_gem_object *obj;
2746
2747 obj = list_first_entry(&ring->active_list,
2748 struct drm_i915_gem_object,
2749 ring_list[ring->id]);
2750
2751 i915_gem_object_retire__read(obj, ring->id);
2752 }
2753
2754 /*
2755 * Clear the execlists queue up before freeing the requests, as those
2756 * are the ones that keep the context and ringbuffer backing objects
2757 * pinned in place.
2758 */
2759 while (!list_empty(&ring->execlist_queue)) {
2760 struct drm_i915_gem_request *submit_req;
2761
2762 submit_req = list_first_entry(&ring->execlist_queue,
2763 struct drm_i915_gem_request,
2764 execlist_link);
2765 list_del(&submit_req->execlist_link);
2766
2767 if (submit_req->ctx != ring->default_context)
2768 intel_lr_context_unpin(ring, submit_req->ctx);
2769
2770 i915_gem_request_unreference(submit_req);
2771 }
2772
2773 /*
2774 * We must free the requests after all the corresponding objects have
2775 * been moved off active lists. Which is the same order as the normal
2776 * retire_requests function does. This is important if object hold
2777 * implicit references on things like e.g. ppgtt address spaces through
2778 * the request.
2779 */
2780 while (!list_empty(&ring->request_list)) {
2781 struct drm_i915_gem_request *request;
2782
2783 request = list_first_entry(&ring->request_list,
2784 struct drm_i915_gem_request,
2785 list);
2786
2787 i915_gem_request_retire(request);
2788 }
2789 }
2790
2791 void i915_gem_restore_fences(struct drm_device *dev)
2792 {
2793 struct drm_i915_private *dev_priv = dev->dev_private;
2794 int i;
2795
2796 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2797 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2798
2799 /*
2800 * Commit delayed tiling changes if we have an object still
2801 * attached to the fence, otherwise just clear the fence.
2802 */
2803 if (reg->obj) {
2804 i915_gem_object_update_fence(reg->obj, reg,
2805 reg->obj->tiling_mode);
2806 } else {
2807 i915_gem_write_fence(dev, i, NULL);
2808 }
2809 }
2810 }
2811
2812 void i915_gem_reset(struct drm_device *dev)
2813 {
2814 struct drm_i915_private *dev_priv = dev->dev_private;
2815 struct intel_engine_cs *ring;
2816 int i;
2817
2818 /*
2819 * Before we free the objects from the requests, we need to inspect
2820 * them for finding the guilty party. As the requests only borrow
2821 * their reference to the objects, the inspection must be done first.
2822 */
2823 for_each_ring(ring, dev_priv, i)
2824 i915_gem_reset_ring_status(dev_priv, ring);
2825
2826 for_each_ring(ring, dev_priv, i)
2827 i915_gem_reset_ring_cleanup(dev_priv, ring);
2828
2829 i915_gem_context_reset(dev);
2830
2831 i915_gem_restore_fences(dev);
2832
2833 WARN_ON(i915_verify_lists(dev));
2834 }
2835
2836 /**
2837 * This function clears the request list as sequence numbers are passed.
2838 */
2839 void
2840 i915_gem_retire_requests_ring(struct intel_engine_cs *ring)
2841 {
2842 WARN_ON(i915_verify_lists(ring->dev));
2843
2844 /* Retire requests first as we use it above for the early return.
2845 * If we retire requests last, we may use a later seqno and so clear
2846 * the requests lists without clearing the active list, leading to
2847 * confusion.
2848 */
2849 while (!list_empty(&ring->request_list)) {
2850 struct drm_i915_gem_request *request;
2851
2852 request = list_first_entry(&ring->request_list,
2853 struct drm_i915_gem_request,
2854 list);
2855
2856 if (!i915_gem_request_completed(request, true))
2857 break;
2858
2859 i915_gem_request_retire(request);
2860 }
2861
2862 /* Move any buffers on the active list that are no longer referenced
2863 * by the ringbuffer to the flushing/inactive lists as appropriate,
2864 * before we free the context associated with the requests.
2865 */
2866 while (!list_empty(&ring->active_list)) {
2867 struct drm_i915_gem_object *obj;
2868
2869 obj = list_first_entry(&ring->active_list,
2870 struct drm_i915_gem_object,
2871 ring_list[ring->id]);
2872
2873 if (!list_empty(&obj->last_read_req[ring->id]->list))
2874 break;
2875
2876 i915_gem_object_retire__read(obj, ring->id);
2877 }
2878
2879 if (unlikely(ring->trace_irq_req &&
2880 i915_gem_request_completed(ring->trace_irq_req, true))) {
2881 ring->irq_put(ring);
2882 i915_gem_request_assign(&ring->trace_irq_req, NULL);
2883 }
2884
2885 WARN_ON(i915_verify_lists(ring->dev));
2886 }
2887
2888 bool
2889 i915_gem_retire_requests(struct drm_device *dev)
2890 {
2891 struct drm_i915_private *dev_priv = dev->dev_private;
2892 struct intel_engine_cs *ring;
2893 bool idle = true;
2894 int i;
2895
2896 for_each_ring(ring, dev_priv, i) {
2897 i915_gem_retire_requests_ring(ring);
2898 idle &= list_empty(&ring->request_list);
2899 if (i915.enable_execlists) {
2900 unsigned long flags;
2901
2902 spin_lock_irqsave(&ring->execlist_lock, flags);
2903 idle &= list_empty(&ring->execlist_queue);
2904 spin_unlock_irqrestore(&ring->execlist_lock, flags);
2905
2906 intel_execlists_retire_requests(ring);
2907 }
2908 }
2909
2910 if (idle)
2911 mod_delayed_work(dev_priv->wq,
2912 &dev_priv->mm.idle_work,
2913 msecs_to_jiffies(100));
2914
2915 return idle;
2916 }
2917
2918 static void
2919 i915_gem_retire_work_handler(struct work_struct *work)
2920 {
2921 struct drm_i915_private *dev_priv =
2922 container_of(work, typeof(*dev_priv), mm.retire_work.work);
2923 struct drm_device *dev = dev_priv->dev;
2924 bool idle;
2925
2926 /* Come back later if the device is busy... */
2927 idle = false;
2928 if (mutex_trylock(&dev->struct_mutex)) {
2929 idle = i915_gem_retire_requests(dev);
2930 mutex_unlock(&dev->struct_mutex);
2931 }
2932 if (!idle)
2933 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2934 round_jiffies_up_relative(HZ));
2935 }
2936
2937 static void
2938 i915_gem_idle_work_handler(struct work_struct *work)
2939 {
2940 struct drm_i915_private *dev_priv =
2941 container_of(work, typeof(*dev_priv), mm.idle_work.work);
2942 struct drm_device *dev = dev_priv->dev;
2943 struct intel_engine_cs *ring;
2944 int i;
2945
2946 for_each_ring(ring, dev_priv, i)
2947 if (!list_empty(&ring->request_list))
2948 return;
2949
2950 intel_mark_idle(dev);
2951
2952 if (mutex_trylock(&dev->struct_mutex)) {
2953 struct intel_engine_cs *ring;
2954 int i;
2955
2956 for_each_ring(ring, dev_priv, i)
2957 i915_gem_batch_pool_fini(&ring->batch_pool);
2958
2959 mutex_unlock(&dev->struct_mutex);
2960 }
2961 }
2962
2963 /**
2964 * Ensures that an object will eventually get non-busy by flushing any required
2965 * write domains, emitting any outstanding lazy request and retiring and
2966 * completed requests.
2967 */
2968 static int
2969 i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2970 {
2971 int i;
2972
2973 if (!obj->active)
2974 return 0;
2975
2976 for (i = 0; i < I915_NUM_RINGS; i++) {
2977 struct drm_i915_gem_request *req;
2978
2979 req = obj->last_read_req[i];
2980 if (req == NULL)
2981 continue;
2982
2983 if (list_empty(&req->list))
2984 goto retire;
2985
2986 if (i915_gem_request_completed(req, true)) {
2987 __i915_gem_request_retire__upto(req);
2988 retire:
2989 i915_gem_object_retire__read(obj, i);
2990 }
2991 }
2992
2993 return 0;
2994 }
2995
2996 /**
2997 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2998 * @DRM_IOCTL_ARGS: standard ioctl arguments
2999 *
3000 * Returns 0 if successful, else an error is returned with the remaining time in
3001 * the timeout parameter.
3002 * -ETIME: object is still busy after timeout
3003 * -ERESTARTSYS: signal interrupted the wait
3004 * -ENONENT: object doesn't exist
3005 * Also possible, but rare:
3006 * -EAGAIN: GPU wedged
3007 * -ENOMEM: damn
3008 * -ENODEV: Internal IRQ fail
3009 * -E?: The add request failed
3010 *
3011 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3012 * non-zero timeout parameter the wait ioctl will wait for the given number of
3013 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3014 * without holding struct_mutex the object may become re-busied before this
3015 * function completes. A similar but shorter * race condition exists in the busy
3016 * ioctl
3017 */
3018 int
3019 i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3020 {
3021 struct drm_i915_private *dev_priv = dev->dev_private;
3022 struct drm_i915_gem_wait *args = data;
3023 struct drm_i915_gem_object *obj;
3024 struct drm_i915_gem_request *req[I915_NUM_RINGS];
3025 unsigned reset_counter;
3026 int i, n = 0;
3027 int ret;
3028
3029 if (args->flags != 0)
3030 return -EINVAL;
3031
3032 ret = i915_mutex_lock_interruptible(dev);
3033 if (ret)
3034 return ret;
3035
3036 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
3037 if (&obj->base == NULL) {
3038 mutex_unlock(&dev->struct_mutex);
3039 return -ENOENT;
3040 }
3041
3042 /* Need to make sure the object gets inactive eventually. */
3043 ret = i915_gem_object_flush_active(obj);
3044 if (ret)
3045 goto out;
3046
3047 if (!obj->active)
3048 goto out;
3049
3050 /* Do this after OLR check to make sure we make forward progress polling
3051 * on this IOCTL with a timeout == 0 (like busy ioctl)
3052 */
3053 if (args->timeout_ns == 0) {
3054 ret = -ETIME;
3055 goto out;
3056 }
3057
3058 drm_gem_object_unreference(&obj->base);
3059 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
3060
3061 for (i = 0; i < I915_NUM_RINGS; i++) {
3062 if (obj->last_read_req[i] == NULL)
3063 continue;
3064
3065 req[n++] = i915_gem_request_reference(obj->last_read_req[i]);
3066 }
3067
3068 mutex_unlock(&dev->struct_mutex);
3069
3070 for (i = 0; i < n; i++) {
3071 if (ret == 0)
3072 ret = __i915_wait_request(req[i], reset_counter, true,
3073 args->timeout_ns > 0 ? &args->timeout_ns : NULL,
3074 file->driver_priv);
3075 i915_gem_request_unreference__unlocked(req[i]);
3076 }
3077 return ret;
3078
3079 out:
3080 drm_gem_object_unreference(&obj->base);
3081 mutex_unlock(&dev->struct_mutex);
3082 return ret;
3083 }
3084
3085 static int
3086 __i915_gem_object_sync(struct drm_i915_gem_object *obj,
3087 struct intel_engine_cs *to,
3088 struct drm_i915_gem_request *from_req,
3089 struct drm_i915_gem_request **to_req)
3090 {
3091 struct intel_engine_cs *from;
3092 int ret;
3093
3094 from = i915_gem_request_get_ring(from_req);
3095 if (to == from)
3096 return 0;
3097
3098 if (i915_gem_request_completed(from_req, true))
3099 return 0;
3100
3101 if (!i915_semaphore_is_enabled(obj->base.dev)) {
3102 struct drm_i915_private *i915 = to_i915(obj->base.dev);
3103 ret = __i915_wait_request(from_req,
3104 atomic_read(&i915->gpu_error.reset_counter),
3105 i915->mm.interruptible,
3106 NULL,
3107 &i915->rps.semaphores);
3108 if (ret)
3109 return ret;
3110
3111 i915_gem_object_retire_request(obj, from_req);
3112 } else {
3113 int idx = intel_ring_sync_index(from, to);
3114 u32 seqno = i915_gem_request_get_seqno(from_req);
3115
3116 WARN_ON(!to_req);
3117
3118 if (seqno <= from->semaphore.sync_seqno[idx])
3119 return 0;
3120
3121 if (*to_req == NULL) {
3122 ret = i915_gem_request_alloc(to, to->default_context, to_req);
3123 if (ret)
3124 return ret;
3125 }
3126
3127 trace_i915_gem_ring_sync_to(*to_req, from, from_req);
3128 ret = to->semaphore.sync_to(*to_req, from, seqno);
3129 if (ret)
3130 return ret;
3131
3132 /* We use last_read_req because sync_to()
3133 * might have just caused seqno wrap under
3134 * the radar.
3135 */
3136 from->semaphore.sync_seqno[idx] =
3137 i915_gem_request_get_seqno(obj->last_read_req[from->id]);
3138 }
3139
3140 return 0;
3141 }
3142
3143 /**
3144 * i915_gem_object_sync - sync an object to a ring.
3145 *
3146 * @obj: object which may be in use on another ring.
3147 * @to: ring we wish to use the object on. May be NULL.
3148 * @to_req: request we wish to use the object for. See below.
3149 * This will be allocated and returned if a request is
3150 * required but not passed in.
3151 *
3152 * This code is meant to abstract object synchronization with the GPU.
3153 * Calling with NULL implies synchronizing the object with the CPU
3154 * rather than a particular GPU ring. Conceptually we serialise writes
3155 * between engines inside the GPU. We only allow one engine to write
3156 * into a buffer at any time, but multiple readers. To ensure each has
3157 * a coherent view of memory, we must:
3158 *
3159 * - If there is an outstanding write request to the object, the new
3160 * request must wait for it to complete (either CPU or in hw, requests
3161 * on the same ring will be naturally ordered).
3162 *
3163 * - If we are a write request (pending_write_domain is set), the new
3164 * request must wait for outstanding read requests to complete.
3165 *
3166 * For CPU synchronisation (NULL to) no request is required. For syncing with
3167 * rings to_req must be non-NULL. However, a request does not have to be
3168 * pre-allocated. If *to_req is NULL and sync commands will be emitted then a
3169 * request will be allocated automatically and returned through *to_req. Note
3170 * that it is not guaranteed that commands will be emitted (because the system
3171 * might already be idle). Hence there is no need to create a request that
3172 * might never have any work submitted. Note further that if a request is
3173 * returned in *to_req, it is the responsibility of the caller to submit
3174 * that request (after potentially adding more work to it).
3175 *
3176 * Returns 0 if successful, else propagates up the lower layer error.
3177 */
3178 int
3179 i915_gem_object_sync(struct drm_i915_gem_object *obj,
3180 struct intel_engine_cs *to,
3181 struct drm_i915_gem_request **to_req)
3182 {
3183 const bool readonly = obj->base.pending_write_domain == 0;
3184 struct drm_i915_gem_request *req[I915_NUM_RINGS];
3185 int ret, i, n;
3186
3187 if (!obj->active)
3188 return 0;
3189
3190 if (to == NULL)
3191 return i915_gem_object_wait_rendering(obj, readonly);
3192
3193 n = 0;
3194 if (readonly) {
3195 if (obj->last_write_req)
3196 req[n++] = obj->last_write_req;
3197 } else {
3198 for (i = 0; i < I915_NUM_RINGS; i++)
3199 if (obj->last_read_req[i])
3200 req[n++] = obj->last_read_req[i];
3201 }
3202 for (i = 0; i < n; i++) {
3203 ret = __i915_gem_object_sync(obj, to, req[i], to_req);
3204 if (ret)
3205 return ret;
3206 }
3207
3208 return 0;
3209 }
3210
3211 static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
3212 {
3213 u32 old_write_domain, old_read_domains;
3214
3215 /* Force a pagefault for domain tracking on next user access */
3216 i915_gem_release_mmap(obj);
3217
3218 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3219 return;
3220
3221 /* Wait for any direct GTT access to complete */
3222 mb();
3223
3224 old_read_domains = obj->base.read_domains;
3225 old_write_domain = obj->base.write_domain;
3226
3227 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
3228 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
3229
3230 trace_i915_gem_object_change_domain(obj,
3231 old_read_domains,
3232 old_write_domain);
3233 }
3234
3235 int i915_vma_unbind(struct i915_vma *vma)
3236 {
3237 struct drm_i915_gem_object *obj = vma->obj;
3238 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3239 int ret;
3240
3241 if (list_empty(&vma->vma_link))
3242 return 0;
3243
3244 if (!drm_mm_node_allocated(&vma->node)) {
3245 i915_gem_vma_destroy(vma);
3246 return 0;
3247 }
3248
3249 if (vma->pin_count)
3250 return -EBUSY;
3251
3252 BUG_ON(obj->pages == NULL);
3253
3254 ret = i915_gem_object_wait_rendering(obj, false);
3255 if (ret)
3256 return ret;
3257 /* Continue on if we fail due to EIO, the GPU is hung so we
3258 * should be safe and we need to cleanup or else we might
3259 * cause memory corruption through use-after-free.
3260 */
3261
3262 if (i915_is_ggtt(vma->vm) &&
3263 vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
3264 i915_gem_object_finish_gtt(obj);
3265
3266 /* release the fence reg _after_ flushing */
3267 ret = i915_gem_object_put_fence(obj);
3268 if (ret)
3269 return ret;
3270 }
3271
3272 trace_i915_vma_unbind(vma);
3273
3274 vma->vm->unbind_vma(vma);
3275 vma->bound = 0;
3276
3277 list_del_init(&vma->mm_list);
3278 if (i915_is_ggtt(vma->vm)) {
3279 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
3280 obj->map_and_fenceable = false;
3281 } else if (vma->ggtt_view.pages) {
3282 sg_free_table(vma->ggtt_view.pages);
3283 kfree(vma->ggtt_view.pages);
3284 vma->ggtt_view.pages = NULL;
3285 }
3286 }
3287
3288 drm_mm_remove_node(&vma->node);
3289 i915_gem_vma_destroy(vma);
3290
3291 /* Since the unbound list is global, only move to that list if
3292 * no more VMAs exist. */
3293 if (list_empty(&obj->vma_list)) {
3294 i915_gem_gtt_finish_object(obj);
3295 list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
3296 }
3297
3298 /* And finally now the object is completely decoupled from this vma,
3299 * we can drop its hold on the backing storage and allow it to be
3300 * reaped by the shrinker.
3301 */
3302 i915_gem_object_unpin_pages(obj);
3303
3304 return 0;
3305 }
3306
3307 int i915_gpu_idle(struct drm_device *dev)
3308 {
3309 struct drm_i915_private *dev_priv = dev->dev_private;
3310 struct intel_engine_cs *ring;
3311 int ret, i;
3312
3313 /* Flush everything onto the inactive list. */
3314 for_each_ring(ring, dev_priv, i) {
3315 if (!i915.enable_execlists) {
3316 struct drm_i915_gem_request *req;
3317
3318 ret = i915_gem_request_alloc(ring, ring->default_context, &req);
3319 if (ret)
3320 return ret;
3321
3322 ret = i915_switch_context(req);
3323 if (ret) {
3324 i915_gem_request_cancel(req);
3325 return ret;
3326 }
3327
3328 i915_add_request_no_flush(req);
3329 }
3330
3331 ret = intel_ring_idle(ring);
3332 if (ret)
3333 return ret;
3334 }
3335
3336 WARN_ON(i915_verify_lists(dev));
3337 return 0;
3338 }
3339
3340 static void i965_write_fence_reg(struct drm_device *dev, int reg,
3341 struct drm_i915_gem_object *obj)
3342 {
3343 struct drm_i915_private *dev_priv = dev->dev_private;
3344 int fence_reg;
3345 int fence_pitch_shift;
3346
3347 if (INTEL_INFO(dev)->gen >= 6) {
3348 fence_reg = FENCE_REG_SANDYBRIDGE_0;
3349 fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
3350 } else {
3351 fence_reg = FENCE_REG_965_0;
3352 fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
3353 }
3354
3355 fence_reg += reg * 8;
3356
3357 /* To w/a incoherency with non-atomic 64-bit register updates,
3358 * we split the 64-bit update into two 32-bit writes. In order
3359 * for a partial fence not to be evaluated between writes, we
3360 * precede the update with write to turn off the fence register,
3361 * and only enable the fence as the last step.
3362 *
3363 * For extra levels of paranoia, we make sure each step lands
3364 * before applying the next step.
3365 */
3366 I915_WRITE(fence_reg, 0);
3367 POSTING_READ(fence_reg);
3368
3369 if (obj) {
3370 u32 size = i915_gem_obj_ggtt_size(obj);
3371 uint64_t val;
3372
3373 /* Adjust fence size to match tiled area */
3374 if (obj->tiling_mode != I915_TILING_NONE) {
3375 uint32_t row_size = obj->stride *
3376 (obj->tiling_mode == I915_TILING_Y ? 32 : 8);
3377 size = (size / row_size) * row_size;
3378 }
3379
3380 val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
3381 0xfffff000) << 32;
3382 val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
3383 val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
3384 if (obj->tiling_mode == I915_TILING_Y)
3385 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
3386 val |= I965_FENCE_REG_VALID;
3387
3388 I915_WRITE(fence_reg + 4, val >> 32);
3389 POSTING_READ(fence_reg + 4);
3390
3391 I915_WRITE(fence_reg + 0, val);
3392 POSTING_READ(fence_reg);
3393 } else {
3394 I915_WRITE(fence_reg + 4, 0);
3395 POSTING_READ(fence_reg + 4);
3396 }
3397 }
3398
3399 static void i915_write_fence_reg(struct drm_device *dev, int reg,
3400 struct drm_i915_gem_object *obj)
3401 {
3402 struct drm_i915_private *dev_priv = dev->dev_private;
3403 u32 val;
3404
3405 if (obj) {
3406 u32 size = i915_gem_obj_ggtt_size(obj);
3407 int pitch_val;
3408 int tile_width;
3409
3410 WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
3411 (size & -size) != size ||
3412 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
3413 "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
3414 i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
3415
3416 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
3417 tile_width = 128;
3418 else
3419 tile_width = 512;
3420
3421 /* Note: pitch better be a power of two tile widths */
3422 pitch_val = obj->stride / tile_width;
3423 pitch_val = ffs(pitch_val) - 1;
3424
3425 val = i915_gem_obj_ggtt_offset(obj);
3426 if (obj->tiling_mode == I915_TILING_Y)
3427 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
3428 val |= I915_FENCE_SIZE_BITS(size);
3429 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
3430 val |= I830_FENCE_REG_VALID;
3431 } else
3432 val = 0;
3433
3434 if (reg < 8)
3435 reg = FENCE_REG_830_0 + reg * 4;
3436 else
3437 reg = FENCE_REG_945_8 + (reg - 8) * 4;
3438
3439 I915_WRITE(reg, val);
3440 POSTING_READ(reg);
3441 }
3442
3443 static void i830_write_fence_reg(struct drm_device *dev, int reg,
3444 struct drm_i915_gem_object *obj)
3445 {
3446 struct drm_i915_private *dev_priv = dev->dev_private;
3447 uint32_t val;
3448
3449 if (obj) {
3450 u32 size = i915_gem_obj_ggtt_size(obj);
3451 uint32_t pitch_val;
3452
3453 WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
3454 (size & -size) != size ||
3455 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
3456 "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
3457 i915_gem_obj_ggtt_offset(obj), size);
3458
3459 pitch_val = obj->stride / 128;
3460 pitch_val = ffs(pitch_val) - 1;
3461
3462 val = i915_gem_obj_ggtt_offset(obj);
3463 if (obj->tiling_mode == I915_TILING_Y)
3464 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
3465 val |= I830_FENCE_SIZE_BITS(size);
3466 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
3467 val |= I830_FENCE_REG_VALID;
3468 } else
3469 val = 0;
3470
3471 I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
3472 POSTING_READ(FENCE_REG_830_0 + reg * 4);
3473 }
3474
3475 inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
3476 {
3477 return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
3478 }
3479
3480 static void i915_gem_write_fence(struct drm_device *dev, int reg,
3481 struct drm_i915_gem_object *obj)
3482 {
3483 struct drm_i915_private *dev_priv = dev->dev_private;
3484
3485 /* Ensure that all CPU reads are completed before installing a fence
3486 * and all writes before removing the fence.
3487 */
3488 if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
3489 mb();
3490
3491 WARN(obj && (!obj->stride || !obj->tiling_mode),
3492 "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
3493 obj->stride, obj->tiling_mode);
3494
3495 if (IS_GEN2(dev))
3496 i830_write_fence_reg(dev, reg, obj);
3497 else if (IS_GEN3(dev))
3498 i915_write_fence_reg(dev, reg, obj);
3499 else if (INTEL_INFO(dev)->gen >= 4)
3500 i965_write_fence_reg(dev, reg, obj);
3501
3502 /* And similarly be paranoid that no direct access to this region
3503 * is reordered to before the fence is installed.
3504 */
3505 if (i915_gem_object_needs_mb(obj))
3506 mb();
3507 }
3508
3509 static inline int fence_number(struct drm_i915_private *dev_priv,
3510 struct drm_i915_fence_reg *fence)
3511 {
3512 return fence - dev_priv->fence_regs;
3513 }
3514
3515 static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
3516 struct drm_i915_fence_reg *fence,
3517 bool enable)
3518 {
3519 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3520 int reg = fence_number(dev_priv, fence);
3521
3522 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
3523
3524 if (enable) {
3525 obj->fence_reg = reg;
3526 fence->obj = obj;
3527 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
3528 } else {
3529 obj->fence_reg = I915_FENCE_REG_NONE;
3530 fence->obj = NULL;
3531 list_del_init(&fence->lru_list);
3532 }
3533 obj->fence_dirty = false;
3534 }
3535
3536 static int
3537 i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
3538 {
3539 if (obj->last_fenced_req) {
3540 int ret = i915_wait_request(obj->last_fenced_req);
3541 if (ret)
3542 return ret;
3543
3544 i915_gem_request_assign(&obj->last_fenced_req, NULL);
3545 }
3546
3547 return 0;
3548 }
3549
3550 int
3551 i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
3552 {
3553 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3554 struct drm_i915_fence_reg *fence;
3555 int ret;
3556
3557 ret = i915_gem_object_wait_fence(obj);
3558 if (ret)
3559 return ret;
3560
3561 if (obj->fence_reg == I915_FENCE_REG_NONE)
3562 return 0;
3563
3564 fence = &dev_priv->fence_regs[obj->fence_reg];
3565
3566 if (WARN_ON(fence->pin_count))
3567 return -EBUSY;
3568
3569 i915_gem_object_fence_lost(obj);
3570 i915_gem_object_update_fence(obj, fence, false);
3571
3572 return 0;
3573 }
3574
3575 static struct drm_i915_fence_reg *
3576 i915_find_fence_reg(struct drm_device *dev)
3577 {
3578 struct drm_i915_private *dev_priv = dev->dev_private;
3579 struct drm_i915_fence_reg *reg, *avail;
3580 int i;
3581
3582 /* First try to find a free reg */
3583 avail = NULL;
3584 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
3585 reg = &dev_priv->fence_regs[i];
3586 if (!reg->obj)
3587 return reg;
3588
3589 if (!reg->pin_count)
3590 avail = reg;
3591 }
3592
3593 if (avail == NULL)
3594 goto deadlock;
3595
3596 /* None available, try to steal one or wait for a user to finish */
3597 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
3598 if (reg->pin_count)
3599 continue;
3600
3601 return reg;
3602 }
3603
3604 deadlock:
3605 /* Wait for completion of pending flips which consume fences */
3606 if (intel_has_pending_fb_unpin(dev))
3607 return ERR_PTR(-EAGAIN);
3608
3609 return ERR_PTR(-EDEADLK);
3610 }
3611
3612 /**
3613 * i915_gem_object_get_fence - set up fencing for an object
3614 * @obj: object to map through a fence reg
3615 *
3616 * When mapping objects through the GTT, userspace wants to be able to write
3617 * to them without having to worry about swizzling if the object is tiled.
3618 * This function walks the fence regs looking for a free one for @obj,
3619 * stealing one if it can't find any.
3620 *
3621 * It then sets up the reg based on the object's properties: address, pitch
3622 * and tiling format.
3623 *
3624 * For an untiled surface, this removes any existing fence.
3625 */
3626 int
3627 i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
3628 {
3629 struct drm_device *dev = obj->base.dev;
3630 struct drm_i915_private *dev_priv = dev->dev_private;
3631 bool enable = obj->tiling_mode != I915_TILING_NONE;
3632 struct drm_i915_fence_reg *reg;
3633 int ret;
3634
3635 /* Have we updated the tiling parameters upon the object and so
3636 * will need to serialise the write to the associated fence register?
3637 */
3638 if (obj->fence_dirty) {
3639 ret = i915_gem_object_wait_fence(obj);
3640 if (ret)
3641 return ret;
3642 }
3643
3644 /* Just update our place in the LRU if our fence is getting reused. */
3645 if (obj->fence_reg != I915_FENCE_REG_NONE) {
3646 reg = &dev_priv->fence_regs[obj->fence_reg];
3647 if (!obj->fence_dirty) {
3648 list_move_tail(&reg->lru_list,
3649 &dev_priv->mm.fence_list);
3650 return 0;
3651 }
3652 } else if (enable) {
3653 if (WARN_ON(!obj->map_and_fenceable))
3654 return -EINVAL;
3655
3656 reg = i915_find_fence_reg(dev);
3657 if (IS_ERR(reg))
3658 return PTR_ERR(reg);
3659
3660 if (reg->obj) {
3661 struct drm_i915_gem_object *old = reg->obj;
3662
3663 ret = i915_gem_object_wait_fence(old);
3664 if (ret)
3665 return ret;
3666
3667 i915_gem_object_fence_lost(old);
3668 }
3669 } else
3670 return 0;
3671
3672 i915_gem_object_update_fence(obj, reg, enable);
3673
3674 return 0;
3675 }
3676
3677 static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
3678 unsigned long cache_level)
3679 {
3680 struct drm_mm_node *gtt_space = &vma->node;
3681 struct drm_mm_node *other;
3682
3683 /*
3684 * On some machines we have to be careful when putting differing types
3685 * of snoopable memory together to avoid the prefetcher crossing memory
3686 * domains and dying. During vm initialisation, we decide whether or not
3687 * these constraints apply and set the drm_mm.color_adjust
3688 * appropriately.
3689 */
3690 if (vma->vm->mm.color_adjust == NULL)
3691 return true;
3692
3693 if (!drm_mm_node_allocated(gtt_space))
3694 return true;
3695
3696 if (list_empty(&gtt_space->node_list))
3697 return true;
3698
3699 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
3700 if (other->allocated && !other->hole_follows && other->color != cache_level)
3701 return false;
3702
3703 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
3704 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
3705 return false;
3706
3707 return true;
3708 }
3709
3710 /**
3711 * Finds free space in the GTT aperture and binds the object or a view of it
3712 * there.
3713 */
3714 static struct i915_vma *
3715 i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
3716 struct i915_address_space *vm,
3717 const struct i915_ggtt_view *ggtt_view,
3718 unsigned alignment,
3719 uint64_t flags)
3720 {
3721 struct drm_device *dev = obj->base.dev;
3722 struct drm_i915_private *dev_priv = dev->dev_private;
3723 u32 size, fence_size, fence_alignment, unfenced_alignment;
3724 u64 start =
3725 flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
3726 u64 end =
3727 flags & PIN_MAPPABLE ? dev_priv->gtt.mappable_end : vm->total;
3728 struct i915_vma *vma;
3729 int ret;
3730
3731 if (i915_is_ggtt(vm)) {
3732 u32 view_size;
3733
3734 if (WARN_ON(!ggtt_view))
3735 return ERR_PTR(-EINVAL);
3736
3737 view_size = i915_ggtt_view_size(obj, ggtt_view);
3738
3739 fence_size = i915_gem_get_gtt_size(dev,
3740 view_size,
3741 obj->tiling_mode);
3742 fence_alignment = i915_gem_get_gtt_alignment(dev,
3743 view_size,
3744 obj->tiling_mode,
3745 true);
3746 unfenced_alignment = i915_gem_get_gtt_alignment(dev,
3747 view_size,
3748 obj->tiling_mode,
3749 false);
3750 size = flags & PIN_MAPPABLE ? fence_size : view_size;
3751 } else {
3752 fence_size = i915_gem_get_gtt_size(dev,
3753 obj->base.size,
3754 obj->tiling_mode);
3755 fence_alignment = i915_gem_get_gtt_alignment(dev,
3756 obj->base.size,
3757 obj->tiling_mode,
3758 true);
3759 unfenced_alignment =
3760 i915_gem_get_gtt_alignment(dev,
3761 obj->base.size,
3762 obj->tiling_mode,
3763 false);
3764 size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
3765 }
3766
3767 if (alignment == 0)
3768 alignment = flags & PIN_MAPPABLE ? fence_alignment :
3769 unfenced_alignment;
3770 if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
3771 DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
3772 ggtt_view ? ggtt_view->type : 0,
3773 alignment);
3774 return ERR_PTR(-EINVAL);
3775 }
3776
3777 /* If binding the object/GGTT view requires more space than the entire
3778 * aperture has, reject it early before evicting everything in a vain
3779 * attempt to find space.
3780 */
3781 if (size > end) {
3782 DRM_DEBUG("Attempting to bind an object (view type=%u) larger than the aperture: size=%u > %s aperture=%llu\n",
3783 ggtt_view ? ggtt_view->type : 0,
3784 size,
3785 flags & PIN_MAPPABLE ? "mappable" : "total",
3786 end);
3787 return ERR_PTR(-E2BIG);
3788 }
3789
3790 ret = i915_gem_object_get_pages(obj);
3791 if (ret)
3792 return ERR_PTR(ret);
3793
3794 i915_gem_object_pin_pages(obj);
3795
3796 vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
3797 i915_gem_obj_lookup_or_create_vma(obj, vm);
3798
3799 if (IS_ERR(vma))
3800 goto err_unpin;
3801
3802 search_free:
3803 ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
3804 size, alignment,
3805 obj->cache_level,
3806 start, end,
3807 DRM_MM_SEARCH_DEFAULT,
3808 DRM_MM_CREATE_DEFAULT);
3809 if (ret) {
3810 ret = i915_gem_evict_something(dev, vm, size, alignment,
3811 obj->cache_level,
3812 start, end,
3813 flags);
3814 if (ret == 0)
3815 goto search_free;
3816
3817 goto err_free_vma;
3818 }
3819 if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
3820 ret = -EINVAL;
3821 goto err_remove_node;
3822 }
3823
3824 ret = i915_gem_gtt_prepare_object(obj);
3825 if (ret)
3826 goto err_remove_node;
3827
3828 trace_i915_vma_bind(vma, flags);
3829 ret = i915_vma_bind(vma, obj->cache_level, flags);
3830 if (ret)
3831 goto err_finish_gtt;
3832
3833 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
3834 list_add_tail(&vma->mm_list, &vm->inactive_list);
3835
3836 return vma;
3837
3838 err_finish_gtt:
3839 i915_gem_gtt_finish_object(obj);
3840 err_remove_node:
3841 drm_mm_remove_node(&vma->node);
3842 err_free_vma:
3843 i915_gem_vma_destroy(vma);
3844 vma = ERR_PTR(ret);
3845 err_unpin:
3846 i915_gem_object_unpin_pages(obj);
3847 return vma;
3848 }
3849
3850 bool
3851 i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3852 bool force)
3853 {
3854 /* If we don't have a page list set up, then we're not pinned
3855 * to GPU, and we can ignore the cache flush because it'll happen
3856 * again at bind time.
3857 */
3858 if (obj->pages == NULL)
3859 return false;
3860
3861 /*
3862 * Stolen memory is always coherent with the GPU as it is explicitly
3863 * marked as wc by the system, or the system is cache-coherent.
3864 */
3865 if (obj->stolen || obj->phys_handle)
3866 return false;
3867
3868 /* If the GPU is snooping the contents of the CPU cache,
3869 * we do not need to manually clear the CPU cache lines. However,
3870 * the caches are only snooped when the render cache is
3871 * flushed/invalidated. As we always have to emit invalidations
3872 * and flushes when moving into and out of the RENDER domain, correct
3873 * snooping behaviour occurs naturally as the result of our domain
3874 * tracking.
3875 */
3876 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
3877 obj->cache_dirty = true;
3878 return false;
3879 }
3880
3881 trace_i915_gem_object_clflush(obj);
3882 drm_clflush_sg(obj->pages);
3883 obj->cache_dirty = false;
3884
3885 return true;
3886 }
3887
3888 /** Flushes the GTT write domain for the object if it's dirty. */
3889 static void
3890 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
3891 {
3892 uint32_t old_write_domain;
3893
3894 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
3895 return;
3896
3897 /* No actual flushing is required for the GTT write domain. Writes
3898 * to it immediately go to main memory as far as we know, so there's
3899 * no chipset flush. It also doesn't land in render cache.
3900 *
3901 * However, we do have to enforce the order so that all writes through
3902 * the GTT land before any writes to the device, such as updates to
3903 * the GATT itself.
3904 */
3905 wmb();
3906
3907 old_write_domain = obj->base.write_domain;
3908 obj->base.write_domain = 0;
3909
3910 intel_fb_obj_flush(obj, false);
3911
3912 trace_i915_gem_object_change_domain(obj,
3913 obj->base.read_domains,
3914 old_write_domain);
3915 }
3916
3917 /** Flushes the CPU write domain for the object if it's dirty. */
3918 static void
3919 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
3920 {
3921 uint32_t old_write_domain;
3922
3923 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
3924 return;
3925
3926 if (i915_gem_clflush_object(obj, obj->pin_display))
3927 i915_gem_chipset_flush(obj->base.dev);
3928
3929 old_write_domain = obj->base.write_domain;
3930 obj->base.write_domain = 0;
3931
3932 intel_fb_obj_flush(obj, false);
3933
3934 trace_i915_gem_object_change_domain(obj,
3935 obj->base.read_domains,
3936 old_write_domain);
3937 }
3938
3939 /**
3940 * Moves a single object to the GTT read, and possibly write domain.
3941 *
3942 * This function returns when the move is complete, including waiting on
3943 * flushes to occur.
3944 */
3945 int
3946 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
3947 {
3948 uint32_t old_write_domain, old_read_domains;
3949 struct i915_vma *vma;
3950 int ret;
3951
3952 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3953 return 0;
3954
3955 ret = i915_gem_object_wait_rendering(obj, !write);
3956 if (ret)
3957 return ret;
3958
3959 /* Flush and acquire obj->pages so that we are coherent through
3960 * direct access in memory with previous cached writes through
3961 * shmemfs and that our cache domain tracking remains valid.
3962 * For example, if the obj->filp was moved to swap without us
3963 * being notified and releasing the pages, we would mistakenly
3964 * continue to assume that the obj remained out of the CPU cached
3965 * domain.
3966 */
3967 ret = i915_gem_object_get_pages(obj);
3968 if (ret)
3969 return ret;
3970
3971 i915_gem_object_flush_cpu_write_domain(obj);
3972
3973 /* Serialise direct access to this object with the barriers for
3974 * coherent writes from the GPU, by effectively invalidating the
3975 * GTT domain upon first access.
3976 */
3977 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3978 mb();
3979
3980 old_write_domain = obj->base.write_domain;
3981 old_read_domains = obj->base.read_domains;
3982
3983 /* It should now be out of any other write domains, and we can update
3984 * the domain values for our changes.
3985 */
3986 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3987 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3988 if (write) {
3989 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3990 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3991 obj->dirty = 1;
3992 }
3993
3994 trace_i915_gem_object_change_domain(obj,
3995 old_read_domains,
3996 old_write_domain);
3997
3998 /* And bump the LRU for this access */
3999 vma = i915_gem_obj_to_ggtt(obj);
4000 if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
4001 list_move_tail(&vma->mm_list,
4002 &to_i915(obj->base.dev)->gtt.base.inactive_list);
4003
4004 return 0;
4005 }
4006
4007 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
4008 enum i915_cache_level cache_level)
4009 {
4010 struct drm_device *dev = obj->base.dev;
4011 struct i915_vma *vma, *next;
4012 int ret;
4013
4014 if (obj->cache_level == cache_level)
4015 return 0;
4016
4017 if (i915_gem_obj_is_pinned(obj)) {
4018 DRM_DEBUG("can not change the cache level of pinned objects\n");
4019 return -EBUSY;
4020 }
4021
4022 list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
4023 if (!i915_gem_valid_gtt_space(vma, cache_level)) {
4024 ret = i915_vma_unbind(vma);
4025 if (ret)
4026 return ret;
4027 }
4028 }
4029
4030 if (i915_gem_obj_bound_any(obj)) {
4031 ret = i915_gem_object_wait_rendering(obj, false);
4032 if (ret)
4033 return ret;
4034
4035 i915_gem_object_finish_gtt(obj);
4036
4037 /* Before SandyBridge, you could not use tiling or fence
4038 * registers with snooped memory, so relinquish any fences
4039 * currently pointing to our region in the aperture.
4040 */
4041 if (INTEL_INFO(dev)->gen < 6) {
4042 ret = i915_gem_object_put_fence(obj);
4043 if (ret)
4044 return ret;
4045 }
4046
4047 list_for_each_entry(vma, &obj->vma_list, vma_link)
4048 if (drm_mm_node_allocated(&vma->node)) {
4049 ret = i915_vma_bind(vma, cache_level,
4050 PIN_UPDATE);
4051 if (ret)
4052 return ret;
4053 }
4054 }
4055
4056 list_for_each_entry(vma, &obj->vma_list, vma_link)
4057 vma->node.color = cache_level;
4058 obj->cache_level = cache_level;
4059
4060 if (obj->cache_dirty &&
4061 obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
4062 cpu_write_needs_clflush(obj)) {
4063 if (i915_gem_clflush_object(obj, true))
4064 i915_gem_chipset_flush(obj->base.dev);
4065 }
4066
4067 return 0;
4068 }
4069
4070 int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
4071 struct drm_file *file)
4072 {
4073 struct drm_i915_gem_caching *args = data;
4074 struct drm_i915_gem_object *obj;
4075
4076 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
4077 if (&obj->base == NULL)
4078 return -ENOENT;
4079
4080 switch (obj->cache_level) {
4081 case I915_CACHE_LLC:
4082 case I915_CACHE_L3_LLC:
4083 args->caching = I915_CACHING_CACHED;
4084 break;
4085
4086 case I915_CACHE_WT:
4087 args->caching = I915_CACHING_DISPLAY;
4088 break;
4089
4090 default:
4091 args->caching = I915_CACHING_NONE;
4092 break;
4093 }
4094
4095 drm_gem_object_unreference_unlocked(&obj->base);
4096 return 0;
4097 }
4098
4099 int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4100 struct drm_file *file)
4101 {
4102 struct drm_i915_gem_caching *args = data;
4103 struct drm_i915_gem_object *obj;
4104 enum i915_cache_level level;
4105 int ret;
4106
4107 switch (args->caching) {
4108 case I915_CACHING_NONE:
4109 level = I915_CACHE_NONE;
4110 break;
4111 case I915_CACHING_CACHED:
4112 level = I915_CACHE_LLC;
4113 break;
4114 case I915_CACHING_DISPLAY:
4115 level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
4116 break;
4117 default:
4118 return -EINVAL;
4119 }
4120
4121 ret = i915_mutex_lock_interruptible(dev);
4122 if (ret)
4123 return ret;
4124
4125 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
4126 if (&obj->base == NULL) {
4127 ret = -ENOENT;
4128 goto unlock;
4129 }
4130
4131 ret = i915_gem_object_set_cache_level(obj, level);
4132
4133 drm_gem_object_unreference(&obj->base);
4134 unlock:
4135 mutex_unlock(&dev->struct_mutex);
4136 return ret;
4137 }
4138
4139 /*
4140 * Prepare buffer for display plane (scanout, cursors, etc).
4141 * Can be called from an uninterruptible phase (modesetting) and allows
4142 * any flushes to be pipelined (for pageflips).
4143 */
4144 int
4145 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4146 u32 alignment,
4147 struct intel_engine_cs *pipelined,
4148 struct drm_i915_gem_request **pipelined_request,
4149 const struct i915_ggtt_view *view)
4150 {
4151 u32 old_read_domains, old_write_domain;
4152 int ret;
4153
4154 ret = i915_gem_object_sync(obj, pipelined, pipelined_request);
4155 if (ret)
4156 return ret;
4157
4158 /* Mark the pin_display early so that we account for the
4159 * display coherency whilst setting up the cache domains.
4160 */
4161 obj->pin_display++;
4162
4163 /* The display engine is not coherent with the LLC cache on gen6. As
4164 * a result, we make sure that the pinning that is about to occur is
4165 * done with uncached PTEs. This is lowest common denominator for all
4166 * chipsets.
4167 *
4168 * However for gen6+, we could do better by using the GFDT bit instead
4169 * of uncaching, which would allow us to flush all the LLC-cached data
4170 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4171 */
4172 ret = i915_gem_object_set_cache_level(obj,
4173 HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
4174 if (ret)
4175 goto err_unpin_display;
4176
4177 /* As the user may map the buffer once pinned in the display plane
4178 * (e.g. libkms for the bootup splash), we have to ensure that we
4179 * always use map_and_fenceable for all scanout buffers.
4180 */
4181 ret = i915_gem_object_ggtt_pin(obj, view, alignment,
4182 view->type == I915_GGTT_VIEW_NORMAL ?
4183 PIN_MAPPABLE : 0);
4184 if (ret)
4185 goto err_unpin_display;
4186
4187 i915_gem_object_flush_cpu_write_domain(obj);
4188
4189 old_write_domain = obj->base.write_domain;
4190 old_read_domains = obj->base.read_domains;
4191
4192 /* It should now be out of any other write domains, and we can update
4193 * the domain values for our changes.
4194 */
4195 obj->base.write_domain = 0;
4196 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
4197
4198 trace_i915_gem_object_change_domain(obj,
4199 old_read_domains,
4200 old_write_domain);
4201
4202 return 0;
4203
4204 err_unpin_display:
4205 obj->pin_display--;
4206 return ret;
4207 }
4208
4209 void
4210 i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
4211 const struct i915_ggtt_view *view)
4212 {
4213 if (WARN_ON(obj->pin_display == 0))
4214 return;
4215
4216 i915_gem_object_ggtt_unpin_view(obj, view);
4217
4218 obj->pin_display--;
4219 }
4220
4221 /**
4222 * Moves a single object to the CPU read, and possibly write domain.
4223 *
4224 * This function returns when the move is complete, including waiting on
4225 * flushes to occur.
4226 */
4227 int
4228 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
4229 {
4230 uint32_t old_write_domain, old_read_domains;
4231 int ret;
4232
4233 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
4234 return 0;
4235
4236 ret = i915_gem_object_wait_rendering(obj, !write);
4237 if (ret)
4238 return ret;
4239
4240 i915_gem_object_flush_gtt_write_domain(obj);
4241
4242 old_write_domain = obj->base.write_domain;
4243 old_read_domains = obj->base.read_domains;
4244
4245 /* Flush the CPU cache if it's still invalid. */
4246 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
4247 i915_gem_clflush_object(obj, false);
4248
4249 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
4250 }
4251
4252 /* It should now be out of any other write domains, and we can update
4253 * the domain values for our changes.
4254 */
4255 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
4256
4257 /* If we're writing through the CPU, then the GPU read domains will
4258 * need to be invalidated at next use.
4259 */
4260 if (write) {
4261 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4262 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4263 }
4264
4265 trace_i915_gem_object_change_domain(obj,
4266 old_read_domains,
4267 old_write_domain);
4268
4269 return 0;
4270 }
4271
4272 /* Throttle our rendering by waiting until the ring has completed our requests
4273 * emitted over 20 msec ago.
4274 *
4275 * Note that if we were to use the current jiffies each time around the loop,
4276 * we wouldn't escape the function with any frames outstanding if the time to
4277 * render a frame was over 20ms.
4278 *
4279 * This should get us reasonable parallelism between CPU and GPU but also
4280 * relatively low latency when blocking on a particular request to finish.
4281 */
4282 static int
4283 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
4284 {
4285 struct drm_i915_private *dev_priv = dev->dev_private;
4286 struct drm_i915_file_private *file_priv = file->driver_priv;
4287 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
4288 struct drm_i915_gem_request *request, *target = NULL;
4289 unsigned reset_counter;
4290 int ret;
4291
4292 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
4293 if (ret)
4294 return ret;
4295
4296 ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
4297 if (ret)
4298 return ret;
4299
4300 spin_lock(&file_priv->mm.lock);
4301 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
4302 if (time_after_eq(request->emitted_jiffies, recent_enough))
4303 break;
4304
4305 /*
4306 * Note that the request might not have been submitted yet.
4307 * In which case emitted_jiffies will be zero.
4308 */
4309 if (!request->emitted_jiffies)
4310 continue;
4311
4312 target = request;
4313 }
4314 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
4315 if (target)
4316 i915_gem_request_reference(target);
4317 spin_unlock(&file_priv->mm.lock);
4318
4319 if (target == NULL)
4320 return 0;
4321
4322 ret = __i915_wait_request(target, reset_counter, true, NULL, NULL);
4323 if (ret == 0)
4324 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
4325
4326 i915_gem_request_unreference__unlocked(target);
4327
4328 return ret;
4329 }
4330
4331 static bool
4332 i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
4333 {
4334 struct drm_i915_gem_object *obj = vma->obj;
4335
4336 if (alignment &&
4337 vma->node.start & (alignment - 1))
4338 return true;
4339
4340 if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
4341 return true;
4342
4343 if (flags & PIN_OFFSET_BIAS &&
4344 vma->node.start < (flags & PIN_OFFSET_MASK))
4345 return true;
4346
4347 return false;
4348 }
4349
4350 static int
4351 i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
4352 struct i915_address_space *vm,
4353 const struct i915_ggtt_view *ggtt_view,
4354 uint32_t alignment,
4355 uint64_t flags)
4356 {
4357 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
4358 struct i915_vma *vma;
4359 unsigned bound;
4360 int ret;
4361
4362 if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
4363 return -ENODEV;
4364
4365 if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
4366 return -EINVAL;
4367
4368 if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
4369 return -EINVAL;
4370
4371 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
4372 return -EINVAL;
4373
4374 vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
4375 i915_gem_obj_to_vma(obj, vm);
4376
4377 if (IS_ERR(vma))
4378 return PTR_ERR(vma);
4379
4380 if (vma) {
4381 if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
4382 return -EBUSY;
4383
4384 if (i915_vma_misplaced(vma, alignment, flags)) {
4385 unsigned long offset;
4386 offset = ggtt_view ? i915_gem_obj_ggtt_offset_view(obj, ggtt_view) :
4387 i915_gem_obj_offset(obj, vm);
4388 WARN(vma->pin_count,
4389 "bo is already pinned in %s with incorrect alignment:"
4390 " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
4391 " obj->map_and_fenceable=%d\n",
4392 ggtt_view ? "ggtt" : "ppgtt",
4393 offset,
4394 alignment,
4395 !!(flags & PIN_MAPPABLE),
4396 obj->map_and_fenceable);
4397 ret = i915_vma_unbind(vma);
4398 if (ret)
4399 return ret;
4400
4401 vma = NULL;
4402 }
4403 }
4404
4405 bound = vma ? vma->bound : 0;
4406 if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
4407 vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
4408 flags);
4409 if (IS_ERR(vma))
4410 return PTR_ERR(vma);
4411 } else {
4412 ret = i915_vma_bind(vma, obj->cache_level, flags);
4413 if (ret)
4414 return ret;
4415 }
4416
4417 if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
4418 (bound ^ vma->bound) & GLOBAL_BIND) {
4419 bool mappable, fenceable;
4420 u32 fence_size, fence_alignment;
4421
4422 fence_size = i915_gem_get_gtt_size(obj->base.dev,
4423 obj->base.size,
4424 obj->tiling_mode);
4425 fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
4426 obj->base.size,
4427 obj->tiling_mode,
4428 true);
4429
4430 fenceable = (vma->node.size == fence_size &&
4431 (vma->node.start & (fence_alignment - 1)) == 0);
4432
4433 mappable = (vma->node.start + fence_size <=
4434 dev_priv->gtt.mappable_end);
4435
4436 obj->map_and_fenceable = mappable && fenceable;
4437
4438 WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
4439 }
4440
4441 vma->pin_count++;
4442 return 0;
4443 }
4444
4445 int
4446 i915_gem_object_pin(struct drm_i915_gem_object *obj,
4447 struct i915_address_space *vm,
4448 uint32_t alignment,
4449 uint64_t flags)
4450 {
4451 return i915_gem_object_do_pin(obj, vm,
4452 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
4453 alignment, flags);
4454 }
4455
4456 int
4457 i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4458 const struct i915_ggtt_view *view,
4459 uint32_t alignment,
4460 uint64_t flags)
4461 {
4462 if (WARN_ONCE(!view, "no view specified"))
4463 return -EINVAL;
4464
4465 return i915_gem_object_do_pin(obj, i915_obj_to_ggtt(obj), view,
4466 alignment, flags | PIN_GLOBAL);
4467 }
4468
4469 void
4470 i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
4471 const struct i915_ggtt_view *view)
4472 {
4473 struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
4474
4475 BUG_ON(!vma);
4476 WARN_ON(vma->pin_count == 0);
4477 WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
4478
4479 --vma->pin_count;
4480 }
4481
4482 bool
4483 i915_gem_object_pin_fence(struct drm_i915_gem_object *obj)
4484 {
4485 if (obj->fence_reg != I915_FENCE_REG_NONE) {
4486 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
4487 struct i915_vma *ggtt_vma = i915_gem_obj_to_ggtt(obj);
4488
4489 WARN_ON(!ggtt_vma ||
4490 dev_priv->fence_regs[obj->fence_reg].pin_count >
4491 ggtt_vma->pin_count);
4492 dev_priv->fence_regs[obj->fence_reg].pin_count++;
4493 return true;
4494 } else
4495 return false;
4496 }
4497
4498 void
4499 i915_gem_object_unpin_fence(struct drm_i915_gem_object *obj)
4500 {
4501 if (obj->fence_reg != I915_FENCE_REG_NONE) {
4502 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
4503 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count <= 0);
4504 dev_priv->fence_regs[obj->fence_reg].pin_count--;
4505 }
4506 }
4507
4508 int
4509 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4510 struct drm_file *file)
4511 {
4512 struct drm_i915_gem_busy *args = data;
4513 struct drm_i915_gem_object *obj;
4514 int ret;
4515
4516 ret = i915_mutex_lock_interruptible(dev);
4517 if (ret)
4518 return ret;
4519
4520 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
4521 if (&obj->base == NULL) {
4522 ret = -ENOENT;
4523 goto unlock;
4524 }
4525
4526 /* Count all active objects as busy, even if they are currently not used
4527 * by the gpu. Users of this interface expect objects to eventually
4528 * become non-busy without any further actions, therefore emit any
4529 * necessary flushes here.
4530 */
4531 ret = i915_gem_object_flush_active(obj);
4532 if (ret)
4533 goto unref;
4534
4535 BUILD_BUG_ON(I915_NUM_RINGS > 16);
4536 args->busy = obj->active << 16;
4537 if (obj->last_write_req)
4538 args->busy |= obj->last_write_req->ring->id;
4539
4540 unref:
4541 drm_gem_object_unreference(&obj->base);
4542 unlock:
4543 mutex_unlock(&dev->struct_mutex);
4544 return ret;
4545 }
4546
4547 int
4548 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4549 struct drm_file *file_priv)
4550 {
4551 return i915_gem_ring_throttle(dev, file_priv);
4552 }
4553
4554 int
4555 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4556 struct drm_file *file_priv)
4557 {
4558 struct drm_i915_private *dev_priv = dev->dev_private;
4559 struct drm_i915_gem_madvise *args = data;
4560 struct drm_i915_gem_object *obj;
4561 int ret;
4562
4563 switch (args->madv) {
4564 case I915_MADV_DONTNEED:
4565 case I915_MADV_WILLNEED:
4566 break;
4567 default:
4568 return -EINVAL;
4569 }
4570
4571 ret = i915_mutex_lock_interruptible(dev);
4572 if (ret)
4573 return ret;
4574
4575 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
4576 if (&obj->base == NULL) {
4577 ret = -ENOENT;
4578 goto unlock;
4579 }
4580
4581 if (i915_gem_obj_is_pinned(obj)) {
4582 ret = -EINVAL;
4583 goto out;
4584 }
4585
4586 if (obj->pages &&
4587 obj->tiling_mode != I915_TILING_NONE &&
4588 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
4589 if (obj->madv == I915_MADV_WILLNEED)
4590 i915_gem_object_unpin_pages(obj);
4591 if (args->madv == I915_MADV_WILLNEED)
4592 i915_gem_object_pin_pages(obj);
4593 }
4594
4595 if (obj->madv != __I915_MADV_PURGED)
4596 obj->madv = args->madv;
4597
4598 /* if the object is no longer attached, discard its backing storage */
4599 if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
4600 i915_gem_object_truncate(obj);
4601
4602 args->retained = obj->madv != __I915_MADV_PURGED;
4603
4604 out:
4605 drm_gem_object_unreference(&obj->base);
4606 unlock:
4607 mutex_unlock(&dev->struct_mutex);
4608 return ret;
4609 }
4610
4611 void i915_gem_object_init(struct drm_i915_gem_object *obj,
4612 const struct drm_i915_gem_object_ops *ops)
4613 {
4614 int i;
4615
4616 INIT_LIST_HEAD(&obj->global_list);
4617 for (i = 0; i < I915_NUM_RINGS; i++)
4618 INIT_LIST_HEAD(&obj->ring_list[i]);
4619 INIT_LIST_HEAD(&obj->obj_exec_link);
4620 INIT_LIST_HEAD(&obj->vma_list);
4621 INIT_LIST_HEAD(&obj->batch_pool_link);
4622
4623 obj->ops = ops;
4624
4625 obj->fence_reg = I915_FENCE_REG_NONE;
4626 obj->madv = I915_MADV_WILLNEED;
4627
4628 i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
4629 }
4630
4631 static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
4632 .get_pages = i915_gem_object_get_pages_gtt,
4633 .put_pages = i915_gem_object_put_pages_gtt,
4634 };
4635
4636 struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
4637 size_t size)
4638 {
4639 struct drm_i915_gem_object *obj;
4640 struct address_space *mapping;
4641 gfp_t mask;
4642
4643 obj = i915_gem_object_alloc(dev);
4644 if (obj == NULL)
4645 return NULL;
4646
4647 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4648 i915_gem_object_free(obj);
4649 return NULL;
4650 }
4651
4652 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
4653 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
4654 /* 965gm cannot relocate objects above 4GiB. */
4655 mask &= ~__GFP_HIGHMEM;
4656 mask |= __GFP_DMA32;
4657 }
4658
4659 mapping = file_inode(obj->base.filp)->i_mapping;
4660 mapping_set_gfp_mask(mapping, mask);
4661
4662 i915_gem_object_init(obj, &i915_gem_object_ops);
4663
4664 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4665 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4666
4667 if (HAS_LLC(dev)) {
4668 /* On some devices, we can have the GPU use the LLC (the CPU
4669 * cache) for about a 10% performance improvement
4670 * compared to uncached. Graphics requests other than
4671 * display scanout are coherent with the CPU in
4672 * accessing this cache. This means in this mode we
4673 * don't need to clflush on the CPU side, and on the
4674 * GPU side we only need to flush internal caches to
4675 * get data visible to the CPU.
4676 *
4677 * However, we maintain the display planes as UC, and so
4678 * need to rebind when first used as such.
4679 */
4680 obj->cache_level = I915_CACHE_LLC;
4681 } else
4682 obj->cache_level = I915_CACHE_NONE;
4683
4684 trace_i915_gem_object_create(obj);
4685
4686 return obj;
4687 }
4688
4689 static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4690 {
4691 /* If we are the last user of the backing storage (be it shmemfs
4692 * pages or stolen etc), we know that the pages are going to be
4693 * immediately released. In this case, we can then skip copying
4694 * back the contents from the GPU.
4695 */
4696
4697 if (obj->madv != I915_MADV_WILLNEED)
4698 return false;
4699
4700 if (obj->base.filp == NULL)
4701 return true;
4702
4703 /* At first glance, this looks racy, but then again so would be
4704 * userspace racing mmap against close. However, the first external
4705 * reference to the filp can only be obtained through the
4706 * i915_gem_mmap_ioctl() which safeguards us against the user
4707 * acquiring such a reference whilst we are in the middle of
4708 * freeing the object.
4709 */
4710 return atomic_long_read(&obj->base.filp->f_count) == 1;
4711 }
4712
4713 void i915_gem_free_object(struct drm_gem_object *gem_obj)
4714 {
4715 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4716 struct drm_device *dev = obj->base.dev;
4717 struct drm_i915_private *dev_priv = dev->dev_private;
4718 struct i915_vma *vma, *next;
4719
4720 intel_runtime_pm_get(dev_priv);
4721
4722 trace_i915_gem_object_destroy(obj);
4723
4724 list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
4725 int ret;
4726
4727 vma->pin_count = 0;
4728 ret = i915_vma_unbind(vma);
4729 if (WARN_ON(ret == -ERESTARTSYS)) {
4730 bool was_interruptible;
4731
4732 was_interruptible = dev_priv->mm.interruptible;
4733 dev_priv->mm.interruptible = false;
4734
4735 WARN_ON(i915_vma_unbind(vma));
4736
4737 dev_priv->mm.interruptible = was_interruptible;
4738 }
4739 }
4740
4741 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
4742 * before progressing. */
4743 if (obj->stolen)
4744 i915_gem_object_unpin_pages(obj);
4745
4746 WARN_ON(obj->frontbuffer_bits);
4747
4748 if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
4749 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
4750 obj->tiling_mode != I915_TILING_NONE)
4751 i915_gem_object_unpin_pages(obj);
4752
4753 if (WARN_ON(obj->pages_pin_count))
4754 obj->pages_pin_count = 0;
4755 if (discard_backing_storage(obj))
4756 obj->madv = I915_MADV_DONTNEED;
4757 i915_gem_object_put_pages(obj);
4758 i915_gem_object_free_mmap_offset(obj);
4759
4760 BUG_ON(obj->pages);
4761
4762 if (obj->base.import_attach)
4763 drm_prime_gem_destroy(&obj->base, NULL);
4764
4765 if (obj->ops->release)
4766 obj->ops->release(obj);
4767
4768 drm_gem_object_release(&obj->base);
4769 i915_gem_info_remove_obj(dev_priv, obj->base.size);
4770
4771 kfree(obj->bit_17);
4772 i915_gem_object_free(obj);
4773
4774 intel_runtime_pm_put(dev_priv);
4775 }
4776
4777 struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
4778 struct i915_address_space *vm)
4779 {
4780 struct i915_vma *vma;
4781 list_for_each_entry(vma, &obj->vma_list, vma_link) {
4782 if (i915_is_ggtt(vma->vm) &&
4783 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
4784 continue;
4785 if (vma->vm == vm)
4786 return vma;
4787 }
4788 return NULL;
4789 }
4790
4791 struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
4792 const struct i915_ggtt_view *view)
4793 {
4794 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
4795 struct i915_vma *vma;
4796
4797 if (WARN_ONCE(!view, "no view specified"))
4798 return ERR_PTR(-EINVAL);
4799
4800 list_for_each_entry(vma, &obj->vma_list, vma_link)
4801 if (vma->vm == ggtt &&
4802 i915_ggtt_view_equal(&vma->ggtt_view, view))
4803 return vma;
4804 return NULL;
4805 }
4806
4807 void i915_gem_vma_destroy(struct i915_vma *vma)
4808 {
4809 struct i915_address_space *vm = NULL;
4810 WARN_ON(vma->node.allocated);
4811
4812 /* Keep the vma as a placeholder in the execbuffer reservation lists */
4813 if (!list_empty(&vma->exec_list))
4814 return;
4815
4816 vm = vma->vm;
4817
4818 if (!i915_is_ggtt(vm))
4819 i915_ppgtt_put(i915_vm_to_ppgtt(vm));
4820
4821 list_del(&vma->vma_link);
4822
4823 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
4824 }
4825
4826 static void
4827 i915_gem_stop_ringbuffers(struct drm_device *dev)
4828 {
4829 struct drm_i915_private *dev_priv = dev->dev_private;
4830 struct intel_engine_cs *ring;
4831 int i;
4832
4833 for_each_ring(ring, dev_priv, i)
4834 dev_priv->gt.stop_ring(ring);
4835 }
4836
4837 int
4838 i915_gem_suspend(struct drm_device *dev)
4839 {
4840 struct drm_i915_private *dev_priv = dev->dev_private;
4841 int ret = 0;
4842
4843 mutex_lock(&dev->struct_mutex);
4844 ret = i915_gpu_idle(dev);
4845 if (ret)
4846 goto err;
4847
4848 i915_gem_retire_requests(dev);
4849
4850 i915_gem_stop_ringbuffers(dev);
4851 mutex_unlock(&dev->struct_mutex);
4852
4853 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
4854 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4855 flush_delayed_work(&dev_priv->mm.idle_work);
4856
4857 /* Assert that we sucessfully flushed all the work and
4858 * reset the GPU back to its idle, low power state.
4859 */
4860 WARN_ON(dev_priv->mm.busy);
4861
4862 return 0;
4863
4864 err:
4865 mutex_unlock(&dev->struct_mutex);
4866 return ret;
4867 }
4868
4869 int i915_gem_l3_remap(struct drm_i915_gem_request *req, int slice)
4870 {
4871 struct intel_engine_cs *ring = req->ring;
4872 struct drm_device *dev = ring->dev;
4873 struct drm_i915_private *dev_priv = dev->dev_private;
4874 u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200);
4875 u32 *remap_info = dev_priv->l3_parity.remap_info[slice];
4876 int i, ret;
4877
4878 if (!HAS_L3_DPF(dev) || !remap_info)
4879 return 0;
4880
4881 ret = intel_ring_begin(req, GEN7_L3LOG_SIZE / 4 * 3);
4882 if (ret)
4883 return ret;
4884
4885 /*
4886 * Note: We do not worry about the concurrent register cacheline hang
4887 * here because no other code should access these registers other than
4888 * at initialization time.
4889 */
4890 for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
4891 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
4892 intel_ring_emit(ring, reg_base + i);
4893 intel_ring_emit(ring, remap_info[i/4]);
4894 }
4895
4896 intel_ring_advance(ring);
4897
4898 return ret;
4899 }
4900
4901 void i915_gem_init_swizzling(struct drm_device *dev)
4902 {
4903 struct drm_i915_private *dev_priv = dev->dev_private;
4904
4905 if (INTEL_INFO(dev)->gen < 5 ||
4906 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4907 return;
4908
4909 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4910 DISP_TILE_SURFACE_SWIZZLING);
4911
4912 if (IS_GEN5(dev))
4913 return;
4914
4915 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
4916 if (IS_GEN6(dev))
4917 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
4918 else if (IS_GEN7(dev))
4919 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
4920 else if (IS_GEN8(dev))
4921 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
4922 else
4923 BUG();
4924 }
4925
4926 static bool
4927 intel_enable_blt(struct drm_device *dev)
4928 {
4929 if (!HAS_BLT(dev))
4930 return false;
4931
4932 /* The blitter was dysfunctional on early prototypes */
4933 if (IS_GEN6(dev) && dev->pdev->revision < 8) {
4934 DRM_INFO("BLT not supported on this pre-production hardware;"
4935 " graphics performance will be degraded.\n");
4936 return false;
4937 }
4938
4939 return true;
4940 }
4941
4942 static void init_unused_ring(struct drm_device *dev, u32 base)
4943 {
4944 struct drm_i915_private *dev_priv = dev->dev_private;
4945
4946 I915_WRITE(RING_CTL(base), 0);
4947 I915_WRITE(RING_HEAD(base), 0);
4948 I915_WRITE(RING_TAIL(base), 0);
4949 I915_WRITE(RING_START(base), 0);
4950 }
4951
4952 static void init_unused_rings(struct drm_device *dev)
4953 {
4954 if (IS_I830(dev)) {
4955 init_unused_ring(dev, PRB1_BASE);
4956 init_unused_ring(dev, SRB0_BASE);
4957 init_unused_ring(dev, SRB1_BASE);
4958 init_unused_ring(dev, SRB2_BASE);
4959 init_unused_ring(dev, SRB3_BASE);
4960 } else if (IS_GEN2(dev)) {
4961 init_unused_ring(dev, SRB0_BASE);
4962 init_unused_ring(dev, SRB1_BASE);
4963 } else if (IS_GEN3(dev)) {
4964 init_unused_ring(dev, PRB1_BASE);
4965 init_unused_ring(dev, PRB2_BASE);
4966 }
4967 }
4968
4969 int i915_gem_init_rings(struct drm_device *dev)
4970 {
4971 struct drm_i915_private *dev_priv = dev->dev_private;
4972 int ret;
4973
4974 ret = intel_init_render_ring_buffer(dev);
4975 if (ret)
4976 return ret;
4977
4978 if (HAS_BSD(dev)) {
4979 ret = intel_init_bsd_ring_buffer(dev);
4980 if (ret)
4981 goto cleanup_render_ring;
4982 }
4983
4984 if (intel_enable_blt(dev)) {
4985 ret = intel_init_blt_ring_buffer(dev);
4986 if (ret)
4987 goto cleanup_bsd_ring;
4988 }
4989
4990 if (HAS_VEBOX(dev)) {
4991 ret = intel_init_vebox_ring_buffer(dev);
4992 if (ret)
4993 goto cleanup_blt_ring;
4994 }
4995
4996 if (HAS_BSD2(dev)) {
4997 ret = intel_init_bsd2_ring_buffer(dev);
4998 if (ret)
4999 goto cleanup_vebox_ring;
5000 }
5001
5002 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
5003 if (ret)
5004 goto cleanup_bsd2_ring;
5005
5006 return 0;
5007
5008 cleanup_bsd2_ring:
5009 intel_cleanup_ring_buffer(&dev_priv->ring[VCS2]);
5010 cleanup_vebox_ring:
5011 intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
5012 cleanup_blt_ring:
5013 intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
5014 cleanup_bsd_ring:
5015 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
5016 cleanup_render_ring:
5017 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
5018
5019 return ret;
5020 }
5021
5022 int
5023 i915_gem_init_hw(struct drm_device *dev)
5024 {
5025 struct drm_i915_private *dev_priv = dev->dev_private;
5026 struct intel_engine_cs *ring;
5027 int ret, i, j;
5028
5029 if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
5030 return -EIO;
5031
5032 /* Double layer security blanket, see i915_gem_init() */
5033 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5034
5035 if (dev_priv->ellc_size)
5036 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
5037
5038 if (IS_HASWELL(dev))
5039 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
5040 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
5041
5042 if (HAS_PCH_NOP(dev)) {
5043 if (IS_IVYBRIDGE(dev)) {
5044 u32 temp = I915_READ(GEN7_MSG_CTL);
5045 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5046 I915_WRITE(GEN7_MSG_CTL, temp);
5047 } else if (INTEL_INFO(dev)->gen >= 7) {
5048 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5049 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5050 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5051 }
5052 }
5053
5054 i915_gem_init_swizzling(dev);
5055
5056 /*
5057 * At least 830 can leave some of the unused rings
5058 * "active" (ie. head != tail) after resume which
5059 * will prevent c3 entry. Makes sure all unused rings
5060 * are totally idle.
5061 */
5062 init_unused_rings(dev);
5063
5064 BUG_ON(!dev_priv->ring[RCS].default_context);
5065
5066 ret = i915_ppgtt_init_hw(dev);
5067 if (ret) {
5068 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
5069 goto out;
5070 }
5071
5072 /* Need to do basic initialisation of all rings first: */
5073 for_each_ring(ring, dev_priv, i) {
5074 ret = ring->init_hw(ring);
5075 if (ret)
5076 goto out;
5077 }
5078
5079 /* Now it is safe to go back round and do everything else: */
5080 for_each_ring(ring, dev_priv, i) {
5081 struct drm_i915_gem_request *req;
5082
5083 WARN_ON(!ring->default_context);
5084
5085 ret = i915_gem_request_alloc(ring, ring->default_context, &req);
5086 if (ret) {
5087 i915_gem_cleanup_ringbuffer(dev);
5088 goto out;
5089 }
5090
5091 if (ring->id == RCS) {
5092 for (j = 0; j < NUM_L3_SLICES(dev); j++)
5093 i915_gem_l3_remap(req, j);
5094 }
5095
5096 ret = i915_ppgtt_init_ring(req);
5097 if (ret && ret != -EIO) {
5098 DRM_ERROR("PPGTT enable ring #%d failed %d\n", i, ret);
5099 i915_gem_request_cancel(req);
5100 i915_gem_cleanup_ringbuffer(dev);
5101 goto out;
5102 }
5103
5104 ret = i915_gem_context_enable(req);
5105 if (ret && ret != -EIO) {
5106 DRM_ERROR("Context enable ring #%d failed %d\n", i, ret);
5107 i915_gem_request_cancel(req);
5108 i915_gem_cleanup_ringbuffer(dev);
5109 goto out;
5110 }
5111
5112 i915_add_request_no_flush(req);
5113 }
5114
5115 out:
5116 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5117 return ret;
5118 }
5119
5120 int i915_gem_init(struct drm_device *dev)
5121 {
5122 struct drm_i915_private *dev_priv = dev->dev_private;
5123 int ret;
5124
5125 i915.enable_execlists = intel_sanitize_enable_execlists(dev,
5126 i915.enable_execlists);
5127
5128 mutex_lock(&dev->struct_mutex);
5129
5130 if (IS_VALLEYVIEW(dev)) {
5131 /* VLVA0 (potential hack), BIOS isn't actually waking us */
5132 I915_WRITE(VLV_GTLC_WAKE_CTRL, VLV_GTLC_ALLOWWAKEREQ);
5133 if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) &
5134 VLV_GTLC_ALLOWWAKEACK), 10))
5135 DRM_DEBUG_DRIVER("allow wake ack timed out\n");
5136 }
5137
5138 if (!i915.enable_execlists) {
5139 dev_priv->gt.execbuf_submit = i915_gem_ringbuffer_submission;
5140 dev_priv->gt.init_rings = i915_gem_init_rings;
5141 dev_priv->gt.cleanup_ring = intel_cleanup_ring_buffer;
5142 dev_priv->gt.stop_ring = intel_stop_ring_buffer;
5143 } else {
5144 dev_priv->gt.execbuf_submit = intel_execlists_submission;
5145 dev_priv->gt.init_rings = intel_logical_rings_init;
5146 dev_priv->gt.cleanup_ring = intel_logical_ring_cleanup;
5147 dev_priv->gt.stop_ring = intel_logical_ring_stop;
5148 }
5149
5150 /* This is just a security blanket to placate dragons.
5151 * On some systems, we very sporadically observe that the first TLBs
5152 * used by the CS may be stale, despite us poking the TLB reset. If
5153 * we hold the forcewake during initialisation these problems
5154 * just magically go away.
5155 */
5156 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5157
5158 ret = i915_gem_init_userptr(dev);
5159 if (ret)
5160 goto out_unlock;
5161
5162 i915_gem_init_global_gtt(dev);
5163
5164 ret = i915_gem_context_init(dev);
5165 if (ret)
5166 goto out_unlock;
5167
5168 ret = dev_priv->gt.init_rings(dev);
5169 if (ret)
5170 goto out_unlock;
5171
5172 ret = i915_gem_init_hw(dev);
5173 if (ret == -EIO) {
5174 /* Allow ring initialisation to fail by marking the GPU as
5175 * wedged. But we only want to do this where the GPU is angry,
5176 * for all other failure, such as an allocation failure, bail.
5177 */
5178 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5179 atomic_set_mask(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
5180 ret = 0;
5181 }
5182
5183 out_unlock:
5184 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5185 mutex_unlock(&dev->struct_mutex);
5186
5187 return ret;
5188 }
5189
5190 void
5191 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
5192 {
5193 struct drm_i915_private *dev_priv = dev->dev_private;
5194 struct intel_engine_cs *ring;
5195 int i;
5196
5197 for_each_ring(ring, dev_priv, i)
5198 dev_priv->gt.cleanup_ring(ring);
5199
5200 if (i915.enable_execlists)
5201 /*
5202 * Neither the BIOS, ourselves or any other kernel
5203 * expects the system to be in execlists mode on startup,
5204 * so we need to reset the GPU back to legacy mode.
5205 */
5206 intel_gpu_reset(dev);
5207 }
5208
5209 static void
5210 init_ring_lists(struct intel_engine_cs *ring)
5211 {
5212 INIT_LIST_HEAD(&ring->active_list);
5213 INIT_LIST_HEAD(&ring->request_list);
5214 }
5215
5216 void i915_init_vm(struct drm_i915_private *dev_priv,
5217 struct i915_address_space *vm)
5218 {
5219 if (!i915_is_ggtt(vm))
5220 drm_mm_init(&vm->mm, vm->start, vm->total);
5221 vm->dev = dev_priv->dev;
5222 INIT_LIST_HEAD(&vm->active_list);
5223 INIT_LIST_HEAD(&vm->inactive_list);
5224 INIT_LIST_HEAD(&vm->global_link);
5225 list_add_tail(&vm->global_link, &dev_priv->vm_list);
5226 }
5227
5228 void
5229 i915_gem_load(struct drm_device *dev)
5230 {
5231 struct drm_i915_private *dev_priv = dev->dev_private;
5232 int i;
5233
5234 dev_priv->objects =
5235 kmem_cache_create("i915_gem_object",
5236 sizeof(struct drm_i915_gem_object), 0,
5237 SLAB_HWCACHE_ALIGN,
5238 NULL);
5239 dev_priv->vmas =
5240 kmem_cache_create("i915_gem_vma",
5241 sizeof(struct i915_vma), 0,
5242 SLAB_HWCACHE_ALIGN,
5243 NULL);
5244 dev_priv->requests =
5245 kmem_cache_create("i915_gem_request",
5246 sizeof(struct drm_i915_gem_request), 0,
5247 SLAB_HWCACHE_ALIGN,
5248 NULL);
5249
5250 INIT_LIST_HEAD(&dev_priv->vm_list);
5251 i915_init_vm(dev_priv, &dev_priv->gtt.base);
5252
5253 INIT_LIST_HEAD(&dev_priv->context_list);
5254 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
5255 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
5256 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
5257 for (i = 0; i < I915_NUM_RINGS; i++)
5258 init_ring_lists(&dev_priv->ring[i]);
5259 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
5260 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
5261 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
5262 i915_gem_retire_work_handler);
5263 INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
5264 i915_gem_idle_work_handler);
5265 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
5266
5267 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
5268
5269 if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
5270 dev_priv->num_fence_regs = 32;
5271 else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
5272 dev_priv->num_fence_regs = 16;
5273 else
5274 dev_priv->num_fence_regs = 8;
5275
5276 if (intel_vgpu_active(dev))
5277 dev_priv->num_fence_regs =
5278 I915_READ(vgtif_reg(avail_rs.fence_num));
5279
5280 /* Initialize fence registers to zero */
5281 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
5282 i915_gem_restore_fences(dev);
5283
5284 i915_gem_detect_bit_6_swizzle(dev);
5285 init_waitqueue_head(&dev_priv->pending_flip_queue);
5286
5287 dev_priv->mm.interruptible = true;
5288
5289 i915_gem_shrinker_init(dev_priv);
5290
5291 mutex_init(&dev_priv->fb_tracking.lock);
5292 }
5293
5294 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
5295 {
5296 struct drm_i915_file_private *file_priv = file->driver_priv;
5297
5298 /* Clean up our request list when the client is going away, so that
5299 * later retire_requests won't dereference our soon-to-be-gone
5300 * file_priv.
5301 */
5302 spin_lock(&file_priv->mm.lock);
5303 while (!list_empty(&file_priv->mm.request_list)) {
5304 struct drm_i915_gem_request *request;
5305
5306 request = list_first_entry(&file_priv->mm.request_list,
5307 struct drm_i915_gem_request,
5308 client_list);
5309 list_del(&request->client_list);
5310 request->file_priv = NULL;
5311 }
5312 spin_unlock(&file_priv->mm.lock);
5313
5314 if (!list_empty(&file_priv->rps.link)) {
5315 spin_lock(&to_i915(dev)->rps.client_lock);
5316 list_del(&file_priv->rps.link);
5317 spin_unlock(&to_i915(dev)->rps.client_lock);
5318 }
5319 }
5320
5321 int i915_gem_open(struct drm_device *dev, struct drm_file *file)
5322 {
5323 struct drm_i915_file_private *file_priv;
5324 int ret;
5325
5326 DRM_DEBUG_DRIVER("\n");
5327
5328 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5329 if (!file_priv)
5330 return -ENOMEM;
5331
5332 file->driver_priv = file_priv;
5333 file_priv->dev_priv = dev->dev_private;
5334 file_priv->file = file;
5335 INIT_LIST_HEAD(&file_priv->rps.link);
5336
5337 spin_lock_init(&file_priv->mm.lock);
5338 INIT_LIST_HEAD(&file_priv->mm.request_list);
5339
5340 ret = i915_gem_context_open(dev, file);
5341 if (ret)
5342 kfree(file_priv);
5343
5344 return ret;
5345 }
5346
5347 /**
5348 * i915_gem_track_fb - update frontbuffer tracking
5349 * old: current GEM buffer for the frontbuffer slots
5350 * new: new GEM buffer for the frontbuffer slots
5351 * frontbuffer_bits: bitmask of frontbuffer slots
5352 *
5353 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5354 * from @old and setting them in @new. Both @old and @new can be NULL.
5355 */
5356 void i915_gem_track_fb(struct drm_i915_gem_object *old,
5357 struct drm_i915_gem_object *new,
5358 unsigned frontbuffer_bits)
5359 {
5360 if (old) {
5361 WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
5362 WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
5363 old->frontbuffer_bits &= ~frontbuffer_bits;
5364 }
5365
5366 if (new) {
5367 WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
5368 WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
5369 new->frontbuffer_bits |= frontbuffer_bits;
5370 }
5371 }
5372
5373 /* All the new VM stuff */
5374 unsigned long
5375 i915_gem_obj_offset(struct drm_i915_gem_object *o,
5376 struct i915_address_space *vm)
5377 {
5378 struct drm_i915_private *dev_priv = o->base.dev->dev_private;
5379 struct i915_vma *vma;
5380
5381 WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
5382
5383 list_for_each_entry(vma, &o->vma_list, vma_link) {
5384 if (i915_is_ggtt(vma->vm) &&
5385 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
5386 continue;
5387 if (vma->vm == vm)
5388 return vma->node.start;
5389 }
5390
5391 WARN(1, "%s vma for this object not found.\n",
5392 i915_is_ggtt(vm) ? "global" : "ppgtt");
5393 return -1;
5394 }
5395
5396 unsigned long
5397 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
5398 const struct i915_ggtt_view *view)
5399 {
5400 struct i915_address_space *ggtt = i915_obj_to_ggtt(o);
5401 struct i915_vma *vma;
5402
5403 list_for_each_entry(vma, &o->vma_list, vma_link)
5404 if (vma->vm == ggtt &&
5405 i915_ggtt_view_equal(&vma->ggtt_view, view))
5406 return vma->node.start;
5407
5408 WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
5409 return -1;
5410 }
5411
5412 bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
5413 struct i915_address_space *vm)
5414 {
5415 struct i915_vma *vma;
5416
5417 list_for_each_entry(vma, &o->vma_list, vma_link) {
5418 if (i915_is_ggtt(vma->vm) &&
5419 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
5420 continue;
5421 if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
5422 return true;
5423 }
5424
5425 return false;
5426 }
5427
5428 bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
5429 const struct i915_ggtt_view *view)
5430 {
5431 struct i915_address_space *ggtt = i915_obj_to_ggtt(o);
5432 struct i915_vma *vma;
5433
5434 list_for_each_entry(vma, &o->vma_list, vma_link)
5435 if (vma->vm == ggtt &&
5436 i915_ggtt_view_equal(&vma->ggtt_view, view) &&
5437 drm_mm_node_allocated(&vma->node))
5438 return true;
5439
5440 return false;
5441 }
5442
5443 bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
5444 {
5445 struct i915_vma *vma;
5446
5447 list_for_each_entry(vma, &o->vma_list, vma_link)
5448 if (drm_mm_node_allocated(&vma->node))
5449 return true;
5450
5451 return false;
5452 }
5453
5454 unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
5455 struct i915_address_space *vm)
5456 {
5457 struct drm_i915_private *dev_priv = o->base.dev->dev_private;
5458 struct i915_vma *vma;
5459
5460 WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
5461
5462 BUG_ON(list_empty(&o->vma_list));
5463
5464 list_for_each_entry(vma, &o->vma_list, vma_link) {
5465 if (i915_is_ggtt(vma->vm) &&
5466 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
5467 continue;
5468 if (vma->vm == vm)
5469 return vma->node.size;
5470 }
5471 return 0;
5472 }
5473
5474 bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
5475 {
5476 struct i915_vma *vma;
5477 list_for_each_entry(vma, &obj->vma_list, vma_link)
5478 if (vma->pin_count > 0)
5479 return true;
5480
5481 return false;
5482 }
This page took 0.155129 seconds and 6 git commands to generate.