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