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