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>
35 i915_gem_object_set_domain(struct drm_gem_object *obj,
36 uint32_t read_domains,
37 uint32_t write_domain);
39 i915_gem_object_set_domain_range(struct drm_gem_object *obj,
42 uint32_t read_domains,
43 uint32_t write_domain);
45 i915_gem_set_domain(struct drm_gem_object *obj,
46 struct drm_file *file_priv,
47 uint32_t read_domains,
48 uint32_t write_domain);
49 static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
50 static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
51 static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
54 i915_gem_cleanup_ringbuffer(struct drm_device *dev);
57 i915_gem_init_ioctl(struct drm_device *dev, void *data,
58 struct drm_file *file_priv)
60 drm_i915_private_t *dev_priv = dev->dev_private;
61 struct drm_i915_gem_init *args = data;
63 mutex_lock(&dev->struct_mutex);
65 if (args->gtt_start >= args->gtt_end ||
66 (args->gtt_start & (PAGE_SIZE - 1)) != 0 ||
67 (args->gtt_end & (PAGE_SIZE - 1)) != 0) {
68 mutex_unlock(&dev->struct_mutex);
72 drm_mm_init(&dev_priv->mm.gtt_space, args->gtt_start,
73 args->gtt_end - args->gtt_start);
75 dev->gtt_total = (uint32_t) (args->gtt_end - args->gtt_start);
77 mutex_unlock(&dev->struct_mutex);
84 * Creates a new mm object and returns a handle to it.
87 i915_gem_create_ioctl(struct drm_device *dev, void *data,
88 struct drm_file *file_priv)
90 struct drm_i915_gem_create *args = data;
91 struct drm_gem_object *obj;
94 args->size = roundup(args->size, PAGE_SIZE);
96 /* Allocate the new object */
97 obj = drm_gem_object_alloc(dev, args->size);
101 ret = drm_gem_handle_create(file_priv, obj, &handle);
102 mutex_lock(&dev->struct_mutex);
103 drm_gem_object_handle_unreference(obj);
104 mutex_unlock(&dev->struct_mutex);
109 args->handle = handle;
115 * Reads data from the object referenced by handle.
117 * On error, the contents of *data are undefined.
120 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
121 struct drm_file *file_priv)
123 struct drm_i915_gem_pread *args = data;
124 struct drm_gem_object *obj;
125 struct drm_i915_gem_object *obj_priv;
130 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
133 obj_priv = obj->driver_private;
135 /* Bounds check source.
137 * XXX: This could use review for overflow issues...
139 if (args->offset > obj->size || args->size > obj->size ||
140 args->offset + args->size > obj->size) {
141 drm_gem_object_unreference(obj);
145 mutex_lock(&dev->struct_mutex);
147 ret = i915_gem_object_set_domain_range(obj, args->offset, args->size,
148 I915_GEM_DOMAIN_CPU, 0);
150 drm_gem_object_unreference(obj);
151 mutex_unlock(&dev->struct_mutex);
155 offset = args->offset;
157 read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
158 args->size, &offset);
159 if (read != args->size) {
160 drm_gem_object_unreference(obj);
161 mutex_unlock(&dev->struct_mutex);
168 drm_gem_object_unreference(obj);
169 mutex_unlock(&dev->struct_mutex);
175 i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
176 struct drm_i915_gem_pwrite *args,
177 struct drm_file *file_priv)
179 struct drm_i915_gem_object *obj_priv = obj->driver_private;
182 char __user *user_data;
188 unsigned long unwritten;
190 user_data = (char __user *) (uintptr_t) args->data_ptr;
192 if (!access_ok(VERIFY_READ, user_data, remain))
196 mutex_lock(&dev->struct_mutex);
197 ret = i915_gem_object_pin(obj, 0);
199 mutex_unlock(&dev->struct_mutex);
202 ret = i915_gem_set_domain(obj, file_priv,
203 I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
207 obj_priv = obj->driver_private;
208 offset = obj_priv->gtt_offset + args->offset;
212 /* Operation in this page
215 * o = offset within page
218 i = offset >> PAGE_SHIFT;
219 o = offset & (PAGE_SIZE-1);
221 if ((o + l) > PAGE_SIZE)
224 pfn = (dev->agp->base >> PAGE_SHIFT) + i;
226 #ifdef CONFIG_HIGHMEM
227 /* This is a workaround for the low performance of iounmap
228 * (approximate 10% cpu cost on normal 3D workloads).
229 * kmap_atomic on HIGHMEM kernels happens to let us map card
230 * memory without taking IPIs. When the vmap rework lands
231 * we should be able to dump this hack.
233 vaddr_atomic = kmap_atomic_pfn(pfn, KM_USER0);
235 DRM_INFO("pwrite i %d o %d l %d pfn %ld vaddr %p\n",
236 i, o, l, pfn, vaddr_atomic);
238 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + o,
240 kunmap_atomic(vaddr_atomic, KM_USER0);
243 #endif /* CONFIG_HIGHMEM */
245 vaddr = ioremap_wc(pfn << PAGE_SHIFT, PAGE_SIZE);
247 DRM_INFO("pwrite slow i %d o %d l %d "
248 "pfn %ld vaddr %p\n",
249 i, o, l, pfn, vaddr);
255 unwritten = __copy_from_user(vaddr + o, user_data, l);
257 DRM_INFO("unwritten %ld\n", unwritten);
270 #if WATCH_PWRITE && 1
271 i915_gem_clflush_object(obj);
272 i915_gem_dump_object(obj, args->offset + args->size, __func__, ~0);
273 i915_gem_clflush_object(obj);
277 i915_gem_object_unpin(obj);
278 mutex_unlock(&dev->struct_mutex);
284 i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
285 struct drm_i915_gem_pwrite *args,
286 struct drm_file *file_priv)
292 mutex_lock(&dev->struct_mutex);
294 ret = i915_gem_set_domain(obj, file_priv,
295 I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
297 mutex_unlock(&dev->struct_mutex);
301 offset = args->offset;
303 written = vfs_write(obj->filp,
304 (char __user *)(uintptr_t) args->data_ptr,
305 args->size, &offset);
306 if (written != args->size) {
307 mutex_unlock(&dev->struct_mutex);
314 mutex_unlock(&dev->struct_mutex);
320 * Writes data to the object referenced by handle.
322 * On error, the contents of the buffer that were to be modified are undefined.
325 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
326 struct drm_file *file_priv)
328 struct drm_i915_gem_pwrite *args = data;
329 struct drm_gem_object *obj;
330 struct drm_i915_gem_object *obj_priv;
333 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
336 obj_priv = obj->driver_private;
338 /* Bounds check destination.
340 * XXX: This could use review for overflow issues...
342 if (args->offset > obj->size || args->size > obj->size ||
343 args->offset + args->size > obj->size) {
344 drm_gem_object_unreference(obj);
348 /* We can only do the GTT pwrite on untiled buffers, as otherwise
349 * it would end up going through the fenced access, and we'll get
350 * different detiling behavior between reading and writing.
351 * pread/pwrite currently are reading and writing from the CPU
352 * perspective, requiring manual detiling by the client.
354 if (obj_priv->tiling_mode == I915_TILING_NONE &&
356 ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
358 ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
362 DRM_INFO("pwrite failed %d\n", ret);
365 drm_gem_object_unreference(obj);
371 * Called when user space prepares to use an object
374 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
375 struct drm_file *file_priv)
377 struct drm_i915_gem_set_domain *args = data;
378 struct drm_gem_object *obj;
381 if (!(dev->driver->driver_features & DRIVER_GEM))
384 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
388 mutex_lock(&dev->struct_mutex);
390 DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
391 obj, obj->size, args->read_domains, args->write_domain);
393 ret = i915_gem_set_domain(obj, file_priv,
394 args->read_domains, args->write_domain);
395 drm_gem_object_unreference(obj);
396 mutex_unlock(&dev->struct_mutex);
401 * Called when user space has done writes to this buffer
404 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
405 struct drm_file *file_priv)
407 struct drm_i915_gem_sw_finish *args = data;
408 struct drm_gem_object *obj;
409 struct drm_i915_gem_object *obj_priv;
412 if (!(dev->driver->driver_features & DRIVER_GEM))
415 mutex_lock(&dev->struct_mutex);
416 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
418 mutex_unlock(&dev->struct_mutex);
423 DRM_INFO("%s: sw_finish %d (%p %d)\n",
424 __func__, args->handle, obj, obj->size);
426 obj_priv = obj->driver_private;
428 /* Pinned buffers may be scanout, so flush the cache */
429 if ((obj->write_domain & I915_GEM_DOMAIN_CPU) && obj_priv->pin_count) {
430 i915_gem_clflush_object(obj);
431 drm_agp_chipset_flush(dev);
433 drm_gem_object_unreference(obj);
434 mutex_unlock(&dev->struct_mutex);
439 * Maps the contents of an object, returning the address it is mapped
442 * While the mapping holds a reference on the contents of the object, it doesn't
443 * imply a ref on the object itself.
446 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
447 struct drm_file *file_priv)
449 struct drm_i915_gem_mmap *args = data;
450 struct drm_gem_object *obj;
454 if (!(dev->driver->driver_features & DRIVER_GEM))
457 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
461 offset = args->offset;
463 down_write(¤t->mm->mmap_sem);
464 addr = do_mmap(obj->filp, 0, args->size,
465 PROT_READ | PROT_WRITE, MAP_SHARED,
467 up_write(¤t->mm->mmap_sem);
468 mutex_lock(&dev->struct_mutex);
469 drm_gem_object_unreference(obj);
470 mutex_unlock(&dev->struct_mutex);
471 if (IS_ERR((void *)addr))
474 args->addr_ptr = (uint64_t) addr;
480 i915_gem_object_free_page_list(struct drm_gem_object *obj)
482 struct drm_i915_gem_object *obj_priv = obj->driver_private;
483 int page_count = obj->size / PAGE_SIZE;
486 if (obj_priv->page_list == NULL)
490 for (i = 0; i < page_count; i++)
491 if (obj_priv->page_list[i] != NULL) {
493 set_page_dirty(obj_priv->page_list[i]);
494 mark_page_accessed(obj_priv->page_list[i]);
495 page_cache_release(obj_priv->page_list[i]);
499 drm_free(obj_priv->page_list,
500 page_count * sizeof(struct page *),
502 obj_priv->page_list = NULL;
506 i915_gem_object_move_to_active(struct drm_gem_object *obj)
508 struct drm_device *dev = obj->dev;
509 drm_i915_private_t *dev_priv = dev->dev_private;
510 struct drm_i915_gem_object *obj_priv = obj->driver_private;
512 /* Add a reference if we're newly entering the active list. */
513 if (!obj_priv->active) {
514 drm_gem_object_reference(obj);
515 obj_priv->active = 1;
517 /* Move from whatever list we were on to the tail of execution. */
518 list_move_tail(&obj_priv->list,
519 &dev_priv->mm.active_list);
524 i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
526 struct drm_device *dev = obj->dev;
527 drm_i915_private_t *dev_priv = dev->dev_private;
528 struct drm_i915_gem_object *obj_priv = obj->driver_private;
530 i915_verify_inactive(dev, __FILE__, __LINE__);
531 if (obj_priv->pin_count != 0)
532 list_del_init(&obj_priv->list);
534 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
536 if (obj_priv->active) {
537 obj_priv->active = 0;
538 drm_gem_object_unreference(obj);
540 i915_verify_inactive(dev, __FILE__, __LINE__);
544 * Creates a new sequence number, emitting a write of it to the status page
545 * plus an interrupt, which will trigger i915_user_interrupt_handler.
547 * Must be called with struct_lock held.
549 * Returned sequence numbers are nonzero on success.
552 i915_add_request(struct drm_device *dev, uint32_t flush_domains)
554 drm_i915_private_t *dev_priv = dev->dev_private;
555 struct drm_i915_gem_request *request;
560 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
564 /* Grab the seqno we're going to make this request be, and bump the
565 * next (skipping 0 so it can be the reserved no-seqno value).
567 seqno = dev_priv->mm.next_gem_seqno;
568 dev_priv->mm.next_gem_seqno++;
569 if (dev_priv->mm.next_gem_seqno == 0)
570 dev_priv->mm.next_gem_seqno++;
573 OUT_RING(MI_STORE_DWORD_INDEX);
574 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
577 OUT_RING(MI_USER_INTERRUPT);
580 DRM_DEBUG("%d\n", seqno);
582 request->seqno = seqno;
583 request->emitted_jiffies = jiffies;
584 request->flush_domains = flush_domains;
585 was_empty = list_empty(&dev_priv->mm.request_list);
586 list_add_tail(&request->list, &dev_priv->mm.request_list);
588 if (was_empty && !dev_priv->mm.suspended)
589 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
594 * Command execution barrier
596 * Ensures that all commands in the ring are finished
597 * before signalling the CPU
600 i915_retire_commands(struct drm_device *dev)
602 drm_i915_private_t *dev_priv = dev->dev_private;
603 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
604 uint32_t flush_domains = 0;
607 /* The sampler always gets flushed on i965 (sigh) */
609 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
612 OUT_RING(0); /* noop */
614 return flush_domains;
618 * Moves buffers associated only with the given active seqno from the active
619 * to inactive list, potentially freeing them.
622 i915_gem_retire_request(struct drm_device *dev,
623 struct drm_i915_gem_request *request)
625 drm_i915_private_t *dev_priv = dev->dev_private;
627 /* Move any buffers on the active list that are no longer referenced
628 * by the ringbuffer to the flushing/inactive lists as appropriate.
630 while (!list_empty(&dev_priv->mm.active_list)) {
631 struct drm_gem_object *obj;
632 struct drm_i915_gem_object *obj_priv;
634 obj_priv = list_first_entry(&dev_priv->mm.active_list,
635 struct drm_i915_gem_object,
639 /* If the seqno being retired doesn't match the oldest in the
640 * list, then the oldest in the list must still be newer than
643 if (obj_priv->last_rendering_seqno != request->seqno)
646 DRM_INFO("%s: retire %d moves to inactive list %p\n",
647 __func__, request->seqno, obj);
650 if (obj->write_domain != 0) {
651 list_move_tail(&obj_priv->list,
652 &dev_priv->mm.flushing_list);
654 i915_gem_object_move_to_inactive(obj);
658 if (request->flush_domains != 0) {
659 struct drm_i915_gem_object *obj_priv, *next;
661 /* Clear the write domain and activity from any buffers
662 * that are just waiting for a flush matching the one retired.
664 list_for_each_entry_safe(obj_priv, next,
665 &dev_priv->mm.flushing_list, list) {
666 struct drm_gem_object *obj = obj_priv->obj;
668 if (obj->write_domain & request->flush_domains) {
669 obj->write_domain = 0;
670 i915_gem_object_move_to_inactive(obj);
678 * Returns true if seq1 is later than seq2.
681 i915_seqno_passed(uint32_t seq1, uint32_t seq2)
683 return (int32_t)(seq1 - seq2) >= 0;
687 i915_get_gem_seqno(struct drm_device *dev)
689 drm_i915_private_t *dev_priv = dev->dev_private;
691 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
695 * This function clears the request list as sequence numbers are passed.
698 i915_gem_retire_requests(struct drm_device *dev)
700 drm_i915_private_t *dev_priv = dev->dev_private;
703 seqno = i915_get_gem_seqno(dev);
705 while (!list_empty(&dev_priv->mm.request_list)) {
706 struct drm_i915_gem_request *request;
707 uint32_t retiring_seqno;
709 request = list_first_entry(&dev_priv->mm.request_list,
710 struct drm_i915_gem_request,
712 retiring_seqno = request->seqno;
714 if (i915_seqno_passed(seqno, retiring_seqno) ||
715 dev_priv->mm.wedged) {
716 i915_gem_retire_request(dev, request);
718 list_del(&request->list);
719 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
726 i915_gem_retire_work_handler(struct work_struct *work)
728 drm_i915_private_t *dev_priv;
729 struct drm_device *dev;
731 dev_priv = container_of(work, drm_i915_private_t,
732 mm.retire_work.work);
735 mutex_lock(&dev->struct_mutex);
736 i915_gem_retire_requests(dev);
737 if (!dev_priv->mm.suspended &&
738 !list_empty(&dev_priv->mm.request_list))
739 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
740 mutex_unlock(&dev->struct_mutex);
744 * Waits for a sequence number to be signaled, and cleans up the
745 * request and object lists appropriately for that event.
748 i915_wait_request(struct drm_device *dev, uint32_t seqno)
750 drm_i915_private_t *dev_priv = dev->dev_private;
755 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
756 dev_priv->mm.waiting_gem_seqno = seqno;
757 i915_user_irq_get(dev);
758 ret = wait_event_interruptible(dev_priv->irq_queue,
759 i915_seqno_passed(i915_get_gem_seqno(dev),
761 dev_priv->mm.wedged);
762 i915_user_irq_put(dev);
763 dev_priv->mm.waiting_gem_seqno = 0;
765 if (dev_priv->mm.wedged)
768 if (ret && ret != -ERESTARTSYS)
769 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
770 __func__, ret, seqno, i915_get_gem_seqno(dev));
772 /* Directly dispatch request retiring. While we have the work queue
773 * to handle this, the waiter on a request often wants an associated
774 * buffer to have made it to the inactive list, and we would need
775 * a separate wait queue to handle that.
778 i915_gem_retire_requests(dev);
784 i915_gem_flush(struct drm_device *dev,
785 uint32_t invalidate_domains,
786 uint32_t flush_domains)
788 drm_i915_private_t *dev_priv = dev->dev_private;
793 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
794 invalidate_domains, flush_domains);
797 if (flush_domains & I915_GEM_DOMAIN_CPU)
798 drm_agp_chipset_flush(dev);
800 if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
801 I915_GEM_DOMAIN_GTT)) {
805 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
806 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
807 * also flushed at 2d versus 3d pipeline switches.
811 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
812 * MI_READ_FLUSH is set, and is always flushed on 965.
814 * I915_GEM_DOMAIN_COMMAND may not exist?
816 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
817 * invalidated when MI_EXE_FLUSH is set.
819 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
820 * invalidated with every MI_FLUSH.
824 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
825 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
826 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
827 * are flushed at any MI_FLUSH.
830 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
831 if ((invalidate_domains|flush_domains) &
832 I915_GEM_DOMAIN_RENDER)
833 cmd &= ~MI_NO_WRITE_FLUSH;
834 if (!IS_I965G(dev)) {
836 * On the 965, the sampler cache always gets flushed
837 * and this bit is reserved.
839 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
840 cmd |= MI_READ_FLUSH;
842 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
846 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
850 OUT_RING(0); /* noop */
856 * Ensures that all rendering to the object has completed and the object is
857 * safe to unbind from the GTT or access from the CPU.
860 i915_gem_object_wait_rendering(struct drm_gem_object *obj)
862 struct drm_device *dev = obj->dev;
863 struct drm_i915_gem_object *obj_priv = obj->driver_private;
866 /* If there are writes queued to the buffer, flush and
867 * create a new seqno to wait for.
869 if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) {
870 uint32_t write_domain = obj->write_domain;
872 DRM_INFO("%s: flushing object %p from write domain %08x\n",
873 __func__, obj, write_domain);
875 i915_gem_flush(dev, 0, write_domain);
877 i915_gem_object_move_to_active(obj);
878 obj_priv->last_rendering_seqno = i915_add_request(dev,
880 BUG_ON(obj_priv->last_rendering_seqno == 0);
882 DRM_INFO("%s: flush moves to exec list %p\n", __func__, obj);
886 /* If there is rendering queued on the buffer being evicted, wait for
889 if (obj_priv->active) {
891 DRM_INFO("%s: object %p wait for seqno %08x\n",
892 __func__, obj, obj_priv->last_rendering_seqno);
894 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
903 * Unbinds an object from the GTT aperture.
906 i915_gem_object_unbind(struct drm_gem_object *obj)
908 struct drm_device *dev = obj->dev;
909 struct drm_i915_gem_object *obj_priv = obj->driver_private;
913 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
914 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
916 if (obj_priv->gtt_space == NULL)
919 if (obj_priv->pin_count != 0) {
920 DRM_ERROR("Attempting to unbind pinned buffer\n");
924 /* Wait for any rendering to complete
926 ret = i915_gem_object_wait_rendering(obj);
928 DRM_ERROR("wait_rendering failed: %d\n", ret);
932 /* Move the object to the CPU domain to ensure that
933 * any possible CPU writes while it's not in the GTT
934 * are flushed when we go to remap it. This will
935 * also ensure that all pending GPU writes are finished
938 ret = i915_gem_object_set_domain(obj, I915_GEM_DOMAIN_CPU,
939 I915_GEM_DOMAIN_CPU);
941 DRM_ERROR("set_domain failed: %d\n", ret);
945 if (obj_priv->agp_mem != NULL) {
946 drm_unbind_agp(obj_priv->agp_mem);
947 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
948 obj_priv->agp_mem = NULL;
951 BUG_ON(obj_priv->active);
953 i915_gem_object_free_page_list(obj);
955 if (obj_priv->gtt_space) {
956 atomic_dec(&dev->gtt_count);
957 atomic_sub(obj->size, &dev->gtt_memory);
959 drm_mm_put_block(obj_priv->gtt_space);
960 obj_priv->gtt_space = NULL;
963 /* Remove ourselves from the LRU list if present. */
964 if (!list_empty(&obj_priv->list))
965 list_del_init(&obj_priv->list);
971 i915_gem_evict_something(struct drm_device *dev)
973 drm_i915_private_t *dev_priv = dev->dev_private;
974 struct drm_gem_object *obj;
975 struct drm_i915_gem_object *obj_priv;
979 /* If there's an inactive buffer available now, grab it
982 if (!list_empty(&dev_priv->mm.inactive_list)) {
983 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
984 struct drm_i915_gem_object,
987 BUG_ON(obj_priv->pin_count != 0);
989 DRM_INFO("%s: evicting %p\n", __func__, obj);
991 BUG_ON(obj_priv->active);
993 /* Wait on the rendering and unbind the buffer. */
994 ret = i915_gem_object_unbind(obj);
998 /* If we didn't get anything, but the ring is still processing
999 * things, wait for one of those things to finish and hopefully
1000 * leave us a buffer to evict.
1002 if (!list_empty(&dev_priv->mm.request_list)) {
1003 struct drm_i915_gem_request *request;
1005 request = list_first_entry(&dev_priv->mm.request_list,
1006 struct drm_i915_gem_request,
1009 ret = i915_wait_request(dev, request->seqno);
1013 /* if waiting caused an object to become inactive,
1014 * then loop around and wait for it. Otherwise, we
1015 * assume that waiting freed and unbound something,
1016 * so there should now be some space in the GTT
1018 if (!list_empty(&dev_priv->mm.inactive_list))
1023 /* If we didn't have anything on the request list but there
1024 * are buffers awaiting a flush, emit one and try again.
1025 * When we wait on it, those buffers waiting for that flush
1026 * will get moved to inactive.
1028 if (!list_empty(&dev_priv->mm.flushing_list)) {
1029 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1030 struct drm_i915_gem_object,
1032 obj = obj_priv->obj;
1037 i915_add_request(dev, obj->write_domain);
1043 DRM_ERROR("inactive empty %d request empty %d "
1044 "flushing empty %d\n",
1045 list_empty(&dev_priv->mm.inactive_list),
1046 list_empty(&dev_priv->mm.request_list),
1047 list_empty(&dev_priv->mm.flushing_list));
1048 /* If we didn't do any of the above, there's nothing to be done
1049 * and we just can't fit it in.
1057 i915_gem_object_get_page_list(struct drm_gem_object *obj)
1059 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1061 struct address_space *mapping;
1062 struct inode *inode;
1066 if (obj_priv->page_list)
1069 /* Get the list of pages out of our struct file. They'll be pinned
1070 * at this point until we release them.
1072 page_count = obj->size / PAGE_SIZE;
1073 BUG_ON(obj_priv->page_list != NULL);
1074 obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
1076 if (obj_priv->page_list == NULL) {
1077 DRM_ERROR("Faled to allocate page list\n");
1081 inode = obj->filp->f_path.dentry->d_inode;
1082 mapping = inode->i_mapping;
1083 for (i = 0; i < page_count; i++) {
1084 page = read_mapping_page(mapping, i, NULL);
1086 ret = PTR_ERR(page);
1087 DRM_ERROR("read_mapping_page failed: %d\n", ret);
1088 i915_gem_object_free_page_list(obj);
1091 obj_priv->page_list[i] = page;
1097 * Finds free space in the GTT aperture and binds the object there.
1100 i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
1102 struct drm_device *dev = obj->dev;
1103 drm_i915_private_t *dev_priv = dev->dev_private;
1104 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1105 struct drm_mm_node *free_space;
1106 int page_count, ret;
1109 alignment = PAGE_SIZE;
1110 if (alignment & (PAGE_SIZE - 1)) {
1111 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
1116 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
1117 obj->size, alignment, 0);
1118 if (free_space != NULL) {
1119 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
1121 if (obj_priv->gtt_space != NULL) {
1122 obj_priv->gtt_space->private = obj;
1123 obj_priv->gtt_offset = obj_priv->gtt_space->start;
1126 if (obj_priv->gtt_space == NULL) {
1127 /* If the gtt is empty and we're still having trouble
1128 * fitting our object in, we're out of memory.
1131 DRM_INFO("%s: GTT full, evicting something\n", __func__);
1133 if (list_empty(&dev_priv->mm.inactive_list) &&
1134 list_empty(&dev_priv->mm.flushing_list) &&
1135 list_empty(&dev_priv->mm.active_list)) {
1136 DRM_ERROR("GTT full, but LRU list empty\n");
1140 ret = i915_gem_evict_something(dev);
1142 DRM_ERROR("Failed to evict a buffer %d\n", ret);
1149 DRM_INFO("Binding object of size %d at 0x%08x\n",
1150 obj->size, obj_priv->gtt_offset);
1152 ret = i915_gem_object_get_page_list(obj);
1154 drm_mm_put_block(obj_priv->gtt_space);
1155 obj_priv->gtt_space = NULL;
1159 page_count = obj->size / PAGE_SIZE;
1160 /* Create an AGP memory structure pointing at our pages, and bind it
1163 obj_priv->agp_mem = drm_agp_bind_pages(dev,
1164 obj_priv->page_list,
1166 obj_priv->gtt_offset,
1167 obj_priv->agp_type);
1168 if (obj_priv->agp_mem == NULL) {
1169 i915_gem_object_free_page_list(obj);
1170 drm_mm_put_block(obj_priv->gtt_space);
1171 obj_priv->gtt_space = NULL;
1174 atomic_inc(&dev->gtt_count);
1175 atomic_add(obj->size, &dev->gtt_memory);
1177 /* Assert that the object is not currently in any GPU domain. As it
1178 * wasn't in the GTT, there shouldn't be any way it could have been in
1181 BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1182 BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1188 i915_gem_clflush_object(struct drm_gem_object *obj)
1190 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1192 /* If we don't have a page list set up, then we're not pinned
1193 * to GPU, and we can ignore the cache flush because it'll happen
1194 * again at bind time.
1196 if (obj_priv->page_list == NULL)
1199 drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
1203 * Set the next domain for the specified object. This
1204 * may not actually perform the necessary flushing/invaliding though,
1205 * as that may want to be batched with other set_domain operations
1207 * This is (we hope) the only really tricky part of gem. The goal
1208 * is fairly simple -- track which caches hold bits of the object
1209 * and make sure they remain coherent. A few concrete examples may
1210 * help to explain how it works. For shorthand, we use the notation
1211 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
1212 * a pair of read and write domain masks.
1214 * Case 1: the batch buffer
1220 * 5. Unmapped from GTT
1223 * Let's take these a step at a time
1226 * Pages allocated from the kernel may still have
1227 * cache contents, so we set them to (CPU, CPU) always.
1228 * 2. Written by CPU (using pwrite)
1229 * The pwrite function calls set_domain (CPU, CPU) and
1230 * this function does nothing (as nothing changes)
1232 * This function asserts that the object is not
1233 * currently in any GPU-based read or write domains
1235 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
1236 * As write_domain is zero, this function adds in the
1237 * current read domains (CPU+COMMAND, 0).
1238 * flush_domains is set to CPU.
1239 * invalidate_domains is set to COMMAND
1240 * clflush is run to get data out of the CPU caches
1241 * then i915_dev_set_domain calls i915_gem_flush to
1242 * emit an MI_FLUSH and drm_agp_chipset_flush
1243 * 5. Unmapped from GTT
1244 * i915_gem_object_unbind calls set_domain (CPU, CPU)
1245 * flush_domains and invalidate_domains end up both zero
1246 * so no flushing/invalidating happens
1250 * Case 2: The shared render buffer
1254 * 3. Read/written by GPU
1255 * 4. set_domain to (CPU,CPU)
1256 * 5. Read/written by CPU
1257 * 6. Read/written by GPU
1260 * Same as last example, (CPU, CPU)
1262 * Nothing changes (assertions find that it is not in the GPU)
1263 * 3. Read/written by GPU
1264 * execbuffer calls set_domain (RENDER, RENDER)
1265 * flush_domains gets CPU
1266 * invalidate_domains gets GPU
1268 * MI_FLUSH and drm_agp_chipset_flush
1269 * 4. set_domain (CPU, CPU)
1270 * flush_domains gets GPU
1271 * invalidate_domains gets CPU
1272 * wait_rendering (obj) to make sure all drawing is complete.
1273 * This will include an MI_FLUSH to get the data from GPU
1275 * clflush (obj) to invalidate the CPU cache
1276 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
1277 * 5. Read/written by CPU
1278 * cache lines are loaded and dirtied
1279 * 6. Read written by GPU
1280 * Same as last GPU access
1282 * Case 3: The constant buffer
1287 * 4. Updated (written) by CPU again
1296 * flush_domains = CPU
1297 * invalidate_domains = RENDER
1300 * drm_agp_chipset_flush
1301 * 4. Updated (written) by CPU again
1303 * flush_domains = 0 (no previous write domain)
1304 * invalidate_domains = 0 (no new read domains)
1307 * flush_domains = CPU
1308 * invalidate_domains = RENDER
1311 * drm_agp_chipset_flush
1314 i915_gem_object_set_domain(struct drm_gem_object *obj,
1315 uint32_t read_domains,
1316 uint32_t write_domain)
1318 struct drm_device *dev = obj->dev;
1319 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1320 uint32_t invalidate_domains = 0;
1321 uint32_t flush_domains = 0;
1325 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
1327 obj->read_domains, read_domains,
1328 obj->write_domain, write_domain);
1331 * If the object isn't moving to a new write domain,
1332 * let the object stay in multiple read domains
1334 if (write_domain == 0)
1335 read_domains |= obj->read_domains;
1337 obj_priv->dirty = 1;
1340 * Flush the current write domain if
1341 * the new read domains don't match. Invalidate
1342 * any read domains which differ from the old
1345 if (obj->write_domain && obj->write_domain != read_domains) {
1346 flush_domains |= obj->write_domain;
1347 invalidate_domains |= read_domains & ~obj->write_domain;
1350 * Invalidate any read caches which may have
1351 * stale data. That is, any new read domains.
1353 invalidate_domains |= read_domains & ~obj->read_domains;
1354 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
1356 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
1357 __func__, flush_domains, invalidate_domains);
1360 * If we're invaliding the CPU cache and flushing a GPU cache,
1361 * then pause for rendering so that the GPU caches will be
1362 * flushed before the cpu cache is invalidated
1364 if ((invalidate_domains & I915_GEM_DOMAIN_CPU) &&
1365 (flush_domains & ~(I915_GEM_DOMAIN_CPU |
1366 I915_GEM_DOMAIN_GTT))) {
1367 ret = i915_gem_object_wait_rendering(obj);
1371 i915_gem_clflush_object(obj);
1374 if ((write_domain | flush_domains) != 0)
1375 obj->write_domain = write_domain;
1377 /* If we're invalidating the CPU domain, clear the per-page CPU
1378 * domain list as well.
1380 if (obj_priv->page_cpu_valid != NULL &&
1381 (write_domain != 0 ||
1382 read_domains & I915_GEM_DOMAIN_CPU)) {
1383 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
1385 obj_priv->page_cpu_valid = NULL;
1387 obj->read_domains = read_domains;
1389 dev->invalidate_domains |= invalidate_domains;
1390 dev->flush_domains |= flush_domains;
1392 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
1394 obj->read_domains, obj->write_domain,
1395 dev->invalidate_domains, dev->flush_domains);
1401 * Set the read/write domain on a range of the object.
1403 * Currently only implemented for CPU reads, otherwise drops to normal
1404 * i915_gem_object_set_domain().
1407 i915_gem_object_set_domain_range(struct drm_gem_object *obj,
1410 uint32_t read_domains,
1411 uint32_t write_domain)
1413 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1416 if (obj->read_domains & I915_GEM_DOMAIN_CPU)
1419 if (read_domains != I915_GEM_DOMAIN_CPU ||
1421 return i915_gem_object_set_domain(obj,
1422 read_domains, write_domain);
1424 /* Wait on any GPU rendering to the object to be flushed. */
1425 if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT)) {
1426 ret = i915_gem_object_wait_rendering(obj);
1431 if (obj_priv->page_cpu_valid == NULL) {
1432 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
1436 /* Flush the cache on any pages that are still invalid from the CPU's
1439 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE; i++) {
1440 if (obj_priv->page_cpu_valid[i])
1443 drm_clflush_pages(obj_priv->page_list + i, 1);
1445 obj_priv->page_cpu_valid[i] = 1;
1452 * Once all of the objects have been set in the proper domain,
1453 * perform the necessary flush and invalidate operations.
1455 * Returns the write domains flushed, for use in flush tracking.
1458 i915_gem_dev_set_domain(struct drm_device *dev)
1460 uint32_t flush_domains = dev->flush_domains;
1463 * Now that all the buffers are synced to the proper domains,
1464 * flush and invalidate the collected domains
1466 if (dev->invalidate_domains | dev->flush_domains) {
1468 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
1470 dev->invalidate_domains,
1471 dev->flush_domains);
1474 dev->invalidate_domains,
1475 dev->flush_domains);
1476 dev->invalidate_domains = 0;
1477 dev->flush_domains = 0;
1480 return flush_domains;
1484 * Pin an object to the GTT and evaluate the relocations landing in it.
1487 i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
1488 struct drm_file *file_priv,
1489 struct drm_i915_gem_exec_object *entry)
1491 struct drm_device *dev = obj->dev;
1492 struct drm_i915_gem_relocation_entry reloc;
1493 struct drm_i915_gem_relocation_entry __user *relocs;
1494 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1496 uint32_t last_reloc_offset = -1;
1497 void __iomem *reloc_page = NULL;
1499 /* Choose the GTT offset for our buffer and put it there. */
1500 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
1504 entry->offset = obj_priv->gtt_offset;
1506 relocs = (struct drm_i915_gem_relocation_entry __user *)
1507 (uintptr_t) entry->relocs_ptr;
1508 /* Apply the relocations, using the GTT aperture to avoid cache
1509 * flushing requirements.
1511 for (i = 0; i < entry->relocation_count; i++) {
1512 struct drm_gem_object *target_obj;
1513 struct drm_i915_gem_object *target_obj_priv;
1514 uint32_t reloc_val, reloc_offset;
1515 uint32_t __iomem *reloc_entry;
1517 ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
1519 i915_gem_object_unpin(obj);
1523 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
1524 reloc.target_handle);
1525 if (target_obj == NULL) {
1526 i915_gem_object_unpin(obj);
1529 target_obj_priv = target_obj->driver_private;
1531 /* The target buffer should have appeared before us in the
1532 * exec_object list, so it should have a GTT space bound by now.
1534 if (target_obj_priv->gtt_space == NULL) {
1535 DRM_ERROR("No GTT space found for object %d\n",
1536 reloc.target_handle);
1537 drm_gem_object_unreference(target_obj);
1538 i915_gem_object_unpin(obj);
1542 if (reloc.offset > obj->size - 4) {
1543 DRM_ERROR("Relocation beyond object bounds: "
1544 "obj %p target %d offset %d size %d.\n",
1545 obj, reloc.target_handle,
1546 (int) reloc.offset, (int) obj->size);
1547 drm_gem_object_unreference(target_obj);
1548 i915_gem_object_unpin(obj);
1551 if (reloc.offset & 3) {
1552 DRM_ERROR("Relocation not 4-byte aligned: "
1553 "obj %p target %d offset %d.\n",
1554 obj, reloc.target_handle,
1555 (int) reloc.offset);
1556 drm_gem_object_unreference(target_obj);
1557 i915_gem_object_unpin(obj);
1561 if (reloc.write_domain && target_obj->pending_write_domain &&
1562 reloc.write_domain != target_obj->pending_write_domain) {
1563 DRM_ERROR("Write domain conflict: "
1564 "obj %p target %d offset %d "
1565 "new %08x old %08x\n",
1566 obj, reloc.target_handle,
1569 target_obj->pending_write_domain);
1570 drm_gem_object_unreference(target_obj);
1571 i915_gem_object_unpin(obj);
1576 DRM_INFO("%s: obj %p offset %08x target %d "
1577 "read %08x write %08x gtt %08x "
1578 "presumed %08x delta %08x\n",
1582 (int) reloc.target_handle,
1583 (int) reloc.read_domains,
1584 (int) reloc.write_domain,
1585 (int) target_obj_priv->gtt_offset,
1586 (int) reloc.presumed_offset,
1590 target_obj->pending_read_domains |= reloc.read_domains;
1591 target_obj->pending_write_domain |= reloc.write_domain;
1593 /* If the relocation already has the right value in it, no
1594 * more work needs to be done.
1596 if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
1597 drm_gem_object_unreference(target_obj);
1601 /* Now that we're going to actually write some data in,
1602 * make sure that any rendering using this buffer's contents
1605 i915_gem_object_wait_rendering(obj);
1607 /* As we're writing through the gtt, flush
1608 * any CPU writes before we write the relocations
1610 if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
1611 i915_gem_clflush_object(obj);
1612 drm_agp_chipset_flush(dev);
1613 obj->write_domain = 0;
1616 /* Map the page containing the relocation we're going to
1619 reloc_offset = obj_priv->gtt_offset + reloc.offset;
1620 if (reloc_page == NULL ||
1621 (last_reloc_offset & ~(PAGE_SIZE - 1)) !=
1622 (reloc_offset & ~(PAGE_SIZE - 1))) {
1623 if (reloc_page != NULL)
1624 iounmap(reloc_page);
1626 reloc_page = ioremap_wc(dev->agp->base +
1630 last_reloc_offset = reloc_offset;
1631 if (reloc_page == NULL) {
1632 drm_gem_object_unreference(target_obj);
1633 i915_gem_object_unpin(obj);
1638 reloc_entry = (uint32_t __iomem *)(reloc_page +
1639 (reloc_offset & (PAGE_SIZE - 1)));
1640 reloc_val = target_obj_priv->gtt_offset + reloc.delta;
1643 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
1644 obj, (unsigned int) reloc.offset,
1645 readl(reloc_entry), reloc_val);
1647 writel(reloc_val, reloc_entry);
1649 /* Write the updated presumed offset for this entry back out
1652 reloc.presumed_offset = target_obj_priv->gtt_offset;
1653 ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
1655 drm_gem_object_unreference(target_obj);
1656 i915_gem_object_unpin(obj);
1660 drm_gem_object_unreference(target_obj);
1663 if (reloc_page != NULL)
1664 iounmap(reloc_page);
1668 i915_gem_dump_object(obj, 128, __func__, ~0);
1673 /** Dispatch a batchbuffer to the ring
1676 i915_dispatch_gem_execbuffer(struct drm_device *dev,
1677 struct drm_i915_gem_execbuffer *exec,
1678 uint64_t exec_offset)
1680 drm_i915_private_t *dev_priv = dev->dev_private;
1681 struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
1682 (uintptr_t) exec->cliprects_ptr;
1683 int nbox = exec->num_cliprects;
1685 uint32_t exec_start, exec_len;
1688 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
1689 exec_len = (uint32_t) exec->batch_len;
1691 if ((exec_start | exec_len) & 0x7) {
1692 DRM_ERROR("alignment\n");
1699 count = nbox ? nbox : 1;
1701 for (i = 0; i < count; i++) {
1703 int ret = i915_emit_box(dev, boxes, i,
1704 exec->DR1, exec->DR4);
1709 if (IS_I830(dev) || IS_845G(dev)) {
1711 OUT_RING(MI_BATCH_BUFFER);
1712 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
1713 OUT_RING(exec_start + exec_len - 4);
1718 if (IS_I965G(dev)) {
1719 OUT_RING(MI_BATCH_BUFFER_START |
1721 MI_BATCH_NON_SECURE_I965);
1722 OUT_RING(exec_start);
1724 OUT_RING(MI_BATCH_BUFFER_START |
1726 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
1732 /* XXX breadcrumb */
1736 /* Throttle our rendering by waiting until the ring has completed our requests
1737 * emitted over 20 msec ago.
1739 * This should get us reasonable parallelism between CPU and GPU but also
1740 * relatively low latency when blocking on a particular request to finish.
1743 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
1745 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1749 mutex_lock(&dev->struct_mutex);
1750 seqno = i915_file_priv->mm.last_gem_throttle_seqno;
1751 i915_file_priv->mm.last_gem_throttle_seqno =
1752 i915_file_priv->mm.last_gem_seqno;
1754 ret = i915_wait_request(dev, seqno);
1755 mutex_unlock(&dev->struct_mutex);
1760 i915_gem_execbuffer(struct drm_device *dev, void *data,
1761 struct drm_file *file_priv)
1763 drm_i915_private_t *dev_priv = dev->dev_private;
1764 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1765 struct drm_i915_gem_execbuffer *args = data;
1766 struct drm_i915_gem_exec_object *exec_list = NULL;
1767 struct drm_gem_object **object_list = NULL;
1768 struct drm_gem_object *batch_obj;
1769 int ret, i, pinned = 0;
1770 uint64_t exec_offset;
1771 uint32_t seqno, flush_domains;
1774 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
1775 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
1778 if (args->buffer_count < 1) {
1779 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
1782 /* Copy in the exec list from userland */
1783 exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
1785 object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
1787 if (exec_list == NULL || object_list == NULL) {
1788 DRM_ERROR("Failed to allocate exec or object list "
1790 args->buffer_count);
1794 ret = copy_from_user(exec_list,
1795 (struct drm_i915_relocation_entry __user *)
1796 (uintptr_t) args->buffers_ptr,
1797 sizeof(*exec_list) * args->buffer_count);
1799 DRM_ERROR("copy %d exec entries failed %d\n",
1800 args->buffer_count, ret);
1804 mutex_lock(&dev->struct_mutex);
1806 i915_verify_inactive(dev, __FILE__, __LINE__);
1808 if (dev_priv->mm.wedged) {
1809 DRM_ERROR("Execbuf while wedged\n");
1810 mutex_unlock(&dev->struct_mutex);
1814 if (dev_priv->mm.suspended) {
1815 DRM_ERROR("Execbuf while VT-switched.\n");
1816 mutex_unlock(&dev->struct_mutex);
1820 /* Zero the gloabl flush/invalidate flags. These
1821 * will be modified as each object is bound to the
1824 dev->invalidate_domains = 0;
1825 dev->flush_domains = 0;
1827 /* Look up object handles and perform the relocations */
1828 for (i = 0; i < args->buffer_count; i++) {
1829 object_list[i] = drm_gem_object_lookup(dev, file_priv,
1830 exec_list[i].handle);
1831 if (object_list[i] == NULL) {
1832 DRM_ERROR("Invalid object handle %d at index %d\n",
1833 exec_list[i].handle, i);
1838 object_list[i]->pending_read_domains = 0;
1839 object_list[i]->pending_write_domain = 0;
1840 ret = i915_gem_object_pin_and_relocate(object_list[i],
1844 DRM_ERROR("object bind and relocate failed %d\n", ret);
1850 /* Set the pending read domains for the batch buffer to COMMAND */
1851 batch_obj = object_list[args->buffer_count-1];
1852 batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
1853 batch_obj->pending_write_domain = 0;
1855 i915_verify_inactive(dev, __FILE__, __LINE__);
1857 for (i = 0; i < args->buffer_count; i++) {
1858 struct drm_gem_object *obj = object_list[i];
1859 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1861 if (obj_priv->gtt_space == NULL) {
1862 /* We evicted the buffer in the process of validating
1863 * our set of buffers in. We could try to recover by
1864 * kicking them everything out and trying again from
1871 /* make sure all previous memory operations have passed */
1872 ret = i915_gem_object_set_domain(obj,
1873 obj->pending_read_domains,
1874 obj->pending_write_domain);
1879 i915_verify_inactive(dev, __FILE__, __LINE__);
1881 /* Flush/invalidate caches and chipset buffer */
1882 flush_domains = i915_gem_dev_set_domain(dev);
1884 i915_verify_inactive(dev, __FILE__, __LINE__);
1887 for (i = 0; i < args->buffer_count; i++) {
1888 i915_gem_object_check_coherency(object_list[i],
1889 exec_list[i].handle);
1893 exec_offset = exec_list[args->buffer_count - 1].offset;
1896 i915_gem_dump_object(object_list[args->buffer_count - 1],
1902 (void)i915_add_request(dev, flush_domains);
1904 /* Exec the batchbuffer */
1905 ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
1907 DRM_ERROR("dispatch failed %d\n", ret);
1912 * Ensure that the commands in the batch buffer are
1913 * finished before the interrupt fires
1915 flush_domains = i915_retire_commands(dev);
1917 i915_verify_inactive(dev, __FILE__, __LINE__);
1920 * Get a seqno representing the execution of the current buffer,
1921 * which we can wait on. We would like to mitigate these interrupts,
1922 * likely by only creating seqnos occasionally (so that we have
1923 * *some* interrupts representing completion of buffers that we can
1924 * wait on when trying to clear up gtt space).
1926 seqno = i915_add_request(dev, flush_domains);
1928 i915_file_priv->mm.last_gem_seqno = seqno;
1929 for (i = 0; i < args->buffer_count; i++) {
1930 struct drm_gem_object *obj = object_list[i];
1931 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1933 i915_gem_object_move_to_active(obj);
1934 obj_priv->last_rendering_seqno = seqno;
1936 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
1940 i915_dump_lru(dev, __func__);
1943 i915_verify_inactive(dev, __FILE__, __LINE__);
1945 /* Copy the new buffer offsets back to the user's exec list. */
1946 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
1947 (uintptr_t) args->buffers_ptr,
1949 sizeof(*exec_list) * args->buffer_count);
1951 DRM_ERROR("failed to copy %d exec entries "
1952 "back to user (%d)\n",
1953 args->buffer_count, ret);
1955 if (object_list != NULL) {
1956 for (i = 0; i < pinned; i++)
1957 i915_gem_object_unpin(object_list[i]);
1959 for (i = 0; i < args->buffer_count; i++)
1960 drm_gem_object_unreference(object_list[i]);
1962 mutex_unlock(&dev->struct_mutex);
1965 drm_free(object_list, sizeof(*object_list) * args->buffer_count,
1967 drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
1974 i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
1976 struct drm_device *dev = obj->dev;
1977 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1980 i915_verify_inactive(dev, __FILE__, __LINE__);
1981 if (obj_priv->gtt_space == NULL) {
1982 ret = i915_gem_object_bind_to_gtt(obj, alignment);
1984 DRM_ERROR("Failure to bind: %d", ret);
1988 obj_priv->pin_count++;
1990 /* If the object is not active and not pending a flush,
1991 * remove it from the inactive list
1993 if (obj_priv->pin_count == 1) {
1994 atomic_inc(&dev->pin_count);
1995 atomic_add(obj->size, &dev->pin_memory);
1996 if (!obj_priv->active &&
1997 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
1998 I915_GEM_DOMAIN_GTT)) == 0 &&
1999 !list_empty(&obj_priv->list))
2000 list_del_init(&obj_priv->list);
2002 i915_verify_inactive(dev, __FILE__, __LINE__);
2008 i915_gem_object_unpin(struct drm_gem_object *obj)
2010 struct drm_device *dev = obj->dev;
2011 drm_i915_private_t *dev_priv = dev->dev_private;
2012 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2014 i915_verify_inactive(dev, __FILE__, __LINE__);
2015 obj_priv->pin_count--;
2016 BUG_ON(obj_priv->pin_count < 0);
2017 BUG_ON(obj_priv->gtt_space == NULL);
2019 /* If the object is no longer pinned, and is
2020 * neither active nor being flushed, then stick it on
2023 if (obj_priv->pin_count == 0) {
2024 if (!obj_priv->active &&
2025 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2026 I915_GEM_DOMAIN_GTT)) == 0)
2027 list_move_tail(&obj_priv->list,
2028 &dev_priv->mm.inactive_list);
2029 atomic_dec(&dev->pin_count);
2030 atomic_sub(obj->size, &dev->pin_memory);
2032 i915_verify_inactive(dev, __FILE__, __LINE__);
2036 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
2037 struct drm_file *file_priv)
2039 struct drm_i915_gem_pin *args = data;
2040 struct drm_gem_object *obj;
2041 struct drm_i915_gem_object *obj_priv;
2044 mutex_lock(&dev->struct_mutex);
2046 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2048 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
2050 mutex_unlock(&dev->struct_mutex);
2053 obj_priv = obj->driver_private;
2055 ret = i915_gem_object_pin(obj, args->alignment);
2057 drm_gem_object_unreference(obj);
2058 mutex_unlock(&dev->struct_mutex);
2062 /* XXX - flush the CPU caches for pinned objects
2063 * as the X server doesn't manage domains yet
2065 if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
2066 i915_gem_clflush_object(obj);
2067 drm_agp_chipset_flush(dev);
2068 obj->write_domain = 0;
2070 args->offset = obj_priv->gtt_offset;
2071 drm_gem_object_unreference(obj);
2072 mutex_unlock(&dev->struct_mutex);
2078 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
2079 struct drm_file *file_priv)
2081 struct drm_i915_gem_pin *args = data;
2082 struct drm_gem_object *obj;
2084 mutex_lock(&dev->struct_mutex);
2086 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2088 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
2090 mutex_unlock(&dev->struct_mutex);
2094 i915_gem_object_unpin(obj);
2096 drm_gem_object_unreference(obj);
2097 mutex_unlock(&dev->struct_mutex);
2102 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
2103 struct drm_file *file_priv)
2105 struct drm_i915_gem_busy *args = data;
2106 struct drm_gem_object *obj;
2107 struct drm_i915_gem_object *obj_priv;
2109 mutex_lock(&dev->struct_mutex);
2110 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2112 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
2114 mutex_unlock(&dev->struct_mutex);
2118 obj_priv = obj->driver_private;
2119 args->busy = obj_priv->active;
2121 drm_gem_object_unreference(obj);
2122 mutex_unlock(&dev->struct_mutex);
2127 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
2128 struct drm_file *file_priv)
2130 return i915_gem_ring_throttle(dev, file_priv);
2133 int i915_gem_init_object(struct drm_gem_object *obj)
2135 struct drm_i915_gem_object *obj_priv;
2137 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
2138 if (obj_priv == NULL)
2142 * We've just allocated pages from the kernel,
2143 * so they've just been written by the CPU with
2144 * zeros. They'll need to be clflushed before we
2145 * use them with the GPU.
2147 obj->write_domain = I915_GEM_DOMAIN_CPU;
2148 obj->read_domains = I915_GEM_DOMAIN_CPU;
2150 obj_priv->agp_type = AGP_USER_MEMORY;
2152 obj->driver_private = obj_priv;
2153 obj_priv->obj = obj;
2154 INIT_LIST_HEAD(&obj_priv->list);
2158 void i915_gem_free_object(struct drm_gem_object *obj)
2160 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2162 while (obj_priv->pin_count > 0)
2163 i915_gem_object_unpin(obj);
2165 i915_gem_object_unbind(obj);
2167 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
2168 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
2172 i915_gem_set_domain(struct drm_gem_object *obj,
2173 struct drm_file *file_priv,
2174 uint32_t read_domains,
2175 uint32_t write_domain)
2177 struct drm_device *dev = obj->dev;
2179 uint32_t flush_domains;
2181 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
2183 ret = i915_gem_object_set_domain(obj, read_domains, write_domain);
2186 flush_domains = i915_gem_dev_set_domain(obj->dev);
2188 if (flush_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT))
2189 (void) i915_add_request(dev, flush_domains);
2194 /** Unbinds all objects that are on the given buffer list. */
2196 i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
2198 struct drm_gem_object *obj;
2199 struct drm_i915_gem_object *obj_priv;
2202 while (!list_empty(head)) {
2203 obj_priv = list_first_entry(head,
2204 struct drm_i915_gem_object,
2206 obj = obj_priv->obj;
2208 if (obj_priv->pin_count != 0) {
2209 DRM_ERROR("Pinned object in unbind list\n");
2210 mutex_unlock(&dev->struct_mutex);
2214 ret = i915_gem_object_unbind(obj);
2216 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
2218 mutex_unlock(&dev->struct_mutex);
2228 i915_gem_idle(struct drm_device *dev)
2230 drm_i915_private_t *dev_priv = dev->dev_private;
2231 uint32_t seqno, cur_seqno, last_seqno;
2234 mutex_lock(&dev->struct_mutex);
2236 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
2237 mutex_unlock(&dev->struct_mutex);
2241 /* Hack! Don't let anybody do execbuf while we don't control the chip.
2242 * We need to replace this with a semaphore, or something.
2244 dev_priv->mm.suspended = 1;
2246 /* Cancel the retire work handler, wait for it to finish if running
2248 mutex_unlock(&dev->struct_mutex);
2249 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
2250 mutex_lock(&dev->struct_mutex);
2252 i915_kernel_lost_context(dev);
2254 /* Flush the GPU along with all non-CPU write domains
2256 i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
2257 ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2258 seqno = i915_add_request(dev, ~(I915_GEM_DOMAIN_CPU |
2259 I915_GEM_DOMAIN_GTT));
2262 mutex_unlock(&dev->struct_mutex);
2266 dev_priv->mm.waiting_gem_seqno = seqno;
2270 cur_seqno = i915_get_gem_seqno(dev);
2271 if (i915_seqno_passed(cur_seqno, seqno))
2273 if (last_seqno == cur_seqno) {
2274 if (stuck++ > 100) {
2275 DRM_ERROR("hardware wedged\n");
2276 dev_priv->mm.wedged = 1;
2277 DRM_WAKEUP(&dev_priv->irq_queue);
2282 last_seqno = cur_seqno;
2284 dev_priv->mm.waiting_gem_seqno = 0;
2286 i915_gem_retire_requests(dev);
2288 /* Active and flushing should now be empty as we've
2289 * waited for a sequence higher than any pending execbuffer
2291 BUG_ON(!list_empty(&dev_priv->mm.active_list));
2292 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2294 /* Request should now be empty as we've also waited
2295 * for the last request in the list
2297 BUG_ON(!list_empty(&dev_priv->mm.request_list));
2299 /* Move all buffers out of the GTT. */
2300 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
2302 mutex_unlock(&dev->struct_mutex);
2306 BUG_ON(!list_empty(&dev_priv->mm.active_list));
2307 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2308 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
2309 BUG_ON(!list_empty(&dev_priv->mm.request_list));
2311 i915_gem_cleanup_ringbuffer(dev);
2312 mutex_unlock(&dev->struct_mutex);
2318 i915_gem_init_hws(struct drm_device *dev)
2320 drm_i915_private_t *dev_priv = dev->dev_private;
2321 struct drm_gem_object *obj;
2322 struct drm_i915_gem_object *obj_priv;
2325 /* If we need a physical address for the status page, it's already
2326 * initialized at driver load time.
2328 if (!I915_NEED_GFX_HWS(dev))
2331 obj = drm_gem_object_alloc(dev, 4096);
2333 DRM_ERROR("Failed to allocate status page\n");
2336 obj_priv = obj->driver_private;
2337 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
2339 ret = i915_gem_object_pin(obj, 4096);
2341 drm_gem_object_unreference(obj);
2345 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
2347 dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
2348 if (dev_priv->hw_status_page == NULL) {
2349 DRM_ERROR("Failed to map status page.\n");
2350 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
2351 drm_gem_object_unreference(obj);
2354 dev_priv->hws_obj = obj;
2355 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
2356 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
2357 I915_READ(HWS_PGA); /* posting read */
2358 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
2364 i915_gem_init_ringbuffer(struct drm_device *dev)
2366 drm_i915_private_t *dev_priv = dev->dev_private;
2367 struct drm_gem_object *obj;
2368 struct drm_i915_gem_object *obj_priv;
2372 ret = i915_gem_init_hws(dev);
2376 obj = drm_gem_object_alloc(dev, 128 * 1024);
2378 DRM_ERROR("Failed to allocate ringbuffer\n");
2381 obj_priv = obj->driver_private;
2383 ret = i915_gem_object_pin(obj, 4096);
2385 drm_gem_object_unreference(obj);
2389 /* Set up the kernel mapping for the ring. */
2390 dev_priv->ring.Size = obj->size;
2391 dev_priv->ring.tail_mask = obj->size - 1;
2393 dev_priv->ring.map.offset = dev->agp->base + obj_priv->gtt_offset;
2394 dev_priv->ring.map.size = obj->size;
2395 dev_priv->ring.map.type = 0;
2396 dev_priv->ring.map.flags = 0;
2397 dev_priv->ring.map.mtrr = 0;
2399 drm_core_ioremap_wc(&dev_priv->ring.map, dev);
2400 if (dev_priv->ring.map.handle == NULL) {
2401 DRM_ERROR("Failed to map ringbuffer.\n");
2402 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
2403 drm_gem_object_unreference(obj);
2406 dev_priv->ring.ring_obj = obj;
2407 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
2409 /* Stop the ring if it's running. */
2410 I915_WRITE(PRB0_CTL, 0);
2411 I915_WRITE(PRB0_TAIL, 0);
2412 I915_WRITE(PRB0_HEAD, 0);
2414 /* Initialize the ring. */
2415 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
2416 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
2418 /* G45 ring initialization fails to reset head to zero */
2420 DRM_ERROR("Ring head not reset to zero "
2421 "ctl %08x head %08x tail %08x start %08x\n",
2422 I915_READ(PRB0_CTL),
2423 I915_READ(PRB0_HEAD),
2424 I915_READ(PRB0_TAIL),
2425 I915_READ(PRB0_START));
2426 I915_WRITE(PRB0_HEAD, 0);
2428 DRM_ERROR("Ring head forced to zero "
2429 "ctl %08x head %08x tail %08x start %08x\n",
2430 I915_READ(PRB0_CTL),
2431 I915_READ(PRB0_HEAD),
2432 I915_READ(PRB0_TAIL),
2433 I915_READ(PRB0_START));
2436 I915_WRITE(PRB0_CTL,
2437 ((obj->size - 4096) & RING_NR_PAGES) |
2441 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
2443 /* If the head is still not zero, the ring is dead */
2445 DRM_ERROR("Ring initialization failed "
2446 "ctl %08x head %08x tail %08x start %08x\n",
2447 I915_READ(PRB0_CTL),
2448 I915_READ(PRB0_HEAD),
2449 I915_READ(PRB0_TAIL),
2450 I915_READ(PRB0_START));
2454 /* Update our cache of the ring state */
2455 i915_kernel_lost_context(dev);
2461 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
2463 drm_i915_private_t *dev_priv = dev->dev_private;
2465 if (dev_priv->ring.ring_obj == NULL)
2468 drm_core_ioremapfree(&dev_priv->ring.map, dev);
2470 i915_gem_object_unpin(dev_priv->ring.ring_obj);
2471 drm_gem_object_unreference(dev_priv->ring.ring_obj);
2472 dev_priv->ring.ring_obj = NULL;
2473 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
2475 if (dev_priv->hws_obj != NULL) {
2476 struct drm_gem_object *obj = dev_priv->hws_obj;
2477 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2479 kunmap(obj_priv->page_list[0]);
2480 i915_gem_object_unpin(obj);
2481 drm_gem_object_unreference(obj);
2482 dev_priv->hws_obj = NULL;
2483 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
2484 dev_priv->hw_status_page = NULL;
2486 /* Write high address into HWS_PGA when disabling. */
2487 I915_WRITE(HWS_PGA, 0x1ffff000);
2492 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
2493 struct drm_file *file_priv)
2495 drm_i915_private_t *dev_priv = dev->dev_private;
2498 if (dev_priv->mm.wedged) {
2499 DRM_ERROR("Reenabling wedged hardware, good luck\n");
2500 dev_priv->mm.wedged = 0;
2503 ret = i915_gem_init_ringbuffer(dev);
2507 mutex_lock(&dev->struct_mutex);
2508 BUG_ON(!list_empty(&dev_priv->mm.active_list));
2509 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2510 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
2511 BUG_ON(!list_empty(&dev_priv->mm.request_list));
2512 dev_priv->mm.suspended = 0;
2513 mutex_unlock(&dev->struct_mutex);
2515 drm_irq_install(dev);
2521 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
2522 struct drm_file *file_priv)
2526 ret = i915_gem_idle(dev);
2527 drm_irq_uninstall(dev);
2533 i915_gem_lastclose(struct drm_device *dev)
2537 ret = i915_gem_idle(dev);
2539 DRM_ERROR("failed to idle hardware: %d\n", ret);
2543 i915_gem_load(struct drm_device *dev)
2545 drm_i915_private_t *dev_priv = dev->dev_private;
2547 INIT_LIST_HEAD(&dev_priv->mm.active_list);
2548 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
2549 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
2550 INIT_LIST_HEAD(&dev_priv->mm.request_list);
2551 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
2552 i915_gem_retire_work_handler);
2553 INIT_WORK(&dev_priv->mm.vblank_work,
2554 i915_gem_vblank_work_handler);
2555 dev_priv->mm.next_gem_seqno = 1;
2557 i915_gem_detect_bit_6_swizzle(dev);