2 * Copyright © 2008 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
32 #include "i915_trace.h"
33 #include "intel_drv.h"
34 #include <linux/slab.h>
35 #include <linux/swap.h>
36 #include <linux/pci.h>
37 #include <linux/intel-gtt.h>
39 static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object
*obj
);
41 static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object
*obj
,
43 static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object
*obj
);
44 static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object
*obj
);
45 static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object
*obj
,
47 static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object
*obj
,
50 static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object
*obj
);
51 static int i915_gem_object_wait_rendering(struct drm_gem_object
*obj
,
53 static int i915_gem_object_bind_to_gtt(struct drm_gem_object
*obj
,
55 static void i915_gem_clear_fence_reg(struct drm_gem_object
*obj
);
56 static int i915_gem_phys_pwrite(struct drm_device
*dev
, struct drm_gem_object
*obj
,
57 struct drm_i915_gem_pwrite
*args
,
58 struct drm_file
*file_priv
);
59 static void i915_gem_free_object_tail(struct drm_gem_object
*obj
);
61 static LIST_HEAD(shrink_list
);
62 static DEFINE_SPINLOCK(shrink_list_lock
);
65 i915_gem_object_is_inactive(struct drm_i915_gem_object
*obj_priv
)
67 return obj_priv
->gtt_space
&&
69 obj_priv
->pin_count
== 0;
72 int i915_gem_do_init(struct drm_device
*dev
, unsigned long start
,
75 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
78 (start
& (PAGE_SIZE
- 1)) != 0 ||
79 (end
& (PAGE_SIZE
- 1)) != 0) {
83 drm_mm_init(&dev_priv
->mm
.gtt_space
, start
,
86 dev
->gtt_total
= (uint32_t) (end
- start
);
92 i915_gem_init_ioctl(struct drm_device
*dev
, void *data
,
93 struct drm_file
*file_priv
)
95 struct drm_i915_gem_init
*args
= data
;
98 mutex_lock(&dev
->struct_mutex
);
99 ret
= i915_gem_do_init(dev
, args
->gtt_start
, args
->gtt_end
);
100 mutex_unlock(&dev
->struct_mutex
);
106 i915_gem_get_aperture_ioctl(struct drm_device
*dev
, void *data
,
107 struct drm_file
*file_priv
)
109 struct drm_i915_gem_get_aperture
*args
= data
;
111 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
114 args
->aper_size
= dev
->gtt_total
;
115 args
->aper_available_size
= (args
->aper_size
-
116 atomic_read(&dev
->pin_memory
));
123 * Creates a new mm object and returns a handle to it.
126 i915_gem_create_ioctl(struct drm_device
*dev
, void *data
,
127 struct drm_file
*file_priv
)
129 struct drm_i915_gem_create
*args
= data
;
130 struct drm_gem_object
*obj
;
134 args
->size
= roundup(args
->size
, PAGE_SIZE
);
136 /* Allocate the new object */
137 obj
= i915_gem_alloc_object(dev
, args
->size
);
141 ret
= drm_gem_handle_create(file_priv
, obj
, &handle
);
143 drm_gem_object_unreference_unlocked(obj
);
147 /* Sink the floating reference from kref_init(handlecount) */
148 drm_gem_object_handle_unreference_unlocked(obj
);
150 args
->handle
= handle
;
155 fast_shmem_read(struct page
**pages
,
156 loff_t page_base
, int page_offset
,
163 vaddr
= kmap_atomic(pages
[page_base
>> PAGE_SHIFT
], KM_USER0
);
166 unwritten
= __copy_to_user_inatomic(data
, vaddr
+ page_offset
, length
);
167 kunmap_atomic(vaddr
, KM_USER0
);
175 static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object
*obj
)
177 drm_i915_private_t
*dev_priv
= obj
->dev
->dev_private
;
178 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
180 return dev_priv
->mm
.bit_6_swizzle_x
== I915_BIT_6_SWIZZLE_9_10_17
&&
181 obj_priv
->tiling_mode
!= I915_TILING_NONE
;
185 slow_shmem_copy(struct page
*dst_page
,
187 struct page
*src_page
,
191 char *dst_vaddr
, *src_vaddr
;
193 dst_vaddr
= kmap(dst_page
);
194 src_vaddr
= kmap(src_page
);
196 memcpy(dst_vaddr
+ dst_offset
, src_vaddr
+ src_offset
, length
);
203 slow_shmem_bit17_copy(struct page
*gpu_page
,
205 struct page
*cpu_page
,
210 char *gpu_vaddr
, *cpu_vaddr
;
212 /* Use the unswizzled path if this page isn't affected. */
213 if ((page_to_phys(gpu_page
) & (1 << 17)) == 0) {
215 return slow_shmem_copy(cpu_page
, cpu_offset
,
216 gpu_page
, gpu_offset
, length
);
218 return slow_shmem_copy(gpu_page
, gpu_offset
,
219 cpu_page
, cpu_offset
, length
);
222 gpu_vaddr
= kmap(gpu_page
);
223 cpu_vaddr
= kmap(cpu_page
);
225 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
226 * XORing with the other bits (A9 for Y, A9 and A10 for X)
229 int cacheline_end
= ALIGN(gpu_offset
+ 1, 64);
230 int this_length
= min(cacheline_end
- gpu_offset
, length
);
231 int swizzled_gpu_offset
= gpu_offset
^ 64;
234 memcpy(cpu_vaddr
+ cpu_offset
,
235 gpu_vaddr
+ swizzled_gpu_offset
,
238 memcpy(gpu_vaddr
+ swizzled_gpu_offset
,
239 cpu_vaddr
+ cpu_offset
,
242 cpu_offset
+= this_length
;
243 gpu_offset
+= this_length
;
244 length
-= this_length
;
252 * This is the fast shmem pread path, which attempts to copy_from_user directly
253 * from the backing pages of the object to the user's address space. On a
254 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
257 i915_gem_shmem_pread_fast(struct drm_device
*dev
, struct drm_gem_object
*obj
,
258 struct drm_i915_gem_pread
*args
,
259 struct drm_file
*file_priv
)
261 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
263 loff_t offset
, page_base
;
264 char __user
*user_data
;
265 int page_offset
, page_length
;
268 user_data
= (char __user
*) (uintptr_t) args
->data_ptr
;
271 mutex_lock(&dev
->struct_mutex
);
273 ret
= i915_gem_object_get_pages(obj
, 0);
277 ret
= i915_gem_object_set_cpu_read_domain_range(obj
, args
->offset
,
282 obj_priv
= to_intel_bo(obj
);
283 offset
= args
->offset
;
286 /* Operation in this page
288 * page_base = page offset within aperture
289 * page_offset = offset within page
290 * page_length = bytes to copy for this page
292 page_base
= (offset
& ~(PAGE_SIZE
-1));
293 page_offset
= offset
& (PAGE_SIZE
-1);
294 page_length
= remain
;
295 if ((page_offset
+ remain
) > PAGE_SIZE
)
296 page_length
= PAGE_SIZE
- page_offset
;
298 ret
= fast_shmem_read(obj_priv
->pages
,
299 page_base
, page_offset
,
300 user_data
, page_length
);
304 remain
-= page_length
;
305 user_data
+= page_length
;
306 offset
+= page_length
;
310 i915_gem_object_put_pages(obj
);
312 mutex_unlock(&dev
->struct_mutex
);
318 i915_gem_object_get_pages_or_evict(struct drm_gem_object
*obj
)
322 ret
= i915_gem_object_get_pages(obj
, __GFP_NORETRY
| __GFP_NOWARN
);
324 /* If we've insufficient memory to map in the pages, attempt
325 * to make some space by throwing out some old buffers.
327 if (ret
== -ENOMEM
) {
328 struct drm_device
*dev
= obj
->dev
;
330 ret
= i915_gem_evict_something(dev
, obj
->size
,
331 i915_gem_get_gtt_alignment(obj
));
335 ret
= i915_gem_object_get_pages(obj
, 0);
342 * This is the fallback shmem pread path, which allocates temporary storage
343 * in kernel space to copy_to_user into outside of the struct_mutex, so we
344 * can copy out of the object's backing pages while holding the struct mutex
345 * and not take page faults.
348 i915_gem_shmem_pread_slow(struct drm_device
*dev
, struct drm_gem_object
*obj
,
349 struct drm_i915_gem_pread
*args
,
350 struct drm_file
*file_priv
)
352 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
353 struct mm_struct
*mm
= current
->mm
;
354 struct page
**user_pages
;
356 loff_t offset
, pinned_pages
, i
;
357 loff_t first_data_page
, last_data_page
, num_pages
;
358 int shmem_page_index
, shmem_page_offset
;
359 int data_page_index
, data_page_offset
;
362 uint64_t data_ptr
= args
->data_ptr
;
363 int do_bit17_swizzling
;
367 /* Pin the user pages containing the data. We can't fault while
368 * holding the struct mutex, yet we want to hold it while
369 * dereferencing the user data.
371 first_data_page
= data_ptr
/ PAGE_SIZE
;
372 last_data_page
= (data_ptr
+ args
->size
- 1) / PAGE_SIZE
;
373 num_pages
= last_data_page
- first_data_page
+ 1;
375 user_pages
= drm_calloc_large(num_pages
, sizeof(struct page
*));
376 if (user_pages
== NULL
)
379 down_read(&mm
->mmap_sem
);
380 pinned_pages
= get_user_pages(current
, mm
, (uintptr_t)args
->data_ptr
,
381 num_pages
, 1, 0, user_pages
, NULL
);
382 up_read(&mm
->mmap_sem
);
383 if (pinned_pages
< num_pages
) {
385 goto fail_put_user_pages
;
388 do_bit17_swizzling
= i915_gem_object_needs_bit17_swizzle(obj
);
390 mutex_lock(&dev
->struct_mutex
);
392 ret
= i915_gem_object_get_pages_or_evict(obj
);
396 ret
= i915_gem_object_set_cpu_read_domain_range(obj
, args
->offset
,
401 obj_priv
= to_intel_bo(obj
);
402 offset
= args
->offset
;
405 /* Operation in this page
407 * shmem_page_index = page number within shmem file
408 * shmem_page_offset = offset within page in shmem file
409 * data_page_index = page number in get_user_pages return
410 * data_page_offset = offset with data_page_index page.
411 * page_length = bytes to copy for this page
413 shmem_page_index
= offset
/ PAGE_SIZE
;
414 shmem_page_offset
= offset
& ~PAGE_MASK
;
415 data_page_index
= data_ptr
/ PAGE_SIZE
- first_data_page
;
416 data_page_offset
= data_ptr
& ~PAGE_MASK
;
418 page_length
= remain
;
419 if ((shmem_page_offset
+ page_length
) > PAGE_SIZE
)
420 page_length
= PAGE_SIZE
- shmem_page_offset
;
421 if ((data_page_offset
+ page_length
) > PAGE_SIZE
)
422 page_length
= PAGE_SIZE
- data_page_offset
;
424 if (do_bit17_swizzling
) {
425 slow_shmem_bit17_copy(obj_priv
->pages
[shmem_page_index
],
427 user_pages
[data_page_index
],
432 slow_shmem_copy(user_pages
[data_page_index
],
434 obj_priv
->pages
[shmem_page_index
],
439 remain
-= page_length
;
440 data_ptr
+= page_length
;
441 offset
+= page_length
;
445 i915_gem_object_put_pages(obj
);
447 mutex_unlock(&dev
->struct_mutex
);
449 for (i
= 0; i
< pinned_pages
; i
++) {
450 SetPageDirty(user_pages
[i
]);
451 page_cache_release(user_pages
[i
]);
453 drm_free_large(user_pages
);
459 * Reads data from the object referenced by handle.
461 * On error, the contents of *data are undefined.
464 i915_gem_pread_ioctl(struct drm_device
*dev
, void *data
,
465 struct drm_file
*file_priv
)
467 struct drm_i915_gem_pread
*args
= data
;
468 struct drm_gem_object
*obj
;
469 struct drm_i915_gem_object
*obj_priv
;
472 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
475 obj_priv
= to_intel_bo(obj
);
477 /* Bounds check source.
479 * XXX: This could use review for overflow issues...
481 if (args
->offset
> obj
->size
|| args
->size
> obj
->size
||
482 args
->offset
+ args
->size
> obj
->size
) {
483 drm_gem_object_unreference_unlocked(obj
);
487 if (i915_gem_object_needs_bit17_swizzle(obj
)) {
488 ret
= i915_gem_shmem_pread_slow(dev
, obj
, args
, file_priv
);
490 ret
= i915_gem_shmem_pread_fast(dev
, obj
, args
, file_priv
);
492 ret
= i915_gem_shmem_pread_slow(dev
, obj
, args
,
496 drm_gem_object_unreference_unlocked(obj
);
501 /* This is the fast write path which cannot handle
502 * page faults in the source data
506 fast_user_write(struct io_mapping
*mapping
,
507 loff_t page_base
, int page_offset
,
508 char __user
*user_data
,
512 unsigned long unwritten
;
514 vaddr_atomic
= io_mapping_map_atomic_wc(mapping
, page_base
, KM_USER0
);
515 unwritten
= __copy_from_user_inatomic_nocache(vaddr_atomic
+ page_offset
,
517 io_mapping_unmap_atomic(vaddr_atomic
, KM_USER0
);
523 /* Here's the write path which can sleep for
528 slow_kernel_write(struct io_mapping
*mapping
,
529 loff_t gtt_base
, int gtt_offset
,
530 struct page
*user_page
, int user_offset
,
533 char __iomem
*dst_vaddr
;
536 dst_vaddr
= io_mapping_map_wc(mapping
, gtt_base
);
537 src_vaddr
= kmap(user_page
);
539 memcpy_toio(dst_vaddr
+ gtt_offset
,
540 src_vaddr
+ user_offset
,
544 io_mapping_unmap(dst_vaddr
);
548 fast_shmem_write(struct page
**pages
,
549 loff_t page_base
, int page_offset
,
554 unsigned long unwritten
;
556 vaddr
= kmap_atomic(pages
[page_base
>> PAGE_SHIFT
], KM_USER0
);
559 unwritten
= __copy_from_user_inatomic(vaddr
+ page_offset
, data
, length
);
560 kunmap_atomic(vaddr
, KM_USER0
);
568 * This is the fast pwrite path, where we copy the data directly from the
569 * user into the GTT, uncached.
572 i915_gem_gtt_pwrite_fast(struct drm_device
*dev
, struct drm_gem_object
*obj
,
573 struct drm_i915_gem_pwrite
*args
,
574 struct drm_file
*file_priv
)
576 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
577 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
579 loff_t offset
, page_base
;
580 char __user
*user_data
;
581 int page_offset
, page_length
;
584 user_data
= (char __user
*) (uintptr_t) args
->data_ptr
;
586 if (!access_ok(VERIFY_READ
, user_data
, remain
))
590 mutex_lock(&dev
->struct_mutex
);
591 ret
= i915_gem_object_pin(obj
, 0);
593 mutex_unlock(&dev
->struct_mutex
);
596 ret
= i915_gem_object_set_to_gtt_domain(obj
, 1);
600 obj_priv
= to_intel_bo(obj
);
601 offset
= obj_priv
->gtt_offset
+ args
->offset
;
604 /* Operation in this page
606 * page_base = page offset within aperture
607 * page_offset = offset within page
608 * page_length = bytes to copy for this page
610 page_base
= (offset
& ~(PAGE_SIZE
-1));
611 page_offset
= offset
& (PAGE_SIZE
-1);
612 page_length
= remain
;
613 if ((page_offset
+ remain
) > PAGE_SIZE
)
614 page_length
= PAGE_SIZE
- page_offset
;
616 ret
= fast_user_write (dev_priv
->mm
.gtt_mapping
, page_base
,
617 page_offset
, user_data
, page_length
);
619 /* If we get a fault while copying data, then (presumably) our
620 * source page isn't available. Return the error and we'll
621 * retry in the slow path.
626 remain
-= page_length
;
627 user_data
+= page_length
;
628 offset
+= page_length
;
632 i915_gem_object_unpin(obj
);
633 mutex_unlock(&dev
->struct_mutex
);
639 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
640 * the memory and maps it using kmap_atomic for copying.
642 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
643 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
646 i915_gem_gtt_pwrite_slow(struct drm_device
*dev
, struct drm_gem_object
*obj
,
647 struct drm_i915_gem_pwrite
*args
,
648 struct drm_file
*file_priv
)
650 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
651 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
653 loff_t gtt_page_base
, offset
;
654 loff_t first_data_page
, last_data_page
, num_pages
;
655 loff_t pinned_pages
, i
;
656 struct page
**user_pages
;
657 struct mm_struct
*mm
= current
->mm
;
658 int gtt_page_offset
, data_page_offset
, data_page_index
, page_length
;
660 uint64_t data_ptr
= args
->data_ptr
;
664 /* Pin the user pages containing the data. We can't fault while
665 * holding the struct mutex, and all of the pwrite implementations
666 * want to hold it while dereferencing the user data.
668 first_data_page
= data_ptr
/ PAGE_SIZE
;
669 last_data_page
= (data_ptr
+ args
->size
- 1) / PAGE_SIZE
;
670 num_pages
= last_data_page
- first_data_page
+ 1;
672 user_pages
= drm_calloc_large(num_pages
, sizeof(struct page
*));
673 if (user_pages
== NULL
)
676 down_read(&mm
->mmap_sem
);
677 pinned_pages
= get_user_pages(current
, mm
, (uintptr_t)args
->data_ptr
,
678 num_pages
, 0, 0, user_pages
, NULL
);
679 up_read(&mm
->mmap_sem
);
680 if (pinned_pages
< num_pages
) {
682 goto out_unpin_pages
;
685 mutex_lock(&dev
->struct_mutex
);
686 ret
= i915_gem_object_pin(obj
, 0);
690 ret
= i915_gem_object_set_to_gtt_domain(obj
, 1);
692 goto out_unpin_object
;
694 obj_priv
= to_intel_bo(obj
);
695 offset
= obj_priv
->gtt_offset
+ args
->offset
;
698 /* Operation in this page
700 * gtt_page_base = page offset within aperture
701 * gtt_page_offset = offset within page in aperture
702 * data_page_index = page number in get_user_pages return
703 * data_page_offset = offset with data_page_index page.
704 * page_length = bytes to copy for this page
706 gtt_page_base
= offset
& PAGE_MASK
;
707 gtt_page_offset
= offset
& ~PAGE_MASK
;
708 data_page_index
= data_ptr
/ PAGE_SIZE
- first_data_page
;
709 data_page_offset
= data_ptr
& ~PAGE_MASK
;
711 page_length
= remain
;
712 if ((gtt_page_offset
+ page_length
) > PAGE_SIZE
)
713 page_length
= PAGE_SIZE
- gtt_page_offset
;
714 if ((data_page_offset
+ page_length
) > PAGE_SIZE
)
715 page_length
= PAGE_SIZE
- data_page_offset
;
717 slow_kernel_write(dev_priv
->mm
.gtt_mapping
,
718 gtt_page_base
, gtt_page_offset
,
719 user_pages
[data_page_index
],
723 remain
-= page_length
;
724 offset
+= page_length
;
725 data_ptr
+= page_length
;
729 i915_gem_object_unpin(obj
);
731 mutex_unlock(&dev
->struct_mutex
);
733 for (i
= 0; i
< pinned_pages
; i
++)
734 page_cache_release(user_pages
[i
]);
735 drm_free_large(user_pages
);
741 * This is the fast shmem pwrite path, which attempts to directly
742 * copy_from_user into the kmapped pages backing the object.
745 i915_gem_shmem_pwrite_fast(struct drm_device
*dev
, struct drm_gem_object
*obj
,
746 struct drm_i915_gem_pwrite
*args
,
747 struct drm_file
*file_priv
)
749 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
751 loff_t offset
, page_base
;
752 char __user
*user_data
;
753 int page_offset
, page_length
;
756 user_data
= (char __user
*) (uintptr_t) args
->data_ptr
;
759 mutex_lock(&dev
->struct_mutex
);
761 ret
= i915_gem_object_get_pages(obj
, 0);
765 ret
= i915_gem_object_set_to_cpu_domain(obj
, 1);
769 obj_priv
= to_intel_bo(obj
);
770 offset
= args
->offset
;
774 /* Operation in this page
776 * page_base = page offset within aperture
777 * page_offset = offset within page
778 * page_length = bytes to copy for this page
780 page_base
= (offset
& ~(PAGE_SIZE
-1));
781 page_offset
= offset
& (PAGE_SIZE
-1);
782 page_length
= remain
;
783 if ((page_offset
+ remain
) > PAGE_SIZE
)
784 page_length
= PAGE_SIZE
- page_offset
;
786 ret
= fast_shmem_write(obj_priv
->pages
,
787 page_base
, page_offset
,
788 user_data
, page_length
);
792 remain
-= page_length
;
793 user_data
+= page_length
;
794 offset
+= page_length
;
798 i915_gem_object_put_pages(obj
);
800 mutex_unlock(&dev
->struct_mutex
);
806 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
807 * the memory and maps it using kmap_atomic for copying.
809 * This avoids taking mmap_sem for faulting on the user's address while the
810 * struct_mutex is held.
813 i915_gem_shmem_pwrite_slow(struct drm_device
*dev
, struct drm_gem_object
*obj
,
814 struct drm_i915_gem_pwrite
*args
,
815 struct drm_file
*file_priv
)
817 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
818 struct mm_struct
*mm
= current
->mm
;
819 struct page
**user_pages
;
821 loff_t offset
, pinned_pages
, i
;
822 loff_t first_data_page
, last_data_page
, num_pages
;
823 int shmem_page_index
, shmem_page_offset
;
824 int data_page_index
, data_page_offset
;
827 uint64_t data_ptr
= args
->data_ptr
;
828 int do_bit17_swizzling
;
832 /* Pin the user pages containing the data. We can't fault while
833 * holding the struct mutex, and all of the pwrite implementations
834 * want to hold it while dereferencing the user data.
836 first_data_page
= data_ptr
/ PAGE_SIZE
;
837 last_data_page
= (data_ptr
+ args
->size
- 1) / PAGE_SIZE
;
838 num_pages
= last_data_page
- first_data_page
+ 1;
840 user_pages
= drm_calloc_large(num_pages
, sizeof(struct page
*));
841 if (user_pages
== NULL
)
844 down_read(&mm
->mmap_sem
);
845 pinned_pages
= get_user_pages(current
, mm
, (uintptr_t)args
->data_ptr
,
846 num_pages
, 0, 0, user_pages
, NULL
);
847 up_read(&mm
->mmap_sem
);
848 if (pinned_pages
< num_pages
) {
850 goto fail_put_user_pages
;
853 do_bit17_swizzling
= i915_gem_object_needs_bit17_swizzle(obj
);
855 mutex_lock(&dev
->struct_mutex
);
857 ret
= i915_gem_object_get_pages_or_evict(obj
);
861 ret
= i915_gem_object_set_to_cpu_domain(obj
, 1);
865 obj_priv
= to_intel_bo(obj
);
866 offset
= args
->offset
;
870 /* Operation in this page
872 * shmem_page_index = page number within shmem file
873 * shmem_page_offset = offset within page in shmem file
874 * data_page_index = page number in get_user_pages return
875 * data_page_offset = offset with data_page_index page.
876 * page_length = bytes to copy for this page
878 shmem_page_index
= offset
/ PAGE_SIZE
;
879 shmem_page_offset
= offset
& ~PAGE_MASK
;
880 data_page_index
= data_ptr
/ PAGE_SIZE
- first_data_page
;
881 data_page_offset
= data_ptr
& ~PAGE_MASK
;
883 page_length
= remain
;
884 if ((shmem_page_offset
+ page_length
) > PAGE_SIZE
)
885 page_length
= PAGE_SIZE
- shmem_page_offset
;
886 if ((data_page_offset
+ page_length
) > PAGE_SIZE
)
887 page_length
= PAGE_SIZE
- data_page_offset
;
889 if (do_bit17_swizzling
) {
890 slow_shmem_bit17_copy(obj_priv
->pages
[shmem_page_index
],
892 user_pages
[data_page_index
],
897 slow_shmem_copy(obj_priv
->pages
[shmem_page_index
],
899 user_pages
[data_page_index
],
904 remain
-= page_length
;
905 data_ptr
+= page_length
;
906 offset
+= page_length
;
910 i915_gem_object_put_pages(obj
);
912 mutex_unlock(&dev
->struct_mutex
);
914 for (i
= 0; i
< pinned_pages
; i
++)
915 page_cache_release(user_pages
[i
]);
916 drm_free_large(user_pages
);
922 * Writes data to the object referenced by handle.
924 * On error, the contents of the buffer that were to be modified are undefined.
927 i915_gem_pwrite_ioctl(struct drm_device
*dev
, void *data
,
928 struct drm_file
*file_priv
)
930 struct drm_i915_gem_pwrite
*args
= data
;
931 struct drm_gem_object
*obj
;
932 struct drm_i915_gem_object
*obj_priv
;
935 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
938 obj_priv
= to_intel_bo(obj
);
940 /* Bounds check destination.
942 * XXX: This could use review for overflow issues...
944 if (args
->offset
> obj
->size
|| args
->size
> obj
->size
||
945 args
->offset
+ args
->size
> obj
->size
) {
946 drm_gem_object_unreference_unlocked(obj
);
950 /* We can only do the GTT pwrite on untiled buffers, as otherwise
951 * it would end up going through the fenced access, and we'll get
952 * different detiling behavior between reading and writing.
953 * pread/pwrite currently are reading and writing from the CPU
954 * perspective, requiring manual detiling by the client.
956 if (obj_priv
->phys_obj
)
957 ret
= i915_gem_phys_pwrite(dev
, obj
, args
, file_priv
);
958 else if (obj_priv
->tiling_mode
== I915_TILING_NONE
&&
959 dev
->gtt_total
!= 0 &&
960 obj
->write_domain
!= I915_GEM_DOMAIN_CPU
) {
961 ret
= i915_gem_gtt_pwrite_fast(dev
, obj
, args
, file_priv
);
962 if (ret
== -EFAULT
) {
963 ret
= i915_gem_gtt_pwrite_slow(dev
, obj
, args
,
966 } else if (i915_gem_object_needs_bit17_swizzle(obj
)) {
967 ret
= i915_gem_shmem_pwrite_slow(dev
, obj
, args
, file_priv
);
969 ret
= i915_gem_shmem_pwrite_fast(dev
, obj
, args
, file_priv
);
970 if (ret
== -EFAULT
) {
971 ret
= i915_gem_shmem_pwrite_slow(dev
, obj
, args
,
978 DRM_INFO("pwrite failed %d\n", ret
);
981 drm_gem_object_unreference_unlocked(obj
);
987 * Called when user space prepares to use an object with the CPU, either
988 * through the mmap ioctl's mapping or a GTT mapping.
991 i915_gem_set_domain_ioctl(struct drm_device
*dev
, void *data
,
992 struct drm_file
*file_priv
)
994 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
995 struct drm_i915_gem_set_domain
*args
= data
;
996 struct drm_gem_object
*obj
;
997 struct drm_i915_gem_object
*obj_priv
;
998 uint32_t read_domains
= args
->read_domains
;
999 uint32_t write_domain
= args
->write_domain
;
1002 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
1005 /* Only handle setting domains to types used by the CPU. */
1006 if (write_domain
& I915_GEM_GPU_DOMAINS
)
1009 if (read_domains
& I915_GEM_GPU_DOMAINS
)
1012 /* Having something in the write domain implies it's in the read
1013 * domain, and only that read domain. Enforce that in the request.
1015 if (write_domain
!= 0 && read_domains
!= write_domain
)
1018 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
1021 obj_priv
= to_intel_bo(obj
);
1023 mutex_lock(&dev
->struct_mutex
);
1025 intel_mark_busy(dev
, obj
);
1028 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
1029 obj
, obj
->size
, read_domains
, write_domain
);
1031 if (read_domains
& I915_GEM_DOMAIN_GTT
) {
1032 ret
= i915_gem_object_set_to_gtt_domain(obj
, write_domain
!= 0);
1034 /* Update the LRU on the fence for the CPU access that's
1037 if (obj_priv
->fence_reg
!= I915_FENCE_REG_NONE
) {
1038 struct drm_i915_fence_reg
*reg
=
1039 &dev_priv
->fence_regs
[obj_priv
->fence_reg
];
1040 list_move_tail(®
->lru_list
,
1041 &dev_priv
->mm
.fence_list
);
1044 /* Silently promote "you're not bound, there was nothing to do"
1045 * to success, since the client was just asking us to
1046 * make sure everything was done.
1051 ret
= i915_gem_object_set_to_cpu_domain(obj
, write_domain
!= 0);
1055 /* Maintain LRU order of "inactive" objects */
1056 if (ret
== 0 && i915_gem_object_is_inactive(obj_priv
))
1057 list_move_tail(&obj_priv
->list
, &dev_priv
->mm
.inactive_list
);
1059 drm_gem_object_unreference(obj
);
1060 mutex_unlock(&dev
->struct_mutex
);
1065 * Called when user space has done writes to this buffer
1068 i915_gem_sw_finish_ioctl(struct drm_device
*dev
, void *data
,
1069 struct drm_file
*file_priv
)
1071 struct drm_i915_gem_sw_finish
*args
= data
;
1072 struct drm_gem_object
*obj
;
1073 struct drm_i915_gem_object
*obj_priv
;
1076 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
1079 mutex_lock(&dev
->struct_mutex
);
1080 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
1082 mutex_unlock(&dev
->struct_mutex
);
1087 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
1088 __func__
, args
->handle
, obj
, obj
->size
);
1090 obj_priv
= to_intel_bo(obj
);
1092 /* Pinned buffers may be scanout, so flush the cache */
1093 if (obj_priv
->pin_count
)
1094 i915_gem_object_flush_cpu_write_domain(obj
);
1096 drm_gem_object_unreference(obj
);
1097 mutex_unlock(&dev
->struct_mutex
);
1102 * Maps the contents of an object, returning the address it is mapped
1105 * While the mapping holds a reference on the contents of the object, it doesn't
1106 * imply a ref on the object itself.
1109 i915_gem_mmap_ioctl(struct drm_device
*dev
, void *data
,
1110 struct drm_file
*file_priv
)
1112 struct drm_i915_gem_mmap
*args
= data
;
1113 struct drm_gem_object
*obj
;
1117 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
1120 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
1124 offset
= args
->offset
;
1126 down_write(¤t
->mm
->mmap_sem
);
1127 addr
= do_mmap(obj
->filp
, 0, args
->size
,
1128 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
1130 up_write(¤t
->mm
->mmap_sem
);
1131 drm_gem_object_unreference_unlocked(obj
);
1132 if (IS_ERR((void *)addr
))
1135 args
->addr_ptr
= (uint64_t) addr
;
1141 * i915_gem_fault - fault a page into the GTT
1142 * vma: VMA in question
1145 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1146 * from userspace. The fault handler takes care of binding the object to
1147 * the GTT (if needed), allocating and programming a fence register (again,
1148 * only if needed based on whether the old reg is still valid or the object
1149 * is tiled) and inserting a new PTE into the faulting process.
1151 * Note that the faulting process may involve evicting existing objects
1152 * from the GTT and/or fence registers to make room. So performance may
1153 * suffer if the GTT working set is large or there are few fence registers
1156 int i915_gem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1158 struct drm_gem_object
*obj
= vma
->vm_private_data
;
1159 struct drm_device
*dev
= obj
->dev
;
1160 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1161 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1162 pgoff_t page_offset
;
1165 bool write
= !!(vmf
->flags
& FAULT_FLAG_WRITE
);
1167 /* We don't use vmf->pgoff since that has the fake offset */
1168 page_offset
= ((unsigned long)vmf
->virtual_address
- vma
->vm_start
) >>
1171 /* Now bind it into the GTT if needed */
1172 mutex_lock(&dev
->struct_mutex
);
1173 if (!obj_priv
->gtt_space
) {
1174 ret
= i915_gem_object_bind_to_gtt(obj
, 0);
1178 ret
= i915_gem_object_set_to_gtt_domain(obj
, write
);
1183 /* Need a new fence register? */
1184 if (obj_priv
->tiling_mode
!= I915_TILING_NONE
) {
1185 ret
= i915_gem_object_get_fence_reg(obj
, true);
1190 if (i915_gem_object_is_inactive(obj_priv
))
1191 list_move_tail(&obj_priv
->list
, &dev_priv
->mm
.inactive_list
);
1193 pfn
= ((dev
->agp
->base
+ obj_priv
->gtt_offset
) >> PAGE_SHIFT
) +
1196 /* Finally, remap it using the new GTT offset */
1197 ret
= vm_insert_pfn(vma
, (unsigned long)vmf
->virtual_address
, pfn
);
1199 mutex_unlock(&dev
->struct_mutex
);
1204 return VM_FAULT_NOPAGE
;
1207 return VM_FAULT_OOM
;
1209 return VM_FAULT_SIGBUS
;
1214 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1215 * @obj: obj in question
1217 * GEM memory mapping works by handing back to userspace a fake mmap offset
1218 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1219 * up the object based on the offset and sets up the various memory mapping
1222 * This routine allocates and attaches a fake offset for @obj.
1225 i915_gem_create_mmap_offset(struct drm_gem_object
*obj
)
1227 struct drm_device
*dev
= obj
->dev
;
1228 struct drm_gem_mm
*mm
= dev
->mm_private
;
1229 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1230 struct drm_map_list
*list
;
1231 struct drm_local_map
*map
;
1234 /* Set the object up for mmap'ing */
1235 list
= &obj
->map_list
;
1236 list
->map
= kzalloc(sizeof(struct drm_map_list
), GFP_KERNEL
);
1241 map
->type
= _DRM_GEM
;
1242 map
->size
= obj
->size
;
1245 /* Get a DRM GEM mmap offset allocated... */
1246 list
->file_offset_node
= drm_mm_search_free(&mm
->offset_manager
,
1247 obj
->size
/ PAGE_SIZE
, 0, 0);
1248 if (!list
->file_offset_node
) {
1249 DRM_ERROR("failed to allocate offset for bo %d\n", obj
->name
);
1254 list
->file_offset_node
= drm_mm_get_block(list
->file_offset_node
,
1255 obj
->size
/ PAGE_SIZE
, 0);
1256 if (!list
->file_offset_node
) {
1261 list
->hash
.key
= list
->file_offset_node
->start
;
1262 if (drm_ht_insert_item(&mm
->offset_hash
, &list
->hash
)) {
1263 DRM_ERROR("failed to add to map hash\n");
1268 /* By now we should be all set, any drm_mmap request on the offset
1269 * below will get to our mmap & fault handler */
1270 obj_priv
->mmap_offset
= ((uint64_t) list
->hash
.key
) << PAGE_SHIFT
;
1275 drm_mm_put_block(list
->file_offset_node
);
1283 * i915_gem_release_mmap - remove physical page mappings
1284 * @obj: obj in question
1286 * Preserve the reservation of the mmapping with the DRM core code, but
1287 * relinquish ownership of the pages back to the system.
1289 * It is vital that we remove the page mapping if we have mapped a tiled
1290 * object through the GTT and then lose the fence register due to
1291 * resource pressure. Similarly if the object has been moved out of the
1292 * aperture, than pages mapped into userspace must be revoked. Removing the
1293 * mapping will then trigger a page fault on the next user access, allowing
1294 * fixup by i915_gem_fault().
1297 i915_gem_release_mmap(struct drm_gem_object
*obj
)
1299 struct drm_device
*dev
= obj
->dev
;
1300 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1302 if (dev
->dev_mapping
)
1303 unmap_mapping_range(dev
->dev_mapping
,
1304 obj_priv
->mmap_offset
, obj
->size
, 1);
1308 i915_gem_free_mmap_offset(struct drm_gem_object
*obj
)
1310 struct drm_device
*dev
= obj
->dev
;
1311 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1312 struct drm_gem_mm
*mm
= dev
->mm_private
;
1313 struct drm_map_list
*list
;
1315 list
= &obj
->map_list
;
1316 drm_ht_remove_item(&mm
->offset_hash
, &list
->hash
);
1318 if (list
->file_offset_node
) {
1319 drm_mm_put_block(list
->file_offset_node
);
1320 list
->file_offset_node
= NULL
;
1328 obj_priv
->mmap_offset
= 0;
1332 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1333 * @obj: object to check
1335 * Return the required GTT alignment for an object, taking into account
1336 * potential fence register mapping if needed.
1339 i915_gem_get_gtt_alignment(struct drm_gem_object
*obj
)
1341 struct drm_device
*dev
= obj
->dev
;
1342 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1346 * Minimum alignment is 4k (GTT page size), but might be greater
1347 * if a fence register is needed for the object.
1349 if (IS_I965G(dev
) || obj_priv
->tiling_mode
== I915_TILING_NONE
)
1353 * Previous chips need to be aligned to the size of the smallest
1354 * fence register that can contain the object.
1361 for (i
= start
; i
< obj
->size
; i
<<= 1)
1368 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1370 * @data: GTT mapping ioctl data
1371 * @file_priv: GEM object info
1373 * Simply returns the fake offset to userspace so it can mmap it.
1374 * The mmap call will end up in drm_gem_mmap(), which will set things
1375 * up so we can get faults in the handler above.
1377 * The fault handler will take care of binding the object into the GTT
1378 * (since it may have been evicted to make room for something), allocating
1379 * a fence register, and mapping the appropriate aperture address into
1383 i915_gem_mmap_gtt_ioctl(struct drm_device
*dev
, void *data
,
1384 struct drm_file
*file_priv
)
1386 struct drm_i915_gem_mmap_gtt
*args
= data
;
1387 struct drm_gem_object
*obj
;
1388 struct drm_i915_gem_object
*obj_priv
;
1391 if (!(dev
->driver
->driver_features
& DRIVER_GEM
))
1394 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
1398 mutex_lock(&dev
->struct_mutex
);
1400 obj_priv
= to_intel_bo(obj
);
1402 if (obj_priv
->madv
!= I915_MADV_WILLNEED
) {
1403 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1404 drm_gem_object_unreference(obj
);
1405 mutex_unlock(&dev
->struct_mutex
);
1410 if (!obj_priv
->mmap_offset
) {
1411 ret
= i915_gem_create_mmap_offset(obj
);
1413 drm_gem_object_unreference(obj
);
1414 mutex_unlock(&dev
->struct_mutex
);
1419 args
->offset
= obj_priv
->mmap_offset
;
1422 * Pull it into the GTT so that we have a page list (makes the
1423 * initial fault faster and any subsequent flushing possible).
1425 if (!obj_priv
->agp_mem
) {
1426 ret
= i915_gem_object_bind_to_gtt(obj
, 0);
1428 drm_gem_object_unreference(obj
);
1429 mutex_unlock(&dev
->struct_mutex
);
1434 drm_gem_object_unreference(obj
);
1435 mutex_unlock(&dev
->struct_mutex
);
1441 i915_gem_object_put_pages(struct drm_gem_object
*obj
)
1443 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1444 int page_count
= obj
->size
/ PAGE_SIZE
;
1447 BUG_ON(obj_priv
->pages_refcount
== 0);
1448 BUG_ON(obj_priv
->madv
== __I915_MADV_PURGED
);
1450 if (--obj_priv
->pages_refcount
!= 0)
1453 if (obj_priv
->tiling_mode
!= I915_TILING_NONE
)
1454 i915_gem_object_save_bit_17_swizzle(obj
);
1456 if (obj_priv
->madv
== I915_MADV_DONTNEED
)
1457 obj_priv
->dirty
= 0;
1459 for (i
= 0; i
< page_count
; i
++) {
1460 if (obj_priv
->dirty
)
1461 set_page_dirty(obj_priv
->pages
[i
]);
1463 if (obj_priv
->madv
== I915_MADV_WILLNEED
)
1464 mark_page_accessed(obj_priv
->pages
[i
]);
1466 page_cache_release(obj_priv
->pages
[i
]);
1468 obj_priv
->dirty
= 0;
1470 drm_free_large(obj_priv
->pages
);
1471 obj_priv
->pages
= NULL
;
1475 i915_gem_next_request_seqno(struct drm_device
*dev
,
1476 struct intel_ring_buffer
*ring
)
1478 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1480 ring
->outstanding_lazy_request
= true;
1482 return dev_priv
->next_seqno
;
1486 i915_gem_object_move_to_active(struct drm_gem_object
*obj
,
1487 struct intel_ring_buffer
*ring
)
1489 struct drm_device
*dev
= obj
->dev
;
1490 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1491 uint32_t seqno
= i915_gem_next_request_seqno(dev
, ring
);
1493 BUG_ON(ring
== NULL
);
1494 obj_priv
->ring
= ring
;
1496 /* Add a reference if we're newly entering the active list. */
1497 if (!obj_priv
->active
) {
1498 drm_gem_object_reference(obj
);
1499 obj_priv
->active
= 1;
1502 /* Move from whatever list we were on to the tail of execution. */
1503 list_move_tail(&obj_priv
->list
, &ring
->active_list
);
1504 obj_priv
->last_rendering_seqno
= seqno
;
1508 i915_gem_object_move_to_flushing(struct drm_gem_object
*obj
)
1510 struct drm_device
*dev
= obj
->dev
;
1511 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1512 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1514 BUG_ON(!obj_priv
->active
);
1515 list_move_tail(&obj_priv
->list
, &dev_priv
->mm
.flushing_list
);
1516 obj_priv
->last_rendering_seqno
= 0;
1519 /* Immediately discard the backing storage */
1521 i915_gem_object_truncate(struct drm_gem_object
*obj
)
1523 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1524 struct inode
*inode
;
1526 /* Our goal here is to return as much of the memory as
1527 * is possible back to the system as we are called from OOM.
1528 * To do this we must instruct the shmfs to drop all of its
1529 * backing pages, *now*. Here we mirror the actions taken
1530 * when by shmem_delete_inode() to release the backing store.
1532 inode
= obj
->filp
->f_path
.dentry
->d_inode
;
1533 truncate_inode_pages(inode
->i_mapping
, 0);
1534 if (inode
->i_op
->truncate_range
)
1535 inode
->i_op
->truncate_range(inode
, 0, (loff_t
)-1);
1537 obj_priv
->madv
= __I915_MADV_PURGED
;
1541 i915_gem_object_is_purgeable(struct drm_i915_gem_object
*obj_priv
)
1543 return obj_priv
->madv
== I915_MADV_DONTNEED
;
1547 i915_gem_object_move_to_inactive(struct drm_gem_object
*obj
)
1549 struct drm_device
*dev
= obj
->dev
;
1550 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1551 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1553 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
1554 if (obj_priv
->pin_count
!= 0)
1555 list_del_init(&obj_priv
->list
);
1557 list_move_tail(&obj_priv
->list
, &dev_priv
->mm
.inactive_list
);
1559 BUG_ON(!list_empty(&obj_priv
->gpu_write_list
));
1561 obj_priv
->last_rendering_seqno
= 0;
1562 obj_priv
->ring
= NULL
;
1563 if (obj_priv
->active
) {
1564 obj_priv
->active
= 0;
1565 drm_gem_object_unreference(obj
);
1567 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
1571 i915_gem_process_flushing_list(struct drm_device
*dev
,
1572 uint32_t flush_domains
,
1573 struct intel_ring_buffer
*ring
)
1575 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1576 struct drm_i915_gem_object
*obj_priv
, *next
;
1578 list_for_each_entry_safe(obj_priv
, next
,
1579 &dev_priv
->mm
.gpu_write_list
,
1581 struct drm_gem_object
*obj
= &obj_priv
->base
;
1583 if ((obj
->write_domain
& flush_domains
) ==
1584 obj
->write_domain
&&
1585 obj_priv
->ring
->ring_flag
== ring
->ring_flag
) {
1586 uint32_t old_write_domain
= obj
->write_domain
;
1588 obj
->write_domain
= 0;
1589 list_del_init(&obj_priv
->gpu_write_list
);
1590 i915_gem_object_move_to_active(obj
, ring
);
1592 /* update the fence lru list */
1593 if (obj_priv
->fence_reg
!= I915_FENCE_REG_NONE
) {
1594 struct drm_i915_fence_reg
*reg
=
1595 &dev_priv
->fence_regs
[obj_priv
->fence_reg
];
1596 list_move_tail(®
->lru_list
,
1597 &dev_priv
->mm
.fence_list
);
1600 trace_i915_gem_object_change_domain(obj
,
1608 i915_add_request(struct drm_device
*dev
,
1609 struct drm_file
*file_priv
,
1610 struct drm_i915_gem_request
*request
,
1611 struct intel_ring_buffer
*ring
)
1613 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1614 struct drm_i915_file_private
*i915_file_priv
= NULL
;
1618 if (file_priv
!= NULL
)
1619 i915_file_priv
= file_priv
->driver_priv
;
1621 if (request
== NULL
) {
1622 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
1623 if (request
== NULL
)
1627 seqno
= ring
->add_request(dev
, ring
, file_priv
, 0);
1629 request
->seqno
= seqno
;
1630 request
->ring
= ring
;
1631 request
->emitted_jiffies
= jiffies
;
1632 was_empty
= list_empty(&ring
->request_list
);
1633 list_add_tail(&request
->list
, &ring
->request_list
);
1635 if (i915_file_priv
) {
1636 list_add_tail(&request
->client_list
,
1637 &i915_file_priv
->mm
.request_list
);
1639 INIT_LIST_HEAD(&request
->client_list
);
1642 if (!dev_priv
->mm
.suspended
) {
1643 mod_timer(&dev_priv
->hangcheck_timer
,
1644 jiffies
+ msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD
));
1646 queue_delayed_work(dev_priv
->wq
,
1647 &dev_priv
->mm
.retire_work
, HZ
);
1653 * Command execution barrier
1655 * Ensures that all commands in the ring are finished
1656 * before signalling the CPU
1659 i915_retire_commands(struct drm_device
*dev
, struct intel_ring_buffer
*ring
)
1661 uint32_t flush_domains
= 0;
1663 /* The sampler always gets flushed on i965 (sigh) */
1665 flush_domains
|= I915_GEM_DOMAIN_SAMPLER
;
1667 ring
->flush(dev
, ring
,
1668 I915_GEM_DOMAIN_COMMAND
, flush_domains
);
1672 * Moves buffers associated only with the given active seqno from the active
1673 * to inactive list, potentially freeing them.
1676 i915_gem_retire_request(struct drm_device
*dev
,
1677 struct drm_i915_gem_request
*request
)
1679 trace_i915_gem_request_retire(dev
, request
->seqno
);
1681 /* Move any buffers on the active list that are no longer referenced
1682 * by the ringbuffer to the flushing/inactive lists as appropriate.
1684 while (!list_empty(&request
->ring
->active_list
)) {
1685 struct drm_gem_object
*obj
;
1686 struct drm_i915_gem_object
*obj_priv
;
1688 obj_priv
= list_first_entry(&request
->ring
->active_list
,
1689 struct drm_i915_gem_object
,
1691 obj
= &obj_priv
->base
;
1693 /* If the seqno being retired doesn't match the oldest in the
1694 * list, then the oldest in the list must still be newer than
1697 if (obj_priv
->last_rendering_seqno
!= request
->seqno
)
1701 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1702 __func__
, request
->seqno
, obj
);
1705 if (obj
->write_domain
!= 0)
1706 i915_gem_object_move_to_flushing(obj
);
1708 i915_gem_object_move_to_inactive(obj
);
1713 * Returns true if seq1 is later than seq2.
1716 i915_seqno_passed(uint32_t seq1
, uint32_t seq2
)
1718 return (int32_t)(seq1
- seq2
) >= 0;
1722 i915_get_gem_seqno(struct drm_device
*dev
,
1723 struct intel_ring_buffer
*ring
)
1725 return ring
->get_gem_seqno(dev
, ring
);
1729 * This function clears the request list as sequence numbers are passed.
1732 i915_gem_retire_requests_ring(struct drm_device
*dev
,
1733 struct intel_ring_buffer
*ring
)
1735 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1738 if (!ring
->status_page
.page_addr
1739 || list_empty(&ring
->request_list
))
1742 seqno
= i915_get_gem_seqno(dev
, ring
);
1744 while (!list_empty(&ring
->request_list
)) {
1745 struct drm_i915_gem_request
*request
;
1746 uint32_t retiring_seqno
;
1748 request
= list_first_entry(&ring
->request_list
,
1749 struct drm_i915_gem_request
,
1751 retiring_seqno
= request
->seqno
;
1753 if (i915_seqno_passed(seqno
, retiring_seqno
) ||
1754 atomic_read(&dev_priv
->mm
.wedged
)) {
1755 i915_gem_retire_request(dev
, request
);
1757 list_del(&request
->list
);
1758 list_del(&request
->client_list
);
1764 if (unlikely (dev_priv
->trace_irq_seqno
&&
1765 i915_seqno_passed(dev_priv
->trace_irq_seqno
, seqno
))) {
1767 ring
->user_irq_put(dev
, ring
);
1768 dev_priv
->trace_irq_seqno
= 0;
1773 i915_gem_retire_requests(struct drm_device
*dev
)
1775 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1777 if (!list_empty(&dev_priv
->mm
.deferred_free_list
)) {
1778 struct drm_i915_gem_object
*obj_priv
, *tmp
;
1780 /* We must be careful that during unbind() we do not
1781 * accidentally infinitely recurse into retire requests.
1783 * retire -> free -> unbind -> wait -> retire_ring
1785 list_for_each_entry_safe(obj_priv
, tmp
,
1786 &dev_priv
->mm
.deferred_free_list
,
1788 i915_gem_free_object_tail(&obj_priv
->base
);
1791 i915_gem_retire_requests_ring(dev
, &dev_priv
->render_ring
);
1793 i915_gem_retire_requests_ring(dev
, &dev_priv
->bsd_ring
);
1797 i915_gem_retire_work_handler(struct work_struct
*work
)
1799 drm_i915_private_t
*dev_priv
;
1800 struct drm_device
*dev
;
1802 dev_priv
= container_of(work
, drm_i915_private_t
,
1803 mm
.retire_work
.work
);
1804 dev
= dev_priv
->dev
;
1806 mutex_lock(&dev
->struct_mutex
);
1807 i915_gem_retire_requests(dev
);
1809 if (!dev_priv
->mm
.suspended
&&
1810 (!list_empty(&dev_priv
->render_ring
.request_list
) ||
1812 !list_empty(&dev_priv
->bsd_ring
.request_list
))))
1813 queue_delayed_work(dev_priv
->wq
, &dev_priv
->mm
.retire_work
, HZ
);
1814 mutex_unlock(&dev
->struct_mutex
);
1818 i915_do_wait_request(struct drm_device
*dev
, uint32_t seqno
,
1819 bool interruptible
, struct intel_ring_buffer
*ring
)
1821 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1827 if (seqno
== dev_priv
->next_seqno
) {
1828 seqno
= i915_add_request(dev
, NULL
, NULL
, ring
);
1833 if (atomic_read(&dev_priv
->mm
.wedged
))
1836 if (!i915_seqno_passed(ring
->get_gem_seqno(dev
, ring
), seqno
)) {
1837 if (HAS_PCH_SPLIT(dev
))
1838 ier
= I915_READ(DEIER
) | I915_READ(GTIER
);
1840 ier
= I915_READ(IER
);
1842 DRM_ERROR("something (likely vbetool) disabled "
1843 "interrupts, re-enabling\n");
1844 i915_driver_irq_preinstall(dev
);
1845 i915_driver_irq_postinstall(dev
);
1848 trace_i915_gem_request_wait_begin(dev
, seqno
);
1850 ring
->waiting_gem_seqno
= seqno
;
1851 ring
->user_irq_get(dev
, ring
);
1853 ret
= wait_event_interruptible(ring
->irq_queue
,
1855 ring
->get_gem_seqno(dev
, ring
), seqno
)
1856 || atomic_read(&dev_priv
->mm
.wedged
));
1858 wait_event(ring
->irq_queue
,
1860 ring
->get_gem_seqno(dev
, ring
), seqno
)
1861 || atomic_read(&dev_priv
->mm
.wedged
));
1863 ring
->user_irq_put(dev
, ring
);
1864 ring
->waiting_gem_seqno
= 0;
1866 trace_i915_gem_request_wait_end(dev
, seqno
);
1868 if (atomic_read(&dev_priv
->mm
.wedged
))
1871 if (ret
&& ret
!= -ERESTARTSYS
)
1872 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
1873 __func__
, ret
, seqno
, ring
->get_gem_seqno(dev
, ring
),
1874 dev_priv
->next_seqno
);
1876 /* Directly dispatch request retiring. While we have the work queue
1877 * to handle this, the waiter on a request often wants an associated
1878 * buffer to have made it to the inactive list, and we would need
1879 * a separate wait queue to handle that.
1882 i915_gem_retire_requests_ring(dev
, ring
);
1888 * Waits for a sequence number to be signaled, and cleans up the
1889 * request and object lists appropriately for that event.
1892 i915_wait_request(struct drm_device
*dev
, uint32_t seqno
,
1893 struct intel_ring_buffer
*ring
)
1895 return i915_do_wait_request(dev
, seqno
, 1, ring
);
1899 i915_gem_flush(struct drm_device
*dev
,
1900 uint32_t invalidate_domains
,
1901 uint32_t flush_domains
)
1903 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
1905 if (flush_domains
& I915_GEM_DOMAIN_CPU
)
1906 drm_agp_chipset_flush(dev
);
1908 dev_priv
->render_ring
.flush(dev
, &dev_priv
->render_ring
,
1913 dev_priv
->bsd_ring
.flush(dev
, &dev_priv
->bsd_ring
,
1919 * Ensures that all rendering to the object has completed and the object is
1920 * safe to unbind from the GTT or access from the CPU.
1923 i915_gem_object_wait_rendering(struct drm_gem_object
*obj
,
1926 struct drm_device
*dev
= obj
->dev
;
1927 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1930 /* This function only exists to support waiting for existing rendering,
1931 * not for emitting required flushes.
1933 BUG_ON((obj
->write_domain
& I915_GEM_GPU_DOMAINS
) != 0);
1935 /* If there is rendering queued on the buffer being evicted, wait for
1938 if (obj_priv
->active
) {
1940 DRM_INFO("%s: object %p wait for seqno %08x\n",
1941 __func__
, obj
, obj_priv
->last_rendering_seqno
);
1943 ret
= i915_do_wait_request(dev
,
1944 obj_priv
->last_rendering_seqno
,
1955 * Unbinds an object from the GTT aperture.
1958 i915_gem_object_unbind(struct drm_gem_object
*obj
)
1960 struct drm_device
*dev
= obj
->dev
;
1961 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
1965 DRM_INFO("%s:%d %p\n", __func__
, __LINE__
, obj
);
1966 DRM_INFO("gtt_space %p\n", obj_priv
->gtt_space
);
1968 if (obj_priv
->gtt_space
== NULL
)
1971 if (obj_priv
->pin_count
!= 0) {
1972 DRM_ERROR("Attempting to unbind pinned buffer\n");
1976 /* blow away mappings if mapped through GTT */
1977 i915_gem_release_mmap(obj
);
1979 /* Move the object to the CPU domain to ensure that
1980 * any possible CPU writes while it's not in the GTT
1981 * are flushed when we go to remap it. This will
1982 * also ensure that all pending GPU writes are finished
1985 ret
= i915_gem_object_set_to_cpu_domain(obj
, 1);
1986 if (ret
== -ERESTARTSYS
)
1988 /* Continue on if we fail due to EIO, the GPU is hung so we
1989 * should be safe and we need to cleanup or else we might
1990 * cause memory corruption through use-after-free.
1993 /* release the fence reg _after_ flushing */
1994 if (obj_priv
->fence_reg
!= I915_FENCE_REG_NONE
)
1995 i915_gem_clear_fence_reg(obj
);
1997 if (obj_priv
->agp_mem
!= NULL
) {
1998 drm_unbind_agp(obj_priv
->agp_mem
);
1999 drm_free_agp(obj_priv
->agp_mem
, obj
->size
/ PAGE_SIZE
);
2000 obj_priv
->agp_mem
= NULL
;
2003 i915_gem_object_put_pages(obj
);
2004 BUG_ON(obj_priv
->pages_refcount
);
2006 if (obj_priv
->gtt_space
) {
2007 atomic_dec(&dev
->gtt_count
);
2008 atomic_sub(obj
->size
, &dev
->gtt_memory
);
2010 drm_mm_put_block(obj_priv
->gtt_space
);
2011 obj_priv
->gtt_space
= NULL
;
2014 /* Remove ourselves from the LRU list if present. */
2015 if (!list_empty(&obj_priv
->list
))
2016 list_del_init(&obj_priv
->list
);
2018 if (i915_gem_object_is_purgeable(obj_priv
))
2019 i915_gem_object_truncate(obj
);
2021 trace_i915_gem_object_unbind(obj
);
2027 i915_gpu_idle(struct drm_device
*dev
)
2029 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2033 lists_empty
= (list_empty(&dev_priv
->mm
.flushing_list
) &&
2034 list_empty(&dev_priv
->render_ring
.active_list
) &&
2036 list_empty(&dev_priv
->bsd_ring
.active_list
)));
2040 /* Flush everything onto the inactive list. */
2041 i915_gem_flush(dev
, I915_GEM_GPU_DOMAINS
, I915_GEM_GPU_DOMAINS
);
2043 ret
= i915_wait_request(dev
,
2044 i915_gem_next_request_seqno(dev
, &dev_priv
->render_ring
),
2045 &dev_priv
->render_ring
);
2050 ret
= i915_wait_request(dev
,
2051 i915_gem_next_request_seqno(dev
, &dev_priv
->bsd_ring
),
2052 &dev_priv
->bsd_ring
);
2061 i915_gem_object_get_pages(struct drm_gem_object
*obj
,
2064 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2066 struct address_space
*mapping
;
2067 struct inode
*inode
;
2070 BUG_ON(obj_priv
->pages_refcount
2071 == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT
);
2073 if (obj_priv
->pages_refcount
++ != 0)
2076 /* Get the list of pages out of our struct file. They'll be pinned
2077 * at this point until we release them.
2079 page_count
= obj
->size
/ PAGE_SIZE
;
2080 BUG_ON(obj_priv
->pages
!= NULL
);
2081 obj_priv
->pages
= drm_calloc_large(page_count
, sizeof(struct page
*));
2082 if (obj_priv
->pages
== NULL
) {
2083 obj_priv
->pages_refcount
--;
2087 inode
= obj
->filp
->f_path
.dentry
->d_inode
;
2088 mapping
= inode
->i_mapping
;
2089 for (i
= 0; i
< page_count
; i
++) {
2090 page
= read_cache_page_gfp(mapping
, i
,
2098 obj_priv
->pages
[i
] = page
;
2101 if (obj_priv
->tiling_mode
!= I915_TILING_NONE
)
2102 i915_gem_object_do_bit_17_swizzle(obj
);
2108 page_cache_release(obj_priv
->pages
[i
]);
2110 drm_free_large(obj_priv
->pages
);
2111 obj_priv
->pages
= NULL
;
2112 obj_priv
->pages_refcount
--;
2113 return PTR_ERR(page
);
2116 static void sandybridge_write_fence_reg(struct drm_i915_fence_reg
*reg
)
2118 struct drm_gem_object
*obj
= reg
->obj
;
2119 struct drm_device
*dev
= obj
->dev
;
2120 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2121 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2122 int regnum
= obj_priv
->fence_reg
;
2125 val
= (uint64_t)((obj_priv
->gtt_offset
+ obj
->size
- 4096) &
2127 val
|= obj_priv
->gtt_offset
& 0xfffff000;
2128 val
|= (uint64_t)((obj_priv
->stride
/ 128) - 1) <<
2129 SANDYBRIDGE_FENCE_PITCH_SHIFT
;
2131 if (obj_priv
->tiling_mode
== I915_TILING_Y
)
2132 val
|= 1 << I965_FENCE_TILING_Y_SHIFT
;
2133 val
|= I965_FENCE_REG_VALID
;
2135 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0
+ (regnum
* 8), val
);
2138 static void i965_write_fence_reg(struct drm_i915_fence_reg
*reg
)
2140 struct drm_gem_object
*obj
= reg
->obj
;
2141 struct drm_device
*dev
= obj
->dev
;
2142 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2143 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2144 int regnum
= obj_priv
->fence_reg
;
2147 val
= (uint64_t)((obj_priv
->gtt_offset
+ obj
->size
- 4096) &
2149 val
|= obj_priv
->gtt_offset
& 0xfffff000;
2150 val
|= ((obj_priv
->stride
/ 128) - 1) << I965_FENCE_PITCH_SHIFT
;
2151 if (obj_priv
->tiling_mode
== I915_TILING_Y
)
2152 val
|= 1 << I965_FENCE_TILING_Y_SHIFT
;
2153 val
|= I965_FENCE_REG_VALID
;
2155 I915_WRITE64(FENCE_REG_965_0
+ (regnum
* 8), val
);
2158 static void i915_write_fence_reg(struct drm_i915_fence_reg
*reg
)
2160 struct drm_gem_object
*obj
= reg
->obj
;
2161 struct drm_device
*dev
= obj
->dev
;
2162 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2163 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2164 int regnum
= obj_priv
->fence_reg
;
2166 uint32_t fence_reg
, val
;
2169 if ((obj_priv
->gtt_offset
& ~I915_FENCE_START_MASK
) ||
2170 (obj_priv
->gtt_offset
& (obj
->size
- 1))) {
2171 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
2172 __func__
, obj_priv
->gtt_offset
, obj
->size
);
2176 if (obj_priv
->tiling_mode
== I915_TILING_Y
&&
2177 HAS_128_BYTE_Y_TILING(dev
))
2182 /* Note: pitch better be a power of two tile widths */
2183 pitch_val
= obj_priv
->stride
/ tile_width
;
2184 pitch_val
= ffs(pitch_val
) - 1;
2186 if (obj_priv
->tiling_mode
== I915_TILING_Y
&&
2187 HAS_128_BYTE_Y_TILING(dev
))
2188 WARN_ON(pitch_val
> I830_FENCE_MAX_PITCH_VAL
);
2190 WARN_ON(pitch_val
> I915_FENCE_MAX_PITCH_VAL
);
2192 val
= obj_priv
->gtt_offset
;
2193 if (obj_priv
->tiling_mode
== I915_TILING_Y
)
2194 val
|= 1 << I830_FENCE_TILING_Y_SHIFT
;
2195 val
|= I915_FENCE_SIZE_BITS(obj
->size
);
2196 val
|= pitch_val
<< I830_FENCE_PITCH_SHIFT
;
2197 val
|= I830_FENCE_REG_VALID
;
2200 fence_reg
= FENCE_REG_830_0
+ (regnum
* 4);
2202 fence_reg
= FENCE_REG_945_8
+ ((regnum
- 8) * 4);
2203 I915_WRITE(fence_reg
, val
);
2206 static void i830_write_fence_reg(struct drm_i915_fence_reg
*reg
)
2208 struct drm_gem_object
*obj
= reg
->obj
;
2209 struct drm_device
*dev
= obj
->dev
;
2210 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2211 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2212 int regnum
= obj_priv
->fence_reg
;
2215 uint32_t fence_size_bits
;
2217 if ((obj_priv
->gtt_offset
& ~I830_FENCE_START_MASK
) ||
2218 (obj_priv
->gtt_offset
& (obj
->size
- 1))) {
2219 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
2220 __func__
, obj_priv
->gtt_offset
);
2224 pitch_val
= obj_priv
->stride
/ 128;
2225 pitch_val
= ffs(pitch_val
) - 1;
2226 WARN_ON(pitch_val
> I830_FENCE_MAX_PITCH_VAL
);
2228 val
= obj_priv
->gtt_offset
;
2229 if (obj_priv
->tiling_mode
== I915_TILING_Y
)
2230 val
|= 1 << I830_FENCE_TILING_Y_SHIFT
;
2231 fence_size_bits
= I830_FENCE_SIZE_BITS(obj
->size
);
2232 WARN_ON(fence_size_bits
& ~0x00000f00);
2233 val
|= fence_size_bits
;
2234 val
|= pitch_val
<< I830_FENCE_PITCH_SHIFT
;
2235 val
|= I830_FENCE_REG_VALID
;
2237 I915_WRITE(FENCE_REG_830_0
+ (regnum
* 4), val
);
2240 static int i915_find_fence_reg(struct drm_device
*dev
,
2243 struct drm_i915_fence_reg
*reg
= NULL
;
2244 struct drm_i915_gem_object
*obj_priv
= NULL
;
2245 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
2246 struct drm_gem_object
*obj
= NULL
;
2249 /* First try to find a free reg */
2251 for (i
= dev_priv
->fence_reg_start
; i
< dev_priv
->num_fence_regs
; i
++) {
2252 reg
= &dev_priv
->fence_regs
[i
];
2256 obj_priv
= to_intel_bo(reg
->obj
);
2257 if (!obj_priv
->pin_count
)
2264 /* None available, try to steal one or wait for a user to finish */
2265 i
= I915_FENCE_REG_NONE
;
2266 list_for_each_entry(reg
, &dev_priv
->mm
.fence_list
,
2269 obj_priv
= to_intel_bo(obj
);
2271 if (obj_priv
->pin_count
)
2275 i
= obj_priv
->fence_reg
;
2279 BUG_ON(i
== I915_FENCE_REG_NONE
);
2281 /* We only have a reference on obj from the active list. put_fence_reg
2282 * might drop that one, causing a use-after-free in it. So hold a
2283 * private reference to obj like the other callers of put_fence_reg
2284 * (set_tiling ioctl) do. */
2285 drm_gem_object_reference(obj
);
2286 ret
= i915_gem_object_put_fence_reg(obj
, interruptible
);
2287 drm_gem_object_unreference(obj
);
2295 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2296 * @obj: object to map through a fence reg
2298 * When mapping objects through the GTT, userspace wants to be able to write
2299 * to them without having to worry about swizzling if the object is tiled.
2301 * This function walks the fence regs looking for a free one for @obj,
2302 * stealing one if it can't find any.
2304 * It then sets up the reg based on the object's properties: address, pitch
2305 * and tiling format.
2308 i915_gem_object_get_fence_reg(struct drm_gem_object
*obj
,
2311 struct drm_device
*dev
= obj
->dev
;
2312 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
2313 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2314 struct drm_i915_fence_reg
*reg
= NULL
;
2317 /* Just update our place in the LRU if our fence is getting used. */
2318 if (obj_priv
->fence_reg
!= I915_FENCE_REG_NONE
) {
2319 reg
= &dev_priv
->fence_regs
[obj_priv
->fence_reg
];
2320 list_move_tail(®
->lru_list
, &dev_priv
->mm
.fence_list
);
2324 switch (obj_priv
->tiling_mode
) {
2325 case I915_TILING_NONE
:
2326 WARN(1, "allocating a fence for non-tiled object?\n");
2329 if (!obj_priv
->stride
)
2331 WARN((obj_priv
->stride
& (512 - 1)),
2332 "object 0x%08x is X tiled but has non-512B pitch\n",
2333 obj_priv
->gtt_offset
);
2336 if (!obj_priv
->stride
)
2338 WARN((obj_priv
->stride
& (128 - 1)),
2339 "object 0x%08x is Y tiled but has non-128B pitch\n",
2340 obj_priv
->gtt_offset
);
2344 ret
= i915_find_fence_reg(dev
, interruptible
);
2348 obj_priv
->fence_reg
= ret
;
2349 reg
= &dev_priv
->fence_regs
[obj_priv
->fence_reg
];
2350 list_add_tail(®
->lru_list
, &dev_priv
->mm
.fence_list
);
2355 sandybridge_write_fence_reg(reg
);
2356 else if (IS_I965G(dev
))
2357 i965_write_fence_reg(reg
);
2358 else if (IS_I9XX(dev
))
2359 i915_write_fence_reg(reg
);
2361 i830_write_fence_reg(reg
);
2363 trace_i915_gem_object_get_fence(obj
, obj_priv
->fence_reg
,
2364 obj_priv
->tiling_mode
);
2370 * i915_gem_clear_fence_reg - clear out fence register info
2371 * @obj: object to clear
2373 * Zeroes out the fence register itself and clears out the associated
2374 * data structures in dev_priv and obj_priv.
2377 i915_gem_clear_fence_reg(struct drm_gem_object
*obj
)
2379 struct drm_device
*dev
= obj
->dev
;
2380 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2381 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2382 struct drm_i915_fence_reg
*reg
=
2383 &dev_priv
->fence_regs
[obj_priv
->fence_reg
];
2386 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0
+
2387 (obj_priv
->fence_reg
* 8), 0);
2388 } else if (IS_I965G(dev
)) {
2389 I915_WRITE64(FENCE_REG_965_0
+ (obj_priv
->fence_reg
* 8), 0);
2393 if (obj_priv
->fence_reg
< 8)
2394 fence_reg
= FENCE_REG_830_0
+ obj_priv
->fence_reg
* 4;
2396 fence_reg
= FENCE_REG_945_8
+ (obj_priv
->fence_reg
-
2399 I915_WRITE(fence_reg
, 0);
2403 obj_priv
->fence_reg
= I915_FENCE_REG_NONE
;
2404 list_del_init(®
->lru_list
);
2408 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2409 * to the buffer to finish, and then resets the fence register.
2410 * @obj: tiled object holding a fence register.
2411 * @bool: whether the wait upon the fence is interruptible
2413 * Zeroes out the fence register itself and clears out the associated
2414 * data structures in dev_priv and obj_priv.
2417 i915_gem_object_put_fence_reg(struct drm_gem_object
*obj
,
2420 struct drm_device
*dev
= obj
->dev
;
2421 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2423 if (obj_priv
->fence_reg
== I915_FENCE_REG_NONE
)
2426 /* If we've changed tiling, GTT-mappings of the object
2427 * need to re-fault to ensure that the correct fence register
2428 * setup is in place.
2430 i915_gem_release_mmap(obj
);
2432 /* On the i915, GPU access to tiled buffers is via a fence,
2433 * therefore we must wait for any outstanding access to complete
2434 * before clearing the fence.
2436 if (!IS_I965G(dev
)) {
2439 ret
= i915_gem_object_flush_gpu_write_domain(obj
, true);
2443 ret
= i915_gem_object_wait_rendering(obj
, interruptible
);
2448 i915_gem_object_flush_gtt_write_domain(obj
);
2449 i915_gem_clear_fence_reg(obj
);
2455 * Finds free space in the GTT aperture and binds the object there.
2458 i915_gem_object_bind_to_gtt(struct drm_gem_object
*obj
, unsigned alignment
)
2460 struct drm_device
*dev
= obj
->dev
;
2461 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
2462 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2463 struct drm_mm_node
*free_space
;
2464 gfp_t gfpmask
= __GFP_NORETRY
| __GFP_NOWARN
;
2467 if (obj_priv
->madv
!= I915_MADV_WILLNEED
) {
2468 DRM_ERROR("Attempting to bind a purgeable object\n");
2473 alignment
= i915_gem_get_gtt_alignment(obj
);
2474 if (alignment
& (i915_gem_get_gtt_alignment(obj
) - 1)) {
2475 DRM_ERROR("Invalid object alignment requested %u\n", alignment
);
2479 /* If the object is bigger than the entire aperture, reject it early
2480 * before evicting everything in a vain attempt to find space.
2482 if (obj
->size
> dev
->gtt_total
) {
2483 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2488 free_space
= drm_mm_search_free(&dev_priv
->mm
.gtt_space
,
2489 obj
->size
, alignment
, 0);
2490 if (free_space
!= NULL
) {
2491 obj_priv
->gtt_space
= drm_mm_get_block(free_space
, obj
->size
,
2493 if (obj_priv
->gtt_space
!= NULL
)
2494 obj_priv
->gtt_offset
= obj_priv
->gtt_space
->start
;
2496 if (obj_priv
->gtt_space
== NULL
) {
2497 /* If the gtt is empty and we're still having trouble
2498 * fitting our object in, we're out of memory.
2501 DRM_INFO("%s: GTT full, evicting something\n", __func__
);
2503 ret
= i915_gem_evict_something(dev
, obj
->size
, alignment
);
2511 DRM_INFO("Binding object of size %zd at 0x%08x\n",
2512 obj
->size
, obj_priv
->gtt_offset
);
2514 ret
= i915_gem_object_get_pages(obj
, gfpmask
);
2516 drm_mm_put_block(obj_priv
->gtt_space
);
2517 obj_priv
->gtt_space
= NULL
;
2519 if (ret
== -ENOMEM
) {
2520 /* first try to clear up some space from the GTT */
2521 ret
= i915_gem_evict_something(dev
, obj
->size
,
2524 /* now try to shrink everyone else */
2539 /* Create an AGP memory structure pointing at our pages, and bind it
2542 obj_priv
->agp_mem
= drm_agp_bind_pages(dev
,
2544 obj
->size
>> PAGE_SHIFT
,
2545 obj_priv
->gtt_offset
,
2546 obj_priv
->agp_type
);
2547 if (obj_priv
->agp_mem
== NULL
) {
2548 i915_gem_object_put_pages(obj
);
2549 drm_mm_put_block(obj_priv
->gtt_space
);
2550 obj_priv
->gtt_space
= NULL
;
2552 ret
= i915_gem_evict_something(dev
, obj
->size
, alignment
);
2558 atomic_inc(&dev
->gtt_count
);
2559 atomic_add(obj
->size
, &dev
->gtt_memory
);
2561 /* keep track of bounds object by adding it to the inactive list */
2562 list_add_tail(&obj_priv
->list
, &dev_priv
->mm
.inactive_list
);
2564 /* Assert that the object is not currently in any GPU domain. As it
2565 * wasn't in the GTT, there shouldn't be any way it could have been in
2568 BUG_ON(obj
->read_domains
& I915_GEM_GPU_DOMAINS
);
2569 BUG_ON(obj
->write_domain
& I915_GEM_GPU_DOMAINS
);
2571 trace_i915_gem_object_bind(obj
, obj_priv
->gtt_offset
);
2577 i915_gem_clflush_object(struct drm_gem_object
*obj
)
2579 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2581 /* If we don't have a page list set up, then we're not pinned
2582 * to GPU, and we can ignore the cache flush because it'll happen
2583 * again at bind time.
2585 if (obj_priv
->pages
== NULL
)
2588 trace_i915_gem_object_clflush(obj
);
2590 drm_clflush_pages(obj_priv
->pages
, obj
->size
/ PAGE_SIZE
);
2593 /** Flushes any GPU write domain for the object if it's dirty. */
2595 i915_gem_object_flush_gpu_write_domain(struct drm_gem_object
*obj
,
2598 struct drm_device
*dev
= obj
->dev
;
2599 uint32_t old_write_domain
;
2601 if ((obj
->write_domain
& I915_GEM_GPU_DOMAINS
) == 0)
2604 /* Queue the GPU write cache flushing we need. */
2605 old_write_domain
= obj
->write_domain
;
2606 i915_gem_flush(dev
, 0, obj
->write_domain
);
2607 BUG_ON(obj
->write_domain
);
2609 trace_i915_gem_object_change_domain(obj
,
2616 return i915_gem_object_wait_rendering(obj
, true);
2619 /** Flushes the GTT write domain for the object if it's dirty. */
2621 i915_gem_object_flush_gtt_write_domain(struct drm_gem_object
*obj
)
2623 uint32_t old_write_domain
;
2625 if (obj
->write_domain
!= I915_GEM_DOMAIN_GTT
)
2628 /* No actual flushing is required for the GTT write domain. Writes
2629 * to it immediately go to main memory as far as we know, so there's
2630 * no chipset flush. It also doesn't land in render cache.
2632 old_write_domain
= obj
->write_domain
;
2633 obj
->write_domain
= 0;
2635 trace_i915_gem_object_change_domain(obj
,
2640 /** Flushes the CPU write domain for the object if it's dirty. */
2642 i915_gem_object_flush_cpu_write_domain(struct drm_gem_object
*obj
)
2644 struct drm_device
*dev
= obj
->dev
;
2645 uint32_t old_write_domain
;
2647 if (obj
->write_domain
!= I915_GEM_DOMAIN_CPU
)
2650 i915_gem_clflush_object(obj
);
2651 drm_agp_chipset_flush(dev
);
2652 old_write_domain
= obj
->write_domain
;
2653 obj
->write_domain
= 0;
2655 trace_i915_gem_object_change_domain(obj
,
2661 * Moves a single object to the GTT read, and possibly write domain.
2663 * This function returns when the move is complete, including waiting on
2667 i915_gem_object_set_to_gtt_domain(struct drm_gem_object
*obj
, int write
)
2669 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2670 uint32_t old_write_domain
, old_read_domains
;
2673 /* Not valid to be called on unbound objects. */
2674 if (obj_priv
->gtt_space
== NULL
)
2677 ret
= i915_gem_object_flush_gpu_write_domain(obj
, false);
2681 i915_gem_object_flush_cpu_write_domain(obj
);
2684 ret
= i915_gem_object_wait_rendering(obj
, true);
2689 old_write_domain
= obj
->write_domain
;
2690 old_read_domains
= obj
->read_domains
;
2692 /* It should now be out of any other write domains, and we can update
2693 * the domain values for our changes.
2695 BUG_ON((obj
->write_domain
& ~I915_GEM_DOMAIN_GTT
) != 0);
2696 obj
->read_domains
|= I915_GEM_DOMAIN_GTT
;
2698 obj
->read_domains
= I915_GEM_DOMAIN_GTT
;
2699 obj
->write_domain
= I915_GEM_DOMAIN_GTT
;
2700 obj_priv
->dirty
= 1;
2703 trace_i915_gem_object_change_domain(obj
,
2711 * Prepare buffer for display plane. Use uninterruptible for possible flush
2712 * wait, as in modesetting process we're not supposed to be interrupted.
2715 i915_gem_object_set_to_display_plane(struct drm_gem_object
*obj
,
2718 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2719 uint32_t old_read_domains
;
2722 /* Not valid to be called on unbound objects. */
2723 if (obj_priv
->gtt_space
== NULL
)
2726 ret
= i915_gem_object_flush_gpu_write_domain(obj
, pipelined
);
2730 i915_gem_object_flush_cpu_write_domain(obj
);
2732 old_read_domains
= obj
->read_domains
;
2733 obj
->read_domains
= I915_GEM_DOMAIN_GTT
;
2735 trace_i915_gem_object_change_domain(obj
,
2743 * Moves a single object to the CPU read, and possibly write domain.
2745 * This function returns when the move is complete, including waiting on
2749 i915_gem_object_set_to_cpu_domain(struct drm_gem_object
*obj
, int write
)
2751 uint32_t old_write_domain
, old_read_domains
;
2754 ret
= i915_gem_object_flush_gpu_write_domain(obj
, false);
2758 i915_gem_object_flush_gtt_write_domain(obj
);
2760 /* If we have a partially-valid cache of the object in the CPU,
2761 * finish invalidating it and free the per-page flags.
2763 i915_gem_object_set_to_full_cpu_read_domain(obj
);
2766 ret
= i915_gem_object_wait_rendering(obj
, true);
2771 old_write_domain
= obj
->write_domain
;
2772 old_read_domains
= obj
->read_domains
;
2774 /* Flush the CPU cache if it's still invalid. */
2775 if ((obj
->read_domains
& I915_GEM_DOMAIN_CPU
) == 0) {
2776 i915_gem_clflush_object(obj
);
2778 obj
->read_domains
|= I915_GEM_DOMAIN_CPU
;
2781 /* It should now be out of any other write domains, and we can update
2782 * the domain values for our changes.
2784 BUG_ON((obj
->write_domain
& ~I915_GEM_DOMAIN_CPU
) != 0);
2786 /* If we're writing through the CPU, then the GPU read domains will
2787 * need to be invalidated at next use.
2790 obj
->read_domains
&= I915_GEM_DOMAIN_CPU
;
2791 obj
->write_domain
= I915_GEM_DOMAIN_CPU
;
2794 trace_i915_gem_object_change_domain(obj
,
2802 * Set the next domain for the specified object. This
2803 * may not actually perform the necessary flushing/invaliding though,
2804 * as that may want to be batched with other set_domain operations
2806 * This is (we hope) the only really tricky part of gem. The goal
2807 * is fairly simple -- track which caches hold bits of the object
2808 * and make sure they remain coherent. A few concrete examples may
2809 * help to explain how it works. For shorthand, we use the notation
2810 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2811 * a pair of read and write domain masks.
2813 * Case 1: the batch buffer
2819 * 5. Unmapped from GTT
2822 * Let's take these a step at a time
2825 * Pages allocated from the kernel may still have
2826 * cache contents, so we set them to (CPU, CPU) always.
2827 * 2. Written by CPU (using pwrite)
2828 * The pwrite function calls set_domain (CPU, CPU) and
2829 * this function does nothing (as nothing changes)
2831 * This function asserts that the object is not
2832 * currently in any GPU-based read or write domains
2834 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2835 * As write_domain is zero, this function adds in the
2836 * current read domains (CPU+COMMAND, 0).
2837 * flush_domains is set to CPU.
2838 * invalidate_domains is set to COMMAND
2839 * clflush is run to get data out of the CPU caches
2840 * then i915_dev_set_domain calls i915_gem_flush to
2841 * emit an MI_FLUSH and drm_agp_chipset_flush
2842 * 5. Unmapped from GTT
2843 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2844 * flush_domains and invalidate_domains end up both zero
2845 * so no flushing/invalidating happens
2849 * Case 2: The shared render buffer
2853 * 3. Read/written by GPU
2854 * 4. set_domain to (CPU,CPU)
2855 * 5. Read/written by CPU
2856 * 6. Read/written by GPU
2859 * Same as last example, (CPU, CPU)
2861 * Nothing changes (assertions find that it is not in the GPU)
2862 * 3. Read/written by GPU
2863 * execbuffer calls set_domain (RENDER, RENDER)
2864 * flush_domains gets CPU
2865 * invalidate_domains gets GPU
2867 * MI_FLUSH and drm_agp_chipset_flush
2868 * 4. set_domain (CPU, CPU)
2869 * flush_domains gets GPU
2870 * invalidate_domains gets CPU
2871 * wait_rendering (obj) to make sure all drawing is complete.
2872 * This will include an MI_FLUSH to get the data from GPU
2874 * clflush (obj) to invalidate the CPU cache
2875 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2876 * 5. Read/written by CPU
2877 * cache lines are loaded and dirtied
2878 * 6. Read written by GPU
2879 * Same as last GPU access
2881 * Case 3: The constant buffer
2886 * 4. Updated (written) by CPU again
2895 * flush_domains = CPU
2896 * invalidate_domains = RENDER
2899 * drm_agp_chipset_flush
2900 * 4. Updated (written) by CPU again
2902 * flush_domains = 0 (no previous write domain)
2903 * invalidate_domains = 0 (no new read domains)
2906 * flush_domains = CPU
2907 * invalidate_domains = RENDER
2910 * drm_agp_chipset_flush
2913 i915_gem_object_set_to_gpu_domain(struct drm_gem_object
*obj
)
2915 struct drm_device
*dev
= obj
->dev
;
2916 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
2917 uint32_t invalidate_domains
= 0;
2918 uint32_t flush_domains
= 0;
2919 uint32_t old_read_domains
;
2921 BUG_ON(obj
->pending_read_domains
& I915_GEM_DOMAIN_CPU
);
2922 BUG_ON(obj
->pending_write_domain
== I915_GEM_DOMAIN_CPU
);
2924 intel_mark_busy(dev
, obj
);
2927 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2929 obj
->read_domains
, obj
->pending_read_domains
,
2930 obj
->write_domain
, obj
->pending_write_domain
);
2933 * If the object isn't moving to a new write domain,
2934 * let the object stay in multiple read domains
2936 if (obj
->pending_write_domain
== 0)
2937 obj
->pending_read_domains
|= obj
->read_domains
;
2939 obj_priv
->dirty
= 1;
2942 * Flush the current write domain if
2943 * the new read domains don't match. Invalidate
2944 * any read domains which differ from the old
2947 if (obj
->write_domain
&&
2948 obj
->write_domain
!= obj
->pending_read_domains
) {
2949 flush_domains
|= obj
->write_domain
;
2950 invalidate_domains
|=
2951 obj
->pending_read_domains
& ~obj
->write_domain
;
2954 * Invalidate any read caches which may have
2955 * stale data. That is, any new read domains.
2957 invalidate_domains
|= obj
->pending_read_domains
& ~obj
->read_domains
;
2958 if ((flush_domains
| invalidate_domains
) & I915_GEM_DOMAIN_CPU
) {
2960 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2961 __func__
, flush_domains
, invalidate_domains
);
2963 i915_gem_clflush_object(obj
);
2966 old_read_domains
= obj
->read_domains
;
2968 /* The actual obj->write_domain will be updated with
2969 * pending_write_domain after we emit the accumulated flush for all
2970 * of our domain changes in execbuffers (which clears objects'
2971 * write_domains). So if we have a current write domain that we
2972 * aren't changing, set pending_write_domain to that.
2974 if (flush_domains
== 0 && obj
->pending_write_domain
== 0)
2975 obj
->pending_write_domain
= obj
->write_domain
;
2976 obj
->read_domains
= obj
->pending_read_domains
;
2978 dev
->invalidate_domains
|= invalidate_domains
;
2979 dev
->flush_domains
|= flush_domains
;
2981 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2983 obj
->read_domains
, obj
->write_domain
,
2984 dev
->invalidate_domains
, dev
->flush_domains
);
2987 trace_i915_gem_object_change_domain(obj
,
2993 * Moves the object from a partially CPU read to a full one.
2995 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2996 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2999 i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object
*obj
)
3001 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
3003 if (!obj_priv
->page_cpu_valid
)
3006 /* If we're partially in the CPU read domain, finish moving it in.
3008 if (obj
->read_domains
& I915_GEM_DOMAIN_CPU
) {
3011 for (i
= 0; i
<= (obj
->size
- 1) / PAGE_SIZE
; i
++) {
3012 if (obj_priv
->page_cpu_valid
[i
])
3014 drm_clflush_pages(obj_priv
->pages
+ i
, 1);
3018 /* Free the page_cpu_valid mappings which are now stale, whether
3019 * or not we've got I915_GEM_DOMAIN_CPU.
3021 kfree(obj_priv
->page_cpu_valid
);
3022 obj_priv
->page_cpu_valid
= NULL
;
3026 * Set the CPU read domain on a range of the object.
3028 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3029 * not entirely valid. The page_cpu_valid member of the object flags which
3030 * pages have been flushed, and will be respected by
3031 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3032 * of the whole object.
3034 * This function returns when the move is complete, including waiting on
3038 i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object
*obj
,
3039 uint64_t offset
, uint64_t size
)
3041 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
3042 uint32_t old_read_domains
;
3045 if (offset
== 0 && size
== obj
->size
)
3046 return i915_gem_object_set_to_cpu_domain(obj
, 0);
3048 ret
= i915_gem_object_flush_gpu_write_domain(obj
, false);
3051 i915_gem_object_flush_gtt_write_domain(obj
);
3053 /* If we're already fully in the CPU read domain, we're done. */
3054 if (obj_priv
->page_cpu_valid
== NULL
&&
3055 (obj
->read_domains
& I915_GEM_DOMAIN_CPU
) != 0)
3058 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3059 * newly adding I915_GEM_DOMAIN_CPU
3061 if (obj_priv
->page_cpu_valid
== NULL
) {
3062 obj_priv
->page_cpu_valid
= kzalloc(obj
->size
/ PAGE_SIZE
,
3064 if (obj_priv
->page_cpu_valid
== NULL
)
3066 } else if ((obj
->read_domains
& I915_GEM_DOMAIN_CPU
) == 0)
3067 memset(obj_priv
->page_cpu_valid
, 0, obj
->size
/ PAGE_SIZE
);
3069 /* Flush the cache on any pages that are still invalid from the CPU's
3072 for (i
= offset
/ PAGE_SIZE
; i
<= (offset
+ size
- 1) / PAGE_SIZE
;
3074 if (obj_priv
->page_cpu_valid
[i
])
3077 drm_clflush_pages(obj_priv
->pages
+ i
, 1);
3079 obj_priv
->page_cpu_valid
[i
] = 1;
3082 /* It should now be out of any other write domains, and we can update
3083 * the domain values for our changes.
3085 BUG_ON((obj
->write_domain
& ~I915_GEM_DOMAIN_CPU
) != 0);
3087 old_read_domains
= obj
->read_domains
;
3088 obj
->read_domains
|= I915_GEM_DOMAIN_CPU
;
3090 trace_i915_gem_object_change_domain(obj
,
3098 * Pin an object to the GTT and evaluate the relocations landing in it.
3101 i915_gem_object_pin_and_relocate(struct drm_gem_object
*obj
,
3102 struct drm_file
*file_priv
,
3103 struct drm_i915_gem_exec_object2
*entry
,
3104 struct drm_i915_gem_relocation_entry
*relocs
)
3106 struct drm_device
*dev
= obj
->dev
;
3107 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
3108 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
3110 void __iomem
*reloc_page
;
3113 need_fence
= entry
->flags
& EXEC_OBJECT_NEEDS_FENCE
&&
3114 obj_priv
->tiling_mode
!= I915_TILING_NONE
;
3116 /* Check fence reg constraints and rebind if necessary */
3118 !i915_gem_object_fence_offset_ok(obj
,
3119 obj_priv
->tiling_mode
)) {
3120 ret
= i915_gem_object_unbind(obj
);
3125 /* Choose the GTT offset for our buffer and put it there. */
3126 ret
= i915_gem_object_pin(obj
, (uint32_t) entry
->alignment
);
3131 * Pre-965 chips need a fence register set up in order to
3132 * properly handle blits to/from tiled surfaces.
3135 ret
= i915_gem_object_get_fence_reg(obj
, false);
3137 i915_gem_object_unpin(obj
);
3142 entry
->offset
= obj_priv
->gtt_offset
;
3144 /* Apply the relocations, using the GTT aperture to avoid cache
3145 * flushing requirements.
3147 for (i
= 0; i
< entry
->relocation_count
; i
++) {
3148 struct drm_i915_gem_relocation_entry
*reloc
= &relocs
[i
];
3149 struct drm_gem_object
*target_obj
;
3150 struct drm_i915_gem_object
*target_obj_priv
;
3151 uint32_t reloc_val
, reloc_offset
;
3152 uint32_t __iomem
*reloc_entry
;
3154 target_obj
= drm_gem_object_lookup(obj
->dev
, file_priv
,
3155 reloc
->target_handle
);
3156 if (target_obj
== NULL
) {
3157 i915_gem_object_unpin(obj
);
3160 target_obj_priv
= to_intel_bo(target_obj
);
3163 DRM_INFO("%s: obj %p offset %08x target %d "
3164 "read %08x write %08x gtt %08x "
3165 "presumed %08x delta %08x\n",
3168 (int) reloc
->offset
,
3169 (int) reloc
->target_handle
,
3170 (int) reloc
->read_domains
,
3171 (int) reloc
->write_domain
,
3172 (int) target_obj_priv
->gtt_offset
,
3173 (int) reloc
->presumed_offset
,
3177 /* The target buffer should have appeared before us in the
3178 * exec_object list, so it should have a GTT space bound by now.
3180 if (target_obj_priv
->gtt_space
== NULL
) {
3181 DRM_ERROR("No GTT space found for object %d\n",
3182 reloc
->target_handle
);
3183 drm_gem_object_unreference(target_obj
);
3184 i915_gem_object_unpin(obj
);
3188 /* Validate that the target is in a valid r/w GPU domain */
3189 if (reloc
->write_domain
& (reloc
->write_domain
- 1)) {
3190 DRM_ERROR("reloc with multiple write domains: "
3191 "obj %p target %d offset %d "
3192 "read %08x write %08x",
3193 obj
, reloc
->target_handle
,
3194 (int) reloc
->offset
,
3195 reloc
->read_domains
,
3196 reloc
->write_domain
);
3199 if (reloc
->write_domain
& I915_GEM_DOMAIN_CPU
||
3200 reloc
->read_domains
& I915_GEM_DOMAIN_CPU
) {
3201 DRM_ERROR("reloc with read/write CPU domains: "
3202 "obj %p target %d offset %d "
3203 "read %08x write %08x",
3204 obj
, reloc
->target_handle
,
3205 (int) reloc
->offset
,
3206 reloc
->read_domains
,
3207 reloc
->write_domain
);
3208 drm_gem_object_unreference(target_obj
);
3209 i915_gem_object_unpin(obj
);
3212 if (reloc
->write_domain
&& target_obj
->pending_write_domain
&&
3213 reloc
->write_domain
!= target_obj
->pending_write_domain
) {
3214 DRM_ERROR("Write domain conflict: "
3215 "obj %p target %d offset %d "
3216 "new %08x old %08x\n",
3217 obj
, reloc
->target_handle
,
3218 (int) reloc
->offset
,
3219 reloc
->write_domain
,
3220 target_obj
->pending_write_domain
);
3221 drm_gem_object_unreference(target_obj
);
3222 i915_gem_object_unpin(obj
);
3226 target_obj
->pending_read_domains
|= reloc
->read_domains
;
3227 target_obj
->pending_write_domain
|= reloc
->write_domain
;
3229 /* If the relocation already has the right value in it, no
3230 * more work needs to be done.
3232 if (target_obj_priv
->gtt_offset
== reloc
->presumed_offset
) {
3233 drm_gem_object_unreference(target_obj
);
3237 /* Check that the relocation address is valid... */
3238 if (reloc
->offset
> obj
->size
- 4) {
3239 DRM_ERROR("Relocation beyond object bounds: "
3240 "obj %p target %d offset %d size %d.\n",
3241 obj
, reloc
->target_handle
,
3242 (int) reloc
->offset
, (int) obj
->size
);
3243 drm_gem_object_unreference(target_obj
);
3244 i915_gem_object_unpin(obj
);
3247 if (reloc
->offset
& 3) {
3248 DRM_ERROR("Relocation not 4-byte aligned: "
3249 "obj %p target %d offset %d.\n",
3250 obj
, reloc
->target_handle
,
3251 (int) reloc
->offset
);
3252 drm_gem_object_unreference(target_obj
);
3253 i915_gem_object_unpin(obj
);
3257 /* and points to somewhere within the target object. */
3258 if (reloc
->delta
>= target_obj
->size
) {
3259 DRM_ERROR("Relocation beyond target object bounds: "
3260 "obj %p target %d delta %d size %d.\n",
3261 obj
, reloc
->target_handle
,
3262 (int) reloc
->delta
, (int) target_obj
->size
);
3263 drm_gem_object_unreference(target_obj
);
3264 i915_gem_object_unpin(obj
);
3268 ret
= i915_gem_object_set_to_gtt_domain(obj
, 1);
3270 drm_gem_object_unreference(target_obj
);
3271 i915_gem_object_unpin(obj
);
3275 /* Map the page containing the relocation we're going to
3278 reloc_offset
= obj_priv
->gtt_offset
+ reloc
->offset
;
3279 reloc_page
= io_mapping_map_atomic_wc(dev_priv
->mm
.gtt_mapping
,
3283 reloc_entry
= (uint32_t __iomem
*)(reloc_page
+
3284 (reloc_offset
& (PAGE_SIZE
- 1)));
3285 reloc_val
= target_obj_priv
->gtt_offset
+ reloc
->delta
;
3288 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
3289 obj
, (unsigned int) reloc
->offset
,
3290 readl(reloc_entry
), reloc_val
);
3292 writel(reloc_val
, reloc_entry
);
3293 io_mapping_unmap_atomic(reloc_page
, KM_USER0
);
3295 /* The updated presumed offset for this entry will be
3296 * copied back out to the user.
3298 reloc
->presumed_offset
= target_obj_priv
->gtt_offset
;
3300 drm_gem_object_unreference(target_obj
);
3305 i915_gem_dump_object(obj
, 128, __func__
, ~0);
3310 /* Throttle our rendering by waiting until the ring has completed our requests
3311 * emitted over 20 msec ago.
3313 * Note that if we were to use the current jiffies each time around the loop,
3314 * we wouldn't escape the function with any frames outstanding if the time to
3315 * render a frame was over 20ms.
3317 * This should get us reasonable parallelism between CPU and GPU but also
3318 * relatively low latency when blocking on a particular request to finish.
3321 i915_gem_ring_throttle(struct drm_device
*dev
, struct drm_file
*file_priv
)
3323 struct drm_i915_file_private
*i915_file_priv
= file_priv
->driver_priv
;
3325 unsigned long recent_enough
= jiffies
- msecs_to_jiffies(20);
3327 mutex_lock(&dev
->struct_mutex
);
3328 while (!list_empty(&i915_file_priv
->mm
.request_list
)) {
3329 struct drm_i915_gem_request
*request
;
3331 request
= list_first_entry(&i915_file_priv
->mm
.request_list
,
3332 struct drm_i915_gem_request
,
3335 if (time_after_eq(request
->emitted_jiffies
, recent_enough
))
3338 ret
= i915_wait_request(dev
, request
->seqno
, request
->ring
);
3342 mutex_unlock(&dev
->struct_mutex
);
3348 i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2
*exec_list
,
3349 uint32_t buffer_count
,
3350 struct drm_i915_gem_relocation_entry
**relocs
)
3352 uint32_t reloc_count
= 0, reloc_index
= 0, i
;
3356 for (i
= 0; i
< buffer_count
; i
++) {
3357 if (reloc_count
+ exec_list
[i
].relocation_count
< reloc_count
)
3359 reloc_count
+= exec_list
[i
].relocation_count
;
3362 *relocs
= drm_calloc_large(reloc_count
, sizeof(**relocs
));
3363 if (*relocs
== NULL
) {
3364 DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count
);
3368 for (i
= 0; i
< buffer_count
; i
++) {
3369 struct drm_i915_gem_relocation_entry __user
*user_relocs
;
3371 user_relocs
= (void __user
*)(uintptr_t)exec_list
[i
].relocs_ptr
;
3373 ret
= copy_from_user(&(*relocs
)[reloc_index
],
3375 exec_list
[i
].relocation_count
*
3378 drm_free_large(*relocs
);
3383 reloc_index
+= exec_list
[i
].relocation_count
;
3390 i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2
*exec_list
,
3391 uint32_t buffer_count
,
3392 struct drm_i915_gem_relocation_entry
*relocs
)
3394 uint32_t reloc_count
= 0, i
;
3400 for (i
= 0; i
< buffer_count
; i
++) {
3401 struct drm_i915_gem_relocation_entry __user
*user_relocs
;
3404 user_relocs
= (void __user
*)(uintptr_t)exec_list
[i
].relocs_ptr
;
3406 unwritten
= copy_to_user(user_relocs
,
3407 &relocs
[reloc_count
],
3408 exec_list
[i
].relocation_count
*
3416 reloc_count
+= exec_list
[i
].relocation_count
;
3420 drm_free_large(relocs
);
3426 i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2
*exec
,
3427 uint64_t exec_offset
)
3429 uint32_t exec_start
, exec_len
;
3431 exec_start
= (uint32_t) exec_offset
+ exec
->batch_start_offset
;
3432 exec_len
= (uint32_t) exec
->batch_len
;
3434 if ((exec_start
| exec_len
) & 0x7)
3444 i915_gem_wait_for_pending_flip(struct drm_device
*dev
,
3445 struct drm_gem_object
**object_list
,
3448 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
3449 struct drm_i915_gem_object
*obj_priv
;
3454 prepare_to_wait(&dev_priv
->pending_flip_queue
,
3455 &wait
, TASK_INTERRUPTIBLE
);
3456 for (i
= 0; i
< count
; i
++) {
3457 obj_priv
= to_intel_bo(object_list
[i
]);
3458 if (atomic_read(&obj_priv
->pending_flip
) > 0)
3464 if (!signal_pending(current
)) {
3465 mutex_unlock(&dev
->struct_mutex
);
3467 mutex_lock(&dev
->struct_mutex
);
3473 finish_wait(&dev_priv
->pending_flip_queue
, &wait
);
3479 i915_gem_do_execbuffer(struct drm_device
*dev
, void *data
,
3480 struct drm_file
*file_priv
,
3481 struct drm_i915_gem_execbuffer2
*args
,
3482 struct drm_i915_gem_exec_object2
*exec_list
)
3484 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
3485 struct drm_gem_object
**object_list
= NULL
;
3486 struct drm_gem_object
*batch_obj
;
3487 struct drm_i915_gem_object
*obj_priv
;
3488 struct drm_clip_rect
*cliprects
= NULL
;
3489 struct drm_i915_gem_relocation_entry
*relocs
= NULL
;
3490 struct drm_i915_gem_request
*request
= NULL
;
3491 int ret
= 0, ret2
, i
, pinned
= 0;
3492 uint64_t exec_offset
;
3493 uint32_t seqno
, reloc_index
;
3494 int pin_tries
, flips
;
3496 struct intel_ring_buffer
*ring
= NULL
;
3499 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3500 (int) args
->buffers_ptr
, args
->buffer_count
, args
->batch_len
);
3502 if (args
->flags
& I915_EXEC_BSD
) {
3503 if (!HAS_BSD(dev
)) {
3504 DRM_ERROR("execbuf with wrong flag\n");
3507 ring
= &dev_priv
->bsd_ring
;
3509 ring
= &dev_priv
->render_ring
;
3512 if (args
->buffer_count
< 1) {
3513 DRM_ERROR("execbuf with %d buffers\n", args
->buffer_count
);
3516 object_list
= drm_malloc_ab(sizeof(*object_list
), args
->buffer_count
);
3517 if (object_list
== NULL
) {
3518 DRM_ERROR("Failed to allocate object list for %d buffers\n",
3519 args
->buffer_count
);
3524 if (args
->num_cliprects
!= 0) {
3525 cliprects
= kcalloc(args
->num_cliprects
, sizeof(*cliprects
),
3527 if (cliprects
== NULL
) {
3532 ret
= copy_from_user(cliprects
,
3533 (struct drm_clip_rect __user
*)
3534 (uintptr_t) args
->cliprects_ptr
,
3535 sizeof(*cliprects
) * args
->num_cliprects
);
3537 DRM_ERROR("copy %d cliprects failed: %d\n",
3538 args
->num_cliprects
, ret
);
3544 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
3545 if (request
== NULL
) {
3550 ret
= i915_gem_get_relocs_from_user(exec_list
, args
->buffer_count
,
3555 mutex_lock(&dev
->struct_mutex
);
3557 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3559 if (atomic_read(&dev_priv
->mm
.wedged
)) {
3560 mutex_unlock(&dev
->struct_mutex
);
3565 if (dev_priv
->mm
.suspended
) {
3566 mutex_unlock(&dev
->struct_mutex
);
3571 /* Look up object handles */
3573 for (i
= 0; i
< args
->buffer_count
; i
++) {
3574 object_list
[i
] = drm_gem_object_lookup(dev
, file_priv
,
3575 exec_list
[i
].handle
);
3576 if (object_list
[i
] == NULL
) {
3577 DRM_ERROR("Invalid object handle %d at index %d\n",
3578 exec_list
[i
].handle
, i
);
3579 /* prevent error path from reading uninitialized data */
3580 args
->buffer_count
= i
+ 1;
3585 obj_priv
= to_intel_bo(object_list
[i
]);
3586 if (obj_priv
->in_execbuffer
) {
3587 DRM_ERROR("Object %p appears more than once in object list\n",
3589 /* prevent error path from reading uninitialized data */
3590 args
->buffer_count
= i
+ 1;
3594 obj_priv
->in_execbuffer
= true;
3595 flips
+= atomic_read(&obj_priv
->pending_flip
);
3599 ret
= i915_gem_wait_for_pending_flip(dev
, object_list
,
3600 args
->buffer_count
);
3605 /* Pin and relocate */
3606 for (pin_tries
= 0; ; pin_tries
++) {
3610 for (i
= 0; i
< args
->buffer_count
; i
++) {
3611 object_list
[i
]->pending_read_domains
= 0;
3612 object_list
[i
]->pending_write_domain
= 0;
3613 ret
= i915_gem_object_pin_and_relocate(object_list
[i
],
3616 &relocs
[reloc_index
]);
3620 reloc_index
+= exec_list
[i
].relocation_count
;
3626 /* error other than GTT full, or we've already tried again */
3627 if (ret
!= -ENOSPC
|| pin_tries
>= 1) {
3628 if (ret
!= -ERESTARTSYS
) {
3629 unsigned long long total_size
= 0;
3631 for (i
= 0; i
< args
->buffer_count
; i
++) {
3632 obj_priv
= to_intel_bo(object_list
[i
]);
3634 total_size
+= object_list
[i
]->size
;
3636 exec_list
[i
].flags
& EXEC_OBJECT_NEEDS_FENCE
&&
3637 obj_priv
->tiling_mode
!= I915_TILING_NONE
;
3639 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
3640 pinned
+1, args
->buffer_count
,
3641 total_size
, num_fences
,
3643 DRM_ERROR("%d objects [%d pinned], "
3644 "%d object bytes [%d pinned], "
3645 "%d/%d gtt bytes\n",
3646 atomic_read(&dev
->object_count
),
3647 atomic_read(&dev
->pin_count
),
3648 atomic_read(&dev
->object_memory
),
3649 atomic_read(&dev
->pin_memory
),
3650 atomic_read(&dev
->gtt_memory
),
3656 /* unpin all of our buffers */
3657 for (i
= 0; i
< pinned
; i
++)
3658 i915_gem_object_unpin(object_list
[i
]);
3661 /* evict everyone we can from the aperture */
3662 ret
= i915_gem_evict_everything(dev
);
3663 if (ret
&& ret
!= -ENOSPC
)
3667 /* Set the pending read domains for the batch buffer to COMMAND */
3668 batch_obj
= object_list
[args
->buffer_count
-1];
3669 if (batch_obj
->pending_write_domain
) {
3670 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3674 batch_obj
->pending_read_domains
|= I915_GEM_DOMAIN_COMMAND
;
3676 /* Sanity check the batch buffer, prior to moving objects */
3677 exec_offset
= exec_list
[args
->buffer_count
- 1].offset
;
3678 ret
= i915_gem_check_execbuffer (args
, exec_offset
);
3680 DRM_ERROR("execbuf with invalid offset/length\n");
3684 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3686 /* Zero the global flush/invalidate flags. These
3687 * will be modified as new domains are computed
3690 dev
->invalidate_domains
= 0;
3691 dev
->flush_domains
= 0;
3693 for (i
= 0; i
< args
->buffer_count
; i
++) {
3694 struct drm_gem_object
*obj
= object_list
[i
];
3696 /* Compute new gpu domains and update invalidate/flush */
3697 i915_gem_object_set_to_gpu_domain(obj
);
3700 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3702 if (dev
->invalidate_domains
| dev
->flush_domains
) {
3704 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3706 dev
->invalidate_domains
,
3707 dev
->flush_domains
);
3710 dev
->invalidate_domains
,
3711 dev
->flush_domains
);
3714 if (dev_priv
->render_ring
.outstanding_lazy_request
) {
3715 (void)i915_add_request(dev
, file_priv
, NULL
, &dev_priv
->render_ring
);
3716 dev_priv
->render_ring
.outstanding_lazy_request
= false;
3718 if (dev_priv
->bsd_ring
.outstanding_lazy_request
) {
3719 (void)i915_add_request(dev
, file_priv
, NULL
, &dev_priv
->bsd_ring
);
3720 dev_priv
->bsd_ring
.outstanding_lazy_request
= false;
3723 for (i
= 0; i
< args
->buffer_count
; i
++) {
3724 struct drm_gem_object
*obj
= object_list
[i
];
3725 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
3726 uint32_t old_write_domain
= obj
->write_domain
;
3728 obj
->write_domain
= obj
->pending_write_domain
;
3729 if (obj
->write_domain
)
3730 list_move_tail(&obj_priv
->gpu_write_list
,
3731 &dev_priv
->mm
.gpu_write_list
);
3733 list_del_init(&obj_priv
->gpu_write_list
);
3735 trace_i915_gem_object_change_domain(obj
,
3740 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3743 for (i
= 0; i
< args
->buffer_count
; i
++) {
3744 i915_gem_object_check_coherency(object_list
[i
],
3745 exec_list
[i
].handle
);
3750 i915_gem_dump_object(batch_obj
,
3756 /* Exec the batchbuffer */
3757 ret
= ring
->dispatch_gem_execbuffer(dev
, ring
, args
,
3758 cliprects
, exec_offset
);
3760 DRM_ERROR("dispatch failed %d\n", ret
);
3765 * Ensure that the commands in the batch buffer are
3766 * finished before the interrupt fires
3768 i915_retire_commands(dev
, ring
);
3770 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3772 for (i
= 0; i
< args
->buffer_count
; i
++) {
3773 struct drm_gem_object
*obj
= object_list
[i
];
3774 obj_priv
= to_intel_bo(obj
);
3776 i915_gem_object_move_to_active(obj
, ring
);
3778 DRM_INFO("%s: move to exec list %p\n", __func__
, obj
);
3783 * Get a seqno representing the execution of the current buffer,
3784 * which we can wait on. We would like to mitigate these interrupts,
3785 * likely by only creating seqnos occasionally (so that we have
3786 * *some* interrupts representing completion of buffers that we can
3787 * wait on when trying to clear up gtt space).
3789 seqno
= i915_add_request(dev
, file_priv
, request
, ring
);
3793 i915_dump_lru(dev
, __func__
);
3796 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3799 for (i
= 0; i
< pinned
; i
++)
3800 i915_gem_object_unpin(object_list
[i
]);
3802 for (i
= 0; i
< args
->buffer_count
; i
++) {
3803 if (object_list
[i
]) {
3804 obj_priv
= to_intel_bo(object_list
[i
]);
3805 obj_priv
->in_execbuffer
= false;
3807 drm_gem_object_unreference(object_list
[i
]);
3810 mutex_unlock(&dev
->struct_mutex
);
3813 /* Copy the updated relocations out regardless of current error
3814 * state. Failure to update the relocs would mean that the next
3815 * time userland calls execbuf, it would do so with presumed offset
3816 * state that didn't match the actual object state.
3818 ret2
= i915_gem_put_relocs_to_user(exec_list
, args
->buffer_count
,
3821 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2
);
3827 drm_free_large(object_list
);
3835 * Legacy execbuffer just creates an exec2 list from the original exec object
3836 * list array and passes it to the real function.
3839 i915_gem_execbuffer(struct drm_device
*dev
, void *data
,
3840 struct drm_file
*file_priv
)
3842 struct drm_i915_gem_execbuffer
*args
= data
;
3843 struct drm_i915_gem_execbuffer2 exec2
;
3844 struct drm_i915_gem_exec_object
*exec_list
= NULL
;
3845 struct drm_i915_gem_exec_object2
*exec2_list
= NULL
;
3849 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3850 (int) args
->buffers_ptr
, args
->buffer_count
, args
->batch_len
);
3853 if (args
->buffer_count
< 1) {
3854 DRM_ERROR("execbuf with %d buffers\n", args
->buffer_count
);
3858 /* Copy in the exec list from userland */
3859 exec_list
= drm_malloc_ab(sizeof(*exec_list
), args
->buffer_count
);
3860 exec2_list
= drm_malloc_ab(sizeof(*exec2_list
), args
->buffer_count
);
3861 if (exec_list
== NULL
|| exec2_list
== NULL
) {
3862 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3863 args
->buffer_count
);
3864 drm_free_large(exec_list
);
3865 drm_free_large(exec2_list
);
3868 ret
= copy_from_user(exec_list
,
3869 (struct drm_i915_relocation_entry __user
*)
3870 (uintptr_t) args
->buffers_ptr
,
3871 sizeof(*exec_list
) * args
->buffer_count
);
3873 DRM_ERROR("copy %d exec entries failed %d\n",
3874 args
->buffer_count
, ret
);
3875 drm_free_large(exec_list
);
3876 drm_free_large(exec2_list
);
3880 for (i
= 0; i
< args
->buffer_count
; i
++) {
3881 exec2_list
[i
].handle
= exec_list
[i
].handle
;
3882 exec2_list
[i
].relocation_count
= exec_list
[i
].relocation_count
;
3883 exec2_list
[i
].relocs_ptr
= exec_list
[i
].relocs_ptr
;
3884 exec2_list
[i
].alignment
= exec_list
[i
].alignment
;
3885 exec2_list
[i
].offset
= exec_list
[i
].offset
;
3887 exec2_list
[i
].flags
= EXEC_OBJECT_NEEDS_FENCE
;
3889 exec2_list
[i
].flags
= 0;
3892 exec2
.buffers_ptr
= args
->buffers_ptr
;
3893 exec2
.buffer_count
= args
->buffer_count
;
3894 exec2
.batch_start_offset
= args
->batch_start_offset
;
3895 exec2
.batch_len
= args
->batch_len
;
3896 exec2
.DR1
= args
->DR1
;
3897 exec2
.DR4
= args
->DR4
;
3898 exec2
.num_cliprects
= args
->num_cliprects
;
3899 exec2
.cliprects_ptr
= args
->cliprects_ptr
;
3900 exec2
.flags
= I915_EXEC_RENDER
;
3902 ret
= i915_gem_do_execbuffer(dev
, data
, file_priv
, &exec2
, exec2_list
);
3904 /* Copy the new buffer offsets back to the user's exec list. */
3905 for (i
= 0; i
< args
->buffer_count
; i
++)
3906 exec_list
[i
].offset
= exec2_list
[i
].offset
;
3907 /* ... and back out to userspace */
3908 ret
= copy_to_user((struct drm_i915_relocation_entry __user
*)
3909 (uintptr_t) args
->buffers_ptr
,
3911 sizeof(*exec_list
) * args
->buffer_count
);
3914 DRM_ERROR("failed to copy %d exec entries "
3915 "back to user (%d)\n",
3916 args
->buffer_count
, ret
);
3920 drm_free_large(exec_list
);
3921 drm_free_large(exec2_list
);
3926 i915_gem_execbuffer2(struct drm_device
*dev
, void *data
,
3927 struct drm_file
*file_priv
)
3929 struct drm_i915_gem_execbuffer2
*args
= data
;
3930 struct drm_i915_gem_exec_object2
*exec2_list
= NULL
;
3934 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3935 (int) args
->buffers_ptr
, args
->buffer_count
, args
->batch_len
);
3938 if (args
->buffer_count
< 1) {
3939 DRM_ERROR("execbuf2 with %d buffers\n", args
->buffer_count
);
3943 exec2_list
= drm_malloc_ab(sizeof(*exec2_list
), args
->buffer_count
);
3944 if (exec2_list
== NULL
) {
3945 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3946 args
->buffer_count
);
3949 ret
= copy_from_user(exec2_list
,
3950 (struct drm_i915_relocation_entry __user
*)
3951 (uintptr_t) args
->buffers_ptr
,
3952 sizeof(*exec2_list
) * args
->buffer_count
);
3954 DRM_ERROR("copy %d exec entries failed %d\n",
3955 args
->buffer_count
, ret
);
3956 drm_free_large(exec2_list
);
3960 ret
= i915_gem_do_execbuffer(dev
, data
, file_priv
, args
, exec2_list
);
3962 /* Copy the new buffer offsets back to the user's exec list. */
3963 ret
= copy_to_user((struct drm_i915_relocation_entry __user
*)
3964 (uintptr_t) args
->buffers_ptr
,
3966 sizeof(*exec2_list
) * args
->buffer_count
);
3969 DRM_ERROR("failed to copy %d exec entries "
3970 "back to user (%d)\n",
3971 args
->buffer_count
, ret
);
3975 drm_free_large(exec2_list
);
3980 i915_gem_object_pin(struct drm_gem_object
*obj
, uint32_t alignment
)
3982 struct drm_device
*dev
= obj
->dev
;
3983 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
3986 BUG_ON(obj_priv
->pin_count
== DRM_I915_GEM_OBJECT_MAX_PIN_COUNT
);
3988 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
3990 if (obj_priv
->gtt_space
!= NULL
) {
3992 alignment
= i915_gem_get_gtt_alignment(obj
);
3993 if (obj_priv
->gtt_offset
& (alignment
- 1)) {
3994 WARN(obj_priv
->pin_count
,
3995 "bo is already pinned with incorrect alignment:"
3996 " offset=%x, req.alignment=%x\n",
3997 obj_priv
->gtt_offset
, alignment
);
3998 ret
= i915_gem_object_unbind(obj
);
4004 if (obj_priv
->gtt_space
== NULL
) {
4005 ret
= i915_gem_object_bind_to_gtt(obj
, alignment
);
4010 obj_priv
->pin_count
++;
4012 /* If the object is not active and not pending a flush,
4013 * remove it from the inactive list
4015 if (obj_priv
->pin_count
== 1) {
4016 atomic_inc(&dev
->pin_count
);
4017 atomic_add(obj
->size
, &dev
->pin_memory
);
4018 if (!obj_priv
->active
&&
4019 (obj
->write_domain
& I915_GEM_GPU_DOMAINS
) == 0)
4020 list_del_init(&obj_priv
->list
);
4022 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
4028 i915_gem_object_unpin(struct drm_gem_object
*obj
)
4030 struct drm_device
*dev
= obj
->dev
;
4031 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4032 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
4034 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
4035 obj_priv
->pin_count
--;
4036 BUG_ON(obj_priv
->pin_count
< 0);
4037 BUG_ON(obj_priv
->gtt_space
== NULL
);
4039 /* If the object is no longer pinned, and is
4040 * neither active nor being flushed, then stick it on
4043 if (obj_priv
->pin_count
== 0) {
4044 if (!obj_priv
->active
&&
4045 (obj
->write_domain
& I915_GEM_GPU_DOMAINS
) == 0)
4046 list_move_tail(&obj_priv
->list
,
4047 &dev_priv
->mm
.inactive_list
);
4048 atomic_dec(&dev
->pin_count
);
4049 atomic_sub(obj
->size
, &dev
->pin_memory
);
4051 i915_verify_inactive(dev
, __FILE__
, __LINE__
);
4055 i915_gem_pin_ioctl(struct drm_device
*dev
, void *data
,
4056 struct drm_file
*file_priv
)
4058 struct drm_i915_gem_pin
*args
= data
;
4059 struct drm_gem_object
*obj
;
4060 struct drm_i915_gem_object
*obj_priv
;
4063 mutex_lock(&dev
->struct_mutex
);
4065 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
4067 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4069 mutex_unlock(&dev
->struct_mutex
);
4072 obj_priv
= to_intel_bo(obj
);
4074 if (obj_priv
->madv
!= I915_MADV_WILLNEED
) {
4075 DRM_ERROR("Attempting to pin a purgeable buffer\n");
4076 drm_gem_object_unreference(obj
);
4077 mutex_unlock(&dev
->struct_mutex
);
4081 if (obj_priv
->pin_filp
!= NULL
&& obj_priv
->pin_filp
!= file_priv
) {
4082 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4084 drm_gem_object_unreference(obj
);
4085 mutex_unlock(&dev
->struct_mutex
);
4089 obj_priv
->user_pin_count
++;
4090 obj_priv
->pin_filp
= file_priv
;
4091 if (obj_priv
->user_pin_count
== 1) {
4092 ret
= i915_gem_object_pin(obj
, args
->alignment
);
4094 drm_gem_object_unreference(obj
);
4095 mutex_unlock(&dev
->struct_mutex
);
4100 /* XXX - flush the CPU caches for pinned objects
4101 * as the X server doesn't manage domains yet
4103 i915_gem_object_flush_cpu_write_domain(obj
);
4104 args
->offset
= obj_priv
->gtt_offset
;
4105 drm_gem_object_unreference(obj
);
4106 mutex_unlock(&dev
->struct_mutex
);
4112 i915_gem_unpin_ioctl(struct drm_device
*dev
, void *data
,
4113 struct drm_file
*file_priv
)
4115 struct drm_i915_gem_pin
*args
= data
;
4116 struct drm_gem_object
*obj
;
4117 struct drm_i915_gem_object
*obj_priv
;
4119 mutex_lock(&dev
->struct_mutex
);
4121 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
4123 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4125 mutex_unlock(&dev
->struct_mutex
);
4129 obj_priv
= to_intel_bo(obj
);
4130 if (obj_priv
->pin_filp
!= file_priv
) {
4131 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4133 drm_gem_object_unreference(obj
);
4134 mutex_unlock(&dev
->struct_mutex
);
4137 obj_priv
->user_pin_count
--;
4138 if (obj_priv
->user_pin_count
== 0) {
4139 obj_priv
->pin_filp
= NULL
;
4140 i915_gem_object_unpin(obj
);
4143 drm_gem_object_unreference(obj
);
4144 mutex_unlock(&dev
->struct_mutex
);
4149 i915_gem_busy_ioctl(struct drm_device
*dev
, void *data
,
4150 struct drm_file
*file_priv
)
4152 struct drm_i915_gem_busy
*args
= data
;
4153 struct drm_gem_object
*obj
;
4154 struct drm_i915_gem_object
*obj_priv
;
4156 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
4158 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4163 mutex_lock(&dev
->struct_mutex
);
4165 /* Count all active objects as busy, even if they are currently not used
4166 * by the gpu. Users of this interface expect objects to eventually
4167 * become non-busy without any further actions, therefore emit any
4168 * necessary flushes here.
4170 obj_priv
= to_intel_bo(obj
);
4171 args
->busy
= obj_priv
->active
;
4173 /* Unconditionally flush objects, even when the gpu still uses this
4174 * object. Userspace calling this function indicates that it wants to
4175 * use this buffer rather sooner than later, so issuing the required
4176 * flush earlier is beneficial.
4178 if (obj
->write_domain
) {
4179 i915_gem_flush(dev
, 0, obj
->write_domain
);
4180 (void)i915_add_request(dev
, file_priv
, NULL
, obj_priv
->ring
);
4183 /* Update the active list for the hardware's current position.
4184 * Otherwise this only updates on a delayed timer or when irqs
4185 * are actually unmasked, and our working set ends up being
4186 * larger than required.
4188 i915_gem_retire_requests_ring(dev
, obj_priv
->ring
);
4190 args
->busy
= obj_priv
->active
;
4193 drm_gem_object_unreference(obj
);
4194 mutex_unlock(&dev
->struct_mutex
);
4199 i915_gem_throttle_ioctl(struct drm_device
*dev
, void *data
,
4200 struct drm_file
*file_priv
)
4202 return i915_gem_ring_throttle(dev
, file_priv
);
4206 i915_gem_madvise_ioctl(struct drm_device
*dev
, void *data
,
4207 struct drm_file
*file_priv
)
4209 struct drm_i915_gem_madvise
*args
= data
;
4210 struct drm_gem_object
*obj
;
4211 struct drm_i915_gem_object
*obj_priv
;
4213 switch (args
->madv
) {
4214 case I915_MADV_DONTNEED
:
4215 case I915_MADV_WILLNEED
:
4221 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
4223 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4228 mutex_lock(&dev
->struct_mutex
);
4229 obj_priv
= to_intel_bo(obj
);
4231 if (obj_priv
->pin_count
) {
4232 drm_gem_object_unreference(obj
);
4233 mutex_unlock(&dev
->struct_mutex
);
4235 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4239 if (obj_priv
->madv
!= __I915_MADV_PURGED
)
4240 obj_priv
->madv
= args
->madv
;
4242 /* if the object is no longer bound, discard its backing storage */
4243 if (i915_gem_object_is_purgeable(obj_priv
) &&
4244 obj_priv
->gtt_space
== NULL
)
4245 i915_gem_object_truncate(obj
);
4247 args
->retained
= obj_priv
->madv
!= __I915_MADV_PURGED
;
4249 drm_gem_object_unreference(obj
);
4250 mutex_unlock(&dev
->struct_mutex
);
4255 struct drm_gem_object
* i915_gem_alloc_object(struct drm_device
*dev
,
4258 struct drm_i915_gem_object
*obj
;
4260 obj
= kzalloc(sizeof(*obj
), GFP_KERNEL
);
4264 if (drm_gem_object_init(dev
, &obj
->base
, size
) != 0) {
4269 obj
->base
.write_domain
= I915_GEM_DOMAIN_CPU
;
4270 obj
->base
.read_domains
= I915_GEM_DOMAIN_CPU
;
4272 obj
->agp_type
= AGP_USER_MEMORY
;
4273 obj
->base
.driver_private
= NULL
;
4274 obj
->fence_reg
= I915_FENCE_REG_NONE
;
4275 INIT_LIST_HEAD(&obj
->list
);
4276 INIT_LIST_HEAD(&obj
->gpu_write_list
);
4277 obj
->madv
= I915_MADV_WILLNEED
;
4279 trace_i915_gem_object_create(&obj
->base
);
4284 int i915_gem_init_object(struct drm_gem_object
*obj
)
4291 static void i915_gem_free_object_tail(struct drm_gem_object
*obj
)
4293 struct drm_device
*dev
= obj
->dev
;
4294 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4295 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
4298 ret
= i915_gem_object_unbind(obj
);
4299 if (ret
== -ERESTARTSYS
) {
4300 list_move(&obj_priv
->list
,
4301 &dev_priv
->mm
.deferred_free_list
);
4305 if (obj_priv
->mmap_offset
)
4306 i915_gem_free_mmap_offset(obj
);
4308 drm_gem_object_release(obj
);
4310 kfree(obj_priv
->page_cpu_valid
);
4311 kfree(obj_priv
->bit_17
);
4315 void i915_gem_free_object(struct drm_gem_object
*obj
)
4317 struct drm_device
*dev
= obj
->dev
;
4318 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
4320 trace_i915_gem_object_destroy(obj
);
4322 while (obj_priv
->pin_count
> 0)
4323 i915_gem_object_unpin(obj
);
4325 if (obj_priv
->phys_obj
)
4326 i915_gem_detach_phys_object(dev
, obj
);
4328 i915_gem_free_object_tail(obj
);
4332 i915_gem_idle(struct drm_device
*dev
)
4334 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4337 mutex_lock(&dev
->struct_mutex
);
4339 if (dev_priv
->mm
.suspended
||
4340 (dev_priv
->render_ring
.gem_object
== NULL
) ||
4342 dev_priv
->bsd_ring
.gem_object
== NULL
)) {
4343 mutex_unlock(&dev
->struct_mutex
);
4347 ret
= i915_gpu_idle(dev
);
4349 mutex_unlock(&dev
->struct_mutex
);
4353 /* Under UMS, be paranoid and evict. */
4354 if (!drm_core_check_feature(dev
, DRIVER_MODESET
)) {
4355 ret
= i915_gem_evict_inactive(dev
);
4357 mutex_unlock(&dev
->struct_mutex
);
4362 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4363 * We need to replace this with a semaphore, or something.
4364 * And not confound mm.suspended!
4366 dev_priv
->mm
.suspended
= 1;
4367 del_timer_sync(&dev_priv
->hangcheck_timer
);
4369 i915_kernel_lost_context(dev
);
4370 i915_gem_cleanup_ringbuffer(dev
);
4372 mutex_unlock(&dev
->struct_mutex
);
4374 /* Cancel the retire work handler, which should be idle now. */
4375 cancel_delayed_work_sync(&dev_priv
->mm
.retire_work
);
4381 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4382 * over cache flushing.
4385 i915_gem_init_pipe_control(struct drm_device
*dev
)
4387 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4388 struct drm_gem_object
*obj
;
4389 struct drm_i915_gem_object
*obj_priv
;
4392 obj
= i915_gem_alloc_object(dev
, 4096);
4394 DRM_ERROR("Failed to allocate seqno page\n");
4398 obj_priv
= to_intel_bo(obj
);
4399 obj_priv
->agp_type
= AGP_USER_CACHED_MEMORY
;
4401 ret
= i915_gem_object_pin(obj
, 4096);
4405 dev_priv
->seqno_gfx_addr
= obj_priv
->gtt_offset
;
4406 dev_priv
->seqno_page
= kmap(obj_priv
->pages
[0]);
4407 if (dev_priv
->seqno_page
== NULL
)
4410 dev_priv
->seqno_obj
= obj
;
4411 memset(dev_priv
->seqno_page
, 0, PAGE_SIZE
);
4416 i915_gem_object_unpin(obj
);
4418 drm_gem_object_unreference(obj
);
4425 i915_gem_cleanup_pipe_control(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
;
4431 obj
= dev_priv
->seqno_obj
;
4432 obj_priv
= to_intel_bo(obj
);
4433 kunmap(obj_priv
->pages
[0]);
4434 i915_gem_object_unpin(obj
);
4435 drm_gem_object_unreference(obj
);
4436 dev_priv
->seqno_obj
= NULL
;
4438 dev_priv
->seqno_page
= NULL
;
4442 i915_gem_init_ringbuffer(struct drm_device
*dev
)
4444 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4447 dev_priv
->render_ring
= render_ring
;
4449 if (!I915_NEED_GFX_HWS(dev
)) {
4450 dev_priv
->render_ring
.status_page
.page_addr
4451 = dev_priv
->status_page_dmah
->vaddr
;
4452 memset(dev_priv
->render_ring
.status_page
.page_addr
,
4456 if (HAS_PIPE_CONTROL(dev
)) {
4457 ret
= i915_gem_init_pipe_control(dev
);
4462 ret
= intel_init_ring_buffer(dev
, &dev_priv
->render_ring
);
4464 goto cleanup_pipe_control
;
4467 dev_priv
->bsd_ring
= bsd_ring
;
4468 ret
= intel_init_ring_buffer(dev
, &dev_priv
->bsd_ring
);
4470 goto cleanup_render_ring
;
4473 dev_priv
->next_seqno
= 1;
4477 cleanup_render_ring
:
4478 intel_cleanup_ring_buffer(dev
, &dev_priv
->render_ring
);
4479 cleanup_pipe_control
:
4480 if (HAS_PIPE_CONTROL(dev
))
4481 i915_gem_cleanup_pipe_control(dev
);
4486 i915_gem_cleanup_ringbuffer(struct drm_device
*dev
)
4488 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4490 intel_cleanup_ring_buffer(dev
, &dev_priv
->render_ring
);
4492 intel_cleanup_ring_buffer(dev
, &dev_priv
->bsd_ring
);
4493 if (HAS_PIPE_CONTROL(dev
))
4494 i915_gem_cleanup_pipe_control(dev
);
4498 i915_gem_entervt_ioctl(struct drm_device
*dev
, void *data
,
4499 struct drm_file
*file_priv
)
4501 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4504 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
4507 if (atomic_read(&dev_priv
->mm
.wedged
)) {
4508 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4509 atomic_set(&dev_priv
->mm
.wedged
, 0);
4512 mutex_lock(&dev
->struct_mutex
);
4513 dev_priv
->mm
.suspended
= 0;
4515 ret
= i915_gem_init_ringbuffer(dev
);
4517 mutex_unlock(&dev
->struct_mutex
);
4521 BUG_ON(!list_empty(&dev_priv
->render_ring
.active_list
));
4522 BUG_ON(HAS_BSD(dev
) && !list_empty(&dev_priv
->bsd_ring
.active_list
));
4523 BUG_ON(!list_empty(&dev_priv
->mm
.flushing_list
));
4524 BUG_ON(!list_empty(&dev_priv
->mm
.inactive_list
));
4525 BUG_ON(!list_empty(&dev_priv
->render_ring
.request_list
));
4526 BUG_ON(HAS_BSD(dev
) && !list_empty(&dev_priv
->bsd_ring
.request_list
));
4527 mutex_unlock(&dev
->struct_mutex
);
4529 ret
= drm_irq_install(dev
);
4531 goto cleanup_ringbuffer
;
4536 mutex_lock(&dev
->struct_mutex
);
4537 i915_gem_cleanup_ringbuffer(dev
);
4538 dev_priv
->mm
.suspended
= 1;
4539 mutex_unlock(&dev
->struct_mutex
);
4545 i915_gem_leavevt_ioctl(struct drm_device
*dev
, void *data
,
4546 struct drm_file
*file_priv
)
4548 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
4551 drm_irq_uninstall(dev
);
4552 return i915_gem_idle(dev
);
4556 i915_gem_lastclose(struct drm_device
*dev
)
4560 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
4563 ret
= i915_gem_idle(dev
);
4565 DRM_ERROR("failed to idle hardware: %d\n", ret
);
4569 i915_gem_load(struct drm_device
*dev
)
4572 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4574 INIT_LIST_HEAD(&dev_priv
->mm
.flushing_list
);
4575 INIT_LIST_HEAD(&dev_priv
->mm
.gpu_write_list
);
4576 INIT_LIST_HEAD(&dev_priv
->mm
.inactive_list
);
4577 INIT_LIST_HEAD(&dev_priv
->mm
.fence_list
);
4578 INIT_LIST_HEAD(&dev_priv
->mm
.deferred_free_list
);
4579 INIT_LIST_HEAD(&dev_priv
->render_ring
.active_list
);
4580 INIT_LIST_HEAD(&dev_priv
->render_ring
.request_list
);
4582 INIT_LIST_HEAD(&dev_priv
->bsd_ring
.active_list
);
4583 INIT_LIST_HEAD(&dev_priv
->bsd_ring
.request_list
);
4585 for (i
= 0; i
< 16; i
++)
4586 INIT_LIST_HEAD(&dev_priv
->fence_regs
[i
].lru_list
);
4587 INIT_DELAYED_WORK(&dev_priv
->mm
.retire_work
,
4588 i915_gem_retire_work_handler
);
4589 spin_lock(&shrink_list_lock
);
4590 list_add(&dev_priv
->mm
.shrink_list
, &shrink_list
);
4591 spin_unlock(&shrink_list_lock
);
4593 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4595 u32 tmp
= I915_READ(MI_ARB_STATE
);
4596 if (!(tmp
& MI_ARB_C3_LP_WRITE_ENABLE
)) {
4597 /* arb state is a masked write, so set bit + bit in mask */
4598 tmp
= MI_ARB_C3_LP_WRITE_ENABLE
| (MI_ARB_C3_LP_WRITE_ENABLE
<< MI_ARB_MASK_SHIFT
);
4599 I915_WRITE(MI_ARB_STATE
, tmp
);
4603 /* Old X drivers will take 0-2 for front, back, depth buffers */
4604 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4605 dev_priv
->fence_reg_start
= 3;
4607 if (IS_I965G(dev
) || IS_I945G(dev
) || IS_I945GM(dev
) || IS_G33(dev
))
4608 dev_priv
->num_fence_regs
= 16;
4610 dev_priv
->num_fence_regs
= 8;
4612 /* Initialize fence registers to zero */
4613 if (IS_I965G(dev
)) {
4614 for (i
= 0; i
< 16; i
++)
4615 I915_WRITE64(FENCE_REG_965_0
+ (i
* 8), 0);
4617 for (i
= 0; i
< 8; i
++)
4618 I915_WRITE(FENCE_REG_830_0
+ (i
* 4), 0);
4619 if (IS_I945G(dev
) || IS_I945GM(dev
) || IS_G33(dev
))
4620 for (i
= 0; i
< 8; i
++)
4621 I915_WRITE(FENCE_REG_945_8
+ (i
* 4), 0);
4623 i915_gem_detect_bit_6_swizzle(dev
);
4624 init_waitqueue_head(&dev_priv
->pending_flip_queue
);
4628 * Create a physically contiguous memory object for this object
4629 * e.g. for cursor + overlay regs
4631 static int i915_gem_init_phys_object(struct drm_device
*dev
,
4632 int id
, int size
, int align
)
4634 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4635 struct drm_i915_gem_phys_object
*phys_obj
;
4638 if (dev_priv
->mm
.phys_objs
[id
- 1] || !size
)
4641 phys_obj
= kzalloc(sizeof(struct drm_i915_gem_phys_object
), GFP_KERNEL
);
4647 phys_obj
->handle
= drm_pci_alloc(dev
, size
, align
);
4648 if (!phys_obj
->handle
) {
4653 set_memory_wc((unsigned long)phys_obj
->handle
->vaddr
, phys_obj
->handle
->size
/ PAGE_SIZE
);
4656 dev_priv
->mm
.phys_objs
[id
- 1] = phys_obj
;
4664 static void i915_gem_free_phys_object(struct drm_device
*dev
, int id
)
4666 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4667 struct drm_i915_gem_phys_object
*phys_obj
;
4669 if (!dev_priv
->mm
.phys_objs
[id
- 1])
4672 phys_obj
= dev_priv
->mm
.phys_objs
[id
- 1];
4673 if (phys_obj
->cur_obj
) {
4674 i915_gem_detach_phys_object(dev
, phys_obj
->cur_obj
);
4678 set_memory_wb((unsigned long)phys_obj
->handle
->vaddr
, phys_obj
->handle
->size
/ PAGE_SIZE
);
4680 drm_pci_free(dev
, phys_obj
->handle
);
4682 dev_priv
->mm
.phys_objs
[id
- 1] = NULL
;
4685 void i915_gem_free_all_phys_object(struct drm_device
*dev
)
4689 for (i
= I915_GEM_PHYS_CURSOR_0
; i
<= I915_MAX_PHYS_OBJECT
; i
++)
4690 i915_gem_free_phys_object(dev
, i
);
4693 void i915_gem_detach_phys_object(struct drm_device
*dev
,
4694 struct drm_gem_object
*obj
)
4696 struct drm_i915_gem_object
*obj_priv
;
4701 obj_priv
= to_intel_bo(obj
);
4702 if (!obj_priv
->phys_obj
)
4705 ret
= i915_gem_object_get_pages(obj
, 0);
4709 page_count
= obj
->size
/ PAGE_SIZE
;
4711 for (i
= 0; i
< page_count
; i
++) {
4712 char *dst
= kmap_atomic(obj_priv
->pages
[i
], KM_USER0
);
4713 char *src
= obj_priv
->phys_obj
->handle
->vaddr
+ (i
* PAGE_SIZE
);
4715 memcpy(dst
, src
, PAGE_SIZE
);
4716 kunmap_atomic(dst
, KM_USER0
);
4718 drm_clflush_pages(obj_priv
->pages
, page_count
);
4719 drm_agp_chipset_flush(dev
);
4721 i915_gem_object_put_pages(obj
);
4723 obj_priv
->phys_obj
->cur_obj
= NULL
;
4724 obj_priv
->phys_obj
= NULL
;
4728 i915_gem_attach_phys_object(struct drm_device
*dev
,
4729 struct drm_gem_object
*obj
,
4733 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4734 struct drm_i915_gem_object
*obj_priv
;
4739 if (id
> I915_MAX_PHYS_OBJECT
)
4742 obj_priv
= to_intel_bo(obj
);
4744 if (obj_priv
->phys_obj
) {
4745 if (obj_priv
->phys_obj
->id
== id
)
4747 i915_gem_detach_phys_object(dev
, obj
);
4750 /* create a new object */
4751 if (!dev_priv
->mm
.phys_objs
[id
- 1]) {
4752 ret
= i915_gem_init_phys_object(dev
, id
,
4755 DRM_ERROR("failed to init phys object %d size: %zu\n", id
, obj
->size
);
4760 /* bind to the object */
4761 obj_priv
->phys_obj
= dev_priv
->mm
.phys_objs
[id
- 1];
4762 obj_priv
->phys_obj
->cur_obj
= obj
;
4764 ret
= i915_gem_object_get_pages(obj
, 0);
4766 DRM_ERROR("failed to get page list\n");
4770 page_count
= obj
->size
/ PAGE_SIZE
;
4772 for (i
= 0; i
< page_count
; i
++) {
4773 char *src
= kmap_atomic(obj_priv
->pages
[i
], KM_USER0
);
4774 char *dst
= obj_priv
->phys_obj
->handle
->vaddr
+ (i
* PAGE_SIZE
);
4776 memcpy(dst
, src
, PAGE_SIZE
);
4777 kunmap_atomic(src
, KM_USER0
);
4780 i915_gem_object_put_pages(obj
);
4788 i915_gem_phys_pwrite(struct drm_device
*dev
, struct drm_gem_object
*obj
,
4789 struct drm_i915_gem_pwrite
*args
,
4790 struct drm_file
*file_priv
)
4792 struct drm_i915_gem_object
*obj_priv
= to_intel_bo(obj
);
4795 char __user
*user_data
;
4797 user_data
= (char __user
*) (uintptr_t) args
->data_ptr
;
4798 obj_addr
= obj_priv
->phys_obj
->handle
->vaddr
+ args
->offset
;
4800 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr
, args
->size
);
4801 ret
= copy_from_user(obj_addr
, user_data
, args
->size
);
4805 drm_agp_chipset_flush(dev
);
4809 void i915_gem_release(struct drm_device
* dev
, struct drm_file
*file_priv
)
4811 struct drm_i915_file_private
*i915_file_priv
= file_priv
->driver_priv
;
4813 /* Clean up our request list when the client is going away, so that
4814 * later retire_requests won't dereference our soon-to-be-gone
4817 mutex_lock(&dev
->struct_mutex
);
4818 while (!list_empty(&i915_file_priv
->mm
.request_list
))
4819 list_del_init(i915_file_priv
->mm
.request_list
.next
);
4820 mutex_unlock(&dev
->struct_mutex
);
4824 i915_gpu_is_active(struct drm_device
*dev
)
4826 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
4829 lists_empty
= list_empty(&dev_priv
->mm
.flushing_list
) &&
4830 list_empty(&dev_priv
->render_ring
.active_list
);
4832 lists_empty
&= list_empty(&dev_priv
->bsd_ring
.active_list
);
4834 return !lists_empty
;
4838 i915_gem_shrink(struct shrinker
*shrink
, int nr_to_scan
, gfp_t gfp_mask
)
4840 drm_i915_private_t
*dev_priv
, *next_dev
;
4841 struct drm_i915_gem_object
*obj_priv
, *next_obj
;
4843 int would_deadlock
= 1;
4845 /* "fast-path" to count number of available objects */
4846 if (nr_to_scan
== 0) {
4847 spin_lock(&shrink_list_lock
);
4848 list_for_each_entry(dev_priv
, &shrink_list
, mm
.shrink_list
) {
4849 struct drm_device
*dev
= dev_priv
->dev
;
4851 if (mutex_trylock(&dev
->struct_mutex
)) {
4852 list_for_each_entry(obj_priv
,
4853 &dev_priv
->mm
.inactive_list
,
4856 mutex_unlock(&dev
->struct_mutex
);
4859 spin_unlock(&shrink_list_lock
);
4861 return (cnt
/ 100) * sysctl_vfs_cache_pressure
;
4864 spin_lock(&shrink_list_lock
);
4867 /* first scan for clean buffers */
4868 list_for_each_entry_safe(dev_priv
, next_dev
,
4869 &shrink_list
, mm
.shrink_list
) {
4870 struct drm_device
*dev
= dev_priv
->dev
;
4872 if (! mutex_trylock(&dev
->struct_mutex
))
4875 spin_unlock(&shrink_list_lock
);
4876 i915_gem_retire_requests(dev
);
4878 list_for_each_entry_safe(obj_priv
, next_obj
,
4879 &dev_priv
->mm
.inactive_list
,
4881 if (i915_gem_object_is_purgeable(obj_priv
)) {
4882 i915_gem_object_unbind(&obj_priv
->base
);
4883 if (--nr_to_scan
<= 0)
4888 spin_lock(&shrink_list_lock
);
4889 mutex_unlock(&dev
->struct_mutex
);
4893 if (nr_to_scan
<= 0)
4897 /* second pass, evict/count anything still on the inactive list */
4898 list_for_each_entry_safe(dev_priv
, next_dev
,
4899 &shrink_list
, mm
.shrink_list
) {
4900 struct drm_device
*dev
= dev_priv
->dev
;
4902 if (! mutex_trylock(&dev
->struct_mutex
))
4905 spin_unlock(&shrink_list_lock
);
4907 list_for_each_entry_safe(obj_priv
, next_obj
,
4908 &dev_priv
->mm
.inactive_list
,
4910 if (nr_to_scan
> 0) {
4911 i915_gem_object_unbind(&obj_priv
->base
);
4917 spin_lock(&shrink_list_lock
);
4918 mutex_unlock(&dev
->struct_mutex
);
4927 * We are desperate for pages, so as a last resort, wait
4928 * for the GPU to finish and discard whatever we can.
4929 * This has a dramatic impact to reduce the number of
4930 * OOM-killer events whilst running the GPU aggressively.
4932 list_for_each_entry(dev_priv
, &shrink_list
, mm
.shrink_list
) {
4933 struct drm_device
*dev
= dev_priv
->dev
;
4935 if (!mutex_trylock(&dev
->struct_mutex
))
4938 spin_unlock(&shrink_list_lock
);
4940 if (i915_gpu_is_active(dev
)) {
4945 spin_lock(&shrink_list_lock
);
4946 mutex_unlock(&dev
->struct_mutex
);
4953 spin_unlock(&shrink_list_lock
);
4958 return (cnt
/ 100) * sysctl_vfs_cache_pressure
;
4963 static struct shrinker shrinker
= {
4964 .shrink
= i915_gem_shrink
,
4965 .seeks
= DEFAULT_SEEKS
,
4969 i915_gem_shrinker_init(void)
4971 register_shrinker(&shrinker
);
4975 i915_gem_shrinker_exit(void)
4977 unregister_shrinker(&shrinker
);