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