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