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))
38 i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
39 uint32_t read_domains,
40 uint32_t write_domain);
41 static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
42 static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
43 static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
44 static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
46 static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
49 static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
50 static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
51 static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
52 static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
53 static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
55 static void i915_gem_object_get_fence_reg(struct drm_gem_object *obj);
56 static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
57 static int i915_gem_evict_something(struct drm_device *dev);
59 int i915_gem_do_init(struct drm_device *dev, unsigned long start,
62 drm_i915_private_t *dev_priv = dev->dev_private;
65 (start & (PAGE_SIZE - 1)) != 0 ||
66 (end & (PAGE_SIZE - 1)) != 0) {
70 drm_mm_init(&dev_priv->mm.gtt_space, start,
73 dev->gtt_total = (uint32_t) (end - start);
79 i915_gem_init_ioctl(struct drm_device *dev, void *data,
80 struct drm_file *file_priv)
82 struct drm_i915_gem_init *args = data;
85 mutex_lock(&dev->struct_mutex);
86 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
87 mutex_unlock(&dev->struct_mutex);
93 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
94 struct drm_file *file_priv)
96 struct drm_i915_gem_get_aperture *args = data;
98 if (!(dev->driver->driver_features & DRIVER_GEM))
101 args->aper_size = dev->gtt_total;
102 args->aper_available_size = (args->aper_size -
103 atomic_read(&dev->pin_memory));
110 * Creates a new mm object and returns a handle to it.
113 i915_gem_create_ioctl(struct drm_device *dev, void *data,
114 struct drm_file *file_priv)
116 struct drm_i915_gem_create *args = data;
117 struct drm_gem_object *obj;
120 args->size = roundup(args->size, PAGE_SIZE);
122 /* Allocate the new object */
123 obj = drm_gem_object_alloc(dev, args->size);
127 ret = drm_gem_handle_create(file_priv, obj, &handle);
128 mutex_lock(&dev->struct_mutex);
129 drm_gem_object_handle_unreference(obj);
130 mutex_unlock(&dev->struct_mutex);
135 args->handle = handle;
141 * Reads data from the object referenced by handle.
143 * On error, the contents of *data are undefined.
146 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
147 struct drm_file *file_priv)
149 struct drm_i915_gem_pread *args = data;
150 struct drm_gem_object *obj;
151 struct drm_i915_gem_object *obj_priv;
156 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
159 obj_priv = obj->driver_private;
161 /* Bounds check source.
163 * XXX: This could use review for overflow issues...
165 if (args->offset > obj->size || args->size > obj->size ||
166 args->offset + args->size > obj->size) {
167 drm_gem_object_unreference(obj);
171 mutex_lock(&dev->struct_mutex);
173 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
176 drm_gem_object_unreference(obj);
177 mutex_unlock(&dev->struct_mutex);
181 offset = args->offset;
183 read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
184 args->size, &offset);
185 if (read != args->size) {
186 drm_gem_object_unreference(obj);
187 mutex_unlock(&dev->struct_mutex);
194 drm_gem_object_unreference(obj);
195 mutex_unlock(&dev->struct_mutex);
200 /* This is the fast write path which cannot handle
201 * page faults in the source data
205 fast_user_write(struct io_mapping *mapping,
206 loff_t page_base, int page_offset,
207 char __user *user_data,
211 unsigned long unwritten;
213 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
214 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
216 io_mapping_unmap_atomic(vaddr_atomic);
222 /* Here's the write path which can sleep for
227 slow_user_write(struct io_mapping *mapping,
228 loff_t page_base, int page_offset,
229 char __user *user_data,
233 unsigned long unwritten;
235 vaddr = io_mapping_map_wc(mapping, page_base);
238 unwritten = __copy_from_user(vaddr + page_offset,
240 io_mapping_unmap(vaddr);
247 i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
248 struct drm_i915_gem_pwrite *args,
249 struct drm_file *file_priv)
251 struct drm_i915_gem_object *obj_priv = obj->driver_private;
252 drm_i915_private_t *dev_priv = dev->dev_private;
254 loff_t offset, page_base;
255 char __user *user_data;
256 int page_offset, page_length;
259 user_data = (char __user *) (uintptr_t) args->data_ptr;
261 if (!access_ok(VERIFY_READ, user_data, remain))
265 mutex_lock(&dev->struct_mutex);
266 ret = i915_gem_object_pin(obj, 0);
268 mutex_unlock(&dev->struct_mutex);
271 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
275 obj_priv = obj->driver_private;
276 offset = obj_priv->gtt_offset + args->offset;
280 /* Operation in this page
282 * page_base = page offset within aperture
283 * page_offset = offset within page
284 * page_length = bytes to copy for this page
286 page_base = (offset & ~(PAGE_SIZE-1));
287 page_offset = offset & (PAGE_SIZE-1);
288 page_length = remain;
289 if ((page_offset + remain) > PAGE_SIZE)
290 page_length = PAGE_SIZE - page_offset;
292 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
293 page_offset, user_data, page_length);
295 /* If we get a fault while copying data, then (presumably) our
296 * source page isn't available. In this case, use the
297 * non-atomic function
300 ret = slow_user_write (dev_priv->mm.gtt_mapping,
301 page_base, page_offset,
302 user_data, page_length);
307 remain -= page_length;
308 user_data += page_length;
309 offset += page_length;
313 i915_gem_object_unpin(obj);
314 mutex_unlock(&dev->struct_mutex);
320 i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
321 struct drm_i915_gem_pwrite *args,
322 struct drm_file *file_priv)
328 mutex_lock(&dev->struct_mutex);
330 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
332 mutex_unlock(&dev->struct_mutex);
336 offset = args->offset;
338 written = vfs_write(obj->filp,
339 (char __user *)(uintptr_t) args->data_ptr,
340 args->size, &offset);
341 if (written != args->size) {
342 mutex_unlock(&dev->struct_mutex);
349 mutex_unlock(&dev->struct_mutex);
355 * Writes data to the object referenced by handle.
357 * On error, the contents of the buffer that were to be modified are undefined.
360 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
361 struct drm_file *file_priv)
363 struct drm_i915_gem_pwrite *args = data;
364 struct drm_gem_object *obj;
365 struct drm_i915_gem_object *obj_priv;
368 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
371 obj_priv = obj->driver_private;
373 /* Bounds check destination.
375 * XXX: This could use review for overflow issues...
377 if (args->offset > obj->size || args->size > obj->size ||
378 args->offset + args->size > obj->size) {
379 drm_gem_object_unreference(obj);
383 /* We can only do the GTT pwrite on untiled buffers, as otherwise
384 * it would end up going through the fenced access, and we'll get
385 * different detiling behavior between reading and writing.
386 * pread/pwrite currently are reading and writing from the CPU
387 * perspective, requiring manual detiling by the client.
389 if (obj_priv->tiling_mode == I915_TILING_NONE &&
391 ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
393 ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
397 DRM_INFO("pwrite failed %d\n", ret);
400 drm_gem_object_unreference(obj);
406 * Called when user space prepares to use an object with the CPU, either
407 * through the mmap ioctl's mapping or a GTT mapping.
410 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
411 struct drm_file *file_priv)
413 struct drm_i915_gem_set_domain *args = data;
414 struct drm_gem_object *obj;
415 uint32_t read_domains = args->read_domains;
416 uint32_t write_domain = args->write_domain;
419 if (!(dev->driver->driver_features & DRIVER_GEM))
422 /* Only handle setting domains to types used by the CPU. */
423 if (write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
426 if (read_domains & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
429 /* Having something in the write domain implies it's in the read
430 * domain, and only that read domain. Enforce that in the request.
432 if (write_domain != 0 && read_domains != write_domain)
435 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
439 mutex_lock(&dev->struct_mutex);
441 DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
442 obj, obj->size, read_domains, write_domain);
444 if (read_domains & I915_GEM_DOMAIN_GTT) {
445 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
447 /* Silently promote "you're not bound, there was nothing to do"
448 * to success, since the client was just asking us to
449 * make sure everything was done.
454 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
457 drm_gem_object_unreference(obj);
458 mutex_unlock(&dev->struct_mutex);
463 * Called when user space has done writes to this buffer
466 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
467 struct drm_file *file_priv)
469 struct drm_i915_gem_sw_finish *args = data;
470 struct drm_gem_object *obj;
471 struct drm_i915_gem_object *obj_priv;
474 if (!(dev->driver->driver_features & DRIVER_GEM))
477 mutex_lock(&dev->struct_mutex);
478 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
480 mutex_unlock(&dev->struct_mutex);
485 DRM_INFO("%s: sw_finish %d (%p %d)\n",
486 __func__, args->handle, obj, obj->size);
488 obj_priv = obj->driver_private;
490 /* Pinned buffers may be scanout, so flush the cache */
491 if (obj_priv->pin_count)
492 i915_gem_object_flush_cpu_write_domain(obj);
494 drm_gem_object_unreference(obj);
495 mutex_unlock(&dev->struct_mutex);
500 * Maps the contents of an object, returning the address it is mapped
503 * While the mapping holds a reference on the contents of the object, it doesn't
504 * imply a ref on the object itself.
507 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
508 struct drm_file *file_priv)
510 struct drm_i915_gem_mmap *args = data;
511 struct drm_gem_object *obj;
515 if (!(dev->driver->driver_features & DRIVER_GEM))
518 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
522 offset = args->offset;
524 down_write(¤t->mm->mmap_sem);
525 addr = do_mmap(obj->filp, 0, args->size,
526 PROT_READ | PROT_WRITE, MAP_SHARED,
528 up_write(¤t->mm->mmap_sem);
529 mutex_lock(&dev->struct_mutex);
530 drm_gem_object_unreference(obj);
531 mutex_unlock(&dev->struct_mutex);
532 if (IS_ERR((void *)addr))
535 args->addr_ptr = (uint64_t) addr;
541 * i915_gem_fault - fault a page into the GTT
542 * vma: VMA in question
545 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
546 * from userspace. The fault handler takes care of binding the object to
547 * the GTT (if needed), allocating and programming a fence register (again,
548 * only if needed based on whether the old reg is still valid or the object
549 * is tiled) and inserting a new PTE into the faulting process.
551 * Note that the faulting process may involve evicting existing objects
552 * from the GTT and/or fence registers to make room. So performance may
553 * suffer if the GTT working set is large or there are few fence registers
556 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
558 struct drm_gem_object *obj = vma->vm_private_data;
559 struct drm_device *dev = obj->dev;
560 struct drm_i915_private *dev_priv = dev->dev_private;
561 struct drm_i915_gem_object *obj_priv = obj->driver_private;
566 /* We don't use vmf->pgoff since that has the fake offset */
567 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
570 /* Now bind it into the GTT if needed */
571 mutex_lock(&dev->struct_mutex);
572 if (!obj_priv->gtt_space) {
573 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
575 mutex_unlock(&dev->struct_mutex);
576 return VM_FAULT_SIGBUS;
578 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
581 /* Need a new fence register? */
582 if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
583 obj_priv->tiling_mode != I915_TILING_NONE)
584 i915_gem_object_get_fence_reg(obj);
586 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
589 /* Finally, remap it using the new GTT offset */
590 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
592 mutex_unlock(&dev->struct_mutex);
600 DRM_ERROR("can't insert pfn?? fault or busy...\n");
601 return VM_FAULT_SIGBUS;
603 return VM_FAULT_NOPAGE;
608 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
609 * @obj: obj in question
611 * GEM memory mapping works by handing back to userspace a fake mmap offset
612 * it can use in a subsequent mmap(2) call. The DRM core code then looks
613 * up the object based on the offset and sets up the various memory mapping
616 * This routine allocates and attaches a fake offset for @obj.
619 i915_gem_create_mmap_offset(struct drm_gem_object *obj)
621 struct drm_device *dev = obj->dev;
622 struct drm_gem_mm *mm = dev->mm_private;
623 struct drm_i915_gem_object *obj_priv = obj->driver_private;
624 struct drm_map_list *list;
628 /* Set the object up for mmap'ing */
629 list = &obj->map_list;
630 list->map = drm_calloc(1, sizeof(struct drm_map_list),
636 map->type = _DRM_GEM;
637 map->size = obj->size;
640 /* Get a DRM GEM mmap offset allocated... */
641 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
642 obj->size / PAGE_SIZE, 0, 0);
643 if (!list->file_offset_node) {
644 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
649 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
650 obj->size / PAGE_SIZE, 0);
651 if (!list->file_offset_node) {
656 list->hash.key = list->file_offset_node->start;
657 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
658 DRM_ERROR("failed to add to map hash\n");
662 /* By now we should be all set, any drm_mmap request on the offset
663 * below will get to our mmap & fault handler */
664 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
669 drm_mm_put_block(list->file_offset_node);
671 drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER);
677 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
678 * @obj: object to check
680 * Return the required GTT alignment for an object, taking into account
681 * potential fence register mapping if needed.
684 i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
686 struct drm_device *dev = obj->dev;
687 struct drm_i915_gem_object *obj_priv = obj->driver_private;
691 * Minimum alignment is 4k (GTT page size), but might be greater
692 * if a fence register is needed for the object.
694 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
698 * Previous chips need to be aligned to the size of the smallest
699 * fence register that can contain the object.
706 for (i = start; i < obj->size; i <<= 1)
713 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
715 * @data: GTT mapping ioctl data
716 * @file_priv: GEM object info
718 * Simply returns the fake offset to userspace so it can mmap it.
719 * The mmap call will end up in drm_gem_mmap(), which will set things
720 * up so we can get faults in the handler above.
722 * The fault handler will take care of binding the object into the GTT
723 * (since it may have been evicted to make room for something), allocating
724 * a fence register, and mapping the appropriate aperture address into
728 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
729 struct drm_file *file_priv)
731 struct drm_i915_gem_mmap_gtt *args = data;
732 struct drm_i915_private *dev_priv = dev->dev_private;
733 struct drm_gem_object *obj;
734 struct drm_i915_gem_object *obj_priv;
737 if (!(dev->driver->driver_features & DRIVER_GEM))
740 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
744 mutex_lock(&dev->struct_mutex);
746 obj_priv = obj->driver_private;
748 if (!obj_priv->mmap_offset) {
749 ret = i915_gem_create_mmap_offset(obj);
754 args->offset = obj_priv->mmap_offset;
756 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
758 /* Make sure the alignment is correct for fence regs etc */
759 if (obj_priv->agp_mem &&
760 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
761 drm_gem_object_unreference(obj);
762 mutex_unlock(&dev->struct_mutex);
767 * Pull it into the GTT so that we have a page list (makes the
768 * initial fault faster and any subsequent flushing possible).
770 if (!obj_priv->agp_mem) {
771 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
773 drm_gem_object_unreference(obj);
774 mutex_unlock(&dev->struct_mutex);
777 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
780 drm_gem_object_unreference(obj);
781 mutex_unlock(&dev->struct_mutex);
787 i915_gem_object_free_page_list(struct drm_gem_object *obj)
789 struct drm_i915_gem_object *obj_priv = obj->driver_private;
790 int page_count = obj->size / PAGE_SIZE;
793 if (obj_priv->page_list == NULL)
797 for (i = 0; i < page_count; i++)
798 if (obj_priv->page_list[i] != NULL) {
800 set_page_dirty(obj_priv->page_list[i]);
801 mark_page_accessed(obj_priv->page_list[i]);
802 page_cache_release(obj_priv->page_list[i]);
806 drm_free(obj_priv->page_list,
807 page_count * sizeof(struct page *),
809 obj_priv->page_list = NULL;
813 i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
815 struct drm_device *dev = obj->dev;
816 drm_i915_private_t *dev_priv = dev->dev_private;
817 struct drm_i915_gem_object *obj_priv = obj->driver_private;
819 /* Add a reference if we're newly entering the active list. */
820 if (!obj_priv->active) {
821 drm_gem_object_reference(obj);
822 obj_priv->active = 1;
824 /* Move from whatever list we were on to the tail of execution. */
825 list_move_tail(&obj_priv->list,
826 &dev_priv->mm.active_list);
827 obj_priv->last_rendering_seqno = seqno;
831 i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
833 struct drm_device *dev = obj->dev;
834 drm_i915_private_t *dev_priv = dev->dev_private;
835 struct drm_i915_gem_object *obj_priv = obj->driver_private;
837 BUG_ON(!obj_priv->active);
838 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
839 obj_priv->last_rendering_seqno = 0;
843 i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
845 struct drm_device *dev = obj->dev;
846 drm_i915_private_t *dev_priv = dev->dev_private;
847 struct drm_i915_gem_object *obj_priv = obj->driver_private;
849 i915_verify_inactive(dev, __FILE__, __LINE__);
850 if (obj_priv->pin_count != 0)
851 list_del_init(&obj_priv->list);
853 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
855 obj_priv->last_rendering_seqno = 0;
856 if (obj_priv->active) {
857 obj_priv->active = 0;
858 drm_gem_object_unreference(obj);
860 i915_verify_inactive(dev, __FILE__, __LINE__);
864 * Creates a new sequence number, emitting a write of it to the status page
865 * plus an interrupt, which will trigger i915_user_interrupt_handler.
867 * Must be called with struct_lock held.
869 * Returned sequence numbers are nonzero on success.
872 i915_add_request(struct drm_device *dev, uint32_t flush_domains)
874 drm_i915_private_t *dev_priv = dev->dev_private;
875 struct drm_i915_gem_request *request;
880 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
884 /* Grab the seqno we're going to make this request be, and bump the
885 * next (skipping 0 so it can be the reserved no-seqno value).
887 seqno = dev_priv->mm.next_gem_seqno;
888 dev_priv->mm.next_gem_seqno++;
889 if (dev_priv->mm.next_gem_seqno == 0)
890 dev_priv->mm.next_gem_seqno++;
893 OUT_RING(MI_STORE_DWORD_INDEX);
894 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
897 OUT_RING(MI_USER_INTERRUPT);
900 DRM_DEBUG("%d\n", seqno);
902 request->seqno = seqno;
903 request->emitted_jiffies = jiffies;
904 was_empty = list_empty(&dev_priv->mm.request_list);
905 list_add_tail(&request->list, &dev_priv->mm.request_list);
907 /* Associate any objects on the flushing list matching the write
908 * domain we're flushing with our flush.
910 if (flush_domains != 0) {
911 struct drm_i915_gem_object *obj_priv, *next;
913 list_for_each_entry_safe(obj_priv, next,
914 &dev_priv->mm.flushing_list, list) {
915 struct drm_gem_object *obj = obj_priv->obj;
917 if ((obj->write_domain & flush_domains) ==
919 obj->write_domain = 0;
920 i915_gem_object_move_to_active(obj, seqno);
926 if (was_empty && !dev_priv->mm.suspended)
927 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
932 * Command execution barrier
934 * Ensures that all commands in the ring are finished
935 * before signalling the CPU
938 i915_retire_commands(struct drm_device *dev)
940 drm_i915_private_t *dev_priv = dev->dev_private;
941 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
942 uint32_t flush_domains = 0;
945 /* The sampler always gets flushed on i965 (sigh) */
947 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
950 OUT_RING(0); /* noop */
952 return flush_domains;
956 * Moves buffers associated only with the given active seqno from the active
957 * to inactive list, potentially freeing them.
960 i915_gem_retire_request(struct drm_device *dev,
961 struct drm_i915_gem_request *request)
963 drm_i915_private_t *dev_priv = dev->dev_private;
965 /* Move any buffers on the active list that are no longer referenced
966 * by the ringbuffer to the flushing/inactive lists as appropriate.
968 while (!list_empty(&dev_priv->mm.active_list)) {
969 struct drm_gem_object *obj;
970 struct drm_i915_gem_object *obj_priv;
972 obj_priv = list_first_entry(&dev_priv->mm.active_list,
973 struct drm_i915_gem_object,
977 /* If the seqno being retired doesn't match the oldest in the
978 * list, then the oldest in the list must still be newer than
981 if (obj_priv->last_rendering_seqno != request->seqno)
985 DRM_INFO("%s: retire %d moves to inactive list %p\n",
986 __func__, request->seqno, obj);
989 if (obj->write_domain != 0)
990 i915_gem_object_move_to_flushing(obj);
992 i915_gem_object_move_to_inactive(obj);
997 * Returns true if seq1 is later than seq2.
1000 i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1002 return (int32_t)(seq1 - seq2) >= 0;
1006 i915_get_gem_seqno(struct drm_device *dev)
1008 drm_i915_private_t *dev_priv = dev->dev_private;
1010 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1014 * This function clears the request list as sequence numbers are passed.
1017 i915_gem_retire_requests(struct drm_device *dev)
1019 drm_i915_private_t *dev_priv = dev->dev_private;
1022 seqno = i915_get_gem_seqno(dev);
1024 while (!list_empty(&dev_priv->mm.request_list)) {
1025 struct drm_i915_gem_request *request;
1026 uint32_t retiring_seqno;
1028 request = list_first_entry(&dev_priv->mm.request_list,
1029 struct drm_i915_gem_request,
1031 retiring_seqno = request->seqno;
1033 if (i915_seqno_passed(seqno, retiring_seqno) ||
1034 dev_priv->mm.wedged) {
1035 i915_gem_retire_request(dev, request);
1037 list_del(&request->list);
1038 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
1045 i915_gem_retire_work_handler(struct work_struct *work)
1047 drm_i915_private_t *dev_priv;
1048 struct drm_device *dev;
1050 dev_priv = container_of(work, drm_i915_private_t,
1051 mm.retire_work.work);
1052 dev = dev_priv->dev;
1054 mutex_lock(&dev->struct_mutex);
1055 i915_gem_retire_requests(dev);
1056 if (!dev_priv->mm.suspended &&
1057 !list_empty(&dev_priv->mm.request_list))
1058 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1059 mutex_unlock(&dev->struct_mutex);
1063 * Waits for a sequence number to be signaled, and cleans up the
1064 * request and object lists appropriately for that event.
1067 i915_wait_request(struct drm_device *dev, uint32_t seqno)
1069 drm_i915_private_t *dev_priv = dev->dev_private;
1074 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
1075 dev_priv->mm.waiting_gem_seqno = seqno;
1076 i915_user_irq_get(dev);
1077 ret = wait_event_interruptible(dev_priv->irq_queue,
1078 i915_seqno_passed(i915_get_gem_seqno(dev),
1080 dev_priv->mm.wedged);
1081 i915_user_irq_put(dev);
1082 dev_priv->mm.waiting_gem_seqno = 0;
1084 if (dev_priv->mm.wedged)
1087 if (ret && ret != -ERESTARTSYS)
1088 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1089 __func__, ret, seqno, i915_get_gem_seqno(dev));
1091 /* Directly dispatch request retiring. While we have the work queue
1092 * to handle this, the waiter on a request often wants an associated
1093 * buffer to have made it to the inactive list, and we would need
1094 * a separate wait queue to handle that.
1097 i915_gem_retire_requests(dev);
1103 i915_gem_flush(struct drm_device *dev,
1104 uint32_t invalidate_domains,
1105 uint32_t flush_domains)
1107 drm_i915_private_t *dev_priv = dev->dev_private;
1112 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1113 invalidate_domains, flush_domains);
1116 if (flush_domains & I915_GEM_DOMAIN_CPU)
1117 drm_agp_chipset_flush(dev);
1119 if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
1120 I915_GEM_DOMAIN_GTT)) {
1122 * read/write caches:
1124 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1125 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1126 * also flushed at 2d versus 3d pipeline switches.
1130 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1131 * MI_READ_FLUSH is set, and is always flushed on 965.
1133 * I915_GEM_DOMAIN_COMMAND may not exist?
1135 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1136 * invalidated when MI_EXE_FLUSH is set.
1138 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1139 * invalidated with every MI_FLUSH.
1143 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1144 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1145 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1146 * are flushed at any MI_FLUSH.
1149 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1150 if ((invalidate_domains|flush_domains) &
1151 I915_GEM_DOMAIN_RENDER)
1152 cmd &= ~MI_NO_WRITE_FLUSH;
1153 if (!IS_I965G(dev)) {
1155 * On the 965, the sampler cache always gets flushed
1156 * and this bit is reserved.
1158 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1159 cmd |= MI_READ_FLUSH;
1161 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1162 cmd |= MI_EXE_FLUSH;
1165 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1169 OUT_RING(0); /* noop */
1175 * Ensures that all rendering to the object has completed and the object is
1176 * safe to unbind from the GTT or access from the CPU.
1179 i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1181 struct drm_device *dev = obj->dev;
1182 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1185 /* This function only exists to support waiting for existing rendering,
1186 * not for emitting required flushes.
1188 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
1190 /* If there is rendering queued on the buffer being evicted, wait for
1193 if (obj_priv->active) {
1195 DRM_INFO("%s: object %p wait for seqno %08x\n",
1196 __func__, obj, obj_priv->last_rendering_seqno);
1198 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1207 * Unbinds an object from the GTT aperture.
1210 i915_gem_object_unbind(struct drm_gem_object *obj)
1212 struct drm_device *dev = obj->dev;
1213 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1218 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1219 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1221 if (obj_priv->gtt_space == NULL)
1224 if (obj_priv->pin_count != 0) {
1225 DRM_ERROR("Attempting to unbind pinned buffer\n");
1229 /* Move the object to the CPU domain to ensure that
1230 * any possible CPU writes while it's not in the GTT
1231 * are flushed when we go to remap it. This will
1232 * also ensure that all pending GPU writes are finished
1235 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1237 if (ret != -ERESTARTSYS)
1238 DRM_ERROR("set_domain failed: %d\n", ret);
1242 if (obj_priv->agp_mem != NULL) {
1243 drm_unbind_agp(obj_priv->agp_mem);
1244 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1245 obj_priv->agp_mem = NULL;
1248 BUG_ON(obj_priv->active);
1250 /* blow away mappings if mapped through GTT */
1251 offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT;
1252 if (dev->dev_mapping)
1253 unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1);
1255 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1256 i915_gem_clear_fence_reg(obj);
1258 i915_gem_object_free_page_list(obj);
1260 if (obj_priv->gtt_space) {
1261 atomic_dec(&dev->gtt_count);
1262 atomic_sub(obj->size, &dev->gtt_memory);
1264 drm_mm_put_block(obj_priv->gtt_space);
1265 obj_priv->gtt_space = NULL;
1268 /* Remove ourselves from the LRU list if present. */
1269 if (!list_empty(&obj_priv->list))
1270 list_del_init(&obj_priv->list);
1276 i915_gem_evict_something(struct drm_device *dev)
1278 drm_i915_private_t *dev_priv = dev->dev_private;
1279 struct drm_gem_object *obj;
1280 struct drm_i915_gem_object *obj_priv;
1284 /* If there's an inactive buffer available now, grab it
1287 if (!list_empty(&dev_priv->mm.inactive_list)) {
1288 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1289 struct drm_i915_gem_object,
1291 obj = obj_priv->obj;
1292 BUG_ON(obj_priv->pin_count != 0);
1294 DRM_INFO("%s: evicting %p\n", __func__, obj);
1296 BUG_ON(obj_priv->active);
1298 /* Wait on the rendering and unbind the buffer. */
1299 ret = i915_gem_object_unbind(obj);
1303 /* If we didn't get anything, but the ring is still processing
1304 * things, wait for one of those things to finish and hopefully
1305 * leave us a buffer to evict.
1307 if (!list_empty(&dev_priv->mm.request_list)) {
1308 struct drm_i915_gem_request *request;
1310 request = list_first_entry(&dev_priv->mm.request_list,
1311 struct drm_i915_gem_request,
1314 ret = i915_wait_request(dev, request->seqno);
1318 /* if waiting caused an object to become inactive,
1319 * then loop around and wait for it. Otherwise, we
1320 * assume that waiting freed and unbound something,
1321 * so there should now be some space in the GTT
1323 if (!list_empty(&dev_priv->mm.inactive_list))
1328 /* If we didn't have anything on the request list but there
1329 * are buffers awaiting a flush, emit one and try again.
1330 * When we wait on it, those buffers waiting for that flush
1331 * will get moved to inactive.
1333 if (!list_empty(&dev_priv->mm.flushing_list)) {
1334 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1335 struct drm_i915_gem_object,
1337 obj = obj_priv->obj;
1342 i915_add_request(dev, obj->write_domain);
1348 DRM_ERROR("inactive empty %d request empty %d "
1349 "flushing empty %d\n",
1350 list_empty(&dev_priv->mm.inactive_list),
1351 list_empty(&dev_priv->mm.request_list),
1352 list_empty(&dev_priv->mm.flushing_list));
1353 /* If we didn't do any of the above, there's nothing to be done
1354 * and we just can't fit it in.
1362 i915_gem_evict_everything(struct drm_device *dev)
1367 ret = i915_gem_evict_something(dev);
1377 i915_gem_object_get_page_list(struct drm_gem_object *obj)
1379 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1381 struct address_space *mapping;
1382 struct inode *inode;
1386 if (obj_priv->page_list)
1389 /* Get the list of pages out of our struct file. They'll be pinned
1390 * at this point until we release them.
1392 page_count = obj->size / PAGE_SIZE;
1393 BUG_ON(obj_priv->page_list != NULL);
1394 obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
1396 if (obj_priv->page_list == NULL) {
1397 DRM_ERROR("Faled to allocate page list\n");
1401 inode = obj->filp->f_path.dentry->d_inode;
1402 mapping = inode->i_mapping;
1403 for (i = 0; i < page_count; i++) {
1404 page = read_mapping_page(mapping, i, NULL);
1406 ret = PTR_ERR(page);
1407 DRM_ERROR("read_mapping_page failed: %d\n", ret);
1408 i915_gem_object_free_page_list(obj);
1411 obj_priv->page_list[i] = page;
1416 static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
1418 struct drm_gem_object *obj = reg->obj;
1419 struct drm_device *dev = obj->dev;
1420 drm_i915_private_t *dev_priv = dev->dev_private;
1421 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1422 int regnum = obj_priv->fence_reg;
1425 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
1427 val |= obj_priv->gtt_offset & 0xfffff000;
1428 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
1429 if (obj_priv->tiling_mode == I915_TILING_Y)
1430 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
1431 val |= I965_FENCE_REG_VALID;
1433 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
1436 static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
1438 struct drm_gem_object *obj = reg->obj;
1439 struct drm_device *dev = obj->dev;
1440 drm_i915_private_t *dev_priv = dev->dev_private;
1441 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1442 int regnum = obj_priv->fence_reg;
1446 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1447 (obj_priv->gtt_offset & (obj->size - 1))) {
1448 WARN(1, "%s: object not 1M or size aligned\n", __func__);
1452 if (obj_priv->tiling_mode == I915_TILING_Y && (IS_I945G(dev) ||
1455 pitch_val = (obj_priv->stride / 128) - 1;
1457 pitch_val = (obj_priv->stride / 512) - 1;
1459 val = obj_priv->gtt_offset;
1460 if (obj_priv->tiling_mode == I915_TILING_Y)
1461 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
1462 val |= I915_FENCE_SIZE_BITS(obj->size);
1463 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
1464 val |= I830_FENCE_REG_VALID;
1466 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
1469 static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
1471 struct drm_gem_object *obj = reg->obj;
1472 struct drm_device *dev = obj->dev;
1473 drm_i915_private_t *dev_priv = dev->dev_private;
1474 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1475 int regnum = obj_priv->fence_reg;
1479 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1480 (obj_priv->gtt_offset & (obj->size - 1))) {
1481 WARN(1, "%s: object not 1M or size aligned\n", __func__);
1485 pitch_val = (obj_priv->stride / 128) - 1;
1487 val = obj_priv->gtt_offset;
1488 if (obj_priv->tiling_mode == I915_TILING_Y)
1489 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
1490 val |= I830_FENCE_SIZE_BITS(obj->size);
1491 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
1492 val |= I830_FENCE_REG_VALID;
1494 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
1499 * i915_gem_object_get_fence_reg - set up a fence reg for an object
1500 * @obj: object to map through a fence reg
1502 * When mapping objects through the GTT, userspace wants to be able to write
1503 * to them without having to worry about swizzling if the object is tiled.
1505 * This function walks the fence regs looking for a free one for @obj,
1506 * stealing one if it can't find any.
1508 * It then sets up the reg based on the object's properties: address, pitch
1509 * and tiling format.
1512 i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
1514 struct drm_device *dev = obj->dev;
1515 struct drm_i915_private *dev_priv = dev->dev_private;
1516 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1517 struct drm_i915_fence_reg *reg = NULL;
1520 switch (obj_priv->tiling_mode) {
1521 case I915_TILING_NONE:
1522 WARN(1, "allocating a fence for non-tiled object?\n");
1525 WARN(obj_priv->stride & (512 - 1),
1526 "object is X tiled but has non-512B pitch\n");
1529 WARN(obj_priv->stride & (128 - 1),
1530 "object is Y tiled but has non-128B pitch\n");
1534 /* First try to find a free reg */
1535 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
1536 reg = &dev_priv->fence_regs[i];
1541 /* None available, try to steal one or wait for a user to finish */
1542 if (i == dev_priv->num_fence_regs) {
1543 struct drm_i915_gem_object *old_obj_priv = NULL;
1547 /* Could try to use LRU here instead... */
1548 for (i = dev_priv->fence_reg_start;
1549 i < dev_priv->num_fence_regs; i++) {
1550 reg = &dev_priv->fence_regs[i];
1551 old_obj_priv = reg->obj->driver_private;
1552 if (!old_obj_priv->pin_count)
1557 * Now things get ugly... we have to wait for one of the
1558 * objects to finish before trying again.
1560 if (i == dev_priv->num_fence_regs) {
1561 ret = i915_gem_object_wait_rendering(reg->obj);
1563 WARN(ret, "wait_rendering failed: %d\n", ret);
1570 * Zap this virtual mapping so we can set up a fence again
1571 * for this object next time we need it.
1573 offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
1574 if (dev->dev_mapping)
1575 unmap_mapping_range(dev->dev_mapping, offset,
1577 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
1580 obj_priv->fence_reg = i;
1584 i965_write_fence_reg(reg);
1585 else if (IS_I9XX(dev))
1586 i915_write_fence_reg(reg);
1588 i830_write_fence_reg(reg);
1592 * i915_gem_clear_fence_reg - clear out fence register info
1593 * @obj: object to clear
1595 * Zeroes out the fence register itself and clears out the associated
1596 * data structures in dev_priv and obj_priv.
1599 i915_gem_clear_fence_reg(struct drm_gem_object *obj)
1601 struct drm_device *dev = obj->dev;
1602 drm_i915_private_t *dev_priv = dev->dev_private;
1603 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1606 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
1608 I915_WRITE(FENCE_REG_830_0 + (obj_priv->fence_reg * 4), 0);
1610 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
1611 obj_priv->fence_reg = I915_FENCE_REG_NONE;
1615 * Finds free space in the GTT aperture and binds the object there.
1618 i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
1620 struct drm_device *dev = obj->dev;
1621 drm_i915_private_t *dev_priv = dev->dev_private;
1622 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1623 struct drm_mm_node *free_space;
1624 int page_count, ret;
1626 if (dev_priv->mm.suspended)
1629 alignment = PAGE_SIZE;
1630 if (alignment & (PAGE_SIZE - 1)) {
1631 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
1636 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
1637 obj->size, alignment, 0);
1638 if (free_space != NULL) {
1639 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
1641 if (obj_priv->gtt_space != NULL) {
1642 obj_priv->gtt_space->private = obj;
1643 obj_priv->gtt_offset = obj_priv->gtt_space->start;
1646 if (obj_priv->gtt_space == NULL) {
1647 /* If the gtt is empty and we're still having trouble
1648 * fitting our object in, we're out of memory.
1651 DRM_INFO("%s: GTT full, evicting something\n", __func__);
1653 if (list_empty(&dev_priv->mm.inactive_list) &&
1654 list_empty(&dev_priv->mm.flushing_list) &&
1655 list_empty(&dev_priv->mm.active_list)) {
1656 DRM_ERROR("GTT full, but LRU list empty\n");
1660 ret = i915_gem_evict_something(dev);
1662 if (ret != -ERESTARTSYS)
1663 DRM_ERROR("Failed to evict a buffer %d\n", ret);
1670 DRM_INFO("Binding object of size %d at 0x%08x\n",
1671 obj->size, obj_priv->gtt_offset);
1673 ret = i915_gem_object_get_page_list(obj);
1675 drm_mm_put_block(obj_priv->gtt_space);
1676 obj_priv->gtt_space = NULL;
1680 page_count = obj->size / PAGE_SIZE;
1681 /* Create an AGP memory structure pointing at our pages, and bind it
1684 obj_priv->agp_mem = drm_agp_bind_pages(dev,
1685 obj_priv->page_list,
1687 obj_priv->gtt_offset,
1688 obj_priv->agp_type);
1689 if (obj_priv->agp_mem == NULL) {
1690 i915_gem_object_free_page_list(obj);
1691 drm_mm_put_block(obj_priv->gtt_space);
1692 obj_priv->gtt_space = NULL;
1695 atomic_inc(&dev->gtt_count);
1696 atomic_add(obj->size, &dev->gtt_memory);
1698 /* Assert that the object is not currently in any GPU domain. As it
1699 * wasn't in the GTT, there shouldn't be any way it could have been in
1702 BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1703 BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1709 i915_gem_clflush_object(struct drm_gem_object *obj)
1711 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1713 /* If we don't have a page list set up, then we're not pinned
1714 * to GPU, and we can ignore the cache flush because it'll happen
1715 * again at bind time.
1717 if (obj_priv->page_list == NULL)
1720 drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
1723 /** Flushes any GPU write domain for the object if it's dirty. */
1725 i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
1727 struct drm_device *dev = obj->dev;
1730 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
1733 /* Queue the GPU write cache flushing we need. */
1734 i915_gem_flush(dev, 0, obj->write_domain);
1735 seqno = i915_add_request(dev, obj->write_domain);
1736 obj->write_domain = 0;
1737 i915_gem_object_move_to_active(obj, seqno);
1740 /** Flushes the GTT write domain for the object if it's dirty. */
1742 i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
1744 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
1747 /* No actual flushing is required for the GTT write domain. Writes
1748 * to it immediately go to main memory as far as we know, so there's
1749 * no chipset flush. It also doesn't land in render cache.
1751 obj->write_domain = 0;
1754 /** Flushes the CPU write domain for the object if it's dirty. */
1756 i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
1758 struct drm_device *dev = obj->dev;
1760 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
1763 i915_gem_clflush_object(obj);
1764 drm_agp_chipset_flush(dev);
1765 obj->write_domain = 0;
1769 * Moves a single object to the GTT read, and possibly write domain.
1771 * This function returns when the move is complete, including waiting on
1775 i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
1777 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1780 /* Not valid to be called on unbound objects. */
1781 if (obj_priv->gtt_space == NULL)
1784 i915_gem_object_flush_gpu_write_domain(obj);
1785 /* Wait on any GPU rendering and flushing to occur. */
1786 ret = i915_gem_object_wait_rendering(obj);
1790 /* If we're writing through the GTT domain, then CPU and GPU caches
1791 * will need to be invalidated at next use.
1794 obj->read_domains &= I915_GEM_DOMAIN_GTT;
1796 i915_gem_object_flush_cpu_write_domain(obj);
1798 /* It should now be out of any other write domains, and we can update
1799 * the domain values for our changes.
1801 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
1802 obj->read_domains |= I915_GEM_DOMAIN_GTT;
1804 obj->write_domain = I915_GEM_DOMAIN_GTT;
1805 obj_priv->dirty = 1;
1812 * Moves a single object to the CPU read, and possibly write domain.
1814 * This function returns when the move is complete, including waiting on
1818 i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
1820 struct drm_device *dev = obj->dev;
1823 i915_gem_object_flush_gpu_write_domain(obj);
1824 /* Wait on any GPU rendering and flushing to occur. */
1825 ret = i915_gem_object_wait_rendering(obj);
1829 i915_gem_object_flush_gtt_write_domain(obj);
1831 /* If we have a partially-valid cache of the object in the CPU,
1832 * finish invalidating it and free the per-page flags.
1834 i915_gem_object_set_to_full_cpu_read_domain(obj);
1836 /* Flush the CPU cache if it's still invalid. */
1837 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
1838 i915_gem_clflush_object(obj);
1839 drm_agp_chipset_flush(dev);
1841 obj->read_domains |= I915_GEM_DOMAIN_CPU;
1844 /* It should now be out of any other write domains, and we can update
1845 * the domain values for our changes.
1847 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
1849 /* If we're writing through the CPU, then the GPU read domains will
1850 * need to be invalidated at next use.
1853 obj->read_domains &= I915_GEM_DOMAIN_CPU;
1854 obj->write_domain = I915_GEM_DOMAIN_CPU;
1861 * Set the next domain for the specified object. This
1862 * may not actually perform the necessary flushing/invaliding though,
1863 * as that may want to be batched with other set_domain operations
1865 * This is (we hope) the only really tricky part of gem. The goal
1866 * is fairly simple -- track which caches hold bits of the object
1867 * and make sure they remain coherent. A few concrete examples may
1868 * help to explain how it works. For shorthand, we use the notation
1869 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
1870 * a pair of read and write domain masks.
1872 * Case 1: the batch buffer
1878 * 5. Unmapped from GTT
1881 * Let's take these a step at a time
1884 * Pages allocated from the kernel may still have
1885 * cache contents, so we set them to (CPU, CPU) always.
1886 * 2. Written by CPU (using pwrite)
1887 * The pwrite function calls set_domain (CPU, CPU) and
1888 * this function does nothing (as nothing changes)
1890 * This function asserts that the object is not
1891 * currently in any GPU-based read or write domains
1893 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
1894 * As write_domain is zero, this function adds in the
1895 * current read domains (CPU+COMMAND, 0).
1896 * flush_domains is set to CPU.
1897 * invalidate_domains is set to COMMAND
1898 * clflush is run to get data out of the CPU caches
1899 * then i915_dev_set_domain calls i915_gem_flush to
1900 * emit an MI_FLUSH and drm_agp_chipset_flush
1901 * 5. Unmapped from GTT
1902 * i915_gem_object_unbind calls set_domain (CPU, CPU)
1903 * flush_domains and invalidate_domains end up both zero
1904 * so no flushing/invalidating happens
1908 * Case 2: The shared render buffer
1912 * 3. Read/written by GPU
1913 * 4. set_domain to (CPU,CPU)
1914 * 5. Read/written by CPU
1915 * 6. Read/written by GPU
1918 * Same as last example, (CPU, CPU)
1920 * Nothing changes (assertions find that it is not in the GPU)
1921 * 3. Read/written by GPU
1922 * execbuffer calls set_domain (RENDER, RENDER)
1923 * flush_domains gets CPU
1924 * invalidate_domains gets GPU
1926 * MI_FLUSH and drm_agp_chipset_flush
1927 * 4. set_domain (CPU, CPU)
1928 * flush_domains gets GPU
1929 * invalidate_domains gets CPU
1930 * wait_rendering (obj) to make sure all drawing is complete.
1931 * This will include an MI_FLUSH to get the data from GPU
1933 * clflush (obj) to invalidate the CPU cache
1934 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
1935 * 5. Read/written by CPU
1936 * cache lines are loaded and dirtied
1937 * 6. Read written by GPU
1938 * Same as last GPU access
1940 * Case 3: The constant buffer
1945 * 4. Updated (written) by CPU again
1954 * flush_domains = CPU
1955 * invalidate_domains = RENDER
1958 * drm_agp_chipset_flush
1959 * 4. Updated (written) by CPU again
1961 * flush_domains = 0 (no previous write domain)
1962 * invalidate_domains = 0 (no new read domains)
1965 * flush_domains = CPU
1966 * invalidate_domains = RENDER
1969 * drm_agp_chipset_flush
1972 i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
1973 uint32_t read_domains,
1974 uint32_t write_domain)
1976 struct drm_device *dev = obj->dev;
1977 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1978 uint32_t invalidate_domains = 0;
1979 uint32_t flush_domains = 0;
1981 BUG_ON(read_domains & I915_GEM_DOMAIN_CPU);
1982 BUG_ON(write_domain == I915_GEM_DOMAIN_CPU);
1985 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
1987 obj->read_domains, read_domains,
1988 obj->write_domain, write_domain);
1991 * If the object isn't moving to a new write domain,
1992 * let the object stay in multiple read domains
1994 if (write_domain == 0)
1995 read_domains |= obj->read_domains;
1997 obj_priv->dirty = 1;
2000 * Flush the current write domain if
2001 * the new read domains don't match. Invalidate
2002 * any read domains which differ from the old
2005 if (obj->write_domain && obj->write_domain != read_domains) {
2006 flush_domains |= obj->write_domain;
2007 invalidate_domains |= read_domains & ~obj->write_domain;
2010 * Invalidate any read caches which may have
2011 * stale data. That is, any new read domains.
2013 invalidate_domains |= read_domains & ~obj->read_domains;
2014 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2016 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2017 __func__, flush_domains, invalidate_domains);
2019 i915_gem_clflush_object(obj);
2022 if ((write_domain | flush_domains) != 0)
2023 obj->write_domain = write_domain;
2024 obj->read_domains = read_domains;
2026 dev->invalidate_domains |= invalidate_domains;
2027 dev->flush_domains |= flush_domains;
2029 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2031 obj->read_domains, obj->write_domain,
2032 dev->invalidate_domains, dev->flush_domains);
2037 * Moves the object from a partially CPU read to a full one.
2039 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2040 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2043 i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2045 struct drm_device *dev = obj->dev;
2046 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2048 if (!obj_priv->page_cpu_valid)
2051 /* If we're partially in the CPU read domain, finish moving it in.
2053 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2056 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2057 if (obj_priv->page_cpu_valid[i])
2059 drm_clflush_pages(obj_priv->page_list + i, 1);
2061 drm_agp_chipset_flush(dev);
2064 /* Free the page_cpu_valid mappings which are now stale, whether
2065 * or not we've got I915_GEM_DOMAIN_CPU.
2067 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
2069 obj_priv->page_cpu_valid = NULL;
2073 * Set the CPU read domain on a range of the object.
2075 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2076 * not entirely valid. The page_cpu_valid member of the object flags which
2077 * pages have been flushed, and will be respected by
2078 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2079 * of the whole object.
2081 * This function returns when the move is complete, including waiting on
2085 i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2086 uint64_t offset, uint64_t size)
2088 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2091 if (offset == 0 && size == obj->size)
2092 return i915_gem_object_set_to_cpu_domain(obj, 0);
2094 i915_gem_object_flush_gpu_write_domain(obj);
2095 /* Wait on any GPU rendering and flushing to occur. */
2096 ret = i915_gem_object_wait_rendering(obj);
2099 i915_gem_object_flush_gtt_write_domain(obj);
2101 /* If we're already fully in the CPU read domain, we're done. */
2102 if (obj_priv->page_cpu_valid == NULL &&
2103 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
2106 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2107 * newly adding I915_GEM_DOMAIN_CPU
2109 if (obj_priv->page_cpu_valid == NULL) {
2110 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
2112 if (obj_priv->page_cpu_valid == NULL)
2114 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2115 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
2117 /* Flush the cache on any pages that are still invalid from the CPU's
2120 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2122 if (obj_priv->page_cpu_valid[i])
2125 drm_clflush_pages(obj_priv->page_list + i, 1);
2127 obj_priv->page_cpu_valid[i] = 1;
2130 /* It should now be out of any other write domains, and we can update
2131 * the domain values for our changes.
2133 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2135 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2141 * Pin an object to the GTT and evaluate the relocations landing in it.
2144 i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2145 struct drm_file *file_priv,
2146 struct drm_i915_gem_exec_object *entry)
2148 struct drm_device *dev = obj->dev;
2149 drm_i915_private_t *dev_priv = dev->dev_private;
2150 struct drm_i915_gem_relocation_entry reloc;
2151 struct drm_i915_gem_relocation_entry __user *relocs;
2152 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2154 void __iomem *reloc_page;
2156 /* Choose the GTT offset for our buffer and put it there. */
2157 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2161 entry->offset = obj_priv->gtt_offset;
2163 relocs = (struct drm_i915_gem_relocation_entry __user *)
2164 (uintptr_t) entry->relocs_ptr;
2165 /* Apply the relocations, using the GTT aperture to avoid cache
2166 * flushing requirements.
2168 for (i = 0; i < entry->relocation_count; i++) {
2169 struct drm_gem_object *target_obj;
2170 struct drm_i915_gem_object *target_obj_priv;
2171 uint32_t reloc_val, reloc_offset;
2172 uint32_t __iomem *reloc_entry;
2174 ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
2176 i915_gem_object_unpin(obj);
2180 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
2181 reloc.target_handle);
2182 if (target_obj == NULL) {
2183 i915_gem_object_unpin(obj);
2186 target_obj_priv = target_obj->driver_private;
2188 /* The target buffer should have appeared before us in the
2189 * exec_object list, so it should have a GTT space bound by now.
2191 if (target_obj_priv->gtt_space == NULL) {
2192 DRM_ERROR("No GTT space found for object %d\n",
2193 reloc.target_handle);
2194 drm_gem_object_unreference(target_obj);
2195 i915_gem_object_unpin(obj);
2199 if (reloc.offset > obj->size - 4) {
2200 DRM_ERROR("Relocation beyond object bounds: "
2201 "obj %p target %d offset %d size %d.\n",
2202 obj, reloc.target_handle,
2203 (int) reloc.offset, (int) obj->size);
2204 drm_gem_object_unreference(target_obj);
2205 i915_gem_object_unpin(obj);
2208 if (reloc.offset & 3) {
2209 DRM_ERROR("Relocation not 4-byte aligned: "
2210 "obj %p target %d offset %d.\n",
2211 obj, reloc.target_handle,
2212 (int) reloc.offset);
2213 drm_gem_object_unreference(target_obj);
2214 i915_gem_object_unpin(obj);
2218 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
2219 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
2220 DRM_ERROR("reloc with read/write CPU domains: "
2221 "obj %p target %d offset %d "
2222 "read %08x write %08x",
2223 obj, reloc.target_handle,
2226 reloc.write_domain);
2230 if (reloc.write_domain && target_obj->pending_write_domain &&
2231 reloc.write_domain != target_obj->pending_write_domain) {
2232 DRM_ERROR("Write domain conflict: "
2233 "obj %p target %d offset %d "
2234 "new %08x old %08x\n",
2235 obj, reloc.target_handle,
2238 target_obj->pending_write_domain);
2239 drm_gem_object_unreference(target_obj);
2240 i915_gem_object_unpin(obj);
2245 DRM_INFO("%s: obj %p offset %08x target %d "
2246 "read %08x write %08x gtt %08x "
2247 "presumed %08x delta %08x\n",
2251 (int) reloc.target_handle,
2252 (int) reloc.read_domains,
2253 (int) reloc.write_domain,
2254 (int) target_obj_priv->gtt_offset,
2255 (int) reloc.presumed_offset,
2259 target_obj->pending_read_domains |= reloc.read_domains;
2260 target_obj->pending_write_domain |= reloc.write_domain;
2262 /* If the relocation already has the right value in it, no
2263 * more work needs to be done.
2265 if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
2266 drm_gem_object_unreference(target_obj);
2270 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
2272 drm_gem_object_unreference(target_obj);
2273 i915_gem_object_unpin(obj);
2277 /* Map the page containing the relocation we're going to
2280 reloc_offset = obj_priv->gtt_offset + reloc.offset;
2281 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
2284 reloc_entry = (uint32_t __iomem *)(reloc_page +
2285 (reloc_offset & (PAGE_SIZE - 1)));
2286 reloc_val = target_obj_priv->gtt_offset + reloc.delta;
2289 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
2290 obj, (unsigned int) reloc.offset,
2291 readl(reloc_entry), reloc_val);
2293 writel(reloc_val, reloc_entry);
2294 io_mapping_unmap_atomic(reloc_page);
2296 /* Write the updated presumed offset for this entry back out
2299 reloc.presumed_offset = target_obj_priv->gtt_offset;
2300 ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
2302 drm_gem_object_unreference(target_obj);
2303 i915_gem_object_unpin(obj);
2307 drm_gem_object_unreference(target_obj);
2312 i915_gem_dump_object(obj, 128, __func__, ~0);
2317 /** Dispatch a batchbuffer to the ring
2320 i915_dispatch_gem_execbuffer(struct drm_device *dev,
2321 struct drm_i915_gem_execbuffer *exec,
2322 uint64_t exec_offset)
2324 drm_i915_private_t *dev_priv = dev->dev_private;
2325 struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
2326 (uintptr_t) exec->cliprects_ptr;
2327 int nbox = exec->num_cliprects;
2329 uint32_t exec_start, exec_len;
2332 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
2333 exec_len = (uint32_t) exec->batch_len;
2335 if ((exec_start | exec_len) & 0x7) {
2336 DRM_ERROR("alignment\n");
2343 count = nbox ? nbox : 1;
2345 for (i = 0; i < count; i++) {
2347 int ret = i915_emit_box(dev, boxes, i,
2348 exec->DR1, exec->DR4);
2353 if (IS_I830(dev) || IS_845G(dev)) {
2355 OUT_RING(MI_BATCH_BUFFER);
2356 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2357 OUT_RING(exec_start + exec_len - 4);
2362 if (IS_I965G(dev)) {
2363 OUT_RING(MI_BATCH_BUFFER_START |
2365 MI_BATCH_NON_SECURE_I965);
2366 OUT_RING(exec_start);
2368 OUT_RING(MI_BATCH_BUFFER_START |
2370 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2376 /* XXX breadcrumb */
2380 /* Throttle our rendering by waiting until the ring has completed our requests
2381 * emitted over 20 msec ago.
2383 * This should get us reasonable parallelism between CPU and GPU but also
2384 * relatively low latency when blocking on a particular request to finish.
2387 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
2389 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2393 mutex_lock(&dev->struct_mutex);
2394 seqno = i915_file_priv->mm.last_gem_throttle_seqno;
2395 i915_file_priv->mm.last_gem_throttle_seqno =
2396 i915_file_priv->mm.last_gem_seqno;
2398 ret = i915_wait_request(dev, seqno);
2399 mutex_unlock(&dev->struct_mutex);
2404 i915_gem_execbuffer(struct drm_device *dev, void *data,
2405 struct drm_file *file_priv)
2407 drm_i915_private_t *dev_priv = dev->dev_private;
2408 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2409 struct drm_i915_gem_execbuffer *args = data;
2410 struct drm_i915_gem_exec_object *exec_list = NULL;
2411 struct drm_gem_object **object_list = NULL;
2412 struct drm_gem_object *batch_obj;
2413 int ret, i, pinned = 0;
2414 uint64_t exec_offset;
2415 uint32_t seqno, flush_domains;
2419 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
2420 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
2423 if (args->buffer_count < 1) {
2424 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
2427 /* Copy in the exec list from userland */
2428 exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
2430 object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
2432 if (exec_list == NULL || object_list == NULL) {
2433 DRM_ERROR("Failed to allocate exec or object list "
2435 args->buffer_count);
2439 ret = copy_from_user(exec_list,
2440 (struct drm_i915_relocation_entry __user *)
2441 (uintptr_t) args->buffers_ptr,
2442 sizeof(*exec_list) * args->buffer_count);
2444 DRM_ERROR("copy %d exec entries failed %d\n",
2445 args->buffer_count, ret);
2449 mutex_lock(&dev->struct_mutex);
2451 i915_verify_inactive(dev, __FILE__, __LINE__);
2453 if (dev_priv->mm.wedged) {
2454 DRM_ERROR("Execbuf while wedged\n");
2455 mutex_unlock(&dev->struct_mutex);
2459 if (dev_priv->mm.suspended) {
2460 DRM_ERROR("Execbuf while VT-switched.\n");
2461 mutex_unlock(&dev->struct_mutex);
2465 /* Look up object handles */
2466 for (i = 0; i < args->buffer_count; i++) {
2467 object_list[i] = drm_gem_object_lookup(dev, file_priv,
2468 exec_list[i].handle);
2469 if (object_list[i] == NULL) {
2470 DRM_ERROR("Invalid object handle %d at index %d\n",
2471 exec_list[i].handle, i);
2477 /* Pin and relocate */
2478 for (pin_tries = 0; ; pin_tries++) {
2480 for (i = 0; i < args->buffer_count; i++) {
2481 object_list[i]->pending_read_domains = 0;
2482 object_list[i]->pending_write_domain = 0;
2483 ret = i915_gem_object_pin_and_relocate(object_list[i],
2494 /* error other than GTT full, or we've already tried again */
2495 if (ret != -ENOMEM || pin_tries >= 1) {
2496 if (ret != -ERESTARTSYS)
2497 DRM_ERROR("Failed to pin buffers %d\n", ret);
2501 /* unpin all of our buffers */
2502 for (i = 0; i < pinned; i++)
2503 i915_gem_object_unpin(object_list[i]);
2506 /* evict everyone we can from the aperture */
2507 ret = i915_gem_evict_everything(dev);
2512 /* Set the pending read domains for the batch buffer to COMMAND */
2513 batch_obj = object_list[args->buffer_count-1];
2514 batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
2515 batch_obj->pending_write_domain = 0;
2517 i915_verify_inactive(dev, __FILE__, __LINE__);
2519 /* Zero the global flush/invalidate flags. These
2520 * will be modified as new domains are computed
2523 dev->invalidate_domains = 0;
2524 dev->flush_domains = 0;
2526 for (i = 0; i < args->buffer_count; i++) {
2527 struct drm_gem_object *obj = object_list[i];
2529 /* Compute new gpu domains and update invalidate/flush */
2530 i915_gem_object_set_to_gpu_domain(obj,
2531 obj->pending_read_domains,
2532 obj->pending_write_domain);
2535 i915_verify_inactive(dev, __FILE__, __LINE__);
2537 if (dev->invalidate_domains | dev->flush_domains) {
2539 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
2541 dev->invalidate_domains,
2542 dev->flush_domains);
2545 dev->invalidate_domains,
2546 dev->flush_domains);
2547 if (dev->flush_domains)
2548 (void)i915_add_request(dev, dev->flush_domains);
2551 i915_verify_inactive(dev, __FILE__, __LINE__);
2554 for (i = 0; i < args->buffer_count; i++) {
2555 i915_gem_object_check_coherency(object_list[i],
2556 exec_list[i].handle);
2560 exec_offset = exec_list[args->buffer_count - 1].offset;
2563 i915_gem_dump_object(object_list[args->buffer_count - 1],
2569 /* Exec the batchbuffer */
2570 ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
2572 DRM_ERROR("dispatch failed %d\n", ret);
2577 * Ensure that the commands in the batch buffer are
2578 * finished before the interrupt fires
2580 flush_domains = i915_retire_commands(dev);
2582 i915_verify_inactive(dev, __FILE__, __LINE__);
2585 * Get a seqno representing the execution of the current buffer,
2586 * which we can wait on. We would like to mitigate these interrupts,
2587 * likely by only creating seqnos occasionally (so that we have
2588 * *some* interrupts representing completion of buffers that we can
2589 * wait on when trying to clear up gtt space).
2591 seqno = i915_add_request(dev, flush_domains);
2593 i915_file_priv->mm.last_gem_seqno = seqno;
2594 for (i = 0; i < args->buffer_count; i++) {
2595 struct drm_gem_object *obj = object_list[i];
2597 i915_gem_object_move_to_active(obj, seqno);
2599 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
2603 i915_dump_lru(dev, __func__);
2606 i915_verify_inactive(dev, __FILE__, __LINE__);
2608 /* Copy the new buffer offsets back to the user's exec list. */
2609 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
2610 (uintptr_t) args->buffers_ptr,
2612 sizeof(*exec_list) * args->buffer_count);
2614 DRM_ERROR("failed to copy %d exec entries "
2615 "back to user (%d)\n",
2616 args->buffer_count, ret);
2618 for (i = 0; i < pinned; i++)
2619 i915_gem_object_unpin(object_list[i]);
2621 for (i = 0; i < args->buffer_count; i++)
2622 drm_gem_object_unreference(object_list[i]);
2624 mutex_unlock(&dev->struct_mutex);
2627 drm_free(object_list, sizeof(*object_list) * args->buffer_count,
2629 drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
2636 i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
2638 struct drm_device *dev = obj->dev;
2639 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2642 i915_verify_inactive(dev, __FILE__, __LINE__);
2643 if (obj_priv->gtt_space == NULL) {
2644 ret = i915_gem_object_bind_to_gtt(obj, alignment);
2646 if (ret != -EBUSY && ret != -ERESTARTSYS)
2647 DRM_ERROR("Failure to bind: %d", ret);
2651 obj_priv->pin_count++;
2653 /* If the object is not active and not pending a flush,
2654 * remove it from the inactive list
2656 if (obj_priv->pin_count == 1) {
2657 atomic_inc(&dev->pin_count);
2658 atomic_add(obj->size, &dev->pin_memory);
2659 if (!obj_priv->active &&
2660 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2661 I915_GEM_DOMAIN_GTT)) == 0 &&
2662 !list_empty(&obj_priv->list))
2663 list_del_init(&obj_priv->list);
2665 i915_verify_inactive(dev, __FILE__, __LINE__);
2671 i915_gem_object_unpin(struct drm_gem_object *obj)
2673 struct drm_device *dev = obj->dev;
2674 drm_i915_private_t *dev_priv = dev->dev_private;
2675 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2677 i915_verify_inactive(dev, __FILE__, __LINE__);
2678 obj_priv->pin_count--;
2679 BUG_ON(obj_priv->pin_count < 0);
2680 BUG_ON(obj_priv->gtt_space == NULL);
2682 /* If the object is no longer pinned, and is
2683 * neither active nor being flushed, then stick it on
2686 if (obj_priv->pin_count == 0) {
2687 if (!obj_priv->active &&
2688 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2689 I915_GEM_DOMAIN_GTT)) == 0)
2690 list_move_tail(&obj_priv->list,
2691 &dev_priv->mm.inactive_list);
2692 atomic_dec(&dev->pin_count);
2693 atomic_sub(obj->size, &dev->pin_memory);
2695 i915_verify_inactive(dev, __FILE__, __LINE__);
2699 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
2700 struct drm_file *file_priv)
2702 struct drm_i915_gem_pin *args = data;
2703 struct drm_gem_object *obj;
2704 struct drm_i915_gem_object *obj_priv;
2707 mutex_lock(&dev->struct_mutex);
2709 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2711 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
2713 mutex_unlock(&dev->struct_mutex);
2716 obj_priv = obj->driver_private;
2718 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
2719 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
2721 mutex_unlock(&dev->struct_mutex);
2725 obj_priv->user_pin_count++;
2726 obj_priv->pin_filp = file_priv;
2727 if (obj_priv->user_pin_count == 1) {
2728 ret = i915_gem_object_pin(obj, args->alignment);
2730 drm_gem_object_unreference(obj);
2731 mutex_unlock(&dev->struct_mutex);
2736 /* XXX - flush the CPU caches for pinned objects
2737 * as the X server doesn't manage domains yet
2739 i915_gem_object_flush_cpu_write_domain(obj);
2740 args->offset = obj_priv->gtt_offset;
2741 drm_gem_object_unreference(obj);
2742 mutex_unlock(&dev->struct_mutex);
2748 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
2749 struct drm_file *file_priv)
2751 struct drm_i915_gem_pin *args = data;
2752 struct drm_gem_object *obj;
2753 struct drm_i915_gem_object *obj_priv;
2755 mutex_lock(&dev->struct_mutex);
2757 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2759 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
2761 mutex_unlock(&dev->struct_mutex);
2765 obj_priv = obj->driver_private;
2766 if (obj_priv->pin_filp != file_priv) {
2767 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
2769 drm_gem_object_unreference(obj);
2770 mutex_unlock(&dev->struct_mutex);
2773 obj_priv->user_pin_count--;
2774 if (obj_priv->user_pin_count == 0) {
2775 obj_priv->pin_filp = NULL;
2776 i915_gem_object_unpin(obj);
2779 drm_gem_object_unreference(obj);
2780 mutex_unlock(&dev->struct_mutex);
2785 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
2786 struct drm_file *file_priv)
2788 struct drm_i915_gem_busy *args = data;
2789 struct drm_gem_object *obj;
2790 struct drm_i915_gem_object *obj_priv;
2792 mutex_lock(&dev->struct_mutex);
2793 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2795 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
2797 mutex_unlock(&dev->struct_mutex);
2801 obj_priv = obj->driver_private;
2802 /* Don't count being on the flushing list against the object being
2803 * done. Otherwise, a buffer left on the flushing list but not getting
2804 * flushed (because nobody's flushing that domain) won't ever return
2805 * unbusy and get reused by libdrm's bo cache. The other expected
2806 * consumer of this interface, OpenGL's occlusion queries, also specs
2807 * that the objects get unbusy "eventually" without any interference.
2809 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
2811 drm_gem_object_unreference(obj);
2812 mutex_unlock(&dev->struct_mutex);
2817 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
2818 struct drm_file *file_priv)
2820 return i915_gem_ring_throttle(dev, file_priv);
2823 int i915_gem_init_object(struct drm_gem_object *obj)
2825 struct drm_i915_gem_object *obj_priv;
2827 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
2828 if (obj_priv == NULL)
2832 * We've just allocated pages from the kernel,
2833 * so they've just been written by the CPU with
2834 * zeros. They'll need to be clflushed before we
2835 * use them with the GPU.
2837 obj->write_domain = I915_GEM_DOMAIN_CPU;
2838 obj->read_domains = I915_GEM_DOMAIN_CPU;
2840 obj_priv->agp_type = AGP_USER_MEMORY;
2842 obj->driver_private = obj_priv;
2843 obj_priv->obj = obj;
2844 obj_priv->fence_reg = I915_FENCE_REG_NONE;
2845 INIT_LIST_HEAD(&obj_priv->list);
2850 void i915_gem_free_object(struct drm_gem_object *obj)
2852 struct drm_device *dev = obj->dev;
2853 struct drm_gem_mm *mm = dev->mm_private;
2854 struct drm_map_list *list;
2855 struct drm_map *map;
2856 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2858 while (obj_priv->pin_count > 0)
2859 i915_gem_object_unpin(obj);
2861 i915_gem_object_unbind(obj);
2863 list = &obj->map_list;
2864 drm_ht_remove_item(&mm->offset_hash, &list->hash);
2866 if (list->file_offset_node) {
2867 drm_mm_put_block(list->file_offset_node);
2868 list->file_offset_node = NULL;
2873 drm_free(map, sizeof(*map), DRM_MEM_DRIVER);
2877 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
2878 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
2881 /** Unbinds all objects that are on the given buffer list. */
2883 i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
2885 struct drm_gem_object *obj;
2886 struct drm_i915_gem_object *obj_priv;
2889 while (!list_empty(head)) {
2890 obj_priv = list_first_entry(head,
2891 struct drm_i915_gem_object,
2893 obj = obj_priv->obj;
2895 if (obj_priv->pin_count != 0) {
2896 DRM_ERROR("Pinned object in unbind list\n");
2897 mutex_unlock(&dev->struct_mutex);
2901 ret = i915_gem_object_unbind(obj);
2903 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
2905 mutex_unlock(&dev->struct_mutex);
2915 i915_gem_idle(struct drm_device *dev)
2917 drm_i915_private_t *dev_priv = dev->dev_private;
2918 uint32_t seqno, cur_seqno, last_seqno;
2921 mutex_lock(&dev->struct_mutex);
2923 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
2924 mutex_unlock(&dev->struct_mutex);
2928 /* Hack! Don't let anybody do execbuf while we don't control the chip.
2929 * We need to replace this with a semaphore, or something.
2931 dev_priv->mm.suspended = 1;
2933 /* Cancel the retire work handler, wait for it to finish if running
2935 mutex_unlock(&dev->struct_mutex);
2936 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
2937 mutex_lock(&dev->struct_mutex);
2939 i915_kernel_lost_context(dev);
2941 /* Flush the GPU along with all non-CPU write domains
2943 i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
2944 ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2945 seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU);
2948 mutex_unlock(&dev->struct_mutex);
2952 dev_priv->mm.waiting_gem_seqno = seqno;
2956 cur_seqno = i915_get_gem_seqno(dev);
2957 if (i915_seqno_passed(cur_seqno, seqno))
2959 if (last_seqno == cur_seqno) {
2960 if (stuck++ > 100) {
2961 DRM_ERROR("hardware wedged\n");
2962 dev_priv->mm.wedged = 1;
2963 DRM_WAKEUP(&dev_priv->irq_queue);
2968 last_seqno = cur_seqno;
2970 dev_priv->mm.waiting_gem_seqno = 0;
2972 i915_gem_retire_requests(dev);
2974 if (!dev_priv->mm.wedged) {
2975 /* Active and flushing should now be empty as we've
2976 * waited for a sequence higher than any pending execbuffer
2978 WARN_ON(!list_empty(&dev_priv->mm.active_list));
2979 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
2980 /* Request should now be empty as we've also waited
2981 * for the last request in the list
2983 WARN_ON(!list_empty(&dev_priv->mm.request_list));
2986 /* Empty the active and flushing lists to inactive. If there's
2987 * anything left at this point, it means that we're wedged and
2988 * nothing good's going to happen by leaving them there. So strip
2989 * the GPU domains and just stuff them onto inactive.
2991 while (!list_empty(&dev_priv->mm.active_list)) {
2992 struct drm_i915_gem_object *obj_priv;
2994 obj_priv = list_first_entry(&dev_priv->mm.active_list,
2995 struct drm_i915_gem_object,
2997 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
2998 i915_gem_object_move_to_inactive(obj_priv->obj);
3001 while (!list_empty(&dev_priv->mm.flushing_list)) {
3002 struct drm_i915_gem_object *obj_priv;
3004 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
3005 struct drm_i915_gem_object,
3007 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3008 i915_gem_object_move_to_inactive(obj_priv->obj);
3012 /* Move all inactive buffers out of the GTT. */
3013 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
3014 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
3016 mutex_unlock(&dev->struct_mutex);
3020 i915_gem_cleanup_ringbuffer(dev);
3021 mutex_unlock(&dev->struct_mutex);
3027 i915_gem_init_hws(struct drm_device *dev)
3029 drm_i915_private_t *dev_priv = dev->dev_private;
3030 struct drm_gem_object *obj;
3031 struct drm_i915_gem_object *obj_priv;
3034 /* If we need a physical address for the status page, it's already
3035 * initialized at driver load time.
3037 if (!I915_NEED_GFX_HWS(dev))
3040 obj = drm_gem_object_alloc(dev, 4096);
3042 DRM_ERROR("Failed to allocate status page\n");
3045 obj_priv = obj->driver_private;
3046 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
3048 ret = i915_gem_object_pin(obj, 4096);
3050 drm_gem_object_unreference(obj);
3054 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
3056 dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
3057 if (dev_priv->hw_status_page == NULL) {
3058 DRM_ERROR("Failed to map status page.\n");
3059 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3060 drm_gem_object_unreference(obj);
3063 dev_priv->hws_obj = obj;
3064 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
3065 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
3066 I915_READ(HWS_PGA); /* posting read */
3067 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
3073 i915_gem_init_ringbuffer(struct drm_device *dev)
3075 drm_i915_private_t *dev_priv = dev->dev_private;
3076 struct drm_gem_object *obj;
3077 struct drm_i915_gem_object *obj_priv;
3078 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
3082 ret = i915_gem_init_hws(dev);
3086 obj = drm_gem_object_alloc(dev, 128 * 1024);
3088 DRM_ERROR("Failed to allocate ringbuffer\n");
3091 obj_priv = obj->driver_private;
3093 ret = i915_gem_object_pin(obj, 4096);
3095 drm_gem_object_unreference(obj);
3099 /* Set up the kernel mapping for the ring. */
3100 ring->Size = obj->size;
3101 ring->tail_mask = obj->size - 1;
3103 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
3104 ring->map.size = obj->size;
3106 ring->map.flags = 0;
3109 drm_core_ioremap_wc(&ring->map, dev);
3110 if (ring->map.handle == NULL) {
3111 DRM_ERROR("Failed to map ringbuffer.\n");
3112 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3113 drm_gem_object_unreference(obj);
3116 ring->ring_obj = obj;
3117 ring->virtual_start = ring->map.handle;
3119 /* Stop the ring if it's running. */
3120 I915_WRITE(PRB0_CTL, 0);
3121 I915_WRITE(PRB0_TAIL, 0);
3122 I915_WRITE(PRB0_HEAD, 0);
3124 /* Initialize the ring. */
3125 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
3126 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3128 /* G45 ring initialization fails to reset head to zero */
3130 DRM_ERROR("Ring head not reset to zero "
3131 "ctl %08x head %08x tail %08x start %08x\n",
3132 I915_READ(PRB0_CTL),
3133 I915_READ(PRB0_HEAD),
3134 I915_READ(PRB0_TAIL),
3135 I915_READ(PRB0_START));
3136 I915_WRITE(PRB0_HEAD, 0);
3138 DRM_ERROR("Ring head forced to zero "
3139 "ctl %08x head %08x tail %08x start %08x\n",
3140 I915_READ(PRB0_CTL),
3141 I915_READ(PRB0_HEAD),
3142 I915_READ(PRB0_TAIL),
3143 I915_READ(PRB0_START));
3146 I915_WRITE(PRB0_CTL,
3147 ((obj->size - 4096) & RING_NR_PAGES) |
3151 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3153 /* If the head is still not zero, the ring is dead */
3155 DRM_ERROR("Ring initialization failed "
3156 "ctl %08x head %08x tail %08x start %08x\n",
3157 I915_READ(PRB0_CTL),
3158 I915_READ(PRB0_HEAD),
3159 I915_READ(PRB0_TAIL),
3160 I915_READ(PRB0_START));
3164 /* Update our cache of the ring state */
3165 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3166 i915_kernel_lost_context(dev);
3168 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3169 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
3170 ring->space = ring->head - (ring->tail + 8);
3171 if (ring->space < 0)
3172 ring->space += ring->Size;
3179 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3181 drm_i915_private_t *dev_priv = dev->dev_private;
3183 if (dev_priv->ring.ring_obj == NULL)
3186 drm_core_ioremapfree(&dev_priv->ring.map, dev);
3188 i915_gem_object_unpin(dev_priv->ring.ring_obj);
3189 drm_gem_object_unreference(dev_priv->ring.ring_obj);
3190 dev_priv->ring.ring_obj = NULL;
3191 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3193 if (dev_priv->hws_obj != NULL) {
3194 struct drm_gem_object *obj = dev_priv->hws_obj;
3195 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3197 kunmap(obj_priv->page_list[0]);
3198 i915_gem_object_unpin(obj);
3199 drm_gem_object_unreference(obj);
3200 dev_priv->hws_obj = NULL;
3201 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3202 dev_priv->hw_status_page = NULL;
3204 /* Write high address into HWS_PGA when disabling. */
3205 I915_WRITE(HWS_PGA, 0x1ffff000);
3210 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3211 struct drm_file *file_priv)
3213 drm_i915_private_t *dev_priv = dev->dev_private;
3216 if (drm_core_check_feature(dev, DRIVER_MODESET))
3219 if (dev_priv->mm.wedged) {
3220 DRM_ERROR("Reenabling wedged hardware, good luck\n");
3221 dev_priv->mm.wedged = 0;
3224 dev_priv->mm.gtt_mapping = io_mapping_create_wc(dev->agp->base,
3225 dev->agp->agp_info.aper_size
3228 mutex_lock(&dev->struct_mutex);
3229 dev_priv->mm.suspended = 0;
3231 ret = i915_gem_init_ringbuffer(dev);
3235 BUG_ON(!list_empty(&dev_priv->mm.active_list));
3236 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3237 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3238 BUG_ON(!list_empty(&dev_priv->mm.request_list));
3239 mutex_unlock(&dev->struct_mutex);
3241 drm_irq_install(dev);
3247 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3248 struct drm_file *file_priv)
3250 drm_i915_private_t *dev_priv = dev->dev_private;
3253 if (drm_core_check_feature(dev, DRIVER_MODESET))
3256 ret = i915_gem_idle(dev);
3257 drm_irq_uninstall(dev);
3259 io_mapping_free(dev_priv->mm.gtt_mapping);
3264 i915_gem_lastclose(struct drm_device *dev)
3268 ret = i915_gem_idle(dev);
3270 DRM_ERROR("failed to idle hardware: %d\n", ret);
3274 i915_gem_load(struct drm_device *dev)
3276 drm_i915_private_t *dev_priv = dev->dev_private;
3278 INIT_LIST_HEAD(&dev_priv->mm.active_list);
3279 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3280 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
3281 INIT_LIST_HEAD(&dev_priv->mm.request_list);
3282 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3283 i915_gem_retire_work_handler);
3284 dev_priv->mm.next_gem_seqno = 1;
3286 /* Old X drivers will take 0-2 for front, back, depth buffers */
3287 dev_priv->fence_reg_start = 3;
3290 dev_priv->num_fence_regs = 16;
3292 dev_priv->num_fence_regs = 8;
3294 i915_gem_detect_bit_6_swizzle(dev);