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