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