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