2 * Copyright © 2008 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
32 #include <linux/swap.h>
33 #include <linux/pci.h>
35 #define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
37 static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
38 static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
39 static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
40 static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
42 static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
45 static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
46 static int i915_gem_object_get_pages(struct drm_gem_object *obj);
47 static void i915_gem_object_put_pages(struct drm_gem_object *obj);
48 static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
49 static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
51 static int i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write);
52 static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
53 static int i915_gem_evict_something(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 int i915_gem_do_init(struct drm_device *dev, unsigned long start,
61 drm_i915_private_t *dev_priv = dev->dev_private;
64 (start & (PAGE_SIZE - 1)) != 0 ||
65 (end & (PAGE_SIZE - 1)) != 0) {
69 drm_mm_init(&dev_priv->mm.gtt_space, start,
72 dev->gtt_total = (uint32_t) (end - start);
78 i915_gem_init_ioctl(struct drm_device *dev, void *data,
79 struct drm_file *file_priv)
81 struct drm_i915_gem_init *args = data;
84 mutex_lock(&dev->struct_mutex);
85 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
86 mutex_unlock(&dev->struct_mutex);
92 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
93 struct drm_file *file_priv)
95 struct drm_i915_gem_get_aperture *args = data;
97 if (!(dev->driver->driver_features & DRIVER_GEM))
100 args->aper_size = dev->gtt_total;
101 args->aper_available_size = (args->aper_size -
102 atomic_read(&dev->pin_memory));
109 * Creates a new mm object and returns a handle to it.
112 i915_gem_create_ioctl(struct drm_device *dev, void *data,
113 struct drm_file *file_priv)
115 struct drm_i915_gem_create *args = data;
116 struct drm_gem_object *obj;
119 args->size = roundup(args->size, PAGE_SIZE);
121 /* Allocate the new object */
122 obj = drm_gem_object_alloc(dev, args->size);
126 ret = drm_gem_handle_create(file_priv, obj, &handle);
127 mutex_lock(&dev->struct_mutex);
128 drm_gem_object_handle_unreference(obj);
129 mutex_unlock(&dev->struct_mutex);
134 args->handle = handle;
140 fast_shmem_read(struct page **pages,
141 loff_t page_base, int page_offset,
148 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
151 ret = __copy_to_user_inatomic(data, vaddr + page_offset, length);
152 kunmap_atomic(vaddr, KM_USER0);
158 slow_shmem_copy(struct page *dst_page,
160 struct page *src_page,
164 char *dst_vaddr, *src_vaddr;
166 dst_vaddr = kmap_atomic(dst_page, KM_USER0);
167 if (dst_vaddr == NULL)
170 src_vaddr = kmap_atomic(src_page, KM_USER1);
171 if (src_vaddr == NULL) {
172 kunmap_atomic(dst_vaddr, KM_USER0);
176 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
178 kunmap_atomic(src_vaddr, KM_USER1);
179 kunmap_atomic(dst_vaddr, KM_USER0);
185 * This is the fast shmem pread path, which attempts to copy_from_user directly
186 * from the backing pages of the object to the user's address space. On a
187 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
190 i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
191 struct drm_i915_gem_pread *args,
192 struct drm_file *file_priv)
194 struct drm_i915_gem_object *obj_priv = obj->driver_private;
196 loff_t offset, page_base;
197 char __user *user_data;
198 int page_offset, page_length;
201 user_data = (char __user *) (uintptr_t) args->data_ptr;
204 mutex_lock(&dev->struct_mutex);
206 ret = i915_gem_object_get_pages(obj);
210 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
215 obj_priv = obj->driver_private;
216 offset = args->offset;
219 /* Operation in this page
221 * page_base = page offset within aperture
222 * page_offset = offset within page
223 * page_length = bytes to copy for this page
225 page_base = (offset & ~(PAGE_SIZE-1));
226 page_offset = offset & (PAGE_SIZE-1);
227 page_length = remain;
228 if ((page_offset + remain) > PAGE_SIZE)
229 page_length = PAGE_SIZE - page_offset;
231 ret = fast_shmem_read(obj_priv->pages,
232 page_base, page_offset,
233 user_data, page_length);
237 remain -= page_length;
238 user_data += page_length;
239 offset += page_length;
243 i915_gem_object_put_pages(obj);
245 mutex_unlock(&dev->struct_mutex);
251 * This is the fallback shmem pread path, which allocates temporary storage
252 * in kernel space to copy_to_user into outside of the struct_mutex, so we
253 * can copy out of the object's backing pages while holding the struct mutex
254 * and not take page faults.
257 i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
258 struct drm_i915_gem_pread *args,
259 struct drm_file *file_priv)
261 struct drm_i915_gem_object *obj_priv = obj->driver_private;
262 struct mm_struct *mm = current->mm;
263 struct page **user_pages;
265 loff_t offset, pinned_pages, i;
266 loff_t first_data_page, last_data_page, num_pages;
267 int shmem_page_index, shmem_page_offset;
268 int data_page_index, data_page_offset;
271 uint64_t data_ptr = args->data_ptr;
275 /* Pin the user pages containing the data. We can't fault while
276 * holding the struct mutex, yet we want to hold it while
277 * dereferencing the user data.
279 first_data_page = data_ptr / PAGE_SIZE;
280 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
281 num_pages = last_data_page - first_data_page + 1;
283 user_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
284 if (user_pages == NULL)
287 down_read(&mm->mmap_sem);
288 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
289 num_pages, 0, 0, user_pages, NULL);
290 up_read(&mm->mmap_sem);
291 if (pinned_pages < num_pages) {
293 goto fail_put_user_pages;
296 mutex_lock(&dev->struct_mutex);
298 ret = i915_gem_object_get_pages(obj);
302 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
307 obj_priv = obj->driver_private;
308 offset = args->offset;
311 /* Operation in this page
313 * shmem_page_index = page number within shmem file
314 * shmem_page_offset = offset within page in shmem file
315 * data_page_index = page number in get_user_pages return
316 * data_page_offset = offset with data_page_index page.
317 * page_length = bytes to copy for this page
319 shmem_page_index = offset / PAGE_SIZE;
320 shmem_page_offset = offset & ~PAGE_MASK;
321 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
322 data_page_offset = data_ptr & ~PAGE_MASK;
324 page_length = remain;
325 if ((shmem_page_offset + page_length) > PAGE_SIZE)
326 page_length = PAGE_SIZE - shmem_page_offset;
327 if ((data_page_offset + page_length) > PAGE_SIZE)
328 page_length = PAGE_SIZE - data_page_offset;
330 ret = slow_shmem_copy(user_pages[data_page_index],
332 obj_priv->pages[shmem_page_index],
338 remain -= page_length;
339 data_ptr += page_length;
340 offset += page_length;
344 i915_gem_object_put_pages(obj);
346 mutex_unlock(&dev->struct_mutex);
348 for (i = 0; i < pinned_pages; i++) {
349 SetPageDirty(user_pages[i]);
350 page_cache_release(user_pages[i]);
358 * Reads data from the object referenced by handle.
360 * On error, the contents of *data are undefined.
363 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
364 struct drm_file *file_priv)
366 struct drm_i915_gem_pread *args = data;
367 struct drm_gem_object *obj;
368 struct drm_i915_gem_object *obj_priv;
371 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
374 obj_priv = obj->driver_private;
376 /* Bounds check source.
378 * XXX: This could use review for overflow issues...
380 if (args->offset > obj->size || args->size > obj->size ||
381 args->offset + args->size > obj->size) {
382 drm_gem_object_unreference(obj);
386 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
388 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
390 drm_gem_object_unreference(obj);
395 /* This is the fast write path which cannot handle
396 * page faults in the source data
400 fast_user_write(struct io_mapping *mapping,
401 loff_t page_base, int page_offset,
402 char __user *user_data,
406 unsigned long unwritten;
408 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
409 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
411 io_mapping_unmap_atomic(vaddr_atomic);
417 /* Here's the write path which can sleep for
422 slow_kernel_write(struct io_mapping *mapping,
423 loff_t gtt_base, int gtt_offset,
424 struct page *user_page, int user_offset,
427 char *src_vaddr, *dst_vaddr;
428 unsigned long unwritten;
430 dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base);
431 src_vaddr = kmap_atomic(user_page, KM_USER1);
432 unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset,
433 src_vaddr + user_offset,
435 kunmap_atomic(src_vaddr, KM_USER1);
436 io_mapping_unmap_atomic(dst_vaddr);
443 fast_shmem_write(struct page **pages,
444 loff_t page_base, int page_offset,
449 unsigned long unwritten;
451 vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
454 unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
455 kunmap_atomic(vaddr, KM_USER0);
463 * This is the fast pwrite path, where we copy the data directly from the
464 * user into the GTT, uncached.
467 i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
468 struct drm_i915_gem_pwrite *args,
469 struct drm_file *file_priv)
471 struct drm_i915_gem_object *obj_priv = obj->driver_private;
472 drm_i915_private_t *dev_priv = dev->dev_private;
474 loff_t offset, page_base;
475 char __user *user_data;
476 int page_offset, page_length;
479 user_data = (char __user *) (uintptr_t) args->data_ptr;
481 if (!access_ok(VERIFY_READ, user_data, remain))
485 mutex_lock(&dev->struct_mutex);
486 ret = i915_gem_object_pin(obj, 0);
488 mutex_unlock(&dev->struct_mutex);
491 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
495 obj_priv = obj->driver_private;
496 offset = obj_priv->gtt_offset + args->offset;
499 /* Operation in this page
501 * page_base = page offset within aperture
502 * page_offset = offset within page
503 * page_length = bytes to copy for this page
505 page_base = (offset & ~(PAGE_SIZE-1));
506 page_offset = offset & (PAGE_SIZE-1);
507 page_length = remain;
508 if ((page_offset + remain) > PAGE_SIZE)
509 page_length = PAGE_SIZE - page_offset;
511 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
512 page_offset, user_data, page_length);
514 /* If we get a fault while copying data, then (presumably) our
515 * source page isn't available. Return the error and we'll
516 * retry in the slow path.
521 remain -= page_length;
522 user_data += page_length;
523 offset += page_length;
527 i915_gem_object_unpin(obj);
528 mutex_unlock(&dev->struct_mutex);
534 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
535 * the memory and maps it using kmap_atomic for copying.
537 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
538 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
541 i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
542 struct drm_i915_gem_pwrite *args,
543 struct drm_file *file_priv)
545 struct drm_i915_gem_object *obj_priv = obj->driver_private;
546 drm_i915_private_t *dev_priv = dev->dev_private;
548 loff_t gtt_page_base, offset;
549 loff_t first_data_page, last_data_page, num_pages;
550 loff_t pinned_pages, i;
551 struct page **user_pages;
552 struct mm_struct *mm = current->mm;
553 int gtt_page_offset, data_page_offset, data_page_index, page_length;
555 uint64_t data_ptr = args->data_ptr;
559 /* Pin the user pages containing the data. We can't fault while
560 * holding the struct mutex, and all of the pwrite implementations
561 * want to hold it while dereferencing the user data.
563 first_data_page = data_ptr / PAGE_SIZE;
564 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
565 num_pages = last_data_page - first_data_page + 1;
567 user_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
568 if (user_pages == NULL)
571 down_read(&mm->mmap_sem);
572 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
573 num_pages, 0, 0, user_pages, NULL);
574 up_read(&mm->mmap_sem);
575 if (pinned_pages < num_pages) {
577 goto out_unpin_pages;
580 mutex_lock(&dev->struct_mutex);
581 ret = i915_gem_object_pin(obj, 0);
585 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
587 goto out_unpin_object;
589 obj_priv = obj->driver_private;
590 offset = obj_priv->gtt_offset + args->offset;
593 /* Operation in this page
595 * gtt_page_base = page offset within aperture
596 * gtt_page_offset = offset within page in aperture
597 * data_page_index = page number in get_user_pages return
598 * data_page_offset = offset with data_page_index page.
599 * page_length = bytes to copy for this page
601 gtt_page_base = offset & PAGE_MASK;
602 gtt_page_offset = offset & ~PAGE_MASK;
603 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
604 data_page_offset = data_ptr & ~PAGE_MASK;
606 page_length = remain;
607 if ((gtt_page_offset + page_length) > PAGE_SIZE)
608 page_length = PAGE_SIZE - gtt_page_offset;
609 if ((data_page_offset + page_length) > PAGE_SIZE)
610 page_length = PAGE_SIZE - data_page_offset;
612 ret = slow_kernel_write(dev_priv->mm.gtt_mapping,
613 gtt_page_base, gtt_page_offset,
614 user_pages[data_page_index],
618 /* If we get a fault while copying data, then (presumably) our
619 * source page isn't available. Return the error and we'll
620 * retry in the slow path.
623 goto out_unpin_object;
625 remain -= page_length;
626 offset += page_length;
627 data_ptr += page_length;
631 i915_gem_object_unpin(obj);
633 mutex_unlock(&dev->struct_mutex);
635 for (i = 0; i < pinned_pages; i++)
636 page_cache_release(user_pages[i]);
643 * This is the fast shmem pwrite path, which attempts to directly
644 * copy_from_user into the kmapped pages backing the object.
647 i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
648 struct drm_i915_gem_pwrite *args,
649 struct drm_file *file_priv)
651 struct drm_i915_gem_object *obj_priv = obj->driver_private;
653 loff_t offset, page_base;
654 char __user *user_data;
655 int page_offset, page_length;
658 user_data = (char __user *) (uintptr_t) args->data_ptr;
661 mutex_lock(&dev->struct_mutex);
663 ret = i915_gem_object_get_pages(obj);
667 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
671 obj_priv = obj->driver_private;
672 offset = args->offset;
676 /* Operation in this page
678 * page_base = page offset within aperture
679 * page_offset = offset within page
680 * page_length = bytes to copy for this page
682 page_base = (offset & ~(PAGE_SIZE-1));
683 page_offset = offset & (PAGE_SIZE-1);
684 page_length = remain;
685 if ((page_offset + remain) > PAGE_SIZE)
686 page_length = PAGE_SIZE - page_offset;
688 ret = fast_shmem_write(obj_priv->pages,
689 page_base, page_offset,
690 user_data, page_length);
694 remain -= page_length;
695 user_data += page_length;
696 offset += page_length;
700 i915_gem_object_put_pages(obj);
702 mutex_unlock(&dev->struct_mutex);
708 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
709 * the memory and maps it using kmap_atomic for copying.
711 * This avoids taking mmap_sem for faulting on the user's address while the
712 * struct_mutex is held.
715 i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
716 struct drm_i915_gem_pwrite *args,
717 struct drm_file *file_priv)
719 struct drm_i915_gem_object *obj_priv = obj->driver_private;
720 struct mm_struct *mm = current->mm;
721 struct page **user_pages;
723 loff_t offset, pinned_pages, i;
724 loff_t first_data_page, last_data_page, num_pages;
725 int shmem_page_index, shmem_page_offset;
726 int data_page_index, data_page_offset;
729 uint64_t data_ptr = args->data_ptr;
733 /* Pin the user pages containing the data. We can't fault while
734 * holding the struct mutex, and all of the pwrite implementations
735 * want to hold it while dereferencing the user data.
737 first_data_page = data_ptr / PAGE_SIZE;
738 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
739 num_pages = last_data_page - first_data_page + 1;
741 user_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
742 if (user_pages == NULL)
745 down_read(&mm->mmap_sem);
746 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
747 num_pages, 0, 0, user_pages, NULL);
748 up_read(&mm->mmap_sem);
749 if (pinned_pages < num_pages) {
751 goto fail_put_user_pages;
754 mutex_lock(&dev->struct_mutex);
756 ret = i915_gem_object_get_pages(obj);
760 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
764 obj_priv = obj->driver_private;
765 offset = args->offset;
769 /* Operation in this page
771 * shmem_page_index = page number within shmem file
772 * shmem_page_offset = offset within page in shmem file
773 * data_page_index = page number in get_user_pages return
774 * data_page_offset = offset with data_page_index page.
775 * page_length = bytes to copy for this page
777 shmem_page_index = offset / PAGE_SIZE;
778 shmem_page_offset = offset & ~PAGE_MASK;
779 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
780 data_page_offset = data_ptr & ~PAGE_MASK;
782 page_length = remain;
783 if ((shmem_page_offset + page_length) > PAGE_SIZE)
784 page_length = PAGE_SIZE - shmem_page_offset;
785 if ((data_page_offset + page_length) > PAGE_SIZE)
786 page_length = PAGE_SIZE - data_page_offset;
788 ret = slow_shmem_copy(obj_priv->pages[shmem_page_index],
790 user_pages[data_page_index],
796 remain -= page_length;
797 data_ptr += page_length;
798 offset += page_length;
802 i915_gem_object_put_pages(obj);
804 mutex_unlock(&dev->struct_mutex);
806 for (i = 0; i < pinned_pages; i++)
807 page_cache_release(user_pages[i]);
814 * Writes data to the object referenced by handle.
816 * On error, the contents of the buffer that were to be modified are undefined.
819 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
820 struct drm_file *file_priv)
822 struct drm_i915_gem_pwrite *args = data;
823 struct drm_gem_object *obj;
824 struct drm_i915_gem_object *obj_priv;
827 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
830 obj_priv = obj->driver_private;
832 /* Bounds check destination.
834 * XXX: This could use review for overflow issues...
836 if (args->offset > obj->size || args->size > obj->size ||
837 args->offset + args->size > obj->size) {
838 drm_gem_object_unreference(obj);
842 /* We can only do the GTT pwrite on untiled buffers, as otherwise
843 * it would end up going through the fenced access, and we'll get
844 * different detiling behavior between reading and writing.
845 * pread/pwrite currently are reading and writing from the CPU
846 * perspective, requiring manual detiling by the client.
848 if (obj_priv->phys_obj)
849 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
850 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
851 dev->gtt_total != 0) {
852 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
853 if (ret == -EFAULT) {
854 ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
858 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
859 if (ret == -EFAULT) {
860 ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
867 DRM_INFO("pwrite failed %d\n", ret);
870 drm_gem_object_unreference(obj);
876 * Called when user space prepares to use an object with the CPU, either
877 * through the mmap ioctl's mapping or a GTT mapping.
880 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
881 struct drm_file *file_priv)
883 struct drm_i915_gem_set_domain *args = data;
884 struct drm_gem_object *obj;
885 uint32_t read_domains = args->read_domains;
886 uint32_t write_domain = args->write_domain;
889 if (!(dev->driver->driver_features & DRIVER_GEM))
892 /* Only handle setting domains to types used by the CPU. */
893 if (write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
896 if (read_domains & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
899 /* Having something in the write domain implies it's in the read
900 * domain, and only that read domain. Enforce that in the request.
902 if (write_domain != 0 && read_domains != write_domain)
905 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
909 mutex_lock(&dev->struct_mutex);
911 DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
912 obj, obj->size, read_domains, write_domain);
914 if (read_domains & I915_GEM_DOMAIN_GTT) {
915 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
917 /* Silently promote "you're not bound, there was nothing to do"
918 * to success, since the client was just asking us to
919 * make sure everything was done.
924 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
927 drm_gem_object_unreference(obj);
928 mutex_unlock(&dev->struct_mutex);
933 * Called when user space has done writes to this buffer
936 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
937 struct drm_file *file_priv)
939 struct drm_i915_gem_sw_finish *args = data;
940 struct drm_gem_object *obj;
941 struct drm_i915_gem_object *obj_priv;
944 if (!(dev->driver->driver_features & DRIVER_GEM))
947 mutex_lock(&dev->struct_mutex);
948 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
950 mutex_unlock(&dev->struct_mutex);
955 DRM_INFO("%s: sw_finish %d (%p %d)\n",
956 __func__, args->handle, obj, obj->size);
958 obj_priv = obj->driver_private;
960 /* Pinned buffers may be scanout, so flush the cache */
961 if (obj_priv->pin_count)
962 i915_gem_object_flush_cpu_write_domain(obj);
964 drm_gem_object_unreference(obj);
965 mutex_unlock(&dev->struct_mutex);
970 * Maps the contents of an object, returning the address it is mapped
973 * While the mapping holds a reference on the contents of the object, it doesn't
974 * imply a ref on the object itself.
977 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
978 struct drm_file *file_priv)
980 struct drm_i915_gem_mmap *args = data;
981 struct drm_gem_object *obj;
985 if (!(dev->driver->driver_features & DRIVER_GEM))
988 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
992 offset = args->offset;
994 down_write(¤t->mm->mmap_sem);
995 addr = do_mmap(obj->filp, 0, args->size,
996 PROT_READ | PROT_WRITE, MAP_SHARED,
998 up_write(¤t->mm->mmap_sem);
999 mutex_lock(&dev->struct_mutex);
1000 drm_gem_object_unreference(obj);
1001 mutex_unlock(&dev->struct_mutex);
1002 if (IS_ERR((void *)addr))
1005 args->addr_ptr = (uint64_t) addr;
1011 * i915_gem_fault - fault a page into the GTT
1012 * vma: VMA in question
1015 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1016 * from userspace. The fault handler takes care of binding the object to
1017 * the GTT (if needed), allocating and programming a fence register (again,
1018 * only if needed based on whether the old reg is still valid or the object
1019 * is tiled) and inserting a new PTE into the faulting process.
1021 * Note that the faulting process may involve evicting existing objects
1022 * from the GTT and/or fence registers to make room. So performance may
1023 * suffer if the GTT working set is large or there are few fence registers
1026 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1028 struct drm_gem_object *obj = vma->vm_private_data;
1029 struct drm_device *dev = obj->dev;
1030 struct drm_i915_private *dev_priv = dev->dev_private;
1031 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1032 pgoff_t page_offset;
1035 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1037 /* We don't use vmf->pgoff since that has the fake offset */
1038 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1041 /* Now bind it into the GTT if needed */
1042 mutex_lock(&dev->struct_mutex);
1043 if (!obj_priv->gtt_space) {
1044 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1046 mutex_unlock(&dev->struct_mutex);
1047 return VM_FAULT_SIGBUS;
1049 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
1052 /* Need a new fence register? */
1053 if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
1054 obj_priv->tiling_mode != I915_TILING_NONE) {
1055 ret = i915_gem_object_get_fence_reg(obj, write);
1057 mutex_unlock(&dev->struct_mutex);
1058 return VM_FAULT_SIGBUS;
1062 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1065 /* Finally, remap it using the new GTT offset */
1066 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1068 mutex_unlock(&dev->struct_mutex);
1073 return VM_FAULT_OOM;
1075 return VM_FAULT_SIGBUS;
1077 return VM_FAULT_NOPAGE;
1082 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1083 * @obj: obj in question
1085 * GEM memory mapping works by handing back to userspace a fake mmap offset
1086 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1087 * up the object based on the offset and sets up the various memory mapping
1090 * This routine allocates and attaches a fake offset for @obj.
1093 i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1095 struct drm_device *dev = obj->dev;
1096 struct drm_gem_mm *mm = dev->mm_private;
1097 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1098 struct drm_map_list *list;
1099 struct drm_local_map *map;
1102 /* Set the object up for mmap'ing */
1103 list = &obj->map_list;
1104 list->map = drm_calloc(1, sizeof(struct drm_map_list),
1110 map->type = _DRM_GEM;
1111 map->size = obj->size;
1114 /* Get a DRM GEM mmap offset allocated... */
1115 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1116 obj->size / PAGE_SIZE, 0, 0);
1117 if (!list->file_offset_node) {
1118 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1123 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1124 obj->size / PAGE_SIZE, 0);
1125 if (!list->file_offset_node) {
1130 list->hash.key = list->file_offset_node->start;
1131 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1132 DRM_ERROR("failed to add to map hash\n");
1136 /* By now we should be all set, any drm_mmap request on the offset
1137 * below will get to our mmap & fault handler */
1138 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1143 drm_mm_put_block(list->file_offset_node);
1145 drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER);
1151 i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1153 struct drm_device *dev = obj->dev;
1154 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1155 struct drm_gem_mm *mm = dev->mm_private;
1156 struct drm_map_list *list;
1158 list = &obj->map_list;
1159 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1161 if (list->file_offset_node) {
1162 drm_mm_put_block(list->file_offset_node);
1163 list->file_offset_node = NULL;
1167 drm_free(list->map, sizeof(struct drm_map), DRM_MEM_DRIVER);
1171 obj_priv->mmap_offset = 0;
1175 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1176 * @obj: object to check
1178 * Return the required GTT alignment for an object, taking into account
1179 * potential fence register mapping if needed.
1182 i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1184 struct drm_device *dev = obj->dev;
1185 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1189 * Minimum alignment is 4k (GTT page size), but might be greater
1190 * if a fence register is needed for the object.
1192 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1196 * Previous chips need to be aligned to the size of the smallest
1197 * fence register that can contain the object.
1204 for (i = start; i < obj->size; i <<= 1)
1211 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1213 * @data: GTT mapping ioctl data
1214 * @file_priv: GEM object info
1216 * Simply returns the fake offset to userspace so it can mmap it.
1217 * The mmap call will end up in drm_gem_mmap(), which will set things
1218 * up so we can get faults in the handler above.
1220 * The fault handler will take care of binding the object into the GTT
1221 * (since it may have been evicted to make room for something), allocating
1222 * a fence register, and mapping the appropriate aperture address into
1226 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1227 struct drm_file *file_priv)
1229 struct drm_i915_gem_mmap_gtt *args = data;
1230 struct drm_i915_private *dev_priv = dev->dev_private;
1231 struct drm_gem_object *obj;
1232 struct drm_i915_gem_object *obj_priv;
1235 if (!(dev->driver->driver_features & DRIVER_GEM))
1238 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1242 mutex_lock(&dev->struct_mutex);
1244 obj_priv = obj->driver_private;
1246 if (!obj_priv->mmap_offset) {
1247 ret = i915_gem_create_mmap_offset(obj);
1249 drm_gem_object_unreference(obj);
1250 mutex_unlock(&dev->struct_mutex);
1255 args->offset = obj_priv->mmap_offset;
1257 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
1259 /* Make sure the alignment is correct for fence regs etc */
1260 if (obj_priv->agp_mem &&
1261 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
1262 drm_gem_object_unreference(obj);
1263 mutex_unlock(&dev->struct_mutex);
1268 * Pull it into the GTT so that we have a page list (makes the
1269 * initial fault faster and any subsequent flushing possible).
1271 if (!obj_priv->agp_mem) {
1272 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1274 drm_gem_object_unreference(obj);
1275 mutex_unlock(&dev->struct_mutex);
1278 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
1281 drm_gem_object_unreference(obj);
1282 mutex_unlock(&dev->struct_mutex);
1288 i915_gem_object_put_pages(struct drm_gem_object *obj)
1290 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1291 int page_count = obj->size / PAGE_SIZE;
1294 BUG_ON(obj_priv->pages_refcount == 0);
1296 if (--obj_priv->pages_refcount != 0)
1299 for (i = 0; i < page_count; i++)
1300 if (obj_priv->pages[i] != NULL) {
1301 if (obj_priv->dirty)
1302 set_page_dirty(obj_priv->pages[i]);
1303 mark_page_accessed(obj_priv->pages[i]);
1304 page_cache_release(obj_priv->pages[i]);
1306 obj_priv->dirty = 0;
1308 drm_free(obj_priv->pages,
1309 page_count * sizeof(struct page *),
1311 obj_priv->pages = NULL;
1315 i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
1317 struct drm_device *dev = obj->dev;
1318 drm_i915_private_t *dev_priv = dev->dev_private;
1319 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1321 /* Add a reference if we're newly entering the active list. */
1322 if (!obj_priv->active) {
1323 drm_gem_object_reference(obj);
1324 obj_priv->active = 1;
1326 /* Move from whatever list we were on to the tail of execution. */
1327 list_move_tail(&obj_priv->list,
1328 &dev_priv->mm.active_list);
1329 obj_priv->last_rendering_seqno = seqno;
1333 i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1335 struct drm_device *dev = obj->dev;
1336 drm_i915_private_t *dev_priv = dev->dev_private;
1337 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1339 BUG_ON(!obj_priv->active);
1340 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1341 obj_priv->last_rendering_seqno = 0;
1345 i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1347 struct drm_device *dev = obj->dev;
1348 drm_i915_private_t *dev_priv = dev->dev_private;
1349 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1351 i915_verify_inactive(dev, __FILE__, __LINE__);
1352 if (obj_priv->pin_count != 0)
1353 list_del_init(&obj_priv->list);
1355 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1357 obj_priv->last_rendering_seqno = 0;
1358 if (obj_priv->active) {
1359 obj_priv->active = 0;
1360 drm_gem_object_unreference(obj);
1362 i915_verify_inactive(dev, __FILE__, __LINE__);
1366 * Creates a new sequence number, emitting a write of it to the status page
1367 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1369 * Must be called with struct_lock held.
1371 * Returned sequence numbers are nonzero on success.
1374 i915_add_request(struct drm_device *dev, uint32_t flush_domains)
1376 drm_i915_private_t *dev_priv = dev->dev_private;
1377 struct drm_i915_gem_request *request;
1382 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
1383 if (request == NULL)
1386 /* Grab the seqno we're going to make this request be, and bump the
1387 * next (skipping 0 so it can be the reserved no-seqno value).
1389 seqno = dev_priv->mm.next_gem_seqno;
1390 dev_priv->mm.next_gem_seqno++;
1391 if (dev_priv->mm.next_gem_seqno == 0)
1392 dev_priv->mm.next_gem_seqno++;
1395 OUT_RING(MI_STORE_DWORD_INDEX);
1396 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1399 OUT_RING(MI_USER_INTERRUPT);
1402 DRM_DEBUG("%d\n", seqno);
1404 request->seqno = seqno;
1405 request->emitted_jiffies = jiffies;
1406 was_empty = list_empty(&dev_priv->mm.request_list);
1407 list_add_tail(&request->list, &dev_priv->mm.request_list);
1409 /* Associate any objects on the flushing list matching the write
1410 * domain we're flushing with our flush.
1412 if (flush_domains != 0) {
1413 struct drm_i915_gem_object *obj_priv, *next;
1415 list_for_each_entry_safe(obj_priv, next,
1416 &dev_priv->mm.flushing_list, list) {
1417 struct drm_gem_object *obj = obj_priv->obj;
1419 if ((obj->write_domain & flush_domains) ==
1420 obj->write_domain) {
1421 obj->write_domain = 0;
1422 i915_gem_object_move_to_active(obj, seqno);
1428 if (was_empty && !dev_priv->mm.suspended)
1429 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1434 * Command execution barrier
1436 * Ensures that all commands in the ring are finished
1437 * before signalling the CPU
1440 i915_retire_commands(struct drm_device *dev)
1442 drm_i915_private_t *dev_priv = dev->dev_private;
1443 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1444 uint32_t flush_domains = 0;
1447 /* The sampler always gets flushed on i965 (sigh) */
1449 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1452 OUT_RING(0); /* noop */
1454 return flush_domains;
1458 * Moves buffers associated only with the given active seqno from the active
1459 * to inactive list, potentially freeing them.
1462 i915_gem_retire_request(struct drm_device *dev,
1463 struct drm_i915_gem_request *request)
1465 drm_i915_private_t *dev_priv = dev->dev_private;
1467 /* Move any buffers on the active list that are no longer referenced
1468 * by the ringbuffer to the flushing/inactive lists as appropriate.
1470 while (!list_empty(&dev_priv->mm.active_list)) {
1471 struct drm_gem_object *obj;
1472 struct drm_i915_gem_object *obj_priv;
1474 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1475 struct drm_i915_gem_object,
1477 obj = obj_priv->obj;
1479 /* If the seqno being retired doesn't match the oldest in the
1480 * list, then the oldest in the list must still be newer than
1483 if (obj_priv->last_rendering_seqno != request->seqno)
1487 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1488 __func__, request->seqno, obj);
1491 if (obj->write_domain != 0)
1492 i915_gem_object_move_to_flushing(obj);
1494 i915_gem_object_move_to_inactive(obj);
1499 * Returns true if seq1 is later than seq2.
1502 i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1504 return (int32_t)(seq1 - seq2) >= 0;
1508 i915_get_gem_seqno(struct drm_device *dev)
1510 drm_i915_private_t *dev_priv = dev->dev_private;
1512 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1516 * This function clears the request list as sequence numbers are passed.
1519 i915_gem_retire_requests(struct drm_device *dev)
1521 drm_i915_private_t *dev_priv = dev->dev_private;
1524 if (!dev_priv->hw_status_page)
1527 seqno = i915_get_gem_seqno(dev);
1529 while (!list_empty(&dev_priv->mm.request_list)) {
1530 struct drm_i915_gem_request *request;
1531 uint32_t retiring_seqno;
1533 request = list_first_entry(&dev_priv->mm.request_list,
1534 struct drm_i915_gem_request,
1536 retiring_seqno = request->seqno;
1538 if (i915_seqno_passed(seqno, retiring_seqno) ||
1539 dev_priv->mm.wedged) {
1540 i915_gem_retire_request(dev, request);
1542 list_del(&request->list);
1543 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
1550 i915_gem_retire_work_handler(struct work_struct *work)
1552 drm_i915_private_t *dev_priv;
1553 struct drm_device *dev;
1555 dev_priv = container_of(work, drm_i915_private_t,
1556 mm.retire_work.work);
1557 dev = dev_priv->dev;
1559 mutex_lock(&dev->struct_mutex);
1560 i915_gem_retire_requests(dev);
1561 if (!dev_priv->mm.suspended &&
1562 !list_empty(&dev_priv->mm.request_list))
1563 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1564 mutex_unlock(&dev->struct_mutex);
1568 * Waits for a sequence number to be signaled, and cleans up the
1569 * request and object lists appropriately for that event.
1572 i915_wait_request(struct drm_device *dev, uint32_t seqno)
1574 drm_i915_private_t *dev_priv = dev->dev_private;
1579 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
1580 dev_priv->mm.waiting_gem_seqno = seqno;
1581 i915_user_irq_get(dev);
1582 ret = wait_event_interruptible(dev_priv->irq_queue,
1583 i915_seqno_passed(i915_get_gem_seqno(dev),
1585 dev_priv->mm.wedged);
1586 i915_user_irq_put(dev);
1587 dev_priv->mm.waiting_gem_seqno = 0;
1589 if (dev_priv->mm.wedged)
1592 if (ret && ret != -ERESTARTSYS)
1593 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1594 __func__, ret, seqno, i915_get_gem_seqno(dev));
1596 /* Directly dispatch request retiring. While we have the work queue
1597 * to handle this, the waiter on a request often wants an associated
1598 * buffer to have made it to the inactive list, and we would need
1599 * a separate wait queue to handle that.
1602 i915_gem_retire_requests(dev);
1608 i915_gem_flush(struct drm_device *dev,
1609 uint32_t invalidate_domains,
1610 uint32_t flush_domains)
1612 drm_i915_private_t *dev_priv = dev->dev_private;
1617 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1618 invalidate_domains, flush_domains);
1621 if (flush_domains & I915_GEM_DOMAIN_CPU)
1622 drm_agp_chipset_flush(dev);
1624 if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
1625 I915_GEM_DOMAIN_GTT)) {
1627 * read/write caches:
1629 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1630 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1631 * also flushed at 2d versus 3d pipeline switches.
1635 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1636 * MI_READ_FLUSH is set, and is always flushed on 965.
1638 * I915_GEM_DOMAIN_COMMAND may not exist?
1640 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1641 * invalidated when MI_EXE_FLUSH is set.
1643 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1644 * invalidated with every MI_FLUSH.
1648 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1649 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1650 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1651 * are flushed at any MI_FLUSH.
1654 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1655 if ((invalidate_domains|flush_domains) &
1656 I915_GEM_DOMAIN_RENDER)
1657 cmd &= ~MI_NO_WRITE_FLUSH;
1658 if (!IS_I965G(dev)) {
1660 * On the 965, the sampler cache always gets flushed
1661 * and this bit is reserved.
1663 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1664 cmd |= MI_READ_FLUSH;
1666 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1667 cmd |= MI_EXE_FLUSH;
1670 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1674 OUT_RING(0); /* noop */
1680 * Ensures that all rendering to the object has completed and the object is
1681 * safe to unbind from the GTT or access from the CPU.
1684 i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1686 struct drm_device *dev = obj->dev;
1687 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1690 /* This function only exists to support waiting for existing rendering,
1691 * not for emitting required flushes.
1693 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
1695 /* If there is rendering queued on the buffer being evicted, wait for
1698 if (obj_priv->active) {
1700 DRM_INFO("%s: object %p wait for seqno %08x\n",
1701 __func__, obj, obj_priv->last_rendering_seqno);
1703 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1712 * Unbinds an object from the GTT aperture.
1715 i915_gem_object_unbind(struct drm_gem_object *obj)
1717 struct drm_device *dev = obj->dev;
1718 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1723 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1724 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1726 if (obj_priv->gtt_space == NULL)
1729 if (obj_priv->pin_count != 0) {
1730 DRM_ERROR("Attempting to unbind pinned buffer\n");
1734 /* Move the object to the CPU domain to ensure that
1735 * any possible CPU writes while it's not in the GTT
1736 * are flushed when we go to remap it. This will
1737 * also ensure that all pending GPU writes are finished
1740 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1742 if (ret != -ERESTARTSYS)
1743 DRM_ERROR("set_domain failed: %d\n", ret);
1747 if (obj_priv->agp_mem != NULL) {
1748 drm_unbind_agp(obj_priv->agp_mem);
1749 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1750 obj_priv->agp_mem = NULL;
1753 BUG_ON(obj_priv->active);
1755 /* blow away mappings if mapped through GTT */
1756 offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT;
1757 if (dev->dev_mapping)
1758 unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1);
1760 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1761 i915_gem_clear_fence_reg(obj);
1763 i915_gem_object_put_pages(obj);
1765 if (obj_priv->gtt_space) {
1766 atomic_dec(&dev->gtt_count);
1767 atomic_sub(obj->size, &dev->gtt_memory);
1769 drm_mm_put_block(obj_priv->gtt_space);
1770 obj_priv->gtt_space = NULL;
1773 /* Remove ourselves from the LRU list if present. */
1774 if (!list_empty(&obj_priv->list))
1775 list_del_init(&obj_priv->list);
1781 i915_gem_evict_something(struct drm_device *dev)
1783 drm_i915_private_t *dev_priv = dev->dev_private;
1784 struct drm_gem_object *obj;
1785 struct drm_i915_gem_object *obj_priv;
1789 /* If there's an inactive buffer available now, grab it
1792 if (!list_empty(&dev_priv->mm.inactive_list)) {
1793 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1794 struct drm_i915_gem_object,
1796 obj = obj_priv->obj;
1797 BUG_ON(obj_priv->pin_count != 0);
1799 DRM_INFO("%s: evicting %p\n", __func__, obj);
1801 BUG_ON(obj_priv->active);
1803 /* Wait on the rendering and unbind the buffer. */
1804 ret = i915_gem_object_unbind(obj);
1808 /* If we didn't get anything, but the ring is still processing
1809 * things, wait for one of those things to finish and hopefully
1810 * leave us a buffer to evict.
1812 if (!list_empty(&dev_priv->mm.request_list)) {
1813 struct drm_i915_gem_request *request;
1815 request = list_first_entry(&dev_priv->mm.request_list,
1816 struct drm_i915_gem_request,
1819 ret = i915_wait_request(dev, request->seqno);
1823 /* if waiting caused an object to become inactive,
1824 * then loop around and wait for it. Otherwise, we
1825 * assume that waiting freed and unbound something,
1826 * so there should now be some space in the GTT
1828 if (!list_empty(&dev_priv->mm.inactive_list))
1833 /* If we didn't have anything on the request list but there
1834 * are buffers awaiting a flush, emit one and try again.
1835 * When we wait on it, those buffers waiting for that flush
1836 * will get moved to inactive.
1838 if (!list_empty(&dev_priv->mm.flushing_list)) {
1839 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1840 struct drm_i915_gem_object,
1842 obj = obj_priv->obj;
1847 i915_add_request(dev, obj->write_domain);
1853 DRM_ERROR("inactive empty %d request empty %d "
1854 "flushing empty %d\n",
1855 list_empty(&dev_priv->mm.inactive_list),
1856 list_empty(&dev_priv->mm.request_list),
1857 list_empty(&dev_priv->mm.flushing_list));
1858 /* If we didn't do any of the above, there's nothing to be done
1859 * and we just can't fit it in.
1867 i915_gem_evict_everything(struct drm_device *dev)
1872 ret = i915_gem_evict_something(dev);
1882 i915_gem_object_get_pages(struct drm_gem_object *obj)
1884 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1886 struct address_space *mapping;
1887 struct inode *inode;
1891 if (obj_priv->pages_refcount++ != 0)
1894 /* Get the list of pages out of our struct file. They'll be pinned
1895 * at this point until we release them.
1897 page_count = obj->size / PAGE_SIZE;
1898 BUG_ON(obj_priv->pages != NULL);
1899 obj_priv->pages = drm_calloc(page_count, sizeof(struct page *),
1901 if (obj_priv->pages == NULL) {
1902 DRM_ERROR("Faled to allocate page list\n");
1903 obj_priv->pages_refcount--;
1907 inode = obj->filp->f_path.dentry->d_inode;
1908 mapping = inode->i_mapping;
1909 for (i = 0; i < page_count; i++) {
1910 page = read_mapping_page(mapping, i, NULL);
1912 ret = PTR_ERR(page);
1913 DRM_ERROR("read_mapping_page failed: %d\n", ret);
1914 i915_gem_object_put_pages(obj);
1917 obj_priv->pages[i] = page;
1922 static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
1924 struct drm_gem_object *obj = reg->obj;
1925 struct drm_device *dev = obj->dev;
1926 drm_i915_private_t *dev_priv = dev->dev_private;
1927 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1928 int regnum = obj_priv->fence_reg;
1931 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
1933 val |= obj_priv->gtt_offset & 0xfffff000;
1934 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
1935 if (obj_priv->tiling_mode == I915_TILING_Y)
1936 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
1937 val |= I965_FENCE_REG_VALID;
1939 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
1942 static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
1944 struct drm_gem_object *obj = reg->obj;
1945 struct drm_device *dev = obj->dev;
1946 drm_i915_private_t *dev_priv = dev->dev_private;
1947 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1948 int regnum = obj_priv->fence_reg;
1950 uint32_t fence_reg, val;
1953 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1954 (obj_priv->gtt_offset & (obj->size - 1))) {
1955 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
1956 __func__, obj_priv->gtt_offset, obj->size);
1960 if (obj_priv->tiling_mode == I915_TILING_Y &&
1961 HAS_128_BYTE_Y_TILING(dev))
1966 /* Note: pitch better be a power of two tile widths */
1967 pitch_val = obj_priv->stride / tile_width;
1968 pitch_val = ffs(pitch_val) - 1;
1970 val = obj_priv->gtt_offset;
1971 if (obj_priv->tiling_mode == I915_TILING_Y)
1972 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
1973 val |= I915_FENCE_SIZE_BITS(obj->size);
1974 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
1975 val |= I830_FENCE_REG_VALID;
1978 fence_reg = FENCE_REG_830_0 + (regnum * 4);
1980 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
1981 I915_WRITE(fence_reg, val);
1984 static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
1986 struct drm_gem_object *obj = reg->obj;
1987 struct drm_device *dev = obj->dev;
1988 drm_i915_private_t *dev_priv = dev->dev_private;
1989 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1990 int regnum = obj_priv->fence_reg;
1994 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1995 (obj_priv->gtt_offset & (obj->size - 1))) {
1996 WARN(1, "%s: object 0x%08x not 1M or size aligned\n",
1997 __func__, obj_priv->gtt_offset);
2001 pitch_val = (obj_priv->stride / 128) - 1;
2003 val = obj_priv->gtt_offset;
2004 if (obj_priv->tiling_mode == I915_TILING_Y)
2005 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2006 val |= I830_FENCE_SIZE_BITS(obj->size);
2007 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2008 val |= I830_FENCE_REG_VALID;
2010 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
2015 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2016 * @obj: object to map through a fence reg
2017 * @write: object is about to be written
2019 * When mapping objects through the GTT, userspace wants to be able to write
2020 * to them without having to worry about swizzling if the object is tiled.
2022 * This function walks the fence regs looking for a free one for @obj,
2023 * stealing one if it can't find any.
2025 * It then sets up the reg based on the object's properties: address, pitch
2026 * and tiling format.
2029 i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write)
2031 struct drm_device *dev = obj->dev;
2032 struct drm_i915_private *dev_priv = dev->dev_private;
2033 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2034 struct drm_i915_fence_reg *reg = NULL;
2035 struct drm_i915_gem_object *old_obj_priv = NULL;
2038 switch (obj_priv->tiling_mode) {
2039 case I915_TILING_NONE:
2040 WARN(1, "allocating a fence for non-tiled object?\n");
2043 if (!obj_priv->stride)
2045 WARN((obj_priv->stride & (512 - 1)),
2046 "object 0x%08x is X tiled but has non-512B pitch\n",
2047 obj_priv->gtt_offset);
2050 if (!obj_priv->stride)
2052 WARN((obj_priv->stride & (128 - 1)),
2053 "object 0x%08x is Y tiled but has non-128B pitch\n",
2054 obj_priv->gtt_offset);
2058 /* First try to find a free reg */
2061 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2062 reg = &dev_priv->fence_regs[i];
2066 old_obj_priv = reg->obj->driver_private;
2067 if (!old_obj_priv->pin_count)
2071 /* None available, try to steal one or wait for a user to finish */
2072 if (i == dev_priv->num_fence_regs) {
2073 uint32_t seqno = dev_priv->mm.next_gem_seqno;
2079 for (i = dev_priv->fence_reg_start;
2080 i < dev_priv->num_fence_regs; i++) {
2081 uint32_t this_seqno;
2083 reg = &dev_priv->fence_regs[i];
2084 old_obj_priv = reg->obj->driver_private;
2086 if (old_obj_priv->pin_count)
2089 /* i915 uses fences for GPU access to tiled buffers */
2090 if (IS_I965G(dev) || !old_obj_priv->active)
2093 /* find the seqno of the first available fence */
2094 this_seqno = old_obj_priv->last_rendering_seqno;
2095 if (this_seqno != 0 &&
2096 reg->obj->write_domain == 0 &&
2097 i915_seqno_passed(seqno, this_seqno))
2102 * Now things get ugly... we have to wait for one of the
2103 * objects to finish before trying again.
2105 if (i == dev_priv->num_fence_regs) {
2106 if (seqno == dev_priv->mm.next_gem_seqno) {
2108 I915_GEM_GPU_DOMAINS,
2109 I915_GEM_GPU_DOMAINS);
2110 seqno = i915_add_request(dev,
2111 I915_GEM_GPU_DOMAINS);
2116 ret = i915_wait_request(dev, seqno);
2122 BUG_ON(old_obj_priv->active ||
2123 (reg->obj->write_domain & I915_GEM_GPU_DOMAINS));
2126 * Zap this virtual mapping so we can set up a fence again
2127 * for this object next time we need it.
2129 offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
2130 if (dev->dev_mapping)
2131 unmap_mapping_range(dev->dev_mapping, offset,
2133 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
2136 obj_priv->fence_reg = i;
2140 i965_write_fence_reg(reg);
2141 else if (IS_I9XX(dev))
2142 i915_write_fence_reg(reg);
2144 i830_write_fence_reg(reg);
2150 * i915_gem_clear_fence_reg - clear out fence register info
2151 * @obj: object to clear
2153 * Zeroes out the fence register itself and clears out the associated
2154 * data structures in dev_priv and obj_priv.
2157 i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2159 struct drm_device *dev = obj->dev;
2160 drm_i915_private_t *dev_priv = dev->dev_private;
2161 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2164 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
2168 if (obj_priv->fence_reg < 8)
2169 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2171 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2174 I915_WRITE(fence_reg, 0);
2177 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2178 obj_priv->fence_reg = I915_FENCE_REG_NONE;
2182 * Finds free space in the GTT aperture and binds the object there.
2185 i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2187 struct drm_device *dev = obj->dev;
2188 drm_i915_private_t *dev_priv = dev->dev_private;
2189 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2190 struct drm_mm_node *free_space;
2191 int page_count, ret;
2193 if (dev_priv->mm.suspended)
2196 alignment = i915_gem_get_gtt_alignment(obj);
2197 if (alignment & (PAGE_SIZE - 1)) {
2198 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2203 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2204 obj->size, alignment, 0);
2205 if (free_space != NULL) {
2206 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2208 if (obj_priv->gtt_space != NULL) {
2209 obj_priv->gtt_space->private = obj;
2210 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2213 if (obj_priv->gtt_space == NULL) {
2214 /* If the gtt is empty and we're still having trouble
2215 * fitting our object in, we're out of memory.
2218 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2220 if (list_empty(&dev_priv->mm.inactive_list) &&
2221 list_empty(&dev_priv->mm.flushing_list) &&
2222 list_empty(&dev_priv->mm.active_list)) {
2223 DRM_ERROR("GTT full, but LRU list empty\n");
2227 ret = i915_gem_evict_something(dev);
2229 if (ret != -ERESTARTSYS)
2230 DRM_ERROR("Failed to evict a buffer %d\n", ret);
2237 DRM_INFO("Binding object of size %d at 0x%08x\n",
2238 obj->size, obj_priv->gtt_offset);
2240 ret = i915_gem_object_get_pages(obj);
2242 drm_mm_put_block(obj_priv->gtt_space);
2243 obj_priv->gtt_space = NULL;
2247 page_count = obj->size / PAGE_SIZE;
2248 /* Create an AGP memory structure pointing at our pages, and bind it
2251 obj_priv->agp_mem = drm_agp_bind_pages(dev,
2254 obj_priv->gtt_offset,
2255 obj_priv->agp_type);
2256 if (obj_priv->agp_mem == NULL) {
2257 i915_gem_object_put_pages(obj);
2258 drm_mm_put_block(obj_priv->gtt_space);
2259 obj_priv->gtt_space = NULL;
2262 atomic_inc(&dev->gtt_count);
2263 atomic_add(obj->size, &dev->gtt_memory);
2265 /* Assert that the object is not currently in any GPU domain. As it
2266 * wasn't in the GTT, there shouldn't be any way it could have been in
2269 BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2270 BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2276 i915_gem_clflush_object(struct drm_gem_object *obj)
2278 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2280 /* If we don't have a page list set up, then we're not pinned
2281 * to GPU, and we can ignore the cache flush because it'll happen
2282 * again at bind time.
2284 if (obj_priv->pages == NULL)
2287 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
2290 /** Flushes any GPU write domain for the object if it's dirty. */
2292 i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2294 struct drm_device *dev = obj->dev;
2297 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2300 /* Queue the GPU write cache flushing we need. */
2301 i915_gem_flush(dev, 0, obj->write_domain);
2302 seqno = i915_add_request(dev, obj->write_domain);
2303 obj->write_domain = 0;
2304 i915_gem_object_move_to_active(obj, seqno);
2307 /** Flushes the GTT write domain for the object if it's dirty. */
2309 i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2311 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2314 /* No actual flushing is required for the GTT write domain. Writes
2315 * to it immediately go to main memory as far as we know, so there's
2316 * no chipset flush. It also doesn't land in render cache.
2318 obj->write_domain = 0;
2321 /** Flushes the CPU write domain for the object if it's dirty. */
2323 i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2325 struct drm_device *dev = obj->dev;
2327 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2330 i915_gem_clflush_object(obj);
2331 drm_agp_chipset_flush(dev);
2332 obj->write_domain = 0;
2336 * Moves a single object to the GTT read, and possibly write domain.
2338 * This function returns when the move is complete, including waiting on
2342 i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2344 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2347 /* Not valid to be called on unbound objects. */
2348 if (obj_priv->gtt_space == NULL)
2351 i915_gem_object_flush_gpu_write_domain(obj);
2352 /* Wait on any GPU rendering and flushing to occur. */
2353 ret = i915_gem_object_wait_rendering(obj);
2357 /* If we're writing through the GTT domain, then CPU and GPU caches
2358 * will need to be invalidated at next use.
2361 obj->read_domains &= I915_GEM_DOMAIN_GTT;
2363 i915_gem_object_flush_cpu_write_domain(obj);
2365 /* It should now be out of any other write domains, and we can update
2366 * the domain values for our changes.
2368 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2369 obj->read_domains |= I915_GEM_DOMAIN_GTT;
2371 obj->write_domain = I915_GEM_DOMAIN_GTT;
2372 obj_priv->dirty = 1;
2379 * Moves a single object to the CPU read, and possibly write domain.
2381 * This function returns when the move is complete, including waiting on
2385 i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2389 i915_gem_object_flush_gpu_write_domain(obj);
2390 /* Wait on any GPU rendering and flushing to occur. */
2391 ret = i915_gem_object_wait_rendering(obj);
2395 i915_gem_object_flush_gtt_write_domain(obj);
2397 /* If we have a partially-valid cache of the object in the CPU,
2398 * finish invalidating it and free the per-page flags.
2400 i915_gem_object_set_to_full_cpu_read_domain(obj);
2402 /* Flush the CPU cache if it's still invalid. */
2403 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2404 i915_gem_clflush_object(obj);
2406 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2409 /* It should now be out of any other write domains, and we can update
2410 * the domain values for our changes.
2412 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2414 /* If we're writing through the CPU, then the GPU read domains will
2415 * need to be invalidated at next use.
2418 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2419 obj->write_domain = I915_GEM_DOMAIN_CPU;
2426 * Set the next domain for the specified object. This
2427 * may not actually perform the necessary flushing/invaliding though,
2428 * as that may want to be batched with other set_domain operations
2430 * This is (we hope) the only really tricky part of gem. The goal
2431 * is fairly simple -- track which caches hold bits of the object
2432 * and make sure they remain coherent. A few concrete examples may
2433 * help to explain how it works. For shorthand, we use the notation
2434 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2435 * a pair of read and write domain masks.
2437 * Case 1: the batch buffer
2443 * 5. Unmapped from GTT
2446 * Let's take these a step at a time
2449 * Pages allocated from the kernel may still have
2450 * cache contents, so we set them to (CPU, CPU) always.
2451 * 2. Written by CPU (using pwrite)
2452 * The pwrite function calls set_domain (CPU, CPU) and
2453 * this function does nothing (as nothing changes)
2455 * This function asserts that the object is not
2456 * currently in any GPU-based read or write domains
2458 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2459 * As write_domain is zero, this function adds in the
2460 * current read domains (CPU+COMMAND, 0).
2461 * flush_domains is set to CPU.
2462 * invalidate_domains is set to COMMAND
2463 * clflush is run to get data out of the CPU caches
2464 * then i915_dev_set_domain calls i915_gem_flush to
2465 * emit an MI_FLUSH and drm_agp_chipset_flush
2466 * 5. Unmapped from GTT
2467 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2468 * flush_domains and invalidate_domains end up both zero
2469 * so no flushing/invalidating happens
2473 * Case 2: The shared render buffer
2477 * 3. Read/written by GPU
2478 * 4. set_domain to (CPU,CPU)
2479 * 5. Read/written by CPU
2480 * 6. Read/written by GPU
2483 * Same as last example, (CPU, CPU)
2485 * Nothing changes (assertions find that it is not in the GPU)
2486 * 3. Read/written by GPU
2487 * execbuffer calls set_domain (RENDER, RENDER)
2488 * flush_domains gets CPU
2489 * invalidate_domains gets GPU
2491 * MI_FLUSH and drm_agp_chipset_flush
2492 * 4. set_domain (CPU, CPU)
2493 * flush_domains gets GPU
2494 * invalidate_domains gets CPU
2495 * wait_rendering (obj) to make sure all drawing is complete.
2496 * This will include an MI_FLUSH to get the data from GPU
2498 * clflush (obj) to invalidate the CPU cache
2499 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2500 * 5. Read/written by CPU
2501 * cache lines are loaded and dirtied
2502 * 6. Read written by GPU
2503 * Same as last GPU access
2505 * Case 3: The constant buffer
2510 * 4. Updated (written) by CPU again
2519 * flush_domains = CPU
2520 * invalidate_domains = RENDER
2523 * drm_agp_chipset_flush
2524 * 4. Updated (written) by CPU again
2526 * flush_domains = 0 (no previous write domain)
2527 * invalidate_domains = 0 (no new read domains)
2530 * flush_domains = CPU
2531 * invalidate_domains = RENDER
2534 * drm_agp_chipset_flush
2537 i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
2539 struct drm_device *dev = obj->dev;
2540 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2541 uint32_t invalidate_domains = 0;
2542 uint32_t flush_domains = 0;
2544 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2545 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
2548 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2550 obj->read_domains, obj->pending_read_domains,
2551 obj->write_domain, obj->pending_write_domain);
2554 * If the object isn't moving to a new write domain,
2555 * let the object stay in multiple read domains
2557 if (obj->pending_write_domain == 0)
2558 obj->pending_read_domains |= obj->read_domains;
2560 obj_priv->dirty = 1;
2563 * Flush the current write domain if
2564 * the new read domains don't match. Invalidate
2565 * any read domains which differ from the old
2568 if (obj->write_domain &&
2569 obj->write_domain != obj->pending_read_domains) {
2570 flush_domains |= obj->write_domain;
2571 invalidate_domains |=
2572 obj->pending_read_domains & ~obj->write_domain;
2575 * Invalidate any read caches which may have
2576 * stale data. That is, any new read domains.
2578 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
2579 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2581 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2582 __func__, flush_domains, invalidate_domains);
2584 i915_gem_clflush_object(obj);
2587 /* The actual obj->write_domain will be updated with
2588 * pending_write_domain after we emit the accumulated flush for all
2589 * of our domain changes in execbuffers (which clears objects'
2590 * write_domains). So if we have a current write domain that we
2591 * aren't changing, set pending_write_domain to that.
2593 if (flush_domains == 0 && obj->pending_write_domain == 0)
2594 obj->pending_write_domain = obj->write_domain;
2595 obj->read_domains = obj->pending_read_domains;
2597 dev->invalidate_domains |= invalidate_domains;
2598 dev->flush_domains |= flush_domains;
2600 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2602 obj->read_domains, obj->write_domain,
2603 dev->invalidate_domains, dev->flush_domains);
2608 * Moves the object from a partially CPU read to a full one.
2610 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2611 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2614 i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2616 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2618 if (!obj_priv->page_cpu_valid)
2621 /* If we're partially in the CPU read domain, finish moving it in.
2623 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2626 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2627 if (obj_priv->page_cpu_valid[i])
2629 drm_clflush_pages(obj_priv->pages + i, 1);
2633 /* Free the page_cpu_valid mappings which are now stale, whether
2634 * or not we've got I915_GEM_DOMAIN_CPU.
2636 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
2638 obj_priv->page_cpu_valid = NULL;
2642 * Set the CPU read domain on a range of the object.
2644 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2645 * not entirely valid. The page_cpu_valid member of the object flags which
2646 * pages have been flushed, and will be respected by
2647 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2648 * of the whole object.
2650 * This function returns when the move is complete, including waiting on
2654 i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2655 uint64_t offset, uint64_t size)
2657 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2660 if (offset == 0 && size == obj->size)
2661 return i915_gem_object_set_to_cpu_domain(obj, 0);
2663 i915_gem_object_flush_gpu_write_domain(obj);
2664 /* Wait on any GPU rendering and flushing to occur. */
2665 ret = i915_gem_object_wait_rendering(obj);
2668 i915_gem_object_flush_gtt_write_domain(obj);
2670 /* If we're already fully in the CPU read domain, we're done. */
2671 if (obj_priv->page_cpu_valid == NULL &&
2672 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
2675 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2676 * newly adding I915_GEM_DOMAIN_CPU
2678 if (obj_priv->page_cpu_valid == NULL) {
2679 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
2681 if (obj_priv->page_cpu_valid == NULL)
2683 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2684 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
2686 /* Flush the cache on any pages that are still invalid from the CPU's
2689 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2691 if (obj_priv->page_cpu_valid[i])
2694 drm_clflush_pages(obj_priv->pages + i, 1);
2696 obj_priv->page_cpu_valid[i] = 1;
2699 /* It should now be out of any other write domains, and we can update
2700 * the domain values for our changes.
2702 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2704 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2710 * Pin an object to the GTT and evaluate the relocations landing in it.
2713 i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2714 struct drm_file *file_priv,
2715 struct drm_i915_gem_exec_object *entry,
2716 struct drm_i915_gem_relocation_entry *relocs)
2718 struct drm_device *dev = obj->dev;
2719 drm_i915_private_t *dev_priv = dev->dev_private;
2720 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2722 void __iomem *reloc_page;
2724 /* Choose the GTT offset for our buffer and put it there. */
2725 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2729 entry->offset = obj_priv->gtt_offset;
2731 /* Apply the relocations, using the GTT aperture to avoid cache
2732 * flushing requirements.
2734 for (i = 0; i < entry->relocation_count; i++) {
2735 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
2736 struct drm_gem_object *target_obj;
2737 struct drm_i915_gem_object *target_obj_priv;
2738 uint32_t reloc_val, reloc_offset;
2739 uint32_t __iomem *reloc_entry;
2741 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
2742 reloc->target_handle);
2743 if (target_obj == NULL) {
2744 i915_gem_object_unpin(obj);
2747 target_obj_priv = target_obj->driver_private;
2749 /* The target buffer should have appeared before us in the
2750 * exec_object list, so it should have a GTT space bound by now.
2752 if (target_obj_priv->gtt_space == NULL) {
2753 DRM_ERROR("No GTT space found for object %d\n",
2754 reloc->target_handle);
2755 drm_gem_object_unreference(target_obj);
2756 i915_gem_object_unpin(obj);
2760 if (reloc->offset > obj->size - 4) {
2761 DRM_ERROR("Relocation beyond object bounds: "
2762 "obj %p target %d offset %d size %d.\n",
2763 obj, reloc->target_handle,
2764 (int) reloc->offset, (int) obj->size);
2765 drm_gem_object_unreference(target_obj);
2766 i915_gem_object_unpin(obj);
2769 if (reloc->offset & 3) {
2770 DRM_ERROR("Relocation not 4-byte aligned: "
2771 "obj %p target %d offset %d.\n",
2772 obj, reloc->target_handle,
2773 (int) reloc->offset);
2774 drm_gem_object_unreference(target_obj);
2775 i915_gem_object_unpin(obj);
2779 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
2780 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
2781 DRM_ERROR("reloc with read/write CPU domains: "
2782 "obj %p target %d offset %d "
2783 "read %08x write %08x",
2784 obj, reloc->target_handle,
2785 (int) reloc->offset,
2786 reloc->read_domains,
2787 reloc->write_domain);
2788 drm_gem_object_unreference(target_obj);
2789 i915_gem_object_unpin(obj);
2793 if (reloc->write_domain && target_obj->pending_write_domain &&
2794 reloc->write_domain != target_obj->pending_write_domain) {
2795 DRM_ERROR("Write domain conflict: "
2796 "obj %p target %d offset %d "
2797 "new %08x old %08x\n",
2798 obj, reloc->target_handle,
2799 (int) reloc->offset,
2800 reloc->write_domain,
2801 target_obj->pending_write_domain);
2802 drm_gem_object_unreference(target_obj);
2803 i915_gem_object_unpin(obj);
2808 DRM_INFO("%s: obj %p offset %08x target %d "
2809 "read %08x write %08x gtt %08x "
2810 "presumed %08x delta %08x\n",
2813 (int) reloc->offset,
2814 (int) reloc->target_handle,
2815 (int) reloc->read_domains,
2816 (int) reloc->write_domain,
2817 (int) target_obj_priv->gtt_offset,
2818 (int) reloc->presumed_offset,
2822 target_obj->pending_read_domains |= reloc->read_domains;
2823 target_obj->pending_write_domain |= reloc->write_domain;
2825 /* If the relocation already has the right value in it, no
2826 * more work needs to be done.
2828 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
2829 drm_gem_object_unreference(target_obj);
2833 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
2835 drm_gem_object_unreference(target_obj);
2836 i915_gem_object_unpin(obj);
2840 /* Map the page containing the relocation we're going to
2843 reloc_offset = obj_priv->gtt_offset + reloc->offset;
2844 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
2847 reloc_entry = (uint32_t __iomem *)(reloc_page +
2848 (reloc_offset & (PAGE_SIZE - 1)));
2849 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
2852 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
2853 obj, (unsigned int) reloc->offset,
2854 readl(reloc_entry), reloc_val);
2856 writel(reloc_val, reloc_entry);
2857 io_mapping_unmap_atomic(reloc_page);
2859 /* The updated presumed offset for this entry will be
2860 * copied back out to the user.
2862 reloc->presumed_offset = target_obj_priv->gtt_offset;
2864 drm_gem_object_unreference(target_obj);
2869 i915_gem_dump_object(obj, 128, __func__, ~0);
2874 /** Dispatch a batchbuffer to the ring
2877 i915_dispatch_gem_execbuffer(struct drm_device *dev,
2878 struct drm_i915_gem_execbuffer *exec,
2879 struct drm_clip_rect *cliprects,
2880 uint64_t exec_offset)
2882 drm_i915_private_t *dev_priv = dev->dev_private;
2883 int nbox = exec->num_cliprects;
2885 uint32_t exec_start, exec_len;
2888 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
2889 exec_len = (uint32_t) exec->batch_len;
2891 if ((exec_start | exec_len) & 0x7) {
2892 DRM_ERROR("alignment\n");
2899 count = nbox ? nbox : 1;
2901 for (i = 0; i < count; i++) {
2903 int ret = i915_emit_box(dev, cliprects, i,
2904 exec->DR1, exec->DR4);
2909 if (IS_I830(dev) || IS_845G(dev)) {
2911 OUT_RING(MI_BATCH_BUFFER);
2912 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2913 OUT_RING(exec_start + exec_len - 4);
2918 if (IS_I965G(dev)) {
2919 OUT_RING(MI_BATCH_BUFFER_START |
2921 MI_BATCH_NON_SECURE_I965);
2922 OUT_RING(exec_start);
2924 OUT_RING(MI_BATCH_BUFFER_START |
2926 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2932 /* XXX breadcrumb */
2936 /* Throttle our rendering by waiting until the ring has completed our requests
2937 * emitted over 20 msec ago.
2939 * This should get us reasonable parallelism between CPU and GPU but also
2940 * relatively low latency when blocking on a particular request to finish.
2943 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
2945 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2949 mutex_lock(&dev->struct_mutex);
2950 seqno = i915_file_priv->mm.last_gem_throttle_seqno;
2951 i915_file_priv->mm.last_gem_throttle_seqno =
2952 i915_file_priv->mm.last_gem_seqno;
2954 ret = i915_wait_request(dev, seqno);
2955 mutex_unlock(&dev->struct_mutex);
2960 i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
2961 uint32_t buffer_count,
2962 struct drm_i915_gem_relocation_entry **relocs)
2964 uint32_t reloc_count = 0, reloc_index = 0, i;
2968 for (i = 0; i < buffer_count; i++) {
2969 if (reloc_count + exec_list[i].relocation_count < reloc_count)
2971 reloc_count += exec_list[i].relocation_count;
2974 *relocs = drm_calloc(reloc_count, sizeof(**relocs), DRM_MEM_DRIVER);
2975 if (*relocs == NULL)
2978 for (i = 0; i < buffer_count; i++) {
2979 struct drm_i915_gem_relocation_entry __user *user_relocs;
2981 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
2983 ret = copy_from_user(&(*relocs)[reloc_index],
2985 exec_list[i].relocation_count *
2988 drm_free(*relocs, reloc_count * sizeof(**relocs),
2994 reloc_index += exec_list[i].relocation_count;
3001 i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3002 uint32_t buffer_count,
3003 struct drm_i915_gem_relocation_entry *relocs)
3005 uint32_t reloc_count = 0, i;
3008 for (i = 0; i < buffer_count; i++) {
3009 struct drm_i915_gem_relocation_entry __user *user_relocs;
3011 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3014 ret = copy_to_user(user_relocs,
3015 &relocs[reloc_count],
3016 exec_list[i].relocation_count *
3020 reloc_count += exec_list[i].relocation_count;
3023 drm_free(relocs, reloc_count * sizeof(*relocs), DRM_MEM_DRIVER);
3029 i915_gem_execbuffer(struct drm_device *dev, void *data,
3030 struct drm_file *file_priv)
3032 drm_i915_private_t *dev_priv = dev->dev_private;
3033 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3034 struct drm_i915_gem_execbuffer *args = data;
3035 struct drm_i915_gem_exec_object *exec_list = NULL;
3036 struct drm_gem_object **object_list = NULL;
3037 struct drm_gem_object *batch_obj;
3038 struct drm_i915_gem_object *obj_priv;
3039 struct drm_clip_rect *cliprects = NULL;
3040 struct drm_i915_gem_relocation_entry *relocs;
3041 int ret, ret2, i, pinned = 0;
3042 uint64_t exec_offset;
3043 uint32_t seqno, flush_domains, reloc_index;
3047 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3048 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3051 if (args->buffer_count < 1) {
3052 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3055 /* Copy in the exec list from userland */
3056 exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
3058 object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
3060 if (exec_list == NULL || object_list == NULL) {
3061 DRM_ERROR("Failed to allocate exec or object list "
3063 args->buffer_count);
3067 ret = copy_from_user(exec_list,
3068 (struct drm_i915_relocation_entry __user *)
3069 (uintptr_t) args->buffers_ptr,
3070 sizeof(*exec_list) * args->buffer_count);
3072 DRM_ERROR("copy %d exec entries failed %d\n",
3073 args->buffer_count, ret);
3077 if (args->num_cliprects != 0) {
3078 cliprects = drm_calloc(args->num_cliprects, sizeof(*cliprects),
3080 if (cliprects == NULL)
3083 ret = copy_from_user(cliprects,
3084 (struct drm_clip_rect __user *)
3085 (uintptr_t) args->cliprects_ptr,
3086 sizeof(*cliprects) * args->num_cliprects);
3088 DRM_ERROR("copy %d cliprects failed: %d\n",
3089 args->num_cliprects, ret);
3094 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3099 mutex_lock(&dev->struct_mutex);
3101 i915_verify_inactive(dev, __FILE__, __LINE__);
3103 if (dev_priv->mm.wedged) {
3104 DRM_ERROR("Execbuf while wedged\n");
3105 mutex_unlock(&dev->struct_mutex);
3110 if (dev_priv->mm.suspended) {
3111 DRM_ERROR("Execbuf while VT-switched.\n");
3112 mutex_unlock(&dev->struct_mutex);
3117 /* Look up object handles */
3118 for (i = 0; i < args->buffer_count; i++) {
3119 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3120 exec_list[i].handle);
3121 if (object_list[i] == NULL) {
3122 DRM_ERROR("Invalid object handle %d at index %d\n",
3123 exec_list[i].handle, i);
3128 obj_priv = object_list[i]->driver_private;
3129 if (obj_priv->in_execbuffer) {
3130 DRM_ERROR("Object %p appears more than once in object list\n",
3135 obj_priv->in_execbuffer = true;
3138 /* Pin and relocate */
3139 for (pin_tries = 0; ; pin_tries++) {
3143 for (i = 0; i < args->buffer_count; i++) {
3144 object_list[i]->pending_read_domains = 0;
3145 object_list[i]->pending_write_domain = 0;
3146 ret = i915_gem_object_pin_and_relocate(object_list[i],
3149 &relocs[reloc_index]);
3153 reloc_index += exec_list[i].relocation_count;
3159 /* error other than GTT full, or we've already tried again */
3160 if (ret != -ENOMEM || pin_tries >= 1) {
3161 if (ret != -ERESTARTSYS)
3162 DRM_ERROR("Failed to pin buffers %d\n", ret);
3166 /* unpin all of our buffers */
3167 for (i = 0; i < pinned; i++)
3168 i915_gem_object_unpin(object_list[i]);
3171 /* evict everyone we can from the aperture */
3172 ret = i915_gem_evict_everything(dev);
3177 /* Set the pending read domains for the batch buffer to COMMAND */
3178 batch_obj = object_list[args->buffer_count-1];
3179 batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
3180 batch_obj->pending_write_domain = 0;
3182 i915_verify_inactive(dev, __FILE__, __LINE__);
3184 /* Zero the global flush/invalidate flags. These
3185 * will be modified as new domains are computed
3188 dev->invalidate_domains = 0;
3189 dev->flush_domains = 0;
3191 for (i = 0; i < args->buffer_count; i++) {
3192 struct drm_gem_object *obj = object_list[i];
3194 /* Compute new gpu domains and update invalidate/flush */
3195 i915_gem_object_set_to_gpu_domain(obj);
3198 i915_verify_inactive(dev, __FILE__, __LINE__);
3200 if (dev->invalidate_domains | dev->flush_domains) {
3202 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3204 dev->invalidate_domains,
3205 dev->flush_domains);
3208 dev->invalidate_domains,
3209 dev->flush_domains);
3210 if (dev->flush_domains)
3211 (void)i915_add_request(dev, dev->flush_domains);
3214 for (i = 0; i < args->buffer_count; i++) {
3215 struct drm_gem_object *obj = object_list[i];
3217 obj->write_domain = obj->pending_write_domain;
3220 i915_verify_inactive(dev, __FILE__, __LINE__);
3223 for (i = 0; i < args->buffer_count; i++) {
3224 i915_gem_object_check_coherency(object_list[i],
3225 exec_list[i].handle);
3229 exec_offset = exec_list[args->buffer_count - 1].offset;
3232 i915_gem_dump_object(object_list[args->buffer_count - 1],
3238 /* Exec the batchbuffer */
3239 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
3241 DRM_ERROR("dispatch failed %d\n", ret);
3246 * Ensure that the commands in the batch buffer are
3247 * finished before the interrupt fires
3249 flush_domains = i915_retire_commands(dev);
3251 i915_verify_inactive(dev, __FILE__, __LINE__);
3254 * Get a seqno representing the execution of the current buffer,
3255 * which we can wait on. We would like to mitigate these interrupts,
3256 * likely by only creating seqnos occasionally (so that we have
3257 * *some* interrupts representing completion of buffers that we can
3258 * wait on when trying to clear up gtt space).
3260 seqno = i915_add_request(dev, flush_domains);
3262 i915_file_priv->mm.last_gem_seqno = seqno;
3263 for (i = 0; i < args->buffer_count; i++) {
3264 struct drm_gem_object *obj = object_list[i];
3266 i915_gem_object_move_to_active(obj, seqno);
3268 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3272 i915_dump_lru(dev, __func__);
3275 i915_verify_inactive(dev, __FILE__, __LINE__);
3278 for (i = 0; i < pinned; i++)
3279 i915_gem_object_unpin(object_list[i]);
3281 for (i = 0; i < args->buffer_count; i++) {
3282 if (object_list[i]) {
3283 obj_priv = object_list[i]->driver_private;
3284 obj_priv->in_execbuffer = false;
3286 drm_gem_object_unreference(object_list[i]);
3289 mutex_unlock(&dev->struct_mutex);
3292 /* Copy the new buffer offsets back to the user's exec list. */
3293 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3294 (uintptr_t) args->buffers_ptr,
3296 sizeof(*exec_list) * args->buffer_count);
3298 DRM_ERROR("failed to copy %d exec entries "
3299 "back to user (%d)\n",
3300 args->buffer_count, ret);
3303 /* Copy the updated relocations out regardless of current error
3304 * state. Failure to update the relocs would mean that the next
3305 * time userland calls execbuf, it would do so with presumed offset
3306 * state that didn't match the actual object state.
3308 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3311 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3318 drm_free(object_list, sizeof(*object_list) * args->buffer_count,
3320 drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
3322 drm_free(cliprects, sizeof(*cliprects) * args->num_cliprects,
3329 i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3331 struct drm_device *dev = obj->dev;
3332 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3335 i915_verify_inactive(dev, __FILE__, __LINE__);
3336 if (obj_priv->gtt_space == NULL) {
3337 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3339 if (ret != -EBUSY && ret != -ERESTARTSYS)
3340 DRM_ERROR("Failure to bind: %d\n", ret);
3345 * Pre-965 chips need a fence register set up in order to
3346 * properly handle tiled surfaces.
3348 if (!IS_I965G(dev) &&
3349 obj_priv->fence_reg == I915_FENCE_REG_NONE &&
3350 obj_priv->tiling_mode != I915_TILING_NONE) {
3351 ret = i915_gem_object_get_fence_reg(obj, true);
3353 if (ret != -EBUSY && ret != -ERESTARTSYS)
3354 DRM_ERROR("Failure to install fence: %d\n",
3359 obj_priv->pin_count++;
3361 /* If the object is not active and not pending a flush,
3362 * remove it from the inactive list
3364 if (obj_priv->pin_count == 1) {
3365 atomic_inc(&dev->pin_count);
3366 atomic_add(obj->size, &dev->pin_memory);
3367 if (!obj_priv->active &&
3368 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
3369 I915_GEM_DOMAIN_GTT)) == 0 &&
3370 !list_empty(&obj_priv->list))
3371 list_del_init(&obj_priv->list);
3373 i915_verify_inactive(dev, __FILE__, __LINE__);
3379 i915_gem_object_unpin(struct drm_gem_object *obj)
3381 struct drm_device *dev = obj->dev;
3382 drm_i915_private_t *dev_priv = dev->dev_private;
3383 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3385 i915_verify_inactive(dev, __FILE__, __LINE__);
3386 obj_priv->pin_count--;
3387 BUG_ON(obj_priv->pin_count < 0);
3388 BUG_ON(obj_priv->gtt_space == NULL);
3390 /* If the object is no longer pinned, and is
3391 * neither active nor being flushed, then stick it on
3394 if (obj_priv->pin_count == 0) {
3395 if (!obj_priv->active &&
3396 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
3397 I915_GEM_DOMAIN_GTT)) == 0)
3398 list_move_tail(&obj_priv->list,
3399 &dev_priv->mm.inactive_list);
3400 atomic_dec(&dev->pin_count);
3401 atomic_sub(obj->size, &dev->pin_memory);
3403 i915_verify_inactive(dev, __FILE__, __LINE__);
3407 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3408 struct drm_file *file_priv)
3410 struct drm_i915_gem_pin *args = data;
3411 struct drm_gem_object *obj;
3412 struct drm_i915_gem_object *obj_priv;
3415 mutex_lock(&dev->struct_mutex);
3417 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3419 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3421 mutex_unlock(&dev->struct_mutex);
3424 obj_priv = obj->driver_private;
3426 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3427 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3429 drm_gem_object_unreference(obj);
3430 mutex_unlock(&dev->struct_mutex);
3434 obj_priv->user_pin_count++;
3435 obj_priv->pin_filp = file_priv;
3436 if (obj_priv->user_pin_count == 1) {
3437 ret = i915_gem_object_pin(obj, args->alignment);
3439 drm_gem_object_unreference(obj);
3440 mutex_unlock(&dev->struct_mutex);
3445 /* XXX - flush the CPU caches for pinned objects
3446 * as the X server doesn't manage domains yet
3448 i915_gem_object_flush_cpu_write_domain(obj);
3449 args->offset = obj_priv->gtt_offset;
3450 drm_gem_object_unreference(obj);
3451 mutex_unlock(&dev->struct_mutex);
3457 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3458 struct drm_file *file_priv)
3460 struct drm_i915_gem_pin *args = data;
3461 struct drm_gem_object *obj;
3462 struct drm_i915_gem_object *obj_priv;
3464 mutex_lock(&dev->struct_mutex);
3466 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3468 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3470 mutex_unlock(&dev->struct_mutex);
3474 obj_priv = obj->driver_private;
3475 if (obj_priv->pin_filp != file_priv) {
3476 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3478 drm_gem_object_unreference(obj);
3479 mutex_unlock(&dev->struct_mutex);
3482 obj_priv->user_pin_count--;
3483 if (obj_priv->user_pin_count == 0) {
3484 obj_priv->pin_filp = NULL;
3485 i915_gem_object_unpin(obj);
3488 drm_gem_object_unreference(obj);
3489 mutex_unlock(&dev->struct_mutex);
3494 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3495 struct drm_file *file_priv)
3497 struct drm_i915_gem_busy *args = data;
3498 struct drm_gem_object *obj;
3499 struct drm_i915_gem_object *obj_priv;
3501 mutex_lock(&dev->struct_mutex);
3502 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3504 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3506 mutex_unlock(&dev->struct_mutex);
3510 /* Update the active list for the hardware's current position.
3511 * Otherwise this only updates on a delayed timer or when irqs are
3512 * actually unmasked, and our working set ends up being larger than
3515 i915_gem_retire_requests(dev);
3517 obj_priv = obj->driver_private;
3518 /* Don't count being on the flushing list against the object being
3519 * done. Otherwise, a buffer left on the flushing list but not getting
3520 * flushed (because nobody's flushing that domain) won't ever return
3521 * unbusy and get reused by libdrm's bo cache. The other expected
3522 * consumer of this interface, OpenGL's occlusion queries, also specs
3523 * that the objects get unbusy "eventually" without any interference.
3525 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
3527 drm_gem_object_unreference(obj);
3528 mutex_unlock(&dev->struct_mutex);
3533 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3534 struct drm_file *file_priv)
3536 return i915_gem_ring_throttle(dev, file_priv);
3539 int i915_gem_init_object(struct drm_gem_object *obj)
3541 struct drm_i915_gem_object *obj_priv;
3543 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
3544 if (obj_priv == NULL)
3548 * We've just allocated pages from the kernel,
3549 * so they've just been written by the CPU with
3550 * zeros. They'll need to be clflushed before we
3551 * use them with the GPU.
3553 obj->write_domain = I915_GEM_DOMAIN_CPU;
3554 obj->read_domains = I915_GEM_DOMAIN_CPU;
3556 obj_priv->agp_type = AGP_USER_MEMORY;
3558 obj->driver_private = obj_priv;
3559 obj_priv->obj = obj;
3560 obj_priv->fence_reg = I915_FENCE_REG_NONE;
3561 INIT_LIST_HEAD(&obj_priv->list);
3566 void i915_gem_free_object(struct drm_gem_object *obj)
3568 struct drm_device *dev = obj->dev;
3569 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3571 while (obj_priv->pin_count > 0)
3572 i915_gem_object_unpin(obj);
3574 if (obj_priv->phys_obj)
3575 i915_gem_detach_phys_object(dev, obj);
3577 i915_gem_object_unbind(obj);
3579 i915_gem_free_mmap_offset(obj);
3581 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
3582 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
3585 /** Unbinds all objects that are on the given buffer list. */
3587 i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3589 struct drm_gem_object *obj;
3590 struct drm_i915_gem_object *obj_priv;
3593 while (!list_empty(head)) {
3594 obj_priv = list_first_entry(head,
3595 struct drm_i915_gem_object,
3597 obj = obj_priv->obj;
3599 if (obj_priv->pin_count != 0) {
3600 DRM_ERROR("Pinned object in unbind list\n");
3601 mutex_unlock(&dev->struct_mutex);
3605 ret = i915_gem_object_unbind(obj);
3607 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3609 mutex_unlock(&dev->struct_mutex);
3619 i915_gem_idle(struct drm_device *dev)
3621 drm_i915_private_t *dev_priv = dev->dev_private;
3622 uint32_t seqno, cur_seqno, last_seqno;
3625 mutex_lock(&dev->struct_mutex);
3627 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3628 mutex_unlock(&dev->struct_mutex);
3632 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3633 * We need to replace this with a semaphore, or something.
3635 dev_priv->mm.suspended = 1;
3637 /* Cancel the retire work handler, wait for it to finish if running
3639 mutex_unlock(&dev->struct_mutex);
3640 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3641 mutex_lock(&dev->struct_mutex);
3643 i915_kernel_lost_context(dev);
3645 /* Flush the GPU along with all non-CPU write domains
3647 i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
3648 ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
3649 seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU);
3652 mutex_unlock(&dev->struct_mutex);
3656 dev_priv->mm.waiting_gem_seqno = seqno;
3660 cur_seqno = i915_get_gem_seqno(dev);
3661 if (i915_seqno_passed(cur_seqno, seqno))
3663 if (last_seqno == cur_seqno) {
3664 if (stuck++ > 100) {
3665 DRM_ERROR("hardware wedged\n");
3666 dev_priv->mm.wedged = 1;
3667 DRM_WAKEUP(&dev_priv->irq_queue);
3672 last_seqno = cur_seqno;
3674 dev_priv->mm.waiting_gem_seqno = 0;
3676 i915_gem_retire_requests(dev);
3678 if (!dev_priv->mm.wedged) {
3679 /* Active and flushing should now be empty as we've
3680 * waited for a sequence higher than any pending execbuffer
3682 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3683 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3684 /* Request should now be empty as we've also waited
3685 * for the last request in the list
3687 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3690 /* Empty the active and flushing lists to inactive. If there's
3691 * anything left at this point, it means that we're wedged and
3692 * nothing good's going to happen by leaving them there. So strip
3693 * the GPU domains and just stuff them onto inactive.
3695 while (!list_empty(&dev_priv->mm.active_list)) {
3696 struct drm_i915_gem_object *obj_priv;
3698 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3699 struct drm_i915_gem_object,
3701 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3702 i915_gem_object_move_to_inactive(obj_priv->obj);
3705 while (!list_empty(&dev_priv->mm.flushing_list)) {
3706 struct drm_i915_gem_object *obj_priv;
3708 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
3709 struct drm_i915_gem_object,
3711 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3712 i915_gem_object_move_to_inactive(obj_priv->obj);
3716 /* Move all inactive buffers out of the GTT. */
3717 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
3718 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
3720 mutex_unlock(&dev->struct_mutex);
3724 i915_gem_cleanup_ringbuffer(dev);
3725 mutex_unlock(&dev->struct_mutex);
3731 i915_gem_init_hws(struct drm_device *dev)
3733 drm_i915_private_t *dev_priv = dev->dev_private;
3734 struct drm_gem_object *obj;
3735 struct drm_i915_gem_object *obj_priv;
3738 /* If we need a physical address for the status page, it's already
3739 * initialized at driver load time.
3741 if (!I915_NEED_GFX_HWS(dev))
3744 obj = drm_gem_object_alloc(dev, 4096);
3746 DRM_ERROR("Failed to allocate status page\n");
3749 obj_priv = obj->driver_private;
3750 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
3752 ret = i915_gem_object_pin(obj, 4096);
3754 drm_gem_object_unreference(obj);
3758 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
3760 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
3761 if (dev_priv->hw_status_page == NULL) {
3762 DRM_ERROR("Failed to map status page.\n");
3763 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3764 i915_gem_object_unpin(obj);
3765 drm_gem_object_unreference(obj);
3768 dev_priv->hws_obj = obj;
3769 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
3770 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
3771 I915_READ(HWS_PGA); /* posting read */
3772 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
3778 i915_gem_cleanup_hws(struct drm_device *dev)
3780 drm_i915_private_t *dev_priv = dev->dev_private;
3781 struct drm_gem_object *obj;
3782 struct drm_i915_gem_object *obj_priv;
3784 if (dev_priv->hws_obj == NULL)
3787 obj = dev_priv->hws_obj;
3788 obj_priv = obj->driver_private;
3790 kunmap(obj_priv->pages[0]);
3791 i915_gem_object_unpin(obj);
3792 drm_gem_object_unreference(obj);
3793 dev_priv->hws_obj = NULL;
3795 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3796 dev_priv->hw_status_page = NULL;
3798 /* Write high address into HWS_PGA when disabling. */
3799 I915_WRITE(HWS_PGA, 0x1ffff000);
3803 i915_gem_init_ringbuffer(struct drm_device *dev)
3805 drm_i915_private_t *dev_priv = dev->dev_private;
3806 struct drm_gem_object *obj;
3807 struct drm_i915_gem_object *obj_priv;
3808 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
3812 ret = i915_gem_init_hws(dev);
3816 obj = drm_gem_object_alloc(dev, 128 * 1024);
3818 DRM_ERROR("Failed to allocate ringbuffer\n");
3819 i915_gem_cleanup_hws(dev);
3822 obj_priv = obj->driver_private;
3824 ret = i915_gem_object_pin(obj, 4096);
3826 drm_gem_object_unreference(obj);
3827 i915_gem_cleanup_hws(dev);
3831 /* Set up the kernel mapping for the ring. */
3832 ring->Size = obj->size;
3833 ring->tail_mask = obj->size - 1;
3835 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
3836 ring->map.size = obj->size;
3838 ring->map.flags = 0;
3841 drm_core_ioremap_wc(&ring->map, dev);
3842 if (ring->map.handle == NULL) {
3843 DRM_ERROR("Failed to map ringbuffer.\n");
3844 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3845 i915_gem_object_unpin(obj);
3846 drm_gem_object_unreference(obj);
3847 i915_gem_cleanup_hws(dev);
3850 ring->ring_obj = obj;
3851 ring->virtual_start = ring->map.handle;
3853 /* Stop the ring if it's running. */
3854 I915_WRITE(PRB0_CTL, 0);
3855 I915_WRITE(PRB0_TAIL, 0);
3856 I915_WRITE(PRB0_HEAD, 0);
3858 /* Initialize the ring. */
3859 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
3860 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3862 /* G45 ring initialization fails to reset head to zero */
3864 DRM_ERROR("Ring head not reset to zero "
3865 "ctl %08x head %08x tail %08x start %08x\n",
3866 I915_READ(PRB0_CTL),
3867 I915_READ(PRB0_HEAD),
3868 I915_READ(PRB0_TAIL),
3869 I915_READ(PRB0_START));
3870 I915_WRITE(PRB0_HEAD, 0);
3872 DRM_ERROR("Ring head forced to zero "
3873 "ctl %08x head %08x tail %08x start %08x\n",
3874 I915_READ(PRB0_CTL),
3875 I915_READ(PRB0_HEAD),
3876 I915_READ(PRB0_TAIL),
3877 I915_READ(PRB0_START));
3880 I915_WRITE(PRB0_CTL,
3881 ((obj->size - 4096) & RING_NR_PAGES) |
3885 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3887 /* If the head is still not zero, the ring is dead */
3889 DRM_ERROR("Ring initialization failed "
3890 "ctl %08x head %08x tail %08x start %08x\n",
3891 I915_READ(PRB0_CTL),
3892 I915_READ(PRB0_HEAD),
3893 I915_READ(PRB0_TAIL),
3894 I915_READ(PRB0_START));
3898 /* Update our cache of the ring state */
3899 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3900 i915_kernel_lost_context(dev);
3902 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3903 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
3904 ring->space = ring->head - (ring->tail + 8);
3905 if (ring->space < 0)
3906 ring->space += ring->Size;
3913 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3915 drm_i915_private_t *dev_priv = dev->dev_private;
3917 if (dev_priv->ring.ring_obj == NULL)
3920 drm_core_ioremapfree(&dev_priv->ring.map, dev);
3922 i915_gem_object_unpin(dev_priv->ring.ring_obj);
3923 drm_gem_object_unreference(dev_priv->ring.ring_obj);
3924 dev_priv->ring.ring_obj = NULL;
3925 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3927 i915_gem_cleanup_hws(dev);
3931 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3932 struct drm_file *file_priv)
3934 drm_i915_private_t *dev_priv = dev->dev_private;
3937 if (drm_core_check_feature(dev, DRIVER_MODESET))
3940 if (dev_priv->mm.wedged) {
3941 DRM_ERROR("Reenabling wedged hardware, good luck\n");
3942 dev_priv->mm.wedged = 0;
3945 mutex_lock(&dev->struct_mutex);
3946 dev_priv->mm.suspended = 0;
3948 ret = i915_gem_init_ringbuffer(dev);
3952 BUG_ON(!list_empty(&dev_priv->mm.active_list));
3953 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3954 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3955 BUG_ON(!list_empty(&dev_priv->mm.request_list));
3956 mutex_unlock(&dev->struct_mutex);
3958 drm_irq_install(dev);
3964 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3965 struct drm_file *file_priv)
3969 if (drm_core_check_feature(dev, DRIVER_MODESET))
3972 ret = i915_gem_idle(dev);
3973 drm_irq_uninstall(dev);
3979 i915_gem_lastclose(struct drm_device *dev)
3983 if (drm_core_check_feature(dev, DRIVER_MODESET))
3986 ret = i915_gem_idle(dev);
3988 DRM_ERROR("failed to idle hardware: %d\n", ret);
3992 i915_gem_load(struct drm_device *dev)
3994 drm_i915_private_t *dev_priv = dev->dev_private;
3996 INIT_LIST_HEAD(&dev_priv->mm.active_list);
3997 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3998 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
3999 INIT_LIST_HEAD(&dev_priv->mm.request_list);
4000 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4001 i915_gem_retire_work_handler);
4002 dev_priv->mm.next_gem_seqno = 1;
4004 /* Old X drivers will take 0-2 for front, back, depth buffers */
4005 dev_priv->fence_reg_start = 3;
4007 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4008 dev_priv->num_fence_regs = 16;
4010 dev_priv->num_fence_regs = 8;
4012 i915_gem_detect_bit_6_swizzle(dev);
4016 * Create a physically contiguous memory object for this object
4017 * e.g. for cursor + overlay regs
4019 int i915_gem_init_phys_object(struct drm_device *dev,
4022 drm_i915_private_t *dev_priv = dev->dev_private;
4023 struct drm_i915_gem_phys_object *phys_obj;
4026 if (dev_priv->mm.phys_objs[id - 1] || !size)
4029 phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
4035 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4036 if (!phys_obj->handle) {
4041 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4044 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4048 drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
4052 void i915_gem_free_phys_object(struct drm_device *dev, int id)
4054 drm_i915_private_t *dev_priv = dev->dev_private;
4055 struct drm_i915_gem_phys_object *phys_obj;
4057 if (!dev_priv->mm.phys_objs[id - 1])
4060 phys_obj = dev_priv->mm.phys_objs[id - 1];
4061 if (phys_obj->cur_obj) {
4062 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4066 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4068 drm_pci_free(dev, phys_obj->handle);
4070 dev_priv->mm.phys_objs[id - 1] = NULL;
4073 void i915_gem_free_all_phys_object(struct drm_device *dev)
4077 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
4078 i915_gem_free_phys_object(dev, i);
4081 void i915_gem_detach_phys_object(struct drm_device *dev,
4082 struct drm_gem_object *obj)
4084 struct drm_i915_gem_object *obj_priv;
4089 obj_priv = obj->driver_private;
4090 if (!obj_priv->phys_obj)
4093 ret = i915_gem_object_get_pages(obj);
4097 page_count = obj->size / PAGE_SIZE;
4099 for (i = 0; i < page_count; i++) {
4100 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
4101 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4103 memcpy(dst, src, PAGE_SIZE);
4104 kunmap_atomic(dst, KM_USER0);
4106 drm_clflush_pages(obj_priv->pages, page_count);
4107 drm_agp_chipset_flush(dev);
4109 obj_priv->phys_obj->cur_obj = NULL;
4110 obj_priv->phys_obj = NULL;
4114 i915_gem_attach_phys_object(struct drm_device *dev,
4115 struct drm_gem_object *obj, int id)
4117 drm_i915_private_t *dev_priv = dev->dev_private;
4118 struct drm_i915_gem_object *obj_priv;
4123 if (id > I915_MAX_PHYS_OBJECT)
4126 obj_priv = obj->driver_private;
4128 if (obj_priv->phys_obj) {
4129 if (obj_priv->phys_obj->id == id)
4131 i915_gem_detach_phys_object(dev, obj);
4135 /* create a new object */
4136 if (!dev_priv->mm.phys_objs[id - 1]) {
4137 ret = i915_gem_init_phys_object(dev, id,
4140 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
4145 /* bind to the object */
4146 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4147 obj_priv->phys_obj->cur_obj = obj;
4149 ret = i915_gem_object_get_pages(obj);
4151 DRM_ERROR("failed to get page list\n");
4155 page_count = obj->size / PAGE_SIZE;
4157 for (i = 0; i < page_count; i++) {
4158 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
4159 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4161 memcpy(dst, src, PAGE_SIZE);
4162 kunmap_atomic(src, KM_USER0);
4171 i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4172 struct drm_i915_gem_pwrite *args,
4173 struct drm_file *file_priv)
4175 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4178 char __user *user_data;
4180 user_data = (char __user *) (uintptr_t) args->data_ptr;
4181 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4183 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
4184 ret = copy_from_user(obj_addr, user_data, args->size);
4188 drm_agp_chipset_flush(dev);