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;
1076 return VM_FAULT_SIGBUS;
1078 return VM_FAULT_NOPAGE;
1083 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1084 * @obj: obj in question
1086 * GEM memory mapping works by handing back to userspace a fake mmap offset
1087 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1088 * up the object based on the offset and sets up the various memory mapping
1091 * This routine allocates and attaches a fake offset for @obj.
1094 i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1096 struct drm_device *dev = obj->dev;
1097 struct drm_gem_mm *mm = dev->mm_private;
1098 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1099 struct drm_map_list *list;
1100 struct drm_local_map *map;
1103 /* Set the object up for mmap'ing */
1104 list = &obj->map_list;
1105 list->map = drm_calloc(1, sizeof(struct drm_map_list),
1111 map->type = _DRM_GEM;
1112 map->size = obj->size;
1115 /* Get a DRM GEM mmap offset allocated... */
1116 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1117 obj->size / PAGE_SIZE, 0, 0);
1118 if (!list->file_offset_node) {
1119 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1124 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1125 obj->size / PAGE_SIZE, 0);
1126 if (!list->file_offset_node) {
1131 list->hash.key = list->file_offset_node->start;
1132 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
1133 DRM_ERROR("failed to add to map hash\n");
1137 /* By now we should be all set, any drm_mmap request on the offset
1138 * below will get to our mmap & fault handler */
1139 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
1144 drm_mm_put_block(list->file_offset_node);
1146 drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER);
1152 i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1154 struct drm_device *dev = obj->dev;
1155 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1156 struct drm_gem_mm *mm = dev->mm_private;
1157 struct drm_map_list *list;
1159 list = &obj->map_list;
1160 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1162 if (list->file_offset_node) {
1163 drm_mm_put_block(list->file_offset_node);
1164 list->file_offset_node = NULL;
1168 drm_free(list->map, sizeof(struct drm_map), DRM_MEM_DRIVER);
1172 obj_priv->mmap_offset = 0;
1176 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1177 * @obj: object to check
1179 * Return the required GTT alignment for an object, taking into account
1180 * potential fence register mapping if needed.
1183 i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
1185 struct drm_device *dev = obj->dev;
1186 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1190 * Minimum alignment is 4k (GTT page size), but might be greater
1191 * if a fence register is needed for the object.
1193 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
1197 * Previous chips need to be aligned to the size of the smallest
1198 * fence register that can contain the object.
1205 for (i = start; i < obj->size; i <<= 1)
1212 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1214 * @data: GTT mapping ioctl data
1215 * @file_priv: GEM object info
1217 * Simply returns the fake offset to userspace so it can mmap it.
1218 * The mmap call will end up in drm_gem_mmap(), which will set things
1219 * up so we can get faults in the handler above.
1221 * The fault handler will take care of binding the object into the GTT
1222 * (since it may have been evicted to make room for something), allocating
1223 * a fence register, and mapping the appropriate aperture address into
1227 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1228 struct drm_file *file_priv)
1230 struct drm_i915_gem_mmap_gtt *args = data;
1231 struct drm_i915_private *dev_priv = dev->dev_private;
1232 struct drm_gem_object *obj;
1233 struct drm_i915_gem_object *obj_priv;
1236 if (!(dev->driver->driver_features & DRIVER_GEM))
1239 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1243 mutex_lock(&dev->struct_mutex);
1245 obj_priv = obj->driver_private;
1247 if (!obj_priv->mmap_offset) {
1248 ret = i915_gem_create_mmap_offset(obj);
1250 drm_gem_object_unreference(obj);
1251 mutex_unlock(&dev->struct_mutex);
1256 args->offset = obj_priv->mmap_offset;
1258 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
1260 /* Make sure the alignment is correct for fence regs etc */
1261 if (obj_priv->agp_mem &&
1262 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
1263 drm_gem_object_unreference(obj);
1264 mutex_unlock(&dev->struct_mutex);
1269 * Pull it into the GTT so that we have a page list (makes the
1270 * initial fault faster and any subsequent flushing possible).
1272 if (!obj_priv->agp_mem) {
1273 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
1275 drm_gem_object_unreference(obj);
1276 mutex_unlock(&dev->struct_mutex);
1279 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
1282 drm_gem_object_unreference(obj);
1283 mutex_unlock(&dev->struct_mutex);
1289 i915_gem_object_put_pages(struct drm_gem_object *obj)
1291 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1292 int page_count = obj->size / PAGE_SIZE;
1295 BUG_ON(obj_priv->pages_refcount == 0);
1297 if (--obj_priv->pages_refcount != 0)
1300 for (i = 0; i < page_count; i++)
1301 if (obj_priv->pages[i] != NULL) {
1302 if (obj_priv->dirty)
1303 set_page_dirty(obj_priv->pages[i]);
1304 mark_page_accessed(obj_priv->pages[i]);
1305 page_cache_release(obj_priv->pages[i]);
1307 obj_priv->dirty = 0;
1309 drm_free(obj_priv->pages,
1310 page_count * sizeof(struct page *),
1312 obj_priv->pages = NULL;
1316 i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
1318 struct drm_device *dev = obj->dev;
1319 drm_i915_private_t *dev_priv = dev->dev_private;
1320 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1322 /* Add a reference if we're newly entering the active list. */
1323 if (!obj_priv->active) {
1324 drm_gem_object_reference(obj);
1325 obj_priv->active = 1;
1327 /* Move from whatever list we were on to the tail of execution. */
1328 spin_lock(&dev_priv->mm.active_list_lock);
1329 list_move_tail(&obj_priv->list,
1330 &dev_priv->mm.active_list);
1331 spin_unlock(&dev_priv->mm.active_list_lock);
1332 obj_priv->last_rendering_seqno = seqno;
1336 i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1338 struct drm_device *dev = obj->dev;
1339 drm_i915_private_t *dev_priv = dev->dev_private;
1340 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1342 BUG_ON(!obj_priv->active);
1343 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
1344 obj_priv->last_rendering_seqno = 0;
1348 i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1350 struct drm_device *dev = obj->dev;
1351 drm_i915_private_t *dev_priv = dev->dev_private;
1352 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1354 i915_verify_inactive(dev, __FILE__, __LINE__);
1355 if (obj_priv->pin_count != 0)
1356 list_del_init(&obj_priv->list);
1358 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
1360 obj_priv->last_rendering_seqno = 0;
1361 if (obj_priv->active) {
1362 obj_priv->active = 0;
1363 drm_gem_object_unreference(obj);
1365 i915_verify_inactive(dev, __FILE__, __LINE__);
1369 * Creates a new sequence number, emitting a write of it to the status page
1370 * plus an interrupt, which will trigger i915_user_interrupt_handler.
1372 * Must be called with struct_lock held.
1374 * Returned sequence numbers are nonzero on success.
1377 i915_add_request(struct drm_device *dev, uint32_t flush_domains)
1379 drm_i915_private_t *dev_priv = dev->dev_private;
1380 struct drm_i915_gem_request *request;
1385 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
1386 if (request == NULL)
1389 /* Grab the seqno we're going to make this request be, and bump the
1390 * next (skipping 0 so it can be the reserved no-seqno value).
1392 seqno = dev_priv->mm.next_gem_seqno;
1393 dev_priv->mm.next_gem_seqno++;
1394 if (dev_priv->mm.next_gem_seqno == 0)
1395 dev_priv->mm.next_gem_seqno++;
1398 OUT_RING(MI_STORE_DWORD_INDEX);
1399 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
1402 OUT_RING(MI_USER_INTERRUPT);
1405 DRM_DEBUG("%d\n", seqno);
1407 request->seqno = seqno;
1408 request->emitted_jiffies = jiffies;
1409 was_empty = list_empty(&dev_priv->mm.request_list);
1410 list_add_tail(&request->list, &dev_priv->mm.request_list);
1412 /* Associate any objects on the flushing list matching the write
1413 * domain we're flushing with our flush.
1415 if (flush_domains != 0) {
1416 struct drm_i915_gem_object *obj_priv, *next;
1418 list_for_each_entry_safe(obj_priv, next,
1419 &dev_priv->mm.flushing_list, list) {
1420 struct drm_gem_object *obj = obj_priv->obj;
1422 if ((obj->write_domain & flush_domains) ==
1423 obj->write_domain) {
1424 obj->write_domain = 0;
1425 i915_gem_object_move_to_active(obj, seqno);
1431 if (was_empty && !dev_priv->mm.suspended)
1432 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1437 * Command execution barrier
1439 * Ensures that all commands in the ring are finished
1440 * before signalling the CPU
1443 i915_retire_commands(struct drm_device *dev)
1445 drm_i915_private_t *dev_priv = dev->dev_private;
1446 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1447 uint32_t flush_domains = 0;
1450 /* The sampler always gets flushed on i965 (sigh) */
1452 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1455 OUT_RING(0); /* noop */
1457 return flush_domains;
1461 * Moves buffers associated only with the given active seqno from the active
1462 * to inactive list, potentially freeing them.
1465 i915_gem_retire_request(struct drm_device *dev,
1466 struct drm_i915_gem_request *request)
1468 drm_i915_private_t *dev_priv = dev->dev_private;
1470 /* Move any buffers on the active list that are no longer referenced
1471 * by the ringbuffer to the flushing/inactive lists as appropriate.
1473 spin_lock(&dev_priv->mm.active_list_lock);
1474 while (!list_empty(&dev_priv->mm.active_list)) {
1475 struct drm_gem_object *obj;
1476 struct drm_i915_gem_object *obj_priv;
1478 obj_priv = list_first_entry(&dev_priv->mm.active_list,
1479 struct drm_i915_gem_object,
1481 obj = obj_priv->obj;
1483 /* If the seqno being retired doesn't match the oldest in the
1484 * list, then the oldest in the list must still be newer than
1487 if (obj_priv->last_rendering_seqno != request->seqno)
1491 DRM_INFO("%s: retire %d moves to inactive list %p\n",
1492 __func__, request->seqno, obj);
1495 if (obj->write_domain != 0)
1496 i915_gem_object_move_to_flushing(obj);
1498 i915_gem_object_move_to_inactive(obj);
1501 spin_unlock(&dev_priv->mm.active_list_lock);
1505 * Returns true if seq1 is later than seq2.
1508 i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1510 return (int32_t)(seq1 - seq2) >= 0;
1514 i915_get_gem_seqno(struct drm_device *dev)
1516 drm_i915_private_t *dev_priv = dev->dev_private;
1518 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1522 * This function clears the request list as sequence numbers are passed.
1525 i915_gem_retire_requests(struct drm_device *dev)
1527 drm_i915_private_t *dev_priv = dev->dev_private;
1530 if (!dev_priv->hw_status_page)
1533 seqno = i915_get_gem_seqno(dev);
1535 while (!list_empty(&dev_priv->mm.request_list)) {
1536 struct drm_i915_gem_request *request;
1537 uint32_t retiring_seqno;
1539 request = list_first_entry(&dev_priv->mm.request_list,
1540 struct drm_i915_gem_request,
1542 retiring_seqno = request->seqno;
1544 if (i915_seqno_passed(seqno, retiring_seqno) ||
1545 dev_priv->mm.wedged) {
1546 i915_gem_retire_request(dev, request);
1548 list_del(&request->list);
1549 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
1556 i915_gem_retire_work_handler(struct work_struct *work)
1558 drm_i915_private_t *dev_priv;
1559 struct drm_device *dev;
1561 dev_priv = container_of(work, drm_i915_private_t,
1562 mm.retire_work.work);
1563 dev = dev_priv->dev;
1565 mutex_lock(&dev->struct_mutex);
1566 i915_gem_retire_requests(dev);
1567 if (!dev_priv->mm.suspended &&
1568 !list_empty(&dev_priv->mm.request_list))
1569 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1570 mutex_unlock(&dev->struct_mutex);
1574 * Waits for a sequence number to be signaled, and cleans up the
1575 * request and object lists appropriately for that event.
1578 i915_wait_request(struct drm_device *dev, uint32_t seqno)
1580 drm_i915_private_t *dev_priv = dev->dev_private;
1585 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
1586 dev_priv->mm.waiting_gem_seqno = seqno;
1587 i915_user_irq_get(dev);
1588 ret = wait_event_interruptible(dev_priv->irq_queue,
1589 i915_seqno_passed(i915_get_gem_seqno(dev),
1591 dev_priv->mm.wedged);
1592 i915_user_irq_put(dev);
1593 dev_priv->mm.waiting_gem_seqno = 0;
1595 if (dev_priv->mm.wedged)
1598 if (ret && ret != -ERESTARTSYS)
1599 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1600 __func__, ret, seqno, i915_get_gem_seqno(dev));
1602 /* Directly dispatch request retiring. While we have the work queue
1603 * to handle this, the waiter on a request often wants an associated
1604 * buffer to have made it to the inactive list, and we would need
1605 * a separate wait queue to handle that.
1608 i915_gem_retire_requests(dev);
1614 i915_gem_flush(struct drm_device *dev,
1615 uint32_t invalidate_domains,
1616 uint32_t flush_domains)
1618 drm_i915_private_t *dev_priv = dev->dev_private;
1623 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1624 invalidate_domains, flush_domains);
1627 if (flush_domains & I915_GEM_DOMAIN_CPU)
1628 drm_agp_chipset_flush(dev);
1630 if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
1631 I915_GEM_DOMAIN_GTT)) {
1633 * read/write caches:
1635 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1636 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1637 * also flushed at 2d versus 3d pipeline switches.
1641 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1642 * MI_READ_FLUSH is set, and is always flushed on 965.
1644 * I915_GEM_DOMAIN_COMMAND may not exist?
1646 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1647 * invalidated when MI_EXE_FLUSH is set.
1649 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1650 * invalidated with every MI_FLUSH.
1654 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1655 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1656 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1657 * are flushed at any MI_FLUSH.
1660 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1661 if ((invalidate_domains|flush_domains) &
1662 I915_GEM_DOMAIN_RENDER)
1663 cmd &= ~MI_NO_WRITE_FLUSH;
1664 if (!IS_I965G(dev)) {
1666 * On the 965, the sampler cache always gets flushed
1667 * and this bit is reserved.
1669 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1670 cmd |= MI_READ_FLUSH;
1672 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1673 cmd |= MI_EXE_FLUSH;
1676 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1680 OUT_RING(0); /* noop */
1686 * Ensures that all rendering to the object has completed and the object is
1687 * safe to unbind from the GTT or access from the CPU.
1690 i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1692 struct drm_device *dev = obj->dev;
1693 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1696 /* This function only exists to support waiting for existing rendering,
1697 * not for emitting required flushes.
1699 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
1701 /* If there is rendering queued on the buffer being evicted, wait for
1704 if (obj_priv->active) {
1706 DRM_INFO("%s: object %p wait for seqno %08x\n",
1707 __func__, obj, obj_priv->last_rendering_seqno);
1709 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1718 * Unbinds an object from the GTT aperture.
1721 i915_gem_object_unbind(struct drm_gem_object *obj)
1723 struct drm_device *dev = obj->dev;
1724 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1729 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1730 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1732 if (obj_priv->gtt_space == NULL)
1735 if (obj_priv->pin_count != 0) {
1736 DRM_ERROR("Attempting to unbind pinned buffer\n");
1740 /* Move the object to the CPU domain to ensure that
1741 * any possible CPU writes while it's not in the GTT
1742 * are flushed when we go to remap it. This will
1743 * also ensure that all pending GPU writes are finished
1746 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1748 if (ret != -ERESTARTSYS)
1749 DRM_ERROR("set_domain failed: %d\n", ret);
1753 if (obj_priv->agp_mem != NULL) {
1754 drm_unbind_agp(obj_priv->agp_mem);
1755 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1756 obj_priv->agp_mem = NULL;
1759 BUG_ON(obj_priv->active);
1761 /* blow away mappings if mapped through GTT */
1762 offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT;
1763 if (dev->dev_mapping)
1764 unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1);
1766 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1767 i915_gem_clear_fence_reg(obj);
1769 i915_gem_object_put_pages(obj);
1771 if (obj_priv->gtt_space) {
1772 atomic_dec(&dev->gtt_count);
1773 atomic_sub(obj->size, &dev->gtt_memory);
1775 drm_mm_put_block(obj_priv->gtt_space);
1776 obj_priv->gtt_space = NULL;
1779 /* Remove ourselves from the LRU list if present. */
1780 if (!list_empty(&obj_priv->list))
1781 list_del_init(&obj_priv->list);
1787 i915_gem_evict_something(struct drm_device *dev)
1789 drm_i915_private_t *dev_priv = dev->dev_private;
1790 struct drm_gem_object *obj;
1791 struct drm_i915_gem_object *obj_priv;
1795 /* If there's an inactive buffer available now, grab it
1798 if (!list_empty(&dev_priv->mm.inactive_list)) {
1799 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1800 struct drm_i915_gem_object,
1802 obj = obj_priv->obj;
1803 BUG_ON(obj_priv->pin_count != 0);
1805 DRM_INFO("%s: evicting %p\n", __func__, obj);
1807 BUG_ON(obj_priv->active);
1809 /* Wait on the rendering and unbind the buffer. */
1810 ret = i915_gem_object_unbind(obj);
1814 /* If we didn't get anything, but the ring is still processing
1815 * things, wait for one of those things to finish and hopefully
1816 * leave us a buffer to evict.
1818 if (!list_empty(&dev_priv->mm.request_list)) {
1819 struct drm_i915_gem_request *request;
1821 request = list_first_entry(&dev_priv->mm.request_list,
1822 struct drm_i915_gem_request,
1825 ret = i915_wait_request(dev, request->seqno);
1829 /* if waiting caused an object to become inactive,
1830 * then loop around and wait for it. Otherwise, we
1831 * assume that waiting freed and unbound something,
1832 * so there should now be some space in the GTT
1834 if (!list_empty(&dev_priv->mm.inactive_list))
1839 /* If we didn't have anything on the request list but there
1840 * are buffers awaiting a flush, emit one and try again.
1841 * When we wait on it, those buffers waiting for that flush
1842 * will get moved to inactive.
1844 if (!list_empty(&dev_priv->mm.flushing_list)) {
1845 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1846 struct drm_i915_gem_object,
1848 obj = obj_priv->obj;
1853 i915_add_request(dev, obj->write_domain);
1859 DRM_ERROR("inactive empty %d request empty %d "
1860 "flushing empty %d\n",
1861 list_empty(&dev_priv->mm.inactive_list),
1862 list_empty(&dev_priv->mm.request_list),
1863 list_empty(&dev_priv->mm.flushing_list));
1864 /* If we didn't do any of the above, there's nothing to be done
1865 * and we just can't fit it in.
1873 i915_gem_evict_everything(struct drm_device *dev)
1878 ret = i915_gem_evict_something(dev);
1888 i915_gem_object_get_pages(struct drm_gem_object *obj)
1890 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1892 struct address_space *mapping;
1893 struct inode *inode;
1897 if (obj_priv->pages_refcount++ != 0)
1900 /* Get the list of pages out of our struct file. They'll be pinned
1901 * at this point until we release them.
1903 page_count = obj->size / PAGE_SIZE;
1904 BUG_ON(obj_priv->pages != NULL);
1905 obj_priv->pages = drm_calloc(page_count, sizeof(struct page *),
1907 if (obj_priv->pages == NULL) {
1908 DRM_ERROR("Faled to allocate page list\n");
1909 obj_priv->pages_refcount--;
1913 inode = obj->filp->f_path.dentry->d_inode;
1914 mapping = inode->i_mapping;
1915 for (i = 0; i < page_count; i++) {
1916 page = read_mapping_page(mapping, i, NULL);
1918 ret = PTR_ERR(page);
1919 DRM_ERROR("read_mapping_page failed: %d\n", ret);
1920 i915_gem_object_put_pages(obj);
1923 obj_priv->pages[i] = page;
1928 static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
1930 struct drm_gem_object *obj = reg->obj;
1931 struct drm_device *dev = obj->dev;
1932 drm_i915_private_t *dev_priv = dev->dev_private;
1933 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1934 int regnum = obj_priv->fence_reg;
1937 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
1939 val |= obj_priv->gtt_offset & 0xfffff000;
1940 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
1941 if (obj_priv->tiling_mode == I915_TILING_Y)
1942 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
1943 val |= I965_FENCE_REG_VALID;
1945 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
1948 static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
1950 struct drm_gem_object *obj = reg->obj;
1951 struct drm_device *dev = obj->dev;
1952 drm_i915_private_t *dev_priv = dev->dev_private;
1953 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1954 int regnum = obj_priv->fence_reg;
1956 uint32_t fence_reg, val;
1959 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1960 (obj_priv->gtt_offset & (obj->size - 1))) {
1961 WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
1962 __func__, obj_priv->gtt_offset, obj->size);
1966 if (obj_priv->tiling_mode == I915_TILING_Y &&
1967 HAS_128_BYTE_Y_TILING(dev))
1972 /* Note: pitch better be a power of two tile widths */
1973 pitch_val = obj_priv->stride / tile_width;
1974 pitch_val = ffs(pitch_val) - 1;
1976 val = obj_priv->gtt_offset;
1977 if (obj_priv->tiling_mode == I915_TILING_Y)
1978 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
1979 val |= I915_FENCE_SIZE_BITS(obj->size);
1980 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
1981 val |= I830_FENCE_REG_VALID;
1984 fence_reg = FENCE_REG_830_0 + (regnum * 4);
1986 fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
1987 I915_WRITE(fence_reg, val);
1990 static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
1992 struct drm_gem_object *obj = reg->obj;
1993 struct drm_device *dev = obj->dev;
1994 drm_i915_private_t *dev_priv = dev->dev_private;
1995 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1996 int regnum = obj_priv->fence_reg;
1999 uint32_t fence_size_bits;
2001 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
2002 (obj_priv->gtt_offset & (obj->size - 1))) {
2003 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
2004 __func__, obj_priv->gtt_offset);
2008 pitch_val = (obj_priv->stride / 128) - 1;
2009 WARN_ON(pitch_val & ~0x0000000f);
2010 val = obj_priv->gtt_offset;
2011 if (obj_priv->tiling_mode == I915_TILING_Y)
2012 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2013 fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
2014 WARN_ON(fence_size_bits & ~0x00000f00);
2015 val |= fence_size_bits;
2016 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2017 val |= I830_FENCE_REG_VALID;
2019 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
2024 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2025 * @obj: object to map through a fence reg
2026 * @write: object is about to be written
2028 * When mapping objects through the GTT, userspace wants to be able to write
2029 * to them without having to worry about swizzling if the object is tiled.
2031 * This function walks the fence regs looking for a free one for @obj,
2032 * stealing one if it can't find any.
2034 * It then sets up the reg based on the object's properties: address, pitch
2035 * and tiling format.
2038 i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write)
2040 struct drm_device *dev = obj->dev;
2041 struct drm_i915_private *dev_priv = dev->dev_private;
2042 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2043 struct drm_i915_fence_reg *reg = NULL;
2044 struct drm_i915_gem_object *old_obj_priv = NULL;
2047 switch (obj_priv->tiling_mode) {
2048 case I915_TILING_NONE:
2049 WARN(1, "allocating a fence for non-tiled object?\n");
2052 if (!obj_priv->stride)
2054 WARN((obj_priv->stride & (512 - 1)),
2055 "object 0x%08x is X tiled but has non-512B pitch\n",
2056 obj_priv->gtt_offset);
2059 if (!obj_priv->stride)
2061 WARN((obj_priv->stride & (128 - 1)),
2062 "object 0x%08x is Y tiled but has non-128B pitch\n",
2063 obj_priv->gtt_offset);
2067 /* First try to find a free reg */
2070 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2071 reg = &dev_priv->fence_regs[i];
2075 old_obj_priv = reg->obj->driver_private;
2076 if (!old_obj_priv->pin_count)
2080 /* None available, try to steal one or wait for a user to finish */
2081 if (i == dev_priv->num_fence_regs) {
2082 uint32_t seqno = dev_priv->mm.next_gem_seqno;
2088 for (i = dev_priv->fence_reg_start;
2089 i < dev_priv->num_fence_regs; i++) {
2090 uint32_t this_seqno;
2092 reg = &dev_priv->fence_regs[i];
2093 old_obj_priv = reg->obj->driver_private;
2095 if (old_obj_priv->pin_count)
2098 /* i915 uses fences for GPU access to tiled buffers */
2099 if (IS_I965G(dev) || !old_obj_priv->active)
2102 /* find the seqno of the first available fence */
2103 this_seqno = old_obj_priv->last_rendering_seqno;
2104 if (this_seqno != 0 &&
2105 reg->obj->write_domain == 0 &&
2106 i915_seqno_passed(seqno, this_seqno))
2111 * Now things get ugly... we have to wait for one of the
2112 * objects to finish before trying again.
2114 if (i == dev_priv->num_fence_regs) {
2115 if (seqno == dev_priv->mm.next_gem_seqno) {
2117 I915_GEM_GPU_DOMAINS,
2118 I915_GEM_GPU_DOMAINS);
2119 seqno = i915_add_request(dev,
2120 I915_GEM_GPU_DOMAINS);
2125 ret = i915_wait_request(dev, seqno);
2131 BUG_ON(old_obj_priv->active ||
2132 (reg->obj->write_domain & I915_GEM_GPU_DOMAINS));
2135 * Zap this virtual mapping so we can set up a fence again
2136 * for this object next time we need it.
2138 offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
2139 if (dev->dev_mapping)
2140 unmap_mapping_range(dev->dev_mapping, offset,
2142 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
2145 obj_priv->fence_reg = i;
2149 i965_write_fence_reg(reg);
2150 else if (IS_I9XX(dev))
2151 i915_write_fence_reg(reg);
2153 i830_write_fence_reg(reg);
2159 * i915_gem_clear_fence_reg - clear out fence register info
2160 * @obj: object to clear
2162 * Zeroes out the fence register itself and clears out the associated
2163 * data structures in dev_priv and obj_priv.
2166 i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2168 struct drm_device *dev = obj->dev;
2169 drm_i915_private_t *dev_priv = dev->dev_private;
2170 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2173 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
2177 if (obj_priv->fence_reg < 8)
2178 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2180 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
2183 I915_WRITE(fence_reg, 0);
2186 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
2187 obj_priv->fence_reg = I915_FENCE_REG_NONE;
2191 * Finds free space in the GTT aperture and binds the object there.
2194 i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
2196 struct drm_device *dev = obj->dev;
2197 drm_i915_private_t *dev_priv = dev->dev_private;
2198 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2199 struct drm_mm_node *free_space;
2200 int page_count, ret;
2202 if (dev_priv->mm.suspended)
2205 alignment = i915_gem_get_gtt_alignment(obj);
2206 if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
2207 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2212 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2213 obj->size, alignment, 0);
2214 if (free_space != NULL) {
2215 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
2217 if (obj_priv->gtt_space != NULL) {
2218 obj_priv->gtt_space->private = obj;
2219 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2222 if (obj_priv->gtt_space == NULL) {
2225 /* If the gtt is empty and we're still having trouble
2226 * fitting our object in, we're out of memory.
2229 DRM_INFO("%s: GTT full, evicting something\n", __func__);
2231 spin_lock(&dev_priv->mm.active_list_lock);
2232 lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
2233 list_empty(&dev_priv->mm.flushing_list) &&
2234 list_empty(&dev_priv->mm.active_list));
2235 spin_unlock(&dev_priv->mm.active_list_lock);
2237 DRM_ERROR("GTT full, but LRU list empty\n");
2241 ret = i915_gem_evict_something(dev);
2243 if (ret != -ERESTARTSYS)
2244 DRM_ERROR("Failed to evict a buffer %d\n", ret);
2251 DRM_INFO("Binding object of size %d at 0x%08x\n",
2252 obj->size, obj_priv->gtt_offset);
2254 ret = i915_gem_object_get_pages(obj);
2256 drm_mm_put_block(obj_priv->gtt_space);
2257 obj_priv->gtt_space = NULL;
2261 page_count = obj->size / PAGE_SIZE;
2262 /* Create an AGP memory structure pointing at our pages, and bind it
2265 obj_priv->agp_mem = drm_agp_bind_pages(dev,
2268 obj_priv->gtt_offset,
2269 obj_priv->agp_type);
2270 if (obj_priv->agp_mem == NULL) {
2271 i915_gem_object_put_pages(obj);
2272 drm_mm_put_block(obj_priv->gtt_space);
2273 obj_priv->gtt_space = NULL;
2276 atomic_inc(&dev->gtt_count);
2277 atomic_add(obj->size, &dev->gtt_memory);
2279 /* Assert that the object is not currently in any GPU domain. As it
2280 * wasn't in the GTT, there shouldn't be any way it could have been in
2283 BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2284 BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2290 i915_gem_clflush_object(struct drm_gem_object *obj)
2292 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2294 /* If we don't have a page list set up, then we're not pinned
2295 * to GPU, and we can ignore the cache flush because it'll happen
2296 * again at bind time.
2298 if (obj_priv->pages == NULL)
2301 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
2304 /** Flushes any GPU write domain for the object if it's dirty. */
2306 i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
2308 struct drm_device *dev = obj->dev;
2311 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2314 /* Queue the GPU write cache flushing we need. */
2315 i915_gem_flush(dev, 0, obj->write_domain);
2316 seqno = i915_add_request(dev, obj->write_domain);
2317 obj->write_domain = 0;
2318 i915_gem_object_move_to_active(obj, seqno);
2321 /** Flushes the GTT write domain for the object if it's dirty. */
2323 i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2325 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2328 /* No actual flushing is required for the GTT write domain. Writes
2329 * to it immediately go to main memory as far as we know, so there's
2330 * no chipset flush. It also doesn't land in render cache.
2332 obj->write_domain = 0;
2335 /** Flushes the CPU write domain for the object if it's dirty. */
2337 i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2339 struct drm_device *dev = obj->dev;
2341 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2344 i915_gem_clflush_object(obj);
2345 drm_agp_chipset_flush(dev);
2346 obj->write_domain = 0;
2350 * Moves a single object to the GTT read, and possibly write domain.
2352 * This function returns when the move is complete, including waiting on
2356 i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2358 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2361 /* Not valid to be called on unbound objects. */
2362 if (obj_priv->gtt_space == NULL)
2365 i915_gem_object_flush_gpu_write_domain(obj);
2366 /* Wait on any GPU rendering and flushing to occur. */
2367 ret = i915_gem_object_wait_rendering(obj);
2371 /* If we're writing through the GTT domain, then CPU and GPU caches
2372 * will need to be invalidated at next use.
2375 obj->read_domains &= I915_GEM_DOMAIN_GTT;
2377 i915_gem_object_flush_cpu_write_domain(obj);
2379 /* It should now be out of any other write domains, and we can update
2380 * the domain values for our changes.
2382 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2383 obj->read_domains |= I915_GEM_DOMAIN_GTT;
2385 obj->write_domain = I915_GEM_DOMAIN_GTT;
2386 obj_priv->dirty = 1;
2393 * Moves a single object to the CPU read, and possibly write domain.
2395 * This function returns when the move is complete, including waiting on
2399 i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
2403 i915_gem_object_flush_gpu_write_domain(obj);
2404 /* Wait on any GPU rendering and flushing to occur. */
2405 ret = i915_gem_object_wait_rendering(obj);
2409 i915_gem_object_flush_gtt_write_domain(obj);
2411 /* If we have a partially-valid cache of the object in the CPU,
2412 * finish invalidating it and free the per-page flags.
2414 i915_gem_object_set_to_full_cpu_read_domain(obj);
2416 /* Flush the CPU cache if it's still invalid. */
2417 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
2418 i915_gem_clflush_object(obj);
2420 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2423 /* It should now be out of any other write domains, and we can update
2424 * the domain values for our changes.
2426 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2428 /* If we're writing through the CPU, then the GPU read domains will
2429 * need to be invalidated at next use.
2432 obj->read_domains &= I915_GEM_DOMAIN_CPU;
2433 obj->write_domain = I915_GEM_DOMAIN_CPU;
2440 * Set the next domain for the specified object. This
2441 * may not actually perform the necessary flushing/invaliding though,
2442 * as that may want to be batched with other set_domain operations
2444 * This is (we hope) the only really tricky part of gem. The goal
2445 * is fairly simple -- track which caches hold bits of the object
2446 * and make sure they remain coherent. A few concrete examples may
2447 * help to explain how it works. For shorthand, we use the notation
2448 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
2449 * a pair of read and write domain masks.
2451 * Case 1: the batch buffer
2457 * 5. Unmapped from GTT
2460 * Let's take these a step at a time
2463 * Pages allocated from the kernel may still have
2464 * cache contents, so we set them to (CPU, CPU) always.
2465 * 2. Written by CPU (using pwrite)
2466 * The pwrite function calls set_domain (CPU, CPU) and
2467 * this function does nothing (as nothing changes)
2469 * This function asserts that the object is not
2470 * currently in any GPU-based read or write domains
2472 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
2473 * As write_domain is zero, this function adds in the
2474 * current read domains (CPU+COMMAND, 0).
2475 * flush_domains is set to CPU.
2476 * invalidate_domains is set to COMMAND
2477 * clflush is run to get data out of the CPU caches
2478 * then i915_dev_set_domain calls i915_gem_flush to
2479 * emit an MI_FLUSH and drm_agp_chipset_flush
2480 * 5. Unmapped from GTT
2481 * i915_gem_object_unbind calls set_domain (CPU, CPU)
2482 * flush_domains and invalidate_domains end up both zero
2483 * so no flushing/invalidating happens
2487 * Case 2: The shared render buffer
2491 * 3. Read/written by GPU
2492 * 4. set_domain to (CPU,CPU)
2493 * 5. Read/written by CPU
2494 * 6. Read/written by GPU
2497 * Same as last example, (CPU, CPU)
2499 * Nothing changes (assertions find that it is not in the GPU)
2500 * 3. Read/written by GPU
2501 * execbuffer calls set_domain (RENDER, RENDER)
2502 * flush_domains gets CPU
2503 * invalidate_domains gets GPU
2505 * MI_FLUSH and drm_agp_chipset_flush
2506 * 4. set_domain (CPU, CPU)
2507 * flush_domains gets GPU
2508 * invalidate_domains gets CPU
2509 * wait_rendering (obj) to make sure all drawing is complete.
2510 * This will include an MI_FLUSH to get the data from GPU
2512 * clflush (obj) to invalidate the CPU cache
2513 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
2514 * 5. Read/written by CPU
2515 * cache lines are loaded and dirtied
2516 * 6. Read written by GPU
2517 * Same as last GPU access
2519 * Case 3: The constant buffer
2524 * 4. Updated (written) by CPU again
2533 * flush_domains = CPU
2534 * invalidate_domains = RENDER
2537 * drm_agp_chipset_flush
2538 * 4. Updated (written) by CPU again
2540 * flush_domains = 0 (no previous write domain)
2541 * invalidate_domains = 0 (no new read domains)
2544 * flush_domains = CPU
2545 * invalidate_domains = RENDER
2548 * drm_agp_chipset_flush
2551 i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
2553 struct drm_device *dev = obj->dev;
2554 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2555 uint32_t invalidate_domains = 0;
2556 uint32_t flush_domains = 0;
2558 BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
2559 BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
2562 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
2564 obj->read_domains, obj->pending_read_domains,
2565 obj->write_domain, obj->pending_write_domain);
2568 * If the object isn't moving to a new write domain,
2569 * let the object stay in multiple read domains
2571 if (obj->pending_write_domain == 0)
2572 obj->pending_read_domains |= obj->read_domains;
2574 obj_priv->dirty = 1;
2577 * Flush the current write domain if
2578 * the new read domains don't match. Invalidate
2579 * any read domains which differ from the old
2582 if (obj->write_domain &&
2583 obj->write_domain != obj->pending_read_domains) {
2584 flush_domains |= obj->write_domain;
2585 invalidate_domains |=
2586 obj->pending_read_domains & ~obj->write_domain;
2589 * Invalidate any read caches which may have
2590 * stale data. That is, any new read domains.
2592 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
2593 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2595 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2596 __func__, flush_domains, invalidate_domains);
2598 i915_gem_clflush_object(obj);
2601 /* The actual obj->write_domain will be updated with
2602 * pending_write_domain after we emit the accumulated flush for all
2603 * of our domain changes in execbuffers (which clears objects'
2604 * write_domains). So if we have a current write domain that we
2605 * aren't changing, set pending_write_domain to that.
2607 if (flush_domains == 0 && obj->pending_write_domain == 0)
2608 obj->pending_write_domain = obj->write_domain;
2609 obj->read_domains = obj->pending_read_domains;
2611 dev->invalidate_domains |= invalidate_domains;
2612 dev->flush_domains |= flush_domains;
2614 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2616 obj->read_domains, obj->write_domain,
2617 dev->invalidate_domains, dev->flush_domains);
2622 * Moves the object from a partially CPU read to a full one.
2624 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2625 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2628 i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2630 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2632 if (!obj_priv->page_cpu_valid)
2635 /* If we're partially in the CPU read domain, finish moving it in.
2637 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2640 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2641 if (obj_priv->page_cpu_valid[i])
2643 drm_clflush_pages(obj_priv->pages + i, 1);
2647 /* Free the page_cpu_valid mappings which are now stale, whether
2648 * or not we've got I915_GEM_DOMAIN_CPU.
2650 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
2652 obj_priv->page_cpu_valid = NULL;
2656 * Set the CPU read domain on a range of the object.
2658 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2659 * not entirely valid. The page_cpu_valid member of the object flags which
2660 * pages have been flushed, and will be respected by
2661 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2662 * of the whole object.
2664 * This function returns when the move is complete, including waiting on
2668 i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2669 uint64_t offset, uint64_t size)
2671 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2674 if (offset == 0 && size == obj->size)
2675 return i915_gem_object_set_to_cpu_domain(obj, 0);
2677 i915_gem_object_flush_gpu_write_domain(obj);
2678 /* Wait on any GPU rendering and flushing to occur. */
2679 ret = i915_gem_object_wait_rendering(obj);
2682 i915_gem_object_flush_gtt_write_domain(obj);
2684 /* If we're already fully in the CPU read domain, we're done. */
2685 if (obj_priv->page_cpu_valid == NULL &&
2686 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
2689 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2690 * newly adding I915_GEM_DOMAIN_CPU
2692 if (obj_priv->page_cpu_valid == NULL) {
2693 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
2695 if (obj_priv->page_cpu_valid == NULL)
2697 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2698 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
2700 /* Flush the cache on any pages that are still invalid from the CPU's
2703 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2705 if (obj_priv->page_cpu_valid[i])
2708 drm_clflush_pages(obj_priv->pages + i, 1);
2710 obj_priv->page_cpu_valid[i] = 1;
2713 /* It should now be out of any other write domains, and we can update
2714 * the domain values for our changes.
2716 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2718 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2724 * Pin an object to the GTT and evaluate the relocations landing in it.
2727 i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2728 struct drm_file *file_priv,
2729 struct drm_i915_gem_exec_object *entry,
2730 struct drm_i915_gem_relocation_entry *relocs)
2732 struct drm_device *dev = obj->dev;
2733 drm_i915_private_t *dev_priv = dev->dev_private;
2734 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2736 void __iomem *reloc_page;
2738 /* Choose the GTT offset for our buffer and put it there. */
2739 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2743 entry->offset = obj_priv->gtt_offset;
2745 /* Apply the relocations, using the GTT aperture to avoid cache
2746 * flushing requirements.
2748 for (i = 0; i < entry->relocation_count; i++) {
2749 struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
2750 struct drm_gem_object *target_obj;
2751 struct drm_i915_gem_object *target_obj_priv;
2752 uint32_t reloc_val, reloc_offset;
2753 uint32_t __iomem *reloc_entry;
2755 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
2756 reloc->target_handle);
2757 if (target_obj == NULL) {
2758 i915_gem_object_unpin(obj);
2761 target_obj_priv = target_obj->driver_private;
2763 /* The target buffer should have appeared before us in the
2764 * exec_object list, so it should have a GTT space bound by now.
2766 if (target_obj_priv->gtt_space == NULL) {
2767 DRM_ERROR("No GTT space found for object %d\n",
2768 reloc->target_handle);
2769 drm_gem_object_unreference(target_obj);
2770 i915_gem_object_unpin(obj);
2774 if (reloc->offset > obj->size - 4) {
2775 DRM_ERROR("Relocation beyond object bounds: "
2776 "obj %p target %d offset %d size %d.\n",
2777 obj, reloc->target_handle,
2778 (int) reloc->offset, (int) obj->size);
2779 drm_gem_object_unreference(target_obj);
2780 i915_gem_object_unpin(obj);
2783 if (reloc->offset & 3) {
2784 DRM_ERROR("Relocation not 4-byte aligned: "
2785 "obj %p target %d offset %d.\n",
2786 obj, reloc->target_handle,
2787 (int) reloc->offset);
2788 drm_gem_object_unreference(target_obj);
2789 i915_gem_object_unpin(obj);
2793 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
2794 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
2795 DRM_ERROR("reloc with read/write CPU domains: "
2796 "obj %p target %d offset %d "
2797 "read %08x write %08x",
2798 obj, reloc->target_handle,
2799 (int) reloc->offset,
2800 reloc->read_domains,
2801 reloc->write_domain);
2802 drm_gem_object_unreference(target_obj);
2803 i915_gem_object_unpin(obj);
2807 if (reloc->write_domain && target_obj->pending_write_domain &&
2808 reloc->write_domain != target_obj->pending_write_domain) {
2809 DRM_ERROR("Write domain conflict: "
2810 "obj %p target %d offset %d "
2811 "new %08x old %08x\n",
2812 obj, reloc->target_handle,
2813 (int) reloc->offset,
2814 reloc->write_domain,
2815 target_obj->pending_write_domain);
2816 drm_gem_object_unreference(target_obj);
2817 i915_gem_object_unpin(obj);
2822 DRM_INFO("%s: obj %p offset %08x target %d "
2823 "read %08x write %08x gtt %08x "
2824 "presumed %08x delta %08x\n",
2827 (int) reloc->offset,
2828 (int) reloc->target_handle,
2829 (int) reloc->read_domains,
2830 (int) reloc->write_domain,
2831 (int) target_obj_priv->gtt_offset,
2832 (int) reloc->presumed_offset,
2836 target_obj->pending_read_domains |= reloc->read_domains;
2837 target_obj->pending_write_domain |= reloc->write_domain;
2839 /* If the relocation already has the right value in it, no
2840 * more work needs to be done.
2842 if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
2843 drm_gem_object_unreference(target_obj);
2847 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
2849 drm_gem_object_unreference(target_obj);
2850 i915_gem_object_unpin(obj);
2854 /* Map the page containing the relocation we're going to
2857 reloc_offset = obj_priv->gtt_offset + reloc->offset;
2858 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
2861 reloc_entry = (uint32_t __iomem *)(reloc_page +
2862 (reloc_offset & (PAGE_SIZE - 1)));
2863 reloc_val = target_obj_priv->gtt_offset + reloc->delta;
2866 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
2867 obj, (unsigned int) reloc->offset,
2868 readl(reloc_entry), reloc_val);
2870 writel(reloc_val, reloc_entry);
2871 io_mapping_unmap_atomic(reloc_page);
2873 /* The updated presumed offset for this entry will be
2874 * copied back out to the user.
2876 reloc->presumed_offset = target_obj_priv->gtt_offset;
2878 drm_gem_object_unreference(target_obj);
2883 i915_gem_dump_object(obj, 128, __func__, ~0);
2888 /** Dispatch a batchbuffer to the ring
2891 i915_dispatch_gem_execbuffer(struct drm_device *dev,
2892 struct drm_i915_gem_execbuffer *exec,
2893 struct drm_clip_rect *cliprects,
2894 uint64_t exec_offset)
2896 drm_i915_private_t *dev_priv = dev->dev_private;
2897 int nbox = exec->num_cliprects;
2899 uint32_t exec_start, exec_len;
2902 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
2903 exec_len = (uint32_t) exec->batch_len;
2905 if ((exec_start | exec_len) & 0x7) {
2906 DRM_ERROR("alignment\n");
2913 count = nbox ? nbox : 1;
2915 for (i = 0; i < count; i++) {
2917 int ret = i915_emit_box(dev, cliprects, i,
2918 exec->DR1, exec->DR4);
2923 if (IS_I830(dev) || IS_845G(dev)) {
2925 OUT_RING(MI_BATCH_BUFFER);
2926 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2927 OUT_RING(exec_start + exec_len - 4);
2932 if (IS_I965G(dev)) {
2933 OUT_RING(MI_BATCH_BUFFER_START |
2935 MI_BATCH_NON_SECURE_I965);
2936 OUT_RING(exec_start);
2938 OUT_RING(MI_BATCH_BUFFER_START |
2940 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2946 /* XXX breadcrumb */
2950 /* Throttle our rendering by waiting until the ring has completed our requests
2951 * emitted over 20 msec ago.
2953 * This should get us reasonable parallelism between CPU and GPU but also
2954 * relatively low latency when blocking on a particular request to finish.
2957 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
2959 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2963 mutex_lock(&dev->struct_mutex);
2964 seqno = i915_file_priv->mm.last_gem_throttle_seqno;
2965 i915_file_priv->mm.last_gem_throttle_seqno =
2966 i915_file_priv->mm.last_gem_seqno;
2968 ret = i915_wait_request(dev, seqno);
2969 mutex_unlock(&dev->struct_mutex);
2974 i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list,
2975 uint32_t buffer_count,
2976 struct drm_i915_gem_relocation_entry **relocs)
2978 uint32_t reloc_count = 0, reloc_index = 0, i;
2982 for (i = 0; i < buffer_count; i++) {
2983 if (reloc_count + exec_list[i].relocation_count < reloc_count)
2985 reloc_count += exec_list[i].relocation_count;
2988 *relocs = drm_calloc(reloc_count, sizeof(**relocs), DRM_MEM_DRIVER);
2989 if (*relocs == NULL)
2992 for (i = 0; i < buffer_count; i++) {
2993 struct drm_i915_gem_relocation_entry __user *user_relocs;
2995 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
2997 ret = copy_from_user(&(*relocs)[reloc_index],
2999 exec_list[i].relocation_count *
3002 drm_free(*relocs, reloc_count * sizeof(**relocs),
3008 reloc_index += exec_list[i].relocation_count;
3015 i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list,
3016 uint32_t buffer_count,
3017 struct drm_i915_gem_relocation_entry *relocs)
3019 uint32_t reloc_count = 0, i;
3022 for (i = 0; i < buffer_count; i++) {
3023 struct drm_i915_gem_relocation_entry __user *user_relocs;
3025 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3028 ret = copy_to_user(user_relocs,
3029 &relocs[reloc_count],
3030 exec_list[i].relocation_count *
3034 reloc_count += exec_list[i].relocation_count;
3037 drm_free(relocs, reloc_count * sizeof(*relocs), DRM_MEM_DRIVER);
3043 i915_gem_execbuffer(struct drm_device *dev, void *data,
3044 struct drm_file *file_priv)
3046 drm_i915_private_t *dev_priv = dev->dev_private;
3047 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
3048 struct drm_i915_gem_execbuffer *args = data;
3049 struct drm_i915_gem_exec_object *exec_list = NULL;
3050 struct drm_gem_object **object_list = NULL;
3051 struct drm_gem_object *batch_obj;
3052 struct drm_i915_gem_object *obj_priv;
3053 struct drm_clip_rect *cliprects = NULL;
3054 struct drm_i915_gem_relocation_entry *relocs;
3055 int ret, ret2, i, pinned = 0;
3056 uint64_t exec_offset;
3057 uint32_t seqno, flush_domains, reloc_index;
3061 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3062 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3065 if (args->buffer_count < 1) {
3066 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3069 /* Copy in the exec list from userland */
3070 exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
3072 object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
3074 if (exec_list == NULL || object_list == NULL) {
3075 DRM_ERROR("Failed to allocate exec or object list "
3077 args->buffer_count);
3081 ret = copy_from_user(exec_list,
3082 (struct drm_i915_relocation_entry __user *)
3083 (uintptr_t) args->buffers_ptr,
3084 sizeof(*exec_list) * args->buffer_count);
3086 DRM_ERROR("copy %d exec entries failed %d\n",
3087 args->buffer_count, ret);
3091 if (args->num_cliprects != 0) {
3092 cliprects = drm_calloc(args->num_cliprects, sizeof(*cliprects),
3094 if (cliprects == NULL)
3097 ret = copy_from_user(cliprects,
3098 (struct drm_clip_rect __user *)
3099 (uintptr_t) args->cliprects_ptr,
3100 sizeof(*cliprects) * args->num_cliprects);
3102 DRM_ERROR("copy %d cliprects failed: %d\n",
3103 args->num_cliprects, ret);
3108 ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
3113 mutex_lock(&dev->struct_mutex);
3115 i915_verify_inactive(dev, __FILE__, __LINE__);
3117 if (dev_priv->mm.wedged) {
3118 DRM_ERROR("Execbuf while wedged\n");
3119 mutex_unlock(&dev->struct_mutex);
3124 if (dev_priv->mm.suspended) {
3125 DRM_ERROR("Execbuf while VT-switched.\n");
3126 mutex_unlock(&dev->struct_mutex);
3131 /* Look up object handles */
3132 for (i = 0; i < args->buffer_count; i++) {
3133 object_list[i] = drm_gem_object_lookup(dev, file_priv,
3134 exec_list[i].handle);
3135 if (object_list[i] == NULL) {
3136 DRM_ERROR("Invalid object handle %d at index %d\n",
3137 exec_list[i].handle, i);
3142 obj_priv = object_list[i]->driver_private;
3143 if (obj_priv->in_execbuffer) {
3144 DRM_ERROR("Object %p appears more than once in object list\n",
3149 obj_priv->in_execbuffer = true;
3152 /* Pin and relocate */
3153 for (pin_tries = 0; ; pin_tries++) {
3157 for (i = 0; i < args->buffer_count; i++) {
3158 object_list[i]->pending_read_domains = 0;
3159 object_list[i]->pending_write_domain = 0;
3160 ret = i915_gem_object_pin_and_relocate(object_list[i],
3163 &relocs[reloc_index]);
3167 reloc_index += exec_list[i].relocation_count;
3173 /* error other than GTT full, or we've already tried again */
3174 if (ret != -ENOMEM || pin_tries >= 1) {
3175 if (ret != -ERESTARTSYS)
3176 DRM_ERROR("Failed to pin buffers %d\n", ret);
3180 /* unpin all of our buffers */
3181 for (i = 0; i < pinned; i++)
3182 i915_gem_object_unpin(object_list[i]);
3185 /* evict everyone we can from the aperture */
3186 ret = i915_gem_evict_everything(dev);
3191 /* Set the pending read domains for the batch buffer to COMMAND */
3192 batch_obj = object_list[args->buffer_count-1];
3193 batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
3194 batch_obj->pending_write_domain = 0;
3196 i915_verify_inactive(dev, __FILE__, __LINE__);
3198 /* Zero the global flush/invalidate flags. These
3199 * will be modified as new domains are computed
3202 dev->invalidate_domains = 0;
3203 dev->flush_domains = 0;
3205 for (i = 0; i < args->buffer_count; i++) {
3206 struct drm_gem_object *obj = object_list[i];
3208 /* Compute new gpu domains and update invalidate/flush */
3209 i915_gem_object_set_to_gpu_domain(obj);
3212 i915_verify_inactive(dev, __FILE__, __LINE__);
3214 if (dev->invalidate_domains | dev->flush_domains) {
3216 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3218 dev->invalidate_domains,
3219 dev->flush_domains);
3222 dev->invalidate_domains,
3223 dev->flush_domains);
3224 if (dev->flush_domains)
3225 (void)i915_add_request(dev, dev->flush_domains);
3228 for (i = 0; i < args->buffer_count; i++) {
3229 struct drm_gem_object *obj = object_list[i];
3231 obj->write_domain = obj->pending_write_domain;
3234 i915_verify_inactive(dev, __FILE__, __LINE__);
3237 for (i = 0; i < args->buffer_count; i++) {
3238 i915_gem_object_check_coherency(object_list[i],
3239 exec_list[i].handle);
3243 exec_offset = exec_list[args->buffer_count - 1].offset;
3246 i915_gem_dump_object(object_list[args->buffer_count - 1],
3252 /* Exec the batchbuffer */
3253 ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
3255 DRM_ERROR("dispatch failed %d\n", ret);
3260 * Ensure that the commands in the batch buffer are
3261 * finished before the interrupt fires
3263 flush_domains = i915_retire_commands(dev);
3265 i915_verify_inactive(dev, __FILE__, __LINE__);
3268 * Get a seqno representing the execution of the current buffer,
3269 * which we can wait on. We would like to mitigate these interrupts,
3270 * likely by only creating seqnos occasionally (so that we have
3271 * *some* interrupts representing completion of buffers that we can
3272 * wait on when trying to clear up gtt space).
3274 seqno = i915_add_request(dev, flush_domains);
3276 i915_file_priv->mm.last_gem_seqno = seqno;
3277 for (i = 0; i < args->buffer_count; i++) {
3278 struct drm_gem_object *obj = object_list[i];
3280 i915_gem_object_move_to_active(obj, seqno);
3282 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
3286 i915_dump_lru(dev, __func__);
3289 i915_verify_inactive(dev, __FILE__, __LINE__);
3292 for (i = 0; i < pinned; i++)
3293 i915_gem_object_unpin(object_list[i]);
3295 for (i = 0; i < args->buffer_count; i++) {
3296 if (object_list[i]) {
3297 obj_priv = object_list[i]->driver_private;
3298 obj_priv->in_execbuffer = false;
3300 drm_gem_object_unreference(object_list[i]);
3303 mutex_unlock(&dev->struct_mutex);
3306 /* Copy the new buffer offsets back to the user's exec list. */
3307 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
3308 (uintptr_t) args->buffers_ptr,
3310 sizeof(*exec_list) * args->buffer_count);
3312 DRM_ERROR("failed to copy %d exec entries "
3313 "back to user (%d)\n",
3314 args->buffer_count, ret);
3317 /* Copy the updated relocations out regardless of current error
3318 * state. Failure to update the relocs would mean that the next
3319 * time userland calls execbuf, it would do so with presumed offset
3320 * state that didn't match the actual object state.
3322 ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
3325 DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
3332 drm_free(object_list, sizeof(*object_list) * args->buffer_count,
3334 drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
3336 drm_free(cliprects, sizeof(*cliprects) * args->num_cliprects,
3343 i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
3345 struct drm_device *dev = obj->dev;
3346 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3349 i915_verify_inactive(dev, __FILE__, __LINE__);
3350 if (obj_priv->gtt_space == NULL) {
3351 ret = i915_gem_object_bind_to_gtt(obj, alignment);
3353 if (ret != -EBUSY && ret != -ERESTARTSYS)
3354 DRM_ERROR("Failure to bind: %d\n", ret);
3359 * Pre-965 chips need a fence register set up in order to
3360 * properly handle tiled surfaces.
3362 if (!IS_I965G(dev) &&
3363 obj_priv->fence_reg == I915_FENCE_REG_NONE &&
3364 obj_priv->tiling_mode != I915_TILING_NONE) {
3365 ret = i915_gem_object_get_fence_reg(obj, true);
3367 if (ret != -EBUSY && ret != -ERESTARTSYS)
3368 DRM_ERROR("Failure to install fence: %d\n",
3373 obj_priv->pin_count++;
3375 /* If the object is not active and not pending a flush,
3376 * remove it from the inactive list
3378 if (obj_priv->pin_count == 1) {
3379 atomic_inc(&dev->pin_count);
3380 atomic_add(obj->size, &dev->pin_memory);
3381 if (!obj_priv->active &&
3382 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
3383 I915_GEM_DOMAIN_GTT)) == 0 &&
3384 !list_empty(&obj_priv->list))
3385 list_del_init(&obj_priv->list);
3387 i915_verify_inactive(dev, __FILE__, __LINE__);
3393 i915_gem_object_unpin(struct drm_gem_object *obj)
3395 struct drm_device *dev = obj->dev;
3396 drm_i915_private_t *dev_priv = dev->dev_private;
3397 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3399 i915_verify_inactive(dev, __FILE__, __LINE__);
3400 obj_priv->pin_count--;
3401 BUG_ON(obj_priv->pin_count < 0);
3402 BUG_ON(obj_priv->gtt_space == NULL);
3404 /* If the object is no longer pinned, and is
3405 * neither active nor being flushed, then stick it on
3408 if (obj_priv->pin_count == 0) {
3409 if (!obj_priv->active &&
3410 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
3411 I915_GEM_DOMAIN_GTT)) == 0)
3412 list_move_tail(&obj_priv->list,
3413 &dev_priv->mm.inactive_list);
3414 atomic_dec(&dev->pin_count);
3415 atomic_sub(obj->size, &dev->pin_memory);
3417 i915_verify_inactive(dev, __FILE__, __LINE__);
3421 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3422 struct drm_file *file_priv)
3424 struct drm_i915_gem_pin *args = data;
3425 struct drm_gem_object *obj;
3426 struct drm_i915_gem_object *obj_priv;
3429 mutex_lock(&dev->struct_mutex);
3431 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3433 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
3435 mutex_unlock(&dev->struct_mutex);
3438 obj_priv = obj->driver_private;
3440 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
3441 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3443 drm_gem_object_unreference(obj);
3444 mutex_unlock(&dev->struct_mutex);
3448 obj_priv->user_pin_count++;
3449 obj_priv->pin_filp = file_priv;
3450 if (obj_priv->user_pin_count == 1) {
3451 ret = i915_gem_object_pin(obj, args->alignment);
3453 drm_gem_object_unreference(obj);
3454 mutex_unlock(&dev->struct_mutex);
3459 /* XXX - flush the CPU caches for pinned objects
3460 * as the X server doesn't manage domains yet
3462 i915_gem_object_flush_cpu_write_domain(obj);
3463 args->offset = obj_priv->gtt_offset;
3464 drm_gem_object_unreference(obj);
3465 mutex_unlock(&dev->struct_mutex);
3471 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3472 struct drm_file *file_priv)
3474 struct drm_i915_gem_pin *args = data;
3475 struct drm_gem_object *obj;
3476 struct drm_i915_gem_object *obj_priv;
3478 mutex_lock(&dev->struct_mutex);
3480 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3482 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
3484 mutex_unlock(&dev->struct_mutex);
3488 obj_priv = obj->driver_private;
3489 if (obj_priv->pin_filp != file_priv) {
3490 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3492 drm_gem_object_unreference(obj);
3493 mutex_unlock(&dev->struct_mutex);
3496 obj_priv->user_pin_count--;
3497 if (obj_priv->user_pin_count == 0) {
3498 obj_priv->pin_filp = NULL;
3499 i915_gem_object_unpin(obj);
3502 drm_gem_object_unreference(obj);
3503 mutex_unlock(&dev->struct_mutex);
3508 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3509 struct drm_file *file_priv)
3511 struct drm_i915_gem_busy *args = data;
3512 struct drm_gem_object *obj;
3513 struct drm_i915_gem_object *obj_priv;
3515 mutex_lock(&dev->struct_mutex);
3516 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
3518 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
3520 mutex_unlock(&dev->struct_mutex);
3524 /* Update the active list for the hardware's current position.
3525 * Otherwise this only updates on a delayed timer or when irqs are
3526 * actually unmasked, and our working set ends up being larger than
3529 i915_gem_retire_requests(dev);
3531 obj_priv = obj->driver_private;
3532 /* Don't count being on the flushing list against the object being
3533 * done. Otherwise, a buffer left on the flushing list but not getting
3534 * flushed (because nobody's flushing that domain) won't ever return
3535 * unbusy and get reused by libdrm's bo cache. The other expected
3536 * consumer of this interface, OpenGL's occlusion queries, also specs
3537 * that the objects get unbusy "eventually" without any interference.
3539 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
3541 drm_gem_object_unreference(obj);
3542 mutex_unlock(&dev->struct_mutex);
3547 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3548 struct drm_file *file_priv)
3550 return i915_gem_ring_throttle(dev, file_priv);
3553 int i915_gem_init_object(struct drm_gem_object *obj)
3555 struct drm_i915_gem_object *obj_priv;
3557 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
3558 if (obj_priv == NULL)
3562 * We've just allocated pages from the kernel,
3563 * so they've just been written by the CPU with
3564 * zeros. They'll need to be clflushed before we
3565 * use them with the GPU.
3567 obj->write_domain = I915_GEM_DOMAIN_CPU;
3568 obj->read_domains = I915_GEM_DOMAIN_CPU;
3570 obj_priv->agp_type = AGP_USER_MEMORY;
3572 obj->driver_private = obj_priv;
3573 obj_priv->obj = obj;
3574 obj_priv->fence_reg = I915_FENCE_REG_NONE;
3575 INIT_LIST_HEAD(&obj_priv->list);
3580 void i915_gem_free_object(struct drm_gem_object *obj)
3582 struct drm_device *dev = obj->dev;
3583 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3585 while (obj_priv->pin_count > 0)
3586 i915_gem_object_unpin(obj);
3588 if (obj_priv->phys_obj)
3589 i915_gem_detach_phys_object(dev, obj);
3591 i915_gem_object_unbind(obj);
3593 i915_gem_free_mmap_offset(obj);
3595 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
3596 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
3599 /** Unbinds all objects that are on the given buffer list. */
3601 i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
3603 struct drm_gem_object *obj;
3604 struct drm_i915_gem_object *obj_priv;
3607 while (!list_empty(head)) {
3608 obj_priv = list_first_entry(head,
3609 struct drm_i915_gem_object,
3611 obj = obj_priv->obj;
3613 if (obj_priv->pin_count != 0) {
3614 DRM_ERROR("Pinned object in unbind list\n");
3615 mutex_unlock(&dev->struct_mutex);
3619 ret = i915_gem_object_unbind(obj);
3621 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
3623 mutex_unlock(&dev->struct_mutex);
3633 i915_gem_idle(struct drm_device *dev)
3635 drm_i915_private_t *dev_priv = dev->dev_private;
3636 uint32_t seqno, cur_seqno, last_seqno;
3639 mutex_lock(&dev->struct_mutex);
3641 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
3642 mutex_unlock(&dev->struct_mutex);
3646 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3647 * We need to replace this with a semaphore, or something.
3649 dev_priv->mm.suspended = 1;
3651 /* Cancel the retire work handler, wait for it to finish if running
3653 mutex_unlock(&dev->struct_mutex);
3654 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3655 mutex_lock(&dev->struct_mutex);
3657 i915_kernel_lost_context(dev);
3659 /* Flush the GPU along with all non-CPU write domains
3661 i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
3662 ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
3663 seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU);
3666 mutex_unlock(&dev->struct_mutex);
3670 dev_priv->mm.waiting_gem_seqno = seqno;
3674 cur_seqno = i915_get_gem_seqno(dev);
3675 if (i915_seqno_passed(cur_seqno, seqno))
3677 if (last_seqno == cur_seqno) {
3678 if (stuck++ > 100) {
3679 DRM_ERROR("hardware wedged\n");
3680 dev_priv->mm.wedged = 1;
3681 DRM_WAKEUP(&dev_priv->irq_queue);
3686 last_seqno = cur_seqno;
3688 dev_priv->mm.waiting_gem_seqno = 0;
3690 i915_gem_retire_requests(dev);
3692 spin_lock(&dev_priv->mm.active_list_lock);
3693 if (!dev_priv->mm.wedged) {
3694 /* Active and flushing should now be empty as we've
3695 * waited for a sequence higher than any pending execbuffer
3697 WARN_ON(!list_empty(&dev_priv->mm.active_list));
3698 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
3699 /* Request should now be empty as we've also waited
3700 * for the last request in the list
3702 WARN_ON(!list_empty(&dev_priv->mm.request_list));
3705 /* Empty the active and flushing lists to inactive. If there's
3706 * anything left at this point, it means that we're wedged and
3707 * nothing good's going to happen by leaving them there. So strip
3708 * the GPU domains and just stuff them onto inactive.
3710 while (!list_empty(&dev_priv->mm.active_list)) {
3711 struct drm_i915_gem_object *obj_priv;
3713 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3714 struct drm_i915_gem_object,
3716 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3717 i915_gem_object_move_to_inactive(obj_priv->obj);
3719 spin_unlock(&dev_priv->mm.active_list_lock);
3721 while (!list_empty(&dev_priv->mm.flushing_list)) {
3722 struct drm_i915_gem_object *obj_priv;
3724 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
3725 struct drm_i915_gem_object,
3727 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3728 i915_gem_object_move_to_inactive(obj_priv->obj);
3732 /* Move all inactive buffers out of the GTT. */
3733 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
3734 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
3736 mutex_unlock(&dev->struct_mutex);
3740 i915_gem_cleanup_ringbuffer(dev);
3741 mutex_unlock(&dev->struct_mutex);
3747 i915_gem_init_hws(struct drm_device *dev)
3749 drm_i915_private_t *dev_priv = dev->dev_private;
3750 struct drm_gem_object *obj;
3751 struct drm_i915_gem_object *obj_priv;
3754 /* If we need a physical address for the status page, it's already
3755 * initialized at driver load time.
3757 if (!I915_NEED_GFX_HWS(dev))
3760 obj = drm_gem_object_alloc(dev, 4096);
3762 DRM_ERROR("Failed to allocate status page\n");
3765 obj_priv = obj->driver_private;
3766 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
3768 ret = i915_gem_object_pin(obj, 4096);
3770 drm_gem_object_unreference(obj);
3774 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
3776 dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
3777 if (dev_priv->hw_status_page == NULL) {
3778 DRM_ERROR("Failed to map status page.\n");
3779 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3780 i915_gem_object_unpin(obj);
3781 drm_gem_object_unreference(obj);
3784 dev_priv->hws_obj = obj;
3785 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
3786 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
3787 I915_READ(HWS_PGA); /* posting read */
3788 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
3794 i915_gem_cleanup_hws(struct drm_device *dev)
3796 drm_i915_private_t *dev_priv = dev->dev_private;
3797 struct drm_gem_object *obj;
3798 struct drm_i915_gem_object *obj_priv;
3800 if (dev_priv->hws_obj == NULL)
3803 obj = dev_priv->hws_obj;
3804 obj_priv = obj->driver_private;
3806 kunmap(obj_priv->pages[0]);
3807 i915_gem_object_unpin(obj);
3808 drm_gem_object_unreference(obj);
3809 dev_priv->hws_obj = NULL;
3811 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3812 dev_priv->hw_status_page = NULL;
3814 /* Write high address into HWS_PGA when disabling. */
3815 I915_WRITE(HWS_PGA, 0x1ffff000);
3819 i915_gem_init_ringbuffer(struct drm_device *dev)
3821 drm_i915_private_t *dev_priv = dev->dev_private;
3822 struct drm_gem_object *obj;
3823 struct drm_i915_gem_object *obj_priv;
3824 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
3828 ret = i915_gem_init_hws(dev);
3832 obj = drm_gem_object_alloc(dev, 128 * 1024);
3834 DRM_ERROR("Failed to allocate ringbuffer\n");
3835 i915_gem_cleanup_hws(dev);
3838 obj_priv = obj->driver_private;
3840 ret = i915_gem_object_pin(obj, 4096);
3842 drm_gem_object_unreference(obj);
3843 i915_gem_cleanup_hws(dev);
3847 /* Set up the kernel mapping for the ring. */
3848 ring->Size = obj->size;
3849 ring->tail_mask = obj->size - 1;
3851 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
3852 ring->map.size = obj->size;
3854 ring->map.flags = 0;
3857 drm_core_ioremap_wc(&ring->map, dev);
3858 if (ring->map.handle == NULL) {
3859 DRM_ERROR("Failed to map ringbuffer.\n");
3860 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3861 i915_gem_object_unpin(obj);
3862 drm_gem_object_unreference(obj);
3863 i915_gem_cleanup_hws(dev);
3866 ring->ring_obj = obj;
3867 ring->virtual_start = ring->map.handle;
3869 /* Stop the ring if it's running. */
3870 I915_WRITE(PRB0_CTL, 0);
3871 I915_WRITE(PRB0_TAIL, 0);
3872 I915_WRITE(PRB0_HEAD, 0);
3874 /* Initialize the ring. */
3875 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
3876 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3878 /* G45 ring initialization fails to reset head to zero */
3880 DRM_ERROR("Ring head not reset to zero "
3881 "ctl %08x head %08x tail %08x start %08x\n",
3882 I915_READ(PRB0_CTL),
3883 I915_READ(PRB0_HEAD),
3884 I915_READ(PRB0_TAIL),
3885 I915_READ(PRB0_START));
3886 I915_WRITE(PRB0_HEAD, 0);
3888 DRM_ERROR("Ring head forced to zero "
3889 "ctl %08x head %08x tail %08x start %08x\n",
3890 I915_READ(PRB0_CTL),
3891 I915_READ(PRB0_HEAD),
3892 I915_READ(PRB0_TAIL),
3893 I915_READ(PRB0_START));
3896 I915_WRITE(PRB0_CTL,
3897 ((obj->size - 4096) & RING_NR_PAGES) |
3901 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3903 /* If the head is still not zero, the ring is dead */
3905 DRM_ERROR("Ring initialization failed "
3906 "ctl %08x head %08x tail %08x start %08x\n",
3907 I915_READ(PRB0_CTL),
3908 I915_READ(PRB0_HEAD),
3909 I915_READ(PRB0_TAIL),
3910 I915_READ(PRB0_START));
3914 /* Update our cache of the ring state */
3915 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3916 i915_kernel_lost_context(dev);
3918 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3919 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
3920 ring->space = ring->head - (ring->tail + 8);
3921 if (ring->space < 0)
3922 ring->space += ring->Size;
3929 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3931 drm_i915_private_t *dev_priv = dev->dev_private;
3933 if (dev_priv->ring.ring_obj == NULL)
3936 drm_core_ioremapfree(&dev_priv->ring.map, dev);
3938 i915_gem_object_unpin(dev_priv->ring.ring_obj);
3939 drm_gem_object_unreference(dev_priv->ring.ring_obj);
3940 dev_priv->ring.ring_obj = NULL;
3941 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3943 i915_gem_cleanup_hws(dev);
3947 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3948 struct drm_file *file_priv)
3950 drm_i915_private_t *dev_priv = dev->dev_private;
3953 if (drm_core_check_feature(dev, DRIVER_MODESET))
3956 if (dev_priv->mm.wedged) {
3957 DRM_ERROR("Reenabling wedged hardware, good luck\n");
3958 dev_priv->mm.wedged = 0;
3961 mutex_lock(&dev->struct_mutex);
3962 dev_priv->mm.suspended = 0;
3964 ret = i915_gem_init_ringbuffer(dev);
3968 spin_lock(&dev_priv->mm.active_list_lock);
3969 BUG_ON(!list_empty(&dev_priv->mm.active_list));
3970 spin_unlock(&dev_priv->mm.active_list_lock);
3972 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3973 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3974 BUG_ON(!list_empty(&dev_priv->mm.request_list));
3975 mutex_unlock(&dev->struct_mutex);
3977 drm_irq_install(dev);
3983 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3984 struct drm_file *file_priv)
3988 if (drm_core_check_feature(dev, DRIVER_MODESET))
3991 ret = i915_gem_idle(dev);
3992 drm_irq_uninstall(dev);
3998 i915_gem_lastclose(struct drm_device *dev)
4002 if (drm_core_check_feature(dev, DRIVER_MODESET))
4005 ret = i915_gem_idle(dev);
4007 DRM_ERROR("failed to idle hardware: %d\n", ret);
4011 i915_gem_load(struct drm_device *dev)
4013 drm_i915_private_t *dev_priv = dev->dev_private;
4015 spin_lock_init(&dev_priv->mm.active_list_lock);
4016 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4017 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4018 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4019 INIT_LIST_HEAD(&dev_priv->mm.request_list);
4020 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4021 i915_gem_retire_work_handler);
4022 dev_priv->mm.next_gem_seqno = 1;
4024 /* Old X drivers will take 0-2 for front, back, depth buffers */
4025 dev_priv->fence_reg_start = 3;
4027 if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4028 dev_priv->num_fence_regs = 16;
4030 dev_priv->num_fence_regs = 8;
4032 i915_gem_detect_bit_6_swizzle(dev);
4036 * Create a physically contiguous memory object for this object
4037 * e.g. for cursor + overlay regs
4039 int i915_gem_init_phys_object(struct drm_device *dev,
4042 drm_i915_private_t *dev_priv = dev->dev_private;
4043 struct drm_i915_gem_phys_object *phys_obj;
4046 if (dev_priv->mm.phys_objs[id - 1] || !size)
4049 phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
4055 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
4056 if (!phys_obj->handle) {
4061 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4064 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4068 drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
4072 void i915_gem_free_phys_object(struct drm_device *dev, int id)
4074 drm_i915_private_t *dev_priv = dev->dev_private;
4075 struct drm_i915_gem_phys_object *phys_obj;
4077 if (!dev_priv->mm.phys_objs[id - 1])
4080 phys_obj = dev_priv->mm.phys_objs[id - 1];
4081 if (phys_obj->cur_obj) {
4082 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4086 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4088 drm_pci_free(dev, phys_obj->handle);
4090 dev_priv->mm.phys_objs[id - 1] = NULL;
4093 void i915_gem_free_all_phys_object(struct drm_device *dev)
4097 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
4098 i915_gem_free_phys_object(dev, i);
4101 void i915_gem_detach_phys_object(struct drm_device *dev,
4102 struct drm_gem_object *obj)
4104 struct drm_i915_gem_object *obj_priv;
4109 obj_priv = obj->driver_private;
4110 if (!obj_priv->phys_obj)
4113 ret = i915_gem_object_get_pages(obj);
4117 page_count = obj->size / PAGE_SIZE;
4119 for (i = 0; i < page_count; i++) {
4120 char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
4121 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4123 memcpy(dst, src, PAGE_SIZE);
4124 kunmap_atomic(dst, KM_USER0);
4126 drm_clflush_pages(obj_priv->pages, page_count);
4127 drm_agp_chipset_flush(dev);
4129 obj_priv->phys_obj->cur_obj = NULL;
4130 obj_priv->phys_obj = NULL;
4134 i915_gem_attach_phys_object(struct drm_device *dev,
4135 struct drm_gem_object *obj, int id)
4137 drm_i915_private_t *dev_priv = dev->dev_private;
4138 struct drm_i915_gem_object *obj_priv;
4143 if (id > I915_MAX_PHYS_OBJECT)
4146 obj_priv = obj->driver_private;
4148 if (obj_priv->phys_obj) {
4149 if (obj_priv->phys_obj->id == id)
4151 i915_gem_detach_phys_object(dev, obj);
4155 /* create a new object */
4156 if (!dev_priv->mm.phys_objs[id - 1]) {
4157 ret = i915_gem_init_phys_object(dev, id,
4160 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
4165 /* bind to the object */
4166 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4167 obj_priv->phys_obj->cur_obj = obj;
4169 ret = i915_gem_object_get_pages(obj);
4171 DRM_ERROR("failed to get page list\n");
4175 page_count = obj->size / PAGE_SIZE;
4177 for (i = 0; i < page_count; i++) {
4178 char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
4179 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4181 memcpy(dst, src, PAGE_SIZE);
4182 kunmap_atomic(src, KM_USER0);
4191 i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4192 struct drm_i915_gem_pwrite *args,
4193 struct drm_file *file_priv)
4195 struct drm_i915_gem_object *obj_priv = obj->driver_private;
4198 char __user *user_data;
4200 user_data = (char __user *) (uintptr_t) args->data_ptr;
4201 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4203 DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
4204 ret = copy_from_user(obj_addr, user_data, args->size);
4208 drm_agp_chipset_flush(dev);