initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / drivers / gpu / drm / i915 / i915_gem.c
blob27a3074f216c01987dc8f61f47301ae61df84a9c
1 /*
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
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
28 #include "drmP.h"
29 #include "drm.h"
30 #include "i915_drm.h"
31 #include "i915_drv.h"
32 #include "i915_trace.h"
33 #include "intel_drv.h"
34 #include <linux/swap.h>
35 #include <linux/pci.h>
37 #define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
39 static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
40 static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
41 static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
42 static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
43 int write);
44 static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
45 uint64_t offset,
46 uint64_t size);
47 static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
48 static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
49 static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
50 unsigned alignment);
51 static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
52 static int i915_gem_evict_something(struct drm_device *dev, int min_size);
53 static int i915_gem_evict_from_inactive_list(struct drm_device *dev);
54 static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
55 struct drm_i915_gem_pwrite *args,
56 struct drm_file *file_priv);
58 static LIST_HEAD(shrink_list);
59 static DEFINE_SPINLOCK(shrink_list_lock);
61 int i915_gem_do_init(struct drm_device *dev, unsigned long start,
62 unsigned long end)
64 drm_i915_private_t *dev_priv = dev->dev_private;
66 if (start >= end ||
67 (start & (PAGE_SIZE - 1)) != 0 ||
68 (end & (PAGE_SIZE - 1)) != 0) {
69 return -EINVAL;
72 drm_mm_init(&dev_priv->mm.gtt_space, start,
73 end - start);
75 dev->gtt_total = (uint32_t) (end - start);
77 return 0;
80 int
81 i915_gem_init_ioctl(struct drm_device *dev, void *data,
82 struct drm_file *file_priv)
84 struct drm_i915_gem_init *args = data;
85 int ret;
87 mutex_lock(&dev->struct_mutex);
88 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
89 mutex_unlock(&dev->struct_mutex);
91 return ret;
94 int
95 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
96 struct drm_file *file_priv)
98 struct drm_i915_gem_get_aperture *args = data;
100 if (!(dev->driver->driver_features & DRIVER_GEM))
101 return -ENODEV;
103 args->aper_size = dev->gtt_total;
104 args->aper_available_size = (args->aper_size -
105 atomic_read(&dev->pin_memory));
107 return 0;
112 * Creates a new mm object and returns a handle to it.
115 i915_gem_create_ioctl(struct drm_device *dev, void *data,
116 struct drm_file *file_priv)
118 struct drm_i915_gem_create *args = data;
119 struct drm_gem_object *obj;
120 int ret;
121 u32 handle;
123 args->size = roundup(args->size, PAGE_SIZE);
125 /* Allocate the new object */
126 obj = drm_gem_object_alloc(dev, args->size);
127 if (obj == NULL)
128 return -ENOMEM;
130 ret = drm_gem_handle_create(file_priv, obj, &handle);
131 mutex_lock(&dev->struct_mutex);
132 drm_gem_object_handle_unreference(obj);
133 mutex_unlock(&dev->struct_mutex);
135 if (ret)
136 return ret;
138 args->handle = handle;
140 return 0;
143 static inline int
144 fast_shmem_read(struct page **pages,
145 loff_t page_base, int page_offset,
146 char __user *data,
147 int length)
149 char __iomem *vaddr;
150 int unwritten;
152 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
153 if (vaddr == NULL)
154 return -ENOMEM;
155 unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
156 kunmap_atomic(vaddr, KM_USER0);
158 if (unwritten)
159 return -EFAULT;
161 return 0;
164 static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
166 drm_i915_private_t *dev_priv = obj->dev->dev_private;
167 struct drm_i915_gem_object *obj_priv = obj->driver_private;
169 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
170 obj_priv->tiling_mode != I915_TILING_NONE;
173 static inline int
174 slow_shmem_copy(struct page *dst_page,
175 int dst_offset,
176 struct page *src_page,
177 int src_offset,
178 int length)
180 char *dst_vaddr, *src_vaddr;
182 dst_vaddr = kmap_atomic(dst_page, KM_USER0);
183 if (dst_vaddr == NULL)
184 return -ENOMEM;
186 src_vaddr = kmap_atomic(src_page, KM_USER1);
187 if (src_vaddr == NULL) {
188 kunmap_atomic(dst_vaddr, KM_USER0);
189 return -ENOMEM;
192 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
194 kunmap_atomic(src_vaddr, KM_USER1);
195 kunmap_atomic(dst_vaddr, KM_USER0);
197 return 0;
200 static inline int
201 slow_shmem_bit17_copy(struct page *gpu_page,
202 int gpu_offset,
203 struct page *cpu_page,
204 int cpu_offset,
205 int length,
206 int is_read)
208 char *gpu_vaddr, *cpu_vaddr;
210 /* Use the unswizzled path if this page isn't affected. */
211 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
212 if (is_read)
213 return slow_shmem_copy(cpu_page, cpu_offset,
214 gpu_page, gpu_offset, length);
215 else
216 return slow_shmem_copy(gpu_page, gpu_offset,
217 cpu_page, cpu_offset, length);
220 gpu_vaddr = kmap_atomic(gpu_page, KM_USER0);
221 if (gpu_vaddr == NULL)
222 return -ENOMEM;
224 cpu_vaddr = kmap_atomic(cpu_page, KM_USER1);
225 if (cpu_vaddr == NULL) {
226 kunmap_atomic(gpu_vaddr, KM_USER0);
227 return -ENOMEM;
230 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
231 * XORing with the other bits (A9 for Y, A9 and A10 for X)
233 while (length > 0) {
234 int cacheline_end = ALIGN(gpu_offset + 1, 64);
235 int this_length = min(cacheline_end - gpu_offset, length);
236 int swizzled_gpu_offset = gpu_offset ^ 64;
238 if (is_read) {
239 memcpy(cpu_vaddr + cpu_offset,
240 gpu_vaddr + swizzled_gpu_offset,
241 this_length);
242 } else {
243 memcpy(gpu_vaddr + swizzled_gpu_offset,
244 cpu_vaddr + cpu_offset,
245 this_length);
247 cpu_offset += this_length;
248 gpu_offset += this_length;
249 length -= this_length;
252 kunmap_atomic(cpu_vaddr, KM_USER1);
253 kunmap_atomic(gpu_vaddr, KM_USER0);
255 return 0;
259 * This is the fast shmem pread path, which attempts to copy_from_user directly
260 * from the backing pages of the object to the user's address space. On a
261 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
263 static int
264 i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
265 struct drm_i915_gem_pread *args,
266 struct drm_file *file_priv)
268 struct drm_i915_gem_object *obj_priv = obj->driver_private;
269 ssize_t remain;
270 loff_t offset, page_base;
271 char __user *user_data;
272 int page_offset, page_length;
273 int ret;
275 user_data = (char __user *) (uintptr_t) args->data_ptr;
276 remain = args->size;
278 mutex_lock(&dev->struct_mutex);
280 ret = i915_gem_object_get_pages(obj, 0);
281 if (ret != 0)
282 goto fail_unlock;
284 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
285 args->size);
286 if (ret != 0)
287 goto fail_put_pages;
289 obj_priv = obj->driver_private;
290 offset = args->offset;
292 while (remain > 0) {
293 /* Operation in this page
295 * page_base = page offset within aperture
296 * page_offset = offset within page
297 * page_length = bytes to copy for this page
299 page_base = (offset & ~(PAGE_SIZE-1));
300 page_offset = offset & (PAGE_SIZE-1);
301 page_length = remain;
302 if ((page_offset + remain) > PAGE_SIZE)
303 page_length = PAGE_SIZE - page_offset;
305 ret = fast_shmem_read(obj_priv->pages,
306 page_base, page_offset,
307 user_data, page_length);
308 if (ret)
309 goto fail_put_pages;
311 remain -= page_length;
312 user_data += page_length;
313 offset += page_length;
316 fail_put_pages:
317 i915_gem_object_put_pages(obj);
318 fail_unlock:
319 mutex_unlock(&dev->struct_mutex);
321 return ret;
324 static int
325 i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
327 int ret;
329 ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
331 /* If we've insufficient memory to map in the pages, attempt
332 * to make some space by throwing out some old buffers.
334 if (ret == -ENOMEM) {
335 struct drm_device *dev = obj->dev;
337 ret = i915_gem_evict_something(dev, obj->size);
338 if (ret)
339 return ret;
341 ret = i915_gem_object_get_pages(obj, 0);
344 return ret;
348 * This is the fallback shmem pread path, which allocates temporary storage
349 * in kernel space to copy_to_user into outside of the struct_mutex, so we
350 * can copy out of the object's backing pages while holding the struct mutex
351 * and not take page faults.
353 static int
354 i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
355 struct drm_i915_gem_pread *args,
356 struct drm_file *file_priv)
358 struct drm_i915_gem_object *obj_priv = obj->driver_private;
359 struct mm_struct *mm = current->mm;
360 struct page **user_pages;
361 ssize_t remain;
362 loff_t offset, pinned_pages, i;
363 loff_t first_data_page, last_data_page, num_pages;
364 int shmem_page_index, shmem_page_offset;
365 int data_page_index, data_page_offset;
366 int page_length;
367 int ret;
368 uint64_t data_ptr = args->data_ptr;
369 int do_bit17_swizzling;
371 remain = args->size;
373 /* Pin the user pages containing the data. We can't fault while
374 * holding the struct mutex, yet we want to hold it while
375 * dereferencing the user data.
377 first_data_page = data_ptr / PAGE_SIZE;
378 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
379 num_pages = last_data_page - first_data_page + 1;
381 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
382 if (user_pages == NULL)
383 return -ENOMEM;
385 down_read(&mm->mmap_sem);
386 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
387 num_pages, 1, 0, user_pages, NULL);
388 up_read(&mm->mmap_sem);
389 if (pinned_pages < num_pages) {
390 ret = -EFAULT;
391 goto fail_put_user_pages;
394 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
396 mutex_lock(&dev->struct_mutex);
398 ret = i915_gem_object_get_pages_or_evict(obj);
399 if (ret)
400 goto fail_unlock;
402 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
403 args->size);
404 if (ret != 0)
405 goto fail_put_pages;
407 obj_priv = obj->driver_private;
408 offset = args->offset;
410 while (remain > 0) {
411 /* Operation in this page
413 * shmem_page_index = page number within shmem file
414 * shmem_page_offset = offset within page in shmem file
415 * data_page_index = page number in get_user_pages return
416 * data_page_offset = offset with data_page_index page.
417 * page_length = bytes to copy for this page
419 shmem_page_index = offset / PAGE_SIZE;
420 shmem_page_offset = offset & ~PAGE_MASK;
421 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
422 data_page_offset = data_ptr & ~PAGE_MASK;
424 page_length = remain;
425 if ((shmem_page_offset + page_length) > PAGE_SIZE)
426 page_length = PAGE_SIZE - shmem_page_offset;
427 if ((data_page_offset + page_length) > PAGE_SIZE)
428 page_length = PAGE_SIZE - data_page_offset;
430 if (do_bit17_swizzling) {
431 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
432 shmem_page_offset,
433 user_pages[data_page_index],
434 data_page_offset,
435 page_length,
437 } else {
438 ret = slow_shmem_copy(user_pages[data_page_index],
439 data_page_offset,
440 obj_priv->pages[shmem_page_index],
441 shmem_page_offset,
442 page_length);
444 if (ret)
445 goto fail_put_pages;
447 remain -= page_length;
448 data_ptr += page_length;
449 offset += page_length;
452 fail_put_pages:
453 i915_gem_object_put_pages(obj);
454 fail_unlock:
455 mutex_unlock(&dev->struct_mutex);
456 fail_put_user_pages:
457 for (i = 0; i < pinned_pages; i++) {
458 SetPageDirty(user_pages[i]);
459 page_cache_release(user_pages[i]);
461 drm_free_large(user_pages);
463 return ret;
467 * Reads data from the object referenced by handle.
469 * On error, the contents of *data are undefined.
472 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
473 struct drm_file *file_priv)
475 struct drm_i915_gem_pread *args = data;
476 struct drm_gem_object *obj;
477 struct drm_i915_gem_object *obj_priv;
478 int ret;
480 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
481 if (obj == NULL)
482 return -EBADF;
483 obj_priv = obj->driver_private;
485 /* Bounds check source. */
486 if (args->offset > obj->size || args->size > obj->size - args->offset) {
487 ret = -EINVAL;
488 goto err;
491 if (!access_ok(VERIFY_WRITE,
492 (char __user *)(uintptr_t)args->data_ptr,
493 args->size)) {
494 ret = -EFAULT;
495 goto err;
498 if (i915_gem_object_needs_bit17_swizzle(obj)) {
499 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
500 } else {
501 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
502 if (ret != 0)
503 ret = i915_gem_shmem_pread_slow(dev, obj, args,
504 file_priv);
507 err:
508 drm_gem_object_unreference(obj);
509 return ret;
512 /* This is the fast write path which cannot handle
513 * page faults in the source data
516 static inline int
517 fast_user_write(struct io_mapping *mapping,
518 loff_t page_base, int page_offset,
519 char __user *user_data,
520 int length)
522 char *vaddr_atomic;
523 unsigned long unwritten;
525 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
526 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
527 user_data, length);
528 io_mapping_unmap_atomic(vaddr_atomic);
529 if (unwritten)
530 return -EFAULT;
531 return 0;
534 /* Here's the write path which can sleep for
535 * page faults
538 static inline int
539 slow_kernel_write(struct io_mapping *mapping,
540 loff_t gtt_base, int gtt_offset,
541 struct page *user_page, int user_offset,
542 int length)
544 char *src_vaddr, *dst_vaddr;
545 unsigned long unwritten;
547 dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base);
548 src_vaddr = kmap_atomic(user_page, KM_USER1);
549 unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset,
550 src_vaddr + user_offset,
551 length);
552 kunmap_atomic(src_vaddr, KM_USER1);
553 io_mapping_unmap_atomic(dst_vaddr);
554 if (unwritten)
555 return -EFAULT;
556 return 0;
559 static inline int
560 fast_shmem_write(struct page **pages,
561 loff_t page_base, int page_offset,
562 char __user *data,
563 int length)
565 char __iomem *vaddr;
566 unsigned long unwritten;
568 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
569 if (vaddr == NULL)
570 return -ENOMEM;
571 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
572 kunmap_atomic(vaddr, KM_USER0);
574 if (unwritten)
575 return -EFAULT;
576 return 0;
580 * This is the fast pwrite path, where we copy the data directly from the
581 * user into the GTT, uncached.
583 static int
584 i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
585 struct drm_i915_gem_pwrite *args,
586 struct drm_file *file_priv)
588 struct drm_i915_gem_object *obj_priv = obj->driver_private;
589 drm_i915_private_t *dev_priv = dev->dev_private;
590 ssize_t remain;
591 loff_t offset, page_base;
592 char __user *user_data;
593 int page_offset, page_length;
594 int ret;
596 user_data = (char __user *) (uintptr_t) args->data_ptr;
597 remain = args->size;
600 mutex_lock(&dev->struct_mutex);
601 ret = i915_gem_object_pin(obj, 0);
602 if (ret) {
603 mutex_unlock(&dev->struct_mutex);
604 return ret;
606 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
607 if (ret)
608 goto fail;
610 obj_priv = obj->driver_private;
611 offset = obj_priv->gtt_offset + args->offset;
613 while (remain > 0) {
614 /* Operation in this page
616 * page_base = page offset within aperture
617 * page_offset = offset within page
618 * page_length = bytes to copy for this page
620 page_base = (offset & ~(PAGE_SIZE-1));
621 page_offset = offset & (PAGE_SIZE-1);
622 page_length = remain;
623 if ((page_offset + remain) > PAGE_SIZE)
624 page_length = PAGE_SIZE - page_offset;
626 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
627 page_offset, user_data, page_length);
629 /* If we get a fault while copying data, then (presumably) our
630 * source page isn't available. Return the error and we'll
631 * retry in the slow path.
633 if (ret)
634 goto fail;
636 remain -= page_length;
637 user_data += page_length;
638 offset += page_length;
641 fail:
642 i915_gem_object_unpin(obj);
643 mutex_unlock(&dev->struct_mutex);
645 return ret;
649 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
650 * the memory and maps it using kmap_atomic for copying.
652 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
653 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
655 static int
656 i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
657 struct drm_i915_gem_pwrite *args,
658 struct drm_file *file_priv)
660 struct drm_i915_gem_object *obj_priv = obj->driver_private;
661 drm_i915_private_t *dev_priv = dev->dev_private;
662 ssize_t remain;
663 loff_t gtt_page_base, offset;
664 loff_t first_data_page, last_data_page, num_pages;
665 loff_t pinned_pages, i;
666 struct page **user_pages;
667 struct mm_struct *mm = current->mm;
668 int gtt_page_offset, data_page_offset, data_page_index, page_length;
669 int ret;
670 uint64_t data_ptr = args->data_ptr;
672 remain = args->size;
674 /* Pin the user pages containing the data. We can't fault while
675 * holding the struct mutex, and all of the pwrite implementations
676 * want to hold it while dereferencing the user data.
678 first_data_page = data_ptr / PAGE_SIZE;
679 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
680 num_pages = last_data_page - first_data_page + 1;
682 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
683 if (user_pages == NULL)
684 return -ENOMEM;
686 down_read(&mm->mmap_sem);
687 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
688 num_pages, 0, 0, user_pages, NULL);
689 up_read(&mm->mmap_sem);
690 if (pinned_pages < num_pages) {
691 ret = -EFAULT;
692 goto out_unpin_pages;
695 mutex_lock(&dev->struct_mutex);
696 ret = i915_gem_object_pin(obj, 0);
697 if (ret)
698 goto out_unlock;
700 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
701 if (ret)
702 goto out_unpin_object;
704 obj_priv = obj->driver_private;
705 offset = obj_priv->gtt_offset + args->offset;
707 while (remain > 0) {
708 /* Operation in this page
710 * gtt_page_base = page offset within aperture
711 * gtt_page_offset = offset within page in aperture
712 * data_page_index = page number in get_user_pages return
713 * data_page_offset = offset with data_page_index page.
714 * page_length = bytes to copy for this page
716 gtt_page_base = offset & PAGE_MASK;
717 gtt_page_offset = offset & ~PAGE_MASK;
718 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
719 data_page_offset = data_ptr & ~PAGE_MASK;
721 page_length = remain;
722 if ((gtt_page_offset + page_length) > PAGE_SIZE)
723 page_length = PAGE_SIZE - gtt_page_offset;
724 if ((data_page_offset + page_length) > PAGE_SIZE)
725 page_length = PAGE_SIZE - data_page_offset;
727 ret = slow_kernel_write(dev_priv->mm.gtt_mapping,
728 gtt_page_base, gtt_page_offset,
729 user_pages[data_page_index],
730 data_page_offset,
731 page_length);
733 /* If we get a fault while copying data, then (presumably) our
734 * source page isn't available. Return the error and we'll
735 * retry in the slow path.
737 if (ret)
738 goto out_unpin_object;
740 remain -= page_length;
741 offset += page_length;
742 data_ptr += page_length;
745 out_unpin_object:
746 i915_gem_object_unpin(obj);
747 out_unlock:
748 mutex_unlock(&dev->struct_mutex);
749 out_unpin_pages:
750 for (i = 0; i < pinned_pages; i++)
751 page_cache_release(user_pages[i]);
752 drm_free_large(user_pages);
754 return ret;
758 * This is the fast shmem pwrite path, which attempts to directly
759 * copy_from_user into the kmapped pages backing the object.
761 static int
762 i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
763 struct drm_i915_gem_pwrite *args,
764 struct drm_file *file_priv)
766 struct drm_i915_gem_object *obj_priv = obj->driver_private;
767 ssize_t remain;
768 loff_t offset, page_base;
769 char __user *user_data;
770 int page_offset, page_length;
771 int ret;
773 user_data = (char __user *) (uintptr_t) args->data_ptr;
774 remain = args->size;
776 mutex_lock(&dev->struct_mutex);
778 ret = i915_gem_object_get_pages(obj, 0);
779 if (ret != 0)
780 goto fail_unlock;
782 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
783 if (ret != 0)
784 goto fail_put_pages;
786 obj_priv = obj->driver_private;
787 offset = args->offset;
788 obj_priv->dirty = 1;
790 while (remain > 0) {
791 /* Operation in this page
793 * page_base = page offset within aperture
794 * page_offset = offset within page
795 * page_length = bytes to copy for this page
797 page_base = (offset & ~(PAGE_SIZE-1));
798 page_offset = offset & (PAGE_SIZE-1);
799 page_length = remain;
800 if ((page_offset + remain) > PAGE_SIZE)
801 page_length = PAGE_SIZE - page_offset;
803 ret = fast_shmem_write(obj_priv->pages,
804 page_base, page_offset,
805 user_data, page_length);
806 if (ret)
807 goto fail_put_pages;
809 remain -= page_length;
810 user_data += page_length;
811 offset += page_length;
814 fail_put_pages:
815 i915_gem_object_put_pages(obj);
816 fail_unlock:
817 mutex_unlock(&dev->struct_mutex);
819 return ret;
823 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
824 * the memory and maps it using kmap_atomic for copying.
826 * This avoids taking mmap_sem for faulting on the user's address while the
827 * struct_mutex is held.
829 static int
830 i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
831 struct drm_i915_gem_pwrite *args,
832 struct drm_file *file_priv)
834 struct drm_i915_gem_object *obj_priv = obj->driver_private;
835 struct mm_struct *mm = current->mm;
836 struct page **user_pages;
837 ssize_t remain;
838 loff_t offset, pinned_pages, i;
839 loff_t first_data_page, last_data_page, num_pages;
840 int shmem_page_index, shmem_page_offset;
841 int data_page_index, data_page_offset;
842 int page_length;
843 int ret;
844 uint64_t data_ptr = args->data_ptr;
845 int do_bit17_swizzling;
847 remain = args->size;
849 /* Pin the user pages containing the data. We can't fault while
850 * holding the struct mutex, and all of the pwrite implementations
851 * want to hold it while dereferencing the user data.
853 first_data_page = data_ptr / PAGE_SIZE;
854 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
855 num_pages = last_data_page - first_data_page + 1;
857 user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
858 if (user_pages == NULL)
859 return -ENOMEM;
861 down_read(&mm->mmap_sem);
862 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
863 num_pages, 0, 0, user_pages, NULL);
864 up_read(&mm->mmap_sem);
865 if (pinned_pages < num_pages) {
866 ret = -EFAULT;
867 goto fail_put_user_pages;
870 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
872 mutex_lock(&dev->struct_mutex);
874 ret = i915_gem_object_get_pages_or_evict(obj);
875 if (ret)
876 goto fail_unlock;
878 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
879 if (ret != 0)
880 goto fail_put_pages;
882 obj_priv = obj->driver_private;
883 offset = args->offset;
884 obj_priv->dirty = 1;
886 while (remain > 0) {
887 /* Operation in this page
889 * shmem_page_index = page number within shmem file
890 * shmem_page_offset = offset within page in shmem file
891 * data_page_index = page number in get_user_pages return
892 * data_page_offset = offset with data_page_index page.
893 * page_length = bytes to copy for this page
895 shmem_page_index = offset / PAGE_SIZE;
896 shmem_page_offset = offset & ~PAGE_MASK;
897 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
898 data_page_offset = data_ptr & ~PAGE_MASK;
900 page_length = remain;
901 if ((shmem_page_offset + page_length) > PAGE_SIZE)
902 page_length = PAGE_SIZE - shmem_page_offset;
903 if ((data_page_offset + page_length) > PAGE_SIZE)
904 page_length = PAGE_SIZE - data_page_offset;
906 if (do_bit17_swizzling) {
907 ret = slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
908 shmem_page_offset,
909 user_pages[data_page_index],
910 data_page_offset,
911 page_length,
913 } else {
914 ret = slow_shmem_copy(obj_priv->pages[shmem_page_index],
915 shmem_page_offset,
916 user_pages[data_page_index],
917 data_page_offset,
918 page_length);
920 if (ret)
921 goto fail_put_pages;
923 remain -= page_length;
924 data_ptr += page_length;
925 offset += page_length;
928 fail_put_pages:
929 i915_gem_object_put_pages(obj);
930 fail_unlock:
931 mutex_unlock(&dev->struct_mutex);
932 fail_put_user_pages:
933 for (i = 0; i < pinned_pages; i++)
934 page_cache_release(user_pages[i]);
935 drm_free_large(user_pages);
937 return ret;
941 * Writes data to the object referenced by handle.
943 * On error, the contents of the buffer that were to be modified are undefined.
946 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
947 struct drm_file *file_priv)
949 struct drm_i915_gem_pwrite *args = data;
950 struct drm_gem_object *obj;
951 struct drm_i915_gem_object *obj_priv;
952 int ret = 0;
954 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
955 if (obj == NULL)
956 return -EBADF;
957 obj_priv = obj->driver_private;
959 /* Bounds check destination. */
960 if (args->offset > obj->size || args->size > obj->size - args->offset) {
961 ret = -EINVAL;
962 goto err;
965 if (!access_ok(VERIFY_READ,
966 (char __user *)(uintptr_t)args->data_ptr,
967 args->size)) {
968 ret = -EFAULT;
969 goto err;
972 /* We can only do the GTT pwrite on untiled buffers, as otherwise
973 * it would end up going through the fenced access, and we'll get
974 * different detiling behavior between reading and writing.
975 * pread/pwrite currently are reading and writing from the CPU
976 * perspective, requiring manual detiling by the client.
978 if (obj_priv->phys_obj)
979 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
980 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
981 dev->gtt_total != 0) {
982 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
983 if (ret == -EFAULT) {
984 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
985 file_priv);
987 } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
988 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
989 } else {
990 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
991 if (ret == -EFAULT) {
992 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
993 file_priv);
997 #if WATCH_PWRITE
998 if (ret)
999 DRM_INFO("pwrite failed %d\n", ret);
1000 #endif
1002 err:
1003 drm_gem_object_unreference(obj);
1004 return ret;
1008 * Called when user space prepares to use an object with the CPU, either
1009 * through the mmap ioctl's mapping or a GTT mapping.
1012 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1013 struct drm_file *file_priv)
1015 struct drm_i915_private *dev_priv = dev->dev_private;
1016 struct drm_i915_gem_set_domain *args = data;
1017 struct drm_gem_object *obj;
1018 struct drm_i915_gem_object *obj_priv;
1019 uint32_t read_domains = args->read_domains;
1020 uint32_t write_domain = args->write_domain;
1021 int ret;
1023 if (!(dev->driver->driver_features & DRIVER_GEM))
1024 return -ENODEV;
1026 /* Only handle setting domains to types used by the CPU. */
1027 if (write_domain & I915_GEM_GPU_DOMAINS)
1028 return -EINVAL;
1030 if (read_domains & I915_GEM_GPU_DOMAINS)
1031 return -EINVAL;
1033 /* Having something in the write domain implies it's in the read
1034 * domain, and only that read domain. Enforce that in the request.
1036 if (write_domain != 0 && read_domains != write_domain)
1037 return -EINVAL;
1039 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1040 if (obj == NULL)
1041 return -EBADF;
1042 obj_priv = obj->driver_private;
1044 mutex_lock(&dev->struct_mutex);
1046 intel_mark_busy(dev, obj);
1048 #if WATCH_BUF
1049 DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
1050 obj, obj->size, read_domains, write_domain);
1051 #endif
1052 if (read_domains & I915_GEM_DOMAIN_GTT) {
1053 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1055 /* Update the LRU on the fence for the CPU access that's
1056 * about to occur.
1058 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1059 list_move_tail(&obj_priv->fence_list,
1060 &dev_priv->mm.fence_list);
1063 /* Silently promote "you're not bound, there was nothing to do"
1064 * to success, since the client was just asking us to
1065 * make sure everything was done.
1067 if (ret == -EINVAL)
1068 ret = 0;
1069 } else {
1070 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1073 drm_gem_object_unreference(obj);
1074 mutex_unlock(&dev->struct_mutex);
1075 return ret;
1079 * Called when user space has done writes to this buffer
1082 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1083 struct drm_file *file_priv)
1085 struct drm_i915_gem_sw_finish *args = data;
1086 struct drm_gem_object *obj;
1087 struct drm_i915_gem_object *obj_priv;
1088 int ret = 0;
1090 if (!(dev->driver->driver_features & DRIVER_GEM))
1091 return -ENODEV;
1093 mutex_lock(&dev->struct_mutex);
1094 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1095 if (obj == NULL) {
1096 mutex_unlock(&dev->struct_mutex);
1097 return -EBADF;
1100 #if WATCH_BUF
1101 DRM_INFO("%s: sw_finish %d (%p %zd)\n",
1102 __func__, args->handle, obj, obj->size);
1103 #endif
1104 obj_priv = obj->driver_private;
1106 /* Pinned buffers may be scanout, so flush the cache */
1107 if (obj_priv->pin_count)
1108 i915_gem_object_flush_cpu_write_domain(obj);
1110 drm_gem_object_unreference(obj);
1111 mutex_unlock(&dev->struct_mutex);
1112 return ret;
1116 * Maps the contents of an object, returning the address it is mapped
1117 * into.
1119 * While the mapping holds a reference on the contents of the object, it doesn't
1120 * imply a ref on the object itself.
1123 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1124 struct drm_file *file_priv)
1126 struct drm_i915_gem_mmap *args = data;
1127 struct drm_gem_object *obj;
1128 loff_t offset;
1129 unsigned long addr;
1131 if (!(dev->driver->driver_features & DRIVER_GEM))
1132 return -ENODEV;
1134 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1135 if (obj == NULL)
1136 return -EBADF;
1138 offset = args->offset;
1140 down_write(&current->mm->mmap_sem);
1141 addr = do_mmap(obj->filp, 0, args->size,
1142 PROT_READ | PROT_WRITE, MAP_SHARED,
1143 args->offset);
1144 up_write(&current->mm->mmap_sem);
1145 mutex_lock(&dev->struct_mutex);
1146 drm_gem_object_unreference(obj);
1147 mutex_unlock(&dev->struct_mutex);
1148 if (IS_ERR((void *)addr))
1149 return addr;
1151 args->addr_ptr = (uint64_t) addr;
1153 return 0;
1157 * i915_gem_fault - fault a page into the GTT
1158 * vma: VMA in question
1159 * vmf: fault info
1161 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1162 * from userspace. The fault handler takes care of binding the object to
1163 * the GTT (if needed), allocating and programming a fence register (again,
1164 * only if needed based on whether the old reg is still valid or the object
1165 * is tiled) and inserting a new PTE into the faulting process.
1167 * Note that the faulting process may involve evicting existing objects
1168 * from the GTT and/or fence registers to make room. So performance may
1169 * suffer if the GTT working set is large or there are few fence registers
1170 * left.
1172 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1174 struct drm_gem_object *obj = vma->vm_private_data;
1175 struct drm_device *dev = obj->dev;
1176 struct drm_i915_private *dev_priv = dev->dev_private;
1177 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1178 pgoff_t page_offset;
1179 unsigned long pfn;
1180 int ret = 0;
1181 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1183 /* We don't use vmf->pgoff since that has the fake offset */
1184 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1185 PAGE_SHIFT;
1187 /* Now bind it into the GTT if needed */
1188 mutex_lock(&dev->struct_mutex);
1189 if (!obj_priv->gtt_space) {
1190 ret = i915_gem_object_bind_to_gtt(obj, 0);
1191 if (ret)
1192 goto unlock;
1194 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1196 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1197 if (ret)
1198 goto unlock;
1201 /* Need a new fence register? */
1202 if (obj_priv->tiling_mode != I915_TILING_NONE) {
1203 ret = i915_gem_object_get_fence_reg(obj);
1204 if (ret)
1205 goto unlock;
1208 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1209 page_offset;
1211 /* Finally, remap it using the new GTT offset */
1212 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1213 unlock:
1214 mutex_unlock(&dev->struct_mutex);
1216 switch (ret) {
1217 case 0:
1218 case -ERESTARTSYS:
1219 return VM_FAULT_NOPAGE;
1220 case -ENOMEM:
1221 case -EAGAIN:
1222 return VM_FAULT_OOM;
1223 default:
1224 return VM_FAULT_SIGBUS;
1229 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1230 * @obj: obj in question
1232 * GEM memory mapping works by handing back to userspace a fake mmap offset
1233 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1234 * up the object based on the offset and sets up the various memory mapping
1235 * structures.
1237 * This routine allocates and attaches a fake offset for @obj.
1239 static int
1240 i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1242 struct drm_device *dev = obj->dev;
1243 struct drm_gem_mm *mm = dev->mm_private;
1244 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1245 struct drm_map_list *list;
1246 struct drm_local_map *map;
1247 int ret = 0;
1249 /* Set the object up for mmap'ing */
1250 list = &obj->map_list;
1251 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
1252 if (!list->map)
1253 return -ENOMEM;
1255 map = list->map;
1256 map->type = _DRM_GEM;
1257 map->size = obj->size;
1258 map->handle = obj;
1260 /* Get a DRM GEM mmap offset allocated... */
1261 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1262 obj->size / PAGE_SIZE, 0, 0);
1263 if (!list->file_offset_node) {
1264 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1265 ret = -ENOMEM;
1266 goto out_free_list;
1269 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1270 obj->size / PAGE_SIZE, 0);
1271 if (!list->file_offset_node) {
1272 ret = -ENOMEM;
1273 goto out_free_list;
1276 list->hash.key = list->file_offset_node->start;
1277 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1278 DRM_ERROR("failed to add to map hash\n");
1279 ret = -ENOMEM;
1280 goto out_free_mm;
1283 /* By now we should be all set, any drm_mmap request on the offset
1284 * below will get to our mmap & fault handler */
1285 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1287 return 0;
1289 out_free_mm:
1290 drm_mm_put_block(list->file_offset_node);
1291 out_free_list:
1292 kfree(list->map);
1294 return ret;
1298 * i915_gem_release_mmap - remove physical page mappings
1299 * @obj: obj in question
1301 * Preserve the reservation of the mmaping with the DRM core code, but
1302 * relinquish ownership of the pages back to the system.
1304 * It is vital that we remove the page mapping if we have mapped a tiled
1305 * object through the GTT and then lose the fence register due to
1306 * resource pressure. Similarly if the object has been moved out of the
1307 * aperture, than pages mapped into userspace must be revoked. Removing the
1308 * mapping will then trigger a page fault on the next user access, allowing
1309 * fixup by i915_gem_fault().
1311 void
1312 i915_gem_release_mmap(struct drm_gem_object *obj)
1314 struct drm_device *dev = obj->dev;
1315 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1317 if (dev->dev_mapping)
1318 unmap_mapping_range(dev->dev_mapping,
1319 obj_priv->mmap_offset, obj->size, 1);
1322 static void
1323 i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1325 struct drm_device *dev = obj->dev;
1326 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1327 struct drm_gem_mm *mm = dev->mm_private;
1328 struct drm_map_list *list;
1330 list = &obj->map_list;
1331 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1333 if (list->file_offset_node) {
1334 drm_mm_put_block(list->file_offset_node);
1335 list->file_offset_node = NULL;
1338 if (list->map) {
1339 kfree(list->map);
1340 list->map = NULL;
1343 obj_priv->mmap_offset = 0;
1347 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1348 * @obj: object to check
1350 * Return the required GTT alignment for an object, taking into account
1351 * potential fence register mapping if needed.
1353 static uint32_t
1354 i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1356 struct drm_device *dev = obj->dev;
1357 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1358 int start, i;
1361 * Minimum alignment is 4k (GTT page size), but might be greater
1362 * if a fence register is needed for the object.
1364 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1365 return 4096;
1368 * Previous chips need to be aligned to the size of the smallest
1369 * fence register that can contain the object.
1371 if (IS_I9XX(dev))
1372 start = 1024*1024;
1373 else
1374 start = 512*1024;
1376 for (i = start; i < obj->size; i <<= 1)
1379 return i;
1383 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1384 * @dev: DRM device
1385 * @data: GTT mapping ioctl data
1386 * @file_priv: GEM object info
1388 * Simply returns the fake offset to userspace so it can mmap it.
1389 * The mmap call will end up in drm_gem_mmap(), which will set things
1390 * up so we can get faults in the handler above.
1392 * The fault handler will take care of binding the object into the GTT
1393 * (since it may have been evicted to make room for something), allocating
1394 * a fence register, and mapping the appropriate aperture address into
1395 * userspace.
1398 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1399 struct drm_file *file_priv)
1401 struct drm_i915_gem_mmap_gtt *args = data;
1402 struct drm_i915_private *dev_priv = dev->dev_private;
1403 struct drm_gem_object *obj;
1404 struct drm_i915_gem_object *obj_priv;
1405 int ret;
1407 if (!(dev->driver->driver_features & DRIVER_GEM))
1408 return -ENODEV;
1410 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1411 if (obj == NULL)
1412 return -EBADF;
1414 mutex_lock(&dev->struct_mutex);
1416 obj_priv = obj->driver_private;
1418 if (obj_priv->madv != I915_MADV_WILLNEED) {
1419 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1420 drm_gem_object_unreference(obj);
1421 mutex_unlock(&dev->struct_mutex);
1422 return -EINVAL;
1426 if (!obj_priv->mmap_offset) {
1427 ret = i915_gem_create_mmap_offset(obj);
1428 if (ret) {
1429 drm_gem_object_unreference(obj);
1430 mutex_unlock(&dev->struct_mutex);
1431 return ret;
1435 args->offset = obj_priv->mmap_offset;
1438 * Pull it into the GTT so that we have a page list (makes the
1439 * initial fault faster and any subsequent flushing possible).
1441 if (!obj_priv->agp_mem) {
1442 ret = i915_gem_object_bind_to_gtt(obj, 0);
1443 if (ret) {
1444 drm_gem_object_unreference(obj);
1445 mutex_unlock(&dev->struct_mutex);
1446 return ret;
1448 list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1451 drm_gem_object_unreference(obj);
1452 mutex_unlock(&dev->struct_mutex);
1454 return 0;
1457 void
1458 i915_gem_object_put_pages(struct drm_gem_object *obj)
1460 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1461 int page_count = obj->size / PAGE_SIZE;
1462 int i;
1464 BUG_ON(obj_priv->pages_refcount == 0);
1465 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
1467 if (--obj_priv->pages_refcount != 0)
1468 return;
1470 if (obj_priv->tiling_mode != I915_TILING_NONE)
1471 i915_gem_object_save_bit_17_swizzle(obj);
1473 if (obj_priv->madv == I915_MADV_DONTNEED)
1474 obj_priv->dirty = 0;
1476 for (i = 0; i < page_count; i++) {
1477 if (obj_priv->dirty)
1478 set_page_dirty(obj_priv->pages[i]);
1480 if (obj_priv->madv == I915_MADV_WILLNEED)
1481 mark_page_accessed(obj_priv->pages[i]);
1483 page_cache_release(obj_priv->pages[i]);
1485 obj_priv->dirty = 0;
1487 drm_free_large(obj_priv->pages);
1488 obj_priv->pages = NULL;
1491 static void
1492 i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
1494 struct drm_device *dev = obj->dev;
1495 drm_i915_private_t *dev_priv = dev->dev_private;
1496 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1498 /* Add a reference if we're newly entering the active list. */
1499 if (!obj_priv->active) {
1500 drm_gem_object_reference(obj);
1501 obj_priv->active = 1;
1503 /* Move from whatever list we were on to the tail of execution. */
1504 spin_lock(&dev_priv->mm.active_list_lock);
1505 list_move_tail(&obj_priv->list,
1506 &dev_priv->mm.active_list);
1507 spin_unlock(&dev_priv->mm.active_list_lock);
1508 obj_priv->last_rendering_seqno = seqno;
1511 static void
1512 i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1514 struct drm_device *dev = obj->dev;
1515 drm_i915_private_t *dev_priv = dev->dev_private;
1516 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1518 BUG_ON(!obj_priv->active);
1519 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1520 obj_priv->last_rendering_seqno = 0;
1523 /* Immediately discard the backing storage */
1524 static void
1525 i915_gem_object_truncate(struct drm_gem_object *obj)
1527 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1528 struct inode *inode;
1530 inode = obj->filp->f_path.dentry->d_inode;
1531 if (inode->i_op->truncate)
1532 inode->i_op->truncate (inode);
1534 obj_priv->madv = __I915_MADV_PURGED;
1537 static inline int
1538 i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1540 return obj_priv->madv == I915_MADV_DONTNEED;
1543 static void
1544 i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1546 struct drm_device *dev = obj->dev;
1547 drm_i915_private_t *dev_priv = dev->dev_private;
1548 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1550 i915_verify_inactive(dev, __FILE__, __LINE__);
1551 if (obj_priv->pin_count != 0)
1552 list_del_init(&obj_priv->list);
1553 else
1554 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1556 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1558 obj_priv->last_rendering_seqno = 0;
1559 if (obj_priv->active) {
1560 obj_priv->active = 0;
1561 drm_gem_object_unreference(obj);
1563 i915_verify_inactive(dev, __FILE__, __LINE__);
1567 * Creates a new sequence number, emitting a write of it to the status page
1568 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1570 * Must be called with struct_lock held.
1572 * Returned sequence numbers are nonzero on success.
1574 static uint32_t
1575 i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
1576 uint32_t flush_domains)
1578 drm_i915_private_t *dev_priv = dev->dev_private;
1579 struct drm_i915_file_private *i915_file_priv = NULL;
1580 struct drm_i915_gem_request *request;
1581 uint32_t seqno;
1582 int was_empty;
1583 RING_LOCALS;
1585 if (file_priv != NULL)
1586 i915_file_priv = file_priv->driver_priv;
1588 request = kzalloc(sizeof(*request), GFP_KERNEL);
1589 if (request == NULL)
1590 return 0;
1592 /* Grab the seqno we're going to make this request be, and bump the
1593 * next (skipping 0 so it can be the reserved no-seqno value).
1595 seqno = dev_priv->mm.next_gem_seqno;
1596 dev_priv->mm.next_gem_seqno++;
1597 if (dev_priv->mm.next_gem_seqno == 0)
1598 dev_priv->mm.next_gem_seqno++;
1600 BEGIN_LP_RING(4);
1601 OUT_RING(MI_STORE_DWORD_INDEX);
1602 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1603 OUT_RING(seqno);
1605 OUT_RING(MI_USER_INTERRUPT);
1606 ADVANCE_LP_RING();
1608 DRM_DEBUG("%d\n", seqno);
1610 request->seqno = seqno;
1611 request->emitted_jiffies = jiffies;
1612 was_empty = list_empty(&dev_priv->mm.request_list);
1613 list_add_tail(&request->list, &dev_priv->mm.request_list);
1614 if (i915_file_priv) {
1615 list_add_tail(&request->client_list,
1616 &i915_file_priv->mm.request_list);
1617 } else {
1618 INIT_LIST_HEAD(&request->client_list);
1621 /* Associate any objects on the flushing list matching the write
1622 * domain we're flushing with our flush.
1624 if (flush_domains != 0) {
1625 struct drm_i915_gem_object *obj_priv, *next;
1627 list_for_each_entry_safe(obj_priv, next,
1628 &dev_priv->mm.gpu_write_list,
1629 gpu_write_list) {
1630 struct drm_gem_object *obj = obj_priv->obj;
1632 if ((obj->write_domain & flush_domains) ==
1633 obj->write_domain) {
1634 uint32_t old_write_domain = obj->write_domain;
1636 obj->write_domain = 0;
1637 list_del_init(&obj_priv->gpu_write_list);
1638 i915_gem_object_move_to_active(obj, seqno);
1640 trace_i915_gem_object_change_domain(obj,
1641 obj->read_domains,
1642 old_write_domain);
1648 if (!dev_priv->mm.suspended) {
1649 mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
1650 if (was_empty)
1651 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1653 return seqno;
1657 * Command execution barrier
1659 * Ensures that all commands in the ring are finished
1660 * before signalling the CPU
1662 static uint32_t
1663 i915_retire_commands(struct drm_device *dev)
1665 drm_i915_private_t *dev_priv = dev->dev_private;
1666 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1667 uint32_t flush_domains = 0;
1668 RING_LOCALS;
1670 /* The sampler always gets flushed on i965 (sigh) */
1671 if (IS_I965G(dev))
1672 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1673 BEGIN_LP_RING(2);
1674 OUT_RING(cmd);
1675 OUT_RING(0); /* noop */
1676 ADVANCE_LP_RING();
1677 return flush_domains;
1681 * Moves buffers associated only with the given active seqno from the active
1682 * to inactive list, potentially freeing them.
1684 static void
1685 i915_gem_retire_request(struct drm_device *dev,
1686 struct drm_i915_gem_request *request)
1688 drm_i915_private_t *dev_priv = dev->dev_private;
1690 trace_i915_gem_request_retire(dev, request->seqno);
1692 /* Move any buffers on the active list that are no longer referenced
1693 * by the ringbuffer to the flushing/inactive lists as appropriate.
1695 spin_lock(&dev_priv->mm.active_list_lock);
1696 while (!list_empty(&dev_priv->mm.active_list)) {
1697 struct drm_gem_object *obj;
1698 struct drm_i915_gem_object *obj_priv;
1700 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1701 struct drm_i915_gem_object,
1702 list);
1703 obj = obj_priv->obj;
1705 /* If the seqno being retired doesn't match the oldest in the
1706 * list, then the oldest in the list must still be newer than
1707 * this seqno.
1709 if (obj_priv->last_rendering_seqno != request->seqno)
1710 goto out;
1712 #if WATCH_LRU
1713 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1714 __func__, request->seqno, obj);
1715 #endif
1717 if (obj->write_domain != 0)
1718 i915_gem_object_move_to_flushing(obj);
1719 else {
1720 /* Take a reference on the object so it won't be
1721 * freed while the spinlock is held. The list
1722 * protection for this spinlock is safe when breaking
1723 * the lock like this since the next thing we do
1724 * is just get the head of the list again.
1726 drm_gem_object_reference(obj);
1727 i915_gem_object_move_to_inactive(obj);
1728 spin_unlock(&dev_priv->mm.active_list_lock);
1729 drm_gem_object_unreference(obj);
1730 spin_lock(&dev_priv->mm.active_list_lock);
1733 out:
1734 spin_unlock(&dev_priv->mm.active_list_lock);
1738 * Returns true if seq1 is later than seq2.
1740 bool
1741 i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1743 return (int32_t)(seq1 - seq2) >= 0;
1746 uint32_t
1747 i915_get_gem_seqno(struct drm_device *dev)
1749 drm_i915_private_t *dev_priv = dev->dev_private;
1751 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1755 * This function clears the request list as sequence numbers are passed.
1757 void
1758 i915_gem_retire_requests(struct drm_device *dev)
1760 drm_i915_private_t *dev_priv = dev->dev_private;
1761 uint32_t seqno;
1763 if (!dev_priv->hw_status_page || list_empty(&dev_priv->mm.request_list))
1764 return;
1766 seqno = i915_get_gem_seqno(dev);
1768 while (!list_empty(&dev_priv->mm.request_list)) {
1769 struct drm_i915_gem_request *request;
1770 uint32_t retiring_seqno;
1772 request = list_first_entry(&dev_priv->mm.request_list,
1773 struct drm_i915_gem_request,
1774 list);
1775 retiring_seqno = request->seqno;
1777 if (i915_seqno_passed(seqno, retiring_seqno) ||
1778 atomic_read(&dev_priv->mm.wedged)) {
1779 i915_gem_retire_request(dev, request);
1781 list_del(&request->list);
1782 list_del(&request->client_list);
1783 kfree(request);
1784 } else
1785 break;
1788 if (unlikely (dev_priv->trace_irq_seqno &&
1789 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
1790 i915_user_irq_put(dev);
1791 dev_priv->trace_irq_seqno = 0;
1795 void
1796 i915_gem_retire_work_handler(struct work_struct *work)
1798 drm_i915_private_t *dev_priv;
1799 struct drm_device *dev;
1801 dev_priv = container_of(work, drm_i915_private_t,
1802 mm.retire_work.work);
1803 dev = dev_priv->dev;
1805 mutex_lock(&dev->struct_mutex);
1806 i915_gem_retire_requests(dev);
1807 if (!dev_priv->mm.suspended &&
1808 !list_empty(&dev_priv->mm.request_list))
1809 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1810 mutex_unlock(&dev->struct_mutex);
1813 static int
1814 i915_do_wait_request(struct drm_device *dev, uint32_t seqno, int interruptible)
1816 drm_i915_private_t *dev_priv = dev->dev_private;
1817 u32 ier;
1818 int ret = 0;
1820 BUG_ON(seqno == 0);
1822 if (atomic_read(&dev_priv->mm.wedged))
1823 return -EIO;
1825 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
1826 if (IS_IGDNG(dev))
1827 ier = I915_READ(DEIER) | I915_READ(GTIER);
1828 else
1829 ier = I915_READ(IER);
1830 if (!ier) {
1831 DRM_ERROR("something (likely vbetool) disabled "
1832 "interrupts, re-enabling\n");
1833 i915_driver_irq_preinstall(dev);
1834 i915_driver_irq_postinstall(dev);
1837 trace_i915_gem_request_wait_begin(dev, seqno);
1839 dev_priv->mm.waiting_gem_seqno = seqno;
1840 i915_user_irq_get(dev);
1841 if (interruptible)
1842 ret = wait_event_interruptible(dev_priv->irq_queue,
1843 i915_seqno_passed(i915_get_gem_seqno(dev), seqno) ||
1844 atomic_read(&dev_priv->mm.wedged));
1845 else
1846 wait_event(dev_priv->irq_queue,
1847 i915_seqno_passed(i915_get_gem_seqno(dev), seqno) ||
1848 atomic_read(&dev_priv->mm.wedged));
1850 i915_user_irq_put(dev);
1851 dev_priv->mm.waiting_gem_seqno = 0;
1853 trace_i915_gem_request_wait_end(dev, seqno);
1855 if (atomic_read(&dev_priv->mm.wedged))
1856 ret = -EIO;
1858 if (ret && ret != -ERESTARTSYS)
1859 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1860 __func__, ret, seqno, i915_get_gem_seqno(dev));
1862 /* Directly dispatch request retiring. While we have the work queue
1863 * to handle this, the waiter on a request often wants an associated
1864 * buffer to have made it to the inactive list, and we would need
1865 * a separate wait queue to handle that.
1867 if (ret == 0)
1868 i915_gem_retire_requests(dev);
1870 return ret;
1874 * Waits for a sequence number to be signaled, and cleans up the
1875 * request and object lists appropriately for that event.
1877 static int
1878 i915_wait_request(struct drm_device *dev, uint32_t seqno)
1880 return i915_do_wait_request(dev, seqno, 1);
1884 * Waits for the ring to finish up to the latest request. Usefull for waiting
1885 * for flip events, e.g for the overlay support. */
1886 int i915_lp_ring_sync(struct drm_device *dev)
1888 uint32_t seqno;
1889 int ret;
1891 seqno = i915_add_request(dev, NULL, 0);
1893 if (seqno == 0)
1894 return -ENOMEM;
1896 ret = i915_do_wait_request(dev, seqno, 0);
1897 BUG_ON(ret == -ERESTARTSYS);
1898 return ret;
1901 static void
1902 i915_gem_flush(struct drm_device *dev,
1903 uint32_t invalidate_domains,
1904 uint32_t flush_domains)
1906 drm_i915_private_t *dev_priv = dev->dev_private;
1907 uint32_t cmd;
1908 RING_LOCALS;
1910 #if WATCH_EXEC
1911 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1912 invalidate_domains, flush_domains);
1913 #endif
1914 trace_i915_gem_request_flush(dev, dev_priv->mm.next_gem_seqno,
1915 invalidate_domains, flush_domains);
1917 if (flush_domains & I915_GEM_DOMAIN_CPU)
1918 drm_agp_chipset_flush(dev);
1920 if ((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) {
1922 * read/write caches:
1924 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1925 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1926 * also flushed at 2d versus 3d pipeline switches.
1928 * read-only caches:
1930 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1931 * MI_READ_FLUSH is set, and is always flushed on 965.
1933 * I915_GEM_DOMAIN_COMMAND may not exist?
1935 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1936 * invalidated when MI_EXE_FLUSH is set.
1938 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1939 * invalidated with every MI_FLUSH.
1941 * TLBs:
1943 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1944 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1945 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1946 * are flushed at any MI_FLUSH.
1949 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1950 if ((invalidate_domains|flush_domains) &
1951 I915_GEM_DOMAIN_RENDER)
1952 cmd &= ~MI_NO_WRITE_FLUSH;
1953 if (!IS_I965G(dev)) {
1955 * On the 965, the sampler cache always gets flushed
1956 * and this bit is reserved.
1958 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1959 cmd |= MI_READ_FLUSH;
1961 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1962 cmd |= MI_EXE_FLUSH;
1964 #if WATCH_EXEC
1965 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1966 #endif
1967 BEGIN_LP_RING(2);
1968 OUT_RING(cmd);
1969 OUT_RING(MI_NOOP);
1970 ADVANCE_LP_RING();
1975 * Ensures that all rendering to the object has completed and the object is
1976 * safe to unbind from the GTT or access from the CPU.
1978 static int
1979 i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1981 struct drm_device *dev = obj->dev;
1982 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1983 int ret;
1985 /* This function only exists to support waiting for existing rendering,
1986 * not for emitting required flushes.
1988 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
1990 /* If there is rendering queued on the buffer being evicted, wait for
1991 * it.
1993 if (obj_priv->active) {
1994 #if WATCH_BUF
1995 DRM_INFO("%s: object %p wait for seqno %08x\n",
1996 __func__, obj, obj_priv->last_rendering_seqno);
1997 #endif
1998 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1999 if (ret != 0)
2000 return ret;
2003 return 0;
2007 * Unbinds an object from the GTT aperture.
2010 i915_gem_object_unbind(struct drm_gem_object *obj)
2012 struct drm_device *dev = obj->dev;
2013 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2014 int ret = 0;
2016 #if WATCH_BUF
2017 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
2018 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
2019 #endif
2020 if (obj_priv->gtt_space == NULL)
2021 return 0;
2023 if (obj_priv->pin_count != 0) {
2024 DRM_ERROR("Attempting to unbind pinned buffer\n");
2025 return -EINVAL;
2028 /* blow away mappings if mapped through GTT */
2029 i915_gem_release_mmap(obj);
2031 /* Move the object to the CPU domain to ensure that
2032 * any possible CPU writes while it's not in the GTT
2033 * are flushed when we go to remap it. This will
2034 * also ensure that all pending GPU writes are finished
2035 * before we unbind.
2037 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2038 if (ret) {
2039 if (ret != -ERESTARTSYS)
2040 DRM_ERROR("set_domain failed: %d\n", ret);
2041 return ret;
2044 BUG_ON(obj_priv->active);
2046 /* release the fence reg _after_ flushing */
2047 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2048 i915_gem_clear_fence_reg(obj);
2050 if (obj_priv->agp_mem != NULL) {
2051 drm_unbind_agp(obj_priv->agp_mem);
2052 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
2053 obj_priv->agp_mem = NULL;
2056 i915_gem_object_put_pages(obj);
2057 BUG_ON(obj_priv->pages_refcount);
2059 if (obj_priv->gtt_space) {
2060 atomic_dec(&dev->gtt_count);
2061 atomic_sub(obj->size, &dev->gtt_memory);
2063 drm_mm_put_block(obj_priv->gtt_space);
2064 obj_priv->gtt_space = NULL;
2067 /* Remove ourselves from the LRU list if present. */
2068 if (!list_empty(&obj_priv->list))
2069 list_del_init(&obj_priv->list);
2071 if (i915_gem_object_is_purgeable(obj_priv))
2072 i915_gem_object_truncate(obj);
2074 trace_i915_gem_object_unbind(obj);
2076 return 0;
2079 static struct drm_gem_object *
2080 i915_gem_find_inactive_object(struct drm_device *dev, int min_size)
2082 drm_i915_private_t *dev_priv = dev->dev_private;
2083 struct drm_i915_gem_object *obj_priv;
2084 struct drm_gem_object *best = NULL;
2085 struct drm_gem_object *first = NULL;
2087 /* Try to find the smallest clean object */
2088 list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
2089 struct drm_gem_object *obj = obj_priv->obj;
2090 if (obj->size >= min_size) {
2091 if ((!obj_priv->dirty ||
2092 i915_gem_object_is_purgeable(obj_priv)) &&
2093 (!best || obj->size < best->size)) {
2094 best = obj;
2095 if (best->size == min_size)
2096 return best;
2098 if (!first)
2099 first = obj;
2103 return best ? best : first;
2106 static int
2107 i915_gem_evict_everything(struct drm_device *dev)
2109 drm_i915_private_t *dev_priv = dev->dev_private;
2110 int ret;
2111 uint32_t seqno;
2112 bool lists_empty;
2114 spin_lock(&dev_priv->mm.active_list_lock);
2115 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2116 list_empty(&dev_priv->mm.flushing_list) &&
2117 list_empty(&dev_priv->mm.active_list));
2118 spin_unlock(&dev_priv->mm.active_list_lock);
2120 if (lists_empty)
2121 return -ENOSPC;
2123 /* Flush everything (on to the inactive lists) and evict */
2124 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2125 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
2126 if (seqno == 0)
2127 return -ENOMEM;
2129 ret = i915_wait_request(dev, seqno);
2130 if (ret)
2131 return ret;
2133 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2135 ret = i915_gem_evict_from_inactive_list(dev);
2136 if (ret)
2137 return ret;
2139 spin_lock(&dev_priv->mm.active_list_lock);
2140 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2141 list_empty(&dev_priv->mm.flushing_list) &&
2142 list_empty(&dev_priv->mm.active_list));
2143 spin_unlock(&dev_priv->mm.active_list_lock);
2144 BUG_ON(!lists_empty);
2146 return 0;
2149 static int
2150 i915_gem_evict_something(struct drm_device *dev, int min_size)
2152 drm_i915_private_t *dev_priv = dev->dev_private;
2153 struct drm_gem_object *obj;
2154 int ret;
2156 for (;;) {
2157 i915_gem_retire_requests(dev);
2159 /* If there's an inactive buffer available now, grab it
2160 * and be done.
2162 obj = i915_gem_find_inactive_object(dev, min_size);
2163 if (obj) {
2164 struct drm_i915_gem_object *obj_priv;
2166 #if WATCH_LRU
2167 DRM_INFO("%s: evicting %p\n", __func__, obj);
2168 #endif
2169 obj_priv = obj->driver_private;
2170 BUG_ON(obj_priv->pin_count != 0);
2171 BUG_ON(obj_priv->active);
2173 /* Wait on the rendering and unbind the buffer. */
2174 return i915_gem_object_unbind(obj);
2177 /* If we didn't get anything, but the ring is still processing
2178 * things, wait for the next to finish and hopefully leave us
2179 * a buffer to evict.
2181 if (!list_empty(&dev_priv->mm.request_list)) {
2182 struct drm_i915_gem_request *request;
2184 request = list_first_entry(&dev_priv->mm.request_list,
2185 struct drm_i915_gem_request,
2186 list);
2188 ret = i915_wait_request(dev, request->seqno);
2189 if (ret)
2190 return ret;
2192 continue;
2195 /* If we didn't have anything on the request list but there
2196 * are buffers awaiting a flush, emit one and try again.
2197 * When we wait on it, those buffers waiting for that flush
2198 * will get moved to inactive.
2200 if (!list_empty(&dev_priv->mm.flushing_list)) {
2201 struct drm_i915_gem_object *obj_priv;
2203 /* Find an object that we can immediately reuse */
2204 list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
2205 obj = obj_priv->obj;
2206 if (obj->size >= min_size)
2207 break;
2209 obj = NULL;
2212 if (obj != NULL) {
2213 uint32_t seqno;
2215 i915_gem_flush(dev,
2216 obj->write_domain,
2217 obj->write_domain);
2218 seqno = i915_add_request(dev, NULL, obj->write_domain);
2219 if (seqno == 0)
2220 return -ENOMEM;
2222 ret = i915_wait_request(dev, seqno);
2223 if (ret)
2224 return ret;
2226 continue;
2230 /* If we didn't do any of the above, there's no single buffer
2231 * large enough to swap out for the new one, so just evict
2232 * everything and start again. (This should be rare.)
2234 if (!list_empty (&dev_priv->mm.inactive_list))
2235 return i915_gem_evict_from_inactive_list(dev);
2236 else
2237 return i915_gem_evict_everything(dev);
2242 i915_gem_object_get_pages(struct drm_gem_object *obj,
2243 gfp_t gfpmask)
2245 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2246 int page_count, i;
2247 struct address_space *mapping;
2248 struct inode *inode;
2249 struct page *page;
2251 if (obj_priv->pages_refcount++ != 0)
2252 return 0;
2254 /* Get the list of pages out of our struct file. They'll be pinned
2255 * at this point until we release them.
2257 page_count = obj->size / PAGE_SIZE;
2258 BUG_ON(obj_priv->pages != NULL);
2259 obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
2260 if (obj_priv->pages == NULL) {
2261 obj_priv->pages_refcount--;
2262 return -ENOMEM;
2265 inode = obj->filp->f_path.dentry->d_inode;
2266 mapping = inode->i_mapping;
2267 for (i = 0; i < page_count; i++) {
2268 page = read_cache_page_gfp(mapping, i,
2269 GFP_HIGHUSER |
2270 __GFP_COLD |
2271 __GFP_RECLAIMABLE |
2272 gfpmask);
2273 if (IS_ERR(page))
2274 goto err_pages;
2276 obj_priv->pages[i] = page;
2279 if (obj_priv->tiling_mode != I915_TILING_NONE)
2280 i915_gem_object_do_bit_17_swizzle(obj);
2282 return 0;
2284 err_pages:
2285 while (i--)
2286 page_cache_release(obj_priv->pages[i]);
2288 drm_free_large(obj_priv->pages);
2289 obj_priv->pages = NULL;
2290 obj_priv->pages_refcount--;
2291 return PTR_ERR(page);
2294 static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
2296 struct drm_gem_object *obj = reg->obj;
2297 struct drm_device *dev = obj->dev;
2298 drm_i915_private_t *dev_priv = dev->dev_private;
2299 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2300 int regnum = obj_priv->fence_reg;
2301 uint64_t val;
2303 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
2304 0xfffff000) << 32;
2305 val |= obj_priv->gtt_offset & 0xfffff000;
2306 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2307 if (obj_priv->tiling_mode == I915_TILING_Y)
2308 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2309 val |= I965_FENCE_REG_VALID;
2311 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2314 static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
2316 struct drm_gem_object *obj = reg->obj;
2317 struct drm_device *dev = obj->dev;
2318 drm_i915_private_t *dev_priv = dev->dev_private;
2319 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2320 int regnum = obj_priv->fence_reg;
2321 int tile_width;
2322 uint32_t fence_reg, val;
2323 uint32_t pitch_val;
2325 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2326 (obj_priv->gtt_offset & (obj->size - 1))) {
2327 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
2328 __func__, obj_priv->gtt_offset, obj->size);
2329 return;
2332 if (obj_priv->tiling_mode == I915_TILING_Y &&
2333 HAS_128_BYTE_Y_TILING(dev))
2334 tile_width = 128;
2335 else
2336 tile_width = 512;
2338 /* Note: pitch better be a power of two tile widths */
2339 pitch_val = obj_priv->stride / tile_width;
2340 pitch_val = ffs(pitch_val) - 1;
2342 if (obj_priv->tiling_mode == I915_TILING_Y &&
2343 HAS_128_BYTE_Y_TILING(dev))
2344 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2345 else
2346 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2348 val = obj_priv->gtt_offset;
2349 if (obj_priv->tiling_mode == I915_TILING_Y)
2350 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2351 val |= I915_FENCE_SIZE_BITS(obj->size);
2352 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2353 val |= I830_FENCE_REG_VALID;
2355 if (regnum < 8)
2356 fence_reg = FENCE_REG_830_0 + (regnum * 4);
2357 else
2358 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
2359 I915_WRITE(fence_reg, val);
2362 static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
2364 struct drm_gem_object *obj = reg->obj;
2365 struct drm_device *dev = obj->dev;
2366 drm_i915_private_t *dev_priv = dev->dev_private;
2367 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2368 int regnum = obj_priv->fence_reg;
2369 uint32_t val;
2370 uint32_t pitch_val;
2371 uint32_t fence_size_bits;
2373 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
2374 (obj_priv->gtt_offset & (obj->size - 1))) {
2375 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
2376 __func__, obj_priv->gtt_offset);
2377 return;
2380 pitch_val = obj_priv->stride / 128;
2381 pitch_val = ffs(pitch_val) - 1;
2382 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2384 val = obj_priv->gtt_offset;
2385 if (obj_priv->tiling_mode == I915_TILING_Y)
2386 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2387 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2388 WARN_ON(fence_size_bits & ~0x00000f00);
2389 val |= fence_size_bits;
2390 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2391 val |= I830_FENCE_REG_VALID;
2393 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
2397 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2398 * @obj: object to map through a fence reg
2400 * When mapping objects through the GTT, userspace wants to be able to write
2401 * to them without having to worry about swizzling if the object is tiled.
2403 * This function walks the fence regs looking for a free one for @obj,
2404 * stealing one if it can't find any.
2406 * It then sets up the reg based on the object's properties: address, pitch
2407 * and tiling format.
2410 i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
2412 struct drm_device *dev = obj->dev;
2413 struct drm_i915_private *dev_priv = dev->dev_private;
2414 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2415 struct drm_i915_fence_reg *reg = NULL;
2416 struct drm_i915_gem_object *old_obj_priv = NULL;
2417 int i, ret, avail;
2419 /* Just update our place in the LRU if our fence is getting used. */
2420 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
2421 list_move_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2422 return 0;
2425 switch (obj_priv->tiling_mode) {
2426 case I915_TILING_NONE:
2427 WARN(1, "allocating a fence for non-tiled object?\n");
2428 break;
2429 case I915_TILING_X:
2430 if (!obj_priv->stride)
2431 return -EINVAL;
2432 WARN((obj_priv->stride & (512 - 1)),
2433 "object 0x%08x is X tiled but has non-512B pitch\n",
2434 obj_priv->gtt_offset);
2435 break;
2436 case I915_TILING_Y:
2437 if (!obj_priv->stride)
2438 return -EINVAL;
2439 WARN((obj_priv->stride & (128 - 1)),
2440 "object 0x%08x is Y tiled but has non-128B pitch\n",
2441 obj_priv->gtt_offset);
2442 break;
2445 /* First try to find a free reg */
2446 avail = 0;
2447 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2448 reg = &dev_priv->fence_regs[i];
2449 if (!reg->obj)
2450 break;
2452 old_obj_priv = reg->obj->driver_private;
2453 if (!old_obj_priv->pin_count)
2454 avail++;
2457 /* None available, try to steal one or wait for a user to finish */
2458 if (i == dev_priv->num_fence_regs) {
2459 struct drm_gem_object *old_obj = NULL;
2461 if (avail == 0)
2462 return -ENOSPC;
2464 list_for_each_entry(old_obj_priv, &dev_priv->mm.fence_list,
2465 fence_list) {
2466 old_obj = old_obj_priv->obj;
2468 if (old_obj_priv->pin_count)
2469 continue;
2471 /* Take a reference, as otherwise the wait_rendering
2472 * below may cause the object to get freed out from
2473 * under us.
2475 drm_gem_object_reference(old_obj);
2477 /* i915 uses fences for GPU access to tiled buffers */
2478 if (IS_I965G(dev) || !old_obj_priv->active)
2479 break;
2481 /* This brings the object to the head of the LRU if it
2482 * had been written to. The only way this should
2483 * result in us waiting longer than the expected
2484 * optimal amount of time is if there was a
2485 * fence-using buffer later that was read-only.
2487 i915_gem_object_flush_gpu_write_domain(old_obj);
2488 ret = i915_gem_object_wait_rendering(old_obj);
2489 if (ret != 0) {
2490 drm_gem_object_unreference(old_obj);
2491 return ret;
2494 break;
2498 * Zap this virtual mapping so we can set up a fence again
2499 * for this object next time we need it.
2501 i915_gem_release_mmap(old_obj);
2503 i = old_obj_priv->fence_reg;
2504 reg = &dev_priv->fence_regs[i];
2506 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
2507 list_del_init(&old_obj_priv->fence_list);
2509 drm_gem_object_unreference(old_obj);
2512 obj_priv->fence_reg = i;
2513 list_add_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
2515 reg->obj = obj;
2517 if (IS_I965G(dev))
2518 i965_write_fence_reg(reg);
2519 else if (IS_I9XX(dev))
2520 i915_write_fence_reg(reg);
2521 else
2522 i830_write_fence_reg(reg);
2524 trace_i915_gem_object_get_fence(obj, i, obj_priv->tiling_mode);
2526 return 0;
2530 * i915_gem_clear_fence_reg - clear out fence register info
2531 * @obj: object to clear
2533 * Zeroes out the fence register itself and clears out the associated
2534 * data structures in dev_priv and obj_priv.
2536 static void
2537 i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2539 struct drm_device *dev = obj->dev;
2540 drm_i915_private_t *dev_priv = dev->dev_private;
2541 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2543 if (IS_I965G(dev))
2544 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
2545 else {
2546 uint32_t fence_reg;
2548 if (obj_priv->fence_reg < 8)
2549 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2550 else
2551 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2552 8) * 4;
2554 I915_WRITE(fence_reg, 0);
2557 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2558 obj_priv->fence_reg = I915_FENCE_REG_NONE;
2559 list_del_init(&obj_priv->fence_list);
2563 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2564 * to the buffer to finish, and then resets the fence register.
2565 * @obj: tiled object holding a fence register.
2567 * Zeroes out the fence register itself and clears out the associated
2568 * data structures in dev_priv and obj_priv.
2571 i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
2573 struct drm_device *dev = obj->dev;
2574 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2576 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2577 return 0;
2579 /* On the i915, GPU access to tiled buffers is via a fence,
2580 * therefore we must wait for any outstanding access to complete
2581 * before clearing the fence.
2583 if (!IS_I965G(dev)) {
2584 int ret;
2586 i915_gem_object_flush_gpu_write_domain(obj);
2587 i915_gem_object_flush_gtt_write_domain(obj);
2588 ret = i915_gem_object_wait_rendering(obj);
2589 if (ret != 0)
2590 return ret;
2593 i915_gem_clear_fence_reg (obj);
2595 return 0;
2599 * Finds free space in the GTT aperture and binds the object there.
2601 static int
2602 i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2604 struct drm_device *dev = obj->dev;
2605 drm_i915_private_t *dev_priv = dev->dev_private;
2606 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2607 struct drm_mm_node *free_space;
2608 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2609 int ret;
2611 if (obj_priv->madv != I915_MADV_WILLNEED) {
2612 DRM_ERROR("Attempting to bind a purgeable object\n");
2613 return -EINVAL;
2616 if (alignment == 0)
2617 alignment = i915_gem_get_gtt_alignment(obj);
2618 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
2619 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2620 return -EINVAL;
2623 /* If the object is bigger than the entire aperture, reject it early
2624 * before evicting everything in a vain attempt to find space.
2626 if (obj->size > dev->gtt_total) {
2627 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2628 return -E2BIG;
2631 search_free:
2632 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2633 obj->size, alignment, 0);
2634 if (free_space != NULL) {
2635 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2636 alignment);
2637 if (obj_priv->gtt_space != NULL) {
2638 obj_priv->gtt_space->private = obj;
2639 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2642 if (obj_priv->gtt_space == NULL) {
2643 /* If the gtt is empty and we're still having trouble
2644 * fitting our object in, we're out of memory.
2646 #if WATCH_LRU
2647 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2648 #endif
2649 ret = i915_gem_evict_something(dev, obj->size);
2650 if (ret)
2651 return ret;
2653 goto search_free;
2656 #if WATCH_BUF
2657 DRM_INFO("Binding object of size %zd at 0x%08x\n",
2658 obj->size, obj_priv->gtt_offset);
2659 #endif
2660 ret = i915_gem_object_get_pages(obj, gfpmask);
2661 if (ret) {
2662 drm_mm_put_block(obj_priv->gtt_space);
2663 obj_priv->gtt_space = NULL;
2665 if (ret == -ENOMEM) {
2666 /* first try to clear up some space from the GTT */
2667 ret = i915_gem_evict_something(dev, obj->size);
2668 if (ret) {
2669 /* now try to shrink everyone else */
2670 if (gfpmask) {
2671 gfpmask = 0;
2672 goto search_free;
2675 return ret;
2678 goto search_free;
2681 return ret;
2684 /* Create an AGP memory structure pointing at our pages, and bind it
2685 * into the GTT.
2687 obj_priv->agp_mem = drm_agp_bind_pages(dev,
2688 obj_priv->pages,
2689 obj->size >> PAGE_SHIFT,
2690 obj_priv->gtt_offset,
2691 obj_priv->agp_type);
2692 if (obj_priv->agp_mem == NULL) {
2693 i915_gem_object_put_pages(obj);
2694 drm_mm_put_block(obj_priv->gtt_space);
2695 obj_priv->gtt_space = NULL;
2697 ret = i915_gem_evict_something(dev, obj->size);
2698 if (ret)
2699 return ret;
2701 goto search_free;
2703 atomic_inc(&dev->gtt_count);
2704 atomic_add(obj->size, &dev->gtt_memory);
2706 /* Assert that the object is not currently in any GPU domain. As it
2707 * wasn't in the GTT, there shouldn't be any way it could have been in
2708 * a GPU cache
2710 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2711 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
2713 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
2715 return 0;
2718 void
2719 i915_gem_clflush_object(struct drm_gem_object *obj)
2721 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2723 /* If we don't have a page list set up, then we're not pinned
2724 * to GPU, and we can ignore the cache flush because it'll happen
2725 * again at bind time.
2727 if (obj_priv->pages == NULL)
2728 return;
2730 trace_i915_gem_object_clflush(obj);
2732 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
2735 /** Flushes any GPU write domain for the object if it's dirty. */
2736 static void
2737 i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2739 struct drm_device *dev = obj->dev;
2740 uint32_t seqno;
2741 uint32_t old_write_domain;
2743 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2744 return;
2746 /* Queue the GPU write cache flushing we need. */
2747 old_write_domain = obj->write_domain;
2748 i915_gem_flush(dev, 0, obj->write_domain);
2749 seqno = i915_add_request(dev, NULL, obj->write_domain);
2750 BUG_ON(obj->write_domain);
2751 i915_gem_object_move_to_active(obj, seqno);
2753 trace_i915_gem_object_change_domain(obj,
2754 obj->read_domains,
2755 old_write_domain);
2758 /** Flushes the GTT write domain for the object if it's dirty. */
2759 static void
2760 i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2762 uint32_t old_write_domain;
2764 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2765 return;
2767 /* No actual flushing is required for the GTT write domain. Writes
2768 * to it immediately go to main memory as far as we know, so there's
2769 * no chipset flush. It also doesn't land in render cache.
2771 old_write_domain = obj->write_domain;
2772 obj->write_domain = 0;
2774 trace_i915_gem_object_change_domain(obj,
2775 obj->read_domains,
2776 old_write_domain);
2779 /** Flushes the CPU write domain for the object if it's dirty. */
2780 static void
2781 i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2783 struct drm_device *dev = obj->dev;
2784 uint32_t old_write_domain;
2786 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2787 return;
2789 i915_gem_clflush_object(obj);
2790 drm_agp_chipset_flush(dev);
2791 old_write_domain = obj->write_domain;
2792 obj->write_domain = 0;
2794 trace_i915_gem_object_change_domain(obj,
2795 obj->read_domains,
2796 old_write_domain);
2800 * Moves a single object to the GTT read, and possibly write domain.
2802 * This function returns when the move is complete, including waiting on
2803 * flushes to occur.
2806 i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2808 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2809 uint32_t old_write_domain, old_read_domains;
2810 int ret;
2812 /* Not valid to be called on unbound objects. */
2813 if (obj_priv->gtt_space == NULL)
2814 return -EINVAL;
2816 i915_gem_object_flush_gpu_write_domain(obj);
2817 /* Wait on any GPU rendering and flushing to occur. */
2818 ret = i915_gem_object_wait_rendering(obj);
2819 if (ret != 0)
2820 return ret;
2822 old_write_domain = obj->write_domain;
2823 old_read_domains = obj->read_domains;
2825 /* If we're writing through the GTT domain, then CPU and GPU caches
2826 * will need to be invalidated at next use.
2828 if (write)
2829 obj->read_domains &= I915_GEM_DOMAIN_GTT;
2831 i915_gem_object_flush_cpu_write_domain(obj);
2833 /* It should now be out of any other write domains, and we can update
2834 * the domain values for our changes.
2836 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2837 obj->read_domains |= I915_GEM_DOMAIN_GTT;
2838 if (write) {
2839 obj->write_domain = I915_GEM_DOMAIN_GTT;
2840 obj_priv->dirty = 1;
2843 trace_i915_gem_object_change_domain(obj,
2844 old_read_domains,
2845 old_write_domain);
2847 return 0;
2851 * Prepare buffer for display plane. Use uninterruptible for possible flush
2852 * wait, as in modesetting process we're not supposed to be interrupted.
2855 i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
2857 struct drm_device *dev = obj->dev;
2858 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2859 uint32_t old_write_domain, old_read_domains;
2860 int ret;
2862 /* Not valid to be called on unbound objects. */
2863 if (obj_priv->gtt_space == NULL)
2864 return -EINVAL;
2866 i915_gem_object_flush_gpu_write_domain(obj);
2868 /* Wait on any GPU rendering and flushing to occur. */
2869 if (obj_priv->active) {
2870 #if WATCH_BUF
2871 DRM_INFO("%s: object %p wait for seqno %08x\n",
2872 __func__, obj, obj_priv->last_rendering_seqno);
2873 #endif
2874 ret = i915_do_wait_request(dev, obj_priv->last_rendering_seqno, 0);
2875 if (ret != 0)
2876 return ret;
2879 old_write_domain = obj->write_domain;
2880 old_read_domains = obj->read_domains;
2882 obj->read_domains &= I915_GEM_DOMAIN_GTT;
2884 i915_gem_object_flush_cpu_write_domain(obj);
2886 /* It should now be out of any other write domains, and we can update
2887 * the domain values for our changes.
2889 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2890 obj->read_domains |= I915_GEM_DOMAIN_GTT;
2891 obj->write_domain = I915_GEM_DOMAIN_GTT;
2892 obj_priv->dirty = 1;
2894 trace_i915_gem_object_change_domain(obj,
2895 old_read_domains,
2896 old_write_domain);
2898 return 0;
2902 * Moves a single object to the CPU read, and possibly write domain.
2904 * This function returns when the move is complete, including waiting on
2905 * flushes to occur.
2907 static int
2908 i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2910 uint32_t old_write_domain, old_read_domains;
2911 int ret;
2913 i915_gem_object_flush_gpu_write_domain(obj);
2914 /* Wait on any GPU rendering and flushing to occur. */
2915 ret = i915_gem_object_wait_rendering(obj);
2916 if (ret != 0)
2917 return ret;
2919 i915_gem_object_flush_gtt_write_domain(obj);
2921 /* If we have a partially-valid cache of the object in the CPU,
2922 * finish invalidating it and free the per-page flags.
2924 i915_gem_object_set_to_full_cpu_read_domain(obj);
2926 old_write_domain = obj->write_domain;
2927 old_read_domains = obj->read_domains;
2929 /* Flush the CPU cache if it's still invalid. */
2930 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2931 i915_gem_clflush_object(obj);
2933 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2936 /* It should now be out of any other write domains, and we can update
2937 * the domain values for our changes.
2939 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2941 /* If we're writing through the CPU, then the GPU read domains will
2942 * need to be invalidated at next use.
2944 if (write) {
2945 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2946 obj->write_domain = I915_GEM_DOMAIN_CPU;
2949 trace_i915_gem_object_change_domain(obj,
2950 old_read_domains,
2951 old_write_domain);
2953 return 0;
2957 * Set the next domain for the specified object. This
2958 * may not actually perform the necessary flushing/invaliding though,
2959 * as that may want to be batched with other set_domain operations
2961 * This is (we hope) the only really tricky part of gem. The goal
2962 * is fairly simple -- track which caches hold bits of the object
2963 * and make sure they remain coherent. A few concrete examples may
2964 * help to explain how it works. For shorthand, we use the notation
2965 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2966 * a pair of read and write domain masks.
2968 * Case 1: the batch buffer
2970 * 1. Allocated
2971 * 2. Written by CPU
2972 * 3. Mapped to GTT
2973 * 4. Read by GPU
2974 * 5. Unmapped from GTT
2975 * 6. Freed
2977 * Let's take these a step at a time
2979 * 1. Allocated
2980 * Pages allocated from the kernel may still have
2981 * cache contents, so we set them to (CPU, CPU) always.
2982 * 2. Written by CPU (using pwrite)
2983 * The pwrite function calls set_domain (CPU, CPU) and
2984 * this function does nothing (as nothing changes)
2985 * 3. Mapped by GTT
2986 * This function asserts that the object is not
2987 * currently in any GPU-based read or write domains
2988 * 4. Read by GPU
2989 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2990 * As write_domain is zero, this function adds in the
2991 * current read domains (CPU+COMMAND, 0).
2992 * flush_domains is set to CPU.
2993 * invalidate_domains is set to COMMAND
2994 * clflush is run to get data out of the CPU caches
2995 * then i915_dev_set_domain calls i915_gem_flush to
2996 * emit an MI_FLUSH and drm_agp_chipset_flush
2997 * 5. Unmapped from GTT
2998 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2999 * flush_domains and invalidate_domains end up both zero
3000 * so no flushing/invalidating happens
3001 * 6. Freed
3002 * yay, done
3004 * Case 2: The shared render buffer
3006 * 1. Allocated
3007 * 2. Mapped to GTT
3008 * 3. Read/written by GPU
3009 * 4. set_domain to (CPU,CPU)
3010 * 5. Read/written by CPU
3011 * 6. Read/written by GPU
3013 * 1. Allocated
3014 * Same as last example, (CPU, CPU)
3015 * 2. Mapped to GTT
3016 * Nothing changes (assertions find that it is not in the GPU)
3017 * 3. Read/written by GPU
3018 * execbuffer calls set_domain (RENDER, RENDER)
3019 * flush_domains gets CPU
3020 * invalidate_domains gets GPU
3021 * clflush (obj)
3022 * MI_FLUSH and drm_agp_chipset_flush
3023 * 4. set_domain (CPU, CPU)
3024 * flush_domains gets GPU
3025 * invalidate_domains gets CPU
3026 * wait_rendering (obj) to make sure all drawing is complete.
3027 * This will include an MI_FLUSH to get the data from GPU
3028 * to memory
3029 * clflush (obj) to invalidate the CPU cache
3030 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3031 * 5. Read/written by CPU
3032 * cache lines are loaded and dirtied
3033 * 6. Read written by GPU
3034 * Same as last GPU access
3036 * Case 3: The constant buffer
3038 * 1. Allocated
3039 * 2. Written by CPU
3040 * 3. Read by GPU
3041 * 4. Updated (written) by CPU again
3042 * 5. Read by GPU
3044 * 1. Allocated
3045 * (CPU, CPU)
3046 * 2. Written by CPU
3047 * (CPU, CPU)
3048 * 3. Read by GPU
3049 * (CPU+RENDER, 0)
3050 * flush_domains = CPU
3051 * invalidate_domains = RENDER
3052 * clflush (obj)
3053 * MI_FLUSH
3054 * drm_agp_chipset_flush
3055 * 4. Updated (written) by CPU again
3056 * (CPU, CPU)
3057 * flush_domains = 0 (no previous write domain)
3058 * invalidate_domains = 0 (no new read domains)
3059 * 5. Read by GPU
3060 * (CPU+RENDER, 0)
3061 * flush_domains = CPU
3062 * invalidate_domains = RENDER
3063 * clflush (obj)
3064 * MI_FLUSH
3065 * drm_agp_chipset_flush
3067 static void
3068 i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
3070 struct drm_device *dev = obj->dev;
3071 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3072 uint32_t invalidate_domains = 0;
3073 uint32_t flush_domains = 0;
3074 uint32_t old_read_domains;
3076 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
3077 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
3079 intel_mark_busy(dev, obj);
3081 #if WATCH_BUF
3082 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
3083 __func__, obj,
3084 obj->read_domains, obj->pending_read_domains,
3085 obj->write_domain, obj->pending_write_domain);
3086 #endif
3088 * If the object isn't moving to a new write domain,
3089 * let the object stay in multiple read domains
3091 if (obj->pending_write_domain == 0)
3092 obj->pending_read_domains |= obj->read_domains;
3093 else
3094 obj_priv->dirty = 1;
3097 * Flush the current write domain if
3098 * the new read domains don't match. Invalidate
3099 * any read domains which differ from the old
3100 * write domain
3102 if (obj->write_domain &&
3103 obj->write_domain != obj->pending_read_domains) {
3104 flush_domains |= obj->write_domain;
3105 invalidate_domains |=
3106 obj->pending_read_domains & ~obj->write_domain;
3109 * Invalidate any read caches which may have
3110 * stale data. That is, any new read domains.
3112 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
3113 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
3114 #if WATCH_BUF
3115 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
3116 __func__, flush_domains, invalidate_domains);
3117 #endif
3118 i915_gem_clflush_object(obj);
3121 old_read_domains = obj->read_domains;
3123 /* The actual obj->write_domain will be updated with
3124 * pending_write_domain after we emit the accumulated flush for all
3125 * of our domain changes in execbuffers (which clears objects'
3126 * write_domains). So if we have a current write domain that we
3127 * aren't changing, set pending_write_domain to that.
3129 if (flush_domains == 0 && obj->pending_write_domain == 0)
3130 obj->pending_write_domain = obj->write_domain;
3131 obj->read_domains = obj->pending_read_domains;
3133 dev->invalidate_domains |= invalidate_domains;
3134 dev->flush_domains |= flush_domains;
3135 #if WATCH_BUF
3136 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
3137 __func__,
3138 obj->read_domains, obj->write_domain,
3139 dev->invalidate_domains, dev->flush_domains);
3140 #endif
3142 trace_i915_gem_object_change_domain(obj,
3143 old_read_domains,
3144 obj->write_domain);
3148 * Moves the object from a partially CPU read to a full one.
3150 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3151 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3153 static void
3154 i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3156 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3158 if (!obj_priv->page_cpu_valid)
3159 return;
3161 /* If we're partially in the CPU read domain, finish moving it in.
3163 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3164 int i;
3166 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3167 if (obj_priv->page_cpu_valid[i])
3168 continue;
3169 drm_clflush_pages(obj_priv->pages + i, 1);
3173 /* Free the page_cpu_valid mappings which are now stale, whether
3174 * or not we've got I915_GEM_DOMAIN_CPU.
3176 kfree(obj_priv->page_cpu_valid);
3177 obj_priv->page_cpu_valid = NULL;
3181 * Set the CPU read domain on a range of the object.
3183 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3184 * not entirely valid. The page_cpu_valid member of the object flags which
3185 * pages have been flushed, and will be respected by
3186 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3187 * of the whole object.
3189 * This function returns when the move is complete, including waiting on
3190 * flushes to occur.
3192 static int
3193 i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3194 uint64_t offset, uint64_t size)
3196 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3197 uint32_t old_read_domains;
3198 int i, ret;
3200 if (offset == 0 && size == obj->size)
3201 return i915_gem_object_set_to_cpu_domain(obj, 0);
3203 i915_gem_object_flush_gpu_write_domain(obj);
3204 /* Wait on any GPU rendering and flushing to occur. */
3205 ret = i915_gem_object_wait_rendering(obj);
3206 if (ret != 0)
3207 return ret;
3208 i915_gem_object_flush_gtt_write_domain(obj);
3210 /* If we're already fully in the CPU read domain, we're done. */
3211 if (obj_priv->page_cpu_valid == NULL &&
3212 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
3213 return 0;
3215 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3216 * newly adding I915_GEM_DOMAIN_CPU
3218 if (obj_priv->page_cpu_valid == NULL) {
3219 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3220 GFP_KERNEL);
3221 if (obj_priv->page_cpu_valid == NULL)
3222 return -ENOMEM;
3223 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3224 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
3226 /* Flush the cache on any pages that are still invalid from the CPU's
3227 * perspective.
3229 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3230 i++) {
3231 if (obj_priv->page_cpu_valid[i])
3232 continue;
3234 drm_clflush_pages(obj_priv->pages + i, 1);
3236 obj_priv->page_cpu_valid[i] = 1;
3239 /* It should now be out of any other write domains, and we can update
3240 * the domain values for our changes.
3242 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3244 old_read_domains = obj->read_domains;
3245 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3247 trace_i915_gem_object_change_domain(obj,
3248 old_read_domains,
3249 obj->write_domain);
3251 return 0;
3255 * Pin an object to the GTT and evaluate the relocations landing in it.
3257 static int
3258 i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
3259 struct drm_file *file_priv,
3260 struct drm_i915_gem_exec_object *entry,
3261 struct drm_i915_gem_relocation_entry *relocs)
3263 struct drm_device *dev = obj->dev;
3264 drm_i915_private_t *dev_priv = dev->dev_private;
3265 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3266 int i, ret;
3267 void __iomem *reloc_page;
3269 /* Choose the GTT offset for our buffer and put it there. */
3270 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
3271 if (ret)
3272 return ret;
3274 entry->offset = obj_priv->gtt_offset;
3276 /* Apply the relocations, using the GTT aperture to avoid cache
3277 * flushing requirements.
3279 for (i = 0; i < entry->relocation_count; i++) {
3280 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
3281 struct drm_gem_object *target_obj;
3282 struct drm_i915_gem_object *target_obj_priv;
3283 uint32_t reloc_val, reloc_offset;
3284 uint32_t __iomem *reloc_entry;
3286 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
3287 reloc->target_handle);
3288 if (target_obj == NULL) {
3289 i915_gem_object_unpin(obj);
3290 return -EBADF;
3292 target_obj_priv = target_obj->driver_private;
3294 #if WATCH_RELOC
3295 DRM_INFO("%s: obj %p offset %08x target %d "
3296 "read %08x write %08x gtt %08x "
3297 "presumed %08x delta %08x\n",
3298 __func__,
3299 obj,
3300 (int) reloc->offset,
3301 (int) reloc->target_handle,
3302 (int) reloc->read_domains,
3303 (int) reloc->write_domain,
3304 (int) target_obj_priv->gtt_offset,
3305 (int) reloc->presumed_offset,
3306 reloc->delta);
3307 #endif
3309 /* The target buffer should have appeared before us in the
3310 * exec_object list, so it should have a GTT space bound by now.
3312 if (target_obj_priv->gtt_space == NULL) {
3313 DRM_ERROR("No GTT space found for object %d\n",
3314 reloc->target_handle);
3315 drm_gem_object_unreference(target_obj);
3316 i915_gem_object_unpin(obj);
3317 return -EINVAL;
3320 /* Validate that the target is in a valid r/w GPU domain */
3321 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3322 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3323 DRM_ERROR("reloc with read/write CPU domains: "
3324 "obj %p target %d offset %d "
3325 "read %08x write %08x",
3326 obj, reloc->target_handle,
3327 (int) reloc->offset,
3328 reloc->read_domains,
3329 reloc->write_domain);
3330 drm_gem_object_unreference(target_obj);
3331 i915_gem_object_unpin(obj);
3332 return -EINVAL;
3334 if (reloc->write_domain && target_obj->pending_write_domain &&
3335 reloc->write_domain != target_obj->pending_write_domain) {
3336 DRM_ERROR("Write domain conflict: "
3337 "obj %p target %d offset %d "
3338 "new %08x old %08x\n",
3339 obj, reloc->target_handle,
3340 (int) reloc->offset,
3341 reloc->write_domain,
3342 target_obj->pending_write_domain);
3343 drm_gem_object_unreference(target_obj);
3344 i915_gem_object_unpin(obj);
3345 return -EINVAL;
3348 target_obj->pending_read_domains |= reloc->read_domains;
3349 target_obj->pending_write_domain |= reloc->write_domain;
3351 /* If the relocation already has the right value in it, no
3352 * more work needs to be done.
3354 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
3355 drm_gem_object_unreference(target_obj);
3356 continue;
3359 /* Check that the relocation address is valid... */
3360 if (reloc->offset > obj->size - 4) {
3361 DRM_ERROR("Relocation beyond object bounds: "
3362 "obj %p target %d offset %d size %d.\n",
3363 obj, reloc->target_handle,
3364 (int) reloc->offset, (int) obj->size);
3365 drm_gem_object_unreference(target_obj);
3366 i915_gem_object_unpin(obj);
3367 return -EINVAL;
3369 if (reloc->offset & 3) {
3370 DRM_ERROR("Relocation not 4-byte aligned: "
3371 "obj %p target %d offset %d.\n",
3372 obj, reloc->target_handle,
3373 (int) reloc->offset);
3374 drm_gem_object_unreference(target_obj);
3375 i915_gem_object_unpin(obj);
3376 return -EINVAL;
3379 /* and points to somewhere within the target object. */
3380 if (reloc->delta >= target_obj->size) {
3381 DRM_ERROR("Relocation beyond target object bounds: "
3382 "obj %p target %d delta %d size %d.\n",
3383 obj, reloc->target_handle,
3384 (int) reloc->delta, (int) target_obj->size);
3385 drm_gem_object_unreference(target_obj);
3386 i915_gem_object_unpin(obj);
3387 return -EINVAL;
3390 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3391 if (ret != 0) {
3392 drm_gem_object_unreference(target_obj);
3393 i915_gem_object_unpin(obj);
3394 return -EINVAL;
3397 /* Map the page containing the relocation we're going to
3398 * perform.
3400 reloc_offset = obj_priv->gtt_offset + reloc->offset;
3401 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3402 (reloc_offset &
3403 ~(PAGE_SIZE - 1)));
3404 reloc_entry = (uint32_t __iomem *)(reloc_page +
3405 (reloc_offset & (PAGE_SIZE - 1)));
3406 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
3408 #if WATCH_BUF
3409 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
3410 obj, (unsigned int) reloc->offset,
3411 readl(reloc_entry), reloc_val);
3412 #endif
3413 writel(reloc_val, reloc_entry);
3414 io_mapping_unmap_atomic(reloc_page);
3416 /* The updated presumed offset for this entry will be
3417 * copied back out to the user.
3419 reloc->presumed_offset = target_obj_priv->gtt_offset;
3421 drm_gem_object_unreference(target_obj);
3424 #if WATCH_BUF
3425 if (0)
3426 i915_gem_dump_object(obj, 128, __func__, ~0);
3427 #endif
3428 return 0;
3431 /** Dispatch a batchbuffer to the ring
3433 static int
3434 i915_dispatch_gem_execbuffer(struct drm_device *dev,
3435 struct drm_i915_gem_execbuffer *exec,
3436 struct drm_clip_rect *cliprects,
3437 uint64_t exec_offset)
3439 drm_i915_private_t *dev_priv = dev->dev_private;
3440 int nbox = exec->num_cliprects;
3441 int i = 0, count;
3442 uint32_t exec_start, exec_len;
3443 RING_LOCALS;
3445 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3446 exec_len = (uint32_t) exec->batch_len;
3448 trace_i915_gem_request_submit(dev, dev_priv->mm.next_gem_seqno + 1);
3450 count = nbox ? nbox : 1;
3452 for (i = 0; i < count; i++) {
3453 if (i < nbox) {
3454 int ret = i915_emit_box(dev, cliprects, i,
3455 exec->DR1, exec->DR4);
3456 if (ret)
3457 return ret;
3460 if (IS_I830(dev) || IS_845G(dev)) {
3461 BEGIN_LP_RING(4);
3462 OUT_RING(MI_BATCH_BUFFER);
3463 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3464 OUT_RING(exec_start + exec_len - 4);
3465 OUT_RING(0);
3466 ADVANCE_LP_RING();
3467 } else {
3468 BEGIN_LP_RING(2);
3469 if (IS_I965G(dev)) {
3470 OUT_RING(MI_BATCH_BUFFER_START |
3471 (2 << 6) |
3472 MI_BATCH_NON_SECURE_I965);
3473 OUT_RING(exec_start);
3474 } else {
3475 OUT_RING(MI_BATCH_BUFFER_START |
3476 (2 << 6));
3477 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
3479 ADVANCE_LP_RING();
3483 /* XXX breadcrumb */
3484 return 0;
3487 /* Throttle our rendering by waiting until the ring has completed our requests
3488 * emitted over 20 msec ago.
3490 * Note that if we were to use the current jiffies each time around the loop,
3491 * we wouldn't escape the function with any frames outstanding if the time to
3492 * render a frame was over 20ms.
3494 * This should get us reasonable parallelism between CPU and GPU but also
3495 * relatively low latency when blocking on a particular request to finish.
3497 static int
3498 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
3500 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3501 int ret = 0;
3502 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3504 mutex_lock(&dev->struct_mutex);
3505 while (!list_empty(&i915_file_priv->mm.request_list)) {
3506 struct drm_i915_gem_request *request;
3508 request = list_first_entry(&i915_file_priv->mm.request_list,
3509 struct drm_i915_gem_request,
3510 client_list);
3512 if (time_after_eq(request->emitted_jiffies, recent_enough))
3513 break;
3515 ret = i915_wait_request(dev, request->seqno);
3516 if (ret != 0)
3517 break;
3519 mutex_unlock(&dev->struct_mutex);
3521 return ret;
3524 static int
3525 i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
3526 uint32_t buffer_count,
3527 struct drm_i915_gem_relocation_entry **relocs)
3529 uint32_t reloc_count = 0, reloc_index = 0, i;
3530 int ret;
3532 *relocs = NULL;
3533 for (i = 0; i < buffer_count; i++) {
3534 if (reloc_count + exec_list[i].relocation_count < reloc_count)
3535 return -EINVAL;
3536 reloc_count += exec_list[i].relocation_count;
3539 *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
3540 if (*relocs == NULL)
3541 return -ENOMEM;
3543 for (i = 0; i < buffer_count; i++) {
3544 struct drm_i915_gem_relocation_entry __user *user_relocs;
3546 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3548 ret = copy_from_user(&(*relocs)[reloc_index],
3549 user_relocs,
3550 exec_list[i].relocation_count *
3551 sizeof(**relocs));
3552 if (ret != 0) {
3553 drm_free_large(*relocs);
3554 *relocs = NULL;
3555 return -EFAULT;
3558 reloc_index += exec_list[i].relocation_count;
3561 return 0;
3564 static int
3565 i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3566 uint32_t buffer_count,
3567 struct drm_i915_gem_relocation_entry *relocs)
3569 uint32_t reloc_count = 0, i;
3570 int ret = 0;
3572 for (i = 0; i < buffer_count; i++) {
3573 struct drm_i915_gem_relocation_entry __user *user_relocs;
3574 int unwritten;
3576 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3578 unwritten = copy_to_user(user_relocs,
3579 &relocs[reloc_count],
3580 exec_list[i].relocation_count *
3581 sizeof(*relocs));
3583 if (unwritten) {
3584 ret = -EFAULT;
3585 goto err;
3588 reloc_count += exec_list[i].relocation_count;
3591 err:
3592 drm_free_large(relocs);
3594 return ret;
3597 static int
3598 i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer *exec,
3599 uint64_t exec_offset)
3601 uint32_t exec_start, exec_len;
3603 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3604 exec_len = (uint32_t) exec->batch_len;
3606 if ((exec_start | exec_len) & 0x7)
3607 return -EINVAL;
3609 if (!exec_start)
3610 return -EINVAL;
3612 return 0;
3616 i915_gem_execbuffer(struct drm_device *dev, void *data,
3617 struct drm_file *file_priv)
3619 drm_i915_private_t *dev_priv = dev->dev_private;
3620 struct drm_i915_gem_execbuffer *args = data;
3621 struct drm_i915_gem_exec_object *exec_list = NULL;
3622 struct drm_gem_object **object_list = NULL;
3623 struct drm_gem_object *batch_obj;
3624 struct drm_i915_gem_object *obj_priv;
3625 struct drm_clip_rect *cliprects = NULL;
3626 struct drm_i915_gem_relocation_entry *relocs;
3627 int ret, ret2, i, pinned = 0;
3628 uint64_t exec_offset;
3629 uint32_t seqno, flush_domains, reloc_index;
3630 int pin_tries;
3632 #if WATCH_EXEC
3633 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3634 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3635 #endif
3637 if (args->buffer_count < 1) {
3638 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3639 return -EINVAL;
3641 /* Copy in the exec list from userland */
3642 exec_list = drm_calloc_large(sizeof(*exec_list), args->buffer_count);
3643 object_list = drm_calloc_large(sizeof(*object_list), args->buffer_count);
3644 if (exec_list == NULL || object_list == NULL) {
3645 DRM_ERROR("Failed to allocate exec or object list "
3646 "for %d buffers\n",
3647 args->buffer_count);
3648 ret = -ENOMEM;
3649 goto pre_mutex_err;
3651 ret = copy_from_user(exec_list,
3652 (struct drm_i915_relocation_entry __user *)
3653 (uintptr_t) args->buffers_ptr,
3654 sizeof(*exec_list) * args->buffer_count);
3655 if (ret != 0) {
3656 DRM_ERROR("copy %d exec entries failed %d\n",
3657 args->buffer_count, ret);
3658 goto pre_mutex_err;
3661 if (args->num_cliprects != 0) {
3662 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3663 GFP_KERNEL);
3664 if (cliprects == NULL)
3665 goto pre_mutex_err;
3667 ret = copy_from_user(cliprects,
3668 (struct drm_clip_rect __user *)
3669 (uintptr_t) args->cliprects_ptr,
3670 sizeof(*cliprects) * args->num_cliprects);
3671 if (ret != 0) {
3672 DRM_ERROR("copy %d cliprects failed: %d\n",
3673 args->num_cliprects, ret);
3674 ret = -EFAULT;
3675 goto pre_mutex_err;
3679 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3680 &relocs);
3681 if (ret != 0)
3682 goto pre_mutex_err;
3684 mutex_lock(&dev->struct_mutex);
3686 i915_verify_inactive(dev, __FILE__, __LINE__);
3688 if (atomic_read(&dev_priv->mm.wedged)) {
3689 DRM_ERROR("Execbuf while wedged\n");
3690 mutex_unlock(&dev->struct_mutex);
3691 ret = -EIO;
3692 goto pre_mutex_err;
3695 if (dev_priv->mm.suspended) {
3696 DRM_ERROR("Execbuf while VT-switched.\n");
3697 mutex_unlock(&dev->struct_mutex);
3698 ret = -EBUSY;
3699 goto pre_mutex_err;
3702 /* Look up object handles */
3703 for (i = 0; i < args->buffer_count; i++) {
3704 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3705 exec_list[i].handle);
3706 if (object_list[i] == NULL) {
3707 DRM_ERROR("Invalid object handle %d at index %d\n",
3708 exec_list[i].handle, i);
3709 ret = -EBADF;
3710 goto err;
3713 obj_priv = object_list[i]->driver_private;
3714 if (obj_priv->in_execbuffer) {
3715 DRM_ERROR("Object %p appears more than once in object list\n",
3716 object_list[i]);
3717 ret = -EBADF;
3718 goto err;
3720 obj_priv->in_execbuffer = true;
3723 /* Pin and relocate */
3724 for (pin_tries = 0; ; pin_tries++) {
3725 ret = 0;
3726 reloc_index = 0;
3728 for (i = 0; i < args->buffer_count; i++) {
3729 object_list[i]->pending_read_domains = 0;
3730 object_list[i]->pending_write_domain = 0;
3731 ret = i915_gem_object_pin_and_relocate(object_list[i],
3732 file_priv,
3733 &exec_list[i],
3734 &relocs[reloc_index]);
3735 if (ret)
3736 break;
3737 pinned = i + 1;
3738 reloc_index += exec_list[i].relocation_count;
3740 /* success */
3741 if (ret == 0)
3742 break;
3744 /* error other than GTT full, or we've already tried again */
3745 if (ret != -ENOSPC || pin_tries >= 1) {
3746 if (ret != -ERESTARTSYS) {
3747 unsigned long long total_size = 0;
3748 for (i = 0; i < args->buffer_count; i++)
3749 total_size += object_list[i]->size;
3750 DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes: %d\n",
3751 pinned+1, args->buffer_count,
3752 total_size, ret);
3753 DRM_ERROR("%d objects [%d pinned], "
3754 "%d object bytes [%d pinned], "
3755 "%d/%d gtt bytes\n",
3756 atomic_read(&dev->object_count),
3757 atomic_read(&dev->pin_count),
3758 atomic_read(&dev->object_memory),
3759 atomic_read(&dev->pin_memory),
3760 atomic_read(&dev->gtt_memory),
3761 dev->gtt_total);
3763 goto err;
3766 /* unpin all of our buffers */
3767 for (i = 0; i < pinned; i++)
3768 i915_gem_object_unpin(object_list[i]);
3769 pinned = 0;
3771 /* evict everyone we can from the aperture */
3772 ret = i915_gem_evict_everything(dev);
3773 if (ret && ret != -ENOSPC)
3774 goto err;
3777 /* Set the pending read domains for the batch buffer to COMMAND */
3778 batch_obj = object_list[args->buffer_count-1];
3779 if (batch_obj->pending_write_domain) {
3780 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3781 ret = -EINVAL;
3782 goto err;
3784 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
3786 /* Sanity check the batch buffer, prior to moving objects */
3787 exec_offset = exec_list[args->buffer_count - 1].offset;
3788 ret = i915_gem_check_execbuffer (args, exec_offset);
3789 if (ret != 0) {
3790 DRM_ERROR("execbuf with invalid offset/length\n");
3791 goto err;
3794 i915_verify_inactive(dev, __FILE__, __LINE__);
3796 /* Zero the global flush/invalidate flags. These
3797 * will be modified as new domains are computed
3798 * for each object
3800 dev->invalidate_domains = 0;
3801 dev->flush_domains = 0;
3803 for (i = 0; i < args->buffer_count; i++) {
3804 struct drm_gem_object *obj = object_list[i];
3806 /* Compute new gpu domains and update invalidate/flush */
3807 i915_gem_object_set_to_gpu_domain(obj);
3810 i915_verify_inactive(dev, __FILE__, __LINE__);
3812 if (dev->invalidate_domains | dev->flush_domains) {
3813 #if WATCH_EXEC
3814 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3815 __func__,
3816 dev->invalidate_domains,
3817 dev->flush_domains);
3818 #endif
3819 i915_gem_flush(dev,
3820 dev->invalidate_domains,
3821 dev->flush_domains);
3822 if (dev->flush_domains & I915_GEM_GPU_DOMAINS)
3823 (void)i915_add_request(dev, file_priv,
3824 dev->flush_domains);
3827 for (i = 0; i < args->buffer_count; i++) {
3828 struct drm_gem_object *obj = object_list[i];
3829 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3830 uint32_t old_write_domain = obj->write_domain;
3832 obj->write_domain = obj->pending_write_domain;
3833 if (obj->write_domain)
3834 list_move_tail(&obj_priv->gpu_write_list,
3835 &dev_priv->mm.gpu_write_list);
3836 else
3837 list_del_init(&obj_priv->gpu_write_list);
3839 trace_i915_gem_object_change_domain(obj,
3840 obj->read_domains,
3841 old_write_domain);
3844 i915_verify_inactive(dev, __FILE__, __LINE__);
3846 #if WATCH_COHERENCY
3847 for (i = 0; i < args->buffer_count; i++) {
3848 i915_gem_object_check_coherency(object_list[i],
3849 exec_list[i].handle);
3851 #endif
3853 #if WATCH_EXEC
3854 i915_gem_dump_object(batch_obj,
3855 args->batch_len,
3856 __func__,
3857 ~0);
3858 #endif
3860 /* Exec the batchbuffer */
3861 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
3862 if (ret) {
3863 DRM_ERROR("dispatch failed %d\n", ret);
3864 goto err;
3868 * Ensure that the commands in the batch buffer are
3869 * finished before the interrupt fires
3871 flush_domains = i915_retire_commands(dev);
3873 i915_verify_inactive(dev, __FILE__, __LINE__);
3876 * Get a seqno representing the execution of the current buffer,
3877 * which we can wait on. We would like to mitigate these interrupts,
3878 * likely by only creating seqnos occasionally (so that we have
3879 * *some* interrupts representing completion of buffers that we can
3880 * wait on when trying to clear up gtt space).
3882 seqno = i915_add_request(dev, file_priv, flush_domains);
3883 BUG_ON(seqno == 0);
3884 for (i = 0; i < args->buffer_count; i++) {
3885 struct drm_gem_object *obj = object_list[i];
3887 i915_gem_object_move_to_active(obj, seqno);
3888 #if WATCH_LRU
3889 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3890 #endif
3892 #if WATCH_LRU
3893 i915_dump_lru(dev, __func__);
3894 #endif
3896 i915_verify_inactive(dev, __FILE__, __LINE__);
3898 err:
3899 for (i = 0; i < pinned; i++)
3900 i915_gem_object_unpin(object_list[i]);
3902 for (i = 0; i < args->buffer_count; i++) {
3903 if (object_list[i]) {
3904 obj_priv = object_list[i]->driver_private;
3905 obj_priv->in_execbuffer = false;
3907 drm_gem_object_unreference(object_list[i]);
3910 mutex_unlock(&dev->struct_mutex);
3912 if (!ret) {
3913 /* Copy the new buffer offsets back to the user's exec list. */
3914 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3915 (uintptr_t) args->buffers_ptr,
3916 exec_list,
3917 sizeof(*exec_list) * args->buffer_count);
3918 if (ret) {
3919 ret = -EFAULT;
3920 DRM_ERROR("failed to copy %d exec entries "
3921 "back to user (%d)\n",
3922 args->buffer_count, ret);
3926 /* Copy the updated relocations out regardless of current error
3927 * state. Failure to update the relocs would mean that the next
3928 * time userland calls execbuf, it would do so with presumed offset
3929 * state that didn't match the actual object state.
3931 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3932 relocs);
3933 if (ret2 != 0) {
3934 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3936 if (ret == 0)
3937 ret = ret2;
3940 pre_mutex_err:
3941 drm_free_large(object_list);
3942 drm_free_large(exec_list);
3943 kfree(cliprects);
3945 return ret;
3949 i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3951 struct drm_device *dev = obj->dev;
3952 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3953 int ret;
3955 i915_verify_inactive(dev, __FILE__, __LINE__);
3957 if (obj_priv->gtt_space != NULL) {
3958 if (alignment == 0)
3959 alignment = i915_gem_get_gtt_alignment(obj);
3960 if (obj_priv->gtt_offset & (alignment - 1)) {
3961 ret = i915_gem_object_unbind(obj);
3962 if (ret)
3963 return ret;
3967 if (obj_priv->gtt_space == NULL) {
3968 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3969 if (ret)
3970 return ret;
3973 * Pre-965 chips need a fence register set up in order to
3974 * properly handle tiled surfaces.
3976 if (!IS_I965G(dev) && obj_priv->tiling_mode != I915_TILING_NONE) {
3977 ret = i915_gem_object_get_fence_reg(obj);
3978 if (ret != 0) {
3979 if (ret != -EBUSY && ret != -ERESTARTSYS)
3980 DRM_ERROR("Failure to install fence: %d\n",
3981 ret);
3982 return ret;
3985 obj_priv->pin_count++;
3987 /* If the object is not active and not pending a flush,
3988 * remove it from the inactive list
3990 if (obj_priv->pin_count == 1) {
3991 atomic_inc(&dev->pin_count);
3992 atomic_add(obj->size, &dev->pin_memory);
3993 if (!obj_priv->active &&
3994 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
3995 !list_empty(&obj_priv->list))
3996 list_del_init(&obj_priv->list);
3998 i915_verify_inactive(dev, __FILE__, __LINE__);
4000 return 0;
4003 void
4004 i915_gem_object_unpin(struct drm_gem_object *obj)
4006 struct drm_device *dev = obj->dev;
4007 drm_i915_private_t *dev_priv = dev->dev_private;
4008 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4010 i915_verify_inactive(dev, __FILE__, __LINE__);
4011 obj_priv->pin_count--;
4012 BUG_ON(obj_priv->pin_count < 0);
4013 BUG_ON(obj_priv->gtt_space == NULL);
4015 /* If the object is no longer pinned, and is
4016 * neither active nor being flushed, then stick it on
4017 * the inactive list
4019 if (obj_priv->pin_count == 0) {
4020 if (!obj_priv->active &&
4021 (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
4022 list_move_tail(&obj_priv->list,
4023 &dev_priv->mm.inactive_list);
4024 atomic_dec(&dev->pin_count);
4025 atomic_sub(obj->size, &dev->pin_memory);
4027 i915_verify_inactive(dev, __FILE__, __LINE__);
4031 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4032 struct drm_file *file_priv)
4034 struct drm_i915_gem_pin *args = data;
4035 struct drm_gem_object *obj;
4036 struct drm_i915_gem_object *obj_priv;
4037 int ret;
4039 mutex_lock(&dev->struct_mutex);
4041 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4042 if (obj == NULL) {
4043 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
4044 args->handle);
4045 mutex_unlock(&dev->struct_mutex);
4046 return -EBADF;
4048 obj_priv = obj->driver_private;
4050 if (obj_priv->madv != I915_MADV_WILLNEED) {
4051 DRM_ERROR("Attempting to pin a purgeable buffer\n");
4052 drm_gem_object_unreference(obj);
4053 mutex_unlock(&dev->struct_mutex);
4054 return -EINVAL;
4057 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4058 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4059 args->handle);
4060 drm_gem_object_unreference(obj);
4061 mutex_unlock(&dev->struct_mutex);
4062 return -EINVAL;
4065 obj_priv->user_pin_count++;
4066 obj_priv->pin_filp = file_priv;
4067 if (obj_priv->user_pin_count == 1) {
4068 ret = i915_gem_object_pin(obj, args->alignment);
4069 if (ret != 0) {
4070 drm_gem_object_unreference(obj);
4071 mutex_unlock(&dev->struct_mutex);
4072 return ret;
4076 /* XXX - flush the CPU caches for pinned objects
4077 * as the X server doesn't manage domains yet
4079 i915_gem_object_flush_cpu_write_domain(obj);
4080 args->offset = obj_priv->gtt_offset;
4081 drm_gem_object_unreference(obj);
4082 mutex_unlock(&dev->struct_mutex);
4084 return 0;
4088 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4089 struct drm_file *file_priv)
4091 struct drm_i915_gem_pin *args = data;
4092 struct drm_gem_object *obj;
4093 struct drm_i915_gem_object *obj_priv;
4095 mutex_lock(&dev->struct_mutex);
4097 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4098 if (obj == NULL) {
4099 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
4100 args->handle);
4101 mutex_unlock(&dev->struct_mutex);
4102 return -EBADF;
4105 obj_priv = obj->driver_private;
4106 if (obj_priv->pin_filp != file_priv) {
4107 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4108 args->handle);
4109 drm_gem_object_unreference(obj);
4110 mutex_unlock(&dev->struct_mutex);
4111 return -EINVAL;
4113 obj_priv->user_pin_count--;
4114 if (obj_priv->user_pin_count == 0) {
4115 obj_priv->pin_filp = NULL;
4116 i915_gem_object_unpin(obj);
4119 drm_gem_object_unreference(obj);
4120 mutex_unlock(&dev->struct_mutex);
4121 return 0;
4125 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4126 struct drm_file *file_priv)
4128 struct drm_i915_gem_busy *args = data;
4129 struct drm_gem_object *obj;
4130 struct drm_i915_gem_object *obj_priv;
4132 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4133 if (obj == NULL) {
4134 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
4135 args->handle);
4136 return -EBADF;
4139 mutex_lock(&dev->struct_mutex);
4140 /* Update the active list for the hardware's current position.
4141 * Otherwise this only updates on a delayed timer or when irqs are
4142 * actually unmasked, and our working set ends up being larger than
4143 * required.
4145 i915_gem_retire_requests(dev);
4147 obj_priv = obj->driver_private;
4148 /* Don't count being on the flushing list against the object being
4149 * done. Otherwise, a buffer left on the flushing list but not getting
4150 * flushed (because nobody's flushing that domain) won't ever return
4151 * unbusy and get reused by libdrm's bo cache. The other expected
4152 * consumer of this interface, OpenGL's occlusion queries, also specs
4153 * that the objects get unbusy "eventually" without any interference.
4155 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
4157 drm_gem_object_unreference(obj);
4158 mutex_unlock(&dev->struct_mutex);
4159 return 0;
4163 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4164 struct drm_file *file_priv)
4166 return i915_gem_ring_throttle(dev, file_priv);
4170 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4171 struct drm_file *file_priv)
4173 struct drm_i915_gem_madvise *args = data;
4174 struct drm_gem_object *obj;
4175 struct drm_i915_gem_object *obj_priv;
4177 switch (args->madv) {
4178 case I915_MADV_DONTNEED:
4179 case I915_MADV_WILLNEED:
4180 break;
4181 default:
4182 return -EINVAL;
4185 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4186 if (obj == NULL) {
4187 DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
4188 args->handle);
4189 return -EBADF;
4192 mutex_lock(&dev->struct_mutex);
4193 obj_priv = obj->driver_private;
4195 if (obj_priv->pin_count) {
4196 drm_gem_object_unreference(obj);
4197 mutex_unlock(&dev->struct_mutex);
4199 DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
4200 return -EINVAL;
4203 if (obj_priv->madv != __I915_MADV_PURGED)
4204 obj_priv->madv = args->madv;
4206 /* if the object is no longer bound, discard its backing storage */
4207 if (i915_gem_object_is_purgeable(obj_priv) &&
4208 obj_priv->gtt_space == NULL)
4209 i915_gem_object_truncate(obj);
4211 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4213 drm_gem_object_unreference(obj);
4214 mutex_unlock(&dev->struct_mutex);
4216 return 0;
4219 int i915_gem_init_object(struct drm_gem_object *obj)
4221 struct drm_i915_gem_object *obj_priv;
4223 obj_priv = kzalloc(sizeof(*obj_priv), GFP_KERNEL);
4224 if (obj_priv == NULL)
4225 return -ENOMEM;
4228 * We've just allocated pages from the kernel,
4229 * so they've just been written by the CPU with
4230 * zeros. They'll need to be clflushed before we
4231 * use them with the GPU.
4233 obj->write_domain = I915_GEM_DOMAIN_CPU;
4234 obj->read_domains = I915_GEM_DOMAIN_CPU;
4236 obj_priv->agp_type = AGP_USER_MEMORY;
4238 obj->driver_private = obj_priv;
4239 obj_priv->obj = obj;
4240 obj_priv->fence_reg = I915_FENCE_REG_NONE;
4241 INIT_LIST_HEAD(&obj_priv->list);
4242 INIT_LIST_HEAD(&obj_priv->gpu_write_list);
4243 INIT_LIST_HEAD(&obj_priv->fence_list);
4244 obj_priv->madv = I915_MADV_WILLNEED;
4246 trace_i915_gem_object_create(obj);
4248 return 0;
4251 void i915_gem_free_object(struct drm_gem_object *obj)
4253 struct drm_device *dev = obj->dev;
4254 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4256 trace_i915_gem_object_destroy(obj);
4258 while (obj_priv->pin_count > 0)
4259 i915_gem_object_unpin(obj);
4261 if (obj_priv->phys_obj)
4262 i915_gem_detach_phys_object(dev, obj);
4264 i915_gem_object_unbind(obj);
4266 if (obj_priv->mmap_offset)
4267 i915_gem_free_mmap_offset(obj);
4269 kfree(obj_priv->page_cpu_valid);
4270 kfree(obj_priv->bit_17);
4271 kfree(obj->driver_private);
4274 /** Unbinds all inactive objects. */
4275 static int
4276 i915_gem_evict_from_inactive_list(struct drm_device *dev)
4278 drm_i915_private_t *dev_priv = dev->dev_private;
4280 while (!list_empty(&dev_priv->mm.inactive_list)) {
4281 struct drm_gem_object *obj;
4282 int ret;
4284 obj = list_first_entry(&dev_priv->mm.inactive_list,
4285 struct drm_i915_gem_object,
4286 list)->obj;
4288 ret = i915_gem_object_unbind(obj);
4289 if (ret != 0) {
4290 DRM_ERROR("Error unbinding object: %d\n", ret);
4291 return ret;
4295 return 0;
4299 i915_gem_idle(struct drm_device *dev)
4301 drm_i915_private_t *dev_priv = dev->dev_private;
4302 uint32_t seqno, cur_seqno, last_seqno;
4303 int stuck, ret;
4305 mutex_lock(&dev->struct_mutex);
4307 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
4308 mutex_unlock(&dev->struct_mutex);
4309 return 0;
4312 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4313 * We need to replace this with a semaphore, or something.
4315 dev_priv->mm.suspended = 1;
4316 del_timer(&dev_priv->hangcheck_timer);
4318 /* Cancel the retire work handler, wait for it to finish if running
4320 mutex_unlock(&dev->struct_mutex);
4321 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4322 mutex_lock(&dev->struct_mutex);
4324 i915_kernel_lost_context(dev);
4326 /* Flush the GPU along with all non-CPU write domains
4328 i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
4329 seqno = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS);
4331 if (seqno == 0) {
4332 mutex_unlock(&dev->struct_mutex);
4333 return -ENOMEM;
4336 dev_priv->mm.waiting_gem_seqno = seqno;
4337 last_seqno = 0;
4338 stuck = 0;
4339 for (;;) {
4340 cur_seqno = i915_get_gem_seqno(dev);
4341 if (i915_seqno_passed(cur_seqno, seqno))
4342 break;
4343 if (last_seqno == cur_seqno) {
4344 if (stuck++ > 100) {
4345 DRM_ERROR("hardware wedged\n");
4346 atomic_set(&dev_priv->mm.wedged, 1);
4347 DRM_WAKEUP(&dev_priv->irq_queue);
4348 break;
4351 msleep(10);
4352 last_seqno = cur_seqno;
4354 dev_priv->mm.waiting_gem_seqno = 0;
4356 i915_gem_retire_requests(dev);
4358 spin_lock(&dev_priv->mm.active_list_lock);
4359 if (!atomic_read(&dev_priv->mm.wedged)) {
4360 /* Active and flushing should now be empty as we've
4361 * waited for a sequence higher than any pending execbuffer
4363 WARN_ON(!list_empty(&dev_priv->mm.active_list));
4364 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
4365 /* Request should now be empty as we've also waited
4366 * for the last request in the list
4368 WARN_ON(!list_empty(&dev_priv->mm.request_list));
4371 /* Empty the active and flushing lists to inactive. If there's
4372 * anything left at this point, it means that we're wedged and
4373 * nothing good's going to happen by leaving them there. So strip
4374 * the GPU domains and just stuff them onto inactive.
4376 while (!list_empty(&dev_priv->mm.active_list)) {
4377 struct drm_gem_object *obj;
4378 uint32_t old_write_domain;
4380 obj = list_first_entry(&dev_priv->mm.active_list,
4381 struct drm_i915_gem_object,
4382 list)->obj;
4383 old_write_domain = obj->write_domain;
4384 obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
4385 i915_gem_object_move_to_inactive(obj);
4387 trace_i915_gem_object_change_domain(obj,
4388 obj->read_domains,
4389 old_write_domain);
4391 spin_unlock(&dev_priv->mm.active_list_lock);
4393 while (!list_empty(&dev_priv->mm.flushing_list)) {
4394 struct drm_gem_object *obj;
4395 uint32_t old_write_domain;
4397 obj = list_first_entry(&dev_priv->mm.flushing_list,
4398 struct drm_i915_gem_object,
4399 list)->obj;
4400 old_write_domain = obj->write_domain;
4401 obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
4402 i915_gem_object_move_to_inactive(obj);
4404 trace_i915_gem_object_change_domain(obj,
4405 obj->read_domains,
4406 old_write_domain);
4410 /* Move all inactive buffers out of the GTT. */
4411 ret = i915_gem_evict_from_inactive_list(dev);
4412 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
4413 if (ret) {
4414 mutex_unlock(&dev->struct_mutex);
4415 return ret;
4418 i915_gem_cleanup_ringbuffer(dev);
4419 mutex_unlock(&dev->struct_mutex);
4421 return 0;
4424 static int
4425 i915_gem_init_hws(struct drm_device *dev)
4427 drm_i915_private_t *dev_priv = dev->dev_private;
4428 struct drm_gem_object *obj;
4429 struct drm_i915_gem_object *obj_priv;
4430 int ret;
4432 /* If we need a physical address for the status page, it's already
4433 * initialized at driver load time.
4435 if (!I915_NEED_GFX_HWS(dev))
4436 return 0;
4438 obj = drm_gem_object_alloc(dev, 4096);
4439 if (obj == NULL) {
4440 DRM_ERROR("Failed to allocate status page\n");
4441 return -ENOMEM;
4443 obj_priv = obj->driver_private;
4444 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4446 ret = i915_gem_object_pin(obj, 4096);
4447 if (ret != 0) {
4448 drm_gem_object_unreference(obj);
4449 return ret;
4452 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
4454 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
4455 if (dev_priv->hw_status_page == NULL) {
4456 DRM_ERROR("Failed to map status page.\n");
4457 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
4458 i915_gem_object_unpin(obj);
4459 drm_gem_object_unreference(obj);
4460 return -EINVAL;
4462 dev_priv->hws_obj = obj;
4463 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
4464 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
4465 I915_READ(HWS_PGA); /* posting read */
4466 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
4468 return 0;
4471 static void
4472 i915_gem_cleanup_hws(struct drm_device *dev)
4474 drm_i915_private_t *dev_priv = dev->dev_private;
4475 struct drm_gem_object *obj;
4476 struct drm_i915_gem_object *obj_priv;
4478 if (dev_priv->hws_obj == NULL)
4479 return;
4481 obj = dev_priv->hws_obj;
4482 obj_priv = obj->driver_private;
4484 kunmap(obj_priv->pages[0]);
4485 i915_gem_object_unpin(obj);
4486 drm_gem_object_unreference(obj);
4487 dev_priv->hws_obj = NULL;
4489 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
4490 dev_priv->hw_status_page = NULL;
4492 /* Write high address into HWS_PGA when disabling. */
4493 I915_WRITE(HWS_PGA, 0x1ffff000);
4497 i915_gem_init_ringbuffer(struct drm_device *dev)
4499 drm_i915_private_t *dev_priv = dev->dev_private;
4500 struct drm_gem_object *obj;
4501 struct drm_i915_gem_object *obj_priv;
4502 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
4503 int ret;
4504 u32 head;
4506 ret = i915_gem_init_hws(dev);
4507 if (ret != 0)
4508 return ret;
4510 obj = drm_gem_object_alloc(dev, 128 * 1024);
4511 if (obj == NULL) {
4512 DRM_ERROR("Failed to allocate ringbuffer\n");
4513 i915_gem_cleanup_hws(dev);
4514 return -ENOMEM;
4516 obj_priv = obj->driver_private;
4518 ret = i915_gem_object_pin(obj, 4096);
4519 if (ret != 0) {
4520 drm_gem_object_unreference(obj);
4521 i915_gem_cleanup_hws(dev);
4522 return ret;
4525 /* Set up the kernel mapping for the ring. */
4526 ring->Size = obj->size;
4528 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
4529 ring->map.size = obj->size;
4530 ring->map.type = 0;
4531 ring->map.flags = 0;
4532 ring->map.mtrr = 0;
4534 drm_core_ioremap_wc(&ring->map, dev);
4535 if (ring->map.handle == NULL) {
4536 DRM_ERROR("Failed to map ringbuffer.\n");
4537 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4538 i915_gem_object_unpin(obj);
4539 drm_gem_object_unreference(obj);
4540 i915_gem_cleanup_hws(dev);
4541 return -EINVAL;
4543 ring->ring_obj = obj;
4544 ring->virtual_start = ring->map.handle;
4546 /* Stop the ring if it's running. */
4547 I915_WRITE(PRB0_CTL, 0);
4548 I915_WRITE(PRB0_TAIL, 0);
4549 I915_WRITE(PRB0_HEAD, 0);
4551 /* Initialize the ring. */
4552 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
4553 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4555 /* G45 ring initialization fails to reset head to zero */
4556 if (head != 0) {
4557 DRM_ERROR("Ring head not reset to zero "
4558 "ctl %08x head %08x tail %08x start %08x\n",
4559 I915_READ(PRB0_CTL),
4560 I915_READ(PRB0_HEAD),
4561 I915_READ(PRB0_TAIL),
4562 I915_READ(PRB0_START));
4563 I915_WRITE(PRB0_HEAD, 0);
4565 DRM_ERROR("Ring head forced to zero "
4566 "ctl %08x head %08x tail %08x start %08x\n",
4567 I915_READ(PRB0_CTL),
4568 I915_READ(PRB0_HEAD),
4569 I915_READ(PRB0_TAIL),
4570 I915_READ(PRB0_START));
4573 I915_WRITE(PRB0_CTL,
4574 ((obj->size - 4096) & RING_NR_PAGES) |
4575 RING_NO_REPORT |
4576 RING_VALID);
4578 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4580 /* If the head is still not zero, the ring is dead */
4581 if (head != 0) {
4582 DRM_ERROR("Ring initialization failed "
4583 "ctl %08x head %08x tail %08x start %08x\n",
4584 I915_READ(PRB0_CTL),
4585 I915_READ(PRB0_HEAD),
4586 I915_READ(PRB0_TAIL),
4587 I915_READ(PRB0_START));
4588 return -EIO;
4591 /* Update our cache of the ring state */
4592 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4593 i915_kernel_lost_context(dev);
4594 else {
4595 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
4596 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
4597 ring->space = ring->head - (ring->tail + 8);
4598 if (ring->space < 0)
4599 ring->space += ring->Size;
4602 return 0;
4605 void
4606 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4608 drm_i915_private_t *dev_priv = dev->dev_private;
4610 if (dev_priv->ring.ring_obj == NULL)
4611 return;
4613 drm_core_ioremapfree(&dev_priv->ring.map, dev);
4615 i915_gem_object_unpin(dev_priv->ring.ring_obj);
4616 drm_gem_object_unreference(dev_priv->ring.ring_obj);
4617 dev_priv->ring.ring_obj = NULL;
4618 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
4620 i915_gem_cleanup_hws(dev);
4624 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4625 struct drm_file *file_priv)
4627 drm_i915_private_t *dev_priv = dev->dev_private;
4628 int ret;
4630 if (drm_core_check_feature(dev, DRIVER_MODESET))
4631 return 0;
4633 if (atomic_read(&dev_priv->mm.wedged)) {
4634 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4635 atomic_set(&dev_priv->mm.wedged, 0);
4638 mutex_lock(&dev->struct_mutex);
4639 dev_priv->mm.suspended = 0;
4641 ret = i915_gem_init_ringbuffer(dev);
4642 if (ret != 0) {
4643 mutex_unlock(&dev->struct_mutex);
4644 return ret;
4647 spin_lock(&dev_priv->mm.active_list_lock);
4648 BUG_ON(!list_empty(&dev_priv->mm.active_list));
4649 spin_unlock(&dev_priv->mm.active_list_lock);
4651 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4652 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4653 BUG_ON(!list_empty(&dev_priv->mm.request_list));
4654 mutex_unlock(&dev->struct_mutex);
4656 drm_irq_install(dev);
4658 return 0;
4662 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4663 struct drm_file *file_priv)
4665 if (drm_core_check_feature(dev, DRIVER_MODESET))
4666 return 0;
4668 drm_irq_uninstall(dev);
4669 return i915_gem_idle(dev);
4672 void
4673 i915_gem_lastclose(struct drm_device *dev)
4675 int ret;
4677 if (drm_core_check_feature(dev, DRIVER_MODESET))
4678 return;
4680 ret = i915_gem_idle(dev);
4681 if (ret)
4682 DRM_ERROR("failed to idle hardware: %d\n", ret);
4685 void
4686 i915_gem_load(struct drm_device *dev)
4688 int i;
4689 drm_i915_private_t *dev_priv = dev->dev_private;
4691 spin_lock_init(&dev_priv->mm.active_list_lock);
4692 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4693 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4694 INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
4695 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4696 INIT_LIST_HEAD(&dev_priv->mm.request_list);
4697 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4698 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4699 i915_gem_retire_work_handler);
4700 dev_priv->mm.next_gem_seqno = 1;
4702 spin_lock(&shrink_list_lock);
4703 list_add(&dev_priv->mm.shrink_list, &shrink_list);
4704 spin_unlock(&shrink_list_lock);
4706 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4707 if (IS_I915G(dev) || IS_I915GM(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
4708 u32 tmp = I915_READ(MI_ARB_STATE);
4709 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4710 /* arb state is a masked write, so set bit + bit in mask */
4711 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4712 I915_WRITE(MI_ARB_STATE, tmp);
4716 /* Old X drivers will take 0-2 for front, back, depth buffers */
4717 dev_priv->fence_reg_start = 3;
4719 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4720 dev_priv->num_fence_regs = 16;
4721 else
4722 dev_priv->num_fence_regs = 8;
4724 /* Initialize fence registers to zero */
4725 if (IS_I965G(dev)) {
4726 for (i = 0; i < 16; i++)
4727 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4728 } else {
4729 for (i = 0; i < 8; i++)
4730 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4731 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4732 for (i = 0; i < 8; i++)
4733 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4736 i915_gem_detect_bit_6_swizzle(dev);
4740 * Create a physically contiguous memory object for this object
4741 * e.g. for cursor + overlay regs
4743 int i915_gem_init_phys_object(struct drm_device *dev,
4744 int id, int size)
4746 drm_i915_private_t *dev_priv = dev->dev_private;
4747 struct drm_i915_gem_phys_object *phys_obj;
4748 int ret;
4750 if (dev_priv->mm.phys_objs[id - 1] || !size)
4751 return 0;
4753 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
4754 if (!phys_obj)
4755 return -ENOMEM;
4757 phys_obj->id = id;
4759 phys_obj->handle = drm_pci_alloc(dev, size, 0);
4760 if (!phys_obj->handle) {
4761 ret = -ENOMEM;
4762 goto kfree_obj;
4764 #ifdef CONFIG_X86
4765 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4766 #endif
4768 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4770 return 0;
4771 kfree_obj:
4772 kfree(phys_obj);
4773 return ret;
4776 void i915_gem_free_phys_object(struct drm_device *dev, int id)
4778 drm_i915_private_t *dev_priv = dev->dev_private;
4779 struct drm_i915_gem_phys_object *phys_obj;
4781 if (!dev_priv->mm.phys_objs[id - 1])
4782 return;
4784 phys_obj = dev_priv->mm.phys_objs[id - 1];
4785 if (phys_obj->cur_obj) {
4786 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4789 #ifdef CONFIG_X86
4790 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4791 #endif
4792 drm_pci_free(dev, phys_obj->handle);
4793 kfree(phys_obj);
4794 dev_priv->mm.phys_objs[id - 1] = NULL;
4797 void i915_gem_free_all_phys_object(struct drm_device *dev)
4799 int i;
4801 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
4802 i915_gem_free_phys_object(dev, i);
4805 void i915_gem_detach_phys_object(struct drm_device *dev,
4806 struct drm_gem_object *obj)
4808 struct drm_i915_gem_object *obj_priv;
4809 int i;
4810 int ret;
4811 int page_count;
4813 obj_priv = obj->driver_private;
4814 if (!obj_priv->phys_obj)
4815 return;
4817 ret = i915_gem_object_get_pages(obj, 0);
4818 if (ret)
4819 goto out;
4821 page_count = obj->size / PAGE_SIZE;
4823 for (i = 0; i < page_count; i++) {
4824 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
4825 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4827 memcpy(dst, src, PAGE_SIZE);
4828 kunmap_atomic(dst, KM_USER0);
4830 drm_clflush_pages(obj_priv->pages, page_count);
4831 drm_agp_chipset_flush(dev);
4833 i915_gem_object_put_pages(obj);
4834 out:
4835 obj_priv->phys_obj->cur_obj = NULL;
4836 obj_priv->phys_obj = NULL;
4840 i915_gem_attach_phys_object(struct drm_device *dev,
4841 struct drm_gem_object *obj, int id)
4843 drm_i915_private_t *dev_priv = dev->dev_private;
4844 struct drm_i915_gem_object *obj_priv;
4845 int ret = 0;
4846 int page_count;
4847 int i;
4849 if (id > I915_MAX_PHYS_OBJECT)
4850 return -EINVAL;
4852 obj_priv = obj->driver_private;
4854 if (obj_priv->phys_obj) {
4855 if (obj_priv->phys_obj->id == id)
4856 return 0;
4857 i915_gem_detach_phys_object(dev, obj);
4861 /* create a new object */
4862 if (!dev_priv->mm.phys_objs[id - 1]) {
4863 ret = i915_gem_init_phys_object(dev, id,
4864 obj->size);
4865 if (ret) {
4866 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
4867 goto out;
4871 /* bind to the object */
4872 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4873 obj_priv->phys_obj->cur_obj = obj;
4875 ret = i915_gem_object_get_pages(obj, 0);
4876 if (ret) {
4877 DRM_ERROR("failed to get page list\n");
4878 goto out;
4881 page_count = obj->size / PAGE_SIZE;
4883 for (i = 0; i < page_count; i++) {
4884 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
4885 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4887 memcpy(dst, src, PAGE_SIZE);
4888 kunmap_atomic(src, KM_USER0);
4891 i915_gem_object_put_pages(obj);
4893 return 0;
4894 out:
4895 return ret;
4898 static int
4899 i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4900 struct drm_i915_gem_pwrite *args,
4901 struct drm_file *file_priv)
4903 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4904 void *obj_addr;
4905 int ret;
4906 char __user *user_data;
4908 user_data = (char __user *) (uintptr_t) args->data_ptr;
4909 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4911 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
4912 ret = copy_from_user(obj_addr, user_data, args->size);
4913 if (ret)
4914 return -EFAULT;
4916 drm_agp_chipset_flush(dev);
4917 return 0;
4920 void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
4922 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
4924 /* Clean up our request list when the client is going away, so that
4925 * later retire_requests won't dereference our soon-to-be-gone
4926 * file_priv.
4928 mutex_lock(&dev->struct_mutex);
4929 while (!list_empty(&i915_file_priv->mm.request_list))
4930 list_del_init(i915_file_priv->mm.request_list.next);
4931 mutex_unlock(&dev->struct_mutex);
4934 static int
4935 i915_gem_shrink(int nr_to_scan, gfp_t gfp_mask)
4937 drm_i915_private_t *dev_priv, *next_dev;
4938 struct drm_i915_gem_object *obj_priv, *next_obj;
4939 int cnt = 0;
4940 int would_deadlock = 1;
4942 /* "fast-path" to count number of available objects */
4943 if (nr_to_scan == 0) {
4944 spin_lock(&shrink_list_lock);
4945 list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
4946 struct drm_device *dev = dev_priv->dev;
4948 if (mutex_trylock(&dev->struct_mutex)) {
4949 list_for_each_entry(obj_priv,
4950 &dev_priv->mm.inactive_list,
4951 list)
4952 cnt++;
4953 mutex_unlock(&dev->struct_mutex);
4956 spin_unlock(&shrink_list_lock);
4958 return (cnt / 100) * sysctl_vfs_cache_pressure;
4961 spin_lock(&shrink_list_lock);
4963 /* first scan for clean buffers */
4964 list_for_each_entry_safe(dev_priv, next_dev,
4965 &shrink_list, mm.shrink_list) {
4966 struct drm_device *dev = dev_priv->dev;
4968 if (! mutex_trylock(&dev->struct_mutex))
4969 continue;
4971 spin_unlock(&shrink_list_lock);
4973 i915_gem_retire_requests(dev);
4975 list_for_each_entry_safe(obj_priv, next_obj,
4976 &dev_priv->mm.inactive_list,
4977 list) {
4978 if (i915_gem_object_is_purgeable(obj_priv)) {
4979 i915_gem_object_unbind(obj_priv->obj);
4980 if (--nr_to_scan <= 0)
4981 break;
4985 spin_lock(&shrink_list_lock);
4986 mutex_unlock(&dev->struct_mutex);
4988 would_deadlock = 0;
4990 if (nr_to_scan <= 0)
4991 break;
4994 /* second pass, evict/count anything still on the inactive list */
4995 list_for_each_entry_safe(dev_priv, next_dev,
4996 &shrink_list, mm.shrink_list) {
4997 struct drm_device *dev = dev_priv->dev;
4999 if (! mutex_trylock(&dev->struct_mutex))
5000 continue;
5002 spin_unlock(&shrink_list_lock);
5004 list_for_each_entry_safe(obj_priv, next_obj,
5005 &dev_priv->mm.inactive_list,
5006 list) {
5007 if (nr_to_scan > 0) {
5008 i915_gem_object_unbind(obj_priv->obj);
5009 nr_to_scan--;
5010 } else
5011 cnt++;
5014 spin_lock(&shrink_list_lock);
5015 mutex_unlock(&dev->struct_mutex);
5017 would_deadlock = 0;
5020 spin_unlock(&shrink_list_lock);
5022 if (would_deadlock)
5023 return -1;
5024 else if (cnt > 0)
5025 return (cnt / 100) * sysctl_vfs_cache_pressure;
5026 else
5027 return 0;
5030 static struct shrinker shrinker = {
5031 .shrink = i915_gem_shrink,
5032 .seeks = DEFAULT_SEEKS,
5035 __init void
5036 i915_gem_shrinker_init(void)
5038 register_shrinker(&shrinker);
5041 __exit void
5042 i915_gem_shrinker_exit(void)
5044 unregister_shrinker(&shrinker);