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