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