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