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