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/swap.h>
35 #include <linux/pci.h>
37 #define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
39 static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object
*obj
);
40 static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object
*obj
);
41 static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object
*obj
);
42 static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object
*obj
,
44 static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object
*obj
,
47 static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object
*obj
);
48 static int i915_gem_object_wait_rendering(struct drm_gem_object
*obj
);
49 static int i915_gem_object_bind_to_gtt(struct drm_gem_object
*obj
,
51 static void i915_gem_clear_fence_reg(struct drm_gem_object
*obj
);
52 static int i915_gem_evict_something(struct drm_device
*dev
, int min_size
);
53 static int i915_gem_evict_from_inactive_list(struct drm_device
*dev
);
54 static int i915_gem_phys_pwrite(struct drm_device
*dev
, struct drm_gem_object
*obj
,
55 struct drm_i915_gem_pwrite
*args
,
56 struct drm_file
*file_priv
);
58 static LIST_HEAD(shrink_list
);
59 static DEFINE_SPINLOCK(shrink_list_lock
);
61 int i915_gem_do_init(struct drm_device
*dev
, unsigned long start
,
64 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
67 (start
& (PAGE_SIZE
- 1)) != 0 ||
68 (end
& (PAGE_SIZE
- 1)) != 0) {
72 drm_mm_init(&dev_priv
->mm
.gtt_space
, start
,
75 dev
->gtt_total
= (uint32_t) (end
- start
);
81 i915_gem_init_ioctl(struct drm_device
*dev
, void *data
,
82 struct drm_file
*file_priv
)
84 struct drm_i915_gem_init
*args
= data
;
87 mutex_lock(&dev
->struct_mutex
);
88 ret
= i915_gem_do_init(dev
, args
->gtt_start
, args
->gtt_end
);
89 mutex_unlock(&dev
->struct_mutex
);
95 i915_gem_get_aperture_ioctl(struct drm_device
*dev
, void *data
,
96 struct drm_file
*file_priv
)
98 struct drm_i915_gem_get_aperture
*args
= data
;
100 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
103 args
->aper_size
= dev
->gtt_total
;
104 args
->aper_available_size
= (args
->aper_size
-
105 atomic_read(&dev
->pin_memory
));
112 * Creates a new mm object and returns a handle to it.
115 i915_gem_create_ioctl(struct drm_device
*dev
, void *data
,
116 struct drm_file
*file_priv
)
118 struct drm_i915_gem_create
*args
= data
;
119 struct drm_gem_object
*obj
;
123 args
->size
= roundup(args
->size
, PAGE_SIZE
);
125 /* Allocate the new object */
126 obj
= drm_gem_object_alloc(dev
, args
->size
);
130 ret
= drm_gem_handle_create(file_priv
, obj
, &handle
);
131 mutex_lock(&dev
->struct_mutex
);
132 drm_gem_object_handle_unreference(obj
);
133 mutex_unlock(&dev
->struct_mutex
);
138 args
->handle
= handle
;
144 fast_shmem_read(struct page
**pages
,
145 loff_t page_base
, int page_offset
,
152 vaddr
= kmap_atomic(pages
[page_base
>> PAGE_SHIFT
], KM_USER0
);
155 unwritten
= __copy_to_user_inatomic(data
, vaddr
+ page_offset
, length
);
156 kunmap_atomic(vaddr
, KM_USER0
);
164 static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object
*obj
)
166 drm_i915_private_t
*dev_priv
= obj
->dev
->dev_private
;
167 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
169 return dev_priv
->mm
.bit_6_swizzle_x
== I915_BIT_6_SWIZZLE_9_10_17
&&
170 obj_priv
->tiling_mode
!= I915_TILING_NONE
;
174 slow_shmem_copy(struct page
*dst_page
,
176 struct page
*src_page
,
180 char *dst_vaddr
, *src_vaddr
;
182 dst_vaddr
= kmap_atomic(dst_page
, KM_USER0
);
183 if (dst_vaddr
== NULL
)
186 src_vaddr
= kmap_atomic(src_page
, KM_USER1
);
187 if (src_vaddr
== NULL
) {
188 kunmap_atomic(dst_vaddr
, KM_USER0
);
192 memcpy(dst_vaddr
+ dst_offset
, src_vaddr
+ src_offset
, length
);
194 kunmap_atomic(src_vaddr
, KM_USER1
);
195 kunmap_atomic(dst_vaddr
, KM_USER0
);
201 slow_shmem_bit17_copy(struct page
*gpu_page
,
203 struct page
*cpu_page
,
208 char *gpu_vaddr
, *cpu_vaddr
;
210 /* Use the unswizzled path if this page isn't affected. */
211 if ((page_to_phys(gpu_page
) & (1 << 17)) == 0) {
213 return slow_shmem_copy(cpu_page
, cpu_offset
,
214 gpu_page
, gpu_offset
, length
);
216 return slow_shmem_copy(gpu_page
, gpu_offset
,
217 cpu_page
, cpu_offset
, length
);
220 gpu_vaddr
= kmap_atomic(gpu_page
, KM_USER0
);
221 if (gpu_vaddr
== NULL
)
224 cpu_vaddr
= kmap_atomic(cpu_page
, KM_USER1
);
225 if (cpu_vaddr
== NULL
) {
226 kunmap_atomic(gpu_vaddr
, KM_USER0
);
230 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
231 * XORing with the other bits (A9 for Y, A9 and A10 for X)
234 int cacheline_end
= ALIGN(gpu_offset
+ 1, 64);
235 int this_length
= min(cacheline_end
- gpu_offset
, length
);
236 int swizzled_gpu_offset
= gpu_offset
^ 64;
239 memcpy(cpu_vaddr
+ cpu_offset
,
240 gpu_vaddr
+ swizzled_gpu_offset
,
243 memcpy(gpu_vaddr
+ swizzled_gpu_offset
,
244 cpu_vaddr
+ cpu_offset
,
247 cpu_offset
+= this_length
;
248 gpu_offset
+= this_length
;
249 length
-= this_length
;
252 kunmap_atomic(cpu_vaddr
, KM_USER1
);
253 kunmap_atomic(gpu_vaddr
, KM_USER0
);
259 * This is the fast shmem pread path, which attempts to copy_from_user directly
260 * from the backing pages of the object to the user's address space. On a
261 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
264 i915_gem_shmem_pread_fast(struct drm_device
*dev
, struct drm_gem_object
*obj
,
265 struct drm_i915_gem_pread
*args
,
266 struct drm_file
*file_priv
)
268 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
270 loff_t offset
, page_base
;
271 char __user
*user_data
;
272 int page_offset
, page_length
;
275 user_data
= (char __user
*) (uintptr_t) args
->data_ptr
;
278 mutex_lock(&dev
->struct_mutex
);
280 ret
= i915_gem_object_get_pages(obj
, 0);
284 ret
= i915_gem_object_set_cpu_read_domain_range(obj
, args
->offset
,
289 obj_priv
= obj
->driver_private
;
290 offset
= args
->offset
;
293 /* Operation in this page
295 * page_base = page offset within aperture
296 * page_offset = offset within page
297 * page_length = bytes to copy for this page
299 page_base
= (offset
& ~(PAGE_SIZE
-1));
300 page_offset
= offset
& (PAGE_SIZE
-1);
301 page_length
= remain
;
302 if ((page_offset
+ remain
) > PAGE_SIZE
)
303 page_length
= PAGE_SIZE
- page_offset
;
305 ret
= fast_shmem_read(obj_priv
->pages
,
306 page_base
, page_offset
,
307 user_data
, page_length
);
311 remain
-= page_length
;
312 user_data
+= page_length
;
313 offset
+= page_length
;
317 i915_gem_object_put_pages(obj
);
319 mutex_unlock(&dev
->struct_mutex
);
325 i915_gem_object_get_pages_or_evict(struct drm_gem_object
*obj
)
329 ret
= i915_gem_object_get_pages(obj
, __GFP_NORETRY
| __GFP_NOWARN
);
331 /* If we've insufficient memory to map in the pages, attempt
332 * to make some space by throwing out some old buffers.
334 if (ret
== -ENOMEM
) {
335 struct drm_device
*dev
= obj
->dev
;
337 ret
= i915_gem_evict_something(dev
, obj
->size
);
341 ret
= i915_gem_object_get_pages(obj
, 0);
348 * This is the fallback shmem pread path, which allocates temporary storage
349 * in kernel space to copy_to_user into outside of the struct_mutex, so we
350 * can copy out of the object's backing pages while holding the struct mutex
351 * and not take page faults.
354 i915_gem_shmem_pread_slow(struct drm_device
*dev
, struct drm_gem_object
*obj
,
355 struct drm_i915_gem_pread
*args
,
356 struct drm_file
*file_priv
)
358 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
359 struct mm_struct
*mm
= current
->mm
;
360 struct page
**user_pages
;
362 loff_t offset
, pinned_pages
, i
;
363 loff_t first_data_page
, last_data_page
, num_pages
;
364 int shmem_page_index
, shmem_page_offset
;
365 int data_page_index
, data_page_offset
;
368 uint64_t data_ptr
= args
->data_ptr
;
369 int do_bit17_swizzling
;
373 /* Pin the user pages containing the data. We can't fault while
374 * holding the struct mutex, yet we want to hold it while
375 * dereferencing the user data.
377 first_data_page
= data_ptr
/ PAGE_SIZE
;
378 last_data_page
= (data_ptr
+ args
->size
- 1) / PAGE_SIZE
;
379 num_pages
= last_data_page
- first_data_page
+ 1;
381 user_pages
= drm_calloc_large(num_pages
, sizeof(struct page
*));
382 if (user_pages
== NULL
)
385 down_read(&mm
->mmap_sem
);
386 pinned_pages
= get_user_pages(current
, mm
, (uintptr_t)args
->data_ptr
,
387 num_pages
, 1, 0, user_pages
, NULL
);
388 up_read(&mm
->mmap_sem
);
389 if (pinned_pages
< num_pages
) {
391 goto fail_put_user_pages
;
394 do_bit17_swizzling
= i915_gem_object_needs_bit17_swizzle(obj
);
396 mutex_lock(&dev
->struct_mutex
);
398 ret
= i915_gem_object_get_pages_or_evict(obj
);
402 ret
= i915_gem_object_set_cpu_read_domain_range(obj
, args
->offset
,
407 obj_priv
= obj
->driver_private
;
408 offset
= args
->offset
;
411 /* Operation in this page
413 * shmem_page_index = page number within shmem file
414 * shmem_page_offset = offset within page in shmem file
415 * data_page_index = page number in get_user_pages return
416 * data_page_offset = offset with data_page_index page.
417 * page_length = bytes to copy for this page
419 shmem_page_index
= offset
/ PAGE_SIZE
;
420 shmem_page_offset
= offset
& ~PAGE_MASK
;
421 data_page_index
= data_ptr
/ PAGE_SIZE
- first_data_page
;
422 data_page_offset
= data_ptr
& ~PAGE_MASK
;
424 page_length
= remain
;
425 if ((shmem_page_offset
+ page_length
) > PAGE_SIZE
)
426 page_length
= PAGE_SIZE
- shmem_page_offset
;
427 if ((data_page_offset
+ page_length
) > PAGE_SIZE
)
428 page_length
= PAGE_SIZE
- data_page_offset
;
430 if (do_bit17_swizzling
) {
431 ret
= slow_shmem_bit17_copy(obj_priv
->pages
[shmem_page_index
],
433 user_pages
[data_page_index
],
438 ret
= slow_shmem_copy(user_pages
[data_page_index
],
440 obj_priv
->pages
[shmem_page_index
],
447 remain
-= page_length
;
448 data_ptr
+= page_length
;
449 offset
+= page_length
;
453 i915_gem_object_put_pages(obj
);
455 mutex_unlock(&dev
->struct_mutex
);
457 for (i
= 0; i
< pinned_pages
; i
++) {
458 SetPageDirty(user_pages
[i
]);
459 page_cache_release(user_pages
[i
]);
461 drm_free_large(user_pages
);
467 * Reads data from the object referenced by handle.
469 * On error, the contents of *data are undefined.
472 i915_gem_pread_ioctl(struct drm_device
*dev
, void *data
,
473 struct drm_file
*file_priv
)
475 struct drm_i915_gem_pread
*args
= data
;
476 struct drm_gem_object
*obj
;
477 struct drm_i915_gem_object
*obj_priv
;
480 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
483 obj_priv
= obj
->driver_private
;
485 /* Bounds check source. */
486 if (args
->offset
> obj
->size
|| args
->size
> obj
->size
- args
->offset
) {
491 if (!access_ok(VERIFY_WRITE
,
492 (char __user
*)(uintptr_t)args
->data_ptr
,
498 if (i915_gem_object_needs_bit17_swizzle(obj
)) {
499 ret
= i915_gem_shmem_pread_slow(dev
, obj
, args
, file_priv
);
501 ret
= i915_gem_shmem_pread_fast(dev
, obj
, args
, file_priv
);
503 ret
= i915_gem_shmem_pread_slow(dev
, obj
, args
,
508 drm_gem_object_unreference(obj
);
512 /* This is the fast write path which cannot handle
513 * page faults in the source data
517 fast_user_write(struct io_mapping
*mapping
,
518 loff_t page_base
, int page_offset
,
519 char __user
*user_data
,
523 unsigned long unwritten
;
525 vaddr_atomic
= io_mapping_map_atomic_wc(mapping
, page_base
);
526 unwritten
= __copy_from_user_inatomic_nocache(vaddr_atomic
+ page_offset
,
528 io_mapping_unmap_atomic(vaddr_atomic
);
534 /* Here's the write path which can sleep for
539 slow_kernel_write(struct io_mapping
*mapping
,
540 loff_t gtt_base
, int gtt_offset
,
541 struct page
*user_page
, int user_offset
,
544 char *src_vaddr
, *dst_vaddr
;
545 unsigned long unwritten
;
547 dst_vaddr
= io_mapping_map_atomic_wc(mapping
, gtt_base
);
548 src_vaddr
= kmap_atomic(user_page
, KM_USER1
);
549 unwritten
= __copy_from_user_inatomic_nocache(dst_vaddr
+ gtt_offset
,
550 src_vaddr
+ user_offset
,
552 kunmap_atomic(src_vaddr
, KM_USER1
);
553 io_mapping_unmap_atomic(dst_vaddr
);
560 fast_shmem_write(struct page
**pages
,
561 loff_t page_base
, int page_offset
,
566 unsigned long unwritten
;
568 vaddr
= kmap_atomic(pages
[page_base
>> PAGE_SHIFT
], KM_USER0
);
571 unwritten
= __copy_from_user_inatomic(vaddr
+ page_offset
, data
, length
);
572 kunmap_atomic(vaddr
, KM_USER0
);
580 * This is the fast pwrite path, where we copy the data directly from the
581 * user into the GTT, uncached.
584 i915_gem_gtt_pwrite_fast(struct drm_device
*dev
, struct drm_gem_object
*obj
,
585 struct drm_i915_gem_pwrite
*args
,
586 struct drm_file
*file_priv
)
588 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
589 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
591 loff_t offset
, page_base
;
592 char __user
*user_data
;
593 int page_offset
, page_length
;
596 user_data
= (char __user
*) (uintptr_t) args
->data_ptr
;
600 mutex_lock(&dev
->struct_mutex
);
601 ret
= i915_gem_object_pin(obj
, 0);
603 mutex_unlock(&dev
->struct_mutex
);
606 ret
= i915_gem_object_set_to_gtt_domain(obj
, 1);
610 obj_priv
= obj
->driver_private
;
611 offset
= obj_priv
->gtt_offset
+ args
->offset
;
614 /* Operation in this page
616 * page_base = page offset within aperture
617 * page_offset = offset within page
618 * page_length = bytes to copy for this page
620 page_base
= (offset
& ~(PAGE_SIZE
-1));
621 page_offset
= offset
& (PAGE_SIZE
-1);
622 page_length
= remain
;
623 if ((page_offset
+ remain
) > PAGE_SIZE
)
624 page_length
= PAGE_SIZE
- page_offset
;
626 ret
= fast_user_write (dev_priv
->mm
.gtt_mapping
, page_base
,
627 page_offset
, user_data
, page_length
);
629 /* If we get a fault while copying data, then (presumably) our
630 * source page isn't available. Return the error and we'll
631 * retry in the slow path.
636 remain
-= page_length
;
637 user_data
+= page_length
;
638 offset
+= page_length
;
642 i915_gem_object_unpin(obj
);
643 mutex_unlock(&dev
->struct_mutex
);
649 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
650 * the memory and maps it using kmap_atomic for copying.
652 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
653 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
656 i915_gem_gtt_pwrite_slow(struct drm_device
*dev
, struct drm_gem_object
*obj
,
657 struct drm_i915_gem_pwrite
*args
,
658 struct drm_file
*file_priv
)
660 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
661 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
663 loff_t gtt_page_base
, offset
;
664 loff_t first_data_page
, last_data_page
, num_pages
;
665 loff_t pinned_pages
, i
;
666 struct page
**user_pages
;
667 struct mm_struct
*mm
= current
->mm
;
668 int gtt_page_offset
, data_page_offset
, data_page_index
, page_length
;
670 uint64_t data_ptr
= args
->data_ptr
;
674 /* Pin the user pages containing the data. We can't fault while
675 * holding the struct mutex, and all of the pwrite implementations
676 * want to hold it while dereferencing the user data.
678 first_data_page
= data_ptr
/ PAGE_SIZE
;
679 last_data_page
= (data_ptr
+ args
->size
- 1) / PAGE_SIZE
;
680 num_pages
= last_data_page
- first_data_page
+ 1;
682 user_pages
= drm_calloc_large(num_pages
, sizeof(struct page
*));
683 if (user_pages
== NULL
)
686 down_read(&mm
->mmap_sem
);
687 pinned_pages
= get_user_pages(current
, mm
, (uintptr_t)args
->data_ptr
,
688 num_pages
, 0, 0, user_pages
, NULL
);
689 up_read(&mm
->mmap_sem
);
690 if (pinned_pages
< num_pages
) {
692 goto out_unpin_pages
;
695 mutex_lock(&dev
->struct_mutex
);
696 ret
= i915_gem_object_pin(obj
, 0);
700 ret
= i915_gem_object_set_to_gtt_domain(obj
, 1);
702 goto out_unpin_object
;
704 obj_priv
= obj
->driver_private
;
705 offset
= obj_priv
->gtt_offset
+ args
->offset
;
708 /* Operation in this page
710 * gtt_page_base = page offset within aperture
711 * gtt_page_offset = offset within page in aperture
712 * data_page_index = page number in get_user_pages return
713 * data_page_offset = offset with data_page_index page.
714 * page_length = bytes to copy for this page
716 gtt_page_base
= offset
& PAGE_MASK
;
717 gtt_page_offset
= offset
& ~PAGE_MASK
;
718 data_page_index
= data_ptr
/ PAGE_SIZE
- first_data_page
;
719 data_page_offset
= data_ptr
& ~PAGE_MASK
;
721 page_length
= remain
;
722 if ((gtt_page_offset
+ page_length
) > PAGE_SIZE
)
723 page_length
= PAGE_SIZE
- gtt_page_offset
;
724 if ((data_page_offset
+ page_length
) > PAGE_SIZE
)
725 page_length
= PAGE_SIZE
- data_page_offset
;
727 ret
= slow_kernel_write(dev_priv
->mm
.gtt_mapping
,
728 gtt_page_base
, gtt_page_offset
,
729 user_pages
[data_page_index
],
733 /* If we get a fault while copying data, then (presumably) our
734 * source page isn't available. Return the error and we'll
735 * retry in the slow path.
738 goto out_unpin_object
;
740 remain
-= page_length
;
741 offset
+= page_length
;
742 data_ptr
+= page_length
;
746 i915_gem_object_unpin(obj
);
748 mutex_unlock(&dev
->struct_mutex
);
750 for (i
= 0; i
< pinned_pages
; i
++)
751 page_cache_release(user_pages
[i
]);
752 drm_free_large(user_pages
);
758 * This is the fast shmem pwrite path, which attempts to directly
759 * copy_from_user into the kmapped pages backing the object.
762 i915_gem_shmem_pwrite_fast(struct drm_device
*dev
, struct drm_gem_object
*obj
,
763 struct drm_i915_gem_pwrite
*args
,
764 struct drm_file
*file_priv
)
766 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
768 loff_t offset
, page_base
;
769 char __user
*user_data
;
770 int page_offset
, page_length
;
773 user_data
= (char __user
*) (uintptr_t) args
->data_ptr
;
776 mutex_lock(&dev
->struct_mutex
);
778 ret
= i915_gem_object_get_pages(obj
, 0);
782 ret
= i915_gem_object_set_to_cpu_domain(obj
, 1);
786 obj_priv
= obj
->driver_private
;
787 offset
= args
->offset
;
791 /* Operation in this page
793 * page_base = page offset within aperture
794 * page_offset = offset within page
795 * page_length = bytes to copy for this page
797 page_base
= (offset
& ~(PAGE_SIZE
-1));
798 page_offset
= offset
& (PAGE_SIZE
-1);
799 page_length
= remain
;
800 if ((page_offset
+ remain
) > PAGE_SIZE
)
801 page_length
= PAGE_SIZE
- page_offset
;
803 ret
= fast_shmem_write(obj_priv
->pages
,
804 page_base
, page_offset
,
805 user_data
, page_length
);
809 remain
-= page_length
;
810 user_data
+= page_length
;
811 offset
+= page_length
;
815 i915_gem_object_put_pages(obj
);
817 mutex_unlock(&dev
->struct_mutex
);
823 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
824 * the memory and maps it using kmap_atomic for copying.
826 * This avoids taking mmap_sem for faulting on the user's address while the
827 * struct_mutex is held.
830 i915_gem_shmem_pwrite_slow(struct drm_device
*dev
, struct drm_gem_object
*obj
,
831 struct drm_i915_gem_pwrite
*args
,
832 struct drm_file
*file_priv
)
834 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
835 struct mm_struct
*mm
= current
->mm
;
836 struct page
**user_pages
;
838 loff_t offset
, pinned_pages
, i
;
839 loff_t first_data_page
, last_data_page
, num_pages
;
840 int shmem_page_index
, shmem_page_offset
;
841 int data_page_index
, data_page_offset
;
844 uint64_t data_ptr
= args
->data_ptr
;
845 int do_bit17_swizzling
;
849 /* Pin the user pages containing the data. We can't fault while
850 * holding the struct mutex, and all of the pwrite implementations
851 * want to hold it while dereferencing the user data.
853 first_data_page
= data_ptr
/ PAGE_SIZE
;
854 last_data_page
= (data_ptr
+ args
->size
- 1) / PAGE_SIZE
;
855 num_pages
= last_data_page
- first_data_page
+ 1;
857 user_pages
= drm_calloc_large(num_pages
, sizeof(struct page
*));
858 if (user_pages
== NULL
)
861 down_read(&mm
->mmap_sem
);
862 pinned_pages
= get_user_pages(current
, mm
, (uintptr_t)args
->data_ptr
,
863 num_pages
, 0, 0, user_pages
, NULL
);
864 up_read(&mm
->mmap_sem
);
865 if (pinned_pages
< num_pages
) {
867 goto fail_put_user_pages
;
870 do_bit17_swizzling
= i915_gem_object_needs_bit17_swizzle(obj
);
872 mutex_lock(&dev
->struct_mutex
);
874 ret
= i915_gem_object_get_pages_or_evict(obj
);
878 ret
= i915_gem_object_set_to_cpu_domain(obj
, 1);
882 obj_priv
= obj
->driver_private
;
883 offset
= args
->offset
;
887 /* Operation in this page
889 * shmem_page_index = page number within shmem file
890 * shmem_page_offset = offset within page in shmem file
891 * data_page_index = page number in get_user_pages return
892 * data_page_offset = offset with data_page_index page.
893 * page_length = bytes to copy for this page
895 shmem_page_index
= offset
/ PAGE_SIZE
;
896 shmem_page_offset
= offset
& ~PAGE_MASK
;
897 data_page_index
= data_ptr
/ PAGE_SIZE
- first_data_page
;
898 data_page_offset
= data_ptr
& ~PAGE_MASK
;
900 page_length
= remain
;
901 if ((shmem_page_offset
+ page_length
) > PAGE_SIZE
)
902 page_length
= PAGE_SIZE
- shmem_page_offset
;
903 if ((data_page_offset
+ page_length
) > PAGE_SIZE
)
904 page_length
= PAGE_SIZE
- data_page_offset
;
906 if (do_bit17_swizzling
) {
907 ret
= slow_shmem_bit17_copy(obj_priv
->pages
[shmem_page_index
],
909 user_pages
[data_page_index
],
914 ret
= slow_shmem_copy(obj_priv
->pages
[shmem_page_index
],
916 user_pages
[data_page_index
],
923 remain
-= page_length
;
924 data_ptr
+= page_length
;
925 offset
+= page_length
;
929 i915_gem_object_put_pages(obj
);
931 mutex_unlock(&dev
->struct_mutex
);
933 for (i
= 0; i
< pinned_pages
; i
++)
934 page_cache_release(user_pages
[i
]);
935 drm_free_large(user_pages
);
941 * Writes data to the object referenced by handle.
943 * On error, the contents of the buffer that were to be modified are undefined.
946 i915_gem_pwrite_ioctl(struct drm_device
*dev
, void *data
,
947 struct drm_file
*file_priv
)
949 struct drm_i915_gem_pwrite
*args
= data
;
950 struct drm_gem_object
*obj
;
951 struct drm_i915_gem_object
*obj_priv
;
954 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
957 obj_priv
= obj
->driver_private
;
959 /* Bounds check destination. */
960 if (args
->offset
> obj
->size
|| args
->size
> obj
->size
- args
->offset
) {
965 if (!access_ok(VERIFY_READ
,
966 (char __user
*)(uintptr_t)args
->data_ptr
,
972 /* We can only do the GTT pwrite on untiled buffers, as otherwise
973 * it would end up going through the fenced access, and we'll get
974 * different detiling behavior between reading and writing.
975 * pread/pwrite currently are reading and writing from the CPU
976 * perspective, requiring manual detiling by the client.
978 if (obj_priv
->phys_obj
)
979 ret
= i915_gem_phys_pwrite(dev
, obj
, args
, file_priv
);
980 else if (obj_priv
->tiling_mode
== I915_TILING_NONE
&&
981 dev
->gtt_total
!= 0) {
982 ret
= i915_gem_gtt_pwrite_fast(dev
, obj
, args
, file_priv
);
983 if (ret
== -EFAULT
) {
984 ret
= i915_gem_gtt_pwrite_slow(dev
, obj
, args
,
987 } else if (i915_gem_object_needs_bit17_swizzle(obj
)) {
988 ret
= i915_gem_shmem_pwrite_slow(dev
, obj
, args
, file_priv
);
990 ret
= i915_gem_shmem_pwrite_fast(dev
, obj
, args
, file_priv
);
991 if (ret
== -EFAULT
) {
992 ret
= i915_gem_shmem_pwrite_slow(dev
, obj
, args
,
999 DRM_INFO("pwrite failed %d\n", ret
);
1003 drm_gem_object_unreference(obj
);
1008 * Called when user space prepares to use an object with the CPU, either
1009 * through the mmap ioctl's mapping or a GTT mapping.
1012 i915_gem_set_domain_ioctl(struct drm_device
*dev
, void *data
,
1013 struct drm_file
*file_priv
)
1015 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1016 struct drm_i915_gem_set_domain
*args
= data
;
1017 struct drm_gem_object
*obj
;
1018 struct drm_i915_gem_object
*obj_priv
;
1019 uint32_t read_domains
= args
->read_domains
;
1020 uint32_t write_domain
= args
->write_domain
;
1023 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
1026 /* Only handle setting domains to types used by the CPU. */
1027 if (write_domain
& I915_GEM_GPU_DOMAINS
)
1030 if (read_domains
& I915_GEM_GPU_DOMAINS
)
1033 /* Having something in the write domain implies it's in the read
1034 * domain, and only that read domain. Enforce that in the request.
1036 if (write_domain
!= 0 && read_domains
!= write_domain
)
1039 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
1042 obj_priv
= obj
->driver_private
;
1044 mutex_lock(&dev
->struct_mutex
);
1046 intel_mark_busy(dev
, obj
);
1049 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
1050 obj
, obj
->size
, read_domains
, write_domain
);
1052 if (read_domains
& I915_GEM_DOMAIN_GTT
) {
1053 ret
= i915_gem_object_set_to_gtt_domain(obj
, write_domain
!= 0);
1055 /* Update the LRU on the fence for the CPU access that's
1058 if (obj_priv
->fence_reg
!= I915_FENCE_REG_NONE
) {
1059 list_move_tail(&obj_priv
->fence_list
,
1060 &dev_priv
->mm
.fence_list
);
1063 /* Silently promote "you're not bound, there was nothing to do"
1064 * to success, since the client was just asking us to
1065 * make sure everything was done.
1070 ret
= i915_gem_object_set_to_cpu_domain(obj
, write_domain
!= 0);
1073 drm_gem_object_unreference(obj
);
1074 mutex_unlock(&dev
->struct_mutex
);
1079 * Called when user space has done writes to this buffer
1082 i915_gem_sw_finish_ioctl(struct drm_device
*dev
, void *data
,
1083 struct drm_file
*file_priv
)
1085 struct drm_i915_gem_sw_finish
*args
= data
;
1086 struct drm_gem_object
*obj
;
1087 struct drm_i915_gem_object
*obj_priv
;
1090 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
1093 mutex_lock(&dev
->struct_mutex
);
1094 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
1096 mutex_unlock(&dev
->struct_mutex
);
1101 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
1102 __func__
, args
->handle
, obj
, obj
->size
);
1104 obj_priv
= obj
->driver_private
;
1106 /* Pinned buffers may be scanout, so flush the cache */
1107 if (obj_priv
->pin_count
)
1108 i915_gem_object_flush_cpu_write_domain(obj
);
1110 drm_gem_object_unreference(obj
);
1111 mutex_unlock(&dev
->struct_mutex
);
1116 * Maps the contents of an object, returning the address it is mapped
1119 * While the mapping holds a reference on the contents of the object, it doesn't
1120 * imply a ref on the object itself.
1123 i915_gem_mmap_ioctl(struct drm_device
*dev
, void *data
,
1124 struct drm_file
*file_priv
)
1126 struct drm_i915_gem_mmap
*args
= data
;
1127 struct drm_gem_object
*obj
;
1131 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
1134 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
1138 offset
= args
->offset
;
1140 down_write(¤t
->mm
->mmap_sem
);
1141 addr
= do_mmap(obj
->filp
, 0, args
->size
,
1142 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
1144 up_write(¤t
->mm
->mmap_sem
);
1145 mutex_lock(&dev
->struct_mutex
);
1146 drm_gem_object_unreference(obj
);
1147 mutex_unlock(&dev
->struct_mutex
);
1148 if (IS_ERR((void *)addr
))
1151 args
->addr_ptr
= (uint64_t) addr
;
1157 * i915_gem_fault - fault a page into the GTT
1158 * vma: VMA in question
1161 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1162 * from userspace. The fault handler takes care of binding the object to
1163 * the GTT (if needed), allocating and programming a fence register (again,
1164 * only if needed based on whether the old reg is still valid or the object
1165 * is tiled) and inserting a new PTE into the faulting process.
1167 * Note that the faulting process may involve evicting existing objects
1168 * from the GTT and/or fence registers to make room. So performance may
1169 * suffer if the GTT working set is large or there are few fence registers
1172 int i915_gem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1174 struct drm_gem_object
*obj
= vma
->vm_private_data
;
1175 struct drm_device
*dev
= obj
->dev
;
1176 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1177 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
1178 pgoff_t page_offset
;
1181 bool write
= !!(vmf
->flags
& FAULT_FLAG_WRITE
);
1183 /* We don't use vmf->pgoff since that has the fake offset */
1184 page_offset
= ((unsigned long)vmf
->virtual_address
- vma
->vm_start
) >>
1187 /* Now bind it into the GTT if needed */
1188 mutex_lock(&dev
->struct_mutex
);
1189 if (!obj_priv
->gtt_space
) {
1190 ret
= i915_gem_object_bind_to_gtt(obj
, 0);
1194 list_add_tail(&obj_priv
->list
, &dev_priv
->mm
.inactive_list
);
1196 ret
= i915_gem_object_set_to_gtt_domain(obj
, write
);
1201 /* Need a new fence register? */
1202 if (obj_priv
->tiling_mode
!= I915_TILING_NONE
) {
1203 ret
= i915_gem_object_get_fence_reg(obj
);
1208 pfn
= ((dev
->agp
->base
+ obj_priv
->gtt_offset
) >> PAGE_SHIFT
) +
1211 /* Finally, remap it using the new GTT offset */
1212 ret
= vm_insert_pfn(vma
, (unsigned long)vmf
->virtual_address
, pfn
);
1214 mutex_unlock(&dev
->struct_mutex
);
1219 return VM_FAULT_NOPAGE
;
1222 return VM_FAULT_OOM
;
1224 return VM_FAULT_SIGBUS
;
1229 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1230 * @obj: obj in question
1232 * GEM memory mapping works by handing back to userspace a fake mmap offset
1233 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1234 * up the object based on the offset and sets up the various memory mapping
1237 * This routine allocates and attaches a fake offset for @obj.
1240 i915_gem_create_mmap_offset(struct drm_gem_object
*obj
)
1242 struct drm_device
*dev
= obj
->dev
;
1243 struct drm_gem_mm
*mm
= dev
->mm_private
;
1244 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
1245 struct drm_map_list
*list
;
1246 struct drm_local_map
*map
;
1249 /* Set the object up for mmap'ing */
1250 list
= &obj
->map_list
;
1251 list
->map
= kzalloc(sizeof(struct drm_map_list
), GFP_KERNEL
);
1256 map
->type
= _DRM_GEM
;
1257 map
->size
= obj
->size
;
1260 /* Get a DRM GEM mmap offset allocated... */
1261 list
->file_offset_node
= drm_mm_search_free(&mm
->offset_manager
,
1262 obj
->size
/ PAGE_SIZE
, 0, 0);
1263 if (!list
->file_offset_node
) {
1264 DRM_ERROR("failed to allocate offset for bo %d\n", obj
->name
);
1269 list
->file_offset_node
= drm_mm_get_block(list
->file_offset_node
,
1270 obj
->size
/ PAGE_SIZE
, 0);
1271 if (!list
->file_offset_node
) {
1276 list
->hash
.key
= list
->file_offset_node
->start
;
1277 if (drm_ht_insert_item(&mm
->offset_hash
, &list
->hash
)) {
1278 DRM_ERROR("failed to add to map hash\n");
1283 /* By now we should be all set, any drm_mmap request on the offset
1284 * below will get to our mmap & fault handler */
1285 obj_priv
->mmap_offset
= ((uint64_t) list
->hash
.key
) << PAGE_SHIFT
;
1290 drm_mm_put_block(list
->file_offset_node
);
1298 * i915_gem_release_mmap - remove physical page mappings
1299 * @obj: obj in question
1301 * Preserve the reservation of the mmaping with the DRM core code, but
1302 * relinquish ownership of the pages back to the system.
1304 * It is vital that we remove the page mapping if we have mapped a tiled
1305 * object through the GTT and then lose the fence register due to
1306 * resource pressure. Similarly if the object has been moved out of the
1307 * aperture, than pages mapped into userspace must be revoked. Removing the
1308 * mapping will then trigger a page fault on the next user access, allowing
1309 * fixup by i915_gem_fault().
1312 i915_gem_release_mmap(struct drm_gem_object
*obj
)
1314 struct drm_device
*dev
= obj
->dev
;
1315 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
1317 if (dev
->dev_mapping
)
1318 unmap_mapping_range(dev
->dev_mapping
,
1319 obj_priv
->mmap_offset
, obj
->size
, 1);
1323 i915_gem_free_mmap_offset(struct drm_gem_object
*obj
)
1325 struct drm_device
*dev
= obj
->dev
;
1326 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
1327 struct drm_gem_mm
*mm
= dev
->mm_private
;
1328 struct drm_map_list
*list
;
1330 list
= &obj
->map_list
;
1331 drm_ht_remove_item(&mm
->offset_hash
, &list
->hash
);
1333 if (list
->file_offset_node
) {
1334 drm_mm_put_block(list
->file_offset_node
);
1335 list
->file_offset_node
= NULL
;
1343 obj_priv
->mmap_offset
= 0;
1347 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1348 * @obj: object to check
1350 * Return the required GTT alignment for an object, taking into account
1351 * potential fence register mapping if needed.
1354 i915_gem_get_gtt_alignment(struct drm_gem_object
*obj
)
1356 struct drm_device
*dev
= obj
->dev
;
1357 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
1361 * Minimum alignment is 4k (GTT page size), but might be greater
1362 * if a fence register is needed for the object.
1364 if (IS_I965G(dev
) || obj_priv
->tiling_mode
== I915_TILING_NONE
)
1368 * Previous chips need to be aligned to the size of the smallest
1369 * fence register that can contain the object.
1376 for (i
= start
; i
< obj
->size
; i
<<= 1)
1383 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1385 * @data: GTT mapping ioctl data
1386 * @file_priv: GEM object info
1388 * Simply returns the fake offset to userspace so it can mmap it.
1389 * The mmap call will end up in drm_gem_mmap(), which will set things
1390 * up so we can get faults in the handler above.
1392 * The fault handler will take care of binding the object into the GTT
1393 * (since it may have been evicted to make room for something), allocating
1394 * a fence register, and mapping the appropriate aperture address into
1398 i915_gem_mmap_gtt_ioctl(struct drm_device
*dev
, void *data
,
1399 struct drm_file
*file_priv
)
1401 struct drm_i915_gem_mmap_gtt
*args
= data
;
1402 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1403 struct drm_gem_object
*obj
;
1404 struct drm_i915_gem_object
*obj_priv
;
1407 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
1410 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
1414 mutex_lock(&dev
->struct_mutex
);
1416 obj_priv
= obj
->driver_private
;
1418 if (obj_priv
->madv
!= I915_MADV_WILLNEED
) {
1419 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1420 drm_gem_object_unreference(obj
);
1421 mutex_unlock(&dev
->struct_mutex
);
1426 if (!obj_priv
->mmap_offset
) {
1427 ret
= i915_gem_create_mmap_offset(obj
);
1429 drm_gem_object_unreference(obj
);
1430 mutex_unlock(&dev
->struct_mutex
);
1435 args
->offset
= obj_priv
->mmap_offset
;
1438 * Pull it into the GTT so that we have a page list (makes the
1439 * initial fault faster and any subsequent flushing possible).
1441 if (!obj_priv
->agp_mem
) {
1442 ret
= i915_gem_object_bind_to_gtt(obj
, 0);
1444 drm_gem_object_unreference(obj
);
1445 mutex_unlock(&dev
->struct_mutex
);
1448 list_add_tail(&obj_priv
->list
, &dev_priv
->mm
.inactive_list
);
1451 drm_gem_object_unreference(obj
);
1452 mutex_unlock(&dev
->struct_mutex
);
1458 i915_gem_object_put_pages(struct drm_gem_object
*obj
)
1460 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
1461 int page_count
= obj
->size
/ PAGE_SIZE
;
1464 BUG_ON(obj_priv
->pages_refcount
== 0);
1465 BUG_ON(obj_priv
->madv
== __I915_MADV_PURGED
);
1467 if (--obj_priv
->pages_refcount
!= 0)
1470 if (obj_priv
->tiling_mode
!= I915_TILING_NONE
)
1471 i915_gem_object_save_bit_17_swizzle(obj
);
1473 if (obj_priv
->madv
== I915_MADV_DONTNEED
)
1474 obj_priv
->dirty
= 0;
1476 for (i
= 0; i
< page_count
; i
++) {
1477 if (obj_priv
->dirty
)
1478 set_page_dirty(obj_priv
->pages
[i
]);
1480 if (obj_priv
->madv
== I915_MADV_WILLNEED
)
1481 mark_page_accessed(obj_priv
->pages
[i
]);
1483 page_cache_release(obj_priv
->pages
[i
]);
1485 obj_priv
->dirty
= 0;
1487 drm_free_large(obj_priv
->pages
);
1488 obj_priv
->pages
= NULL
;
1492 i915_gem_object_move_to_active(struct drm_gem_object
*obj
, uint32_t seqno
)
1494 struct drm_device
*dev
= obj
->dev
;
1495 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1496 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
1498 /* Add a reference if we're newly entering the active list. */
1499 if (!obj_priv
->active
) {
1500 drm_gem_object_reference(obj
);
1501 obj_priv
->active
= 1;
1503 /* Move from whatever list we were on to the tail of execution. */
1504 spin_lock(&dev_priv
->mm
.active_list_lock
);
1505 list_move_tail(&obj_priv
->list
,
1506 &dev_priv
->mm
.active_list
);
1507 spin_unlock(&dev_priv
->mm
.active_list_lock
);
1508 obj_priv
->last_rendering_seqno
= seqno
;
1512 i915_gem_object_move_to_flushing(struct drm_gem_object
*obj
)
1514 struct drm_device
*dev
= obj
->dev
;
1515 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1516 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
1518 BUG_ON(!obj_priv
->active
);
1519 list_move_tail(&obj_priv
->list
, &dev_priv
->mm
.flushing_list
);
1520 obj_priv
->last_rendering_seqno
= 0;
1523 /* Immediately discard the backing storage */
1525 i915_gem_object_truncate(struct drm_gem_object
*obj
)
1527 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
1528 struct inode
*inode
;
1530 inode
= obj
->filp
->f_path
.dentry
->d_inode
;
1531 if (inode
->i_op
->truncate
)
1532 inode
->i_op
->truncate (inode
);
1534 obj_priv
->madv
= __I915_MADV_PURGED
;
1538 i915_gem_object_is_purgeable(struct drm_i915_gem_object
*obj_priv
)
1540 return obj_priv
->madv
== I915_MADV_DONTNEED
;
1544 i915_gem_object_move_to_inactive(struct drm_gem_object
*obj
)
1546 struct drm_device
*dev
= obj
->dev
;
1547 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1548 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
1550 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
1551 if (obj_priv
->pin_count
!= 0)
1552 list_del_init(&obj_priv
->list
);
1554 list_move_tail(&obj_priv
->list
, &dev_priv
->mm
.inactive_list
);
1556 BUG_ON(!list_empty(&obj_priv
->gpu_write_list
));
1558 obj_priv
->last_rendering_seqno
= 0;
1559 if (obj_priv
->active
) {
1560 obj_priv
->active
= 0;
1561 drm_gem_object_unreference(obj
);
1563 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
1567 * Creates a new sequence number, emitting a write of it to the status page
1568 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1570 * Must be called with struct_lock held.
1572 * Returned sequence numbers are nonzero on success.
1575 i915_add_request(struct drm_device
*dev
, struct drm_file
*file_priv
,
1576 uint32_t flush_domains
)
1578 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1579 struct drm_i915_file_private
*i915_file_priv
= NULL
;
1580 struct drm_i915_gem_request
*request
;
1585 if (file_priv
!= NULL
)
1586 i915_file_priv
= file_priv
->driver_priv
;
1588 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
1589 if (request
== NULL
)
1592 /* Grab the seqno we're going to make this request be, and bump the
1593 * next (skipping 0 so it can be the reserved no-seqno value).
1595 seqno
= dev_priv
->mm
.next_gem_seqno
;
1596 dev_priv
->mm
.next_gem_seqno
++;
1597 if (dev_priv
->mm
.next_gem_seqno
== 0)
1598 dev_priv
->mm
.next_gem_seqno
++;
1601 OUT_RING(MI_STORE_DWORD_INDEX
);
1602 OUT_RING(I915_GEM_HWS_INDEX
<< MI_STORE_DWORD_INDEX_SHIFT
);
1605 OUT_RING(MI_USER_INTERRUPT
);
1608 DRM_DEBUG("%d\n", seqno
);
1610 request
->seqno
= seqno
;
1611 request
->emitted_jiffies
= jiffies
;
1612 was_empty
= list_empty(&dev_priv
->mm
.request_list
);
1613 list_add_tail(&request
->list
, &dev_priv
->mm
.request_list
);
1614 if (i915_file_priv
) {
1615 list_add_tail(&request
->client_list
,
1616 &i915_file_priv
->mm
.request_list
);
1618 INIT_LIST_HEAD(&request
->client_list
);
1621 /* Associate any objects on the flushing list matching the write
1622 * domain we're flushing with our flush.
1624 if (flush_domains
!= 0) {
1625 struct drm_i915_gem_object
*obj_priv
, *next
;
1627 list_for_each_entry_safe(obj_priv
, next
,
1628 &dev_priv
->mm
.gpu_write_list
,
1630 struct drm_gem_object
*obj
= obj_priv
->obj
;
1632 if ((obj
->write_domain
& flush_domains
) ==
1633 obj
->write_domain
) {
1634 uint32_t old_write_domain
= obj
->write_domain
;
1636 obj
->write_domain
= 0;
1637 list_del_init(&obj_priv
->gpu_write_list
);
1638 i915_gem_object_move_to_active(obj
, seqno
);
1640 trace_i915_gem_object_change_domain(obj
,
1648 if (!dev_priv
->mm
.suspended
) {
1649 mod_timer(&dev_priv
->hangcheck_timer
, jiffies
+ DRM_I915_HANGCHECK_PERIOD
);
1651 queue_delayed_work(dev_priv
->wq
, &dev_priv
->mm
.retire_work
, HZ
);
1657 * Command execution barrier
1659 * Ensures that all commands in the ring are finished
1660 * before signalling the CPU
1663 i915_retire_commands(struct drm_device
*dev
)
1665 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1666 uint32_t cmd
= MI_FLUSH
| MI_NO_WRITE_FLUSH
;
1667 uint32_t flush_domains
= 0;
1670 /* The sampler always gets flushed on i965 (sigh) */
1672 flush_domains
|= I915_GEM_DOMAIN_SAMPLER
;
1675 OUT_RING(0); /* noop */
1677 return flush_domains
;
1681 * Moves buffers associated only with the given active seqno from the active
1682 * to inactive list, potentially freeing them.
1685 i915_gem_retire_request(struct drm_device
*dev
,
1686 struct drm_i915_gem_request
*request
)
1688 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1690 trace_i915_gem_request_retire(dev
, request
->seqno
);
1692 /* Move any buffers on the active list that are no longer referenced
1693 * by the ringbuffer to the flushing/inactive lists as appropriate.
1695 spin_lock(&dev_priv
->mm
.active_list_lock
);
1696 while (!list_empty(&dev_priv
->mm
.active_list
)) {
1697 struct drm_gem_object
*obj
;
1698 struct drm_i915_gem_object
*obj_priv
;
1700 obj_priv
= list_first_entry(&dev_priv
->mm
.active_list
,
1701 struct drm_i915_gem_object
,
1703 obj
= obj_priv
->obj
;
1705 /* If the seqno being retired doesn't match the oldest in the
1706 * list, then the oldest in the list must still be newer than
1709 if (obj_priv
->last_rendering_seqno
!= request
->seqno
)
1713 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1714 __func__
, request
->seqno
, obj
);
1717 if (obj
->write_domain
!= 0)
1718 i915_gem_object_move_to_flushing(obj
);
1720 /* Take a reference on the object so it won't be
1721 * freed while the spinlock is held. The list
1722 * protection for this spinlock is safe when breaking
1723 * the lock like this since the next thing we do
1724 * is just get the head of the list again.
1726 drm_gem_object_reference(obj
);
1727 i915_gem_object_move_to_inactive(obj
);
1728 spin_unlock(&dev_priv
->mm
.active_list_lock
);
1729 drm_gem_object_unreference(obj
);
1730 spin_lock(&dev_priv
->mm
.active_list_lock
);
1734 spin_unlock(&dev_priv
->mm
.active_list_lock
);
1738 * Returns true if seq1 is later than seq2.
1741 i915_seqno_passed(uint32_t seq1
, uint32_t seq2
)
1743 return (int32_t)(seq1
- seq2
) >= 0;
1747 i915_get_gem_seqno(struct drm_device
*dev
)
1749 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1751 return READ_HWSP(dev_priv
, I915_GEM_HWS_INDEX
);
1755 * This function clears the request list as sequence numbers are passed.
1758 i915_gem_retire_requests(struct drm_device
*dev
)
1760 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1763 if (!dev_priv
->hw_status_page
|| list_empty(&dev_priv
->mm
.request_list
))
1766 seqno
= i915_get_gem_seqno(dev
);
1768 while (!list_empty(&dev_priv
->mm
.request_list
)) {
1769 struct drm_i915_gem_request
*request
;
1770 uint32_t retiring_seqno
;
1772 request
= list_first_entry(&dev_priv
->mm
.request_list
,
1773 struct drm_i915_gem_request
,
1775 retiring_seqno
= request
->seqno
;
1777 if (i915_seqno_passed(seqno
, retiring_seqno
) ||
1778 atomic_read(&dev_priv
->mm
.wedged
)) {
1779 i915_gem_retire_request(dev
, request
);
1781 list_del(&request
->list
);
1782 list_del(&request
->client_list
);
1788 if (unlikely (dev_priv
->trace_irq_seqno
&&
1789 i915_seqno_passed(dev_priv
->trace_irq_seqno
, seqno
))) {
1790 i915_user_irq_put(dev
);
1791 dev_priv
->trace_irq_seqno
= 0;
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
);
1807 if (!dev_priv
->mm
.suspended
&&
1808 !list_empty(&dev_priv
->mm
.request_list
))
1809 queue_delayed_work(dev_priv
->wq
, &dev_priv
->mm
.retire_work
, HZ
);
1810 mutex_unlock(&dev
->struct_mutex
);
1814 i915_do_wait_request(struct drm_device
*dev
, uint32_t seqno
, int interruptible
)
1816 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1822 if (atomic_read(&dev_priv
->mm
.wedged
))
1825 if (!i915_seqno_passed(i915_get_gem_seqno(dev
), seqno
)) {
1827 ier
= I915_READ(DEIER
) | I915_READ(GTIER
);
1829 ier
= I915_READ(IER
);
1831 DRM_ERROR("something (likely vbetool) disabled "
1832 "interrupts, re-enabling\n");
1833 i915_driver_irq_preinstall(dev
);
1834 i915_driver_irq_postinstall(dev
);
1837 trace_i915_gem_request_wait_begin(dev
, seqno
);
1839 dev_priv
->mm
.waiting_gem_seqno
= seqno
;
1840 i915_user_irq_get(dev
);
1842 ret
= wait_event_interruptible(dev_priv
->irq_queue
,
1843 i915_seqno_passed(i915_get_gem_seqno(dev
), seqno
) ||
1844 atomic_read(&dev_priv
->mm
.wedged
));
1846 wait_event(dev_priv
->irq_queue
,
1847 i915_seqno_passed(i915_get_gem_seqno(dev
), seqno
) ||
1848 atomic_read(&dev_priv
->mm
.wedged
));
1850 i915_user_irq_put(dev
);
1851 dev_priv
->mm
.waiting_gem_seqno
= 0;
1853 trace_i915_gem_request_wait_end(dev
, seqno
);
1855 if (atomic_read(&dev_priv
->mm
.wedged
))
1858 if (ret
&& ret
!= -ERESTARTSYS
)
1859 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1860 __func__
, ret
, seqno
, i915_get_gem_seqno(dev
));
1862 /* Directly dispatch request retiring. While we have the work queue
1863 * to handle this, the waiter on a request often wants an associated
1864 * buffer to have made it to the inactive list, and we would need
1865 * a separate wait queue to handle that.
1868 i915_gem_retire_requests(dev
);
1874 * Waits for a sequence number to be signaled, and cleans up the
1875 * request and object lists appropriately for that event.
1878 i915_wait_request(struct drm_device
*dev
, uint32_t seqno
)
1880 return i915_do_wait_request(dev
, seqno
, 1);
1884 * Waits for the ring to finish up to the latest request. Usefull for waiting
1885 * for flip events, e.g for the overlay support. */
1886 int i915_lp_ring_sync(struct drm_device
*dev
)
1891 seqno
= i915_add_request(dev
, NULL
, 0);
1896 ret
= i915_do_wait_request(dev
, seqno
, 0);
1897 BUG_ON(ret
== -ERESTARTSYS
);
1902 i915_gem_flush(struct drm_device
*dev
,
1903 uint32_t invalidate_domains
,
1904 uint32_t flush_domains
)
1906 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1911 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__
,
1912 invalidate_domains
, flush_domains
);
1914 trace_i915_gem_request_flush(dev
, dev_priv
->mm
.next_gem_seqno
,
1915 invalidate_domains
, flush_domains
);
1917 if (flush_domains
& I915_GEM_DOMAIN_CPU
)
1918 drm_agp_chipset_flush(dev
);
1920 if ((invalidate_domains
| flush_domains
) & I915_GEM_GPU_DOMAINS
) {
1922 * read/write caches:
1924 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1925 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1926 * also flushed at 2d versus 3d pipeline switches.
1930 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1931 * MI_READ_FLUSH is set, and is always flushed on 965.
1933 * I915_GEM_DOMAIN_COMMAND may not exist?
1935 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1936 * invalidated when MI_EXE_FLUSH is set.
1938 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1939 * invalidated with every MI_FLUSH.
1943 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1944 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1945 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1946 * are flushed at any MI_FLUSH.
1949 cmd
= MI_FLUSH
| MI_NO_WRITE_FLUSH
;
1950 if ((invalidate_domains
|flush_domains
) &
1951 I915_GEM_DOMAIN_RENDER
)
1952 cmd
&= ~MI_NO_WRITE_FLUSH
;
1953 if (!IS_I965G(dev
)) {
1955 * On the 965, the sampler cache always gets flushed
1956 * and this bit is reserved.
1958 if (invalidate_domains
& I915_GEM_DOMAIN_SAMPLER
)
1959 cmd
|= MI_READ_FLUSH
;
1961 if (invalidate_domains
& I915_GEM_DOMAIN_INSTRUCTION
)
1962 cmd
|= MI_EXE_FLUSH
;
1965 DRM_INFO("%s: queue flush %08x to ring\n", __func__
, cmd
);
1975 * Ensures that all rendering to the object has completed and the object is
1976 * safe to unbind from the GTT or access from the CPU.
1979 i915_gem_object_wait_rendering(struct drm_gem_object
*obj
)
1981 struct drm_device
*dev
= obj
->dev
;
1982 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
1985 /* This function only exists to support waiting for existing rendering,
1986 * not for emitting required flushes.
1988 BUG_ON((obj
->write_domain
& I915_GEM_GPU_DOMAINS
) != 0);
1990 /* If there is rendering queued on the buffer being evicted, wait for
1993 if (obj_priv
->active
) {
1995 DRM_INFO("%s: object %p wait for seqno %08x\n",
1996 __func__
, obj
, obj_priv
->last_rendering_seqno
);
1998 ret
= i915_wait_request(dev
, obj_priv
->last_rendering_seqno
);
2007 * Unbinds an object from the GTT aperture.
2010 i915_gem_object_unbind(struct drm_gem_object
*obj
)
2012 struct drm_device
*dev
= obj
->dev
;
2013 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
2017 DRM_INFO("%s:%d %p\n", __func__
, __LINE__
, obj
);
2018 DRM_INFO("gtt_space %p\n", obj_priv
->gtt_space
);
2020 if (obj_priv
->gtt_space
== NULL
)
2023 if (obj_priv
->pin_count
!= 0) {
2024 DRM_ERROR("Attempting to unbind pinned buffer\n");
2028 /* blow away mappings if mapped through GTT */
2029 i915_gem_release_mmap(obj
);
2031 /* Move the object to the CPU domain to ensure that
2032 * any possible CPU writes while it's not in the GTT
2033 * are flushed when we go to remap it. This will
2034 * also ensure that all pending GPU writes are finished
2037 ret
= i915_gem_object_set_to_cpu_domain(obj
, 1);
2039 if (ret
!= -ERESTARTSYS
)
2040 DRM_ERROR("set_domain failed: %d\n", ret
);
2044 BUG_ON(obj_priv
->active
);
2046 /* release the fence reg _after_ flushing */
2047 if (obj_priv
->fence_reg
!= I915_FENCE_REG_NONE
)
2048 i915_gem_clear_fence_reg(obj
);
2050 if (obj_priv
->agp_mem
!= NULL
) {
2051 drm_unbind_agp(obj_priv
->agp_mem
);
2052 drm_free_agp(obj_priv
->agp_mem
, obj
->size
/ PAGE_SIZE
);
2053 obj_priv
->agp_mem
= NULL
;
2056 i915_gem_object_put_pages(obj
);
2057 BUG_ON(obj_priv
->pages_refcount
);
2059 if (obj_priv
->gtt_space
) {
2060 atomic_dec(&dev
->gtt_count
);
2061 atomic_sub(obj
->size
, &dev
->gtt_memory
);
2063 drm_mm_put_block(obj_priv
->gtt_space
);
2064 obj_priv
->gtt_space
= NULL
;
2067 /* Remove ourselves from the LRU list if present. */
2068 if (!list_empty(&obj_priv
->list
))
2069 list_del_init(&obj_priv
->list
);
2071 if (i915_gem_object_is_purgeable(obj_priv
))
2072 i915_gem_object_truncate(obj
);
2074 trace_i915_gem_object_unbind(obj
);
2079 static struct drm_gem_object
*
2080 i915_gem_find_inactive_object(struct drm_device
*dev
, int min_size
)
2082 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2083 struct drm_i915_gem_object
*obj_priv
;
2084 struct drm_gem_object
*best
= NULL
;
2085 struct drm_gem_object
*first
= NULL
;
2087 /* Try to find the smallest clean object */
2088 list_for_each_entry(obj_priv
, &dev_priv
->mm
.inactive_list
, list
) {
2089 struct drm_gem_object
*obj
= obj_priv
->obj
;
2090 if (obj
->size
>= min_size
) {
2091 if ((!obj_priv
->dirty
||
2092 i915_gem_object_is_purgeable(obj_priv
)) &&
2093 (!best
|| obj
->size
< best
->size
)) {
2095 if (best
->size
== min_size
)
2103 return best
? best
: first
;
2107 i915_gem_evict_everything(struct drm_device
*dev
)
2109 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2114 spin_lock(&dev_priv
->mm
.active_list_lock
);
2115 lists_empty
= (list_empty(&dev_priv
->mm
.inactive_list
) &&
2116 list_empty(&dev_priv
->mm
.flushing_list
) &&
2117 list_empty(&dev_priv
->mm
.active_list
));
2118 spin_unlock(&dev_priv
->mm
.active_list_lock
);
2123 /* Flush everything (on to the inactive lists) and evict */
2124 i915_gem_flush(dev
, I915_GEM_GPU_DOMAINS
, I915_GEM_GPU_DOMAINS
);
2125 seqno
= i915_add_request(dev
, NULL
, I915_GEM_GPU_DOMAINS
);
2129 ret
= i915_wait_request(dev
, seqno
);
2133 BUG_ON(!list_empty(&dev_priv
->mm
.flushing_list
));
2135 ret
= i915_gem_evict_from_inactive_list(dev
);
2139 spin_lock(&dev_priv
->mm
.active_list_lock
);
2140 lists_empty
= (list_empty(&dev_priv
->mm
.inactive_list
) &&
2141 list_empty(&dev_priv
->mm
.flushing_list
) &&
2142 list_empty(&dev_priv
->mm
.active_list
));
2143 spin_unlock(&dev_priv
->mm
.active_list_lock
);
2144 BUG_ON(!lists_empty
);
2150 i915_gem_evict_something(struct drm_device
*dev
, int min_size
)
2152 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2153 struct drm_gem_object
*obj
;
2157 i915_gem_retire_requests(dev
);
2159 /* If there's an inactive buffer available now, grab it
2162 obj
= i915_gem_find_inactive_object(dev
, min_size
);
2164 struct drm_i915_gem_object
*obj_priv
;
2167 DRM_INFO("%s: evicting %p\n", __func__
, obj
);
2169 obj_priv
= obj
->driver_private
;
2170 BUG_ON(obj_priv
->pin_count
!= 0);
2171 BUG_ON(obj_priv
->active
);
2173 /* Wait on the rendering and unbind the buffer. */
2174 return i915_gem_object_unbind(obj
);
2177 /* If we didn't get anything, but the ring is still processing
2178 * things, wait for the next to finish and hopefully leave us
2179 * a buffer to evict.
2181 if (!list_empty(&dev_priv
->mm
.request_list
)) {
2182 struct drm_i915_gem_request
*request
;
2184 request
= list_first_entry(&dev_priv
->mm
.request_list
,
2185 struct drm_i915_gem_request
,
2188 ret
= i915_wait_request(dev
, request
->seqno
);
2195 /* If we didn't have anything on the request list but there
2196 * are buffers awaiting a flush, emit one and try again.
2197 * When we wait on it, those buffers waiting for that flush
2198 * will get moved to inactive.
2200 if (!list_empty(&dev_priv
->mm
.flushing_list
)) {
2201 struct drm_i915_gem_object
*obj_priv
;
2203 /* Find an object that we can immediately reuse */
2204 list_for_each_entry(obj_priv
, &dev_priv
->mm
.flushing_list
, list
) {
2205 obj
= obj_priv
->obj
;
2206 if (obj
->size
>= min_size
)
2218 seqno
= i915_add_request(dev
, NULL
, obj
->write_domain
);
2222 ret
= i915_wait_request(dev
, seqno
);
2230 /* If we didn't do any of the above, there's no single buffer
2231 * large enough to swap out for the new one, so just evict
2232 * everything and start again. (This should be rare.)
2234 if (!list_empty (&dev_priv
->mm
.inactive_list
))
2235 return i915_gem_evict_from_inactive_list(dev
);
2237 return i915_gem_evict_everything(dev
);
2242 i915_gem_object_get_pages(struct drm_gem_object
*obj
,
2245 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
2247 struct address_space
*mapping
;
2248 struct inode
*inode
;
2251 if (obj_priv
->pages_refcount
++ != 0)
2254 /* Get the list of pages out of our struct file. They'll be pinned
2255 * at this point until we release them.
2257 page_count
= obj
->size
/ PAGE_SIZE
;
2258 BUG_ON(obj_priv
->pages
!= NULL
);
2259 obj_priv
->pages
= drm_calloc_large(page_count
, sizeof(struct page
*));
2260 if (obj_priv
->pages
== NULL
) {
2261 obj_priv
->pages_refcount
--;
2265 inode
= obj
->filp
->f_path
.dentry
->d_inode
;
2266 mapping
= inode
->i_mapping
;
2267 for (i
= 0; i
< page_count
; i
++) {
2268 page
= read_cache_page_gfp(mapping
, i
,
2276 obj_priv
->pages
[i
] = page
;
2279 if (obj_priv
->tiling_mode
!= I915_TILING_NONE
)
2280 i915_gem_object_do_bit_17_swizzle(obj
);
2286 page_cache_release(obj_priv
->pages
[i
]);
2288 drm_free_large(obj_priv
->pages
);
2289 obj_priv
->pages
= NULL
;
2290 obj_priv
->pages_refcount
--;
2291 return PTR_ERR(page
);
2294 static void i965_write_fence_reg(struct drm_i915_fence_reg
*reg
)
2296 struct drm_gem_object
*obj
= reg
->obj
;
2297 struct drm_device
*dev
= obj
->dev
;
2298 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2299 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
2300 int regnum
= obj_priv
->fence_reg
;
2303 val
= (uint64_t)((obj_priv
->gtt_offset
+ obj
->size
- 4096) &
2305 val
|= obj_priv
->gtt_offset
& 0xfffff000;
2306 val
|= ((obj_priv
->stride
/ 128) - 1) << I965_FENCE_PITCH_SHIFT
;
2307 if (obj_priv
->tiling_mode
== I915_TILING_Y
)
2308 val
|= 1 << I965_FENCE_TILING_Y_SHIFT
;
2309 val
|= I965_FENCE_REG_VALID
;
2311 I915_WRITE64(FENCE_REG_965_0
+ (regnum
* 8), val
);
2314 static void i915_write_fence_reg(struct drm_i915_fence_reg
*reg
)
2316 struct drm_gem_object
*obj
= reg
->obj
;
2317 struct drm_device
*dev
= obj
->dev
;
2318 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2319 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
2320 int regnum
= obj_priv
->fence_reg
;
2322 uint32_t fence_reg
, val
;
2325 if ((obj_priv
->gtt_offset
& ~I915_FENCE_START_MASK
) ||
2326 (obj_priv
->gtt_offset
& (obj
->size
- 1))) {
2327 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
2328 __func__
, obj_priv
->gtt_offset
, obj
->size
);
2332 if (obj_priv
->tiling_mode
== I915_TILING_Y
&&
2333 HAS_128_BYTE_Y_TILING(dev
))
2338 /* Note: pitch better be a power of two tile widths */
2339 pitch_val
= obj_priv
->stride
/ tile_width
;
2340 pitch_val
= ffs(pitch_val
) - 1;
2342 if (obj_priv
->tiling_mode
== I915_TILING_Y
&&
2343 HAS_128_BYTE_Y_TILING(dev
))
2344 WARN_ON(pitch_val
> I830_FENCE_MAX_PITCH_VAL
);
2346 WARN_ON(pitch_val
> I915_FENCE_MAX_PITCH_VAL
);
2348 val
= obj_priv
->gtt_offset
;
2349 if (obj_priv
->tiling_mode
== I915_TILING_Y
)
2350 val
|= 1 << I830_FENCE_TILING_Y_SHIFT
;
2351 val
|= I915_FENCE_SIZE_BITS(obj
->size
);
2352 val
|= pitch_val
<< I830_FENCE_PITCH_SHIFT
;
2353 val
|= I830_FENCE_REG_VALID
;
2356 fence_reg
= FENCE_REG_830_0
+ (regnum
* 4);
2358 fence_reg
= FENCE_REG_945_8
+ ((regnum
- 8) * 4);
2359 I915_WRITE(fence_reg
, val
);
2362 static void i830_write_fence_reg(struct drm_i915_fence_reg
*reg
)
2364 struct drm_gem_object
*obj
= reg
->obj
;
2365 struct drm_device
*dev
= obj
->dev
;
2366 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2367 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
2368 int regnum
= obj_priv
->fence_reg
;
2371 uint32_t fence_size_bits
;
2373 if ((obj_priv
->gtt_offset
& ~I830_FENCE_START_MASK
) ||
2374 (obj_priv
->gtt_offset
& (obj
->size
- 1))) {
2375 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
2376 __func__
, obj_priv
->gtt_offset
);
2380 pitch_val
= obj_priv
->stride
/ 128;
2381 pitch_val
= ffs(pitch_val
) - 1;
2382 WARN_ON(pitch_val
> I830_FENCE_MAX_PITCH_VAL
);
2384 val
= obj_priv
->gtt_offset
;
2385 if (obj_priv
->tiling_mode
== I915_TILING_Y
)
2386 val
|= 1 << I830_FENCE_TILING_Y_SHIFT
;
2387 fence_size_bits
= I830_FENCE_SIZE_BITS(obj
->size
);
2388 WARN_ON(fence_size_bits
& ~0x00000f00);
2389 val
|= fence_size_bits
;
2390 val
|= pitch_val
<< I830_FENCE_PITCH_SHIFT
;
2391 val
|= I830_FENCE_REG_VALID
;
2393 I915_WRITE(FENCE_REG_830_0
+ (regnum
* 4), val
);
2397 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2398 * @obj: object to map through a fence reg
2400 * When mapping objects through the GTT, userspace wants to be able to write
2401 * to them without having to worry about swizzling if the object is tiled.
2403 * This function walks the fence regs looking for a free one for @obj,
2404 * stealing one if it can't find any.
2406 * It then sets up the reg based on the object's properties: address, pitch
2407 * and tiling format.
2410 i915_gem_object_get_fence_reg(struct drm_gem_object
*obj
)
2412 struct drm_device
*dev
= obj
->dev
;
2413 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
2414 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
2415 struct drm_i915_fence_reg
*reg
= NULL
;
2416 struct drm_i915_gem_object
*old_obj_priv
= NULL
;
2419 /* Just update our place in the LRU if our fence is getting used. */
2420 if (obj_priv
->fence_reg
!= I915_FENCE_REG_NONE
) {
2421 list_move_tail(&obj_priv
->fence_list
, &dev_priv
->mm
.fence_list
);
2425 switch (obj_priv
->tiling_mode
) {
2426 case I915_TILING_NONE
:
2427 WARN(1, "allocating a fence for non-tiled object?\n");
2430 if (!obj_priv
->stride
)
2432 WARN((obj_priv
->stride
& (512 - 1)),
2433 "object 0x%08x is X tiled but has non-512B pitch\n",
2434 obj_priv
->gtt_offset
);
2437 if (!obj_priv
->stride
)
2439 WARN((obj_priv
->stride
& (128 - 1)),
2440 "object 0x%08x is Y tiled but has non-128B pitch\n",
2441 obj_priv
->gtt_offset
);
2445 /* First try to find a free reg */
2447 for (i
= dev_priv
->fence_reg_start
; i
< dev_priv
->num_fence_regs
; i
++) {
2448 reg
= &dev_priv
->fence_regs
[i
];
2452 old_obj_priv
= reg
->obj
->driver_private
;
2453 if (!old_obj_priv
->pin_count
)
2457 /* None available, try to steal one or wait for a user to finish */
2458 if (i
== dev_priv
->num_fence_regs
) {
2459 struct drm_gem_object
*old_obj
= NULL
;
2464 list_for_each_entry(old_obj_priv
, &dev_priv
->mm
.fence_list
,
2466 old_obj
= old_obj_priv
->obj
;
2468 if (old_obj_priv
->pin_count
)
2471 /* Take a reference, as otherwise the wait_rendering
2472 * below may cause the object to get freed out from
2475 drm_gem_object_reference(old_obj
);
2477 /* i915 uses fences for GPU access to tiled buffers */
2478 if (IS_I965G(dev
) || !old_obj_priv
->active
)
2481 /* This brings the object to the head of the LRU if it
2482 * had been written to. The only way this should
2483 * result in us waiting longer than the expected
2484 * optimal amount of time is if there was a
2485 * fence-using buffer later that was read-only.
2487 i915_gem_object_flush_gpu_write_domain(old_obj
);
2488 ret
= i915_gem_object_wait_rendering(old_obj
);
2490 drm_gem_object_unreference(old_obj
);
2498 * Zap this virtual mapping so we can set up a fence again
2499 * for this object next time we need it.
2501 i915_gem_release_mmap(old_obj
);
2503 i
= old_obj_priv
->fence_reg
;
2504 reg
= &dev_priv
->fence_regs
[i
];
2506 old_obj_priv
->fence_reg
= I915_FENCE_REG_NONE
;
2507 list_del_init(&old_obj_priv
->fence_list
);
2509 drm_gem_object_unreference(old_obj
);
2512 obj_priv
->fence_reg
= i
;
2513 list_add_tail(&obj_priv
->fence_list
, &dev_priv
->mm
.fence_list
);
2518 i965_write_fence_reg(reg
);
2519 else if (IS_I9XX(dev
))
2520 i915_write_fence_reg(reg
);
2522 i830_write_fence_reg(reg
);
2524 trace_i915_gem_object_get_fence(obj
, i
, obj_priv
->tiling_mode
);
2530 * i915_gem_clear_fence_reg - clear out fence register info
2531 * @obj: object to clear
2533 * Zeroes out the fence register itself and clears out the associated
2534 * data structures in dev_priv and obj_priv.
2537 i915_gem_clear_fence_reg(struct drm_gem_object
*obj
)
2539 struct drm_device
*dev
= obj
->dev
;
2540 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2541 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
2544 I915_WRITE64(FENCE_REG_965_0
+ (obj_priv
->fence_reg
* 8), 0);
2548 if (obj_priv
->fence_reg
< 8)
2549 fence_reg
= FENCE_REG_830_0
+ obj_priv
->fence_reg
* 4;
2551 fence_reg
= FENCE_REG_945_8
+ (obj_priv
->fence_reg
-
2554 I915_WRITE(fence_reg
, 0);
2557 dev_priv
->fence_regs
[obj_priv
->fence_reg
].obj
= NULL
;
2558 obj_priv
->fence_reg
= I915_FENCE_REG_NONE
;
2559 list_del_init(&obj_priv
->fence_list
);
2563 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2564 * to the buffer to finish, and then resets the fence register.
2565 * @obj: tiled object holding a fence register.
2567 * Zeroes out the fence register itself and clears out the associated
2568 * data structures in dev_priv and obj_priv.
2571 i915_gem_object_put_fence_reg(struct drm_gem_object
*obj
)
2573 struct drm_device
*dev
= obj
->dev
;
2574 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
2576 if (obj_priv
->fence_reg
== I915_FENCE_REG_NONE
)
2579 /* On the i915, GPU access to tiled buffers is via a fence,
2580 * therefore we must wait for any outstanding access to complete
2581 * before clearing the fence.
2583 if (!IS_I965G(dev
)) {
2586 i915_gem_object_flush_gpu_write_domain(obj
);
2587 i915_gem_object_flush_gtt_write_domain(obj
);
2588 ret
= i915_gem_object_wait_rendering(obj
);
2593 i915_gem_clear_fence_reg (obj
);
2599 * Finds free space in the GTT aperture and binds the object there.
2602 i915_gem_object_bind_to_gtt(struct drm_gem_object
*obj
, unsigned alignment
)
2604 struct drm_device
*dev
= obj
->dev
;
2605 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2606 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
2607 struct drm_mm_node
*free_space
;
2608 gfp_t gfpmask
= __GFP_NORETRY
| __GFP_NOWARN
;
2611 if (obj_priv
->madv
!= I915_MADV_WILLNEED
) {
2612 DRM_ERROR("Attempting to bind a purgeable object\n");
2617 alignment
= i915_gem_get_gtt_alignment(obj
);
2618 if (alignment
& (i915_gem_get_gtt_alignment(obj
) - 1)) {
2619 DRM_ERROR("Invalid object alignment requested %u\n", alignment
);
2623 /* If the object is bigger than the entire aperture, reject it early
2624 * before evicting everything in a vain attempt to find space.
2626 if (obj
->size
> dev
->gtt_total
) {
2627 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2632 free_space
= drm_mm_search_free(&dev_priv
->mm
.gtt_space
,
2633 obj
->size
, alignment
, 0);
2634 if (free_space
!= NULL
) {
2635 obj_priv
->gtt_space
= drm_mm_get_block(free_space
, obj
->size
,
2637 if (obj_priv
->gtt_space
!= NULL
) {
2638 obj_priv
->gtt_space
->private = obj
;
2639 obj_priv
->gtt_offset
= obj_priv
->gtt_space
->start
;
2642 if (obj_priv
->gtt_space
== NULL
) {
2643 /* If the gtt is empty and we're still having trouble
2644 * fitting our object in, we're out of memory.
2647 DRM_INFO("%s: GTT full, evicting something\n", __func__
);
2649 ret
= i915_gem_evict_something(dev
, obj
->size
);
2657 DRM_INFO("Binding object of size %zd at 0x%08x\n",
2658 obj
->size
, obj_priv
->gtt_offset
);
2660 ret
= i915_gem_object_get_pages(obj
, gfpmask
);
2662 drm_mm_put_block(obj_priv
->gtt_space
);
2663 obj_priv
->gtt_space
= NULL
;
2665 if (ret
== -ENOMEM
) {
2666 /* first try to clear up some space from the GTT */
2667 ret
= i915_gem_evict_something(dev
, obj
->size
);
2669 /* now try to shrink everyone else */
2684 /* Create an AGP memory structure pointing at our pages, and bind it
2687 obj_priv
->agp_mem
= drm_agp_bind_pages(dev
,
2689 obj
->size
>> PAGE_SHIFT
,
2690 obj_priv
->gtt_offset
,
2691 obj_priv
->agp_type
);
2692 if (obj_priv
->agp_mem
== NULL
) {
2693 i915_gem_object_put_pages(obj
);
2694 drm_mm_put_block(obj_priv
->gtt_space
);
2695 obj_priv
->gtt_space
= NULL
;
2697 ret
= i915_gem_evict_something(dev
, obj
->size
);
2703 atomic_inc(&dev
->gtt_count
);
2704 atomic_add(obj
->size
, &dev
->gtt_memory
);
2706 /* Assert that the object is not currently in any GPU domain. As it
2707 * wasn't in the GTT, there shouldn't be any way it could have been in
2710 BUG_ON(obj
->read_domains
& I915_GEM_GPU_DOMAINS
);
2711 BUG_ON(obj
->write_domain
& I915_GEM_GPU_DOMAINS
);
2713 trace_i915_gem_object_bind(obj
, obj_priv
->gtt_offset
);
2719 i915_gem_clflush_object(struct drm_gem_object
*obj
)
2721 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
2723 /* If we don't have a page list set up, then we're not pinned
2724 * to GPU, and we can ignore the cache flush because it'll happen
2725 * again at bind time.
2727 if (obj_priv
->pages
== NULL
)
2730 trace_i915_gem_object_clflush(obj
);
2732 drm_clflush_pages(obj_priv
->pages
, obj
->size
/ PAGE_SIZE
);
2735 /** Flushes any GPU write domain for the object if it's dirty. */
2737 i915_gem_object_flush_gpu_write_domain(struct drm_gem_object
*obj
)
2739 struct drm_device
*dev
= obj
->dev
;
2741 uint32_t old_write_domain
;
2743 if ((obj
->write_domain
& I915_GEM_GPU_DOMAINS
) == 0)
2746 /* Queue the GPU write cache flushing we need. */
2747 old_write_domain
= obj
->write_domain
;
2748 i915_gem_flush(dev
, 0, obj
->write_domain
);
2749 seqno
= i915_add_request(dev
, NULL
, obj
->write_domain
);
2750 BUG_ON(obj
->write_domain
);
2751 i915_gem_object_move_to_active(obj
, seqno
);
2753 trace_i915_gem_object_change_domain(obj
,
2758 /** Flushes the GTT write domain for the object if it's dirty. */
2760 i915_gem_object_flush_gtt_write_domain(struct drm_gem_object
*obj
)
2762 uint32_t old_write_domain
;
2764 if (obj
->write_domain
!= I915_GEM_DOMAIN_GTT
)
2767 /* No actual flushing is required for the GTT write domain. Writes
2768 * to it immediately go to main memory as far as we know, so there's
2769 * no chipset flush. It also doesn't land in render cache.
2771 old_write_domain
= obj
->write_domain
;
2772 obj
->write_domain
= 0;
2774 trace_i915_gem_object_change_domain(obj
,
2779 /** Flushes the CPU write domain for the object if it's dirty. */
2781 i915_gem_object_flush_cpu_write_domain(struct drm_gem_object
*obj
)
2783 struct drm_device
*dev
= obj
->dev
;
2784 uint32_t old_write_domain
;
2786 if (obj
->write_domain
!= I915_GEM_DOMAIN_CPU
)
2789 i915_gem_clflush_object(obj
);
2790 drm_agp_chipset_flush(dev
);
2791 old_write_domain
= obj
->write_domain
;
2792 obj
->write_domain
= 0;
2794 trace_i915_gem_object_change_domain(obj
,
2800 * Moves a single object to the GTT read, and possibly write domain.
2802 * This function returns when the move is complete, including waiting on
2806 i915_gem_object_set_to_gtt_domain(struct drm_gem_object
*obj
, int write
)
2808 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
2809 uint32_t old_write_domain
, old_read_domains
;
2812 /* Not valid to be called on unbound objects. */
2813 if (obj_priv
->gtt_space
== NULL
)
2816 i915_gem_object_flush_gpu_write_domain(obj
);
2817 /* Wait on any GPU rendering and flushing to occur. */
2818 ret
= i915_gem_object_wait_rendering(obj
);
2822 old_write_domain
= obj
->write_domain
;
2823 old_read_domains
= obj
->read_domains
;
2825 /* If we're writing through the GTT domain, then CPU and GPU caches
2826 * will need to be invalidated at next use.
2829 obj
->read_domains
&= I915_GEM_DOMAIN_GTT
;
2831 i915_gem_object_flush_cpu_write_domain(obj
);
2833 /* It should now be out of any other write domains, and we can update
2834 * the domain values for our changes.
2836 BUG_ON((obj
->write_domain
& ~I915_GEM_DOMAIN_GTT
) != 0);
2837 obj
->read_domains
|= I915_GEM_DOMAIN_GTT
;
2839 obj
->write_domain
= I915_GEM_DOMAIN_GTT
;
2840 obj_priv
->dirty
= 1;
2843 trace_i915_gem_object_change_domain(obj
,
2851 * Prepare buffer for display plane. Use uninterruptible for possible flush
2852 * wait, as in modesetting process we're not supposed to be interrupted.
2855 i915_gem_object_set_to_display_plane(struct drm_gem_object
*obj
)
2857 struct drm_device
*dev
= obj
->dev
;
2858 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
2859 uint32_t old_write_domain
, old_read_domains
;
2862 /* Not valid to be called on unbound objects. */
2863 if (obj_priv
->gtt_space
== NULL
)
2866 i915_gem_object_flush_gpu_write_domain(obj
);
2868 /* Wait on any GPU rendering and flushing to occur. */
2869 if (obj_priv
->active
) {
2871 DRM_INFO("%s: object %p wait for seqno %08x\n",
2872 __func__
, obj
, obj_priv
->last_rendering_seqno
);
2874 ret
= i915_do_wait_request(dev
, obj_priv
->last_rendering_seqno
, 0);
2879 old_write_domain
= obj
->write_domain
;
2880 old_read_domains
= obj
->read_domains
;
2882 obj
->read_domains
&= I915_GEM_DOMAIN_GTT
;
2884 i915_gem_object_flush_cpu_write_domain(obj
);
2886 /* It should now be out of any other write domains, and we can update
2887 * the domain values for our changes.
2889 BUG_ON((obj
->write_domain
& ~I915_GEM_DOMAIN_GTT
) != 0);
2890 obj
->read_domains
|= I915_GEM_DOMAIN_GTT
;
2891 obj
->write_domain
= I915_GEM_DOMAIN_GTT
;
2892 obj_priv
->dirty
= 1;
2894 trace_i915_gem_object_change_domain(obj
,
2902 * Moves a single object to the CPU read, and possibly write domain.
2904 * This function returns when the move is complete, including waiting on
2908 i915_gem_object_set_to_cpu_domain(struct drm_gem_object
*obj
, int write
)
2910 uint32_t old_write_domain
, old_read_domains
;
2913 i915_gem_object_flush_gpu_write_domain(obj
);
2914 /* Wait on any GPU rendering and flushing to occur. */
2915 ret
= i915_gem_object_wait_rendering(obj
);
2919 i915_gem_object_flush_gtt_write_domain(obj
);
2921 /* If we have a partially-valid cache of the object in the CPU,
2922 * finish invalidating it and free the per-page flags.
2924 i915_gem_object_set_to_full_cpu_read_domain(obj
);
2926 old_write_domain
= obj
->write_domain
;
2927 old_read_domains
= obj
->read_domains
;
2929 /* Flush the CPU cache if it's still invalid. */
2930 if ((obj
->read_domains
& I915_GEM_DOMAIN_CPU
) == 0) {
2931 i915_gem_clflush_object(obj
);
2933 obj
->read_domains
|= I915_GEM_DOMAIN_CPU
;
2936 /* It should now be out of any other write domains, and we can update
2937 * the domain values for our changes.
2939 BUG_ON((obj
->write_domain
& ~I915_GEM_DOMAIN_CPU
) != 0);
2941 /* If we're writing through the CPU, then the GPU read domains will
2942 * need to be invalidated at next use.
2945 obj
->read_domains
&= I915_GEM_DOMAIN_CPU
;
2946 obj
->write_domain
= I915_GEM_DOMAIN_CPU
;
2949 trace_i915_gem_object_change_domain(obj
,
2957 * Set the next domain for the specified object. This
2958 * may not actually perform the necessary flushing/invaliding though,
2959 * as that may want to be batched with other set_domain operations
2961 * This is (we hope) the only really tricky part of gem. The goal
2962 * is fairly simple -- track which caches hold bits of the object
2963 * and make sure they remain coherent. A few concrete examples may
2964 * help to explain how it works. For shorthand, we use the notation
2965 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2966 * a pair of read and write domain masks.
2968 * Case 1: the batch buffer
2974 * 5. Unmapped from GTT
2977 * Let's take these a step at a time
2980 * Pages allocated from the kernel may still have
2981 * cache contents, so we set them to (CPU, CPU) always.
2982 * 2. Written by CPU (using pwrite)
2983 * The pwrite function calls set_domain (CPU, CPU) and
2984 * this function does nothing (as nothing changes)
2986 * This function asserts that the object is not
2987 * currently in any GPU-based read or write domains
2989 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2990 * As write_domain is zero, this function adds in the
2991 * current read domains (CPU+COMMAND, 0).
2992 * flush_domains is set to CPU.
2993 * invalidate_domains is set to COMMAND
2994 * clflush is run to get data out of the CPU caches
2995 * then i915_dev_set_domain calls i915_gem_flush to
2996 * emit an MI_FLUSH and drm_agp_chipset_flush
2997 * 5. Unmapped from GTT
2998 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2999 * flush_domains and invalidate_domains end up both zero
3000 * so no flushing/invalidating happens
3004 * Case 2: The shared render buffer
3008 * 3. Read/written by GPU
3009 * 4. set_domain to (CPU,CPU)
3010 * 5. Read/written by CPU
3011 * 6. Read/written by GPU
3014 * Same as last example, (CPU, CPU)
3016 * Nothing changes (assertions find that it is not in the GPU)
3017 * 3. Read/written by GPU
3018 * execbuffer calls set_domain (RENDER, RENDER)
3019 * flush_domains gets CPU
3020 * invalidate_domains gets GPU
3022 * MI_FLUSH and drm_agp_chipset_flush
3023 * 4. set_domain (CPU, CPU)
3024 * flush_domains gets GPU
3025 * invalidate_domains gets CPU
3026 * wait_rendering (obj) to make sure all drawing is complete.
3027 * This will include an MI_FLUSH to get the data from GPU
3029 * clflush (obj) to invalidate the CPU cache
3030 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3031 * 5. Read/written by CPU
3032 * cache lines are loaded and dirtied
3033 * 6. Read written by GPU
3034 * Same as last GPU access
3036 * Case 3: The constant buffer
3041 * 4. Updated (written) by CPU again
3050 * flush_domains = CPU
3051 * invalidate_domains = RENDER
3054 * drm_agp_chipset_flush
3055 * 4. Updated (written) by CPU again
3057 * flush_domains = 0 (no previous write domain)
3058 * invalidate_domains = 0 (no new read domains)
3061 * flush_domains = CPU
3062 * invalidate_domains = RENDER
3065 * drm_agp_chipset_flush
3068 i915_gem_object_set_to_gpu_domain(struct drm_gem_object
*obj
)
3070 struct drm_device
*dev
= obj
->dev
;
3071 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
3072 uint32_t invalidate_domains
= 0;
3073 uint32_t flush_domains
= 0;
3074 uint32_t old_read_domains
;
3076 BUG_ON(obj
->pending_read_domains
& I915_GEM_DOMAIN_CPU
);
3077 BUG_ON(obj
->pending_write_domain
== I915_GEM_DOMAIN_CPU
);
3079 intel_mark_busy(dev
, obj
);
3082 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
3084 obj
->read_domains
, obj
->pending_read_domains
,
3085 obj
->write_domain
, obj
->pending_write_domain
);
3088 * If the object isn't moving to a new write domain,
3089 * let the object stay in multiple read domains
3091 if (obj
->pending_write_domain
== 0)
3092 obj
->pending_read_domains
|= obj
->read_domains
;
3094 obj_priv
->dirty
= 1;
3097 * Flush the current write domain if
3098 * the new read domains don't match. Invalidate
3099 * any read domains which differ from the old
3102 if (obj
->write_domain
&&
3103 obj
->write_domain
!= obj
->pending_read_domains
) {
3104 flush_domains
|= obj
->write_domain
;
3105 invalidate_domains
|=
3106 obj
->pending_read_domains
& ~obj
->write_domain
;
3109 * Invalidate any read caches which may have
3110 * stale data. That is, any new read domains.
3112 invalidate_domains
|= obj
->pending_read_domains
& ~obj
->read_domains
;
3113 if ((flush_domains
| invalidate_domains
) & I915_GEM_DOMAIN_CPU
) {
3115 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3116 __func__
, flush_domains
, invalidate_domains
);
3118 i915_gem_clflush_object(obj
);
3121 old_read_domains
= obj
->read_domains
;
3123 /* The actual obj->write_domain will be updated with
3124 * pending_write_domain after we emit the accumulated flush for all
3125 * of our domain changes in execbuffers (which clears objects'
3126 * write_domains). So if we have a current write domain that we
3127 * aren't changing, set pending_write_domain to that.
3129 if (flush_domains
== 0 && obj
->pending_write_domain
== 0)
3130 obj
->pending_write_domain
= obj
->write_domain
;
3131 obj
->read_domains
= obj
->pending_read_domains
;
3133 dev
->invalidate_domains
|= invalidate_domains
;
3134 dev
->flush_domains
|= flush_domains
;
3136 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3138 obj
->read_domains
, obj
->write_domain
,
3139 dev
->invalidate_domains
, dev
->flush_domains
);
3142 trace_i915_gem_object_change_domain(obj
,
3148 * Moves the object from a partially CPU read to a full one.
3150 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3151 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3154 i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object
*obj
)
3156 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
3158 if (!obj_priv
->page_cpu_valid
)
3161 /* If we're partially in the CPU read domain, finish moving it in.
3163 if (obj
->read_domains
& I915_GEM_DOMAIN_CPU
) {
3166 for (i
= 0; i
<= (obj
->size
- 1) / PAGE_SIZE
; i
++) {
3167 if (obj_priv
->page_cpu_valid
[i
])
3169 drm_clflush_pages(obj_priv
->pages
+ i
, 1);
3173 /* Free the page_cpu_valid mappings which are now stale, whether
3174 * or not we've got I915_GEM_DOMAIN_CPU.
3176 kfree(obj_priv
->page_cpu_valid
);
3177 obj_priv
->page_cpu_valid
= NULL
;
3181 * Set the CPU read domain on a range of the object.
3183 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3184 * not entirely valid. The page_cpu_valid member of the object flags which
3185 * pages have been flushed, and will be respected by
3186 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3187 * of the whole object.
3189 * This function returns when the move is complete, including waiting on
3193 i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object
*obj
,
3194 uint64_t offset
, uint64_t size
)
3196 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
3197 uint32_t old_read_domains
;
3200 if (offset
== 0 && size
== obj
->size
)
3201 return i915_gem_object_set_to_cpu_domain(obj
, 0);
3203 i915_gem_object_flush_gpu_write_domain(obj
);
3204 /* Wait on any GPU rendering and flushing to occur. */
3205 ret
= i915_gem_object_wait_rendering(obj
);
3208 i915_gem_object_flush_gtt_write_domain(obj
);
3210 /* If we're already fully in the CPU read domain, we're done. */
3211 if (obj_priv
->page_cpu_valid
== NULL
&&
3212 (obj
->read_domains
& I915_GEM_DOMAIN_CPU
) != 0)
3215 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3216 * newly adding I915_GEM_DOMAIN_CPU
3218 if (obj_priv
->page_cpu_valid
== NULL
) {
3219 obj_priv
->page_cpu_valid
= kzalloc(obj
->size
/ PAGE_SIZE
,
3221 if (obj_priv
->page_cpu_valid
== NULL
)
3223 } else if ((obj
->read_domains
& I915_GEM_DOMAIN_CPU
) == 0)
3224 memset(obj_priv
->page_cpu_valid
, 0, obj
->size
/ PAGE_SIZE
);
3226 /* Flush the cache on any pages that are still invalid from the CPU's
3229 for (i
= offset
/ PAGE_SIZE
; i
<= (offset
+ size
- 1) / PAGE_SIZE
;
3231 if (obj_priv
->page_cpu_valid
[i
])
3234 drm_clflush_pages(obj_priv
->pages
+ i
, 1);
3236 obj_priv
->page_cpu_valid
[i
] = 1;
3239 /* It should now be out of any other write domains, and we can update
3240 * the domain values for our changes.
3242 BUG_ON((obj
->write_domain
& ~I915_GEM_DOMAIN_CPU
) != 0);
3244 old_read_domains
= obj
->read_domains
;
3245 obj
->read_domains
|= I915_GEM_DOMAIN_CPU
;
3247 trace_i915_gem_object_change_domain(obj
,
3255 * Pin an object to the GTT and evaluate the relocations landing in it.
3258 i915_gem_object_pin_and_relocate(struct drm_gem_object
*obj
,
3259 struct drm_file
*file_priv
,
3260 struct drm_i915_gem_exec_object
*entry
,
3261 struct drm_i915_gem_relocation_entry
*relocs
)
3263 struct drm_device
*dev
= obj
->dev
;
3264 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
3265 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
3267 void __iomem
*reloc_page
;
3269 /* Choose the GTT offset for our buffer and put it there. */
3270 ret
= i915_gem_object_pin(obj
, (uint32_t) entry
->alignment
);
3274 entry
->offset
= obj_priv
->gtt_offset
;
3276 /* Apply the relocations, using the GTT aperture to avoid cache
3277 * flushing requirements.
3279 for (i
= 0; i
< entry
->relocation_count
; i
++) {
3280 struct drm_i915_gem_relocation_entry
*reloc
= &relocs
[i
];
3281 struct drm_gem_object
*target_obj
;
3282 struct drm_i915_gem_object
*target_obj_priv
;
3283 uint32_t reloc_val
, reloc_offset
;
3284 uint32_t __iomem
*reloc_entry
;
3286 target_obj
= drm_gem_object_lookup(obj
->dev
, file_priv
,
3287 reloc
->target_handle
);
3288 if (target_obj
== NULL
) {
3289 i915_gem_object_unpin(obj
);
3292 target_obj_priv
= target_obj
->driver_private
;
3295 DRM_INFO("%s: obj %p offset %08x target %d "
3296 "read %08x write %08x gtt %08x "
3297 "presumed %08x delta %08x\n",
3300 (int) reloc
->offset
,
3301 (int) reloc
->target_handle
,
3302 (int) reloc
->read_domains
,
3303 (int) reloc
->write_domain
,
3304 (int) target_obj_priv
->gtt_offset
,
3305 (int) reloc
->presumed_offset
,
3309 /* The target buffer should have appeared before us in the
3310 * exec_object list, so it should have a GTT space bound by now.
3312 if (target_obj_priv
->gtt_space
== NULL
) {
3313 DRM_ERROR("No GTT space found for object %d\n",
3314 reloc
->target_handle
);
3315 drm_gem_object_unreference(target_obj
);
3316 i915_gem_object_unpin(obj
);
3320 /* Validate that the target is in a valid r/w GPU domain */
3321 if (reloc
->write_domain
& I915_GEM_DOMAIN_CPU
||
3322 reloc
->read_domains
& I915_GEM_DOMAIN_CPU
) {
3323 DRM_ERROR("reloc with read/write CPU domains: "
3324 "obj %p target %d offset %d "
3325 "read %08x write %08x",
3326 obj
, reloc
->target_handle
,
3327 (int) reloc
->offset
,
3328 reloc
->read_domains
,
3329 reloc
->write_domain
);
3330 drm_gem_object_unreference(target_obj
);
3331 i915_gem_object_unpin(obj
);
3334 if (reloc
->write_domain
&& target_obj
->pending_write_domain
&&
3335 reloc
->write_domain
!= target_obj
->pending_write_domain
) {
3336 DRM_ERROR("Write domain conflict: "
3337 "obj %p target %d offset %d "
3338 "new %08x old %08x\n",
3339 obj
, reloc
->target_handle
,
3340 (int) reloc
->offset
,
3341 reloc
->write_domain
,
3342 target_obj
->pending_write_domain
);
3343 drm_gem_object_unreference(target_obj
);
3344 i915_gem_object_unpin(obj
);
3348 target_obj
->pending_read_domains
|= reloc
->read_domains
;
3349 target_obj
->pending_write_domain
|= reloc
->write_domain
;
3351 /* If the relocation already has the right value in it, no
3352 * more work needs to be done.
3354 if (target_obj_priv
->gtt_offset
== reloc
->presumed_offset
) {
3355 drm_gem_object_unreference(target_obj
);
3359 /* Check that the relocation address is valid... */
3360 if (reloc
->offset
> obj
->size
- 4) {
3361 DRM_ERROR("Relocation beyond object bounds: "
3362 "obj %p target %d offset %d size %d.\n",
3363 obj
, reloc
->target_handle
,
3364 (int) reloc
->offset
, (int) obj
->size
);
3365 drm_gem_object_unreference(target_obj
);
3366 i915_gem_object_unpin(obj
);
3369 if (reloc
->offset
& 3) {
3370 DRM_ERROR("Relocation not 4-byte aligned: "
3371 "obj %p target %d offset %d.\n",
3372 obj
, reloc
->target_handle
,
3373 (int) reloc
->offset
);
3374 drm_gem_object_unreference(target_obj
);
3375 i915_gem_object_unpin(obj
);
3379 /* and points to somewhere within the target object. */
3380 if (reloc
->delta
>= target_obj
->size
) {
3381 DRM_ERROR("Relocation beyond target object bounds: "
3382 "obj %p target %d delta %d size %d.\n",
3383 obj
, reloc
->target_handle
,
3384 (int) reloc
->delta
, (int) target_obj
->size
);
3385 drm_gem_object_unreference(target_obj
);
3386 i915_gem_object_unpin(obj
);
3390 ret
= i915_gem_object_set_to_gtt_domain(obj
, 1);
3392 drm_gem_object_unreference(target_obj
);
3393 i915_gem_object_unpin(obj
);
3397 /* Map the page containing the relocation we're going to
3400 reloc_offset
= obj_priv
->gtt_offset
+ reloc
->offset
;
3401 reloc_page
= io_mapping_map_atomic_wc(dev_priv
->mm
.gtt_mapping
,
3404 reloc_entry
= (uint32_t __iomem
*)(reloc_page
+
3405 (reloc_offset
& (PAGE_SIZE
- 1)));
3406 reloc_val
= target_obj_priv
->gtt_offset
+ reloc
->delta
;
3409 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
3410 obj
, (unsigned int) reloc
->offset
,
3411 readl(reloc_entry
), reloc_val
);
3413 writel(reloc_val
, reloc_entry
);
3414 io_mapping_unmap_atomic(reloc_page
);
3416 /* The updated presumed offset for this entry will be
3417 * copied back out to the user.
3419 reloc
->presumed_offset
= target_obj_priv
->gtt_offset
;
3421 drm_gem_object_unreference(target_obj
);
3426 i915_gem_dump_object(obj
, 128, __func__
, ~0);
3431 /** Dispatch a batchbuffer to the ring
3434 i915_dispatch_gem_execbuffer(struct drm_device
*dev
,
3435 struct drm_i915_gem_execbuffer
*exec
,
3436 struct drm_clip_rect
*cliprects
,
3437 uint64_t exec_offset
)
3439 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
3440 int nbox
= exec
->num_cliprects
;
3442 uint32_t exec_start
, exec_len
;
3445 exec_start
= (uint32_t) exec_offset
+ exec
->batch_start_offset
;
3446 exec_len
= (uint32_t) exec
->batch_len
;
3448 trace_i915_gem_request_submit(dev
, dev_priv
->mm
.next_gem_seqno
+ 1);
3450 count
= nbox
? nbox
: 1;
3452 for (i
= 0; i
< count
; i
++) {
3454 int ret
= i915_emit_box(dev
, cliprects
, i
,
3455 exec
->DR1
, exec
->DR4
);
3460 if (IS_I830(dev
) || IS_845G(dev
)) {
3462 OUT_RING(MI_BATCH_BUFFER
);
3463 OUT_RING(exec_start
| MI_BATCH_NON_SECURE
);
3464 OUT_RING(exec_start
+ exec_len
- 4);
3469 if (IS_I965G(dev
)) {
3470 OUT_RING(MI_BATCH_BUFFER_START
|
3472 MI_BATCH_NON_SECURE_I965
);
3473 OUT_RING(exec_start
);
3475 OUT_RING(MI_BATCH_BUFFER_START
|
3477 OUT_RING(exec_start
| MI_BATCH_NON_SECURE
);
3483 /* XXX breadcrumb */
3487 /* Throttle our rendering by waiting until the ring has completed our requests
3488 * emitted over 20 msec ago.
3490 * Note that if we were to use the current jiffies each time around the loop,
3491 * we wouldn't escape the function with any frames outstanding if the time to
3492 * render a frame was over 20ms.
3494 * This should get us reasonable parallelism between CPU and GPU but also
3495 * relatively low latency when blocking on a particular request to finish.
3498 i915_gem_ring_throttle(struct drm_device
*dev
, struct drm_file
*file_priv
)
3500 struct drm_i915_file_private
*i915_file_priv
= file_priv
->driver_priv
;
3502 unsigned long recent_enough
= jiffies
- msecs_to_jiffies(20);
3504 mutex_lock(&dev
->struct_mutex
);
3505 while (!list_empty(&i915_file_priv
->mm
.request_list
)) {
3506 struct drm_i915_gem_request
*request
;
3508 request
= list_first_entry(&i915_file_priv
->mm
.request_list
,
3509 struct drm_i915_gem_request
,
3512 if (time_after_eq(request
->emitted_jiffies
, recent_enough
))
3515 ret
= i915_wait_request(dev
, request
->seqno
);
3519 mutex_unlock(&dev
->struct_mutex
);
3525 i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object
*exec_list
,
3526 uint32_t buffer_count
,
3527 struct drm_i915_gem_relocation_entry
**relocs
)
3529 uint32_t reloc_count
= 0, reloc_index
= 0, i
;
3533 for (i
= 0; i
< buffer_count
; i
++) {
3534 if (reloc_count
+ exec_list
[i
].relocation_count
< reloc_count
)
3536 reloc_count
+= exec_list
[i
].relocation_count
;
3539 *relocs
= drm_calloc_large(reloc_count
, sizeof(**relocs
));
3540 if (*relocs
== NULL
)
3543 for (i
= 0; i
< buffer_count
; i
++) {
3544 struct drm_i915_gem_relocation_entry __user
*user_relocs
;
3546 user_relocs
= (void __user
*)(uintptr_t)exec_list
[i
].relocs_ptr
;
3548 ret
= copy_from_user(&(*relocs
)[reloc_index
],
3550 exec_list
[i
].relocation_count
*
3553 drm_free_large(*relocs
);
3558 reloc_index
+= exec_list
[i
].relocation_count
;
3565 i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object
*exec_list
,
3566 uint32_t buffer_count
,
3567 struct drm_i915_gem_relocation_entry
*relocs
)
3569 uint32_t reloc_count
= 0, i
;
3572 for (i
= 0; i
< buffer_count
; i
++) {
3573 struct drm_i915_gem_relocation_entry __user
*user_relocs
;
3576 user_relocs
= (void __user
*)(uintptr_t)exec_list
[i
].relocs_ptr
;
3578 unwritten
= copy_to_user(user_relocs
,
3579 &relocs
[reloc_count
],
3580 exec_list
[i
].relocation_count
*
3588 reloc_count
+= exec_list
[i
].relocation_count
;
3592 drm_free_large(relocs
);
3598 i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer
*exec
,
3599 uint64_t exec_offset
)
3601 uint32_t exec_start
, exec_len
;
3603 exec_start
= (uint32_t) exec_offset
+ exec
->batch_start_offset
;
3604 exec_len
= (uint32_t) exec
->batch_len
;
3606 if ((exec_start
| exec_len
) & 0x7)
3616 i915_gem_execbuffer(struct drm_device
*dev
, void *data
,
3617 struct drm_file
*file_priv
)
3619 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
3620 struct drm_i915_gem_execbuffer
*args
= data
;
3621 struct drm_i915_gem_exec_object
*exec_list
= NULL
;
3622 struct drm_gem_object
**object_list
= NULL
;
3623 struct drm_gem_object
*batch_obj
;
3624 struct drm_i915_gem_object
*obj_priv
;
3625 struct drm_clip_rect
*cliprects
= NULL
;
3626 struct drm_i915_gem_relocation_entry
*relocs
;
3627 int ret
, ret2
, i
, pinned
= 0;
3628 uint64_t exec_offset
;
3629 uint32_t seqno
, flush_domains
, reloc_index
;
3633 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3634 (int) args
->buffers_ptr
, args
->buffer_count
, args
->batch_len
);
3637 if (args
->buffer_count
< 1) {
3638 DRM_ERROR("execbuf with %d buffers\n", args
->buffer_count
);
3641 /* Copy in the exec list from userland */
3642 exec_list
= drm_calloc_large(sizeof(*exec_list
), args
->buffer_count
);
3643 object_list
= drm_calloc_large(sizeof(*object_list
), args
->buffer_count
);
3644 if (exec_list
== NULL
|| object_list
== NULL
) {
3645 DRM_ERROR("Failed to allocate exec or object list "
3647 args
->buffer_count
);
3651 ret
= copy_from_user(exec_list
,
3652 (struct drm_i915_relocation_entry __user
*)
3653 (uintptr_t) args
->buffers_ptr
,
3654 sizeof(*exec_list
) * args
->buffer_count
);
3656 DRM_ERROR("copy %d exec entries failed %d\n",
3657 args
->buffer_count
, ret
);
3661 if (args
->num_cliprects
!= 0) {
3662 cliprects
= kcalloc(args
->num_cliprects
, sizeof(*cliprects
),
3664 if (cliprects
== NULL
)
3667 ret
= copy_from_user(cliprects
,
3668 (struct drm_clip_rect __user
*)
3669 (uintptr_t) args
->cliprects_ptr
,
3670 sizeof(*cliprects
) * args
->num_cliprects
);
3672 DRM_ERROR("copy %d cliprects failed: %d\n",
3673 args
->num_cliprects
, ret
);
3679 ret
= i915_gem_get_relocs_from_user(exec_list
, args
->buffer_count
,
3684 mutex_lock(&dev
->struct_mutex
);
3686 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3688 if (atomic_read(&dev_priv
->mm
.wedged
)) {
3689 DRM_ERROR("Execbuf while wedged\n");
3690 mutex_unlock(&dev
->struct_mutex
);
3695 if (dev_priv
->mm
.suspended
) {
3696 DRM_ERROR("Execbuf while VT-switched.\n");
3697 mutex_unlock(&dev
->struct_mutex
);
3702 /* Look up object handles */
3703 for (i
= 0; i
< args
->buffer_count
; i
++) {
3704 object_list
[i
] = drm_gem_object_lookup(dev
, file_priv
,
3705 exec_list
[i
].handle
);
3706 if (object_list
[i
] == NULL
) {
3707 DRM_ERROR("Invalid object handle %d at index %d\n",
3708 exec_list
[i
].handle
, i
);
3713 obj_priv
= object_list
[i
]->driver_private
;
3714 if (obj_priv
->in_execbuffer
) {
3715 DRM_ERROR("Object %p appears more than once in object list\n",
3720 obj_priv
->in_execbuffer
= true;
3723 /* Pin and relocate */
3724 for (pin_tries
= 0; ; pin_tries
++) {
3728 for (i
= 0; i
< args
->buffer_count
; i
++) {
3729 object_list
[i
]->pending_read_domains
= 0;
3730 object_list
[i
]->pending_write_domain
= 0;
3731 ret
= i915_gem_object_pin_and_relocate(object_list
[i
],
3734 &relocs
[reloc_index
]);
3738 reloc_index
+= exec_list
[i
].relocation_count
;
3744 /* error other than GTT full, or we've already tried again */
3745 if (ret
!= -ENOSPC
|| pin_tries
>= 1) {
3746 if (ret
!= -ERESTARTSYS
) {
3747 unsigned long long total_size
= 0;
3748 for (i
= 0; i
< args
->buffer_count
; i
++)
3749 total_size
+= object_list
[i
]->size
;
3750 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes: %d\n",
3751 pinned
+1, args
->buffer_count
,
3753 DRM_ERROR("%d objects [%d pinned], "
3754 "%d object bytes [%d pinned], "
3755 "%d/%d gtt bytes\n",
3756 atomic_read(&dev
->object_count
),
3757 atomic_read(&dev
->pin_count
),
3758 atomic_read(&dev
->object_memory
),
3759 atomic_read(&dev
->pin_memory
),
3760 atomic_read(&dev
->gtt_memory
),
3766 /* unpin all of our buffers */
3767 for (i
= 0; i
< pinned
; i
++)
3768 i915_gem_object_unpin(object_list
[i
]);
3771 /* evict everyone we can from the aperture */
3772 ret
= i915_gem_evict_everything(dev
);
3773 if (ret
&& ret
!= -ENOSPC
)
3777 /* Set the pending read domains for the batch buffer to COMMAND */
3778 batch_obj
= object_list
[args
->buffer_count
-1];
3779 if (batch_obj
->pending_write_domain
) {
3780 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3784 batch_obj
->pending_read_domains
|= I915_GEM_DOMAIN_COMMAND
;
3786 /* Sanity check the batch buffer, prior to moving objects */
3787 exec_offset
= exec_list
[args
->buffer_count
- 1].offset
;
3788 ret
= i915_gem_check_execbuffer (args
, exec_offset
);
3790 DRM_ERROR("execbuf with invalid offset/length\n");
3794 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3796 /* Zero the global flush/invalidate flags. These
3797 * will be modified as new domains are computed
3800 dev
->invalidate_domains
= 0;
3801 dev
->flush_domains
= 0;
3803 for (i
= 0; i
< args
->buffer_count
; i
++) {
3804 struct drm_gem_object
*obj
= object_list
[i
];
3806 /* Compute new gpu domains and update invalidate/flush */
3807 i915_gem_object_set_to_gpu_domain(obj
);
3810 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3812 if (dev
->invalidate_domains
| dev
->flush_domains
) {
3814 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3816 dev
->invalidate_domains
,
3817 dev
->flush_domains
);
3820 dev
->invalidate_domains
,
3821 dev
->flush_domains
);
3822 if (dev
->flush_domains
& I915_GEM_GPU_DOMAINS
)
3823 (void)i915_add_request(dev
, file_priv
,
3824 dev
->flush_domains
);
3827 for (i
= 0; i
< args
->buffer_count
; i
++) {
3828 struct drm_gem_object
*obj
= object_list
[i
];
3829 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
3830 uint32_t old_write_domain
= obj
->write_domain
;
3832 obj
->write_domain
= obj
->pending_write_domain
;
3833 if (obj
->write_domain
)
3834 list_move_tail(&obj_priv
->gpu_write_list
,
3835 &dev_priv
->mm
.gpu_write_list
);
3837 list_del_init(&obj_priv
->gpu_write_list
);
3839 trace_i915_gem_object_change_domain(obj
,
3844 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3847 for (i
= 0; i
< args
->buffer_count
; i
++) {
3848 i915_gem_object_check_coherency(object_list
[i
],
3849 exec_list
[i
].handle
);
3854 i915_gem_dump_object(batch_obj
,
3860 /* Exec the batchbuffer */
3861 ret
= i915_dispatch_gem_execbuffer(dev
, args
, cliprects
, exec_offset
);
3863 DRM_ERROR("dispatch failed %d\n", ret
);
3868 * Ensure that the commands in the batch buffer are
3869 * finished before the interrupt fires
3871 flush_domains
= i915_retire_commands(dev
);
3873 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3876 * Get a seqno representing the execution of the current buffer,
3877 * which we can wait on. We would like to mitigate these interrupts,
3878 * likely by only creating seqnos occasionally (so that we have
3879 * *some* interrupts representing completion of buffers that we can
3880 * wait on when trying to clear up gtt space).
3882 seqno
= i915_add_request(dev
, file_priv
, flush_domains
);
3884 for (i
= 0; i
< args
->buffer_count
; i
++) {
3885 struct drm_gem_object
*obj
= object_list
[i
];
3887 i915_gem_object_move_to_active(obj
, seqno
);
3889 DRM_INFO("%s: move to exec list %p\n", __func__
, obj
);
3893 i915_dump_lru(dev
, __func__
);
3896 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3899 for (i
= 0; i
< pinned
; i
++)
3900 i915_gem_object_unpin(object_list
[i
]);
3902 for (i
= 0; i
< args
->buffer_count
; i
++) {
3903 if (object_list
[i
]) {
3904 obj_priv
= object_list
[i
]->driver_private
;
3905 obj_priv
->in_execbuffer
= false;
3907 drm_gem_object_unreference(object_list
[i
]);
3910 mutex_unlock(&dev
->struct_mutex
);
3913 /* Copy the new buffer offsets back to the user's exec list. */
3914 ret
= copy_to_user((struct drm_i915_relocation_entry __user
*)
3915 (uintptr_t) args
->buffers_ptr
,
3917 sizeof(*exec_list
) * args
->buffer_count
);
3920 DRM_ERROR("failed to copy %d exec entries "
3921 "back to user (%d)\n",
3922 args
->buffer_count
, ret
);
3926 /* Copy the updated relocations out regardless of current error
3927 * state. Failure to update the relocs would mean that the next
3928 * time userland calls execbuf, it would do so with presumed offset
3929 * state that didn't match the actual object state.
3931 ret2
= i915_gem_put_relocs_to_user(exec_list
, args
->buffer_count
,
3934 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2
);
3941 drm_free_large(object_list
);
3942 drm_free_large(exec_list
);
3949 i915_gem_object_pin(struct drm_gem_object
*obj
, uint32_t alignment
)
3951 struct drm_device
*dev
= obj
->dev
;
3952 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
3955 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3957 if (obj_priv
->gtt_space
!= NULL
) {
3959 alignment
= i915_gem_get_gtt_alignment(obj
);
3960 if (obj_priv
->gtt_offset
& (alignment
- 1)) {
3961 ret
= i915_gem_object_unbind(obj
);
3967 if (obj_priv
->gtt_space
== NULL
) {
3968 ret
= i915_gem_object_bind_to_gtt(obj
, alignment
);
3973 * Pre-965 chips need a fence register set up in order to
3974 * properly handle tiled surfaces.
3976 if (!IS_I965G(dev
) && obj_priv
->tiling_mode
!= I915_TILING_NONE
) {
3977 ret
= i915_gem_object_get_fence_reg(obj
);
3979 if (ret
!= -EBUSY
&& ret
!= -ERESTARTSYS
)
3980 DRM_ERROR("Failure to install fence: %d\n",
3985 obj_priv
->pin_count
++;
3987 /* If the object is not active and not pending a flush,
3988 * remove it from the inactive list
3990 if (obj_priv
->pin_count
== 1) {
3991 atomic_inc(&dev
->pin_count
);
3992 atomic_add(obj
->size
, &dev
->pin_memory
);
3993 if (!obj_priv
->active
&&
3994 (obj
->write_domain
& I915_GEM_GPU_DOMAINS
) == 0 &&
3995 !list_empty(&obj_priv
->list
))
3996 list_del_init(&obj_priv
->list
);
3998 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
4004 i915_gem_object_unpin(struct drm_gem_object
*obj
)
4006 struct drm_device
*dev
= obj
->dev
;
4007 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4008 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
4010 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
4011 obj_priv
->pin_count
--;
4012 BUG_ON(obj_priv
->pin_count
< 0);
4013 BUG_ON(obj_priv
->gtt_space
== NULL
);
4015 /* If the object is no longer pinned, and is
4016 * neither active nor being flushed, then stick it on
4019 if (obj_priv
->pin_count
== 0) {
4020 if (!obj_priv
->active
&&
4021 (obj
->write_domain
& I915_GEM_GPU_DOMAINS
) == 0)
4022 list_move_tail(&obj_priv
->list
,
4023 &dev_priv
->mm
.inactive_list
);
4024 atomic_dec(&dev
->pin_count
);
4025 atomic_sub(obj
->size
, &dev
->pin_memory
);
4027 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
4031 i915_gem_pin_ioctl(struct drm_device
*dev
, void *data
,
4032 struct drm_file
*file_priv
)
4034 struct drm_i915_gem_pin
*args
= data
;
4035 struct drm_gem_object
*obj
;
4036 struct drm_i915_gem_object
*obj_priv
;
4039 mutex_lock(&dev
->struct_mutex
);
4041 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
4043 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4045 mutex_unlock(&dev
->struct_mutex
);
4048 obj_priv
= obj
->driver_private
;
4050 if (obj_priv
->madv
!= I915_MADV_WILLNEED
) {
4051 DRM_ERROR("Attempting to pin a purgeable buffer\n");
4052 drm_gem_object_unreference(obj
);
4053 mutex_unlock(&dev
->struct_mutex
);
4057 if (obj_priv
->pin_filp
!= NULL
&& obj_priv
->pin_filp
!= file_priv
) {
4058 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4060 drm_gem_object_unreference(obj
);
4061 mutex_unlock(&dev
->struct_mutex
);
4065 obj_priv
->user_pin_count
++;
4066 obj_priv
->pin_filp
= file_priv
;
4067 if (obj_priv
->user_pin_count
== 1) {
4068 ret
= i915_gem_object_pin(obj
, args
->alignment
);
4070 drm_gem_object_unreference(obj
);
4071 mutex_unlock(&dev
->struct_mutex
);
4076 /* XXX - flush the CPU caches for pinned objects
4077 * as the X server doesn't manage domains yet
4079 i915_gem_object_flush_cpu_write_domain(obj
);
4080 args
->offset
= obj_priv
->gtt_offset
;
4081 drm_gem_object_unreference(obj
);
4082 mutex_unlock(&dev
->struct_mutex
);
4088 i915_gem_unpin_ioctl(struct drm_device
*dev
, void *data
,
4089 struct drm_file
*file_priv
)
4091 struct drm_i915_gem_pin
*args
= data
;
4092 struct drm_gem_object
*obj
;
4093 struct drm_i915_gem_object
*obj_priv
;
4095 mutex_lock(&dev
->struct_mutex
);
4097 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
4099 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4101 mutex_unlock(&dev
->struct_mutex
);
4105 obj_priv
= obj
->driver_private
;
4106 if (obj_priv
->pin_filp
!= file_priv
) {
4107 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4109 drm_gem_object_unreference(obj
);
4110 mutex_unlock(&dev
->struct_mutex
);
4113 obj_priv
->user_pin_count
--;
4114 if (obj_priv
->user_pin_count
== 0) {
4115 obj_priv
->pin_filp
= NULL
;
4116 i915_gem_object_unpin(obj
);
4119 drm_gem_object_unreference(obj
);
4120 mutex_unlock(&dev
->struct_mutex
);
4125 i915_gem_busy_ioctl(struct drm_device
*dev
, void *data
,
4126 struct drm_file
*file_priv
)
4128 struct drm_i915_gem_busy
*args
= data
;
4129 struct drm_gem_object
*obj
;
4130 struct drm_i915_gem_object
*obj_priv
;
4132 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
4134 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4139 mutex_lock(&dev
->struct_mutex
);
4140 /* Update the active list for the hardware's current position.
4141 * Otherwise this only updates on a delayed timer or when irqs are
4142 * actually unmasked, and our working set ends up being larger than
4145 i915_gem_retire_requests(dev
);
4147 obj_priv
= obj
->driver_private
;
4148 /* Don't count being on the flushing list against the object being
4149 * done. Otherwise, a buffer left on the flushing list but not getting
4150 * flushed (because nobody's flushing that domain) won't ever return
4151 * unbusy and get reused by libdrm's bo cache. The other expected
4152 * consumer of this interface, OpenGL's occlusion queries, also specs
4153 * that the objects get unbusy "eventually" without any interference.
4155 args
->busy
= obj_priv
->active
&& obj_priv
->last_rendering_seqno
!= 0;
4157 drm_gem_object_unreference(obj
);
4158 mutex_unlock(&dev
->struct_mutex
);
4163 i915_gem_throttle_ioctl(struct drm_device
*dev
, void *data
,
4164 struct drm_file
*file_priv
)
4166 return i915_gem_ring_throttle(dev
, file_priv
);
4170 i915_gem_madvise_ioctl(struct drm_device
*dev
, void *data
,
4171 struct drm_file
*file_priv
)
4173 struct drm_i915_gem_madvise
*args
= data
;
4174 struct drm_gem_object
*obj
;
4175 struct drm_i915_gem_object
*obj_priv
;
4177 switch (args
->madv
) {
4178 case I915_MADV_DONTNEED
:
4179 case I915_MADV_WILLNEED
:
4185 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
4187 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4192 mutex_lock(&dev
->struct_mutex
);
4193 obj_priv
= obj
->driver_private
;
4195 if (obj_priv
->pin_count
) {
4196 drm_gem_object_unreference(obj
);
4197 mutex_unlock(&dev
->struct_mutex
);
4199 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4203 if (obj_priv
->madv
!= __I915_MADV_PURGED
)
4204 obj_priv
->madv
= args
->madv
;
4206 /* if the object is no longer bound, discard its backing storage */
4207 if (i915_gem_object_is_purgeable(obj_priv
) &&
4208 obj_priv
->gtt_space
== NULL
)
4209 i915_gem_object_truncate(obj
);
4211 args
->retained
= obj_priv
->madv
!= __I915_MADV_PURGED
;
4213 drm_gem_object_unreference(obj
);
4214 mutex_unlock(&dev
->struct_mutex
);
4219 int i915_gem_init_object(struct drm_gem_object
*obj
)
4221 struct drm_i915_gem_object
*obj_priv
;
4223 obj_priv
= kzalloc(sizeof(*obj_priv
), GFP_KERNEL
);
4224 if (obj_priv
== NULL
)
4228 * We've just allocated pages from the kernel,
4229 * so they've just been written by the CPU with
4230 * zeros. They'll need to be clflushed before we
4231 * use them with the GPU.
4233 obj
->write_domain
= I915_GEM_DOMAIN_CPU
;
4234 obj
->read_domains
= I915_GEM_DOMAIN_CPU
;
4236 obj_priv
->agp_type
= AGP_USER_MEMORY
;
4238 obj
->driver_private
= obj_priv
;
4239 obj_priv
->obj
= obj
;
4240 obj_priv
->fence_reg
= I915_FENCE_REG_NONE
;
4241 INIT_LIST_HEAD(&obj_priv
->list
);
4242 INIT_LIST_HEAD(&obj_priv
->gpu_write_list
);
4243 INIT_LIST_HEAD(&obj_priv
->fence_list
);
4244 obj_priv
->madv
= I915_MADV_WILLNEED
;
4246 trace_i915_gem_object_create(obj
);
4251 void i915_gem_free_object(struct drm_gem_object
*obj
)
4253 struct drm_device
*dev
= obj
->dev
;
4254 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
4256 trace_i915_gem_object_destroy(obj
);
4258 while (obj_priv
->pin_count
> 0)
4259 i915_gem_object_unpin(obj
);
4261 if (obj_priv
->phys_obj
)
4262 i915_gem_detach_phys_object(dev
, obj
);
4264 i915_gem_object_unbind(obj
);
4266 if (obj_priv
->mmap_offset
)
4267 i915_gem_free_mmap_offset(obj
);
4269 kfree(obj_priv
->page_cpu_valid
);
4270 kfree(obj_priv
->bit_17
);
4271 kfree(obj
->driver_private
);
4274 /** Unbinds all inactive objects. */
4276 i915_gem_evict_from_inactive_list(struct drm_device
*dev
)
4278 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4280 while (!list_empty(&dev_priv
->mm
.inactive_list
)) {
4281 struct drm_gem_object
*obj
;
4284 obj
= list_first_entry(&dev_priv
->mm
.inactive_list
,
4285 struct drm_i915_gem_object
,
4288 ret
= i915_gem_object_unbind(obj
);
4290 DRM_ERROR("Error unbinding object: %d\n", ret
);
4299 i915_gem_idle(struct drm_device
*dev
)
4301 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4302 uint32_t seqno
, cur_seqno
, last_seqno
;
4305 mutex_lock(&dev
->struct_mutex
);
4307 if (dev_priv
->mm
.suspended
|| dev_priv
->ring
.ring_obj
== NULL
) {
4308 mutex_unlock(&dev
->struct_mutex
);
4312 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4313 * We need to replace this with a semaphore, or something.
4315 dev_priv
->mm
.suspended
= 1;
4316 del_timer(&dev_priv
->hangcheck_timer
);
4318 /* Cancel the retire work handler, wait for it to finish if running
4320 mutex_unlock(&dev
->struct_mutex
);
4321 cancel_delayed_work_sync(&dev_priv
->mm
.retire_work
);
4322 mutex_lock(&dev
->struct_mutex
);
4324 i915_kernel_lost_context(dev
);
4326 /* Flush the GPU along with all non-CPU write domains
4328 i915_gem_flush(dev
, I915_GEM_GPU_DOMAINS
, I915_GEM_GPU_DOMAINS
);
4329 seqno
= i915_add_request(dev
, NULL
, I915_GEM_GPU_DOMAINS
);
4332 mutex_unlock(&dev
->struct_mutex
);
4336 dev_priv
->mm
.waiting_gem_seqno
= seqno
;
4340 cur_seqno
= i915_get_gem_seqno(dev
);
4341 if (i915_seqno_passed(cur_seqno
, seqno
))
4343 if (last_seqno
== cur_seqno
) {
4344 if (stuck
++ > 100) {
4345 DRM_ERROR("hardware wedged\n");
4346 atomic_set(&dev_priv
->mm
.wedged
, 1);
4347 DRM_WAKEUP(&dev_priv
->irq_queue
);
4352 last_seqno
= cur_seqno
;
4354 dev_priv
->mm
.waiting_gem_seqno
= 0;
4356 i915_gem_retire_requests(dev
);
4358 spin_lock(&dev_priv
->mm
.active_list_lock
);
4359 if (!atomic_read(&dev_priv
->mm
.wedged
)) {
4360 /* Active and flushing should now be empty as we've
4361 * waited for a sequence higher than any pending execbuffer
4363 WARN_ON(!list_empty(&dev_priv
->mm
.active_list
));
4364 WARN_ON(!list_empty(&dev_priv
->mm
.flushing_list
));
4365 /* Request should now be empty as we've also waited
4366 * for the last request in the list
4368 WARN_ON(!list_empty(&dev_priv
->mm
.request_list
));
4371 /* Empty the active and flushing lists to inactive. If there's
4372 * anything left at this point, it means that we're wedged and
4373 * nothing good's going to happen by leaving them there. So strip
4374 * the GPU domains and just stuff them onto inactive.
4376 while (!list_empty(&dev_priv
->mm
.active_list
)) {
4377 struct drm_gem_object
*obj
;
4378 uint32_t old_write_domain
;
4380 obj
= list_first_entry(&dev_priv
->mm
.active_list
,
4381 struct drm_i915_gem_object
,
4383 old_write_domain
= obj
->write_domain
;
4384 obj
->write_domain
&= ~I915_GEM_GPU_DOMAINS
;
4385 i915_gem_object_move_to_inactive(obj
);
4387 trace_i915_gem_object_change_domain(obj
,
4391 spin_unlock(&dev_priv
->mm
.active_list_lock
);
4393 while (!list_empty(&dev_priv
->mm
.flushing_list
)) {
4394 struct drm_gem_object
*obj
;
4395 uint32_t old_write_domain
;
4397 obj
= list_first_entry(&dev_priv
->mm
.flushing_list
,
4398 struct drm_i915_gem_object
,
4400 old_write_domain
= obj
->write_domain
;
4401 obj
->write_domain
&= ~I915_GEM_GPU_DOMAINS
;
4402 i915_gem_object_move_to_inactive(obj
);
4404 trace_i915_gem_object_change_domain(obj
,
4410 /* Move all inactive buffers out of the GTT. */
4411 ret
= i915_gem_evict_from_inactive_list(dev
);
4412 WARN_ON(!list_empty(&dev_priv
->mm
.inactive_list
));
4414 mutex_unlock(&dev
->struct_mutex
);
4418 i915_gem_cleanup_ringbuffer(dev
);
4419 mutex_unlock(&dev
->struct_mutex
);
4425 i915_gem_init_hws(struct drm_device
*dev
)
4427 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4428 struct drm_gem_object
*obj
;
4429 struct drm_i915_gem_object
*obj_priv
;
4432 /* If we need a physical address for the status page, it's already
4433 * initialized at driver load time.
4435 if (!I915_NEED_GFX_HWS(dev
))
4438 obj
= drm_gem_object_alloc(dev
, 4096);
4440 DRM_ERROR("Failed to allocate status page\n");
4443 obj_priv
= obj
->driver_private
;
4444 obj_priv
->agp_type
= AGP_USER_CACHED_MEMORY
;
4446 ret
= i915_gem_object_pin(obj
, 4096);
4448 drm_gem_object_unreference(obj
);
4452 dev_priv
->status_gfx_addr
= obj_priv
->gtt_offset
;
4454 dev_priv
->hw_status_page
= kmap(obj_priv
->pages
[0]);
4455 if (dev_priv
->hw_status_page
== NULL
) {
4456 DRM_ERROR("Failed to map status page.\n");
4457 memset(&dev_priv
->hws_map
, 0, sizeof(dev_priv
->hws_map
));
4458 i915_gem_object_unpin(obj
);
4459 drm_gem_object_unreference(obj
);
4462 dev_priv
->hws_obj
= obj
;
4463 memset(dev_priv
->hw_status_page
, 0, PAGE_SIZE
);
4464 I915_WRITE(HWS_PGA
, dev_priv
->status_gfx_addr
);
4465 I915_READ(HWS_PGA
); /* posting read */
4466 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv
->status_gfx_addr
);
4472 i915_gem_cleanup_hws(struct drm_device
*dev
)
4474 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4475 struct drm_gem_object
*obj
;
4476 struct drm_i915_gem_object
*obj_priv
;
4478 if (dev_priv
->hws_obj
== NULL
)
4481 obj
= dev_priv
->hws_obj
;
4482 obj_priv
= obj
->driver_private
;
4484 kunmap(obj_priv
->pages
[0]);
4485 i915_gem_object_unpin(obj
);
4486 drm_gem_object_unreference(obj
);
4487 dev_priv
->hws_obj
= NULL
;
4489 memset(&dev_priv
->hws_map
, 0, sizeof(dev_priv
->hws_map
));
4490 dev_priv
->hw_status_page
= NULL
;
4492 /* Write high address into HWS_PGA when disabling. */
4493 I915_WRITE(HWS_PGA
, 0x1ffff000);
4497 i915_gem_init_ringbuffer(struct drm_device
*dev
)
4499 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4500 struct drm_gem_object
*obj
;
4501 struct drm_i915_gem_object
*obj_priv
;
4502 drm_i915_ring_buffer_t
*ring
= &dev_priv
->ring
;
4506 ret
= i915_gem_init_hws(dev
);
4510 obj
= drm_gem_object_alloc(dev
, 128 * 1024);
4512 DRM_ERROR("Failed to allocate ringbuffer\n");
4513 i915_gem_cleanup_hws(dev
);
4516 obj_priv
= obj
->driver_private
;
4518 ret
= i915_gem_object_pin(obj
, 4096);
4520 drm_gem_object_unreference(obj
);
4521 i915_gem_cleanup_hws(dev
);
4525 /* Set up the kernel mapping for the ring. */
4526 ring
->Size
= obj
->size
;
4528 ring
->map
.offset
= dev
->agp
->base
+ obj_priv
->gtt_offset
;
4529 ring
->map
.size
= obj
->size
;
4531 ring
->map
.flags
= 0;
4534 drm_core_ioremap_wc(&ring
->map
, dev
);
4535 if (ring
->map
.handle
== NULL
) {
4536 DRM_ERROR("Failed to map ringbuffer.\n");
4537 memset(&dev_priv
->ring
, 0, sizeof(dev_priv
->ring
));
4538 i915_gem_object_unpin(obj
);
4539 drm_gem_object_unreference(obj
);
4540 i915_gem_cleanup_hws(dev
);
4543 ring
->ring_obj
= obj
;
4544 ring
->virtual_start
= ring
->map
.handle
;
4546 /* Stop the ring if it's running. */
4547 I915_WRITE(PRB0_CTL
, 0);
4548 I915_WRITE(PRB0_TAIL
, 0);
4549 I915_WRITE(PRB0_HEAD
, 0);
4551 /* Initialize the ring. */
4552 I915_WRITE(PRB0_START
, obj_priv
->gtt_offset
);
4553 head
= I915_READ(PRB0_HEAD
) & HEAD_ADDR
;
4555 /* G45 ring initialization fails to reset head to zero */
4557 DRM_ERROR("Ring head not reset to zero "
4558 "ctl %08x head %08x tail %08x start %08x\n",
4559 I915_READ(PRB0_CTL
),
4560 I915_READ(PRB0_HEAD
),
4561 I915_READ(PRB0_TAIL
),
4562 I915_READ(PRB0_START
));
4563 I915_WRITE(PRB0_HEAD
, 0);
4565 DRM_ERROR("Ring head forced to zero "
4566 "ctl %08x head %08x tail %08x start %08x\n",
4567 I915_READ(PRB0_CTL
),
4568 I915_READ(PRB0_HEAD
),
4569 I915_READ(PRB0_TAIL
),
4570 I915_READ(PRB0_START
));
4573 I915_WRITE(PRB0_CTL
,
4574 ((obj
->size
- 4096) & RING_NR_PAGES
) |
4578 head
= I915_READ(PRB0_HEAD
) & HEAD_ADDR
;
4580 /* If the head is still not zero, the ring is dead */
4582 DRM_ERROR("Ring initialization failed "
4583 "ctl %08x head %08x tail %08x start %08x\n",
4584 I915_READ(PRB0_CTL
),
4585 I915_READ(PRB0_HEAD
),
4586 I915_READ(PRB0_TAIL
),
4587 I915_READ(PRB0_START
));
4591 /* Update our cache of the ring state */
4592 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4593 i915_kernel_lost_context(dev
);
4595 ring
->head
= I915_READ(PRB0_HEAD
) & HEAD_ADDR
;
4596 ring
->tail
= I915_READ(PRB0_TAIL
) & TAIL_ADDR
;
4597 ring
->space
= ring
->head
- (ring
->tail
+ 8);
4598 if (ring
->space
< 0)
4599 ring
->space
+= ring
->Size
;
4606 i915_gem_cleanup_ringbuffer(struct drm_device
*dev
)
4608 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4610 if (dev_priv
->ring
.ring_obj
== NULL
)
4613 drm_core_ioremapfree(&dev_priv
->ring
.map
, dev
);
4615 i915_gem_object_unpin(dev_priv
->ring
.ring_obj
);
4616 drm_gem_object_unreference(dev_priv
->ring
.ring_obj
);
4617 dev_priv
->ring
.ring_obj
= NULL
;
4618 memset(&dev_priv
->ring
, 0, sizeof(dev_priv
->ring
));
4620 i915_gem_cleanup_hws(dev
);
4624 i915_gem_entervt_ioctl(struct drm_device
*dev
, void *data
,
4625 struct drm_file
*file_priv
)
4627 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4630 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
4633 if (atomic_read(&dev_priv
->mm
.wedged
)) {
4634 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4635 atomic_set(&dev_priv
->mm
.wedged
, 0);
4638 mutex_lock(&dev
->struct_mutex
);
4639 dev_priv
->mm
.suspended
= 0;
4641 ret
= i915_gem_init_ringbuffer(dev
);
4643 mutex_unlock(&dev
->struct_mutex
);
4647 spin_lock(&dev_priv
->mm
.active_list_lock
);
4648 BUG_ON(!list_empty(&dev_priv
->mm
.active_list
));
4649 spin_unlock(&dev_priv
->mm
.active_list_lock
);
4651 BUG_ON(!list_empty(&dev_priv
->mm
.flushing_list
));
4652 BUG_ON(!list_empty(&dev_priv
->mm
.inactive_list
));
4653 BUG_ON(!list_empty(&dev_priv
->mm
.request_list
));
4654 mutex_unlock(&dev
->struct_mutex
);
4656 drm_irq_install(dev
);
4662 i915_gem_leavevt_ioctl(struct drm_device
*dev
, void *data
,
4663 struct drm_file
*file_priv
)
4665 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
4668 drm_irq_uninstall(dev
);
4669 return i915_gem_idle(dev
);
4673 i915_gem_lastclose(struct drm_device
*dev
)
4677 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
4680 ret
= i915_gem_idle(dev
);
4682 DRM_ERROR("failed to idle hardware: %d\n", ret
);
4686 i915_gem_load(struct drm_device
*dev
)
4689 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4691 spin_lock_init(&dev_priv
->mm
.active_list_lock
);
4692 INIT_LIST_HEAD(&dev_priv
->mm
.active_list
);
4693 INIT_LIST_HEAD(&dev_priv
->mm
.flushing_list
);
4694 INIT_LIST_HEAD(&dev_priv
->mm
.gpu_write_list
);
4695 INIT_LIST_HEAD(&dev_priv
->mm
.inactive_list
);
4696 INIT_LIST_HEAD(&dev_priv
->mm
.request_list
);
4697 INIT_LIST_HEAD(&dev_priv
->mm
.fence_list
);
4698 INIT_DELAYED_WORK(&dev_priv
->mm
.retire_work
,
4699 i915_gem_retire_work_handler
);
4700 dev_priv
->mm
.next_gem_seqno
= 1;
4702 spin_lock(&shrink_list_lock
);
4703 list_add(&dev_priv
->mm
.shrink_list
, &shrink_list
);
4704 spin_unlock(&shrink_list_lock
);
4706 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4707 if (IS_I915G(dev
) || IS_I915GM(dev
) || IS_I945G(dev
) || IS_I945GM(dev
) || IS_G33(dev
)) {
4708 u32 tmp
= I915_READ(MI_ARB_STATE
);
4709 if (!(tmp
& MI_ARB_C3_LP_WRITE_ENABLE
)) {
4710 /* arb state is a masked write, so set bit + bit in mask */
4711 tmp
= MI_ARB_C3_LP_WRITE_ENABLE
| (MI_ARB_C3_LP_WRITE_ENABLE
<< MI_ARB_MASK_SHIFT
);
4712 I915_WRITE(MI_ARB_STATE
, tmp
);
4716 /* Old X drivers will take 0-2 for front, back, depth buffers */
4717 dev_priv
->fence_reg_start
= 3;
4719 if (IS_I965G(dev
) || IS_I945G(dev
) || IS_I945GM(dev
) || IS_G33(dev
))
4720 dev_priv
->num_fence_regs
= 16;
4722 dev_priv
->num_fence_regs
= 8;
4724 /* Initialize fence registers to zero */
4725 if (IS_I965G(dev
)) {
4726 for (i
= 0; i
< 16; i
++)
4727 I915_WRITE64(FENCE_REG_965_0
+ (i
* 8), 0);
4729 for (i
= 0; i
< 8; i
++)
4730 I915_WRITE(FENCE_REG_830_0
+ (i
* 4), 0);
4731 if (IS_I945G(dev
) || IS_I945GM(dev
) || IS_G33(dev
))
4732 for (i
= 0; i
< 8; i
++)
4733 I915_WRITE(FENCE_REG_945_8
+ (i
* 4), 0);
4736 i915_gem_detect_bit_6_swizzle(dev
);
4740 * Create a physically contiguous memory object for this object
4741 * e.g. for cursor + overlay regs
4743 int i915_gem_init_phys_object(struct drm_device
*dev
,
4746 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4747 struct drm_i915_gem_phys_object
*phys_obj
;
4750 if (dev_priv
->mm
.phys_objs
[id
- 1] || !size
)
4753 phys_obj
= kzalloc(sizeof(struct drm_i915_gem_phys_object
), GFP_KERNEL
);
4759 phys_obj
->handle
= drm_pci_alloc(dev
, size
, 0);
4760 if (!phys_obj
->handle
) {
4765 set_memory_wc((unsigned long)phys_obj
->handle
->vaddr
, phys_obj
->handle
->size
/ PAGE_SIZE
);
4768 dev_priv
->mm
.phys_objs
[id
- 1] = phys_obj
;
4776 void i915_gem_free_phys_object(struct drm_device
*dev
, int id
)
4778 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4779 struct drm_i915_gem_phys_object
*phys_obj
;
4781 if (!dev_priv
->mm
.phys_objs
[id
- 1])
4784 phys_obj
= dev_priv
->mm
.phys_objs
[id
- 1];
4785 if (phys_obj
->cur_obj
) {
4786 i915_gem_detach_phys_object(dev
, phys_obj
->cur_obj
);
4790 set_memory_wb((unsigned long)phys_obj
->handle
->vaddr
, phys_obj
->handle
->size
/ PAGE_SIZE
);
4792 drm_pci_free(dev
, phys_obj
->handle
);
4794 dev_priv
->mm
.phys_objs
[id
- 1] = NULL
;
4797 void i915_gem_free_all_phys_object(struct drm_device
*dev
)
4801 for (i
= I915_GEM_PHYS_CURSOR_0
; i
<= I915_MAX_PHYS_OBJECT
; i
++)
4802 i915_gem_free_phys_object(dev
, i
);
4805 void i915_gem_detach_phys_object(struct drm_device
*dev
,
4806 struct drm_gem_object
*obj
)
4808 struct drm_i915_gem_object
*obj_priv
;
4813 obj_priv
= obj
->driver_private
;
4814 if (!obj_priv
->phys_obj
)
4817 ret
= i915_gem_object_get_pages(obj
, 0);
4821 page_count
= obj
->size
/ PAGE_SIZE
;
4823 for (i
= 0; i
< page_count
; i
++) {
4824 char *dst
= kmap_atomic(obj_priv
->pages
[i
], KM_USER0
);
4825 char *src
= obj_priv
->phys_obj
->handle
->vaddr
+ (i
* PAGE_SIZE
);
4827 memcpy(dst
, src
, PAGE_SIZE
);
4828 kunmap_atomic(dst
, KM_USER0
);
4830 drm_clflush_pages(obj_priv
->pages
, page_count
);
4831 drm_agp_chipset_flush(dev
);
4833 i915_gem_object_put_pages(obj
);
4835 obj_priv
->phys_obj
->cur_obj
= NULL
;
4836 obj_priv
->phys_obj
= NULL
;
4840 i915_gem_attach_phys_object(struct drm_device
*dev
,
4841 struct drm_gem_object
*obj
, int id
)
4843 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4844 struct drm_i915_gem_object
*obj_priv
;
4849 if (id
> I915_MAX_PHYS_OBJECT
)
4852 obj_priv
= obj
->driver_private
;
4854 if (obj_priv
->phys_obj
) {
4855 if (obj_priv
->phys_obj
->id
== id
)
4857 i915_gem_detach_phys_object(dev
, obj
);
4861 /* create a new object */
4862 if (!dev_priv
->mm
.phys_objs
[id
- 1]) {
4863 ret
= i915_gem_init_phys_object(dev
, id
,
4866 DRM_ERROR("failed to init phys object %d size: %zu\n", id
, obj
->size
);
4871 /* bind to the object */
4872 obj_priv
->phys_obj
= dev_priv
->mm
.phys_objs
[id
- 1];
4873 obj_priv
->phys_obj
->cur_obj
= obj
;
4875 ret
= i915_gem_object_get_pages(obj
, 0);
4877 DRM_ERROR("failed to get page list\n");
4881 page_count
= obj
->size
/ PAGE_SIZE
;
4883 for (i
= 0; i
< page_count
; i
++) {
4884 char *src
= kmap_atomic(obj_priv
->pages
[i
], KM_USER0
);
4885 char *dst
= obj_priv
->phys_obj
->handle
->vaddr
+ (i
* PAGE_SIZE
);
4887 memcpy(dst
, src
, PAGE_SIZE
);
4888 kunmap_atomic(src
, KM_USER0
);
4891 i915_gem_object_put_pages(obj
);
4899 i915_gem_phys_pwrite(struct drm_device
*dev
, struct drm_gem_object
*obj
,
4900 struct drm_i915_gem_pwrite
*args
,
4901 struct drm_file
*file_priv
)
4903 struct drm_i915_gem_object
*obj_priv
= obj
->driver_private
;
4906 char __user
*user_data
;
4908 user_data
= (char __user
*) (uintptr_t) args
->data_ptr
;
4909 obj_addr
= obj_priv
->phys_obj
->handle
->vaddr
+ args
->offset
;
4911 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr
, args
->size
);
4912 ret
= copy_from_user(obj_addr
, user_data
, args
->size
);
4916 drm_agp_chipset_flush(dev
);
4920 void i915_gem_release(struct drm_device
* dev
, struct drm_file
*file_priv
)
4922 struct drm_i915_file_private
*i915_file_priv
= file_priv
->driver_priv
;
4924 /* Clean up our request list when the client is going away, so that
4925 * later retire_requests won't dereference our soon-to-be-gone
4928 mutex_lock(&dev
->struct_mutex
);
4929 while (!list_empty(&i915_file_priv
->mm
.request_list
))
4930 list_del_init(i915_file_priv
->mm
.request_list
.next
);
4931 mutex_unlock(&dev
->struct_mutex
);
4935 i915_gem_shrink(int nr_to_scan
, gfp_t gfp_mask
)
4937 drm_i915_private_t
*dev_priv
, *next_dev
;
4938 struct drm_i915_gem_object
*obj_priv
, *next_obj
;
4940 int would_deadlock
= 1;
4942 /* "fast-path" to count number of available objects */
4943 if (nr_to_scan
== 0) {
4944 spin_lock(&shrink_list_lock
);
4945 list_for_each_entry(dev_priv
, &shrink_list
, mm
.shrink_list
) {
4946 struct drm_device
*dev
= dev_priv
->dev
;
4948 if (mutex_trylock(&dev
->struct_mutex
)) {
4949 list_for_each_entry(obj_priv
,
4950 &dev_priv
->mm
.inactive_list
,
4953 mutex_unlock(&dev
->struct_mutex
);
4956 spin_unlock(&shrink_list_lock
);
4958 return (cnt
/ 100) * sysctl_vfs_cache_pressure
;
4961 spin_lock(&shrink_list_lock
);
4963 /* first scan for clean buffers */
4964 list_for_each_entry_safe(dev_priv
, next_dev
,
4965 &shrink_list
, mm
.shrink_list
) {
4966 struct drm_device
*dev
= dev_priv
->dev
;
4968 if (! mutex_trylock(&dev
->struct_mutex
))
4971 spin_unlock(&shrink_list_lock
);
4973 i915_gem_retire_requests(dev
);
4975 list_for_each_entry_safe(obj_priv
, next_obj
,
4976 &dev_priv
->mm
.inactive_list
,
4978 if (i915_gem_object_is_purgeable(obj_priv
)) {
4979 i915_gem_object_unbind(obj_priv
->obj
);
4980 if (--nr_to_scan
<= 0)
4985 spin_lock(&shrink_list_lock
);
4986 mutex_unlock(&dev
->struct_mutex
);
4990 if (nr_to_scan
<= 0)
4994 /* second pass, evict/count anything still on the inactive list */
4995 list_for_each_entry_safe(dev_priv
, next_dev
,
4996 &shrink_list
, mm
.shrink_list
) {
4997 struct drm_device
*dev
= dev_priv
->dev
;
4999 if (! mutex_trylock(&dev
->struct_mutex
))
5002 spin_unlock(&shrink_list_lock
);
5004 list_for_each_entry_safe(obj_priv
, next_obj
,
5005 &dev_priv
->mm
.inactive_list
,
5007 if (nr_to_scan
> 0) {
5008 i915_gem_object_unbind(obj_priv
->obj
);
5014 spin_lock(&shrink_list_lock
);
5015 mutex_unlock(&dev
->struct_mutex
);
5020 spin_unlock(&shrink_list_lock
);
5025 return (cnt
/ 100) * sysctl_vfs_cache_pressure
;
5030 static struct shrinker shrinker
= {
5031 .shrink
= i915_gem_shrink
,
5032 .seeks
= DEFAULT_SEEKS
,
5036 i915_gem_shrinker_init(void)
5038 register_shrinker(&shrinker
);
5042 i915_gem_shrinker_exit(void)
5044 unregister_shrinker(&shrinker
);