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);
58 static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
59 struct drm_i915_gem_pwrite *args,
60 struct drm_file *file_priv);
62 int i915_gem_do_init(struct drm_device *dev, unsigned long start,
65 drm_i915_private_t *dev_priv = dev->dev_private;
68 (start & (PAGE_SIZE - 1)) != 0 ||
69 (end & (PAGE_SIZE - 1)) != 0) {
73 drm_mm_init(&dev_priv->mm.gtt_space, start,
76 dev->gtt_total = (uint32_t) (end - start);
82 i915_gem_init_ioctl(struct drm_device *dev, void *data,
83 struct drm_file *file_priv)
85 struct drm_i915_gem_init *args = data;
88 mutex_lock(&dev->struct_mutex);
89 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
90 mutex_unlock(&dev->struct_mutex);
96 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
97 struct drm_file *file_priv)
99 struct drm_i915_gem_get_aperture *args = data;
101 if (!(dev->driver->driver_features & DRIVER_GEM))
104 args->aper_size = dev->gtt_total;
105 args->aper_available_size = (args->aper_size -
106 atomic_read(&dev->pin_memory));
113 * Creates a new mm object and returns a handle to it.
116 i915_gem_create_ioctl(struct drm_device *dev, void *data,
117 struct drm_file *file_priv)
119 struct drm_i915_gem_create *args = data;
120 struct drm_gem_object *obj;
123 args->size = roundup(args->size, PAGE_SIZE);
125 /* Allocate the new object */
126 obj = drm_gem_object_alloc(dev, args->size);
130 ret = drm_gem_handle_create(file_priv, obj, &handle);
131 mutex_lock(&dev->struct_mutex);
132 drm_gem_object_handle_unreference(obj);
133 mutex_unlock(&dev->struct_mutex);
138 args->handle = handle;
144 * Reads data from the object referenced by handle.
146 * On error, the contents of *data are undefined.
149 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
150 struct drm_file *file_priv)
152 struct drm_i915_gem_pread *args = data;
153 struct drm_gem_object *obj;
154 struct drm_i915_gem_object *obj_priv;
159 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
162 obj_priv = obj->driver_private;
164 /* Bounds check source.
166 * XXX: This could use review for overflow issues...
168 if (args->offset > obj->size || args->size > obj->size ||
169 args->offset + args->size > obj->size) {
170 drm_gem_object_unreference(obj);
174 mutex_lock(&dev->struct_mutex);
176 ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
179 drm_gem_object_unreference(obj);
180 mutex_unlock(&dev->struct_mutex);
184 offset = args->offset;
186 read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
187 args->size, &offset);
188 if (read != args->size) {
189 drm_gem_object_unreference(obj);
190 mutex_unlock(&dev->struct_mutex);
197 drm_gem_object_unreference(obj);
198 mutex_unlock(&dev->struct_mutex);
203 /* This is the fast write path which cannot handle
204 * page faults in the source data
208 fast_user_write(struct io_mapping *mapping,
209 loff_t page_base, int page_offset,
210 char __user *user_data,
214 unsigned long unwritten;
216 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
217 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
219 io_mapping_unmap_atomic(vaddr_atomic);
225 /* Here's the write path which can sleep for
230 slow_user_write(struct io_mapping *mapping,
231 loff_t page_base, int page_offset,
232 char __user *user_data,
236 unsigned long unwritten;
238 vaddr = io_mapping_map_wc(mapping, page_base);
241 unwritten = __copy_from_user(vaddr + page_offset,
243 io_mapping_unmap(vaddr);
250 i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
251 struct drm_i915_gem_pwrite *args,
252 struct drm_file *file_priv)
254 struct drm_i915_gem_object *obj_priv = obj->driver_private;
255 drm_i915_private_t *dev_priv = dev->dev_private;
257 loff_t offset, page_base;
258 char __user *user_data;
259 int page_offset, page_length;
262 user_data = (char __user *) (uintptr_t) args->data_ptr;
264 if (!access_ok(VERIFY_READ, user_data, remain))
268 mutex_lock(&dev->struct_mutex);
269 ret = i915_gem_object_pin(obj, 0);
271 mutex_unlock(&dev->struct_mutex);
274 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
278 obj_priv = obj->driver_private;
279 offset = obj_priv->gtt_offset + args->offset;
283 /* Operation in this page
285 * page_base = page offset within aperture
286 * page_offset = offset within page
287 * page_length = bytes to copy for this page
289 page_base = (offset & ~(PAGE_SIZE-1));
290 page_offset = offset & (PAGE_SIZE-1);
291 page_length = remain;
292 if ((page_offset + remain) > PAGE_SIZE)
293 page_length = PAGE_SIZE - page_offset;
295 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
296 page_offset, user_data, page_length);
298 /* If we get a fault while copying data, then (presumably) our
299 * source page isn't available. In this case, use the
300 * non-atomic function
303 ret = slow_user_write (dev_priv->mm.gtt_mapping,
304 page_base, page_offset,
305 user_data, page_length);
310 remain -= page_length;
311 user_data += page_length;
312 offset += page_length;
316 i915_gem_object_unpin(obj);
317 mutex_unlock(&dev->struct_mutex);
323 i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
324 struct drm_i915_gem_pwrite *args,
325 struct drm_file *file_priv)
331 mutex_lock(&dev->struct_mutex);
333 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
335 mutex_unlock(&dev->struct_mutex);
339 offset = args->offset;
341 written = vfs_write(obj->filp,
342 (char __user *)(uintptr_t) args->data_ptr,
343 args->size, &offset);
344 if (written != args->size) {
345 mutex_unlock(&dev->struct_mutex);
352 mutex_unlock(&dev->struct_mutex);
358 * Writes data to the object referenced by handle.
360 * On error, the contents of the buffer that were to be modified are undefined.
363 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
364 struct drm_file *file_priv)
366 struct drm_i915_gem_pwrite *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 destination.
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 /* We can only do the GTT pwrite on untiled buffers, as otherwise
387 * it would end up going through the fenced access, and we'll get
388 * different detiling behavior between reading and writing.
389 * pread/pwrite currently are reading and writing from the CPU
390 * perspective, requiring manual detiling by the client.
392 if (obj_priv->phys_obj)
393 ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
394 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
396 ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
398 ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
402 DRM_INFO("pwrite failed %d\n", ret);
405 drm_gem_object_unreference(obj);
411 * Called when user space prepares to use an object with the CPU, either
412 * through the mmap ioctl's mapping or a GTT mapping.
415 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
416 struct drm_file *file_priv)
418 struct drm_i915_gem_set_domain *args = data;
419 struct drm_gem_object *obj;
420 uint32_t read_domains = args->read_domains;
421 uint32_t write_domain = args->write_domain;
424 if (!(dev->driver->driver_features & DRIVER_GEM))
427 /* Only handle setting domains to types used by the CPU. */
428 if (write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
431 if (read_domains & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
434 /* Having something in the write domain implies it's in the read
435 * domain, and only that read domain. Enforce that in the request.
437 if (write_domain != 0 && read_domains != write_domain)
440 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
444 mutex_lock(&dev->struct_mutex);
446 DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
447 obj, obj->size, read_domains, write_domain);
449 if (read_domains & I915_GEM_DOMAIN_GTT) {
450 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
452 /* Silently promote "you're not bound, there was nothing to do"
453 * to success, since the client was just asking us to
454 * make sure everything was done.
459 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
462 drm_gem_object_unreference(obj);
463 mutex_unlock(&dev->struct_mutex);
468 * Called when user space has done writes to this buffer
471 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
472 struct drm_file *file_priv)
474 struct drm_i915_gem_sw_finish *args = data;
475 struct drm_gem_object *obj;
476 struct drm_i915_gem_object *obj_priv;
479 if (!(dev->driver->driver_features & DRIVER_GEM))
482 mutex_lock(&dev->struct_mutex);
483 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
485 mutex_unlock(&dev->struct_mutex);
490 DRM_INFO("%s: sw_finish %d (%p %d)\n",
491 __func__, args->handle, obj, obj->size);
493 obj_priv = obj->driver_private;
495 /* Pinned buffers may be scanout, so flush the cache */
496 if (obj_priv->pin_count)
497 i915_gem_object_flush_cpu_write_domain(obj);
499 drm_gem_object_unreference(obj);
500 mutex_unlock(&dev->struct_mutex);
505 * Maps the contents of an object, returning the address it is mapped
508 * While the mapping holds a reference on the contents of the object, it doesn't
509 * imply a ref on the object itself.
512 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
513 struct drm_file *file_priv)
515 struct drm_i915_gem_mmap *args = data;
516 struct drm_gem_object *obj;
520 if (!(dev->driver->driver_features & DRIVER_GEM))
523 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
527 offset = args->offset;
529 down_write(¤t->mm->mmap_sem);
530 addr = do_mmap(obj->filp, 0, args->size,
531 PROT_READ | PROT_WRITE, MAP_SHARED,
533 up_write(¤t->mm->mmap_sem);
534 mutex_lock(&dev->struct_mutex);
535 drm_gem_object_unreference(obj);
536 mutex_unlock(&dev->struct_mutex);
537 if (IS_ERR((void *)addr))
540 args->addr_ptr = (uint64_t) addr;
546 * i915_gem_fault - fault a page into the GTT
547 * vma: VMA in question
550 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
551 * from userspace. The fault handler takes care of binding the object to
552 * the GTT (if needed), allocating and programming a fence register (again,
553 * only if needed based on whether the old reg is still valid or the object
554 * is tiled) and inserting a new PTE into the faulting process.
556 * Note that the faulting process may involve evicting existing objects
557 * from the GTT and/or fence registers to make room. So performance may
558 * suffer if the GTT working set is large or there are few fence registers
561 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
563 struct drm_gem_object *obj = vma->vm_private_data;
564 struct drm_device *dev = obj->dev;
565 struct drm_i915_private *dev_priv = dev->dev_private;
566 struct drm_i915_gem_object *obj_priv = obj->driver_private;
571 /* We don't use vmf->pgoff since that has the fake offset */
572 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
575 /* Now bind it into the GTT if needed */
576 mutex_lock(&dev->struct_mutex);
577 if (!obj_priv->gtt_space) {
578 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
580 mutex_unlock(&dev->struct_mutex);
581 return VM_FAULT_SIGBUS;
583 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
586 /* Need a new fence register? */
587 if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
588 obj_priv->tiling_mode != I915_TILING_NONE)
589 i915_gem_object_get_fence_reg(obj);
591 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
594 /* Finally, remap it using the new GTT offset */
595 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
597 mutex_unlock(&dev->struct_mutex);
605 DRM_ERROR("can't insert pfn?? fault or busy...\n");
606 return VM_FAULT_SIGBUS;
608 return VM_FAULT_NOPAGE;
613 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
614 * @obj: obj in question
616 * GEM memory mapping works by handing back to userspace a fake mmap offset
617 * it can use in a subsequent mmap(2) call. The DRM core code then looks
618 * up the object based on the offset and sets up the various memory mapping
621 * This routine allocates and attaches a fake offset for @obj.
624 i915_gem_create_mmap_offset(struct drm_gem_object *obj)
626 struct drm_device *dev = obj->dev;
627 struct drm_gem_mm *mm = dev->mm_private;
628 struct drm_i915_gem_object *obj_priv = obj->driver_private;
629 struct drm_map_list *list;
633 /* Set the object up for mmap'ing */
634 list = &obj->map_list;
635 list->map = drm_calloc(1, sizeof(struct drm_map_list),
641 map->type = _DRM_GEM;
642 map->size = obj->size;
645 /* Get a DRM GEM mmap offset allocated... */
646 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
647 obj->size / PAGE_SIZE, 0, 0);
648 if (!list->file_offset_node) {
649 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
654 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
655 obj->size / PAGE_SIZE, 0);
656 if (!list->file_offset_node) {
661 list->hash.key = list->file_offset_node->start;
662 if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
663 DRM_ERROR("failed to add to map hash\n");
667 /* By now we should be all set, any drm_mmap request on the offset
668 * below will get to our mmap & fault handler */
669 obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
674 drm_mm_put_block(list->file_offset_node);
676 drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER);
682 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
683 * @obj: object to check
685 * Return the required GTT alignment for an object, taking into account
686 * potential fence register mapping if needed.
689 i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
691 struct drm_device *dev = obj->dev;
692 struct drm_i915_gem_object *obj_priv = obj->driver_private;
696 * Minimum alignment is 4k (GTT page size), but might be greater
697 * if a fence register is needed for the object.
699 if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
703 * Previous chips need to be aligned to the size of the smallest
704 * fence register that can contain the object.
711 for (i = start; i < obj->size; i <<= 1)
718 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
720 * @data: GTT mapping ioctl data
721 * @file_priv: GEM object info
723 * Simply returns the fake offset to userspace so it can mmap it.
724 * The mmap call will end up in drm_gem_mmap(), which will set things
725 * up so we can get faults in the handler above.
727 * The fault handler will take care of binding the object into the GTT
728 * (since it may have been evicted to make room for something), allocating
729 * a fence register, and mapping the appropriate aperture address into
733 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
734 struct drm_file *file_priv)
736 struct drm_i915_gem_mmap_gtt *args = data;
737 struct drm_i915_private *dev_priv = dev->dev_private;
738 struct drm_gem_object *obj;
739 struct drm_i915_gem_object *obj_priv;
742 if (!(dev->driver->driver_features & DRIVER_GEM))
745 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
749 mutex_lock(&dev->struct_mutex);
751 obj_priv = obj->driver_private;
753 if (!obj_priv->mmap_offset) {
754 ret = i915_gem_create_mmap_offset(obj);
759 args->offset = obj_priv->mmap_offset;
761 obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
763 /* Make sure the alignment is correct for fence regs etc */
764 if (obj_priv->agp_mem &&
765 (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
766 drm_gem_object_unreference(obj);
767 mutex_unlock(&dev->struct_mutex);
772 * Pull it into the GTT so that we have a page list (makes the
773 * initial fault faster and any subsequent flushing possible).
775 if (!obj_priv->agp_mem) {
776 ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
778 drm_gem_object_unreference(obj);
779 mutex_unlock(&dev->struct_mutex);
782 list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
785 drm_gem_object_unreference(obj);
786 mutex_unlock(&dev->struct_mutex);
792 i915_gem_object_free_page_list(struct drm_gem_object *obj)
794 struct drm_i915_gem_object *obj_priv = obj->driver_private;
795 int page_count = obj->size / PAGE_SIZE;
798 if (obj_priv->page_list == NULL)
802 for (i = 0; i < page_count; i++)
803 if (obj_priv->page_list[i] != NULL) {
805 set_page_dirty(obj_priv->page_list[i]);
806 mark_page_accessed(obj_priv->page_list[i]);
807 page_cache_release(obj_priv->page_list[i]);
811 drm_free(obj_priv->page_list,
812 page_count * sizeof(struct page *),
814 obj_priv->page_list = NULL;
818 i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
820 struct drm_device *dev = obj->dev;
821 drm_i915_private_t *dev_priv = dev->dev_private;
822 struct drm_i915_gem_object *obj_priv = obj->driver_private;
824 /* Add a reference if we're newly entering the active list. */
825 if (!obj_priv->active) {
826 drm_gem_object_reference(obj);
827 obj_priv->active = 1;
829 /* Move from whatever list we were on to the tail of execution. */
830 list_move_tail(&obj_priv->list,
831 &dev_priv->mm.active_list);
832 obj_priv->last_rendering_seqno = seqno;
836 i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
838 struct drm_device *dev = obj->dev;
839 drm_i915_private_t *dev_priv = dev->dev_private;
840 struct drm_i915_gem_object *obj_priv = obj->driver_private;
842 BUG_ON(!obj_priv->active);
843 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
844 obj_priv->last_rendering_seqno = 0;
848 i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
850 struct drm_device *dev = obj->dev;
851 drm_i915_private_t *dev_priv = dev->dev_private;
852 struct drm_i915_gem_object *obj_priv = obj->driver_private;
854 i915_verify_inactive(dev, __FILE__, __LINE__);
855 if (obj_priv->pin_count != 0)
856 list_del_init(&obj_priv->list);
858 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
860 obj_priv->last_rendering_seqno = 0;
861 if (obj_priv->active) {
862 obj_priv->active = 0;
863 drm_gem_object_unreference(obj);
865 i915_verify_inactive(dev, __FILE__, __LINE__);
869 * Creates a new sequence number, emitting a write of it to the status page
870 * plus an interrupt, which will trigger i915_user_interrupt_handler.
872 * Must be called with struct_lock held.
874 * Returned sequence numbers are nonzero on success.
877 i915_add_request(struct drm_device *dev, uint32_t flush_domains)
879 drm_i915_private_t *dev_priv = dev->dev_private;
880 struct drm_i915_gem_request *request;
885 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
889 /* Grab the seqno we're going to make this request be, and bump the
890 * next (skipping 0 so it can be the reserved no-seqno value).
892 seqno = dev_priv->mm.next_gem_seqno;
893 dev_priv->mm.next_gem_seqno++;
894 if (dev_priv->mm.next_gem_seqno == 0)
895 dev_priv->mm.next_gem_seqno++;
898 OUT_RING(MI_STORE_DWORD_INDEX);
899 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
902 OUT_RING(MI_USER_INTERRUPT);
905 DRM_DEBUG("%d\n", seqno);
907 request->seqno = seqno;
908 request->emitted_jiffies = jiffies;
909 was_empty = list_empty(&dev_priv->mm.request_list);
910 list_add_tail(&request->list, &dev_priv->mm.request_list);
912 /* Associate any objects on the flushing list matching the write
913 * domain we're flushing with our flush.
915 if (flush_domains != 0) {
916 struct drm_i915_gem_object *obj_priv, *next;
918 list_for_each_entry_safe(obj_priv, next,
919 &dev_priv->mm.flushing_list, list) {
920 struct drm_gem_object *obj = obj_priv->obj;
922 if ((obj->write_domain & flush_domains) ==
924 obj->write_domain = 0;
925 i915_gem_object_move_to_active(obj, seqno);
931 if (was_empty && !dev_priv->mm.suspended)
932 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
937 * Command execution barrier
939 * Ensures that all commands in the ring are finished
940 * before signalling the CPU
943 i915_retire_commands(struct drm_device *dev)
945 drm_i915_private_t *dev_priv = dev->dev_private;
946 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
947 uint32_t flush_domains = 0;
950 /* The sampler always gets flushed on i965 (sigh) */
952 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
955 OUT_RING(0); /* noop */
957 return flush_domains;
961 * Moves buffers associated only with the given active seqno from the active
962 * to inactive list, potentially freeing them.
965 i915_gem_retire_request(struct drm_device *dev,
966 struct drm_i915_gem_request *request)
968 drm_i915_private_t *dev_priv = dev->dev_private;
970 /* Move any buffers on the active list that are no longer referenced
971 * by the ringbuffer to the flushing/inactive lists as appropriate.
973 while (!list_empty(&dev_priv->mm.active_list)) {
974 struct drm_gem_object *obj;
975 struct drm_i915_gem_object *obj_priv;
977 obj_priv = list_first_entry(&dev_priv->mm.active_list,
978 struct drm_i915_gem_object,
982 /* If the seqno being retired doesn't match the oldest in the
983 * list, then the oldest in the list must still be newer than
986 if (obj_priv->last_rendering_seqno != request->seqno)
990 DRM_INFO("%s: retire %d moves to inactive list %p\n",
991 __func__, request->seqno, obj);
994 if (obj->write_domain != 0)
995 i915_gem_object_move_to_flushing(obj);
997 i915_gem_object_move_to_inactive(obj);
1002 * Returns true if seq1 is later than seq2.
1005 i915_seqno_passed(uint32_t seq1, uint32_t seq2)
1007 return (int32_t)(seq1 - seq2) >= 0;
1011 i915_get_gem_seqno(struct drm_device *dev)
1013 drm_i915_private_t *dev_priv = dev->dev_private;
1015 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
1019 * This function clears the request list as sequence numbers are passed.
1022 i915_gem_retire_requests(struct drm_device *dev)
1024 drm_i915_private_t *dev_priv = dev->dev_private;
1027 seqno = i915_get_gem_seqno(dev);
1029 while (!list_empty(&dev_priv->mm.request_list)) {
1030 struct drm_i915_gem_request *request;
1031 uint32_t retiring_seqno;
1033 request = list_first_entry(&dev_priv->mm.request_list,
1034 struct drm_i915_gem_request,
1036 retiring_seqno = request->seqno;
1038 if (i915_seqno_passed(seqno, retiring_seqno) ||
1039 dev_priv->mm.wedged) {
1040 i915_gem_retire_request(dev, request);
1042 list_del(&request->list);
1043 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
1050 i915_gem_retire_work_handler(struct work_struct *work)
1052 drm_i915_private_t *dev_priv;
1053 struct drm_device *dev;
1055 dev_priv = container_of(work, drm_i915_private_t,
1056 mm.retire_work.work);
1057 dev = dev_priv->dev;
1059 mutex_lock(&dev->struct_mutex);
1060 i915_gem_retire_requests(dev);
1061 if (!dev_priv->mm.suspended &&
1062 !list_empty(&dev_priv->mm.request_list))
1063 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
1064 mutex_unlock(&dev->struct_mutex);
1068 * Waits for a sequence number to be signaled, and cleans up the
1069 * request and object lists appropriately for that event.
1072 i915_wait_request(struct drm_device *dev, uint32_t seqno)
1074 drm_i915_private_t *dev_priv = dev->dev_private;
1079 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
1080 dev_priv->mm.waiting_gem_seqno = seqno;
1081 i915_user_irq_get(dev);
1082 ret = wait_event_interruptible(dev_priv->irq_queue,
1083 i915_seqno_passed(i915_get_gem_seqno(dev),
1085 dev_priv->mm.wedged);
1086 i915_user_irq_put(dev);
1087 dev_priv->mm.waiting_gem_seqno = 0;
1089 if (dev_priv->mm.wedged)
1092 if (ret && ret != -ERESTARTSYS)
1093 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
1094 __func__, ret, seqno, i915_get_gem_seqno(dev));
1096 /* Directly dispatch request retiring. While we have the work queue
1097 * to handle this, the waiter on a request often wants an associated
1098 * buffer to have made it to the inactive list, and we would need
1099 * a separate wait queue to handle that.
1102 i915_gem_retire_requests(dev);
1108 i915_gem_flush(struct drm_device *dev,
1109 uint32_t invalidate_domains,
1110 uint32_t flush_domains)
1112 drm_i915_private_t *dev_priv = dev->dev_private;
1117 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
1118 invalidate_domains, flush_domains);
1121 if (flush_domains & I915_GEM_DOMAIN_CPU)
1122 drm_agp_chipset_flush(dev);
1124 if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
1125 I915_GEM_DOMAIN_GTT)) {
1127 * read/write caches:
1129 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
1130 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
1131 * also flushed at 2d versus 3d pipeline switches.
1135 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
1136 * MI_READ_FLUSH is set, and is always flushed on 965.
1138 * I915_GEM_DOMAIN_COMMAND may not exist?
1140 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
1141 * invalidated when MI_EXE_FLUSH is set.
1143 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
1144 * invalidated with every MI_FLUSH.
1148 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
1149 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
1150 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
1151 * are flushed at any MI_FLUSH.
1154 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
1155 if ((invalidate_domains|flush_domains) &
1156 I915_GEM_DOMAIN_RENDER)
1157 cmd &= ~MI_NO_WRITE_FLUSH;
1158 if (!IS_I965G(dev)) {
1160 * On the 965, the sampler cache always gets flushed
1161 * and this bit is reserved.
1163 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
1164 cmd |= MI_READ_FLUSH;
1166 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
1167 cmd |= MI_EXE_FLUSH;
1170 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
1174 OUT_RING(0); /* noop */
1180 * Ensures that all rendering to the object has completed and the object is
1181 * safe to unbind from the GTT or access from the CPU.
1184 i915_gem_object_wait_rendering(struct drm_gem_object *obj)
1186 struct drm_device *dev = obj->dev;
1187 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1190 /* This function only exists to support waiting for existing rendering,
1191 * not for emitting required flushes.
1193 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
1195 /* If there is rendering queued on the buffer being evicted, wait for
1198 if (obj_priv->active) {
1200 DRM_INFO("%s: object %p wait for seqno %08x\n",
1201 __func__, obj, obj_priv->last_rendering_seqno);
1203 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1212 * Unbinds an object from the GTT aperture.
1215 i915_gem_object_unbind(struct drm_gem_object *obj)
1217 struct drm_device *dev = obj->dev;
1218 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1223 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
1224 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
1226 if (obj_priv->gtt_space == NULL)
1229 if (obj_priv->pin_count != 0) {
1230 DRM_ERROR("Attempting to unbind pinned buffer\n");
1234 /* Move the object to the CPU domain to ensure that
1235 * any possible CPU writes while it's not in the GTT
1236 * are flushed when we go to remap it. This will
1237 * also ensure that all pending GPU writes are finished
1240 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1242 if (ret != -ERESTARTSYS)
1243 DRM_ERROR("set_domain failed: %d\n", ret);
1247 if (obj_priv->agp_mem != NULL) {
1248 drm_unbind_agp(obj_priv->agp_mem);
1249 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1250 obj_priv->agp_mem = NULL;
1253 BUG_ON(obj_priv->active);
1255 /* blow away mappings if mapped through GTT */
1256 offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT;
1257 if (dev->dev_mapping)
1258 unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1);
1260 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
1261 i915_gem_clear_fence_reg(obj);
1263 i915_gem_object_free_page_list(obj);
1265 if (obj_priv->gtt_space) {
1266 atomic_dec(&dev->gtt_count);
1267 atomic_sub(obj->size, &dev->gtt_memory);
1269 drm_mm_put_block(obj_priv->gtt_space);
1270 obj_priv->gtt_space = NULL;
1273 /* Remove ourselves from the LRU list if present. */
1274 if (!list_empty(&obj_priv->list))
1275 list_del_init(&obj_priv->list);
1281 i915_gem_evict_something(struct drm_device *dev)
1283 drm_i915_private_t *dev_priv = dev->dev_private;
1284 struct drm_gem_object *obj;
1285 struct drm_i915_gem_object *obj_priv;
1289 /* If there's an inactive buffer available now, grab it
1292 if (!list_empty(&dev_priv->mm.inactive_list)) {
1293 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1294 struct drm_i915_gem_object,
1296 obj = obj_priv->obj;
1297 BUG_ON(obj_priv->pin_count != 0);
1299 DRM_INFO("%s: evicting %p\n", __func__, obj);
1301 BUG_ON(obj_priv->active);
1303 /* Wait on the rendering and unbind the buffer. */
1304 ret = i915_gem_object_unbind(obj);
1308 /* If we didn't get anything, but the ring is still processing
1309 * things, wait for one of those things to finish and hopefully
1310 * leave us a buffer to evict.
1312 if (!list_empty(&dev_priv->mm.request_list)) {
1313 struct drm_i915_gem_request *request;
1315 request = list_first_entry(&dev_priv->mm.request_list,
1316 struct drm_i915_gem_request,
1319 ret = i915_wait_request(dev, request->seqno);
1323 /* if waiting caused an object to become inactive,
1324 * then loop around and wait for it. Otherwise, we
1325 * assume that waiting freed and unbound something,
1326 * so there should now be some space in the GTT
1328 if (!list_empty(&dev_priv->mm.inactive_list))
1333 /* If we didn't have anything on the request list but there
1334 * are buffers awaiting a flush, emit one and try again.
1335 * When we wait on it, those buffers waiting for that flush
1336 * will get moved to inactive.
1338 if (!list_empty(&dev_priv->mm.flushing_list)) {
1339 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1340 struct drm_i915_gem_object,
1342 obj = obj_priv->obj;
1347 i915_add_request(dev, obj->write_domain);
1353 DRM_ERROR("inactive empty %d request empty %d "
1354 "flushing empty %d\n",
1355 list_empty(&dev_priv->mm.inactive_list),
1356 list_empty(&dev_priv->mm.request_list),
1357 list_empty(&dev_priv->mm.flushing_list));
1358 /* If we didn't do any of the above, there's nothing to be done
1359 * and we just can't fit it in.
1367 i915_gem_evict_everything(struct drm_device *dev)
1372 ret = i915_gem_evict_something(dev);
1382 i915_gem_object_get_page_list(struct drm_gem_object *obj)
1384 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1386 struct address_space *mapping;
1387 struct inode *inode;
1391 if (obj_priv->page_list)
1394 /* Get the list of pages out of our struct file. They'll be pinned
1395 * at this point until we release them.
1397 page_count = obj->size / PAGE_SIZE;
1398 BUG_ON(obj_priv->page_list != NULL);
1399 obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
1401 if (obj_priv->page_list == NULL) {
1402 DRM_ERROR("Faled to allocate page list\n");
1406 inode = obj->filp->f_path.dentry->d_inode;
1407 mapping = inode->i_mapping;
1408 for (i = 0; i < page_count; i++) {
1409 page = read_mapping_page(mapping, i, NULL);
1411 ret = PTR_ERR(page);
1412 DRM_ERROR("read_mapping_page failed: %d\n", ret);
1413 i915_gem_object_free_page_list(obj);
1416 obj_priv->page_list[i] = page;
1421 static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
1423 struct drm_gem_object *obj = reg->obj;
1424 struct drm_device *dev = obj->dev;
1425 drm_i915_private_t *dev_priv = dev->dev_private;
1426 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1427 int regnum = obj_priv->fence_reg;
1430 val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
1432 val |= obj_priv->gtt_offset & 0xfffff000;
1433 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
1434 if (obj_priv->tiling_mode == I915_TILING_Y)
1435 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
1436 val |= I965_FENCE_REG_VALID;
1438 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
1441 static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
1443 struct drm_gem_object *obj = reg->obj;
1444 struct drm_device *dev = obj->dev;
1445 drm_i915_private_t *dev_priv = dev->dev_private;
1446 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1447 int regnum = obj_priv->fence_reg;
1451 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1452 (obj_priv->gtt_offset & (obj->size - 1))) {
1453 WARN(1, "%s: object not 1M or size aligned\n", __func__);
1457 if (obj_priv->tiling_mode == I915_TILING_Y && (IS_I945G(dev) ||
1460 pitch_val = (obj_priv->stride / 128) - 1;
1462 pitch_val = (obj_priv->stride / 512) - 1;
1464 val = obj_priv->gtt_offset;
1465 if (obj_priv->tiling_mode == I915_TILING_Y)
1466 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
1467 val |= I915_FENCE_SIZE_BITS(obj->size);
1468 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
1469 val |= I830_FENCE_REG_VALID;
1471 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
1474 static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
1476 struct drm_gem_object *obj = reg->obj;
1477 struct drm_device *dev = obj->dev;
1478 drm_i915_private_t *dev_priv = dev->dev_private;
1479 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1480 int regnum = obj_priv->fence_reg;
1484 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
1485 (obj_priv->gtt_offset & (obj->size - 1))) {
1486 WARN(1, "%s: object not 1M or size aligned\n", __func__);
1490 pitch_val = (obj_priv->stride / 128) - 1;
1492 val = obj_priv->gtt_offset;
1493 if (obj_priv->tiling_mode == I915_TILING_Y)
1494 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
1495 val |= I830_FENCE_SIZE_BITS(obj->size);
1496 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
1497 val |= I830_FENCE_REG_VALID;
1499 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
1504 * i915_gem_object_get_fence_reg - set up a fence reg for an object
1505 * @obj: object to map through a fence reg
1507 * When mapping objects through the GTT, userspace wants to be able to write
1508 * to them without having to worry about swizzling if the object is tiled.
1510 * This function walks the fence regs looking for a free one for @obj,
1511 * stealing one if it can't find any.
1513 * It then sets up the reg based on the object's properties: address, pitch
1514 * and tiling format.
1517 i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
1519 struct drm_device *dev = obj->dev;
1520 struct drm_i915_private *dev_priv = dev->dev_private;
1521 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1522 struct drm_i915_fence_reg *reg = NULL;
1525 switch (obj_priv->tiling_mode) {
1526 case I915_TILING_NONE:
1527 WARN(1, "allocating a fence for non-tiled object?\n");
1530 WARN(obj_priv->stride & (512 - 1),
1531 "object is X tiled but has non-512B pitch\n");
1534 WARN(obj_priv->stride & (128 - 1),
1535 "object is Y tiled but has non-128B pitch\n");
1539 /* First try to find a free reg */
1540 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
1541 reg = &dev_priv->fence_regs[i];
1546 /* None available, try to steal one or wait for a user to finish */
1547 if (i == dev_priv->num_fence_regs) {
1548 struct drm_i915_gem_object *old_obj_priv = NULL;
1552 /* Could try to use LRU here instead... */
1553 for (i = dev_priv->fence_reg_start;
1554 i < dev_priv->num_fence_regs; i++) {
1555 reg = &dev_priv->fence_regs[i];
1556 old_obj_priv = reg->obj->driver_private;
1557 if (!old_obj_priv->pin_count)
1562 * Now things get ugly... we have to wait for one of the
1563 * objects to finish before trying again.
1565 if (i == dev_priv->num_fence_regs) {
1566 ret = i915_gem_object_wait_rendering(reg->obj);
1568 WARN(ret, "wait_rendering failed: %d\n", ret);
1575 * Zap this virtual mapping so we can set up a fence again
1576 * for this object next time we need it.
1578 offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
1579 if (dev->dev_mapping)
1580 unmap_mapping_range(dev->dev_mapping, offset,
1582 old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
1585 obj_priv->fence_reg = i;
1589 i965_write_fence_reg(reg);
1590 else if (IS_I9XX(dev))
1591 i915_write_fence_reg(reg);
1593 i830_write_fence_reg(reg);
1597 * i915_gem_clear_fence_reg - clear out fence register info
1598 * @obj: object to clear
1600 * Zeroes out the fence register itself and clears out the associated
1601 * data structures in dev_priv and obj_priv.
1604 i915_gem_clear_fence_reg(struct drm_gem_object *obj)
1606 struct drm_device *dev = obj->dev;
1607 drm_i915_private_t *dev_priv = dev->dev_private;
1608 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1611 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
1613 I915_WRITE(FENCE_REG_830_0 + (obj_priv->fence_reg * 4), 0);
1615 dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
1616 obj_priv->fence_reg = I915_FENCE_REG_NONE;
1620 * Finds free space in the GTT aperture and binds the object there.
1623 i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
1625 struct drm_device *dev = obj->dev;
1626 drm_i915_private_t *dev_priv = dev->dev_private;
1627 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1628 struct drm_mm_node *free_space;
1629 int page_count, ret;
1631 if (dev_priv->mm.suspended)
1634 alignment = PAGE_SIZE;
1635 if (alignment & (PAGE_SIZE - 1)) {
1636 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
1641 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
1642 obj->size, alignment, 0);
1643 if (free_space != NULL) {
1644 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
1646 if (obj_priv->gtt_space != NULL) {
1647 obj_priv->gtt_space->private = obj;
1648 obj_priv->gtt_offset = obj_priv->gtt_space->start;
1651 if (obj_priv->gtt_space == NULL) {
1652 /* If the gtt is empty and we're still having trouble
1653 * fitting our object in, we're out of memory.
1656 DRM_INFO("%s: GTT full, evicting something\n", __func__);
1658 if (list_empty(&dev_priv->mm.inactive_list) &&
1659 list_empty(&dev_priv->mm.flushing_list) &&
1660 list_empty(&dev_priv->mm.active_list)) {
1661 DRM_ERROR("GTT full, but LRU list empty\n");
1665 ret = i915_gem_evict_something(dev);
1667 if (ret != -ERESTARTSYS)
1668 DRM_ERROR("Failed to evict a buffer %d\n", ret);
1675 DRM_INFO("Binding object of size %d at 0x%08x\n",
1676 obj->size, obj_priv->gtt_offset);
1678 ret = i915_gem_object_get_page_list(obj);
1680 drm_mm_put_block(obj_priv->gtt_space);
1681 obj_priv->gtt_space = NULL;
1685 page_count = obj->size / PAGE_SIZE;
1686 /* Create an AGP memory structure pointing at our pages, and bind it
1689 obj_priv->agp_mem = drm_agp_bind_pages(dev,
1690 obj_priv->page_list,
1692 obj_priv->gtt_offset,
1693 obj_priv->agp_type);
1694 if (obj_priv->agp_mem == NULL) {
1695 i915_gem_object_free_page_list(obj);
1696 drm_mm_put_block(obj_priv->gtt_space);
1697 obj_priv->gtt_space = NULL;
1700 atomic_inc(&dev->gtt_count);
1701 atomic_add(obj->size, &dev->gtt_memory);
1703 /* Assert that the object is not currently in any GPU domain. As it
1704 * wasn't in the GTT, there shouldn't be any way it could have been in
1707 BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1708 BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1714 i915_gem_clflush_object(struct drm_gem_object *obj)
1716 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1718 /* If we don't have a page list set up, then we're not pinned
1719 * to GPU, and we can ignore the cache flush because it'll happen
1720 * again at bind time.
1722 if (obj_priv->page_list == NULL)
1725 drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
1728 /** Flushes any GPU write domain for the object if it's dirty. */
1730 i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
1732 struct drm_device *dev = obj->dev;
1735 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
1738 /* Queue the GPU write cache flushing we need. */
1739 i915_gem_flush(dev, 0, obj->write_domain);
1740 seqno = i915_add_request(dev, obj->write_domain);
1741 obj->write_domain = 0;
1742 i915_gem_object_move_to_active(obj, seqno);
1745 /** Flushes the GTT write domain for the object if it's dirty. */
1747 i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
1749 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
1752 /* No actual flushing is required for the GTT write domain. Writes
1753 * to it immediately go to main memory as far as we know, so there's
1754 * no chipset flush. It also doesn't land in render cache.
1756 obj->write_domain = 0;
1759 /** Flushes the CPU write domain for the object if it's dirty. */
1761 i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
1763 struct drm_device *dev = obj->dev;
1765 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
1768 i915_gem_clflush_object(obj);
1769 drm_agp_chipset_flush(dev);
1770 obj->write_domain = 0;
1774 * Moves a single object to the GTT read, and possibly write domain.
1776 * This function returns when the move is complete, including waiting on
1780 i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
1782 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1785 /* Not valid to be called on unbound objects. */
1786 if (obj_priv->gtt_space == NULL)
1789 i915_gem_object_flush_gpu_write_domain(obj);
1790 /* Wait on any GPU rendering and flushing to occur. */
1791 ret = i915_gem_object_wait_rendering(obj);
1795 /* If we're writing through the GTT domain, then CPU and GPU caches
1796 * will need to be invalidated at next use.
1799 obj->read_domains &= I915_GEM_DOMAIN_GTT;
1801 i915_gem_object_flush_cpu_write_domain(obj);
1803 /* It should now be out of any other write domains, and we can update
1804 * the domain values for our changes.
1806 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
1807 obj->read_domains |= I915_GEM_DOMAIN_GTT;
1809 obj->write_domain = I915_GEM_DOMAIN_GTT;
1810 obj_priv->dirty = 1;
1817 * Moves a single object to the CPU read, and possibly write domain.
1819 * This function returns when the move is complete, including waiting on
1823 i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
1825 struct drm_device *dev = obj->dev;
1828 i915_gem_object_flush_gpu_write_domain(obj);
1829 /* Wait on any GPU rendering and flushing to occur. */
1830 ret = i915_gem_object_wait_rendering(obj);
1834 i915_gem_object_flush_gtt_write_domain(obj);
1836 /* If we have a partially-valid cache of the object in the CPU,
1837 * finish invalidating it and free the per-page flags.
1839 i915_gem_object_set_to_full_cpu_read_domain(obj);
1841 /* Flush the CPU cache if it's still invalid. */
1842 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
1843 i915_gem_clflush_object(obj);
1844 drm_agp_chipset_flush(dev);
1846 obj->read_domains |= I915_GEM_DOMAIN_CPU;
1849 /* It should now be out of any other write domains, and we can update
1850 * the domain values for our changes.
1852 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
1854 /* If we're writing through the CPU, then the GPU read domains will
1855 * need to be invalidated at next use.
1858 obj->read_domains &= I915_GEM_DOMAIN_CPU;
1859 obj->write_domain = I915_GEM_DOMAIN_CPU;
1866 * Set the next domain for the specified object. This
1867 * may not actually perform the necessary flushing/invaliding though,
1868 * as that may want to be batched with other set_domain operations
1870 * This is (we hope) the only really tricky part of gem. The goal
1871 * is fairly simple -- track which caches hold bits of the object
1872 * and make sure they remain coherent. A few concrete examples may
1873 * help to explain how it works. For shorthand, we use the notation
1874 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
1875 * a pair of read and write domain masks.
1877 * Case 1: the batch buffer
1883 * 5. Unmapped from GTT
1886 * Let's take these a step at a time
1889 * Pages allocated from the kernel may still have
1890 * cache contents, so we set them to (CPU, CPU) always.
1891 * 2. Written by CPU (using pwrite)
1892 * The pwrite function calls set_domain (CPU, CPU) and
1893 * this function does nothing (as nothing changes)
1895 * This function asserts that the object is not
1896 * currently in any GPU-based read or write domains
1898 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
1899 * As write_domain is zero, this function adds in the
1900 * current read domains (CPU+COMMAND, 0).
1901 * flush_domains is set to CPU.
1902 * invalidate_domains is set to COMMAND
1903 * clflush is run to get data out of the CPU caches
1904 * then i915_dev_set_domain calls i915_gem_flush to
1905 * emit an MI_FLUSH and drm_agp_chipset_flush
1906 * 5. Unmapped from GTT
1907 * i915_gem_object_unbind calls set_domain (CPU, CPU)
1908 * flush_domains and invalidate_domains end up both zero
1909 * so no flushing/invalidating happens
1913 * Case 2: The shared render buffer
1917 * 3. Read/written by GPU
1918 * 4. set_domain to (CPU,CPU)
1919 * 5. Read/written by CPU
1920 * 6. Read/written by GPU
1923 * Same as last example, (CPU, CPU)
1925 * Nothing changes (assertions find that it is not in the GPU)
1926 * 3. Read/written by GPU
1927 * execbuffer calls set_domain (RENDER, RENDER)
1928 * flush_domains gets CPU
1929 * invalidate_domains gets GPU
1931 * MI_FLUSH and drm_agp_chipset_flush
1932 * 4. set_domain (CPU, CPU)
1933 * flush_domains gets GPU
1934 * invalidate_domains gets CPU
1935 * wait_rendering (obj) to make sure all drawing is complete.
1936 * This will include an MI_FLUSH to get the data from GPU
1938 * clflush (obj) to invalidate the CPU cache
1939 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
1940 * 5. Read/written by CPU
1941 * cache lines are loaded and dirtied
1942 * 6. Read written by GPU
1943 * Same as last GPU access
1945 * Case 3: The constant buffer
1950 * 4. Updated (written) by CPU again
1959 * flush_domains = CPU
1960 * invalidate_domains = RENDER
1963 * drm_agp_chipset_flush
1964 * 4. Updated (written) by CPU again
1966 * flush_domains = 0 (no previous write domain)
1967 * invalidate_domains = 0 (no new read domains)
1970 * flush_domains = CPU
1971 * invalidate_domains = RENDER
1974 * drm_agp_chipset_flush
1977 i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
1978 uint32_t read_domains,
1979 uint32_t write_domain)
1981 struct drm_device *dev = obj->dev;
1982 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1983 uint32_t invalidate_domains = 0;
1984 uint32_t flush_domains = 0;
1986 BUG_ON(read_domains & I915_GEM_DOMAIN_CPU);
1987 BUG_ON(write_domain == I915_GEM_DOMAIN_CPU);
1990 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
1992 obj->read_domains, read_domains,
1993 obj->write_domain, write_domain);
1996 * If the object isn't moving to a new write domain,
1997 * let the object stay in multiple read domains
1999 if (write_domain == 0)
2000 read_domains |= obj->read_domains;
2002 obj_priv->dirty = 1;
2005 * Flush the current write domain if
2006 * the new read domains don't match. Invalidate
2007 * any read domains which differ from the old
2010 if (obj->write_domain && obj->write_domain != read_domains) {
2011 flush_domains |= obj->write_domain;
2012 invalidate_domains |= read_domains & ~obj->write_domain;
2015 * Invalidate any read caches which may have
2016 * stale data. That is, any new read domains.
2018 invalidate_domains |= read_domains & ~obj->read_domains;
2019 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
2021 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
2022 __func__, flush_domains, invalidate_domains);
2024 i915_gem_clflush_object(obj);
2027 if ((write_domain | flush_domains) != 0)
2028 obj->write_domain = write_domain;
2029 obj->read_domains = read_domains;
2031 dev->invalidate_domains |= invalidate_domains;
2032 dev->flush_domains |= flush_domains;
2034 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
2036 obj->read_domains, obj->write_domain,
2037 dev->invalidate_domains, dev->flush_domains);
2042 * Moves the object from a partially CPU read to a full one.
2044 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
2045 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
2048 i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
2050 struct drm_device *dev = obj->dev;
2051 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2053 if (!obj_priv->page_cpu_valid)
2056 /* If we're partially in the CPU read domain, finish moving it in.
2058 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
2061 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
2062 if (obj_priv->page_cpu_valid[i])
2064 drm_clflush_pages(obj_priv->page_list + i, 1);
2066 drm_agp_chipset_flush(dev);
2069 /* Free the page_cpu_valid mappings which are now stale, whether
2070 * or not we've got I915_GEM_DOMAIN_CPU.
2072 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
2074 obj_priv->page_cpu_valid = NULL;
2078 * Set the CPU read domain on a range of the object.
2080 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
2081 * not entirely valid. The page_cpu_valid member of the object flags which
2082 * pages have been flushed, and will be respected by
2083 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
2084 * of the whole object.
2086 * This function returns when the move is complete, including waiting on
2090 i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
2091 uint64_t offset, uint64_t size)
2093 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2096 if (offset == 0 && size == obj->size)
2097 return i915_gem_object_set_to_cpu_domain(obj, 0);
2099 i915_gem_object_flush_gpu_write_domain(obj);
2100 /* Wait on any GPU rendering and flushing to occur. */
2101 ret = i915_gem_object_wait_rendering(obj);
2104 i915_gem_object_flush_gtt_write_domain(obj);
2106 /* If we're already fully in the CPU read domain, we're done. */
2107 if (obj_priv->page_cpu_valid == NULL &&
2108 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
2111 /* Otherwise, create/clear the per-page CPU read domain flag if we're
2112 * newly adding I915_GEM_DOMAIN_CPU
2114 if (obj_priv->page_cpu_valid == NULL) {
2115 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
2117 if (obj_priv->page_cpu_valid == NULL)
2119 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
2120 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
2122 /* Flush the cache on any pages that are still invalid from the CPU's
2125 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
2127 if (obj_priv->page_cpu_valid[i])
2130 drm_clflush_pages(obj_priv->page_list + i, 1);
2132 obj_priv->page_cpu_valid[i] = 1;
2135 /* It should now be out of any other write domains, and we can update
2136 * the domain values for our changes.
2138 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
2140 obj->read_domains |= I915_GEM_DOMAIN_CPU;
2146 * Pin an object to the GTT and evaluate the relocations landing in it.
2149 i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
2150 struct drm_file *file_priv,
2151 struct drm_i915_gem_exec_object *entry)
2153 struct drm_device *dev = obj->dev;
2154 drm_i915_private_t *dev_priv = dev->dev_private;
2155 struct drm_i915_gem_relocation_entry reloc;
2156 struct drm_i915_gem_relocation_entry __user *relocs;
2157 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2159 void __iomem *reloc_page;
2161 /* Choose the GTT offset for our buffer and put it there. */
2162 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
2166 entry->offset = obj_priv->gtt_offset;
2168 relocs = (struct drm_i915_gem_relocation_entry __user *)
2169 (uintptr_t) entry->relocs_ptr;
2170 /* Apply the relocations, using the GTT aperture to avoid cache
2171 * flushing requirements.
2173 for (i = 0; i < entry->relocation_count; i++) {
2174 struct drm_gem_object *target_obj;
2175 struct drm_i915_gem_object *target_obj_priv;
2176 uint32_t reloc_val, reloc_offset;
2177 uint32_t __iomem *reloc_entry;
2179 ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
2181 i915_gem_object_unpin(obj);
2185 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
2186 reloc.target_handle);
2187 if (target_obj == NULL) {
2188 i915_gem_object_unpin(obj);
2191 target_obj_priv = target_obj->driver_private;
2193 /* The target buffer should have appeared before us in the
2194 * exec_object list, so it should have a GTT space bound by now.
2196 if (target_obj_priv->gtt_space == NULL) {
2197 DRM_ERROR("No GTT space found for object %d\n",
2198 reloc.target_handle);
2199 drm_gem_object_unreference(target_obj);
2200 i915_gem_object_unpin(obj);
2204 if (reloc.offset > obj->size - 4) {
2205 DRM_ERROR("Relocation beyond object bounds: "
2206 "obj %p target %d offset %d size %d.\n",
2207 obj, reloc.target_handle,
2208 (int) reloc.offset, (int) obj->size);
2209 drm_gem_object_unreference(target_obj);
2210 i915_gem_object_unpin(obj);
2213 if (reloc.offset & 3) {
2214 DRM_ERROR("Relocation not 4-byte aligned: "
2215 "obj %p target %d offset %d.\n",
2216 obj, reloc.target_handle,
2217 (int) reloc.offset);
2218 drm_gem_object_unreference(target_obj);
2219 i915_gem_object_unpin(obj);
2223 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
2224 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
2225 DRM_ERROR("reloc with read/write CPU domains: "
2226 "obj %p target %d offset %d "
2227 "read %08x write %08x",
2228 obj, reloc.target_handle,
2231 reloc.write_domain);
2235 if (reloc.write_domain && target_obj->pending_write_domain &&
2236 reloc.write_domain != target_obj->pending_write_domain) {
2237 DRM_ERROR("Write domain conflict: "
2238 "obj %p target %d offset %d "
2239 "new %08x old %08x\n",
2240 obj, reloc.target_handle,
2243 target_obj->pending_write_domain);
2244 drm_gem_object_unreference(target_obj);
2245 i915_gem_object_unpin(obj);
2250 DRM_INFO("%s: obj %p offset %08x target %d "
2251 "read %08x write %08x gtt %08x "
2252 "presumed %08x delta %08x\n",
2256 (int) reloc.target_handle,
2257 (int) reloc.read_domains,
2258 (int) reloc.write_domain,
2259 (int) target_obj_priv->gtt_offset,
2260 (int) reloc.presumed_offset,
2264 target_obj->pending_read_domains |= reloc.read_domains;
2265 target_obj->pending_write_domain |= reloc.write_domain;
2267 /* If the relocation already has the right value in it, no
2268 * more work needs to be done.
2270 if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
2271 drm_gem_object_unreference(target_obj);
2275 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
2277 drm_gem_object_unreference(target_obj);
2278 i915_gem_object_unpin(obj);
2282 /* Map the page containing the relocation we're going to
2285 reloc_offset = obj_priv->gtt_offset + reloc.offset;
2286 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
2289 reloc_entry = (uint32_t __iomem *)(reloc_page +
2290 (reloc_offset & (PAGE_SIZE - 1)));
2291 reloc_val = target_obj_priv->gtt_offset + reloc.delta;
2294 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
2295 obj, (unsigned int) reloc.offset,
2296 readl(reloc_entry), reloc_val);
2298 writel(reloc_val, reloc_entry);
2299 io_mapping_unmap_atomic(reloc_page);
2301 /* Write the updated presumed offset for this entry back out
2304 reloc.presumed_offset = target_obj_priv->gtt_offset;
2305 ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
2307 drm_gem_object_unreference(target_obj);
2308 i915_gem_object_unpin(obj);
2312 drm_gem_object_unreference(target_obj);
2317 i915_gem_dump_object(obj, 128, __func__, ~0);
2322 /** Dispatch a batchbuffer to the ring
2325 i915_dispatch_gem_execbuffer(struct drm_device *dev,
2326 struct drm_i915_gem_execbuffer *exec,
2327 uint64_t exec_offset)
2329 drm_i915_private_t *dev_priv = dev->dev_private;
2330 struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
2331 (uintptr_t) exec->cliprects_ptr;
2332 int nbox = exec->num_cliprects;
2334 uint32_t exec_start, exec_len;
2337 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
2338 exec_len = (uint32_t) exec->batch_len;
2340 if ((exec_start | exec_len) & 0x7) {
2341 DRM_ERROR("alignment\n");
2348 count = nbox ? nbox : 1;
2350 for (i = 0; i < count; i++) {
2352 int ret = i915_emit_box(dev, boxes, i,
2353 exec->DR1, exec->DR4);
2358 if (IS_I830(dev) || IS_845G(dev)) {
2360 OUT_RING(MI_BATCH_BUFFER);
2361 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2362 OUT_RING(exec_start + exec_len - 4);
2367 if (IS_I965G(dev)) {
2368 OUT_RING(MI_BATCH_BUFFER_START |
2370 MI_BATCH_NON_SECURE_I965);
2371 OUT_RING(exec_start);
2373 OUT_RING(MI_BATCH_BUFFER_START |
2375 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
2381 /* XXX breadcrumb */
2385 /* Throttle our rendering by waiting until the ring has completed our requests
2386 * emitted over 20 msec ago.
2388 * This should get us reasonable parallelism between CPU and GPU but also
2389 * relatively low latency when blocking on a particular request to finish.
2392 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
2394 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2398 mutex_lock(&dev->struct_mutex);
2399 seqno = i915_file_priv->mm.last_gem_throttle_seqno;
2400 i915_file_priv->mm.last_gem_throttle_seqno =
2401 i915_file_priv->mm.last_gem_seqno;
2403 ret = i915_wait_request(dev, seqno);
2404 mutex_unlock(&dev->struct_mutex);
2409 i915_gem_execbuffer(struct drm_device *dev, void *data,
2410 struct drm_file *file_priv)
2412 drm_i915_private_t *dev_priv = dev->dev_private;
2413 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2414 struct drm_i915_gem_execbuffer *args = data;
2415 struct drm_i915_gem_exec_object *exec_list = NULL;
2416 struct drm_gem_object **object_list = NULL;
2417 struct drm_gem_object *batch_obj;
2418 int ret, i, pinned = 0;
2419 uint64_t exec_offset;
2420 uint32_t seqno, flush_domains;
2424 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
2425 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
2428 if (args->buffer_count < 1) {
2429 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
2432 /* Copy in the exec list from userland */
2433 exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
2435 object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
2437 if (exec_list == NULL || object_list == NULL) {
2438 DRM_ERROR("Failed to allocate exec or object list "
2440 args->buffer_count);
2444 ret = copy_from_user(exec_list,
2445 (struct drm_i915_relocation_entry __user *)
2446 (uintptr_t) args->buffers_ptr,
2447 sizeof(*exec_list) * args->buffer_count);
2449 DRM_ERROR("copy %d exec entries failed %d\n",
2450 args->buffer_count, ret);
2454 mutex_lock(&dev->struct_mutex);
2456 i915_verify_inactive(dev, __FILE__, __LINE__);
2458 if (dev_priv->mm.wedged) {
2459 DRM_ERROR("Execbuf while wedged\n");
2460 mutex_unlock(&dev->struct_mutex);
2464 if (dev_priv->mm.suspended) {
2465 DRM_ERROR("Execbuf while VT-switched.\n");
2466 mutex_unlock(&dev->struct_mutex);
2470 /* Look up object handles */
2471 for (i = 0; i < args->buffer_count; i++) {
2472 object_list[i] = drm_gem_object_lookup(dev, file_priv,
2473 exec_list[i].handle);
2474 if (object_list[i] == NULL) {
2475 DRM_ERROR("Invalid object handle %d at index %d\n",
2476 exec_list[i].handle, i);
2482 /* Pin and relocate */
2483 for (pin_tries = 0; ; pin_tries++) {
2485 for (i = 0; i < args->buffer_count; i++) {
2486 object_list[i]->pending_read_domains = 0;
2487 object_list[i]->pending_write_domain = 0;
2488 ret = i915_gem_object_pin_and_relocate(object_list[i],
2499 /* error other than GTT full, or we've already tried again */
2500 if (ret != -ENOMEM || pin_tries >= 1) {
2501 if (ret != -ERESTARTSYS)
2502 DRM_ERROR("Failed to pin buffers %d\n", ret);
2506 /* unpin all of our buffers */
2507 for (i = 0; i < pinned; i++)
2508 i915_gem_object_unpin(object_list[i]);
2511 /* evict everyone we can from the aperture */
2512 ret = i915_gem_evict_everything(dev);
2517 /* Set the pending read domains for the batch buffer to COMMAND */
2518 batch_obj = object_list[args->buffer_count-1];
2519 batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
2520 batch_obj->pending_write_domain = 0;
2522 i915_verify_inactive(dev, __FILE__, __LINE__);
2524 /* Zero the global flush/invalidate flags. These
2525 * will be modified as new domains are computed
2528 dev->invalidate_domains = 0;
2529 dev->flush_domains = 0;
2531 for (i = 0; i < args->buffer_count; i++) {
2532 struct drm_gem_object *obj = object_list[i];
2534 /* Compute new gpu domains and update invalidate/flush */
2535 i915_gem_object_set_to_gpu_domain(obj,
2536 obj->pending_read_domains,
2537 obj->pending_write_domain);
2540 i915_verify_inactive(dev, __FILE__, __LINE__);
2542 if (dev->invalidate_domains | dev->flush_domains) {
2544 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
2546 dev->invalidate_domains,
2547 dev->flush_domains);
2550 dev->invalidate_domains,
2551 dev->flush_domains);
2552 if (dev->flush_domains)
2553 (void)i915_add_request(dev, dev->flush_domains);
2556 i915_verify_inactive(dev, __FILE__, __LINE__);
2559 for (i = 0; i < args->buffer_count; i++) {
2560 i915_gem_object_check_coherency(object_list[i],
2561 exec_list[i].handle);
2565 exec_offset = exec_list[args->buffer_count - 1].offset;
2568 i915_gem_dump_object(object_list[args->buffer_count - 1],
2574 /* Exec the batchbuffer */
2575 ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
2577 DRM_ERROR("dispatch failed %d\n", ret);
2582 * Ensure that the commands in the batch buffer are
2583 * finished before the interrupt fires
2585 flush_domains = i915_retire_commands(dev);
2587 i915_verify_inactive(dev, __FILE__, __LINE__);
2590 * Get a seqno representing the execution of the current buffer,
2591 * which we can wait on. We would like to mitigate these interrupts,
2592 * likely by only creating seqnos occasionally (so that we have
2593 * *some* interrupts representing completion of buffers that we can
2594 * wait on when trying to clear up gtt space).
2596 seqno = i915_add_request(dev, flush_domains);
2598 i915_file_priv->mm.last_gem_seqno = seqno;
2599 for (i = 0; i < args->buffer_count; i++) {
2600 struct drm_gem_object *obj = object_list[i];
2602 i915_gem_object_move_to_active(obj, seqno);
2604 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
2608 i915_dump_lru(dev, __func__);
2611 i915_verify_inactive(dev, __FILE__, __LINE__);
2613 /* Copy the new buffer offsets back to the user's exec list. */
2614 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
2615 (uintptr_t) args->buffers_ptr,
2617 sizeof(*exec_list) * args->buffer_count);
2619 DRM_ERROR("failed to copy %d exec entries "
2620 "back to user (%d)\n",
2621 args->buffer_count, ret);
2623 for (i = 0; i < pinned; i++)
2624 i915_gem_object_unpin(object_list[i]);
2626 for (i = 0; i < args->buffer_count; i++)
2627 drm_gem_object_unreference(object_list[i]);
2629 mutex_unlock(&dev->struct_mutex);
2632 drm_free(object_list, sizeof(*object_list) * args->buffer_count,
2634 drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
2641 i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
2643 struct drm_device *dev = obj->dev;
2644 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2647 i915_verify_inactive(dev, __FILE__, __LINE__);
2648 if (obj_priv->gtt_space == NULL) {
2649 ret = i915_gem_object_bind_to_gtt(obj, alignment);
2651 if (ret != -EBUSY && ret != -ERESTARTSYS)
2652 DRM_ERROR("Failure to bind: %d", ret);
2656 obj_priv->pin_count++;
2658 /* If the object is not active and not pending a flush,
2659 * remove it from the inactive list
2661 if (obj_priv->pin_count == 1) {
2662 atomic_inc(&dev->pin_count);
2663 atomic_add(obj->size, &dev->pin_memory);
2664 if (!obj_priv->active &&
2665 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2666 I915_GEM_DOMAIN_GTT)) == 0 &&
2667 !list_empty(&obj_priv->list))
2668 list_del_init(&obj_priv->list);
2670 i915_verify_inactive(dev, __FILE__, __LINE__);
2676 i915_gem_object_unpin(struct drm_gem_object *obj)
2678 struct drm_device *dev = obj->dev;
2679 drm_i915_private_t *dev_priv = dev->dev_private;
2680 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2682 i915_verify_inactive(dev, __FILE__, __LINE__);
2683 obj_priv->pin_count--;
2684 BUG_ON(obj_priv->pin_count < 0);
2685 BUG_ON(obj_priv->gtt_space == NULL);
2687 /* If the object is no longer pinned, and is
2688 * neither active nor being flushed, then stick it on
2691 if (obj_priv->pin_count == 0) {
2692 if (!obj_priv->active &&
2693 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2694 I915_GEM_DOMAIN_GTT)) == 0)
2695 list_move_tail(&obj_priv->list,
2696 &dev_priv->mm.inactive_list);
2697 atomic_dec(&dev->pin_count);
2698 atomic_sub(obj->size, &dev->pin_memory);
2700 i915_verify_inactive(dev, __FILE__, __LINE__);
2704 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
2705 struct drm_file *file_priv)
2707 struct drm_i915_gem_pin *args = data;
2708 struct drm_gem_object *obj;
2709 struct drm_i915_gem_object *obj_priv;
2712 mutex_lock(&dev->struct_mutex);
2714 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2716 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
2718 mutex_unlock(&dev->struct_mutex);
2721 obj_priv = obj->driver_private;
2723 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
2724 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
2726 mutex_unlock(&dev->struct_mutex);
2730 obj_priv->user_pin_count++;
2731 obj_priv->pin_filp = file_priv;
2732 if (obj_priv->user_pin_count == 1) {
2733 ret = i915_gem_object_pin(obj, args->alignment);
2735 drm_gem_object_unreference(obj);
2736 mutex_unlock(&dev->struct_mutex);
2741 /* XXX - flush the CPU caches for pinned objects
2742 * as the X server doesn't manage domains yet
2744 i915_gem_object_flush_cpu_write_domain(obj);
2745 args->offset = obj_priv->gtt_offset;
2746 drm_gem_object_unreference(obj);
2747 mutex_unlock(&dev->struct_mutex);
2753 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
2754 struct drm_file *file_priv)
2756 struct drm_i915_gem_pin *args = data;
2757 struct drm_gem_object *obj;
2758 struct drm_i915_gem_object *obj_priv;
2760 mutex_lock(&dev->struct_mutex);
2762 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2764 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
2766 mutex_unlock(&dev->struct_mutex);
2770 obj_priv = obj->driver_private;
2771 if (obj_priv->pin_filp != file_priv) {
2772 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
2774 drm_gem_object_unreference(obj);
2775 mutex_unlock(&dev->struct_mutex);
2778 obj_priv->user_pin_count--;
2779 if (obj_priv->user_pin_count == 0) {
2780 obj_priv->pin_filp = NULL;
2781 i915_gem_object_unpin(obj);
2784 drm_gem_object_unreference(obj);
2785 mutex_unlock(&dev->struct_mutex);
2790 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
2791 struct drm_file *file_priv)
2793 struct drm_i915_gem_busy *args = data;
2794 struct drm_gem_object *obj;
2795 struct drm_i915_gem_object *obj_priv;
2797 mutex_lock(&dev->struct_mutex);
2798 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2800 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
2802 mutex_unlock(&dev->struct_mutex);
2806 obj_priv = obj->driver_private;
2807 /* Don't count being on the flushing list against the object being
2808 * done. Otherwise, a buffer left on the flushing list but not getting
2809 * flushed (because nobody's flushing that domain) won't ever return
2810 * unbusy and get reused by libdrm's bo cache. The other expected
2811 * consumer of this interface, OpenGL's occlusion queries, also specs
2812 * that the objects get unbusy "eventually" without any interference.
2814 args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
2816 drm_gem_object_unreference(obj);
2817 mutex_unlock(&dev->struct_mutex);
2822 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
2823 struct drm_file *file_priv)
2825 return i915_gem_ring_throttle(dev, file_priv);
2828 int i915_gem_init_object(struct drm_gem_object *obj)
2830 struct drm_i915_gem_object *obj_priv;
2832 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
2833 if (obj_priv == NULL)
2837 * We've just allocated pages from the kernel,
2838 * so they've just been written by the CPU with
2839 * zeros. They'll need to be clflushed before we
2840 * use them with the GPU.
2842 obj->write_domain = I915_GEM_DOMAIN_CPU;
2843 obj->read_domains = I915_GEM_DOMAIN_CPU;
2845 obj_priv->agp_type = AGP_USER_MEMORY;
2847 obj->driver_private = obj_priv;
2848 obj_priv->obj = obj;
2849 obj_priv->fence_reg = I915_FENCE_REG_NONE;
2850 INIT_LIST_HEAD(&obj_priv->list);
2855 void i915_gem_free_object(struct drm_gem_object *obj)
2857 struct drm_device *dev = obj->dev;
2858 struct drm_gem_mm *mm = dev->mm_private;
2859 struct drm_map_list *list;
2860 struct drm_map *map;
2861 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2863 while (obj_priv->pin_count > 0)
2864 i915_gem_object_unpin(obj);
2866 if (obj_priv->phys_obj)
2867 i915_gem_detach_phys_object(dev, obj);
2869 i915_gem_object_unbind(obj);
2871 list = &obj->map_list;
2872 drm_ht_remove_item(&mm->offset_hash, &list->hash);
2874 if (list->file_offset_node) {
2875 drm_mm_put_block(list->file_offset_node);
2876 list->file_offset_node = NULL;
2881 drm_free(map, sizeof(*map), DRM_MEM_DRIVER);
2885 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
2886 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
2889 /** Unbinds all objects that are on the given buffer list. */
2891 i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
2893 struct drm_gem_object *obj;
2894 struct drm_i915_gem_object *obj_priv;
2897 while (!list_empty(head)) {
2898 obj_priv = list_first_entry(head,
2899 struct drm_i915_gem_object,
2901 obj = obj_priv->obj;
2903 if (obj_priv->pin_count != 0) {
2904 DRM_ERROR("Pinned object in unbind list\n");
2905 mutex_unlock(&dev->struct_mutex);
2909 ret = i915_gem_object_unbind(obj);
2911 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
2913 mutex_unlock(&dev->struct_mutex);
2923 i915_gem_idle(struct drm_device *dev)
2925 drm_i915_private_t *dev_priv = dev->dev_private;
2926 uint32_t seqno, cur_seqno, last_seqno;
2929 mutex_lock(&dev->struct_mutex);
2931 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
2932 mutex_unlock(&dev->struct_mutex);
2936 /* Hack! Don't let anybody do execbuf while we don't control the chip.
2937 * We need to replace this with a semaphore, or something.
2939 dev_priv->mm.suspended = 1;
2941 /* Cancel the retire work handler, wait for it to finish if running
2943 mutex_unlock(&dev->struct_mutex);
2944 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
2945 mutex_lock(&dev->struct_mutex);
2947 i915_kernel_lost_context(dev);
2949 /* Flush the GPU along with all non-CPU write domains
2951 i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
2952 ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2953 seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU);
2956 mutex_unlock(&dev->struct_mutex);
2960 dev_priv->mm.waiting_gem_seqno = seqno;
2964 cur_seqno = i915_get_gem_seqno(dev);
2965 if (i915_seqno_passed(cur_seqno, seqno))
2967 if (last_seqno == cur_seqno) {
2968 if (stuck++ > 100) {
2969 DRM_ERROR("hardware wedged\n");
2970 dev_priv->mm.wedged = 1;
2971 DRM_WAKEUP(&dev_priv->irq_queue);
2976 last_seqno = cur_seqno;
2978 dev_priv->mm.waiting_gem_seqno = 0;
2980 i915_gem_retire_requests(dev);
2982 if (!dev_priv->mm.wedged) {
2983 /* Active and flushing should now be empty as we've
2984 * waited for a sequence higher than any pending execbuffer
2986 WARN_ON(!list_empty(&dev_priv->mm.active_list));
2987 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
2988 /* Request should now be empty as we've also waited
2989 * for the last request in the list
2991 WARN_ON(!list_empty(&dev_priv->mm.request_list));
2994 /* Empty the active and flushing lists to inactive. If there's
2995 * anything left at this point, it means that we're wedged and
2996 * nothing good's going to happen by leaving them there. So strip
2997 * the GPU domains and just stuff them onto inactive.
2999 while (!list_empty(&dev_priv->mm.active_list)) {
3000 struct drm_i915_gem_object *obj_priv;
3002 obj_priv = list_first_entry(&dev_priv->mm.active_list,
3003 struct drm_i915_gem_object,
3005 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3006 i915_gem_object_move_to_inactive(obj_priv->obj);
3009 while (!list_empty(&dev_priv->mm.flushing_list)) {
3010 struct drm_i915_gem_object *obj_priv;
3012 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
3013 struct drm_i915_gem_object,
3015 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
3016 i915_gem_object_move_to_inactive(obj_priv->obj);
3020 /* Move all inactive buffers out of the GTT. */
3021 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
3022 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
3024 mutex_unlock(&dev->struct_mutex);
3028 i915_gem_cleanup_ringbuffer(dev);
3029 mutex_unlock(&dev->struct_mutex);
3035 i915_gem_init_hws(struct drm_device *dev)
3037 drm_i915_private_t *dev_priv = dev->dev_private;
3038 struct drm_gem_object *obj;
3039 struct drm_i915_gem_object *obj_priv;
3042 /* If we need a physical address for the status page, it's already
3043 * initialized at driver load time.
3045 if (!I915_NEED_GFX_HWS(dev))
3048 obj = drm_gem_object_alloc(dev, 4096);
3050 DRM_ERROR("Failed to allocate status page\n");
3053 obj_priv = obj->driver_private;
3054 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
3056 ret = i915_gem_object_pin(obj, 4096);
3058 drm_gem_object_unreference(obj);
3062 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
3064 dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
3065 if (dev_priv->hw_status_page == NULL) {
3066 DRM_ERROR("Failed to map status page.\n");
3067 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3068 drm_gem_object_unreference(obj);
3071 dev_priv->hws_obj = obj;
3072 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
3073 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
3074 I915_READ(HWS_PGA); /* posting read */
3075 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
3081 i915_gem_init_ringbuffer(struct drm_device *dev)
3083 drm_i915_private_t *dev_priv = dev->dev_private;
3084 struct drm_gem_object *obj;
3085 struct drm_i915_gem_object *obj_priv;
3086 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
3090 ret = i915_gem_init_hws(dev);
3094 obj = drm_gem_object_alloc(dev, 128 * 1024);
3096 DRM_ERROR("Failed to allocate ringbuffer\n");
3099 obj_priv = obj->driver_private;
3101 ret = i915_gem_object_pin(obj, 4096);
3103 drm_gem_object_unreference(obj);
3107 /* Set up the kernel mapping for the ring. */
3108 ring->Size = obj->size;
3109 ring->tail_mask = obj->size - 1;
3111 ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
3112 ring->map.size = obj->size;
3114 ring->map.flags = 0;
3117 drm_core_ioremap_wc(&ring->map, dev);
3118 if (ring->map.handle == NULL) {
3119 DRM_ERROR("Failed to map ringbuffer.\n");
3120 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3121 drm_gem_object_unreference(obj);
3124 ring->ring_obj = obj;
3125 ring->virtual_start = ring->map.handle;
3127 /* Stop the ring if it's running. */
3128 I915_WRITE(PRB0_CTL, 0);
3129 I915_WRITE(PRB0_TAIL, 0);
3130 I915_WRITE(PRB0_HEAD, 0);
3132 /* Initialize the ring. */
3133 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
3134 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3136 /* G45 ring initialization fails to reset head to zero */
3138 DRM_ERROR("Ring head not reset 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));
3144 I915_WRITE(PRB0_HEAD, 0);
3146 DRM_ERROR("Ring head forced to zero "
3147 "ctl %08x head %08x tail %08x start %08x\n",
3148 I915_READ(PRB0_CTL),
3149 I915_READ(PRB0_HEAD),
3150 I915_READ(PRB0_TAIL),
3151 I915_READ(PRB0_START));
3154 I915_WRITE(PRB0_CTL,
3155 ((obj->size - 4096) & RING_NR_PAGES) |
3159 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3161 /* If the head is still not zero, the ring is dead */
3163 DRM_ERROR("Ring initialization failed "
3164 "ctl %08x head %08x tail %08x start %08x\n",
3165 I915_READ(PRB0_CTL),
3166 I915_READ(PRB0_HEAD),
3167 I915_READ(PRB0_TAIL),
3168 I915_READ(PRB0_START));
3172 /* Update our cache of the ring state */
3173 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3174 i915_kernel_lost_context(dev);
3176 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
3177 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
3178 ring->space = ring->head - (ring->tail + 8);
3179 if (ring->space < 0)
3180 ring->space += ring->Size;
3187 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3189 drm_i915_private_t *dev_priv = dev->dev_private;
3191 if (dev_priv->ring.ring_obj == NULL)
3194 drm_core_ioremapfree(&dev_priv->ring.map, dev);
3196 i915_gem_object_unpin(dev_priv->ring.ring_obj);
3197 drm_gem_object_unreference(dev_priv->ring.ring_obj);
3198 dev_priv->ring.ring_obj = NULL;
3199 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
3201 if (dev_priv->hws_obj != NULL) {
3202 struct drm_gem_object *obj = dev_priv->hws_obj;
3203 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3205 kunmap(obj_priv->page_list[0]);
3206 i915_gem_object_unpin(obj);
3207 drm_gem_object_unreference(obj);
3208 dev_priv->hws_obj = NULL;
3209 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
3210 dev_priv->hw_status_page = NULL;
3212 /* Write high address into HWS_PGA when disabling. */
3213 I915_WRITE(HWS_PGA, 0x1ffff000);
3218 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3219 struct drm_file *file_priv)
3221 drm_i915_private_t *dev_priv = dev->dev_private;
3224 if (drm_core_check_feature(dev, DRIVER_MODESET))
3227 if (dev_priv->mm.wedged) {
3228 DRM_ERROR("Reenabling wedged hardware, good luck\n");
3229 dev_priv->mm.wedged = 0;
3232 dev_priv->mm.gtt_mapping = io_mapping_create_wc(dev->agp->base,
3233 dev->agp->agp_info.aper_size
3236 mutex_lock(&dev->struct_mutex);
3237 dev_priv->mm.suspended = 0;
3239 ret = i915_gem_init_ringbuffer(dev);
3243 BUG_ON(!list_empty(&dev_priv->mm.active_list));
3244 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3245 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3246 BUG_ON(!list_empty(&dev_priv->mm.request_list));
3247 mutex_unlock(&dev->struct_mutex);
3249 drm_irq_install(dev);
3255 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3256 struct drm_file *file_priv)
3258 drm_i915_private_t *dev_priv = dev->dev_private;
3261 if (drm_core_check_feature(dev, DRIVER_MODESET))
3264 ret = i915_gem_idle(dev);
3265 drm_irq_uninstall(dev);
3267 io_mapping_free(dev_priv->mm.gtt_mapping);
3272 i915_gem_lastclose(struct drm_device *dev)
3276 ret = i915_gem_idle(dev);
3278 DRM_ERROR("failed to idle hardware: %d\n", ret);
3282 i915_gem_load(struct drm_device *dev)
3284 drm_i915_private_t *dev_priv = dev->dev_private;
3286 INIT_LIST_HEAD(&dev_priv->mm.active_list);
3287 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3288 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
3289 INIT_LIST_HEAD(&dev_priv->mm.request_list);
3290 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3291 i915_gem_retire_work_handler);
3292 dev_priv->mm.next_gem_seqno = 1;
3294 /* Old X drivers will take 0-2 for front, back, depth buffers */
3295 dev_priv->fence_reg_start = 3;
3298 dev_priv->num_fence_regs = 16;
3300 dev_priv->num_fence_regs = 8;
3302 i915_gem_detect_bit_6_swizzle(dev);
3306 * Create a physically contiguous memory object for this object
3307 * e.g. for cursor + overlay regs
3309 int i915_gem_init_phys_object(struct drm_device *dev,
3312 drm_i915_private_t *dev_priv = dev->dev_private;
3313 struct drm_i915_gem_phys_object *phys_obj;
3316 if (dev_priv->mm.phys_objs[id - 1] || !size)
3319 phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
3325 phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
3326 if (!phys_obj->handle) {
3331 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3334 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3338 drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
3342 void i915_gem_free_phys_object(struct drm_device *dev, int id)
3344 drm_i915_private_t *dev_priv = dev->dev_private;
3345 struct drm_i915_gem_phys_object *phys_obj;
3347 if (!dev_priv->mm.phys_objs[id - 1])
3350 phys_obj = dev_priv->mm.phys_objs[id - 1];
3351 if (phys_obj->cur_obj) {
3352 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3356 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3358 drm_pci_free(dev, phys_obj->handle);
3360 dev_priv->mm.phys_objs[id - 1] = NULL;
3363 void i915_gem_free_all_phys_object(struct drm_device *dev)
3367 for (i = 0; i < I915_MAX_PHYS_OBJECT; i++)
3368 i915_gem_free_phys_object(dev, i);
3371 void i915_gem_detach_phys_object(struct drm_device *dev,
3372 struct drm_gem_object *obj)
3374 struct drm_i915_gem_object *obj_priv;
3379 obj_priv = obj->driver_private;
3380 if (!obj_priv->phys_obj)
3383 ret = i915_gem_object_get_page_list(obj);
3387 page_count = obj->size / PAGE_SIZE;
3389 for (i = 0; i < page_count; i++) {
3390 char *dst = kmap_atomic(obj_priv->page_list[i], KM_USER0);
3391 char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
3393 memcpy(dst, src, PAGE_SIZE);
3394 kunmap_atomic(dst, KM_USER0);
3396 drm_clflush_pages(obj_priv->page_list, page_count);
3397 drm_agp_chipset_flush(dev);
3399 obj_priv->phys_obj->cur_obj = NULL;
3400 obj_priv->phys_obj = NULL;
3404 i915_gem_attach_phys_object(struct drm_device *dev,
3405 struct drm_gem_object *obj, int id)
3407 drm_i915_private_t *dev_priv = dev->dev_private;
3408 struct drm_i915_gem_object *obj_priv;
3413 if (id > I915_MAX_PHYS_OBJECT)
3416 obj_priv = obj->driver_private;
3418 if (obj_priv->phys_obj) {
3419 if (obj_priv->phys_obj->id == id)
3421 i915_gem_detach_phys_object(dev, obj);
3425 /* create a new object */
3426 if (!dev_priv->mm.phys_objs[id - 1]) {
3427 ret = i915_gem_init_phys_object(dev, id,
3430 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
3435 /* bind to the object */
3436 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
3437 obj_priv->phys_obj->cur_obj = obj;
3439 ret = i915_gem_object_get_page_list(obj);
3441 DRM_ERROR("failed to get page list\n");
3445 page_count = obj->size / PAGE_SIZE;
3447 for (i = 0; i < page_count; i++) {
3448 char *src = kmap_atomic(obj_priv->page_list[i], KM_USER0);
3449 char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
3451 memcpy(dst, src, PAGE_SIZE);
3452 kunmap_atomic(src, KM_USER0);
3461 i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
3462 struct drm_i915_gem_pwrite *args,
3463 struct drm_file *file_priv)
3465 struct drm_i915_gem_object *obj_priv = obj->driver_private;
3468 char __user *user_data;
3470 user_data = (char __user *) (uintptr_t) args->data_ptr;
3471 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
3473 DRM_ERROR("obj_addr %p, %lld\n", obj_addr, args->size);
3474 ret = copy_from_user(obj_addr, user_data, args->size);
3478 drm_agp_chipset_flush(dev);