2 * Copyright © 2008 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
32 #include "i915_trace.h"
33 #include "intel_drv.h"
34 #include <linux/slab.h>
35 #include <linux/swap.h>
36 #include <linux/pci.h>
37 #include <linux/intel-gtt.h>
39 static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object
*obj
);
41 static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object
*obj
,
43 static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object
*obj
);
44 static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object
*obj
);
45 static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object
*obj
,
47 static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object
*obj
,
50 static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object
*obj
);
51 static int i915_gem_object_wait_rendering(struct drm_gem_object
*obj
);
52 static int i915_gem_object_bind_to_gtt(struct drm_gem_object
*obj
,
54 static void i915_gem_clear_fence_reg(struct drm_gem_object
*obj
);
55 static int i915_gem_phys_pwrite(struct drm_device
*dev
, struct drm_gem_object
*obj
,
56 struct drm_i915_gem_pwrite
*args
,
57 struct drm_file
*file_priv
);
58 static void i915_gem_free_object_tail(struct drm_gem_object
*obj
);
60 static LIST_HEAD(shrink_list
);
61 static DEFINE_SPINLOCK(shrink_list_lock
);
64 i915_gem_object_is_inactive(struct drm_i915_gem_object
*obj_priv
)
66 return obj_priv
->gtt_space
&&
68 obj_priv
->pin_count
== 0;
71 int i915_gem_do_init(struct drm_device
*dev
, unsigned long start
,
74 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
77 (start
& (PAGE_SIZE
- 1)) != 0 ||
78 (end
& (PAGE_SIZE
- 1)) != 0) {
82 drm_mm_init(&dev_priv
->mm
.gtt_space
, start
,
85 dev
->gtt_total
= (uint32_t) (end
- start
);
91 i915_gem_init_ioctl(struct drm_device
*dev
, void *data
,
92 struct drm_file
*file_priv
)
94 struct drm_i915_gem_init
*args
= data
;
97 mutex_lock(&dev
->struct_mutex
);
98 ret
= i915_gem_do_init(dev
, args
->gtt_start
, args
->gtt_end
);
99 mutex_unlock(&dev
->struct_mutex
);
105 i915_gem_get_aperture_ioctl(struct drm_device
*dev
, void *data
,
106 struct drm_file
*file_priv
)
108 struct drm_i915_gem_get_aperture
*args
= data
;
110 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
113 args
->aper_size
= dev
->gtt_total
;
114 args
->aper_available_size
= (args
->aper_size
-
115 atomic_read(&dev
->pin_memory
));
122 * Creates a new mm object and returns a handle to it.
125 i915_gem_create_ioctl(struct drm_device
*dev
, void *data
,
126 struct drm_file
*file_priv
)
128 struct drm_i915_gem_create
*args
= data
;
129 struct drm_gem_object
*obj
;
133 args
->size
= roundup(args
->size
, PAGE_SIZE
);
135 /* Allocate the new object */
136 obj
= i915_gem_alloc_object(dev
, args
->size
);
140 ret
= drm_gem_handle_create(file_priv
, obj
, &handle
);
142 drm_gem_object_unreference_unlocked(obj
);
146 /* Sink the floating reference from kref_init(handlecount) */
147 drm_gem_object_handle_unreference_unlocked(obj
);
149 args
->handle
= handle
;
154 fast_shmem_read(struct page
**pages
,
155 loff_t page_base
, int page_offset
,
162 vaddr
= kmap_atomic(pages
[page_base
>> PAGE_SHIFT
], KM_USER0
);
165 unwritten
= __copy_to_user_inatomic(data
, vaddr
+ page_offset
, length
);
166 kunmap_atomic(vaddr
, KM_USER0
);
174 static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object
*obj
)
176 drm_i915_private_t
*dev_priv
= obj
->dev
->dev_private
;
177 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
179 return dev_priv
->mm
.bit_6_swizzle_x
== I915_BIT_6_SWIZZLE_9_10_17
&&
180 obj_priv
->tiling_mode
!= I915_TILING_NONE
;
184 slow_shmem_copy(struct page
*dst_page
,
186 struct page
*src_page
,
190 char *dst_vaddr
, *src_vaddr
;
192 dst_vaddr
= kmap(dst_page
);
193 src_vaddr
= kmap(src_page
);
195 memcpy(dst_vaddr
+ dst_offset
, src_vaddr
+ src_offset
, length
);
202 slow_shmem_bit17_copy(struct page
*gpu_page
,
204 struct page
*cpu_page
,
209 char *gpu_vaddr
, *cpu_vaddr
;
211 /* Use the unswizzled path if this page isn't affected. */
212 if ((page_to_phys(gpu_page
) & (1 << 17)) == 0) {
214 return slow_shmem_copy(cpu_page
, cpu_offset
,
215 gpu_page
, gpu_offset
, length
);
217 return slow_shmem_copy(gpu_page
, gpu_offset
,
218 cpu_page
, cpu_offset
, length
);
221 gpu_vaddr
= kmap(gpu_page
);
222 cpu_vaddr
= kmap(cpu_page
);
224 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
225 * XORing with the other bits (A9 for Y, A9 and A10 for X)
228 int cacheline_end
= ALIGN(gpu_offset
+ 1, 64);
229 int this_length
= min(cacheline_end
- gpu_offset
, length
);
230 int swizzled_gpu_offset
= gpu_offset
^ 64;
233 memcpy(cpu_vaddr
+ cpu_offset
,
234 gpu_vaddr
+ swizzled_gpu_offset
,
237 memcpy(gpu_vaddr
+ swizzled_gpu_offset
,
238 cpu_vaddr
+ cpu_offset
,
241 cpu_offset
+= this_length
;
242 gpu_offset
+= this_length
;
243 length
-= this_length
;
251 * This is the fast shmem pread path, which attempts to copy_from_user directly
252 * from the backing pages of the object to the user's address space. On a
253 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
256 i915_gem_shmem_pread_fast(struct drm_device
*dev
, struct drm_gem_object
*obj
,
257 struct drm_i915_gem_pread
*args
,
258 struct drm_file
*file_priv
)
260 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
262 loff_t offset
, page_base
;
263 char __user
*user_data
;
264 int page_offset
, page_length
;
267 user_data
= (char __user
*) (uintptr_t) args
->data_ptr
;
270 mutex_lock(&dev
->struct_mutex
);
272 ret
= i915_gem_object_get_pages(obj
, 0);
276 ret
= i915_gem_object_set_cpu_read_domain_range(obj
, args
->offset
,
281 obj_priv
= to_intel_bo(obj
);
282 offset
= args
->offset
;
285 /* Operation in this page
287 * page_base = page offset within aperture
288 * page_offset = offset within page
289 * page_length = bytes to copy for this page
291 page_base
= (offset
& ~(PAGE_SIZE
-1));
292 page_offset
= offset
& (PAGE_SIZE
-1);
293 page_length
= remain
;
294 if ((page_offset
+ remain
) > PAGE_SIZE
)
295 page_length
= PAGE_SIZE
- page_offset
;
297 ret
= fast_shmem_read(obj_priv
->pages
,
298 page_base
, page_offset
,
299 user_data
, page_length
);
303 remain
-= page_length
;
304 user_data
+= page_length
;
305 offset
+= page_length
;
309 i915_gem_object_put_pages(obj
);
311 mutex_unlock(&dev
->struct_mutex
);
317 i915_gem_object_get_pages_or_evict(struct drm_gem_object
*obj
)
321 ret
= i915_gem_object_get_pages(obj
, __GFP_NORETRY
| __GFP_NOWARN
);
323 /* If we've insufficient memory to map in the pages, attempt
324 * to make some space by throwing out some old buffers.
326 if (ret
== -ENOMEM
) {
327 struct drm_device
*dev
= obj
->dev
;
329 ret
= i915_gem_evict_something(dev
, obj
->size
,
330 i915_gem_get_gtt_alignment(obj
));
334 ret
= i915_gem_object_get_pages(obj
, 0);
341 * This is the fallback shmem pread path, which allocates temporary storage
342 * in kernel space to copy_to_user into outside of the struct_mutex, so we
343 * can copy out of the object's backing pages while holding the struct mutex
344 * and not take page faults.
347 i915_gem_shmem_pread_slow(struct drm_device
*dev
, struct drm_gem_object
*obj
,
348 struct drm_i915_gem_pread
*args
,
349 struct drm_file
*file_priv
)
351 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
352 struct mm_struct
*mm
= current
->mm
;
353 struct page
**user_pages
;
355 loff_t offset
, pinned_pages
, i
;
356 loff_t first_data_page
, last_data_page
, num_pages
;
357 int shmem_page_index
, shmem_page_offset
;
358 int data_page_index
, data_page_offset
;
361 uint64_t data_ptr
= args
->data_ptr
;
362 int do_bit17_swizzling
;
366 /* Pin the user pages containing the data. We can't fault while
367 * holding the struct mutex, yet we want to hold it while
368 * dereferencing the user data.
370 first_data_page
= data_ptr
/ PAGE_SIZE
;
371 last_data_page
= (data_ptr
+ args
->size
- 1) / PAGE_SIZE
;
372 num_pages
= last_data_page
- first_data_page
+ 1;
374 user_pages
= drm_calloc_large(num_pages
, sizeof(struct page
*));
375 if (user_pages
== NULL
)
378 down_read(&mm
->mmap_sem
);
379 pinned_pages
= get_user_pages(current
, mm
, (uintptr_t)args
->data_ptr
,
380 num_pages
, 1, 0, user_pages
, NULL
);
381 up_read(&mm
->mmap_sem
);
382 if (pinned_pages
< num_pages
) {
384 goto fail_put_user_pages
;
387 do_bit17_swizzling
= i915_gem_object_needs_bit17_swizzle(obj
);
389 mutex_lock(&dev
->struct_mutex
);
391 ret
= i915_gem_object_get_pages_or_evict(obj
);
395 ret
= i915_gem_object_set_cpu_read_domain_range(obj
, args
->offset
,
400 obj_priv
= to_intel_bo(obj
);
401 offset
= args
->offset
;
404 /* Operation in this page
406 * shmem_page_index = page number within shmem file
407 * shmem_page_offset = offset within page in shmem file
408 * data_page_index = page number in get_user_pages return
409 * data_page_offset = offset with data_page_index page.
410 * page_length = bytes to copy for this page
412 shmem_page_index
= offset
/ PAGE_SIZE
;
413 shmem_page_offset
= offset
& ~PAGE_MASK
;
414 data_page_index
= data_ptr
/ PAGE_SIZE
- first_data_page
;
415 data_page_offset
= data_ptr
& ~PAGE_MASK
;
417 page_length
= remain
;
418 if ((shmem_page_offset
+ page_length
) > PAGE_SIZE
)
419 page_length
= PAGE_SIZE
- shmem_page_offset
;
420 if ((data_page_offset
+ page_length
) > PAGE_SIZE
)
421 page_length
= PAGE_SIZE
- data_page_offset
;
423 if (do_bit17_swizzling
) {
424 slow_shmem_bit17_copy(obj_priv
->pages
[shmem_page_index
],
426 user_pages
[data_page_index
],
431 slow_shmem_copy(user_pages
[data_page_index
],
433 obj_priv
->pages
[shmem_page_index
],
438 remain
-= page_length
;
439 data_ptr
+= page_length
;
440 offset
+= page_length
;
444 i915_gem_object_put_pages(obj
);
446 mutex_unlock(&dev
->struct_mutex
);
448 for (i
= 0; i
< pinned_pages
; i
++) {
449 SetPageDirty(user_pages
[i
]);
450 page_cache_release(user_pages
[i
]);
452 drm_free_large(user_pages
);
458 * Reads data from the object referenced by handle.
460 * On error, the contents of *data are undefined.
463 i915_gem_pread_ioctl(struct drm_device
*dev
, void *data
,
464 struct drm_file
*file_priv
)
466 struct drm_i915_gem_pread
*args
= data
;
467 struct drm_gem_object
*obj
;
468 struct drm_i915_gem_object
*obj_priv
;
471 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
474 obj_priv
= to_intel_bo(obj
);
476 /* Bounds check source.
478 * XXX: This could use review for overflow issues...
480 if (args
->offset
> obj
->size
|| args
->size
> obj
->size
||
481 args
->offset
+ args
->size
> obj
->size
) {
482 drm_gem_object_unreference_unlocked(obj
);
486 if (i915_gem_object_needs_bit17_swizzle(obj
)) {
487 ret
= i915_gem_shmem_pread_slow(dev
, obj
, args
, file_priv
);
489 ret
= i915_gem_shmem_pread_fast(dev
, obj
, args
, file_priv
);
491 ret
= i915_gem_shmem_pread_slow(dev
, obj
, args
,
495 drm_gem_object_unreference_unlocked(obj
);
500 /* This is the fast write path which cannot handle
501 * page faults in the source data
505 fast_user_write(struct io_mapping
*mapping
,
506 loff_t page_base
, int page_offset
,
507 char __user
*user_data
,
511 unsigned long unwritten
;
513 vaddr_atomic
= io_mapping_map_atomic_wc(mapping
, page_base
, KM_USER0
);
514 unwritten
= __copy_from_user_inatomic_nocache(vaddr_atomic
+ page_offset
,
516 io_mapping_unmap_atomic(vaddr_atomic
, KM_USER0
);
522 /* Here's the write path which can sleep for
527 slow_kernel_write(struct io_mapping
*mapping
,
528 loff_t gtt_base
, int gtt_offset
,
529 struct page
*user_page
, int user_offset
,
532 char __iomem
*dst_vaddr
;
535 dst_vaddr
= io_mapping_map_wc(mapping
, gtt_base
);
536 src_vaddr
= kmap(user_page
);
538 memcpy_toio(dst_vaddr
+ gtt_offset
,
539 src_vaddr
+ user_offset
,
543 io_mapping_unmap(dst_vaddr
);
547 fast_shmem_write(struct page
**pages
,
548 loff_t page_base
, int page_offset
,
553 unsigned long unwritten
;
555 vaddr
= kmap_atomic(pages
[page_base
>> PAGE_SHIFT
], KM_USER0
);
558 unwritten
= __copy_from_user_inatomic(vaddr
+ page_offset
, data
, length
);
559 kunmap_atomic(vaddr
, KM_USER0
);
567 * This is the fast pwrite path, where we copy the data directly from the
568 * user into the GTT, uncached.
571 i915_gem_gtt_pwrite_fast(struct drm_device
*dev
, struct drm_gem_object
*obj
,
572 struct drm_i915_gem_pwrite
*args
,
573 struct drm_file
*file_priv
)
575 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
576 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
578 loff_t offset
, page_base
;
579 char __user
*user_data
;
580 int page_offset
, page_length
;
583 user_data
= (char __user
*) (uintptr_t) args
->data_ptr
;
585 if (!access_ok(VERIFY_READ
, user_data
, remain
))
589 mutex_lock(&dev
->struct_mutex
);
590 ret
= i915_gem_object_pin(obj
, 0);
592 mutex_unlock(&dev
->struct_mutex
);
595 ret
= i915_gem_object_set_to_gtt_domain(obj
, 1);
599 obj_priv
= to_intel_bo(obj
);
600 offset
= obj_priv
->gtt_offset
+ args
->offset
;
603 /* Operation in this page
605 * page_base = page offset within aperture
606 * page_offset = offset within page
607 * page_length = bytes to copy for this page
609 page_base
= (offset
& ~(PAGE_SIZE
-1));
610 page_offset
= offset
& (PAGE_SIZE
-1);
611 page_length
= remain
;
612 if ((page_offset
+ remain
) > PAGE_SIZE
)
613 page_length
= PAGE_SIZE
- page_offset
;
615 ret
= fast_user_write (dev_priv
->mm
.gtt_mapping
, page_base
,
616 page_offset
, user_data
, page_length
);
618 /* If we get a fault while copying data, then (presumably) our
619 * source page isn't available. Return the error and we'll
620 * retry in the slow path.
625 remain
-= page_length
;
626 user_data
+= page_length
;
627 offset
+= page_length
;
631 i915_gem_object_unpin(obj
);
632 mutex_unlock(&dev
->struct_mutex
);
638 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
639 * the memory and maps it using kmap_atomic for copying.
641 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
642 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
645 i915_gem_gtt_pwrite_slow(struct drm_device
*dev
, struct drm_gem_object
*obj
,
646 struct drm_i915_gem_pwrite
*args
,
647 struct drm_file
*file_priv
)
649 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
650 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
652 loff_t gtt_page_base
, offset
;
653 loff_t first_data_page
, last_data_page
, num_pages
;
654 loff_t pinned_pages
, i
;
655 struct page
**user_pages
;
656 struct mm_struct
*mm
= current
->mm
;
657 int gtt_page_offset
, data_page_offset
, data_page_index
, page_length
;
659 uint64_t data_ptr
= args
->data_ptr
;
663 /* Pin the user pages containing the data. We can't fault while
664 * holding the struct mutex, and all of the pwrite implementations
665 * want to hold it while dereferencing the user data.
667 first_data_page
= data_ptr
/ PAGE_SIZE
;
668 last_data_page
= (data_ptr
+ args
->size
- 1) / PAGE_SIZE
;
669 num_pages
= last_data_page
- first_data_page
+ 1;
671 user_pages
= drm_calloc_large(num_pages
, sizeof(struct page
*));
672 if (user_pages
== NULL
)
675 down_read(&mm
->mmap_sem
);
676 pinned_pages
= get_user_pages(current
, mm
, (uintptr_t)args
->data_ptr
,
677 num_pages
, 0, 0, user_pages
, NULL
);
678 up_read(&mm
->mmap_sem
);
679 if (pinned_pages
< num_pages
) {
681 goto out_unpin_pages
;
684 mutex_lock(&dev
->struct_mutex
);
685 ret
= i915_gem_object_pin(obj
, 0);
689 ret
= i915_gem_object_set_to_gtt_domain(obj
, 1);
691 goto out_unpin_object
;
693 obj_priv
= to_intel_bo(obj
);
694 offset
= obj_priv
->gtt_offset
+ args
->offset
;
697 /* Operation in this page
699 * gtt_page_base = page offset within aperture
700 * gtt_page_offset = offset within page in aperture
701 * data_page_index = page number in get_user_pages return
702 * data_page_offset = offset with data_page_index page.
703 * page_length = bytes to copy for this page
705 gtt_page_base
= offset
& PAGE_MASK
;
706 gtt_page_offset
= offset
& ~PAGE_MASK
;
707 data_page_index
= data_ptr
/ PAGE_SIZE
- first_data_page
;
708 data_page_offset
= data_ptr
& ~PAGE_MASK
;
710 page_length
= remain
;
711 if ((gtt_page_offset
+ page_length
) > PAGE_SIZE
)
712 page_length
= PAGE_SIZE
- gtt_page_offset
;
713 if ((data_page_offset
+ page_length
) > PAGE_SIZE
)
714 page_length
= PAGE_SIZE
- data_page_offset
;
716 slow_kernel_write(dev_priv
->mm
.gtt_mapping
,
717 gtt_page_base
, gtt_page_offset
,
718 user_pages
[data_page_index
],
722 remain
-= page_length
;
723 offset
+= page_length
;
724 data_ptr
+= page_length
;
728 i915_gem_object_unpin(obj
);
730 mutex_unlock(&dev
->struct_mutex
);
732 for (i
= 0; i
< pinned_pages
; i
++)
733 page_cache_release(user_pages
[i
]);
734 drm_free_large(user_pages
);
740 * This is the fast shmem pwrite path, which attempts to directly
741 * copy_from_user into the kmapped pages backing the object.
744 i915_gem_shmem_pwrite_fast(struct drm_device
*dev
, struct drm_gem_object
*obj
,
745 struct drm_i915_gem_pwrite
*args
,
746 struct drm_file
*file_priv
)
748 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
750 loff_t offset
, page_base
;
751 char __user
*user_data
;
752 int page_offset
, page_length
;
755 user_data
= (char __user
*) (uintptr_t) args
->data_ptr
;
758 mutex_lock(&dev
->struct_mutex
);
760 ret
= i915_gem_object_get_pages(obj
, 0);
764 ret
= i915_gem_object_set_to_cpu_domain(obj
, 1);
768 obj_priv
= to_intel_bo(obj
);
769 offset
= args
->offset
;
773 /* Operation in this page
775 * page_base = page offset within aperture
776 * page_offset = offset within page
777 * page_length = bytes to copy for this page
779 page_base
= (offset
& ~(PAGE_SIZE
-1));
780 page_offset
= offset
& (PAGE_SIZE
-1);
781 page_length
= remain
;
782 if ((page_offset
+ remain
) > PAGE_SIZE
)
783 page_length
= PAGE_SIZE
- page_offset
;
785 ret
= fast_shmem_write(obj_priv
->pages
,
786 page_base
, page_offset
,
787 user_data
, page_length
);
791 remain
-= page_length
;
792 user_data
+= page_length
;
793 offset
+= page_length
;
797 i915_gem_object_put_pages(obj
);
799 mutex_unlock(&dev
->struct_mutex
);
805 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
806 * the memory and maps it using kmap_atomic for copying.
808 * This avoids taking mmap_sem for faulting on the user's address while the
809 * struct_mutex is held.
812 i915_gem_shmem_pwrite_slow(struct drm_device
*dev
, struct drm_gem_object
*obj
,
813 struct drm_i915_gem_pwrite
*args
,
814 struct drm_file
*file_priv
)
816 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
817 struct mm_struct
*mm
= current
->mm
;
818 struct page
**user_pages
;
820 loff_t offset
, pinned_pages
, i
;
821 loff_t first_data_page
, last_data_page
, num_pages
;
822 int shmem_page_index
, shmem_page_offset
;
823 int data_page_index
, data_page_offset
;
826 uint64_t data_ptr
= args
->data_ptr
;
827 int do_bit17_swizzling
;
831 /* Pin the user pages containing the data. We can't fault while
832 * holding the struct mutex, and all of the pwrite implementations
833 * want to hold it while dereferencing the user data.
835 first_data_page
= data_ptr
/ PAGE_SIZE
;
836 last_data_page
= (data_ptr
+ args
->size
- 1) / PAGE_SIZE
;
837 num_pages
= last_data_page
- first_data_page
+ 1;
839 user_pages
= drm_calloc_large(num_pages
, sizeof(struct page
*));
840 if (user_pages
== NULL
)
843 down_read(&mm
->mmap_sem
);
844 pinned_pages
= get_user_pages(current
, mm
, (uintptr_t)args
->data_ptr
,
845 num_pages
, 0, 0, user_pages
, NULL
);
846 up_read(&mm
->mmap_sem
);
847 if (pinned_pages
< num_pages
) {
849 goto fail_put_user_pages
;
852 do_bit17_swizzling
= i915_gem_object_needs_bit17_swizzle(obj
);
854 mutex_lock(&dev
->struct_mutex
);
856 ret
= i915_gem_object_get_pages_or_evict(obj
);
860 ret
= i915_gem_object_set_to_cpu_domain(obj
, 1);
864 obj_priv
= to_intel_bo(obj
);
865 offset
= args
->offset
;
869 /* Operation in this page
871 * shmem_page_index = page number within shmem file
872 * shmem_page_offset = offset within page in shmem file
873 * data_page_index = page number in get_user_pages return
874 * data_page_offset = offset with data_page_index page.
875 * page_length = bytes to copy for this page
877 shmem_page_index
= offset
/ PAGE_SIZE
;
878 shmem_page_offset
= offset
& ~PAGE_MASK
;
879 data_page_index
= data_ptr
/ PAGE_SIZE
- first_data_page
;
880 data_page_offset
= data_ptr
& ~PAGE_MASK
;
882 page_length
= remain
;
883 if ((shmem_page_offset
+ page_length
) > PAGE_SIZE
)
884 page_length
= PAGE_SIZE
- shmem_page_offset
;
885 if ((data_page_offset
+ page_length
) > PAGE_SIZE
)
886 page_length
= PAGE_SIZE
- data_page_offset
;
888 if (do_bit17_swizzling
) {
889 slow_shmem_bit17_copy(obj_priv
->pages
[shmem_page_index
],
891 user_pages
[data_page_index
],
896 slow_shmem_copy(obj_priv
->pages
[shmem_page_index
],
898 user_pages
[data_page_index
],
903 remain
-= page_length
;
904 data_ptr
+= page_length
;
905 offset
+= page_length
;
909 i915_gem_object_put_pages(obj
);
911 mutex_unlock(&dev
->struct_mutex
);
913 for (i
= 0; i
< pinned_pages
; i
++)
914 page_cache_release(user_pages
[i
]);
915 drm_free_large(user_pages
);
921 * Writes data to the object referenced by handle.
923 * On error, the contents of the buffer that were to be modified are undefined.
926 i915_gem_pwrite_ioctl(struct drm_device
*dev
, void *data
,
927 struct drm_file
*file_priv
)
929 struct drm_i915_gem_pwrite
*args
= data
;
930 struct drm_gem_object
*obj
;
931 struct drm_i915_gem_object
*obj_priv
;
934 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
937 obj_priv
= to_intel_bo(obj
);
939 /* Bounds check destination.
941 * XXX: This could use review for overflow issues...
943 if (args
->offset
> obj
->size
|| args
->size
> obj
->size
||
944 args
->offset
+ args
->size
> obj
->size
) {
945 drm_gem_object_unreference_unlocked(obj
);
949 /* We can only do the GTT pwrite on untiled buffers, as otherwise
950 * it would end up going through the fenced access, and we'll get
951 * different detiling behavior between reading and writing.
952 * pread/pwrite currently are reading and writing from the CPU
953 * perspective, requiring manual detiling by the client.
955 if (obj_priv
->phys_obj
)
956 ret
= i915_gem_phys_pwrite(dev
, obj
, args
, file_priv
);
957 else if (obj_priv
->tiling_mode
== I915_TILING_NONE
&&
958 dev
->gtt_total
!= 0 &&
959 obj
->write_domain
!= I915_GEM_DOMAIN_CPU
) {
960 ret
= i915_gem_gtt_pwrite_fast(dev
, obj
, args
, file_priv
);
961 if (ret
== -EFAULT
) {
962 ret
= i915_gem_gtt_pwrite_slow(dev
, obj
, args
,
965 } else if (i915_gem_object_needs_bit17_swizzle(obj
)) {
966 ret
= i915_gem_shmem_pwrite_slow(dev
, obj
, args
, file_priv
);
968 ret
= i915_gem_shmem_pwrite_fast(dev
, obj
, args
, file_priv
);
969 if (ret
== -EFAULT
) {
970 ret
= i915_gem_shmem_pwrite_slow(dev
, obj
, args
,
977 DRM_INFO("pwrite failed %d\n", ret
);
980 drm_gem_object_unreference_unlocked(obj
);
986 * Called when user space prepares to use an object with the CPU, either
987 * through the mmap ioctl's mapping or a GTT mapping.
990 i915_gem_set_domain_ioctl(struct drm_device
*dev
, void *data
,
991 struct drm_file
*file_priv
)
993 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
994 struct drm_i915_gem_set_domain
*args
= data
;
995 struct drm_gem_object
*obj
;
996 struct drm_i915_gem_object
*obj_priv
;
997 uint32_t read_domains
= args
->read_domains
;
998 uint32_t write_domain
= args
->write_domain
;
1001 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
1004 /* Only handle setting domains to types used by the CPU. */
1005 if (write_domain
& I915_GEM_GPU_DOMAINS
)
1008 if (read_domains
& I915_GEM_GPU_DOMAINS
)
1011 /* Having something in the write domain implies it's in the read
1012 * domain, and only that read domain. Enforce that in the request.
1014 if (write_domain
!= 0 && read_domains
!= write_domain
)
1017 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
1020 obj_priv
= to_intel_bo(obj
);
1022 mutex_lock(&dev
->struct_mutex
);
1024 intel_mark_busy(dev
, obj
);
1027 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
1028 obj
, obj
->size
, read_domains
, write_domain
);
1030 if (read_domains
& I915_GEM_DOMAIN_GTT
) {
1031 ret
= i915_gem_object_set_to_gtt_domain(obj
, write_domain
!= 0);
1033 /* Update the LRU on the fence for the CPU access that's
1036 if (obj_priv
->fence_reg
!= I915_FENCE_REG_NONE
) {
1037 struct drm_i915_fence_reg
*reg
=
1038 &dev_priv
->fence_regs
[obj_priv
->fence_reg
];
1039 list_move_tail(®
->lru_list
,
1040 &dev_priv
->mm
.fence_list
);
1043 /* Silently promote "you're not bound, there was nothing to do"
1044 * to success, since the client was just asking us to
1045 * make sure everything was done.
1050 ret
= i915_gem_object_set_to_cpu_domain(obj
, write_domain
!= 0);
1054 /* Maintain LRU order of "inactive" objects */
1055 if (ret
== 0 && i915_gem_object_is_inactive(obj_priv
))
1056 list_move_tail(&obj_priv
->list
, &dev_priv
->mm
.inactive_list
);
1058 drm_gem_object_unreference(obj
);
1059 mutex_unlock(&dev
->struct_mutex
);
1064 * Called when user space has done writes to this buffer
1067 i915_gem_sw_finish_ioctl(struct drm_device
*dev
, void *data
,
1068 struct drm_file
*file_priv
)
1070 struct drm_i915_gem_sw_finish
*args
= data
;
1071 struct drm_gem_object
*obj
;
1072 struct drm_i915_gem_object
*obj_priv
;
1075 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
1078 mutex_lock(&dev
->struct_mutex
);
1079 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
1081 mutex_unlock(&dev
->struct_mutex
);
1086 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
1087 __func__
, args
->handle
, obj
, obj
->size
);
1089 obj_priv
= to_intel_bo(obj
);
1091 /* Pinned buffers may be scanout, so flush the cache */
1092 if (obj_priv
->pin_count
)
1093 i915_gem_object_flush_cpu_write_domain(obj
);
1095 drm_gem_object_unreference(obj
);
1096 mutex_unlock(&dev
->struct_mutex
);
1101 * Maps the contents of an object, returning the address it is mapped
1104 * While the mapping holds a reference on the contents of the object, it doesn't
1105 * imply a ref on the object itself.
1108 i915_gem_mmap_ioctl(struct drm_device
*dev
, void *data
,
1109 struct drm_file
*file_priv
)
1111 struct drm_i915_gem_mmap
*args
= data
;
1112 struct drm_gem_object
*obj
;
1116 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
1119 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
1123 offset
= args
->offset
;
1125 down_write(¤t
->mm
->mmap_sem
);
1126 addr
= do_mmap(obj
->filp
, 0, args
->size
,
1127 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
1129 up_write(¤t
->mm
->mmap_sem
);
1130 drm_gem_object_unreference_unlocked(obj
);
1131 if (IS_ERR((void *)addr
))
1134 args
->addr_ptr
= (uint64_t) addr
;
1140 * i915_gem_fault - fault a page into the GTT
1141 * vma: VMA in question
1144 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1145 * from userspace. The fault handler takes care of binding the object to
1146 * the GTT (if needed), allocating and programming a fence register (again,
1147 * only if needed based on whether the old reg is still valid or the object
1148 * is tiled) and inserting a new PTE into the faulting process.
1150 * Note that the faulting process may involve evicting existing objects
1151 * from the GTT and/or fence registers to make room. So performance may
1152 * suffer if the GTT working set is large or there are few fence registers
1155 int i915_gem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1157 struct drm_gem_object
*obj
= vma
->vm_private_data
;
1158 struct drm_device
*dev
= obj
->dev
;
1159 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1160 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1161 pgoff_t page_offset
;
1164 bool write
= !!(vmf
->flags
& FAULT_FLAG_WRITE
);
1166 /* We don't use vmf->pgoff since that has the fake offset */
1167 page_offset
= ((unsigned long)vmf
->virtual_address
- vma
->vm_start
) >>
1170 /* Now bind it into the GTT if needed */
1171 mutex_lock(&dev
->struct_mutex
);
1172 if (!obj_priv
->gtt_space
) {
1173 ret
= i915_gem_object_bind_to_gtt(obj
, 0);
1177 ret
= i915_gem_object_set_to_gtt_domain(obj
, write
);
1182 /* Need a new fence register? */
1183 if (obj_priv
->tiling_mode
!= I915_TILING_NONE
) {
1184 ret
= i915_gem_object_get_fence_reg(obj
);
1189 if (i915_gem_object_is_inactive(obj_priv
))
1190 list_move_tail(&obj_priv
->list
, &dev_priv
->mm
.inactive_list
);
1192 pfn
= ((dev
->agp
->base
+ obj_priv
->gtt_offset
) >> PAGE_SHIFT
) +
1195 /* Finally, remap it using the new GTT offset */
1196 ret
= vm_insert_pfn(vma
, (unsigned long)vmf
->virtual_address
, pfn
);
1198 mutex_unlock(&dev
->struct_mutex
);
1203 return VM_FAULT_NOPAGE
;
1206 return VM_FAULT_OOM
;
1208 return VM_FAULT_SIGBUS
;
1213 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1214 * @obj: obj in question
1216 * GEM memory mapping works by handing back to userspace a fake mmap offset
1217 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1218 * up the object based on the offset and sets up the various memory mapping
1221 * This routine allocates and attaches a fake offset for @obj.
1224 i915_gem_create_mmap_offset(struct drm_gem_object
*obj
)
1226 struct drm_device
*dev
= obj
->dev
;
1227 struct drm_gem_mm
*mm
= dev
->mm_private
;
1228 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1229 struct drm_map_list
*list
;
1230 struct drm_local_map
*map
;
1233 /* Set the object up for mmap'ing */
1234 list
= &obj
->map_list
;
1235 list
->map
= kzalloc(sizeof(struct drm_map_list
), GFP_KERNEL
);
1240 map
->type
= _DRM_GEM
;
1241 map
->size
= obj
->size
;
1244 /* Get a DRM GEM mmap offset allocated... */
1245 list
->file_offset_node
= drm_mm_search_free(&mm
->offset_manager
,
1246 obj
->size
/ PAGE_SIZE
, 0, 0);
1247 if (!list
->file_offset_node
) {
1248 DRM_ERROR("failed to allocate offset for bo %d\n", obj
->name
);
1253 list
->file_offset_node
= drm_mm_get_block(list
->file_offset_node
,
1254 obj
->size
/ PAGE_SIZE
, 0);
1255 if (!list
->file_offset_node
) {
1260 list
->hash
.key
= list
->file_offset_node
->start
;
1261 if (drm_ht_insert_item(&mm
->offset_hash
, &list
->hash
)) {
1262 DRM_ERROR("failed to add to map hash\n");
1267 /* By now we should be all set, any drm_mmap request on the offset
1268 * below will get to our mmap & fault handler */
1269 obj_priv
->mmap_offset
= ((uint64_t) list
->hash
.key
) << PAGE_SHIFT
;
1274 drm_mm_put_block(list
->file_offset_node
);
1282 * i915_gem_release_mmap - remove physical page mappings
1283 * @obj: obj in question
1285 * Preserve the reservation of the mmapping with the DRM core code, but
1286 * relinquish ownership of the pages back to the system.
1288 * It is vital that we remove the page mapping if we have mapped a tiled
1289 * object through the GTT and then lose the fence register due to
1290 * resource pressure. Similarly if the object has been moved out of the
1291 * aperture, than pages mapped into userspace must be revoked. Removing the
1292 * mapping will then trigger a page fault on the next user access, allowing
1293 * fixup by i915_gem_fault().
1296 i915_gem_release_mmap(struct drm_gem_object
*obj
)
1298 struct drm_device
*dev
= obj
->dev
;
1299 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1301 if (dev
->dev_mapping
)
1302 unmap_mapping_range(dev
->dev_mapping
,
1303 obj_priv
->mmap_offset
, obj
->size
, 1);
1307 i915_gem_free_mmap_offset(struct drm_gem_object
*obj
)
1309 struct drm_device
*dev
= obj
->dev
;
1310 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1311 struct drm_gem_mm
*mm
= dev
->mm_private
;
1312 struct drm_map_list
*list
;
1314 list
= &obj
->map_list
;
1315 drm_ht_remove_item(&mm
->offset_hash
, &list
->hash
);
1317 if (list
->file_offset_node
) {
1318 drm_mm_put_block(list
->file_offset_node
);
1319 list
->file_offset_node
= NULL
;
1327 obj_priv
->mmap_offset
= 0;
1331 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1332 * @obj: object to check
1334 * Return the required GTT alignment for an object, taking into account
1335 * potential fence register mapping if needed.
1338 i915_gem_get_gtt_alignment(struct drm_gem_object
*obj
)
1340 struct drm_device
*dev
= obj
->dev
;
1341 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1345 * Minimum alignment is 4k (GTT page size), but might be greater
1346 * if a fence register is needed for the object.
1348 if (IS_I965G(dev
) || obj_priv
->tiling_mode
== I915_TILING_NONE
)
1352 * Previous chips need to be aligned to the size of the smallest
1353 * fence register that can contain the object.
1360 for (i
= start
; i
< obj
->size
; i
<<= 1)
1367 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1369 * @data: GTT mapping ioctl data
1370 * @file_priv: GEM object info
1372 * Simply returns the fake offset to userspace so it can mmap it.
1373 * The mmap call will end up in drm_gem_mmap(), which will set things
1374 * up so we can get faults in the handler above.
1376 * The fault handler will take care of binding the object into the GTT
1377 * (since it may have been evicted to make room for something), allocating
1378 * a fence register, and mapping the appropriate aperture address into
1382 i915_gem_mmap_gtt_ioctl(struct drm_device
*dev
, void *data
,
1383 struct drm_file
*file_priv
)
1385 struct drm_i915_gem_mmap_gtt
*args
= data
;
1386 struct drm_gem_object
*obj
;
1387 struct drm_i915_gem_object
*obj_priv
;
1390 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
1393 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
1397 mutex_lock(&dev
->struct_mutex
);
1399 obj_priv
= to_intel_bo(obj
);
1401 if (obj_priv
->madv
!= I915_MADV_WILLNEED
) {
1402 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1403 drm_gem_object_unreference(obj
);
1404 mutex_unlock(&dev
->struct_mutex
);
1409 if (!obj_priv
->mmap_offset
) {
1410 ret
= i915_gem_create_mmap_offset(obj
);
1412 drm_gem_object_unreference(obj
);
1413 mutex_unlock(&dev
->struct_mutex
);
1418 args
->offset
= obj_priv
->mmap_offset
;
1421 * Pull it into the GTT so that we have a page list (makes the
1422 * initial fault faster and any subsequent flushing possible).
1424 if (!obj_priv
->agp_mem
) {
1425 ret
= i915_gem_object_bind_to_gtt(obj
, 0);
1427 drm_gem_object_unreference(obj
);
1428 mutex_unlock(&dev
->struct_mutex
);
1433 drm_gem_object_unreference(obj
);
1434 mutex_unlock(&dev
->struct_mutex
);
1440 i915_gem_object_put_pages(struct drm_gem_object
*obj
)
1442 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1443 int page_count
= obj
->size
/ PAGE_SIZE
;
1446 BUG_ON(obj_priv
->pages_refcount
== 0);
1447 BUG_ON(obj_priv
->madv
== __I915_MADV_PURGED
);
1449 if (--obj_priv
->pages_refcount
!= 0)
1452 if (obj_priv
->tiling_mode
!= I915_TILING_NONE
)
1453 i915_gem_object_save_bit_17_swizzle(obj
);
1455 if (obj_priv
->madv
== I915_MADV_DONTNEED
)
1456 obj_priv
->dirty
= 0;
1458 for (i
= 0; i
< page_count
; i
++) {
1459 if (obj_priv
->dirty
)
1460 set_page_dirty(obj_priv
->pages
[i
]);
1462 if (obj_priv
->madv
== I915_MADV_WILLNEED
)
1463 mark_page_accessed(obj_priv
->pages
[i
]);
1465 page_cache_release(obj_priv
->pages
[i
]);
1467 obj_priv
->dirty
= 0;
1469 drm_free_large(obj_priv
->pages
);
1470 obj_priv
->pages
= NULL
;
1474 i915_gem_next_request_seqno(struct drm_device
*dev
,
1475 struct intel_ring_buffer
*ring
)
1477 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1479 ring
->outstanding_lazy_request
= true;
1481 return dev_priv
->next_seqno
;
1485 i915_gem_object_move_to_active(struct drm_gem_object
*obj
,
1486 struct intel_ring_buffer
*ring
)
1488 struct drm_device
*dev
= obj
->dev
;
1489 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1490 uint32_t seqno
= i915_gem_next_request_seqno(dev
, ring
);
1492 BUG_ON(ring
== NULL
);
1493 obj_priv
->ring
= ring
;
1495 /* Add a reference if we're newly entering the active list. */
1496 if (!obj_priv
->active
) {
1497 drm_gem_object_reference(obj
);
1498 obj_priv
->active
= 1;
1501 /* Move from whatever list we were on to the tail of execution. */
1502 list_move_tail(&obj_priv
->list
, &ring
->active_list
);
1503 obj_priv
->last_rendering_seqno
= seqno
;
1507 i915_gem_object_move_to_flushing(struct drm_gem_object
*obj
)
1509 struct drm_device
*dev
= obj
->dev
;
1510 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1511 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1513 BUG_ON(!obj_priv
->active
);
1514 list_move_tail(&obj_priv
->list
, &dev_priv
->mm
.flushing_list
);
1515 obj_priv
->last_rendering_seqno
= 0;
1518 /* Immediately discard the backing storage */
1520 i915_gem_object_truncate(struct drm_gem_object
*obj
)
1522 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1523 struct inode
*inode
;
1525 /* Our goal here is to return as much of the memory as
1526 * is possible back to the system as we are called from OOM.
1527 * To do this we must instruct the shmfs to drop all of its
1528 * backing pages, *now*. Here we mirror the actions taken
1529 * when by shmem_delete_inode() to release the backing store.
1531 inode
= obj
->filp
->f_path
.dentry
->d_inode
;
1532 truncate_inode_pages(inode
->i_mapping
, 0);
1533 if (inode
->i_op
->truncate_range
)
1534 inode
->i_op
->truncate_range(inode
, 0, (loff_t
)-1);
1536 obj_priv
->madv
= __I915_MADV_PURGED
;
1540 i915_gem_object_is_purgeable(struct drm_i915_gem_object
*obj_priv
)
1542 return obj_priv
->madv
== I915_MADV_DONTNEED
;
1546 i915_gem_object_move_to_inactive(struct drm_gem_object
*obj
)
1548 struct drm_device
*dev
= obj
->dev
;
1549 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1550 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1552 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
1553 if (obj_priv
->pin_count
!= 0)
1554 list_del_init(&obj_priv
->list
);
1556 list_move_tail(&obj_priv
->list
, &dev_priv
->mm
.inactive_list
);
1558 BUG_ON(!list_empty(&obj_priv
->gpu_write_list
));
1560 obj_priv
->last_rendering_seqno
= 0;
1561 obj_priv
->ring
= NULL
;
1562 if (obj_priv
->active
) {
1563 obj_priv
->active
= 0;
1564 drm_gem_object_unreference(obj
);
1566 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
1570 i915_gem_process_flushing_list(struct drm_device
*dev
,
1571 uint32_t flush_domains
,
1572 struct intel_ring_buffer
*ring
)
1574 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1575 struct drm_i915_gem_object
*obj_priv
, *next
;
1577 list_for_each_entry_safe(obj_priv
, next
,
1578 &dev_priv
->mm
.gpu_write_list
,
1580 struct drm_gem_object
*obj
= &obj_priv
->base
;
1582 if ((obj
->write_domain
& flush_domains
) ==
1583 obj
->write_domain
&&
1584 obj_priv
->ring
->ring_flag
== ring
->ring_flag
) {
1585 uint32_t old_write_domain
= obj
->write_domain
;
1587 obj
->write_domain
= 0;
1588 list_del_init(&obj_priv
->gpu_write_list
);
1589 i915_gem_object_move_to_active(obj
, ring
);
1591 /* update the fence lru list */
1592 if (obj_priv
->fence_reg
!= I915_FENCE_REG_NONE
) {
1593 struct drm_i915_fence_reg
*reg
=
1594 &dev_priv
->fence_regs
[obj_priv
->fence_reg
];
1595 list_move_tail(®
->lru_list
,
1596 &dev_priv
->mm
.fence_list
);
1599 trace_i915_gem_object_change_domain(obj
,
1607 i915_add_request(struct drm_device
*dev
,
1608 struct drm_file
*file_priv
,
1609 struct drm_i915_gem_request
*request
,
1610 struct intel_ring_buffer
*ring
)
1612 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1613 struct drm_i915_file_private
*i915_file_priv
= NULL
;
1617 if (file_priv
!= NULL
)
1618 i915_file_priv
= file_priv
->driver_priv
;
1620 if (request
== NULL
) {
1621 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
1622 if (request
== NULL
)
1626 seqno
= ring
->add_request(dev
, ring
, file_priv
, 0);
1628 request
->seqno
= seqno
;
1629 request
->ring
= ring
;
1630 request
->emitted_jiffies
= jiffies
;
1631 was_empty
= list_empty(&ring
->request_list
);
1632 list_add_tail(&request
->list
, &ring
->request_list
);
1634 if (i915_file_priv
) {
1635 list_add_tail(&request
->client_list
,
1636 &i915_file_priv
->mm
.request_list
);
1638 INIT_LIST_HEAD(&request
->client_list
);
1641 if (!dev_priv
->mm
.suspended
) {
1642 mod_timer(&dev_priv
->hangcheck_timer
,
1643 jiffies
+ msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD
));
1645 queue_delayed_work(dev_priv
->wq
,
1646 &dev_priv
->mm
.retire_work
, HZ
);
1652 * Command execution barrier
1654 * Ensures that all commands in the ring are finished
1655 * before signalling the CPU
1658 i915_retire_commands(struct drm_device
*dev
, struct intel_ring_buffer
*ring
)
1660 uint32_t flush_domains
= 0;
1662 /* The sampler always gets flushed on i965 (sigh) */
1664 flush_domains
|= I915_GEM_DOMAIN_SAMPLER
;
1666 ring
->flush(dev
, ring
,
1667 I915_GEM_DOMAIN_COMMAND
, flush_domains
);
1671 * Moves buffers associated only with the given active seqno from the active
1672 * to inactive list, potentially freeing them.
1675 i915_gem_retire_request(struct drm_device
*dev
,
1676 struct drm_i915_gem_request
*request
)
1678 trace_i915_gem_request_retire(dev
, request
->seqno
);
1680 /* Move any buffers on the active list that are no longer referenced
1681 * by the ringbuffer to the flushing/inactive lists as appropriate.
1683 while (!list_empty(&request
->ring
->active_list
)) {
1684 struct drm_gem_object
*obj
;
1685 struct drm_i915_gem_object
*obj_priv
;
1687 obj_priv
= list_first_entry(&request
->ring
->active_list
,
1688 struct drm_i915_gem_object
,
1690 obj
= &obj_priv
->base
;
1692 /* If the seqno being retired doesn't match the oldest in the
1693 * list, then the oldest in the list must still be newer than
1696 if (obj_priv
->last_rendering_seqno
!= request
->seqno
)
1700 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1701 __func__
, request
->seqno
, obj
);
1704 if (obj
->write_domain
!= 0)
1705 i915_gem_object_move_to_flushing(obj
);
1707 i915_gem_object_move_to_inactive(obj
);
1712 * Returns true if seq1 is later than seq2.
1715 i915_seqno_passed(uint32_t seq1
, uint32_t seq2
)
1717 return (int32_t)(seq1
- seq2
) >= 0;
1721 i915_get_gem_seqno(struct drm_device
*dev
,
1722 struct intel_ring_buffer
*ring
)
1724 return ring
->get_gem_seqno(dev
, ring
);
1728 * This function clears the request list as sequence numbers are passed.
1731 i915_gem_retire_requests_ring(struct drm_device
*dev
,
1732 struct intel_ring_buffer
*ring
)
1734 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1737 if (!ring
->status_page
.page_addr
1738 || list_empty(&ring
->request_list
))
1741 seqno
= i915_get_gem_seqno(dev
, ring
);
1743 while (!list_empty(&ring
->request_list
)) {
1744 struct drm_i915_gem_request
*request
;
1745 uint32_t retiring_seqno
;
1747 request
= list_first_entry(&ring
->request_list
,
1748 struct drm_i915_gem_request
,
1750 retiring_seqno
= request
->seqno
;
1752 if (i915_seqno_passed(seqno
, retiring_seqno
) ||
1753 atomic_read(&dev_priv
->mm
.wedged
)) {
1754 i915_gem_retire_request(dev
, request
);
1756 list_del(&request
->list
);
1757 list_del(&request
->client_list
);
1763 if (unlikely (dev_priv
->trace_irq_seqno
&&
1764 i915_seqno_passed(dev_priv
->trace_irq_seqno
, seqno
))) {
1766 ring
->user_irq_put(dev
, ring
);
1767 dev_priv
->trace_irq_seqno
= 0;
1772 i915_gem_retire_requests(struct drm_device
*dev
)
1774 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1776 if (!list_empty(&dev_priv
->mm
.deferred_free_list
)) {
1777 struct drm_i915_gem_object
*obj_priv
, *tmp
;
1779 /* We must be careful that during unbind() we do not
1780 * accidentally infinitely recurse into retire requests.
1782 * retire -> free -> unbind -> wait -> retire_ring
1784 list_for_each_entry_safe(obj_priv
, tmp
,
1785 &dev_priv
->mm
.deferred_free_list
,
1787 i915_gem_free_object_tail(&obj_priv
->base
);
1790 i915_gem_retire_requests_ring(dev
, &dev_priv
->render_ring
);
1792 i915_gem_retire_requests_ring(dev
, &dev_priv
->bsd_ring
);
1796 i915_gem_retire_work_handler(struct work_struct
*work
)
1798 drm_i915_private_t
*dev_priv
;
1799 struct drm_device
*dev
;
1801 dev_priv
= container_of(work
, drm_i915_private_t
,
1802 mm
.retire_work
.work
);
1803 dev
= dev_priv
->dev
;
1805 mutex_lock(&dev
->struct_mutex
);
1806 i915_gem_retire_requests(dev
);
1808 if (!dev_priv
->mm
.suspended
&&
1809 (!list_empty(&dev_priv
->render_ring
.request_list
) ||
1811 !list_empty(&dev_priv
->bsd_ring
.request_list
))))
1812 queue_delayed_work(dev_priv
->wq
, &dev_priv
->mm
.retire_work
, HZ
);
1813 mutex_unlock(&dev
->struct_mutex
);
1817 i915_do_wait_request(struct drm_device
*dev
, uint32_t seqno
,
1818 bool interruptible
, struct intel_ring_buffer
*ring
)
1820 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1826 if (seqno
== dev_priv
->next_seqno
) {
1827 seqno
= i915_add_request(dev
, NULL
, NULL
, ring
);
1832 if (atomic_read(&dev_priv
->mm
.wedged
))
1835 if (!i915_seqno_passed(ring
->get_gem_seqno(dev
, ring
), seqno
)) {
1836 if (HAS_PCH_SPLIT(dev
))
1837 ier
= I915_READ(DEIER
) | I915_READ(GTIER
);
1839 ier
= I915_READ(IER
);
1841 DRM_ERROR("something (likely vbetool) disabled "
1842 "interrupts, re-enabling\n");
1843 i915_driver_irq_preinstall(dev
);
1844 i915_driver_irq_postinstall(dev
);
1847 trace_i915_gem_request_wait_begin(dev
, seqno
);
1849 ring
->waiting_gem_seqno
= seqno
;
1850 ring
->user_irq_get(dev
, ring
);
1852 ret
= wait_event_interruptible(ring
->irq_queue
,
1854 ring
->get_gem_seqno(dev
, ring
), seqno
)
1855 || atomic_read(&dev_priv
->mm
.wedged
));
1857 wait_event(ring
->irq_queue
,
1859 ring
->get_gem_seqno(dev
, ring
), seqno
)
1860 || atomic_read(&dev_priv
->mm
.wedged
));
1862 ring
->user_irq_put(dev
, ring
);
1863 ring
->waiting_gem_seqno
= 0;
1865 trace_i915_gem_request_wait_end(dev
, seqno
);
1867 if (atomic_read(&dev_priv
->mm
.wedged
))
1870 if (ret
&& ret
!= -ERESTARTSYS
)
1871 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
1872 __func__
, ret
, seqno
, ring
->get_gem_seqno(dev
, ring
),
1873 dev_priv
->next_seqno
);
1875 /* Directly dispatch request retiring. While we have the work queue
1876 * to handle this, the waiter on a request often wants an associated
1877 * buffer to have made it to the inactive list, and we would need
1878 * a separate wait queue to handle that.
1881 i915_gem_retire_requests_ring(dev
, ring
);
1887 * Waits for a sequence number to be signaled, and cleans up the
1888 * request and object lists appropriately for that event.
1891 i915_wait_request(struct drm_device
*dev
, uint32_t seqno
,
1892 struct intel_ring_buffer
*ring
)
1894 return i915_do_wait_request(dev
, seqno
, 1, ring
);
1898 i915_gem_flush(struct drm_device
*dev
,
1899 uint32_t invalidate_domains
,
1900 uint32_t flush_domains
)
1902 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1904 if (flush_domains
& I915_GEM_DOMAIN_CPU
)
1905 drm_agp_chipset_flush(dev
);
1907 dev_priv
->render_ring
.flush(dev
, &dev_priv
->render_ring
,
1912 dev_priv
->bsd_ring
.flush(dev
, &dev_priv
->bsd_ring
,
1918 * Ensures that all rendering to the object has completed and the object is
1919 * safe to unbind from the GTT or access from the CPU.
1922 i915_gem_object_wait_rendering(struct drm_gem_object
*obj
)
1924 struct drm_device
*dev
= obj
->dev
;
1925 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1928 /* This function only exists to support waiting for existing rendering,
1929 * not for emitting required flushes.
1931 BUG_ON((obj
->write_domain
& I915_GEM_GPU_DOMAINS
) != 0);
1933 /* If there is rendering queued on the buffer being evicted, wait for
1936 if (obj_priv
->active
) {
1938 DRM_INFO("%s: object %p wait for seqno %08x\n",
1939 __func__
, obj
, obj_priv
->last_rendering_seqno
);
1941 ret
= i915_wait_request(dev
,
1942 obj_priv
->last_rendering_seqno
,
1952 * Unbinds an object from the GTT aperture.
1955 i915_gem_object_unbind(struct drm_gem_object
*obj
)
1957 struct drm_device
*dev
= obj
->dev
;
1958 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1962 DRM_INFO("%s:%d %p\n", __func__
, __LINE__
, obj
);
1963 DRM_INFO("gtt_space %p\n", obj_priv
->gtt_space
);
1965 if (obj_priv
->gtt_space
== NULL
)
1968 if (obj_priv
->pin_count
!= 0) {
1969 DRM_ERROR("Attempting to unbind pinned buffer\n");
1973 /* blow away mappings if mapped through GTT */
1974 i915_gem_release_mmap(obj
);
1976 /* Move the object to the CPU domain to ensure that
1977 * any possible CPU writes while it's not in the GTT
1978 * are flushed when we go to remap it. This will
1979 * also ensure that all pending GPU writes are finished
1982 ret
= i915_gem_object_set_to_cpu_domain(obj
, 1);
1983 if (ret
== -ERESTARTSYS
)
1985 /* Continue on if we fail due to EIO, the GPU is hung so we
1986 * should be safe and we need to cleanup or else we might
1987 * cause memory corruption through use-after-free.
1990 /* release the fence reg _after_ flushing */
1991 if (obj_priv
->fence_reg
!= I915_FENCE_REG_NONE
)
1992 i915_gem_clear_fence_reg(obj
);
1994 if (obj_priv
->agp_mem
!= NULL
) {
1995 drm_unbind_agp(obj_priv
->agp_mem
);
1996 drm_free_agp(obj_priv
->agp_mem
, obj
->size
/ PAGE_SIZE
);
1997 obj_priv
->agp_mem
= NULL
;
2000 i915_gem_object_put_pages(obj
);
2001 BUG_ON(obj_priv
->pages_refcount
);
2003 if (obj_priv
->gtt_space
) {
2004 atomic_dec(&dev
->gtt_count
);
2005 atomic_sub(obj
->size
, &dev
->gtt_memory
);
2007 drm_mm_put_block(obj_priv
->gtt_space
);
2008 obj_priv
->gtt_space
= NULL
;
2011 /* Remove ourselves from the LRU list if present. */
2012 if (!list_empty(&obj_priv
->list
))
2013 list_del_init(&obj_priv
->list
);
2015 if (i915_gem_object_is_purgeable(obj_priv
))
2016 i915_gem_object_truncate(obj
);
2018 trace_i915_gem_object_unbind(obj
);
2024 i915_gpu_idle(struct drm_device
*dev
)
2026 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2030 lists_empty
= (list_empty(&dev_priv
->mm
.flushing_list
) &&
2031 list_empty(&dev_priv
->render_ring
.active_list
) &&
2033 list_empty(&dev_priv
->bsd_ring
.active_list
)));
2037 /* Flush everything onto the inactive list. */
2038 i915_gem_flush(dev
, I915_GEM_GPU_DOMAINS
, I915_GEM_GPU_DOMAINS
);
2040 ret
= i915_wait_request(dev
,
2041 i915_gem_next_request_seqno(dev
, &dev_priv
->render_ring
),
2042 &dev_priv
->render_ring
);
2047 ret
= i915_wait_request(dev
,
2048 i915_gem_next_request_seqno(dev
, &dev_priv
->bsd_ring
),
2049 &dev_priv
->bsd_ring
);
2058 i915_gem_object_get_pages(struct drm_gem_object
*obj
,
2061 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2063 struct address_space
*mapping
;
2064 struct inode
*inode
;
2067 BUG_ON(obj_priv
->pages_refcount
2068 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT
);
2070 if (obj_priv
->pages_refcount
++ != 0)
2073 /* Get the list of pages out of our struct file. They'll be pinned
2074 * at this point until we release them.
2076 page_count
= obj
->size
/ PAGE_SIZE
;
2077 BUG_ON(obj_priv
->pages
!= NULL
);
2078 obj_priv
->pages
= drm_calloc_large(page_count
, sizeof(struct page
*));
2079 if (obj_priv
->pages
== NULL
) {
2080 obj_priv
->pages_refcount
--;
2084 inode
= obj
->filp
->f_path
.dentry
->d_inode
;
2085 mapping
= inode
->i_mapping
;
2086 for (i
= 0; i
< page_count
; i
++) {
2087 page
= read_cache_page_gfp(mapping
, i
,
2095 obj_priv
->pages
[i
] = page
;
2098 if (obj_priv
->tiling_mode
!= I915_TILING_NONE
)
2099 i915_gem_object_do_bit_17_swizzle(obj
);
2105 page_cache_release(obj_priv
->pages
[i
]);
2107 drm_free_large(obj_priv
->pages
);
2108 obj_priv
->pages
= NULL
;
2109 obj_priv
->pages_refcount
--;
2110 return PTR_ERR(page
);
2113 static void sandybridge_write_fence_reg(struct drm_i915_fence_reg
*reg
)
2115 struct drm_gem_object
*obj
= reg
->obj
;
2116 struct drm_device
*dev
= obj
->dev
;
2117 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2118 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2119 int regnum
= obj_priv
->fence_reg
;
2122 val
= (uint64_t)((obj_priv
->gtt_offset
+ obj
->size
- 4096) &
2124 val
|= obj_priv
->gtt_offset
& 0xfffff000;
2125 val
|= (uint64_t)((obj_priv
->stride
/ 128) - 1) <<
2126 SANDYBRIDGE_FENCE_PITCH_SHIFT
;
2128 if (obj_priv
->tiling_mode
== I915_TILING_Y
)
2129 val
|= 1 << I965_FENCE_TILING_Y_SHIFT
;
2130 val
|= I965_FENCE_REG_VALID
;
2132 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0
+ (regnum
* 8), val
);
2135 static void i965_write_fence_reg(struct drm_i915_fence_reg
*reg
)
2137 struct drm_gem_object
*obj
= reg
->obj
;
2138 struct drm_device
*dev
= obj
->dev
;
2139 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2140 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2141 int regnum
= obj_priv
->fence_reg
;
2144 val
= (uint64_t)((obj_priv
->gtt_offset
+ obj
->size
- 4096) &
2146 val
|= obj_priv
->gtt_offset
& 0xfffff000;
2147 val
|= ((obj_priv
->stride
/ 128) - 1) << I965_FENCE_PITCH_SHIFT
;
2148 if (obj_priv
->tiling_mode
== I915_TILING_Y
)
2149 val
|= 1 << I965_FENCE_TILING_Y_SHIFT
;
2150 val
|= I965_FENCE_REG_VALID
;
2152 I915_WRITE64(FENCE_REG_965_0
+ (regnum
* 8), val
);
2155 static void i915_write_fence_reg(struct drm_i915_fence_reg
*reg
)
2157 struct drm_gem_object
*obj
= reg
->obj
;
2158 struct drm_device
*dev
= obj
->dev
;
2159 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2160 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2161 int regnum
= obj_priv
->fence_reg
;
2163 uint32_t fence_reg
, val
;
2166 if ((obj_priv
->gtt_offset
& ~I915_FENCE_START_MASK
) ||
2167 (obj_priv
->gtt_offset
& (obj
->size
- 1))) {
2168 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
2169 __func__
, obj_priv
->gtt_offset
, obj
->size
);
2173 if (obj_priv
->tiling_mode
== I915_TILING_Y
&&
2174 HAS_128_BYTE_Y_TILING(dev
))
2179 /* Note: pitch better be a power of two tile widths */
2180 pitch_val
= obj_priv
->stride
/ tile_width
;
2181 pitch_val
= ffs(pitch_val
) - 1;
2183 if (obj_priv
->tiling_mode
== I915_TILING_Y
&&
2184 HAS_128_BYTE_Y_TILING(dev
))
2185 WARN_ON(pitch_val
> I830_FENCE_MAX_PITCH_VAL
);
2187 WARN_ON(pitch_val
> I915_FENCE_MAX_PITCH_VAL
);
2189 val
= obj_priv
->gtt_offset
;
2190 if (obj_priv
->tiling_mode
== I915_TILING_Y
)
2191 val
|= 1 << I830_FENCE_TILING_Y_SHIFT
;
2192 val
|= I915_FENCE_SIZE_BITS(obj
->size
);
2193 val
|= pitch_val
<< I830_FENCE_PITCH_SHIFT
;
2194 val
|= I830_FENCE_REG_VALID
;
2197 fence_reg
= FENCE_REG_830_0
+ (regnum
* 4);
2199 fence_reg
= FENCE_REG_945_8
+ ((regnum
- 8) * 4);
2200 I915_WRITE(fence_reg
, val
);
2203 static void i830_write_fence_reg(struct drm_i915_fence_reg
*reg
)
2205 struct drm_gem_object
*obj
= reg
->obj
;
2206 struct drm_device
*dev
= obj
->dev
;
2207 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2208 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2209 int regnum
= obj_priv
->fence_reg
;
2212 uint32_t fence_size_bits
;
2214 if ((obj_priv
->gtt_offset
& ~I830_FENCE_START_MASK
) ||
2215 (obj_priv
->gtt_offset
& (obj
->size
- 1))) {
2216 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
2217 __func__
, obj_priv
->gtt_offset
);
2221 pitch_val
= obj_priv
->stride
/ 128;
2222 pitch_val
= ffs(pitch_val
) - 1;
2223 WARN_ON(pitch_val
> I830_FENCE_MAX_PITCH_VAL
);
2225 val
= obj_priv
->gtt_offset
;
2226 if (obj_priv
->tiling_mode
== I915_TILING_Y
)
2227 val
|= 1 << I830_FENCE_TILING_Y_SHIFT
;
2228 fence_size_bits
= I830_FENCE_SIZE_BITS(obj
->size
);
2229 WARN_ON(fence_size_bits
& ~0x00000f00);
2230 val
|= fence_size_bits
;
2231 val
|= pitch_val
<< I830_FENCE_PITCH_SHIFT
;
2232 val
|= I830_FENCE_REG_VALID
;
2234 I915_WRITE(FENCE_REG_830_0
+ (regnum
* 4), val
);
2237 static int i915_find_fence_reg(struct drm_device
*dev
)
2239 struct drm_i915_fence_reg
*reg
= NULL
;
2240 struct drm_i915_gem_object
*obj_priv
= NULL
;
2241 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
2242 struct drm_gem_object
*obj
= NULL
;
2245 /* First try to find a free reg */
2247 for (i
= dev_priv
->fence_reg_start
; i
< dev_priv
->num_fence_regs
; i
++) {
2248 reg
= &dev_priv
->fence_regs
[i
];
2252 obj_priv
= to_intel_bo(reg
->obj
);
2253 if (!obj_priv
->pin_count
)
2260 /* None available, try to steal one or wait for a user to finish */
2261 i
= I915_FENCE_REG_NONE
;
2262 list_for_each_entry(reg
, &dev_priv
->mm
.fence_list
,
2265 obj_priv
= to_intel_bo(obj
);
2267 if (obj_priv
->pin_count
)
2271 i
= obj_priv
->fence_reg
;
2275 BUG_ON(i
== I915_FENCE_REG_NONE
);
2277 /* We only have a reference on obj from the active list. put_fence_reg
2278 * might drop that one, causing a use-after-free in it. So hold a
2279 * private reference to obj like the other callers of put_fence_reg
2280 * (set_tiling ioctl) do. */
2281 drm_gem_object_reference(obj
);
2282 ret
= i915_gem_object_put_fence_reg(obj
);
2283 drm_gem_object_unreference(obj
);
2291 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2292 * @obj: object to map through a fence reg
2294 * When mapping objects through the GTT, userspace wants to be able to write
2295 * to them without having to worry about swizzling if the object is tiled.
2297 * This function walks the fence regs looking for a free one for @obj,
2298 * stealing one if it can't find any.
2300 * It then sets up the reg based on the object's properties: address, pitch
2301 * and tiling format.
2304 i915_gem_object_get_fence_reg(struct drm_gem_object
*obj
)
2306 struct drm_device
*dev
= obj
->dev
;
2307 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
2308 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2309 struct drm_i915_fence_reg
*reg
= NULL
;
2312 /* Just update our place in the LRU if our fence is getting used. */
2313 if (obj_priv
->fence_reg
!= I915_FENCE_REG_NONE
) {
2314 reg
= &dev_priv
->fence_regs
[obj_priv
->fence_reg
];
2315 list_move_tail(®
->lru_list
, &dev_priv
->mm
.fence_list
);
2319 switch (obj_priv
->tiling_mode
) {
2320 case I915_TILING_NONE
:
2321 WARN(1, "allocating a fence for non-tiled object?\n");
2324 if (!obj_priv
->stride
)
2326 WARN((obj_priv
->stride
& (512 - 1)),
2327 "object 0x%08x is X tiled but has non-512B pitch\n",
2328 obj_priv
->gtt_offset
);
2331 if (!obj_priv
->stride
)
2333 WARN((obj_priv
->stride
& (128 - 1)),
2334 "object 0x%08x is Y tiled but has non-128B pitch\n",
2335 obj_priv
->gtt_offset
);
2339 ret
= i915_find_fence_reg(dev
);
2343 obj_priv
->fence_reg
= ret
;
2344 reg
= &dev_priv
->fence_regs
[obj_priv
->fence_reg
];
2345 list_add_tail(®
->lru_list
, &dev_priv
->mm
.fence_list
);
2350 sandybridge_write_fence_reg(reg
);
2351 else if (IS_I965G(dev
))
2352 i965_write_fence_reg(reg
);
2353 else if (IS_I9XX(dev
))
2354 i915_write_fence_reg(reg
);
2356 i830_write_fence_reg(reg
);
2358 trace_i915_gem_object_get_fence(obj
, obj_priv
->fence_reg
,
2359 obj_priv
->tiling_mode
);
2365 * i915_gem_clear_fence_reg - clear out fence register info
2366 * @obj: object to clear
2368 * Zeroes out the fence register itself and clears out the associated
2369 * data structures in dev_priv and obj_priv.
2372 i915_gem_clear_fence_reg(struct drm_gem_object
*obj
)
2374 struct drm_device
*dev
= obj
->dev
;
2375 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2376 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2377 struct drm_i915_fence_reg
*reg
=
2378 &dev_priv
->fence_regs
[obj_priv
->fence_reg
];
2381 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0
+
2382 (obj_priv
->fence_reg
* 8), 0);
2383 } else if (IS_I965G(dev
)) {
2384 I915_WRITE64(FENCE_REG_965_0
+ (obj_priv
->fence_reg
* 8), 0);
2388 if (obj_priv
->fence_reg
< 8)
2389 fence_reg
= FENCE_REG_830_0
+ obj_priv
->fence_reg
* 4;
2391 fence_reg
= FENCE_REG_945_8
+ (obj_priv
->fence_reg
-
2394 I915_WRITE(fence_reg
, 0);
2398 obj_priv
->fence_reg
= I915_FENCE_REG_NONE
;
2399 list_del_init(®
->lru_list
);
2403 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2404 * to the buffer to finish, and then resets the fence register.
2405 * @obj: tiled object holding a fence register.
2407 * Zeroes out the fence register itself and clears out the associated
2408 * data structures in dev_priv and obj_priv.
2411 i915_gem_object_put_fence_reg(struct drm_gem_object
*obj
)
2413 struct drm_device
*dev
= obj
->dev
;
2414 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2416 if (obj_priv
->fence_reg
== I915_FENCE_REG_NONE
)
2419 /* If we've changed tiling, GTT-mappings of the object
2420 * need to re-fault to ensure that the correct fence register
2421 * setup is in place.
2423 i915_gem_release_mmap(obj
);
2425 /* On the i915, GPU access to tiled buffers is via a fence,
2426 * therefore we must wait for any outstanding access to complete
2427 * before clearing the fence.
2429 if (!IS_I965G(dev
)) {
2432 ret
= i915_gem_object_flush_gpu_write_domain(obj
, false);
2437 i915_gem_object_flush_gtt_write_domain(obj
);
2438 i915_gem_clear_fence_reg (obj
);
2444 * Finds free space in the GTT aperture and binds the object there.
2447 i915_gem_object_bind_to_gtt(struct drm_gem_object
*obj
, unsigned alignment
)
2449 struct drm_device
*dev
= obj
->dev
;
2450 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2451 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2452 struct drm_mm_node
*free_space
;
2453 gfp_t gfpmask
= __GFP_NORETRY
| __GFP_NOWARN
;
2456 if (obj_priv
->madv
!= I915_MADV_WILLNEED
) {
2457 DRM_ERROR("Attempting to bind a purgeable object\n");
2462 alignment
= i915_gem_get_gtt_alignment(obj
);
2463 if (alignment
& (i915_gem_get_gtt_alignment(obj
) - 1)) {
2464 DRM_ERROR("Invalid object alignment requested %u\n", alignment
);
2468 /* If the object is bigger than the entire aperture, reject it early
2469 * before evicting everything in a vain attempt to find space.
2471 if (obj
->size
> dev
->gtt_total
) {
2472 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2477 free_space
= drm_mm_search_free(&dev_priv
->mm
.gtt_space
,
2478 obj
->size
, alignment
, 0);
2479 if (free_space
!= NULL
) {
2480 obj_priv
->gtt_space
= drm_mm_get_block(free_space
, obj
->size
,
2482 if (obj_priv
->gtt_space
!= NULL
)
2483 obj_priv
->gtt_offset
= obj_priv
->gtt_space
->start
;
2485 if (obj_priv
->gtt_space
== NULL
) {
2486 /* If the gtt is empty and we're still having trouble
2487 * fitting our object in, we're out of memory.
2490 DRM_INFO("%s: GTT full, evicting something\n", __func__
);
2492 ret
= i915_gem_evict_something(dev
, obj
->size
, alignment
);
2500 DRM_INFO("Binding object of size %zd at 0x%08x\n",
2501 obj
->size
, obj_priv
->gtt_offset
);
2503 ret
= i915_gem_object_get_pages(obj
, gfpmask
);
2505 drm_mm_put_block(obj_priv
->gtt_space
);
2506 obj_priv
->gtt_space
= NULL
;
2508 if (ret
== -ENOMEM
) {
2509 /* first try to clear up some space from the GTT */
2510 ret
= i915_gem_evict_something(dev
, obj
->size
,
2513 /* now try to shrink everyone else */
2528 /* Create an AGP memory structure pointing at our pages, and bind it
2531 obj_priv
->agp_mem
= drm_agp_bind_pages(dev
,
2533 obj
->size
>> PAGE_SHIFT
,
2534 obj_priv
->gtt_offset
,
2535 obj_priv
->agp_type
);
2536 if (obj_priv
->agp_mem
== NULL
) {
2537 i915_gem_object_put_pages(obj
);
2538 drm_mm_put_block(obj_priv
->gtt_space
);
2539 obj_priv
->gtt_space
= NULL
;
2541 ret
= i915_gem_evict_something(dev
, obj
->size
, alignment
);
2547 atomic_inc(&dev
->gtt_count
);
2548 atomic_add(obj
->size
, &dev
->gtt_memory
);
2550 /* keep track of bounds object by adding it to the inactive list */
2551 list_add_tail(&obj_priv
->list
, &dev_priv
->mm
.inactive_list
);
2553 /* Assert that the object is not currently in any GPU domain. As it
2554 * wasn't in the GTT, there shouldn't be any way it could have been in
2557 BUG_ON(obj
->read_domains
& I915_GEM_GPU_DOMAINS
);
2558 BUG_ON(obj
->write_domain
& I915_GEM_GPU_DOMAINS
);
2560 trace_i915_gem_object_bind(obj
, obj_priv
->gtt_offset
);
2566 i915_gem_clflush_object(struct drm_gem_object
*obj
)
2568 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2570 /* If we don't have a page list set up, then we're not pinned
2571 * to GPU, and we can ignore the cache flush because it'll happen
2572 * again at bind time.
2574 if (obj_priv
->pages
== NULL
)
2577 trace_i915_gem_object_clflush(obj
);
2579 drm_clflush_pages(obj_priv
->pages
, obj
->size
/ PAGE_SIZE
);
2582 /** Flushes any GPU write domain for the object if it's dirty. */
2584 i915_gem_object_flush_gpu_write_domain(struct drm_gem_object
*obj
,
2587 struct drm_device
*dev
= obj
->dev
;
2588 uint32_t old_write_domain
;
2590 if ((obj
->write_domain
& I915_GEM_GPU_DOMAINS
) == 0)
2593 /* Queue the GPU write cache flushing we need. */
2594 old_write_domain
= obj
->write_domain
;
2595 i915_gem_flush(dev
, 0, obj
->write_domain
);
2597 trace_i915_gem_object_change_domain(obj
,
2604 return i915_gem_object_wait_rendering(obj
);
2607 /** Flushes the GTT write domain for the object if it's dirty. */
2609 i915_gem_object_flush_gtt_write_domain(struct drm_gem_object
*obj
)
2611 uint32_t old_write_domain
;
2613 if (obj
->write_domain
!= I915_GEM_DOMAIN_GTT
)
2616 /* No actual flushing is required for the GTT write domain. Writes
2617 * to it immediately go to main memory as far as we know, so there's
2618 * no chipset flush. It also doesn't land in render cache.
2620 old_write_domain
= obj
->write_domain
;
2621 obj
->write_domain
= 0;
2623 trace_i915_gem_object_change_domain(obj
,
2628 /** Flushes the CPU write domain for the object if it's dirty. */
2630 i915_gem_object_flush_cpu_write_domain(struct drm_gem_object
*obj
)
2632 struct drm_device
*dev
= obj
->dev
;
2633 uint32_t old_write_domain
;
2635 if (obj
->write_domain
!= I915_GEM_DOMAIN_CPU
)
2638 i915_gem_clflush_object(obj
);
2639 drm_agp_chipset_flush(dev
);
2640 old_write_domain
= obj
->write_domain
;
2641 obj
->write_domain
= 0;
2643 trace_i915_gem_object_change_domain(obj
,
2649 * Moves a single object to the GTT read, and possibly write domain.
2651 * This function returns when the move is complete, including waiting on
2655 i915_gem_object_set_to_gtt_domain(struct drm_gem_object
*obj
, int write
)
2657 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2658 uint32_t old_write_domain
, old_read_domains
;
2661 /* Not valid to be called on unbound objects. */
2662 if (obj_priv
->gtt_space
== NULL
)
2665 ret
= i915_gem_object_flush_gpu_write_domain(obj
, false);
2669 i915_gem_object_flush_cpu_write_domain(obj
);
2672 ret
= i915_gem_object_wait_rendering(obj
);
2677 old_write_domain
= obj
->write_domain
;
2678 old_read_domains
= obj
->read_domains
;
2680 /* It should now be out of any other write domains, and we can update
2681 * the domain values for our changes.
2683 BUG_ON((obj
->write_domain
& ~I915_GEM_DOMAIN_GTT
) != 0);
2684 obj
->read_domains
|= I915_GEM_DOMAIN_GTT
;
2686 obj
->read_domains
= I915_GEM_DOMAIN_GTT
;
2687 obj
->write_domain
= I915_GEM_DOMAIN_GTT
;
2688 obj_priv
->dirty
= 1;
2691 trace_i915_gem_object_change_domain(obj
,
2699 * Prepare buffer for display plane. Use uninterruptible for possible flush
2700 * wait, as in modesetting process we're not supposed to be interrupted.
2703 i915_gem_object_set_to_display_plane(struct drm_gem_object
*obj
)
2705 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2706 uint32_t old_read_domains
;
2709 /* Not valid to be called on unbound objects. */
2710 if (obj_priv
->gtt_space
== NULL
)
2713 ret
= i915_gem_object_flush_gpu_write_domain(obj
, true);
2717 i915_gem_object_flush_cpu_write_domain(obj
);
2719 old_read_domains
= obj
->read_domains
;
2720 obj
->read_domains
= I915_GEM_DOMAIN_GTT
;
2722 trace_i915_gem_object_change_domain(obj
,
2730 * Moves a single object to the CPU read, and possibly write domain.
2732 * This function returns when the move is complete, including waiting on
2736 i915_gem_object_set_to_cpu_domain(struct drm_gem_object
*obj
, int write
)
2738 uint32_t old_write_domain
, old_read_domains
;
2741 ret
= i915_gem_object_flush_gpu_write_domain(obj
, false);
2745 i915_gem_object_flush_gtt_write_domain(obj
);
2747 /* If we have a partially-valid cache of the object in the CPU,
2748 * finish invalidating it and free the per-page flags.
2750 i915_gem_object_set_to_full_cpu_read_domain(obj
);
2753 ret
= i915_gem_object_wait_rendering(obj
);
2758 old_write_domain
= obj
->write_domain
;
2759 old_read_domains
= obj
->read_domains
;
2761 /* Flush the CPU cache if it's still invalid. */
2762 if ((obj
->read_domains
& I915_GEM_DOMAIN_CPU
) == 0) {
2763 i915_gem_clflush_object(obj
);
2765 obj
->read_domains
|= I915_GEM_DOMAIN_CPU
;
2768 /* It should now be out of any other write domains, and we can update
2769 * the domain values for our changes.
2771 BUG_ON((obj
->write_domain
& ~I915_GEM_DOMAIN_CPU
) != 0);
2773 /* If we're writing through the CPU, then the GPU read domains will
2774 * need to be invalidated at next use.
2777 obj
->read_domains
&= I915_GEM_DOMAIN_CPU
;
2778 obj
->write_domain
= I915_GEM_DOMAIN_CPU
;
2781 trace_i915_gem_object_change_domain(obj
,
2789 * Set the next domain for the specified object. This
2790 * may not actually perform the necessary flushing/invaliding though,
2791 * as that may want to be batched with other set_domain operations
2793 * This is (we hope) the only really tricky part of gem. The goal
2794 * is fairly simple -- track which caches hold bits of the object
2795 * and make sure they remain coherent. A few concrete examples may
2796 * help to explain how it works. For shorthand, we use the notation
2797 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2798 * a pair of read and write domain masks.
2800 * Case 1: the batch buffer
2806 * 5. Unmapped from GTT
2809 * Let's take these a step at a time
2812 * Pages allocated from the kernel may still have
2813 * cache contents, so we set them to (CPU, CPU) always.
2814 * 2. Written by CPU (using pwrite)
2815 * The pwrite function calls set_domain (CPU, CPU) and
2816 * this function does nothing (as nothing changes)
2818 * This function asserts that the object is not
2819 * currently in any GPU-based read or write domains
2821 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2822 * As write_domain is zero, this function adds in the
2823 * current read domains (CPU+COMMAND, 0).
2824 * flush_domains is set to CPU.
2825 * invalidate_domains is set to COMMAND
2826 * clflush is run to get data out of the CPU caches
2827 * then i915_dev_set_domain calls i915_gem_flush to
2828 * emit an MI_FLUSH and drm_agp_chipset_flush
2829 * 5. Unmapped from GTT
2830 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2831 * flush_domains and invalidate_domains end up both zero
2832 * so no flushing/invalidating happens
2836 * Case 2: The shared render buffer
2840 * 3. Read/written by GPU
2841 * 4. set_domain to (CPU,CPU)
2842 * 5. Read/written by CPU
2843 * 6. Read/written by GPU
2846 * Same as last example, (CPU, CPU)
2848 * Nothing changes (assertions find that it is not in the GPU)
2849 * 3. Read/written by GPU
2850 * execbuffer calls set_domain (RENDER, RENDER)
2851 * flush_domains gets CPU
2852 * invalidate_domains gets GPU
2854 * MI_FLUSH and drm_agp_chipset_flush
2855 * 4. set_domain (CPU, CPU)
2856 * flush_domains gets GPU
2857 * invalidate_domains gets CPU
2858 * wait_rendering (obj) to make sure all drawing is complete.
2859 * This will include an MI_FLUSH to get the data from GPU
2861 * clflush (obj) to invalidate the CPU cache
2862 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2863 * 5. Read/written by CPU
2864 * cache lines are loaded and dirtied
2865 * 6. Read written by GPU
2866 * Same as last GPU access
2868 * Case 3: The constant buffer
2873 * 4. Updated (written) by CPU again
2882 * flush_domains = CPU
2883 * invalidate_domains = RENDER
2886 * drm_agp_chipset_flush
2887 * 4. Updated (written) by CPU again
2889 * flush_domains = 0 (no previous write domain)
2890 * invalidate_domains = 0 (no new read domains)
2893 * flush_domains = CPU
2894 * invalidate_domains = RENDER
2897 * drm_agp_chipset_flush
2900 i915_gem_object_set_to_gpu_domain(struct drm_gem_object
*obj
)
2902 struct drm_device
*dev
= obj
->dev
;
2903 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2904 uint32_t invalidate_domains
= 0;
2905 uint32_t flush_domains
= 0;
2906 uint32_t old_read_domains
;
2908 BUG_ON(obj
->pending_read_domains
& I915_GEM_DOMAIN_CPU
);
2909 BUG_ON(obj
->pending_write_domain
== I915_GEM_DOMAIN_CPU
);
2911 intel_mark_busy(dev
, obj
);
2914 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2916 obj
->read_domains
, obj
->pending_read_domains
,
2917 obj
->write_domain
, obj
->pending_write_domain
);
2920 * If the object isn't moving to a new write domain,
2921 * let the object stay in multiple read domains
2923 if (obj
->pending_write_domain
== 0)
2924 obj
->pending_read_domains
|= obj
->read_domains
;
2926 obj_priv
->dirty
= 1;
2929 * Flush the current write domain if
2930 * the new read domains don't match. Invalidate
2931 * any read domains which differ from the old
2934 if (obj
->write_domain
&&
2935 obj
->write_domain
!= obj
->pending_read_domains
) {
2936 flush_domains
|= obj
->write_domain
;
2937 invalidate_domains
|=
2938 obj
->pending_read_domains
& ~obj
->write_domain
;
2941 * Invalidate any read caches which may have
2942 * stale data. That is, any new read domains.
2944 invalidate_domains
|= obj
->pending_read_domains
& ~obj
->read_domains
;
2945 if ((flush_domains
| invalidate_domains
) & I915_GEM_DOMAIN_CPU
) {
2947 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2948 __func__
, flush_domains
, invalidate_domains
);
2950 i915_gem_clflush_object(obj
);
2953 old_read_domains
= obj
->read_domains
;
2955 /* The actual obj->write_domain will be updated with
2956 * pending_write_domain after we emit the accumulated flush for all
2957 * of our domain changes in execbuffers (which clears objects'
2958 * write_domains). So if we have a current write domain that we
2959 * aren't changing, set pending_write_domain to that.
2961 if (flush_domains
== 0 && obj
->pending_write_domain
== 0)
2962 obj
->pending_write_domain
= obj
->write_domain
;
2963 obj
->read_domains
= obj
->pending_read_domains
;
2965 dev
->invalidate_domains
|= invalidate_domains
;
2966 dev
->flush_domains
|= flush_domains
;
2968 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2970 obj
->read_domains
, obj
->write_domain
,
2971 dev
->invalidate_domains
, dev
->flush_domains
);
2974 trace_i915_gem_object_change_domain(obj
,
2980 * Moves the object from a partially CPU read to a full one.
2982 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2983 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2986 i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object
*obj
)
2988 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2990 if (!obj_priv
->page_cpu_valid
)
2993 /* If we're partially in the CPU read domain, finish moving it in.
2995 if (obj
->read_domains
& I915_GEM_DOMAIN_CPU
) {
2998 for (i
= 0; i
<= (obj
->size
- 1) / PAGE_SIZE
; i
++) {
2999 if (obj_priv
->page_cpu_valid
[i
])
3001 drm_clflush_pages(obj_priv
->pages
+ i
, 1);
3005 /* Free the page_cpu_valid mappings which are now stale, whether
3006 * or not we've got I915_GEM_DOMAIN_CPU.
3008 kfree(obj_priv
->page_cpu_valid
);
3009 obj_priv
->page_cpu_valid
= NULL
;
3013 * Set the CPU read domain on a range of the object.
3015 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3016 * not entirely valid. The page_cpu_valid member of the object flags which
3017 * pages have been flushed, and will be respected by
3018 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3019 * of the whole object.
3021 * This function returns when the move is complete, including waiting on
3025 i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object
*obj
,
3026 uint64_t offset
, uint64_t size
)
3028 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
3029 uint32_t old_read_domains
;
3032 if (offset
== 0 && size
== obj
->size
)
3033 return i915_gem_object_set_to_cpu_domain(obj
, 0);
3035 ret
= i915_gem_object_flush_gpu_write_domain(obj
, false);
3038 i915_gem_object_flush_gtt_write_domain(obj
);
3040 /* If we're already fully in the CPU read domain, we're done. */
3041 if (obj_priv
->page_cpu_valid
== NULL
&&
3042 (obj
->read_domains
& I915_GEM_DOMAIN_CPU
) != 0)
3045 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3046 * newly adding I915_GEM_DOMAIN_CPU
3048 if (obj_priv
->page_cpu_valid
== NULL
) {
3049 obj_priv
->page_cpu_valid
= kzalloc(obj
->size
/ PAGE_SIZE
,
3051 if (obj_priv
->page_cpu_valid
== NULL
)
3053 } else if ((obj
->read_domains
& I915_GEM_DOMAIN_CPU
) == 0)
3054 memset(obj_priv
->page_cpu_valid
, 0, obj
->size
/ PAGE_SIZE
);
3056 /* Flush the cache on any pages that are still invalid from the CPU's
3059 for (i
= offset
/ PAGE_SIZE
; i
<= (offset
+ size
- 1) / PAGE_SIZE
;
3061 if (obj_priv
->page_cpu_valid
[i
])
3064 drm_clflush_pages(obj_priv
->pages
+ i
, 1);
3066 obj_priv
->page_cpu_valid
[i
] = 1;
3069 /* It should now be out of any other write domains, and we can update
3070 * the domain values for our changes.
3072 BUG_ON((obj
->write_domain
& ~I915_GEM_DOMAIN_CPU
) != 0);
3074 old_read_domains
= obj
->read_domains
;
3075 obj
->read_domains
|= I915_GEM_DOMAIN_CPU
;
3077 trace_i915_gem_object_change_domain(obj
,
3085 * Pin an object to the GTT and evaluate the relocations landing in it.
3088 i915_gem_object_pin_and_relocate(struct drm_gem_object
*obj
,
3089 struct drm_file
*file_priv
,
3090 struct drm_i915_gem_exec_object2
*entry
,
3091 struct drm_i915_gem_relocation_entry
*relocs
)
3093 struct drm_device
*dev
= obj
->dev
;
3094 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
3095 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
3097 void __iomem
*reloc_page
;
3100 need_fence
= entry
->flags
& EXEC_OBJECT_NEEDS_FENCE
&&
3101 obj_priv
->tiling_mode
!= I915_TILING_NONE
;
3103 /* Check fence reg constraints and rebind if necessary */
3105 !i915_gem_object_fence_offset_ok(obj
,
3106 obj_priv
->tiling_mode
)) {
3107 ret
= i915_gem_object_unbind(obj
);
3112 /* Choose the GTT offset for our buffer and put it there. */
3113 ret
= i915_gem_object_pin(obj
, (uint32_t) entry
->alignment
);
3118 * Pre-965 chips need a fence register set up in order to
3119 * properly handle blits to/from tiled surfaces.
3122 ret
= i915_gem_object_get_fence_reg(obj
);
3124 i915_gem_object_unpin(obj
);
3129 entry
->offset
= obj_priv
->gtt_offset
;
3131 /* Apply the relocations, using the GTT aperture to avoid cache
3132 * flushing requirements.
3134 for (i
= 0; i
< entry
->relocation_count
; i
++) {
3135 struct drm_i915_gem_relocation_entry
*reloc
= &relocs
[i
];
3136 struct drm_gem_object
*target_obj
;
3137 struct drm_i915_gem_object
*target_obj_priv
;
3138 uint32_t reloc_val
, reloc_offset
;
3139 uint32_t __iomem
*reloc_entry
;
3141 target_obj
= drm_gem_object_lookup(obj
->dev
, file_priv
,
3142 reloc
->target_handle
);
3143 if (target_obj
== NULL
) {
3144 i915_gem_object_unpin(obj
);
3147 target_obj_priv
= to_intel_bo(target_obj
);
3150 DRM_INFO("%s: obj %p offset %08x target %d "
3151 "read %08x write %08x gtt %08x "
3152 "presumed %08x delta %08x\n",
3155 (int) reloc
->offset
,
3156 (int) reloc
->target_handle
,
3157 (int) reloc
->read_domains
,
3158 (int) reloc
->write_domain
,
3159 (int) target_obj_priv
->gtt_offset
,
3160 (int) reloc
->presumed_offset
,
3164 /* The target buffer should have appeared before us in the
3165 * exec_object list, so it should have a GTT space bound by now.
3167 if (target_obj_priv
->gtt_space
== NULL
) {
3168 DRM_ERROR("No GTT space found for object %d\n",
3169 reloc
->target_handle
);
3170 drm_gem_object_unreference(target_obj
);
3171 i915_gem_object_unpin(obj
);
3175 /* Validate that the target is in a valid r/w GPU domain */
3176 if (reloc
->write_domain
& (reloc
->write_domain
- 1)) {
3177 DRM_ERROR("reloc with multiple write domains: "
3178 "obj %p target %d offset %d "
3179 "read %08x write %08x",
3180 obj
, reloc
->target_handle
,
3181 (int) reloc
->offset
,
3182 reloc
->read_domains
,
3183 reloc
->write_domain
);
3186 if (reloc
->write_domain
& I915_GEM_DOMAIN_CPU
||
3187 reloc
->read_domains
& I915_GEM_DOMAIN_CPU
) {
3188 DRM_ERROR("reloc with read/write CPU domains: "
3189 "obj %p target %d offset %d "
3190 "read %08x write %08x",
3191 obj
, reloc
->target_handle
,
3192 (int) reloc
->offset
,
3193 reloc
->read_domains
,
3194 reloc
->write_domain
);
3195 drm_gem_object_unreference(target_obj
);
3196 i915_gem_object_unpin(obj
);
3199 if (reloc
->write_domain
&& target_obj
->pending_write_domain
&&
3200 reloc
->write_domain
!= target_obj
->pending_write_domain
) {
3201 DRM_ERROR("Write domain conflict: "
3202 "obj %p target %d offset %d "
3203 "new %08x old %08x\n",
3204 obj
, reloc
->target_handle
,
3205 (int) reloc
->offset
,
3206 reloc
->write_domain
,
3207 target_obj
->pending_write_domain
);
3208 drm_gem_object_unreference(target_obj
);
3209 i915_gem_object_unpin(obj
);
3213 target_obj
->pending_read_domains
|= reloc
->read_domains
;
3214 target_obj
->pending_write_domain
|= reloc
->write_domain
;
3216 /* If the relocation already has the right value in it, no
3217 * more work needs to be done.
3219 if (target_obj_priv
->gtt_offset
== reloc
->presumed_offset
) {
3220 drm_gem_object_unreference(target_obj
);
3224 /* Check that the relocation address is valid... */
3225 if (reloc
->offset
> obj
->size
- 4) {
3226 DRM_ERROR("Relocation beyond object bounds: "
3227 "obj %p target %d offset %d size %d.\n",
3228 obj
, reloc
->target_handle
,
3229 (int) reloc
->offset
, (int) obj
->size
);
3230 drm_gem_object_unreference(target_obj
);
3231 i915_gem_object_unpin(obj
);
3234 if (reloc
->offset
& 3) {
3235 DRM_ERROR("Relocation not 4-byte aligned: "
3236 "obj %p target %d offset %d.\n",
3237 obj
, reloc
->target_handle
,
3238 (int) reloc
->offset
);
3239 drm_gem_object_unreference(target_obj
);
3240 i915_gem_object_unpin(obj
);
3244 /* and points to somewhere within the target object. */
3245 if (reloc
->delta
>= target_obj
->size
) {
3246 DRM_ERROR("Relocation beyond target object bounds: "
3247 "obj %p target %d delta %d size %d.\n",
3248 obj
, reloc
->target_handle
,
3249 (int) reloc
->delta
, (int) target_obj
->size
);
3250 drm_gem_object_unreference(target_obj
);
3251 i915_gem_object_unpin(obj
);
3255 ret
= i915_gem_object_set_to_gtt_domain(obj
, 1);
3257 drm_gem_object_unreference(target_obj
);
3258 i915_gem_object_unpin(obj
);
3262 /* Map the page containing the relocation we're going to
3265 reloc_offset
= obj_priv
->gtt_offset
+ reloc
->offset
;
3266 reloc_page
= io_mapping_map_atomic_wc(dev_priv
->mm
.gtt_mapping
,
3270 reloc_entry
= (uint32_t __iomem
*)(reloc_page
+
3271 (reloc_offset
& (PAGE_SIZE
- 1)));
3272 reloc_val
= target_obj_priv
->gtt_offset
+ reloc
->delta
;
3275 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
3276 obj
, (unsigned int) reloc
->offset
,
3277 readl(reloc_entry
), reloc_val
);
3279 writel(reloc_val
, reloc_entry
);
3280 io_mapping_unmap_atomic(reloc_page
, KM_USER0
);
3282 /* The updated presumed offset for this entry will be
3283 * copied back out to the user.
3285 reloc
->presumed_offset
= target_obj_priv
->gtt_offset
;
3287 drm_gem_object_unreference(target_obj
);
3292 i915_gem_dump_object(obj
, 128, __func__
, ~0);
3297 /* Throttle our rendering by waiting until the ring has completed our requests
3298 * emitted over 20 msec ago.
3300 * Note that if we were to use the current jiffies each time around the loop,
3301 * we wouldn't escape the function with any frames outstanding if the time to
3302 * render a frame was over 20ms.
3304 * This should get us reasonable parallelism between CPU and GPU but also
3305 * relatively low latency when blocking on a particular request to finish.
3308 i915_gem_ring_throttle(struct drm_device
*dev
, struct drm_file
*file_priv
)
3310 struct drm_i915_file_private
*i915_file_priv
= file_priv
->driver_priv
;
3312 unsigned long recent_enough
= jiffies
- msecs_to_jiffies(20);
3314 mutex_lock(&dev
->struct_mutex
);
3315 while (!list_empty(&i915_file_priv
->mm
.request_list
)) {
3316 struct drm_i915_gem_request
*request
;
3318 request
= list_first_entry(&i915_file_priv
->mm
.request_list
,
3319 struct drm_i915_gem_request
,
3322 if (time_after_eq(request
->emitted_jiffies
, recent_enough
))
3325 ret
= i915_wait_request(dev
, request
->seqno
, request
->ring
);
3329 mutex_unlock(&dev
->struct_mutex
);
3335 i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2
*exec_list
,
3336 uint32_t buffer_count
,
3337 struct drm_i915_gem_relocation_entry
**relocs
)
3339 uint32_t reloc_count
= 0, reloc_index
= 0, i
;
3343 for (i
= 0; i
< buffer_count
; i
++) {
3344 if (reloc_count
+ exec_list
[i
].relocation_count
< reloc_count
)
3346 reloc_count
+= exec_list
[i
].relocation_count
;
3349 *relocs
= drm_calloc_large(reloc_count
, sizeof(**relocs
));
3350 if (*relocs
== NULL
) {
3351 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count
);
3355 for (i
= 0; i
< buffer_count
; i
++) {
3356 struct drm_i915_gem_relocation_entry __user
*user_relocs
;
3358 user_relocs
= (void __user
*)(uintptr_t)exec_list
[i
].relocs_ptr
;
3360 ret
= copy_from_user(&(*relocs
)[reloc_index
],
3362 exec_list
[i
].relocation_count
*
3365 drm_free_large(*relocs
);
3370 reloc_index
+= exec_list
[i
].relocation_count
;
3377 i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2
*exec_list
,
3378 uint32_t buffer_count
,
3379 struct drm_i915_gem_relocation_entry
*relocs
)
3381 uint32_t reloc_count
= 0, i
;
3387 for (i
= 0; i
< buffer_count
; i
++) {
3388 struct drm_i915_gem_relocation_entry __user
*user_relocs
;
3391 user_relocs
= (void __user
*)(uintptr_t)exec_list
[i
].relocs_ptr
;
3393 unwritten
= copy_to_user(user_relocs
,
3394 &relocs
[reloc_count
],
3395 exec_list
[i
].relocation_count
*
3403 reloc_count
+= exec_list
[i
].relocation_count
;
3407 drm_free_large(relocs
);
3413 i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2
*exec
,
3414 uint64_t exec_offset
)
3416 uint32_t exec_start
, exec_len
;
3418 exec_start
= (uint32_t) exec_offset
+ exec
->batch_start_offset
;
3419 exec_len
= (uint32_t) exec
->batch_len
;
3421 if ((exec_start
| exec_len
) & 0x7)
3431 i915_gem_wait_for_pending_flip(struct drm_device
*dev
,
3432 struct drm_gem_object
**object_list
,
3435 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
3436 struct drm_i915_gem_object
*obj_priv
;
3441 prepare_to_wait(&dev_priv
->pending_flip_queue
,
3442 &wait
, TASK_INTERRUPTIBLE
);
3443 for (i
= 0; i
< count
; i
++) {
3444 obj_priv
= to_intel_bo(object_list
[i
]);
3445 if (atomic_read(&obj_priv
->pending_flip
) > 0)
3451 if (!signal_pending(current
)) {
3452 mutex_unlock(&dev
->struct_mutex
);
3454 mutex_lock(&dev
->struct_mutex
);
3460 finish_wait(&dev_priv
->pending_flip_queue
, &wait
);
3466 i915_gem_do_execbuffer(struct drm_device
*dev
, void *data
,
3467 struct drm_file
*file_priv
,
3468 struct drm_i915_gem_execbuffer2
*args
,
3469 struct drm_i915_gem_exec_object2
*exec_list
)
3471 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
3472 struct drm_gem_object
**object_list
= NULL
;
3473 struct drm_gem_object
*batch_obj
;
3474 struct drm_i915_gem_object
*obj_priv
;
3475 struct drm_clip_rect
*cliprects
= NULL
;
3476 struct drm_i915_gem_relocation_entry
*relocs
= NULL
;
3477 struct drm_i915_gem_request
*request
= NULL
;
3478 int ret
= 0, ret2
, i
, pinned
= 0;
3479 uint64_t exec_offset
;
3480 uint32_t seqno
, reloc_index
;
3481 int pin_tries
, flips
;
3483 struct intel_ring_buffer
*ring
= NULL
;
3486 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3487 (int) args
->buffers_ptr
, args
->buffer_count
, args
->batch_len
);
3489 if (args
->flags
& I915_EXEC_BSD
) {
3490 if (!HAS_BSD(dev
)) {
3491 DRM_ERROR("execbuf with wrong flag\n");
3494 ring
= &dev_priv
->bsd_ring
;
3496 ring
= &dev_priv
->render_ring
;
3499 if (args
->buffer_count
< 1) {
3500 DRM_ERROR("execbuf with %d buffers\n", args
->buffer_count
);
3503 object_list
= drm_malloc_ab(sizeof(*object_list
), args
->buffer_count
);
3504 if (object_list
== NULL
) {
3505 DRM_ERROR("Failed to allocate object list for %d buffers\n",
3506 args
->buffer_count
);
3511 if (args
->num_cliprects
!= 0) {
3512 cliprects
= kcalloc(args
->num_cliprects
, sizeof(*cliprects
),
3514 if (cliprects
== NULL
) {
3519 ret
= copy_from_user(cliprects
,
3520 (struct drm_clip_rect __user
*)
3521 (uintptr_t) args
->cliprects_ptr
,
3522 sizeof(*cliprects
) * args
->num_cliprects
);
3524 DRM_ERROR("copy %d cliprects failed: %d\n",
3525 args
->num_cliprects
, ret
);
3531 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
3532 if (request
== NULL
) {
3537 ret
= i915_gem_get_relocs_from_user(exec_list
, args
->buffer_count
,
3542 mutex_lock(&dev
->struct_mutex
);
3544 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3546 if (atomic_read(&dev_priv
->mm
.wedged
)) {
3547 mutex_unlock(&dev
->struct_mutex
);
3552 if (dev_priv
->mm
.suspended
) {
3553 mutex_unlock(&dev
->struct_mutex
);
3558 /* Look up object handles */
3560 for (i
= 0; i
< args
->buffer_count
; i
++) {
3561 object_list
[i
] = drm_gem_object_lookup(dev
, file_priv
,
3562 exec_list
[i
].handle
);
3563 if (object_list
[i
] == NULL
) {
3564 DRM_ERROR("Invalid object handle %d at index %d\n",
3565 exec_list
[i
].handle
, i
);
3566 /* prevent error path from reading uninitialized data */
3567 args
->buffer_count
= i
+ 1;
3572 obj_priv
= to_intel_bo(object_list
[i
]);
3573 if (obj_priv
->in_execbuffer
) {
3574 DRM_ERROR("Object %p appears more than once in object list\n",
3576 /* prevent error path from reading uninitialized data */
3577 args
->buffer_count
= i
+ 1;
3581 obj_priv
->in_execbuffer
= true;
3582 flips
+= atomic_read(&obj_priv
->pending_flip
);
3586 ret
= i915_gem_wait_for_pending_flip(dev
, object_list
,
3587 args
->buffer_count
);
3592 /* Pin and relocate */
3593 for (pin_tries
= 0; ; pin_tries
++) {
3597 for (i
= 0; i
< args
->buffer_count
; i
++) {
3598 object_list
[i
]->pending_read_domains
= 0;
3599 object_list
[i
]->pending_write_domain
= 0;
3600 ret
= i915_gem_object_pin_and_relocate(object_list
[i
],
3603 &relocs
[reloc_index
]);
3607 reloc_index
+= exec_list
[i
].relocation_count
;
3613 /* error other than GTT full, or we've already tried again */
3614 if (ret
!= -ENOSPC
|| pin_tries
>= 1) {
3615 if (ret
!= -ERESTARTSYS
) {
3616 unsigned long long total_size
= 0;
3618 for (i
= 0; i
< args
->buffer_count
; i
++) {
3619 obj_priv
= to_intel_bo(object_list
[i
]);
3621 total_size
+= object_list
[i
]->size
;
3623 exec_list
[i
].flags
& EXEC_OBJECT_NEEDS_FENCE
&&
3624 obj_priv
->tiling_mode
!= I915_TILING_NONE
;
3626 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
3627 pinned
+1, args
->buffer_count
,
3628 total_size
, num_fences
,
3630 DRM_ERROR("%d objects [%d pinned], "
3631 "%d object bytes [%d pinned], "
3632 "%d/%d gtt bytes\n",
3633 atomic_read(&dev
->object_count
),
3634 atomic_read(&dev
->pin_count
),
3635 atomic_read(&dev
->object_memory
),
3636 atomic_read(&dev
->pin_memory
),
3637 atomic_read(&dev
->gtt_memory
),
3643 /* unpin all of our buffers */
3644 for (i
= 0; i
< pinned
; i
++)
3645 i915_gem_object_unpin(object_list
[i
]);
3648 /* evict everyone we can from the aperture */
3649 ret
= i915_gem_evict_everything(dev
);
3650 if (ret
&& ret
!= -ENOSPC
)
3654 /* Set the pending read domains for the batch buffer to COMMAND */
3655 batch_obj
= object_list
[args
->buffer_count
-1];
3656 if (batch_obj
->pending_write_domain
) {
3657 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3661 batch_obj
->pending_read_domains
|= I915_GEM_DOMAIN_COMMAND
;
3663 /* Sanity check the batch buffer, prior to moving objects */
3664 exec_offset
= exec_list
[args
->buffer_count
- 1].offset
;
3665 ret
= i915_gem_check_execbuffer (args
, exec_offset
);
3667 DRM_ERROR("execbuf with invalid offset/length\n");
3671 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3673 /* Zero the global flush/invalidate flags. These
3674 * will be modified as new domains are computed
3677 dev
->invalidate_domains
= 0;
3678 dev
->flush_domains
= 0;
3680 for (i
= 0; i
< args
->buffer_count
; i
++) {
3681 struct drm_gem_object
*obj
= object_list
[i
];
3683 /* Compute new gpu domains and update invalidate/flush */
3684 i915_gem_object_set_to_gpu_domain(obj
);
3687 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3689 if (dev
->invalidate_domains
| dev
->flush_domains
) {
3691 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3693 dev
->invalidate_domains
,
3694 dev
->flush_domains
);
3697 dev
->invalidate_domains
,
3698 dev
->flush_domains
);
3701 if (dev_priv
->render_ring
.outstanding_lazy_request
) {
3702 (void)i915_add_request(dev
, file_priv
, NULL
, &dev_priv
->render_ring
);
3703 dev_priv
->render_ring
.outstanding_lazy_request
= false;
3705 if (dev_priv
->bsd_ring
.outstanding_lazy_request
) {
3706 (void)i915_add_request(dev
, file_priv
, NULL
, &dev_priv
->bsd_ring
);
3707 dev_priv
->bsd_ring
.outstanding_lazy_request
= false;
3710 for (i
= 0; i
< args
->buffer_count
; i
++) {
3711 struct drm_gem_object
*obj
= object_list
[i
];
3712 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
3713 uint32_t old_write_domain
= obj
->write_domain
;
3715 obj
->write_domain
= obj
->pending_write_domain
;
3716 if (obj
->write_domain
)
3717 list_move_tail(&obj_priv
->gpu_write_list
,
3718 &dev_priv
->mm
.gpu_write_list
);
3720 list_del_init(&obj_priv
->gpu_write_list
);
3722 trace_i915_gem_object_change_domain(obj
,
3727 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3730 for (i
= 0; i
< args
->buffer_count
; i
++) {
3731 i915_gem_object_check_coherency(object_list
[i
],
3732 exec_list
[i
].handle
);
3737 i915_gem_dump_object(batch_obj
,
3743 /* Exec the batchbuffer */
3744 ret
= ring
->dispatch_gem_execbuffer(dev
, ring
, args
,
3745 cliprects
, exec_offset
);
3747 DRM_ERROR("dispatch failed %d\n", ret
);
3752 * Ensure that the commands in the batch buffer are
3753 * finished before the interrupt fires
3755 i915_retire_commands(dev
, ring
);
3757 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3759 for (i
= 0; i
< args
->buffer_count
; i
++) {
3760 struct drm_gem_object
*obj
= object_list
[i
];
3761 obj_priv
= to_intel_bo(obj
);
3763 i915_gem_object_move_to_active(obj
, ring
);
3765 DRM_INFO("%s: move to exec list %p\n", __func__
, obj
);
3770 * Get a seqno representing the execution of the current buffer,
3771 * which we can wait on. We would like to mitigate these interrupts,
3772 * likely by only creating seqnos occasionally (so that we have
3773 * *some* interrupts representing completion of buffers that we can
3774 * wait on when trying to clear up gtt space).
3776 seqno
= i915_add_request(dev
, file_priv
, request
, ring
);
3780 i915_dump_lru(dev
, __func__
);
3783 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3786 for (i
= 0; i
< pinned
; i
++)
3787 i915_gem_object_unpin(object_list
[i
]);
3789 for (i
= 0; i
< args
->buffer_count
; i
++) {
3790 if (object_list
[i
]) {
3791 obj_priv
= to_intel_bo(object_list
[i
]);
3792 obj_priv
->in_execbuffer
= false;
3794 drm_gem_object_unreference(object_list
[i
]);
3797 mutex_unlock(&dev
->struct_mutex
);
3800 /* Copy the updated relocations out regardless of current error
3801 * state. Failure to update the relocs would mean that the next
3802 * time userland calls execbuf, it would do so with presumed offset
3803 * state that didn't match the actual object state.
3805 ret2
= i915_gem_put_relocs_to_user(exec_list
, args
->buffer_count
,
3808 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2
);
3814 drm_free_large(object_list
);
3822 * Legacy execbuffer just creates an exec2 list from the original exec object
3823 * list array and passes it to the real function.
3826 i915_gem_execbuffer(struct drm_device
*dev
, void *data
,
3827 struct drm_file
*file_priv
)
3829 struct drm_i915_gem_execbuffer
*args
= data
;
3830 struct drm_i915_gem_execbuffer2 exec2
;
3831 struct drm_i915_gem_exec_object
*exec_list
= NULL
;
3832 struct drm_i915_gem_exec_object2
*exec2_list
= NULL
;
3836 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3837 (int) args
->buffers_ptr
, args
->buffer_count
, args
->batch_len
);
3840 if (args
->buffer_count
< 1) {
3841 DRM_ERROR("execbuf with %d buffers\n", args
->buffer_count
);
3845 /* Copy in the exec list from userland */
3846 exec_list
= drm_malloc_ab(sizeof(*exec_list
), args
->buffer_count
);
3847 exec2_list
= drm_malloc_ab(sizeof(*exec2_list
), args
->buffer_count
);
3848 if (exec_list
== NULL
|| exec2_list
== NULL
) {
3849 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3850 args
->buffer_count
);
3851 drm_free_large(exec_list
);
3852 drm_free_large(exec2_list
);
3855 ret
= copy_from_user(exec_list
,
3856 (struct drm_i915_relocation_entry __user
*)
3857 (uintptr_t) args
->buffers_ptr
,
3858 sizeof(*exec_list
) * args
->buffer_count
);
3860 DRM_ERROR("copy %d exec entries failed %d\n",
3861 args
->buffer_count
, ret
);
3862 drm_free_large(exec_list
);
3863 drm_free_large(exec2_list
);
3867 for (i
= 0; i
< args
->buffer_count
; i
++) {
3868 exec2_list
[i
].handle
= exec_list
[i
].handle
;
3869 exec2_list
[i
].relocation_count
= exec_list
[i
].relocation_count
;
3870 exec2_list
[i
].relocs_ptr
= exec_list
[i
].relocs_ptr
;
3871 exec2_list
[i
].alignment
= exec_list
[i
].alignment
;
3872 exec2_list
[i
].offset
= exec_list
[i
].offset
;
3874 exec2_list
[i
].flags
= EXEC_OBJECT_NEEDS_FENCE
;
3876 exec2_list
[i
].flags
= 0;
3879 exec2
.buffers_ptr
= args
->buffers_ptr
;
3880 exec2
.buffer_count
= args
->buffer_count
;
3881 exec2
.batch_start_offset
= args
->batch_start_offset
;
3882 exec2
.batch_len
= args
->batch_len
;
3883 exec2
.DR1
= args
->DR1
;
3884 exec2
.DR4
= args
->DR4
;
3885 exec2
.num_cliprects
= args
->num_cliprects
;
3886 exec2
.cliprects_ptr
= args
->cliprects_ptr
;
3887 exec2
.flags
= I915_EXEC_RENDER
;
3889 ret
= i915_gem_do_execbuffer(dev
, data
, file_priv
, &exec2
, exec2_list
);
3891 /* Copy the new buffer offsets back to the user's exec list. */
3892 for (i
= 0; i
< args
->buffer_count
; i
++)
3893 exec_list
[i
].offset
= exec2_list
[i
].offset
;
3894 /* ... and back out to userspace */
3895 ret
= copy_to_user((struct drm_i915_relocation_entry __user
*)
3896 (uintptr_t) args
->buffers_ptr
,
3898 sizeof(*exec_list
) * args
->buffer_count
);
3901 DRM_ERROR("failed to copy %d exec entries "
3902 "back to user (%d)\n",
3903 args
->buffer_count
, ret
);
3907 drm_free_large(exec_list
);
3908 drm_free_large(exec2_list
);
3913 i915_gem_execbuffer2(struct drm_device
*dev
, void *data
,
3914 struct drm_file
*file_priv
)
3916 struct drm_i915_gem_execbuffer2
*args
= data
;
3917 struct drm_i915_gem_exec_object2
*exec2_list
= NULL
;
3921 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3922 (int) args
->buffers_ptr
, args
->buffer_count
, args
->batch_len
);
3925 if (args
->buffer_count
< 1) {
3926 DRM_ERROR("execbuf2 with %d buffers\n", args
->buffer_count
);
3930 exec2_list
= drm_malloc_ab(sizeof(*exec2_list
), args
->buffer_count
);
3931 if (exec2_list
== NULL
) {
3932 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3933 args
->buffer_count
);
3936 ret
= copy_from_user(exec2_list
,
3937 (struct drm_i915_relocation_entry __user
*)
3938 (uintptr_t) args
->buffers_ptr
,
3939 sizeof(*exec2_list
) * args
->buffer_count
);
3941 DRM_ERROR("copy %d exec entries failed %d\n",
3942 args
->buffer_count
, ret
);
3943 drm_free_large(exec2_list
);
3947 ret
= i915_gem_do_execbuffer(dev
, data
, file_priv
, args
, exec2_list
);
3949 /* Copy the new buffer offsets back to the user's exec list. */
3950 ret
= copy_to_user((struct drm_i915_relocation_entry __user
*)
3951 (uintptr_t) args
->buffers_ptr
,
3953 sizeof(*exec2_list
) * args
->buffer_count
);
3956 DRM_ERROR("failed to copy %d exec entries "
3957 "back to user (%d)\n",
3958 args
->buffer_count
, ret
);
3962 drm_free_large(exec2_list
);
3967 i915_gem_object_pin(struct drm_gem_object
*obj
, uint32_t alignment
)
3969 struct drm_device
*dev
= obj
->dev
;
3970 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
3973 BUG_ON(obj_priv
->pin_count
== DRM_I915_GEM_OBJECT_MAX_PIN_COUNT
);
3975 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3977 if (obj_priv
->gtt_space
!= NULL
) {
3979 alignment
= i915_gem_get_gtt_alignment(obj
);
3980 if (obj_priv
->gtt_offset
& (alignment
- 1)) {
3981 WARN(obj_priv
->pin_count
,
3982 "bo is already pinned with incorrect alignment:"
3983 " offset=%x, req.alignment=%x\n",
3984 obj_priv
->gtt_offset
, alignment
);
3985 ret
= i915_gem_object_unbind(obj
);
3991 if (obj_priv
->gtt_space
== NULL
) {
3992 ret
= i915_gem_object_bind_to_gtt(obj
, alignment
);
3997 obj_priv
->pin_count
++;
3999 /* If the object is not active and not pending a flush,
4000 * remove it from the inactive list
4002 if (obj_priv
->pin_count
== 1) {
4003 atomic_inc(&dev
->pin_count
);
4004 atomic_add(obj
->size
, &dev
->pin_memory
);
4005 if (!obj_priv
->active
&&
4006 (obj
->write_domain
& I915_GEM_GPU_DOMAINS
) == 0)
4007 list_del_init(&obj_priv
->list
);
4009 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
4015 i915_gem_object_unpin(struct drm_gem_object
*obj
)
4017 struct drm_device
*dev
= obj
->dev
;
4018 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4019 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
4021 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
4022 obj_priv
->pin_count
--;
4023 BUG_ON(obj_priv
->pin_count
< 0);
4024 BUG_ON(obj_priv
->gtt_space
== NULL
);
4026 /* If the object is no longer pinned, and is
4027 * neither active nor being flushed, then stick it on
4030 if (obj_priv
->pin_count
== 0) {
4031 if (!obj_priv
->active
&&
4032 (obj
->write_domain
& I915_GEM_GPU_DOMAINS
) == 0)
4033 list_move_tail(&obj_priv
->list
,
4034 &dev_priv
->mm
.inactive_list
);
4035 atomic_dec(&dev
->pin_count
);
4036 atomic_sub(obj
->size
, &dev
->pin_memory
);
4038 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
4042 i915_gem_pin_ioctl(struct drm_device
*dev
, void *data
,
4043 struct drm_file
*file_priv
)
4045 struct drm_i915_gem_pin
*args
= data
;
4046 struct drm_gem_object
*obj
;
4047 struct drm_i915_gem_object
*obj_priv
;
4050 mutex_lock(&dev
->struct_mutex
);
4052 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
4054 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4056 mutex_unlock(&dev
->struct_mutex
);
4059 obj_priv
= to_intel_bo(obj
);
4061 if (obj_priv
->madv
!= I915_MADV_WILLNEED
) {
4062 DRM_ERROR("Attempting to pin a purgeable buffer\n");
4063 drm_gem_object_unreference(obj
);
4064 mutex_unlock(&dev
->struct_mutex
);
4068 if (obj_priv
->pin_filp
!= NULL
&& obj_priv
->pin_filp
!= file_priv
) {
4069 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4071 drm_gem_object_unreference(obj
);
4072 mutex_unlock(&dev
->struct_mutex
);
4076 obj_priv
->user_pin_count
++;
4077 obj_priv
->pin_filp
= file_priv
;
4078 if (obj_priv
->user_pin_count
== 1) {
4079 ret
= i915_gem_object_pin(obj
, args
->alignment
);
4081 drm_gem_object_unreference(obj
);
4082 mutex_unlock(&dev
->struct_mutex
);
4087 /* XXX - flush the CPU caches for pinned objects
4088 * as the X server doesn't manage domains yet
4090 i915_gem_object_flush_cpu_write_domain(obj
);
4091 args
->offset
= obj_priv
->gtt_offset
;
4092 drm_gem_object_unreference(obj
);
4093 mutex_unlock(&dev
->struct_mutex
);
4099 i915_gem_unpin_ioctl(struct drm_device
*dev
, void *data
,
4100 struct drm_file
*file_priv
)
4102 struct drm_i915_gem_pin
*args
= data
;
4103 struct drm_gem_object
*obj
;
4104 struct drm_i915_gem_object
*obj_priv
;
4106 mutex_lock(&dev
->struct_mutex
);
4108 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
4110 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4112 mutex_unlock(&dev
->struct_mutex
);
4116 obj_priv
= to_intel_bo(obj
);
4117 if (obj_priv
->pin_filp
!= file_priv
) {
4118 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4120 drm_gem_object_unreference(obj
);
4121 mutex_unlock(&dev
->struct_mutex
);
4124 obj_priv
->user_pin_count
--;
4125 if (obj_priv
->user_pin_count
== 0) {
4126 obj_priv
->pin_filp
= NULL
;
4127 i915_gem_object_unpin(obj
);
4130 drm_gem_object_unreference(obj
);
4131 mutex_unlock(&dev
->struct_mutex
);
4136 i915_gem_busy_ioctl(struct drm_device
*dev
, void *data
,
4137 struct drm_file
*file_priv
)
4139 struct drm_i915_gem_busy
*args
= data
;
4140 struct drm_gem_object
*obj
;
4141 struct drm_i915_gem_object
*obj_priv
;
4143 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
4145 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4150 mutex_lock(&dev
->struct_mutex
);
4152 /* Count all active objects as busy, even if they are currently not used
4153 * by the gpu. Users of this interface expect objects to eventually
4154 * become non-busy without any further actions, therefore emit any
4155 * necessary flushes here.
4157 obj_priv
= to_intel_bo(obj
);
4158 args
->busy
= obj_priv
->active
;
4160 /* Unconditionally flush objects, even when the gpu still uses this
4161 * object. Userspace calling this function indicates that it wants to
4162 * use this buffer rather sooner than later, so issuing the required
4163 * flush earlier is beneficial.
4165 if (obj
->write_domain
) {
4166 i915_gem_flush(dev
, 0, obj
->write_domain
);
4167 (void)i915_add_request(dev
, file_priv
, NULL
, obj_priv
->ring
);
4170 /* Update the active list for the hardware's current position.
4171 * Otherwise this only updates on a delayed timer or when irqs
4172 * are actually unmasked, and our working set ends up being
4173 * larger than required.
4175 i915_gem_retire_requests_ring(dev
, obj_priv
->ring
);
4177 args
->busy
= obj_priv
->active
;
4180 drm_gem_object_unreference(obj
);
4181 mutex_unlock(&dev
->struct_mutex
);
4186 i915_gem_throttle_ioctl(struct drm_device
*dev
, void *data
,
4187 struct drm_file
*file_priv
)
4189 return i915_gem_ring_throttle(dev
, file_priv
);
4193 i915_gem_madvise_ioctl(struct drm_device
*dev
, void *data
,
4194 struct drm_file
*file_priv
)
4196 struct drm_i915_gem_madvise
*args
= data
;
4197 struct drm_gem_object
*obj
;
4198 struct drm_i915_gem_object
*obj_priv
;
4200 switch (args
->madv
) {
4201 case I915_MADV_DONTNEED
:
4202 case I915_MADV_WILLNEED
:
4208 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
4210 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4215 mutex_lock(&dev
->struct_mutex
);
4216 obj_priv
= to_intel_bo(obj
);
4218 if (obj_priv
->pin_count
) {
4219 drm_gem_object_unreference(obj
);
4220 mutex_unlock(&dev
->struct_mutex
);
4222 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4226 if (obj_priv
->madv
!= __I915_MADV_PURGED
)
4227 obj_priv
->madv
= args
->madv
;
4229 /* if the object is no longer bound, discard its backing storage */
4230 if (i915_gem_object_is_purgeable(obj_priv
) &&
4231 obj_priv
->gtt_space
== NULL
)
4232 i915_gem_object_truncate(obj
);
4234 args
->retained
= obj_priv
->madv
!= __I915_MADV_PURGED
;
4236 drm_gem_object_unreference(obj
);
4237 mutex_unlock(&dev
->struct_mutex
);
4242 struct drm_gem_object
* i915_gem_alloc_object(struct drm_device
*dev
,
4245 struct drm_i915_gem_object
*obj
;
4247 obj
= kzalloc(sizeof(*obj
), GFP_KERNEL
);
4251 if (drm_gem_object_init(dev
, &obj
->base
, size
) != 0) {
4256 obj
->base
.write_domain
= I915_GEM_DOMAIN_CPU
;
4257 obj
->base
.read_domains
= I915_GEM_DOMAIN_CPU
;
4259 obj
->agp_type
= AGP_USER_MEMORY
;
4260 obj
->base
.driver_private
= NULL
;
4261 obj
->fence_reg
= I915_FENCE_REG_NONE
;
4262 INIT_LIST_HEAD(&obj
->list
);
4263 INIT_LIST_HEAD(&obj
->gpu_write_list
);
4264 obj
->madv
= I915_MADV_WILLNEED
;
4266 trace_i915_gem_object_create(&obj
->base
);
4271 int i915_gem_init_object(struct drm_gem_object
*obj
)
4278 static void i915_gem_free_object_tail(struct drm_gem_object
*obj
)
4280 struct drm_device
*dev
= obj
->dev
;
4281 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4282 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
4285 ret
= i915_gem_object_unbind(obj
);
4286 if (ret
== -ERESTARTSYS
) {
4287 list_move(&obj_priv
->list
,
4288 &dev_priv
->mm
.deferred_free_list
);
4292 if (obj_priv
->mmap_offset
)
4293 i915_gem_free_mmap_offset(obj
);
4295 drm_gem_object_release(obj
);
4297 kfree(obj_priv
->page_cpu_valid
);
4298 kfree(obj_priv
->bit_17
);
4302 void i915_gem_free_object(struct drm_gem_object
*obj
)
4304 struct drm_device
*dev
= obj
->dev
;
4305 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
4307 trace_i915_gem_object_destroy(obj
);
4309 while (obj_priv
->pin_count
> 0)
4310 i915_gem_object_unpin(obj
);
4312 if (obj_priv
->phys_obj
)
4313 i915_gem_detach_phys_object(dev
, obj
);
4315 i915_gem_free_object_tail(obj
);
4319 i915_gem_idle(struct drm_device
*dev
)
4321 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4324 mutex_lock(&dev
->struct_mutex
);
4326 if (dev_priv
->mm
.suspended
||
4327 (dev_priv
->render_ring
.gem_object
== NULL
) ||
4329 dev_priv
->bsd_ring
.gem_object
== NULL
)) {
4330 mutex_unlock(&dev
->struct_mutex
);
4334 ret
= i915_gpu_idle(dev
);
4336 mutex_unlock(&dev
->struct_mutex
);
4340 /* Under UMS, be paranoid and evict. */
4341 if (!drm_core_check_feature(dev
, DRIVER_MODESET
)) {
4342 ret
= i915_gem_evict_inactive(dev
);
4344 mutex_unlock(&dev
->struct_mutex
);
4349 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4350 * We need to replace this with a semaphore, or something.
4351 * And not confound mm.suspended!
4353 dev_priv
->mm
.suspended
= 1;
4354 del_timer_sync(&dev_priv
->hangcheck_timer
);
4356 i915_kernel_lost_context(dev
);
4357 i915_gem_cleanup_ringbuffer(dev
);
4359 mutex_unlock(&dev
->struct_mutex
);
4361 /* Cancel the retire work handler, which should be idle now. */
4362 cancel_delayed_work_sync(&dev_priv
->mm
.retire_work
);
4368 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4369 * over cache flushing.
4372 i915_gem_init_pipe_control(struct drm_device
*dev
)
4374 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4375 struct drm_gem_object
*obj
;
4376 struct drm_i915_gem_object
*obj_priv
;
4379 obj
= i915_gem_alloc_object(dev
, 4096);
4381 DRM_ERROR("Failed to allocate seqno page\n");
4385 obj_priv
= to_intel_bo(obj
);
4386 obj_priv
->agp_type
= AGP_USER_CACHED_MEMORY
;
4388 ret
= i915_gem_object_pin(obj
, 4096);
4392 dev_priv
->seqno_gfx_addr
= obj_priv
->gtt_offset
;
4393 dev_priv
->seqno_page
= kmap(obj_priv
->pages
[0]);
4394 if (dev_priv
->seqno_page
== NULL
)
4397 dev_priv
->seqno_obj
= obj
;
4398 memset(dev_priv
->seqno_page
, 0, PAGE_SIZE
);
4403 i915_gem_object_unpin(obj
);
4405 drm_gem_object_unreference(obj
);
4412 i915_gem_cleanup_pipe_control(struct drm_device
*dev
)
4414 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4415 struct drm_gem_object
*obj
;
4416 struct drm_i915_gem_object
*obj_priv
;
4418 obj
= dev_priv
->seqno_obj
;
4419 obj_priv
= to_intel_bo(obj
);
4420 kunmap(obj_priv
->pages
[0]);
4421 i915_gem_object_unpin(obj
);
4422 drm_gem_object_unreference(obj
);
4423 dev_priv
->seqno_obj
= NULL
;
4425 dev_priv
->seqno_page
= NULL
;
4429 i915_gem_init_ringbuffer(struct drm_device
*dev
)
4431 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4434 dev_priv
->render_ring
= render_ring
;
4436 if (!I915_NEED_GFX_HWS(dev
)) {
4437 dev_priv
->render_ring
.status_page
.page_addr
4438 = dev_priv
->status_page_dmah
->vaddr
;
4439 memset(dev_priv
->render_ring
.status_page
.page_addr
,
4443 if (HAS_PIPE_CONTROL(dev
)) {
4444 ret
= i915_gem_init_pipe_control(dev
);
4449 ret
= intel_init_ring_buffer(dev
, &dev_priv
->render_ring
);
4451 goto cleanup_pipe_control
;
4454 dev_priv
->bsd_ring
= bsd_ring
;
4455 ret
= intel_init_ring_buffer(dev
, &dev_priv
->bsd_ring
);
4457 goto cleanup_render_ring
;
4460 dev_priv
->next_seqno
= 1;
4464 cleanup_render_ring
:
4465 intel_cleanup_ring_buffer(dev
, &dev_priv
->render_ring
);
4466 cleanup_pipe_control
:
4467 if (HAS_PIPE_CONTROL(dev
))
4468 i915_gem_cleanup_pipe_control(dev
);
4473 i915_gem_cleanup_ringbuffer(struct drm_device
*dev
)
4475 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4477 intel_cleanup_ring_buffer(dev
, &dev_priv
->render_ring
);
4479 intel_cleanup_ring_buffer(dev
, &dev_priv
->bsd_ring
);
4480 if (HAS_PIPE_CONTROL(dev
))
4481 i915_gem_cleanup_pipe_control(dev
);
4485 i915_gem_entervt_ioctl(struct drm_device
*dev
, void *data
,
4486 struct drm_file
*file_priv
)
4488 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4491 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
4494 if (atomic_read(&dev_priv
->mm
.wedged
)) {
4495 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4496 atomic_set(&dev_priv
->mm
.wedged
, 0);
4499 mutex_lock(&dev
->struct_mutex
);
4500 dev_priv
->mm
.suspended
= 0;
4502 ret
= i915_gem_init_ringbuffer(dev
);
4504 mutex_unlock(&dev
->struct_mutex
);
4508 BUG_ON(!list_empty(&dev_priv
->render_ring
.active_list
));
4509 BUG_ON(HAS_BSD(dev
) && !list_empty(&dev_priv
->bsd_ring
.active_list
));
4510 BUG_ON(!list_empty(&dev_priv
->mm
.flushing_list
));
4511 BUG_ON(!list_empty(&dev_priv
->mm
.inactive_list
));
4512 BUG_ON(!list_empty(&dev_priv
->render_ring
.request_list
));
4513 BUG_ON(HAS_BSD(dev
) && !list_empty(&dev_priv
->bsd_ring
.request_list
));
4514 mutex_unlock(&dev
->struct_mutex
);
4516 ret
= drm_irq_install(dev
);
4518 goto cleanup_ringbuffer
;
4523 mutex_lock(&dev
->struct_mutex
);
4524 i915_gem_cleanup_ringbuffer(dev
);
4525 dev_priv
->mm
.suspended
= 1;
4526 mutex_unlock(&dev
->struct_mutex
);
4532 i915_gem_leavevt_ioctl(struct drm_device
*dev
, void *data
,
4533 struct drm_file
*file_priv
)
4535 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
4538 drm_irq_uninstall(dev
);
4539 return i915_gem_idle(dev
);
4543 i915_gem_lastclose(struct drm_device
*dev
)
4547 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
4550 ret
= i915_gem_idle(dev
);
4552 DRM_ERROR("failed to idle hardware: %d\n", ret
);
4556 i915_gem_load(struct drm_device
*dev
)
4559 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4561 INIT_LIST_HEAD(&dev_priv
->mm
.flushing_list
);
4562 INIT_LIST_HEAD(&dev_priv
->mm
.gpu_write_list
);
4563 INIT_LIST_HEAD(&dev_priv
->mm
.inactive_list
);
4564 INIT_LIST_HEAD(&dev_priv
->mm
.fence_list
);
4565 INIT_LIST_HEAD(&dev_priv
->mm
.deferred_free_list
);
4566 INIT_LIST_HEAD(&dev_priv
->render_ring
.active_list
);
4567 INIT_LIST_HEAD(&dev_priv
->render_ring
.request_list
);
4569 INIT_LIST_HEAD(&dev_priv
->bsd_ring
.active_list
);
4570 INIT_LIST_HEAD(&dev_priv
->bsd_ring
.request_list
);
4572 for (i
= 0; i
< 16; i
++)
4573 INIT_LIST_HEAD(&dev_priv
->fence_regs
[i
].lru_list
);
4574 INIT_DELAYED_WORK(&dev_priv
->mm
.retire_work
,
4575 i915_gem_retire_work_handler
);
4576 spin_lock(&shrink_list_lock
);
4577 list_add(&dev_priv
->mm
.shrink_list
, &shrink_list
);
4578 spin_unlock(&shrink_list_lock
);
4580 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4582 u32 tmp
= I915_READ(MI_ARB_STATE
);
4583 if (!(tmp
& MI_ARB_C3_LP_WRITE_ENABLE
)) {
4584 /* arb state is a masked write, so set bit + bit in mask */
4585 tmp
= MI_ARB_C3_LP_WRITE_ENABLE
| (MI_ARB_C3_LP_WRITE_ENABLE
<< MI_ARB_MASK_SHIFT
);
4586 I915_WRITE(MI_ARB_STATE
, tmp
);
4590 /* Old X drivers will take 0-2 for front, back, depth buffers */
4591 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4592 dev_priv
->fence_reg_start
= 3;
4594 if (IS_I965G(dev
) || IS_I945G(dev
) || IS_I945GM(dev
) || IS_G33(dev
))
4595 dev_priv
->num_fence_regs
= 16;
4597 dev_priv
->num_fence_regs
= 8;
4599 /* Initialize fence registers to zero */
4600 if (IS_I965G(dev
)) {
4601 for (i
= 0; i
< 16; i
++)
4602 I915_WRITE64(FENCE_REG_965_0
+ (i
* 8), 0);
4604 for (i
= 0; i
< 8; i
++)
4605 I915_WRITE(FENCE_REG_830_0
+ (i
* 4), 0);
4606 if (IS_I945G(dev
) || IS_I945GM(dev
) || IS_G33(dev
))
4607 for (i
= 0; i
< 8; i
++)
4608 I915_WRITE(FENCE_REG_945_8
+ (i
* 4), 0);
4610 i915_gem_detect_bit_6_swizzle(dev
);
4611 init_waitqueue_head(&dev_priv
->pending_flip_queue
);
4615 * Create a physically contiguous memory object for this object
4616 * e.g. for cursor + overlay regs
4618 static int i915_gem_init_phys_object(struct drm_device
*dev
,
4619 int id
, int size
, int align
)
4621 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4622 struct drm_i915_gem_phys_object
*phys_obj
;
4625 if (dev_priv
->mm
.phys_objs
[id
- 1] || !size
)
4628 phys_obj
= kzalloc(sizeof(struct drm_i915_gem_phys_object
), GFP_KERNEL
);
4634 phys_obj
->handle
= drm_pci_alloc(dev
, size
, align
);
4635 if (!phys_obj
->handle
) {
4640 set_memory_wc((unsigned long)phys_obj
->handle
->vaddr
, phys_obj
->handle
->size
/ PAGE_SIZE
);
4643 dev_priv
->mm
.phys_objs
[id
- 1] = phys_obj
;
4651 static void i915_gem_free_phys_object(struct drm_device
*dev
, int id
)
4653 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4654 struct drm_i915_gem_phys_object
*phys_obj
;
4656 if (!dev_priv
->mm
.phys_objs
[id
- 1])
4659 phys_obj
= dev_priv
->mm
.phys_objs
[id
- 1];
4660 if (phys_obj
->cur_obj
) {
4661 i915_gem_detach_phys_object(dev
, phys_obj
->cur_obj
);
4665 set_memory_wb((unsigned long)phys_obj
->handle
->vaddr
, phys_obj
->handle
->size
/ PAGE_SIZE
);
4667 drm_pci_free(dev
, phys_obj
->handle
);
4669 dev_priv
->mm
.phys_objs
[id
- 1] = NULL
;
4672 void i915_gem_free_all_phys_object(struct drm_device
*dev
)
4676 for (i
= I915_GEM_PHYS_CURSOR_0
; i
<= I915_MAX_PHYS_OBJECT
; i
++)
4677 i915_gem_free_phys_object(dev
, i
);
4680 void i915_gem_detach_phys_object(struct drm_device
*dev
,
4681 struct drm_gem_object
*obj
)
4683 struct drm_i915_gem_object
*obj_priv
;
4688 obj_priv
= to_intel_bo(obj
);
4689 if (!obj_priv
->phys_obj
)
4692 ret
= i915_gem_object_get_pages(obj
, 0);
4696 page_count
= obj
->size
/ PAGE_SIZE
;
4698 for (i
= 0; i
< page_count
; i
++) {
4699 char *dst
= kmap_atomic(obj_priv
->pages
[i
], KM_USER0
);
4700 char *src
= obj_priv
->phys_obj
->handle
->vaddr
+ (i
* PAGE_SIZE
);
4702 memcpy(dst
, src
, PAGE_SIZE
);
4703 kunmap_atomic(dst
, KM_USER0
);
4705 drm_clflush_pages(obj_priv
->pages
, page_count
);
4706 drm_agp_chipset_flush(dev
);
4708 i915_gem_object_put_pages(obj
);
4710 obj_priv
->phys_obj
->cur_obj
= NULL
;
4711 obj_priv
->phys_obj
= NULL
;
4715 i915_gem_attach_phys_object(struct drm_device
*dev
,
4716 struct drm_gem_object
*obj
,
4720 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4721 struct drm_i915_gem_object
*obj_priv
;
4726 if (id
> I915_MAX_PHYS_OBJECT
)
4729 obj_priv
= to_intel_bo(obj
);
4731 if (obj_priv
->phys_obj
) {
4732 if (obj_priv
->phys_obj
->id
== id
)
4734 i915_gem_detach_phys_object(dev
, obj
);
4737 /* create a new object */
4738 if (!dev_priv
->mm
.phys_objs
[id
- 1]) {
4739 ret
= i915_gem_init_phys_object(dev
, id
,
4742 DRM_ERROR("failed to init phys object %d size: %zu\n", id
, obj
->size
);
4747 /* bind to the object */
4748 obj_priv
->phys_obj
= dev_priv
->mm
.phys_objs
[id
- 1];
4749 obj_priv
->phys_obj
->cur_obj
= obj
;
4751 ret
= i915_gem_object_get_pages(obj
, 0);
4753 DRM_ERROR("failed to get page list\n");
4757 page_count
= obj
->size
/ PAGE_SIZE
;
4759 for (i
= 0; i
< page_count
; i
++) {
4760 char *src
= kmap_atomic(obj_priv
->pages
[i
], KM_USER0
);
4761 char *dst
= obj_priv
->phys_obj
->handle
->vaddr
+ (i
* PAGE_SIZE
);
4763 memcpy(dst
, src
, PAGE_SIZE
);
4764 kunmap_atomic(src
, KM_USER0
);
4767 i915_gem_object_put_pages(obj
);
4775 i915_gem_phys_pwrite(struct drm_device
*dev
, struct drm_gem_object
*obj
,
4776 struct drm_i915_gem_pwrite
*args
,
4777 struct drm_file
*file_priv
)
4779 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
4782 char __user
*user_data
;
4784 user_data
= (char __user
*) (uintptr_t) args
->data_ptr
;
4785 obj_addr
= obj_priv
->phys_obj
->handle
->vaddr
+ args
->offset
;
4787 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr
, args
->size
);
4788 ret
= copy_from_user(obj_addr
, user_data
, args
->size
);
4792 drm_agp_chipset_flush(dev
);
4796 void i915_gem_release(struct drm_device
* dev
, struct drm_file
*file_priv
)
4798 struct drm_i915_file_private
*i915_file_priv
= file_priv
->driver_priv
;
4800 /* Clean up our request list when the client is going away, so that
4801 * later retire_requests won't dereference our soon-to-be-gone
4804 mutex_lock(&dev
->struct_mutex
);
4805 while (!list_empty(&i915_file_priv
->mm
.request_list
))
4806 list_del_init(i915_file_priv
->mm
.request_list
.next
);
4807 mutex_unlock(&dev
->struct_mutex
);
4811 i915_gpu_is_active(struct drm_device
*dev
)
4813 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4816 lists_empty
= list_empty(&dev_priv
->mm
.flushing_list
) &&
4817 list_empty(&dev_priv
->render_ring
.active_list
);
4819 lists_empty
&= list_empty(&dev_priv
->bsd_ring
.active_list
);
4821 return !lists_empty
;
4825 i915_gem_shrink(struct shrinker
*shrink
, int nr_to_scan
, gfp_t gfp_mask
)
4827 drm_i915_private_t
*dev_priv
, *next_dev
;
4828 struct drm_i915_gem_object
*obj_priv
, *next_obj
;
4830 int would_deadlock
= 1;
4832 /* "fast-path" to count number of available objects */
4833 if (nr_to_scan
== 0) {
4834 spin_lock(&shrink_list_lock
);
4835 list_for_each_entry(dev_priv
, &shrink_list
, mm
.shrink_list
) {
4836 struct drm_device
*dev
= dev_priv
->dev
;
4838 if (mutex_trylock(&dev
->struct_mutex
)) {
4839 list_for_each_entry(obj_priv
,
4840 &dev_priv
->mm
.inactive_list
,
4843 mutex_unlock(&dev
->struct_mutex
);
4846 spin_unlock(&shrink_list_lock
);
4848 return (cnt
/ 100) * sysctl_vfs_cache_pressure
;
4851 spin_lock(&shrink_list_lock
);
4854 /* first scan for clean buffers */
4855 list_for_each_entry_safe(dev_priv
, next_dev
,
4856 &shrink_list
, mm
.shrink_list
) {
4857 struct drm_device
*dev
= dev_priv
->dev
;
4859 if (! mutex_trylock(&dev
->struct_mutex
))
4862 spin_unlock(&shrink_list_lock
);
4863 i915_gem_retire_requests(dev
);
4865 list_for_each_entry_safe(obj_priv
, next_obj
,
4866 &dev_priv
->mm
.inactive_list
,
4868 if (i915_gem_object_is_purgeable(obj_priv
)) {
4869 i915_gem_object_unbind(&obj_priv
->base
);
4870 if (--nr_to_scan
<= 0)
4875 spin_lock(&shrink_list_lock
);
4876 mutex_unlock(&dev
->struct_mutex
);
4880 if (nr_to_scan
<= 0)
4884 /* second pass, evict/count anything still on the inactive list */
4885 list_for_each_entry_safe(dev_priv
, next_dev
,
4886 &shrink_list
, mm
.shrink_list
) {
4887 struct drm_device
*dev
= dev_priv
->dev
;
4889 if (! mutex_trylock(&dev
->struct_mutex
))
4892 spin_unlock(&shrink_list_lock
);
4894 list_for_each_entry_safe(obj_priv
, next_obj
,
4895 &dev_priv
->mm
.inactive_list
,
4897 if (nr_to_scan
> 0) {
4898 i915_gem_object_unbind(&obj_priv
->base
);
4904 spin_lock(&shrink_list_lock
);
4905 mutex_unlock(&dev
->struct_mutex
);
4914 * We are desperate for pages, so as a last resort, wait
4915 * for the GPU to finish and discard whatever we can.
4916 * This has a dramatic impact to reduce the number of
4917 * OOM-killer events whilst running the GPU aggressively.
4919 list_for_each_entry(dev_priv
, &shrink_list
, mm
.shrink_list
) {
4920 struct drm_device
*dev
= dev_priv
->dev
;
4922 if (!mutex_trylock(&dev
->struct_mutex
))
4925 spin_unlock(&shrink_list_lock
);
4927 if (i915_gpu_is_active(dev
)) {
4932 spin_lock(&shrink_list_lock
);
4933 mutex_unlock(&dev
->struct_mutex
);
4940 spin_unlock(&shrink_list_lock
);
4945 return (cnt
/ 100) * sysctl_vfs_cache_pressure
;
4950 static struct shrinker shrinker
= {
4951 .shrink
= i915_gem_shrink
,
4952 .seeks
= DEFAULT_SEEKS
,
4956 i915_gem_shrinker_init(void)
4958 register_shrinker(&shrinker
);
4962 i915_gem_shrinker_exit(void)
4964 unregister_shrinker(&shrinker
);