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