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