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