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>
34 #define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
37 i915_gem_object_set_domain(struct drm_gem_object *obj,
38 uint32_t read_domains,
39 uint32_t write_domain);
41 i915_gem_object_set_domain_range(struct drm_gem_object *obj,
44 uint32_t read_domains,
45 uint32_t write_domain);
47 i915_gem_set_domain(struct drm_gem_object *obj,
48 struct drm_file *file_priv,
49 uint32_t read_domains,
50 uint32_t write_domain);
51 static int i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj,
53 static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
54 static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
55 static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
58 i915_gem_cleanup_ringbuffer(struct drm_device *dev);
61 i915_gem_init_ioctl(struct drm_device *dev, void *data,
62 struct drm_file *file_priv)
64 drm_i915_private_t *dev_priv = dev->dev_private;
65 struct drm_i915_gem_init *args = data;
67 mutex_lock(&dev->struct_mutex);
69 if (args->gtt_start >= args->gtt_end ||
70 (args->gtt_start & (PAGE_SIZE - 1)) != 0 ||
71 (args->gtt_end & (PAGE_SIZE - 1)) != 0) {
72 mutex_unlock(&dev->struct_mutex);
76 drm_mm_init(&dev_priv->mm.gtt_space, args->gtt_start,
77 args->gtt_end - args->gtt_start);
79 dev->gtt_total = (uint32_t) (args->gtt_end - args->gtt_start);
81 mutex_unlock(&dev->struct_mutex);
87 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
88 struct drm_file *file_priv)
90 struct drm_i915_gem_get_aperture *args = data;
92 if (!(dev->driver->driver_features & DRIVER_GEM))
95 args->aper_size = dev->gtt_total;
96 args->aper_available_size = (args->aper_size -
97 atomic_read(&dev->pin_memory));
104 * Creates a new mm object and returns a handle to it.
107 i915_gem_create_ioctl(struct drm_device *dev, void *data,
108 struct drm_file *file_priv)
110 struct drm_i915_gem_create *args = data;
111 struct drm_gem_object *obj;
114 args->size = roundup(args->size, PAGE_SIZE);
116 /* Allocate the new object */
117 obj = drm_gem_object_alloc(dev, args->size);
121 ret = drm_gem_handle_create(file_priv, obj, &handle);
122 mutex_lock(&dev->struct_mutex);
123 drm_gem_object_handle_unreference(obj);
124 mutex_unlock(&dev->struct_mutex);
129 args->handle = handle;
135 * Reads data from the object referenced by handle.
137 * On error, the contents of *data are undefined.
140 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
141 struct drm_file *file_priv)
143 struct drm_i915_gem_pread *args = data;
144 struct drm_gem_object *obj;
145 struct drm_i915_gem_object *obj_priv;
150 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
153 obj_priv = obj->driver_private;
155 /* Bounds check source.
157 * XXX: This could use review for overflow issues...
159 if (args->offset > obj->size || args->size > obj->size ||
160 args->offset + args->size > obj->size) {
161 drm_gem_object_unreference(obj);
165 mutex_lock(&dev->struct_mutex);
167 ret = i915_gem_object_set_domain_range(obj, args->offset, args->size,
168 I915_GEM_DOMAIN_CPU, 0);
170 drm_gem_object_unreference(obj);
171 mutex_unlock(&dev->struct_mutex);
175 offset = args->offset;
177 read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
178 args->size, &offset);
179 if (read != args->size) {
180 drm_gem_object_unreference(obj);
181 mutex_unlock(&dev->struct_mutex);
188 drm_gem_object_unreference(obj);
189 mutex_unlock(&dev->struct_mutex);
194 /* This is the fast write path which cannot handle
195 * page faults in the source data
199 fast_user_write(struct io_mapping *mapping,
200 loff_t page_base, int page_offset,
201 char __user *user_data,
205 unsigned long unwritten;
207 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
208 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
210 io_mapping_unmap_atomic(vaddr_atomic);
216 /* Here's the write path which can sleep for
221 slow_user_write(struct io_mapping *mapping,
222 loff_t page_base, int page_offset,
223 char __user *user_data,
227 unsigned long unwritten;
229 vaddr = io_mapping_map_wc(mapping, page_base);
232 unwritten = __copy_from_user(vaddr + page_offset,
234 io_mapping_unmap(vaddr);
241 i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
242 struct drm_i915_gem_pwrite *args,
243 struct drm_file *file_priv)
245 struct drm_i915_gem_object *obj_priv = obj->driver_private;
246 drm_i915_private_t *dev_priv = dev->dev_private;
248 loff_t offset, page_base;
249 char __user *user_data;
250 int page_offset, page_length;
253 user_data = (char __user *) (uintptr_t) args->data_ptr;
255 if (!access_ok(VERIFY_READ, user_data, remain))
259 mutex_lock(&dev->struct_mutex);
260 ret = i915_gem_object_pin(obj, 0);
262 mutex_unlock(&dev->struct_mutex);
265 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
269 obj_priv = obj->driver_private;
270 offset = obj_priv->gtt_offset + args->offset;
274 /* Operation in this page
276 * page_base = page offset within aperture
277 * page_offset = offset within page
278 * page_length = bytes to copy for this page
280 page_base = (offset & ~(PAGE_SIZE-1));
281 page_offset = offset & (PAGE_SIZE-1);
282 page_length = remain;
283 if ((page_offset + remain) > PAGE_SIZE)
284 page_length = PAGE_SIZE - page_offset;
286 ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
287 page_offset, user_data, page_length);
289 /* If we get a fault while copying data, then (presumably) our
290 * source page isn't available. In this case, use the
291 * non-atomic function
294 ret = slow_user_write (dev_priv->mm.gtt_mapping,
295 page_base, page_offset,
296 user_data, page_length);
301 remain -= page_length;
302 user_data += page_length;
303 offset += page_length;
307 i915_gem_object_unpin(obj);
308 mutex_unlock(&dev->struct_mutex);
314 i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
315 struct drm_i915_gem_pwrite *args,
316 struct drm_file *file_priv)
322 mutex_lock(&dev->struct_mutex);
324 ret = i915_gem_set_domain(obj, file_priv,
325 I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
327 mutex_unlock(&dev->struct_mutex);
331 offset = args->offset;
333 written = vfs_write(obj->filp,
334 (char __user *)(uintptr_t) args->data_ptr,
335 args->size, &offset);
336 if (written != args->size) {
337 mutex_unlock(&dev->struct_mutex);
344 mutex_unlock(&dev->struct_mutex);
350 * Writes data to the object referenced by handle.
352 * On error, the contents of the buffer that were to be modified are undefined.
355 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
356 struct drm_file *file_priv)
358 struct drm_i915_gem_pwrite *args = data;
359 struct drm_gem_object *obj;
360 struct drm_i915_gem_object *obj_priv;
363 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
366 obj_priv = obj->driver_private;
368 /* Bounds check destination.
370 * XXX: This could use review for overflow issues...
372 if (args->offset > obj->size || args->size > obj->size ||
373 args->offset + args->size > obj->size) {
374 drm_gem_object_unreference(obj);
378 /* We can only do the GTT pwrite on untiled buffers, as otherwise
379 * it would end up going through the fenced access, and we'll get
380 * different detiling behavior between reading and writing.
381 * pread/pwrite currently are reading and writing from the CPU
382 * perspective, requiring manual detiling by the client.
384 if (obj_priv->tiling_mode == I915_TILING_NONE &&
386 ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
388 ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
392 DRM_INFO("pwrite failed %d\n", ret);
395 drm_gem_object_unreference(obj);
401 * Called when user space prepares to use an object with the CPU, either
402 * through the mmap ioctl's mapping or a GTT mapping.
405 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
406 struct drm_file *file_priv)
408 struct drm_i915_gem_set_domain *args = data;
409 struct drm_gem_object *obj;
410 uint32_t read_domains = args->read_domains;
411 uint32_t write_domain = args->write_domain;
414 if (!(dev->driver->driver_features & DRIVER_GEM))
417 /* Only handle setting domains to types used by the CPU. */
418 if (write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
421 if (read_domains & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
424 /* Having something in the write domain implies it's in the read
425 * domain, and only that read domain. Enforce that in the request.
427 if (write_domain != 0 && read_domains != write_domain)
430 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
434 mutex_lock(&dev->struct_mutex);
436 DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
437 obj, obj->size, read_domains, write_domain);
439 if (read_domains & I915_GEM_DOMAIN_GTT) {
440 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
442 ret = i915_gem_set_domain(obj, file_priv,
443 read_domains, write_domain);
446 drm_gem_object_unreference(obj);
447 mutex_unlock(&dev->struct_mutex);
452 * Called when user space has done writes to this buffer
455 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
456 struct drm_file *file_priv)
458 struct drm_i915_gem_sw_finish *args = data;
459 struct drm_gem_object *obj;
460 struct drm_i915_gem_object *obj_priv;
463 if (!(dev->driver->driver_features & DRIVER_GEM))
466 mutex_lock(&dev->struct_mutex);
467 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
469 mutex_unlock(&dev->struct_mutex);
474 DRM_INFO("%s: sw_finish %d (%p %d)\n",
475 __func__, args->handle, obj, obj->size);
477 obj_priv = obj->driver_private;
479 /* Pinned buffers may be scanout, so flush the cache */
480 if ((obj->write_domain & I915_GEM_DOMAIN_CPU) && obj_priv->pin_count) {
481 i915_gem_clflush_object(obj);
482 drm_agp_chipset_flush(dev);
484 drm_gem_object_unreference(obj);
485 mutex_unlock(&dev->struct_mutex);
490 * Maps the contents of an object, returning the address it is mapped
493 * While the mapping holds a reference on the contents of the object, it doesn't
494 * imply a ref on the object itself.
497 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
498 struct drm_file *file_priv)
500 struct drm_i915_gem_mmap *args = data;
501 struct drm_gem_object *obj;
505 if (!(dev->driver->driver_features & DRIVER_GEM))
508 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
512 offset = args->offset;
514 down_write(¤t->mm->mmap_sem);
515 addr = do_mmap(obj->filp, 0, args->size,
516 PROT_READ | PROT_WRITE, MAP_SHARED,
518 up_write(¤t->mm->mmap_sem);
519 mutex_lock(&dev->struct_mutex);
520 drm_gem_object_unreference(obj);
521 mutex_unlock(&dev->struct_mutex);
522 if (IS_ERR((void *)addr))
525 args->addr_ptr = (uint64_t) addr;
531 i915_gem_object_free_page_list(struct drm_gem_object *obj)
533 struct drm_i915_gem_object *obj_priv = obj->driver_private;
534 int page_count = obj->size / PAGE_SIZE;
537 if (obj_priv->page_list == NULL)
541 for (i = 0; i < page_count; i++)
542 if (obj_priv->page_list[i] != NULL) {
544 set_page_dirty(obj_priv->page_list[i]);
545 mark_page_accessed(obj_priv->page_list[i]);
546 page_cache_release(obj_priv->page_list[i]);
550 drm_free(obj_priv->page_list,
551 page_count * sizeof(struct page *),
553 obj_priv->page_list = NULL;
557 i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
559 struct drm_device *dev = obj->dev;
560 drm_i915_private_t *dev_priv = dev->dev_private;
561 struct drm_i915_gem_object *obj_priv = obj->driver_private;
563 /* Add a reference if we're newly entering the active list. */
564 if (!obj_priv->active) {
565 drm_gem_object_reference(obj);
566 obj_priv->active = 1;
568 /* Move from whatever list we were on to the tail of execution. */
569 list_move_tail(&obj_priv->list,
570 &dev_priv->mm.active_list);
571 obj_priv->last_rendering_seqno = seqno;
575 i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
577 struct drm_device *dev = obj->dev;
578 drm_i915_private_t *dev_priv = dev->dev_private;
579 struct drm_i915_gem_object *obj_priv = obj->driver_private;
581 BUG_ON(!obj_priv->active);
582 list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
583 obj_priv->last_rendering_seqno = 0;
587 i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
589 struct drm_device *dev = obj->dev;
590 drm_i915_private_t *dev_priv = dev->dev_private;
591 struct drm_i915_gem_object *obj_priv = obj->driver_private;
593 i915_verify_inactive(dev, __FILE__, __LINE__);
594 if (obj_priv->pin_count != 0)
595 list_del_init(&obj_priv->list);
597 list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
599 obj_priv->last_rendering_seqno = 0;
600 if (obj_priv->active) {
601 obj_priv->active = 0;
602 drm_gem_object_unreference(obj);
604 i915_verify_inactive(dev, __FILE__, __LINE__);
608 * Creates a new sequence number, emitting a write of it to the status page
609 * plus an interrupt, which will trigger i915_user_interrupt_handler.
611 * Must be called with struct_lock held.
613 * Returned sequence numbers are nonzero on success.
616 i915_add_request(struct drm_device *dev, uint32_t flush_domains)
618 drm_i915_private_t *dev_priv = dev->dev_private;
619 struct drm_i915_gem_request *request;
624 request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
628 /* Grab the seqno we're going to make this request be, and bump the
629 * next (skipping 0 so it can be the reserved no-seqno value).
631 seqno = dev_priv->mm.next_gem_seqno;
632 dev_priv->mm.next_gem_seqno++;
633 if (dev_priv->mm.next_gem_seqno == 0)
634 dev_priv->mm.next_gem_seqno++;
637 OUT_RING(MI_STORE_DWORD_INDEX);
638 OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
641 OUT_RING(MI_USER_INTERRUPT);
644 DRM_DEBUG("%d\n", seqno);
646 request->seqno = seqno;
647 request->emitted_jiffies = jiffies;
648 was_empty = list_empty(&dev_priv->mm.request_list);
649 list_add_tail(&request->list, &dev_priv->mm.request_list);
651 /* Associate any objects on the flushing list matching the write
652 * domain we're flushing with our flush.
654 if (flush_domains != 0) {
655 struct drm_i915_gem_object *obj_priv, *next;
657 list_for_each_entry_safe(obj_priv, next,
658 &dev_priv->mm.flushing_list, list) {
659 struct drm_gem_object *obj = obj_priv->obj;
661 if ((obj->write_domain & flush_domains) ==
663 obj->write_domain = 0;
664 i915_gem_object_move_to_active(obj, seqno);
670 if (was_empty && !dev_priv->mm.suspended)
671 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
676 * Command execution barrier
678 * Ensures that all commands in the ring are finished
679 * before signalling the CPU
682 i915_retire_commands(struct drm_device *dev)
684 drm_i915_private_t *dev_priv = dev->dev_private;
685 uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
686 uint32_t flush_domains = 0;
689 /* The sampler always gets flushed on i965 (sigh) */
691 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
694 OUT_RING(0); /* noop */
696 return flush_domains;
700 * Moves buffers associated only with the given active seqno from the active
701 * to inactive list, potentially freeing them.
704 i915_gem_retire_request(struct drm_device *dev,
705 struct drm_i915_gem_request *request)
707 drm_i915_private_t *dev_priv = dev->dev_private;
709 /* Move any buffers on the active list that are no longer referenced
710 * by the ringbuffer to the flushing/inactive lists as appropriate.
712 while (!list_empty(&dev_priv->mm.active_list)) {
713 struct drm_gem_object *obj;
714 struct drm_i915_gem_object *obj_priv;
716 obj_priv = list_first_entry(&dev_priv->mm.active_list,
717 struct drm_i915_gem_object,
721 /* If the seqno being retired doesn't match the oldest in the
722 * list, then the oldest in the list must still be newer than
725 if (obj_priv->last_rendering_seqno != request->seqno)
728 DRM_INFO("%s: retire %d moves to inactive list %p\n",
729 __func__, request->seqno, obj);
732 if (obj->write_domain != 0)
733 i915_gem_object_move_to_flushing(obj);
735 i915_gem_object_move_to_inactive(obj);
740 * Returns true if seq1 is later than seq2.
743 i915_seqno_passed(uint32_t seq1, uint32_t seq2)
745 return (int32_t)(seq1 - seq2) >= 0;
749 i915_get_gem_seqno(struct drm_device *dev)
751 drm_i915_private_t *dev_priv = dev->dev_private;
753 return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
757 * This function clears the request list as sequence numbers are passed.
760 i915_gem_retire_requests(struct drm_device *dev)
762 drm_i915_private_t *dev_priv = dev->dev_private;
765 seqno = i915_get_gem_seqno(dev);
767 while (!list_empty(&dev_priv->mm.request_list)) {
768 struct drm_i915_gem_request *request;
769 uint32_t retiring_seqno;
771 request = list_first_entry(&dev_priv->mm.request_list,
772 struct drm_i915_gem_request,
774 retiring_seqno = request->seqno;
776 if (i915_seqno_passed(seqno, retiring_seqno) ||
777 dev_priv->mm.wedged) {
778 i915_gem_retire_request(dev, request);
780 list_del(&request->list);
781 drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
788 i915_gem_retire_work_handler(struct work_struct *work)
790 drm_i915_private_t *dev_priv;
791 struct drm_device *dev;
793 dev_priv = container_of(work, drm_i915_private_t,
794 mm.retire_work.work);
797 mutex_lock(&dev->struct_mutex);
798 i915_gem_retire_requests(dev);
799 if (!dev_priv->mm.suspended &&
800 !list_empty(&dev_priv->mm.request_list))
801 schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
802 mutex_unlock(&dev->struct_mutex);
806 * Waits for a sequence number to be signaled, and cleans up the
807 * request and object lists appropriately for that event.
810 i915_wait_request(struct drm_device *dev, uint32_t seqno)
812 drm_i915_private_t *dev_priv = dev->dev_private;
817 if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
818 dev_priv->mm.waiting_gem_seqno = seqno;
819 i915_user_irq_get(dev);
820 ret = wait_event_interruptible(dev_priv->irq_queue,
821 i915_seqno_passed(i915_get_gem_seqno(dev),
823 dev_priv->mm.wedged);
824 i915_user_irq_put(dev);
825 dev_priv->mm.waiting_gem_seqno = 0;
827 if (dev_priv->mm.wedged)
830 if (ret && ret != -ERESTARTSYS)
831 DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
832 __func__, ret, seqno, i915_get_gem_seqno(dev));
834 /* Directly dispatch request retiring. While we have the work queue
835 * to handle this, the waiter on a request often wants an associated
836 * buffer to have made it to the inactive list, and we would need
837 * a separate wait queue to handle that.
840 i915_gem_retire_requests(dev);
846 i915_gem_flush(struct drm_device *dev,
847 uint32_t invalidate_domains,
848 uint32_t flush_domains)
850 drm_i915_private_t *dev_priv = dev->dev_private;
855 DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
856 invalidate_domains, flush_domains);
859 if (flush_domains & I915_GEM_DOMAIN_CPU)
860 drm_agp_chipset_flush(dev);
862 if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
863 I915_GEM_DOMAIN_GTT)) {
867 * I915_GEM_DOMAIN_RENDER is always invalidated, but is
868 * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
869 * also flushed at 2d versus 3d pipeline switches.
873 * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
874 * MI_READ_FLUSH is set, and is always flushed on 965.
876 * I915_GEM_DOMAIN_COMMAND may not exist?
878 * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
879 * invalidated when MI_EXE_FLUSH is set.
881 * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
882 * invalidated with every MI_FLUSH.
886 * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
887 * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
888 * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
889 * are flushed at any MI_FLUSH.
892 cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
893 if ((invalidate_domains|flush_domains) &
894 I915_GEM_DOMAIN_RENDER)
895 cmd &= ~MI_NO_WRITE_FLUSH;
896 if (!IS_I965G(dev)) {
898 * On the 965, the sampler cache always gets flushed
899 * and this bit is reserved.
901 if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
902 cmd |= MI_READ_FLUSH;
904 if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
908 DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
912 OUT_RING(0); /* noop */
918 * Ensures that all rendering to the object has completed and the object is
919 * safe to unbind from the GTT or access from the CPU.
922 i915_gem_object_wait_rendering(struct drm_gem_object *obj)
924 struct drm_device *dev = obj->dev;
925 struct drm_i915_gem_object *obj_priv = obj->driver_private;
928 /* If there are writes queued to the buffer, flush and
929 * create a new seqno to wait for.
931 if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) {
932 uint32_t seqno, write_domain = obj->write_domain;
934 DRM_INFO("%s: flushing object %p from write domain %08x\n",
935 __func__, obj, write_domain);
937 i915_gem_flush(dev, 0, write_domain);
939 seqno = i915_add_request(dev, write_domain);
940 i915_gem_object_move_to_active(obj, seqno);
942 DRM_INFO("%s: flush moves to exec list %p\n", __func__, obj);
946 /* If there is rendering queued on the buffer being evicted, wait for
949 if (obj_priv->active) {
951 DRM_INFO("%s: object %p wait for seqno %08x\n",
952 __func__, obj, obj_priv->last_rendering_seqno);
954 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
963 * Unbinds an object from the GTT aperture.
966 i915_gem_object_unbind(struct drm_gem_object *obj)
968 struct drm_device *dev = obj->dev;
969 struct drm_i915_gem_object *obj_priv = obj->driver_private;
973 DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
974 DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
976 if (obj_priv->gtt_space == NULL)
979 if (obj_priv->pin_count != 0) {
980 DRM_ERROR("Attempting to unbind pinned buffer\n");
984 /* Wait for any rendering to complete
986 ret = i915_gem_object_wait_rendering(obj);
988 DRM_ERROR("wait_rendering failed: %d\n", ret);
992 /* Move the object to the CPU domain to ensure that
993 * any possible CPU writes while it's not in the GTT
994 * are flushed when we go to remap it. This will
995 * also ensure that all pending GPU writes are finished
998 ret = i915_gem_object_set_domain(obj, I915_GEM_DOMAIN_CPU,
999 I915_GEM_DOMAIN_CPU);
1001 DRM_ERROR("set_domain failed: %d\n", ret);
1005 if (obj_priv->agp_mem != NULL) {
1006 drm_unbind_agp(obj_priv->agp_mem);
1007 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
1008 obj_priv->agp_mem = NULL;
1011 BUG_ON(obj_priv->active);
1013 i915_gem_object_free_page_list(obj);
1015 if (obj_priv->gtt_space) {
1016 atomic_dec(&dev->gtt_count);
1017 atomic_sub(obj->size, &dev->gtt_memory);
1019 drm_mm_put_block(obj_priv->gtt_space);
1020 obj_priv->gtt_space = NULL;
1023 /* Remove ourselves from the LRU list if present. */
1024 if (!list_empty(&obj_priv->list))
1025 list_del_init(&obj_priv->list);
1031 i915_gem_evict_something(struct drm_device *dev)
1033 drm_i915_private_t *dev_priv = dev->dev_private;
1034 struct drm_gem_object *obj;
1035 struct drm_i915_gem_object *obj_priv;
1039 /* If there's an inactive buffer available now, grab it
1042 if (!list_empty(&dev_priv->mm.inactive_list)) {
1043 obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
1044 struct drm_i915_gem_object,
1046 obj = obj_priv->obj;
1047 BUG_ON(obj_priv->pin_count != 0);
1049 DRM_INFO("%s: evicting %p\n", __func__, obj);
1051 BUG_ON(obj_priv->active);
1053 /* Wait on the rendering and unbind the buffer. */
1054 ret = i915_gem_object_unbind(obj);
1058 /* If we didn't get anything, but the ring is still processing
1059 * things, wait for one of those things to finish and hopefully
1060 * leave us a buffer to evict.
1062 if (!list_empty(&dev_priv->mm.request_list)) {
1063 struct drm_i915_gem_request *request;
1065 request = list_first_entry(&dev_priv->mm.request_list,
1066 struct drm_i915_gem_request,
1069 ret = i915_wait_request(dev, request->seqno);
1073 /* if waiting caused an object to become inactive,
1074 * then loop around and wait for it. Otherwise, we
1075 * assume that waiting freed and unbound something,
1076 * so there should now be some space in the GTT
1078 if (!list_empty(&dev_priv->mm.inactive_list))
1083 /* If we didn't have anything on the request list but there
1084 * are buffers awaiting a flush, emit one and try again.
1085 * When we wait on it, those buffers waiting for that flush
1086 * will get moved to inactive.
1088 if (!list_empty(&dev_priv->mm.flushing_list)) {
1089 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1090 struct drm_i915_gem_object,
1092 obj = obj_priv->obj;
1097 i915_add_request(dev, obj->write_domain);
1103 DRM_ERROR("inactive empty %d request empty %d "
1104 "flushing empty %d\n",
1105 list_empty(&dev_priv->mm.inactive_list),
1106 list_empty(&dev_priv->mm.request_list),
1107 list_empty(&dev_priv->mm.flushing_list));
1108 /* If we didn't do any of the above, there's nothing to be done
1109 * and we just can't fit it in.
1117 i915_gem_object_get_page_list(struct drm_gem_object *obj)
1119 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1121 struct address_space *mapping;
1122 struct inode *inode;
1126 if (obj_priv->page_list)
1129 /* Get the list of pages out of our struct file. They'll be pinned
1130 * at this point until we release them.
1132 page_count = obj->size / PAGE_SIZE;
1133 BUG_ON(obj_priv->page_list != NULL);
1134 obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
1136 if (obj_priv->page_list == NULL) {
1137 DRM_ERROR("Faled to allocate page list\n");
1141 inode = obj->filp->f_path.dentry->d_inode;
1142 mapping = inode->i_mapping;
1143 for (i = 0; i < page_count; i++) {
1144 page = read_mapping_page(mapping, i, NULL);
1146 ret = PTR_ERR(page);
1147 DRM_ERROR("read_mapping_page failed: %d\n", ret);
1148 i915_gem_object_free_page_list(obj);
1151 obj_priv->page_list[i] = page;
1157 * Finds free space in the GTT aperture and binds the object there.
1160 i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
1162 struct drm_device *dev = obj->dev;
1163 drm_i915_private_t *dev_priv = dev->dev_private;
1164 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1165 struct drm_mm_node *free_space;
1166 int page_count, ret;
1169 alignment = PAGE_SIZE;
1170 if (alignment & (PAGE_SIZE - 1)) {
1171 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
1176 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
1177 obj->size, alignment, 0);
1178 if (free_space != NULL) {
1179 obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
1181 if (obj_priv->gtt_space != NULL) {
1182 obj_priv->gtt_space->private = obj;
1183 obj_priv->gtt_offset = obj_priv->gtt_space->start;
1186 if (obj_priv->gtt_space == NULL) {
1187 /* If the gtt is empty and we're still having trouble
1188 * fitting our object in, we're out of memory.
1191 DRM_INFO("%s: GTT full, evicting something\n", __func__);
1193 if (list_empty(&dev_priv->mm.inactive_list) &&
1194 list_empty(&dev_priv->mm.flushing_list) &&
1195 list_empty(&dev_priv->mm.active_list)) {
1196 DRM_ERROR("GTT full, but LRU list empty\n");
1200 ret = i915_gem_evict_something(dev);
1202 DRM_ERROR("Failed to evict a buffer %d\n", ret);
1209 DRM_INFO("Binding object of size %d at 0x%08x\n",
1210 obj->size, obj_priv->gtt_offset);
1212 ret = i915_gem_object_get_page_list(obj);
1214 drm_mm_put_block(obj_priv->gtt_space);
1215 obj_priv->gtt_space = NULL;
1219 page_count = obj->size / PAGE_SIZE;
1220 /* Create an AGP memory structure pointing at our pages, and bind it
1223 obj_priv->agp_mem = drm_agp_bind_pages(dev,
1224 obj_priv->page_list,
1226 obj_priv->gtt_offset,
1227 obj_priv->agp_type);
1228 if (obj_priv->agp_mem == NULL) {
1229 i915_gem_object_free_page_list(obj);
1230 drm_mm_put_block(obj_priv->gtt_space);
1231 obj_priv->gtt_space = NULL;
1234 atomic_inc(&dev->gtt_count);
1235 atomic_add(obj->size, &dev->gtt_memory);
1237 /* Assert that the object is not currently in any GPU domain. As it
1238 * wasn't in the GTT, there shouldn't be any way it could have been in
1241 BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1242 BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
1248 i915_gem_clflush_object(struct drm_gem_object *obj)
1250 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1252 /* If we don't have a page list set up, then we're not pinned
1253 * to GPU, and we can ignore the cache flush because it'll happen
1254 * again at bind time.
1256 if (obj_priv->page_list == NULL)
1259 drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
1263 * Moves a single object to the GTT read, and possibly write domain.
1265 * This function returns when the move is complete, including waiting on
1269 i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
1271 struct drm_device *dev = obj->dev;
1272 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1273 uint32_t flush_domains;
1275 /* Figure out what GPU domains we need to flush or invalidate for
1278 flush_domains = obj->write_domain & I915_GEM_GPU_DOMAINS;
1280 /* Queue the GPU write cache flushing we need. */
1281 if (flush_domains != 0) {
1284 obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
1285 i915_gem_flush(dev, 0, flush_domains);
1286 seqno = i915_add_request(dev, flush_domains);
1287 i915_gem_object_move_to_active(obj, seqno);
1290 /* Wait on any GPU rendering and flushing to occur. */
1291 if (obj_priv->active) {
1294 ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
1299 /* If we're writing through the GTT domain, then CPU and GPU caches
1300 * will need to be invalidated at next use.
1303 obj->read_domains &= ~(I915_GEM_GPU_DOMAINS |
1304 I915_GEM_DOMAIN_CPU);
1306 /* Flush the CPU domain if it's dirty. */
1307 if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
1308 i915_gem_clflush_object(obj);
1309 drm_agp_chipset_flush(dev);
1311 obj->write_domain &= ~I915_GEM_DOMAIN_CPU;
1314 /* It should now be out of any other write domains, and we can update
1315 * the domain values for our changes.
1317 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
1318 obj->read_domains |= I915_GEM_DOMAIN_GTT;
1320 obj->write_domain = I915_GEM_DOMAIN_GTT;
1326 * Set the next domain for the specified object. This
1327 * may not actually perform the necessary flushing/invaliding though,
1328 * as that may want to be batched with other set_domain operations
1330 * This is (we hope) the only really tricky part of gem. The goal
1331 * is fairly simple -- track which caches hold bits of the object
1332 * and make sure they remain coherent. A few concrete examples may
1333 * help to explain how it works. For shorthand, we use the notation
1334 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
1335 * a pair of read and write domain masks.
1337 * Case 1: the batch buffer
1343 * 5. Unmapped from GTT
1346 * Let's take these a step at a time
1349 * Pages allocated from the kernel may still have
1350 * cache contents, so we set them to (CPU, CPU) always.
1351 * 2. Written by CPU (using pwrite)
1352 * The pwrite function calls set_domain (CPU, CPU) and
1353 * this function does nothing (as nothing changes)
1355 * This function asserts that the object is not
1356 * currently in any GPU-based read or write domains
1358 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
1359 * As write_domain is zero, this function adds in the
1360 * current read domains (CPU+COMMAND, 0).
1361 * flush_domains is set to CPU.
1362 * invalidate_domains is set to COMMAND
1363 * clflush is run to get data out of the CPU caches
1364 * then i915_dev_set_domain calls i915_gem_flush to
1365 * emit an MI_FLUSH and drm_agp_chipset_flush
1366 * 5. Unmapped from GTT
1367 * i915_gem_object_unbind calls set_domain (CPU, CPU)
1368 * flush_domains and invalidate_domains end up both zero
1369 * so no flushing/invalidating happens
1373 * Case 2: The shared render buffer
1377 * 3. Read/written by GPU
1378 * 4. set_domain to (CPU,CPU)
1379 * 5. Read/written by CPU
1380 * 6. Read/written by GPU
1383 * Same as last example, (CPU, CPU)
1385 * Nothing changes (assertions find that it is not in the GPU)
1386 * 3. Read/written by GPU
1387 * execbuffer calls set_domain (RENDER, RENDER)
1388 * flush_domains gets CPU
1389 * invalidate_domains gets GPU
1391 * MI_FLUSH and drm_agp_chipset_flush
1392 * 4. set_domain (CPU, CPU)
1393 * flush_domains gets GPU
1394 * invalidate_domains gets CPU
1395 * wait_rendering (obj) to make sure all drawing is complete.
1396 * This will include an MI_FLUSH to get the data from GPU
1398 * clflush (obj) to invalidate the CPU cache
1399 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
1400 * 5. Read/written by CPU
1401 * cache lines are loaded and dirtied
1402 * 6. Read written by GPU
1403 * Same as last GPU access
1405 * Case 3: The constant buffer
1410 * 4. Updated (written) by CPU again
1419 * flush_domains = CPU
1420 * invalidate_domains = RENDER
1423 * drm_agp_chipset_flush
1424 * 4. Updated (written) by CPU again
1426 * flush_domains = 0 (no previous write domain)
1427 * invalidate_domains = 0 (no new read domains)
1430 * flush_domains = CPU
1431 * invalidate_domains = RENDER
1434 * drm_agp_chipset_flush
1437 i915_gem_object_set_domain(struct drm_gem_object *obj,
1438 uint32_t read_domains,
1439 uint32_t write_domain)
1441 struct drm_device *dev = obj->dev;
1442 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1443 uint32_t invalidate_domains = 0;
1444 uint32_t flush_domains = 0;
1448 DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
1450 obj->read_domains, read_domains,
1451 obj->write_domain, write_domain);
1454 * If the object isn't moving to a new write domain,
1455 * let the object stay in multiple read domains
1457 if (write_domain == 0)
1458 read_domains |= obj->read_domains;
1460 obj_priv->dirty = 1;
1463 * Flush the current write domain if
1464 * the new read domains don't match. Invalidate
1465 * any read domains which differ from the old
1468 if (obj->write_domain && obj->write_domain != read_domains) {
1469 flush_domains |= obj->write_domain;
1470 invalidate_domains |= read_domains & ~obj->write_domain;
1473 * Invalidate any read caches which may have
1474 * stale data. That is, any new read domains.
1476 invalidate_domains |= read_domains & ~obj->read_domains;
1477 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
1479 DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
1480 __func__, flush_domains, invalidate_domains);
1483 * If we're invaliding the CPU cache and flushing a GPU cache,
1484 * then pause for rendering so that the GPU caches will be
1485 * flushed before the cpu cache is invalidated
1487 if ((invalidate_domains & I915_GEM_DOMAIN_CPU) &&
1488 (flush_domains & ~(I915_GEM_DOMAIN_CPU |
1489 I915_GEM_DOMAIN_GTT))) {
1490 ret = i915_gem_object_wait_rendering(obj);
1494 i915_gem_clflush_object(obj);
1497 if ((write_domain | flush_domains) != 0)
1498 obj->write_domain = write_domain;
1500 /* If we're invalidating the CPU domain, clear the per-page CPU
1501 * domain list as well.
1503 if (obj_priv->page_cpu_valid != NULL &&
1504 (write_domain != 0 ||
1505 read_domains & I915_GEM_DOMAIN_CPU)) {
1506 drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
1508 obj_priv->page_cpu_valid = NULL;
1510 obj->read_domains = read_domains;
1512 dev->invalidate_domains |= invalidate_domains;
1513 dev->flush_domains |= flush_domains;
1515 DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
1517 obj->read_domains, obj->write_domain,
1518 dev->invalidate_domains, dev->flush_domains);
1524 * Set the read/write domain on a range of the object.
1526 * Currently only implemented for CPU reads, otherwise drops to normal
1527 * i915_gem_object_set_domain().
1530 i915_gem_object_set_domain_range(struct drm_gem_object *obj,
1533 uint32_t read_domains,
1534 uint32_t write_domain)
1536 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1539 if (obj->read_domains & I915_GEM_DOMAIN_CPU)
1542 if (read_domains != I915_GEM_DOMAIN_CPU ||
1544 return i915_gem_object_set_domain(obj,
1545 read_domains, write_domain);
1547 /* Wait on any GPU rendering to the object to be flushed. */
1548 ret = i915_gem_object_wait_rendering(obj);
1552 if (obj_priv->page_cpu_valid == NULL) {
1553 obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
1557 /* Flush the cache on any pages that are still invalid from the CPU's
1560 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE; i++) {
1561 if (obj_priv->page_cpu_valid[i])
1564 drm_clflush_pages(obj_priv->page_list + i, 1);
1566 obj_priv->page_cpu_valid[i] = 1;
1573 * Once all of the objects have been set in the proper domain,
1574 * perform the necessary flush and invalidate operations.
1576 * Returns the write domains flushed, for use in flush tracking.
1579 i915_gem_dev_set_domain(struct drm_device *dev)
1581 uint32_t flush_domains = dev->flush_domains;
1584 * Now that all the buffers are synced to the proper domains,
1585 * flush and invalidate the collected domains
1587 if (dev->invalidate_domains | dev->flush_domains) {
1589 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
1591 dev->invalidate_domains,
1592 dev->flush_domains);
1595 dev->invalidate_domains,
1596 dev->flush_domains);
1597 dev->invalidate_domains = 0;
1598 dev->flush_domains = 0;
1601 return flush_domains;
1605 * Pin an object to the GTT and evaluate the relocations landing in it.
1608 i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
1609 struct drm_file *file_priv,
1610 struct drm_i915_gem_exec_object *entry)
1612 struct drm_device *dev = obj->dev;
1613 drm_i915_private_t *dev_priv = dev->dev_private;
1614 struct drm_i915_gem_relocation_entry reloc;
1615 struct drm_i915_gem_relocation_entry __user *relocs;
1616 struct drm_i915_gem_object *obj_priv = obj->driver_private;
1618 void __iomem *reloc_page;
1620 /* Choose the GTT offset for our buffer and put it there. */
1621 ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
1625 entry->offset = obj_priv->gtt_offset;
1627 relocs = (struct drm_i915_gem_relocation_entry __user *)
1628 (uintptr_t) entry->relocs_ptr;
1629 /* Apply the relocations, using the GTT aperture to avoid cache
1630 * flushing requirements.
1632 for (i = 0; i < entry->relocation_count; i++) {
1633 struct drm_gem_object *target_obj;
1634 struct drm_i915_gem_object *target_obj_priv;
1635 uint32_t reloc_val, reloc_offset;
1636 uint32_t __iomem *reloc_entry;
1638 ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
1640 i915_gem_object_unpin(obj);
1644 target_obj = drm_gem_object_lookup(obj->dev, file_priv,
1645 reloc.target_handle);
1646 if (target_obj == NULL) {
1647 i915_gem_object_unpin(obj);
1650 target_obj_priv = target_obj->driver_private;
1652 /* The target buffer should have appeared before us in the
1653 * exec_object list, so it should have a GTT space bound by now.
1655 if (target_obj_priv->gtt_space == NULL) {
1656 DRM_ERROR("No GTT space found for object %d\n",
1657 reloc.target_handle);
1658 drm_gem_object_unreference(target_obj);
1659 i915_gem_object_unpin(obj);
1663 if (reloc.offset > obj->size - 4) {
1664 DRM_ERROR("Relocation beyond object bounds: "
1665 "obj %p target %d offset %d size %d.\n",
1666 obj, reloc.target_handle,
1667 (int) reloc.offset, (int) obj->size);
1668 drm_gem_object_unreference(target_obj);
1669 i915_gem_object_unpin(obj);
1672 if (reloc.offset & 3) {
1673 DRM_ERROR("Relocation not 4-byte aligned: "
1674 "obj %p target %d offset %d.\n",
1675 obj, reloc.target_handle,
1676 (int) reloc.offset);
1677 drm_gem_object_unreference(target_obj);
1678 i915_gem_object_unpin(obj);
1682 if (reloc.write_domain && target_obj->pending_write_domain &&
1683 reloc.write_domain != target_obj->pending_write_domain) {
1684 DRM_ERROR("Write domain conflict: "
1685 "obj %p target %d offset %d "
1686 "new %08x old %08x\n",
1687 obj, reloc.target_handle,
1690 target_obj->pending_write_domain);
1691 drm_gem_object_unreference(target_obj);
1692 i915_gem_object_unpin(obj);
1697 DRM_INFO("%s: obj %p offset %08x target %d "
1698 "read %08x write %08x gtt %08x "
1699 "presumed %08x delta %08x\n",
1703 (int) reloc.target_handle,
1704 (int) reloc.read_domains,
1705 (int) reloc.write_domain,
1706 (int) target_obj_priv->gtt_offset,
1707 (int) reloc.presumed_offset,
1711 target_obj->pending_read_domains |= reloc.read_domains;
1712 target_obj->pending_write_domain |= reloc.write_domain;
1714 /* If the relocation already has the right value in it, no
1715 * more work needs to be done.
1717 if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
1718 drm_gem_object_unreference(target_obj);
1722 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1724 drm_gem_object_unreference(target_obj);
1725 i915_gem_object_unpin(obj);
1729 /* Map the page containing the relocation we're going to
1732 reloc_offset = obj_priv->gtt_offset + reloc.offset;
1733 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
1736 reloc_entry = (uint32_t __iomem *)(reloc_page +
1737 (reloc_offset & (PAGE_SIZE - 1)));
1738 reloc_val = target_obj_priv->gtt_offset + reloc.delta;
1741 DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
1742 obj, (unsigned int) reloc.offset,
1743 readl(reloc_entry), reloc_val);
1745 writel(reloc_val, reloc_entry);
1746 io_mapping_unmap_atomic(reloc_page);
1748 /* Write the updated presumed offset for this entry back out
1751 reloc.presumed_offset = target_obj_priv->gtt_offset;
1752 ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
1754 drm_gem_object_unreference(target_obj);
1755 i915_gem_object_unpin(obj);
1759 drm_gem_object_unreference(target_obj);
1764 i915_gem_dump_object(obj, 128, __func__, ~0);
1769 /** Dispatch a batchbuffer to the ring
1772 i915_dispatch_gem_execbuffer(struct drm_device *dev,
1773 struct drm_i915_gem_execbuffer *exec,
1774 uint64_t exec_offset)
1776 drm_i915_private_t *dev_priv = dev->dev_private;
1777 struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
1778 (uintptr_t) exec->cliprects_ptr;
1779 int nbox = exec->num_cliprects;
1781 uint32_t exec_start, exec_len;
1784 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
1785 exec_len = (uint32_t) exec->batch_len;
1787 if ((exec_start | exec_len) & 0x7) {
1788 DRM_ERROR("alignment\n");
1795 count = nbox ? nbox : 1;
1797 for (i = 0; i < count; i++) {
1799 int ret = i915_emit_box(dev, boxes, i,
1800 exec->DR1, exec->DR4);
1805 if (IS_I830(dev) || IS_845G(dev)) {
1807 OUT_RING(MI_BATCH_BUFFER);
1808 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
1809 OUT_RING(exec_start + exec_len - 4);
1814 if (IS_I965G(dev)) {
1815 OUT_RING(MI_BATCH_BUFFER_START |
1817 MI_BATCH_NON_SECURE_I965);
1818 OUT_RING(exec_start);
1820 OUT_RING(MI_BATCH_BUFFER_START |
1822 OUT_RING(exec_start | MI_BATCH_NON_SECURE);
1828 /* XXX breadcrumb */
1832 /* Throttle our rendering by waiting until the ring has completed our requests
1833 * emitted over 20 msec ago.
1835 * This should get us reasonable parallelism between CPU and GPU but also
1836 * relatively low latency when blocking on a particular request to finish.
1839 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
1841 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1845 mutex_lock(&dev->struct_mutex);
1846 seqno = i915_file_priv->mm.last_gem_throttle_seqno;
1847 i915_file_priv->mm.last_gem_throttle_seqno =
1848 i915_file_priv->mm.last_gem_seqno;
1850 ret = i915_wait_request(dev, seqno);
1851 mutex_unlock(&dev->struct_mutex);
1856 i915_gem_execbuffer(struct drm_device *dev, void *data,
1857 struct drm_file *file_priv)
1859 drm_i915_private_t *dev_priv = dev->dev_private;
1860 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1861 struct drm_i915_gem_execbuffer *args = data;
1862 struct drm_i915_gem_exec_object *exec_list = NULL;
1863 struct drm_gem_object **object_list = NULL;
1864 struct drm_gem_object *batch_obj;
1865 int ret, i, pinned = 0;
1866 uint64_t exec_offset;
1867 uint32_t seqno, flush_domains;
1870 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
1871 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
1874 if (args->buffer_count < 1) {
1875 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
1878 /* Copy in the exec list from userland */
1879 exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
1881 object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
1883 if (exec_list == NULL || object_list == NULL) {
1884 DRM_ERROR("Failed to allocate exec or object list "
1886 args->buffer_count);
1890 ret = copy_from_user(exec_list,
1891 (struct drm_i915_relocation_entry __user *)
1892 (uintptr_t) args->buffers_ptr,
1893 sizeof(*exec_list) * args->buffer_count);
1895 DRM_ERROR("copy %d exec entries failed %d\n",
1896 args->buffer_count, ret);
1900 mutex_lock(&dev->struct_mutex);
1902 i915_verify_inactive(dev, __FILE__, __LINE__);
1904 if (dev_priv->mm.wedged) {
1905 DRM_ERROR("Execbuf while wedged\n");
1906 mutex_unlock(&dev->struct_mutex);
1910 if (dev_priv->mm.suspended) {
1911 DRM_ERROR("Execbuf while VT-switched.\n");
1912 mutex_unlock(&dev->struct_mutex);
1916 /* Zero the gloabl flush/invalidate flags. These
1917 * will be modified as each object is bound to the
1920 dev->invalidate_domains = 0;
1921 dev->flush_domains = 0;
1923 /* Look up object handles and perform the relocations */
1924 for (i = 0; i < args->buffer_count; i++) {
1925 object_list[i] = drm_gem_object_lookup(dev, file_priv,
1926 exec_list[i].handle);
1927 if (object_list[i] == NULL) {
1928 DRM_ERROR("Invalid object handle %d at index %d\n",
1929 exec_list[i].handle, i);
1934 object_list[i]->pending_read_domains = 0;
1935 object_list[i]->pending_write_domain = 0;
1936 ret = i915_gem_object_pin_and_relocate(object_list[i],
1940 DRM_ERROR("object bind and relocate failed %d\n", ret);
1946 /* Set the pending read domains for the batch buffer to COMMAND */
1947 batch_obj = object_list[args->buffer_count-1];
1948 batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
1949 batch_obj->pending_write_domain = 0;
1951 i915_verify_inactive(dev, __FILE__, __LINE__);
1953 for (i = 0; i < args->buffer_count; i++) {
1954 struct drm_gem_object *obj = object_list[i];
1956 /* make sure all previous memory operations have passed */
1957 ret = i915_gem_object_set_domain(obj,
1958 obj->pending_read_domains,
1959 obj->pending_write_domain);
1961 /* As we've partially updated domains on our buffers,
1962 * we have to emit the flush we've accumulated
1963 * before exiting, or we'll have broken the
1964 * active/flushing/inactive invariants.
1966 * We'll potentially have some things marked as
1967 * being in write domains that they actually aren't,
1968 * but that should be merely a minor performance loss.
1970 flush_domains = i915_gem_dev_set_domain(dev);
1971 (void)i915_add_request(dev, flush_domains);
1976 i915_verify_inactive(dev, __FILE__, __LINE__);
1978 /* Flush/invalidate caches and chipset buffer */
1979 flush_domains = i915_gem_dev_set_domain(dev);
1981 i915_verify_inactive(dev, __FILE__, __LINE__);
1984 for (i = 0; i < args->buffer_count; i++) {
1985 i915_gem_object_check_coherency(object_list[i],
1986 exec_list[i].handle);
1990 exec_offset = exec_list[args->buffer_count - 1].offset;
1993 i915_gem_dump_object(object_list[args->buffer_count - 1],
1999 (void)i915_add_request(dev, flush_domains);
2001 /* Exec the batchbuffer */
2002 ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
2004 DRM_ERROR("dispatch failed %d\n", ret);
2009 * Ensure that the commands in the batch buffer are
2010 * finished before the interrupt fires
2012 flush_domains = i915_retire_commands(dev);
2014 i915_verify_inactive(dev, __FILE__, __LINE__);
2017 * Get a seqno representing the execution of the current buffer,
2018 * which we can wait on. We would like to mitigate these interrupts,
2019 * likely by only creating seqnos occasionally (so that we have
2020 * *some* interrupts representing completion of buffers that we can
2021 * wait on when trying to clear up gtt space).
2023 seqno = i915_add_request(dev, flush_domains);
2025 i915_file_priv->mm.last_gem_seqno = seqno;
2026 for (i = 0; i < args->buffer_count; i++) {
2027 struct drm_gem_object *obj = object_list[i];
2029 i915_gem_object_move_to_active(obj, seqno);
2031 DRM_INFO("%s: move to exec list %p\n", __func__, obj);
2035 i915_dump_lru(dev, __func__);
2038 i915_verify_inactive(dev, __FILE__, __LINE__);
2040 /* Copy the new buffer offsets back to the user's exec list. */
2041 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
2042 (uintptr_t) args->buffers_ptr,
2044 sizeof(*exec_list) * args->buffer_count);
2046 DRM_ERROR("failed to copy %d exec entries "
2047 "back to user (%d)\n",
2048 args->buffer_count, ret);
2050 if (object_list != NULL) {
2051 for (i = 0; i < pinned; i++)
2052 i915_gem_object_unpin(object_list[i]);
2054 for (i = 0; i < args->buffer_count; i++)
2055 drm_gem_object_unreference(object_list[i]);
2057 mutex_unlock(&dev->struct_mutex);
2060 drm_free(object_list, sizeof(*object_list) * args->buffer_count,
2062 drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
2069 i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
2071 struct drm_device *dev = obj->dev;
2072 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2075 i915_verify_inactive(dev, __FILE__, __LINE__);
2076 if (obj_priv->gtt_space == NULL) {
2077 ret = i915_gem_object_bind_to_gtt(obj, alignment);
2079 DRM_ERROR("Failure to bind: %d", ret);
2083 obj_priv->pin_count++;
2085 /* If the object is not active and not pending a flush,
2086 * remove it from the inactive list
2088 if (obj_priv->pin_count == 1) {
2089 atomic_inc(&dev->pin_count);
2090 atomic_add(obj->size, &dev->pin_memory);
2091 if (!obj_priv->active &&
2092 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2093 I915_GEM_DOMAIN_GTT)) == 0 &&
2094 !list_empty(&obj_priv->list))
2095 list_del_init(&obj_priv->list);
2097 i915_verify_inactive(dev, __FILE__, __LINE__);
2103 i915_gem_object_unpin(struct drm_gem_object *obj)
2105 struct drm_device *dev = obj->dev;
2106 drm_i915_private_t *dev_priv = dev->dev_private;
2107 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2109 i915_verify_inactive(dev, __FILE__, __LINE__);
2110 obj_priv->pin_count--;
2111 BUG_ON(obj_priv->pin_count < 0);
2112 BUG_ON(obj_priv->gtt_space == NULL);
2114 /* If the object is no longer pinned, and is
2115 * neither active nor being flushed, then stick it on
2118 if (obj_priv->pin_count == 0) {
2119 if (!obj_priv->active &&
2120 (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
2121 I915_GEM_DOMAIN_GTT)) == 0)
2122 list_move_tail(&obj_priv->list,
2123 &dev_priv->mm.inactive_list);
2124 atomic_dec(&dev->pin_count);
2125 atomic_sub(obj->size, &dev->pin_memory);
2127 i915_verify_inactive(dev, __FILE__, __LINE__);
2131 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
2132 struct drm_file *file_priv)
2134 struct drm_i915_gem_pin *args = data;
2135 struct drm_gem_object *obj;
2136 struct drm_i915_gem_object *obj_priv;
2139 mutex_lock(&dev->struct_mutex);
2141 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2143 DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
2145 mutex_unlock(&dev->struct_mutex);
2148 obj_priv = obj->driver_private;
2150 ret = i915_gem_object_pin(obj, args->alignment);
2152 drm_gem_object_unreference(obj);
2153 mutex_unlock(&dev->struct_mutex);
2157 /* XXX - flush the CPU caches for pinned objects
2158 * as the X server doesn't manage domains yet
2160 if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
2161 i915_gem_clflush_object(obj);
2162 drm_agp_chipset_flush(dev);
2163 obj->write_domain = 0;
2165 args->offset = obj_priv->gtt_offset;
2166 drm_gem_object_unreference(obj);
2167 mutex_unlock(&dev->struct_mutex);
2173 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
2174 struct drm_file *file_priv)
2176 struct drm_i915_gem_pin *args = data;
2177 struct drm_gem_object *obj;
2179 mutex_lock(&dev->struct_mutex);
2181 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2183 DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
2185 mutex_unlock(&dev->struct_mutex);
2189 i915_gem_object_unpin(obj);
2191 drm_gem_object_unreference(obj);
2192 mutex_unlock(&dev->struct_mutex);
2197 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
2198 struct drm_file *file_priv)
2200 struct drm_i915_gem_busy *args = data;
2201 struct drm_gem_object *obj;
2202 struct drm_i915_gem_object *obj_priv;
2204 mutex_lock(&dev->struct_mutex);
2205 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
2207 DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
2209 mutex_unlock(&dev->struct_mutex);
2213 obj_priv = obj->driver_private;
2214 args->busy = obj_priv->active;
2216 drm_gem_object_unreference(obj);
2217 mutex_unlock(&dev->struct_mutex);
2222 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
2223 struct drm_file *file_priv)
2225 return i915_gem_ring_throttle(dev, file_priv);
2228 int i915_gem_init_object(struct drm_gem_object *obj)
2230 struct drm_i915_gem_object *obj_priv;
2232 obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
2233 if (obj_priv == NULL)
2237 * We've just allocated pages from the kernel,
2238 * so they've just been written by the CPU with
2239 * zeros. They'll need to be clflushed before we
2240 * use them with the GPU.
2242 obj->write_domain = I915_GEM_DOMAIN_CPU;
2243 obj->read_domains = I915_GEM_DOMAIN_CPU;
2245 obj_priv->agp_type = AGP_USER_MEMORY;
2247 obj->driver_private = obj_priv;
2248 obj_priv->obj = obj;
2249 INIT_LIST_HEAD(&obj_priv->list);
2253 void i915_gem_free_object(struct drm_gem_object *obj)
2255 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2257 while (obj_priv->pin_count > 0)
2258 i915_gem_object_unpin(obj);
2260 i915_gem_object_unbind(obj);
2262 drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
2263 drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
2267 i915_gem_set_domain(struct drm_gem_object *obj,
2268 struct drm_file *file_priv,
2269 uint32_t read_domains,
2270 uint32_t write_domain)
2272 struct drm_device *dev = obj->dev;
2274 uint32_t flush_domains;
2276 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
2278 ret = i915_gem_object_set_domain(obj, read_domains, write_domain);
2281 flush_domains = i915_gem_dev_set_domain(obj->dev);
2283 if (flush_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT))
2284 (void) i915_add_request(dev, flush_domains);
2289 /** Unbinds all objects that are on the given buffer list. */
2291 i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
2293 struct drm_gem_object *obj;
2294 struct drm_i915_gem_object *obj_priv;
2297 while (!list_empty(head)) {
2298 obj_priv = list_first_entry(head,
2299 struct drm_i915_gem_object,
2301 obj = obj_priv->obj;
2303 if (obj_priv->pin_count != 0) {
2304 DRM_ERROR("Pinned object in unbind list\n");
2305 mutex_unlock(&dev->struct_mutex);
2309 ret = i915_gem_object_unbind(obj);
2311 DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
2313 mutex_unlock(&dev->struct_mutex);
2323 i915_gem_idle(struct drm_device *dev)
2325 drm_i915_private_t *dev_priv = dev->dev_private;
2326 uint32_t seqno, cur_seqno, last_seqno;
2329 mutex_lock(&dev->struct_mutex);
2331 if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
2332 mutex_unlock(&dev->struct_mutex);
2336 /* Hack! Don't let anybody do execbuf while we don't control the chip.
2337 * We need to replace this with a semaphore, or something.
2339 dev_priv->mm.suspended = 1;
2341 /* Cancel the retire work handler, wait for it to finish if running
2343 mutex_unlock(&dev->struct_mutex);
2344 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
2345 mutex_lock(&dev->struct_mutex);
2347 i915_kernel_lost_context(dev);
2349 /* Flush the GPU along with all non-CPU write domains
2351 i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
2352 ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
2353 seqno = i915_add_request(dev, ~(I915_GEM_DOMAIN_CPU |
2354 I915_GEM_DOMAIN_GTT));
2357 mutex_unlock(&dev->struct_mutex);
2361 dev_priv->mm.waiting_gem_seqno = seqno;
2365 cur_seqno = i915_get_gem_seqno(dev);
2366 if (i915_seqno_passed(cur_seqno, seqno))
2368 if (last_seqno == cur_seqno) {
2369 if (stuck++ > 100) {
2370 DRM_ERROR("hardware wedged\n");
2371 dev_priv->mm.wedged = 1;
2372 DRM_WAKEUP(&dev_priv->irq_queue);
2377 last_seqno = cur_seqno;
2379 dev_priv->mm.waiting_gem_seqno = 0;
2381 i915_gem_retire_requests(dev);
2383 if (!dev_priv->mm.wedged) {
2384 /* Active and flushing should now be empty as we've
2385 * waited for a sequence higher than any pending execbuffer
2387 WARN_ON(!list_empty(&dev_priv->mm.active_list));
2388 WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
2389 /* Request should now be empty as we've also waited
2390 * for the last request in the list
2392 WARN_ON(!list_empty(&dev_priv->mm.request_list));
2395 /* Empty the active and flushing lists to inactive. If there's
2396 * anything left at this point, it means that we're wedged and
2397 * nothing good's going to happen by leaving them there. So strip
2398 * the GPU domains and just stuff them onto inactive.
2400 while (!list_empty(&dev_priv->mm.active_list)) {
2401 struct drm_i915_gem_object *obj_priv;
2403 obj_priv = list_first_entry(&dev_priv->mm.active_list,
2404 struct drm_i915_gem_object,
2406 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
2407 i915_gem_object_move_to_inactive(obj_priv->obj);
2410 while (!list_empty(&dev_priv->mm.flushing_list)) {
2411 struct drm_i915_gem_object *obj_priv;
2413 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
2414 struct drm_i915_gem_object,
2416 obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
2417 i915_gem_object_move_to_inactive(obj_priv->obj);
2421 /* Move all inactive buffers out of the GTT. */
2422 ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
2423 WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
2425 mutex_unlock(&dev->struct_mutex);
2429 i915_gem_cleanup_ringbuffer(dev);
2430 mutex_unlock(&dev->struct_mutex);
2436 i915_gem_init_hws(struct drm_device *dev)
2438 drm_i915_private_t *dev_priv = dev->dev_private;
2439 struct drm_gem_object *obj;
2440 struct drm_i915_gem_object *obj_priv;
2443 /* If we need a physical address for the status page, it's already
2444 * initialized at driver load time.
2446 if (!I915_NEED_GFX_HWS(dev))
2449 obj = drm_gem_object_alloc(dev, 4096);
2451 DRM_ERROR("Failed to allocate status page\n");
2454 obj_priv = obj->driver_private;
2455 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
2457 ret = i915_gem_object_pin(obj, 4096);
2459 drm_gem_object_unreference(obj);
2463 dev_priv->status_gfx_addr = obj_priv->gtt_offset;
2465 dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
2466 if (dev_priv->hw_status_page == NULL) {
2467 DRM_ERROR("Failed to map status page.\n");
2468 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
2469 drm_gem_object_unreference(obj);
2472 dev_priv->hws_obj = obj;
2473 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
2474 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
2475 I915_READ(HWS_PGA); /* posting read */
2476 DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
2482 i915_gem_init_ringbuffer(struct drm_device *dev)
2484 drm_i915_private_t *dev_priv = dev->dev_private;
2485 struct drm_gem_object *obj;
2486 struct drm_i915_gem_object *obj_priv;
2490 ret = i915_gem_init_hws(dev);
2494 obj = drm_gem_object_alloc(dev, 128 * 1024);
2496 DRM_ERROR("Failed to allocate ringbuffer\n");
2499 obj_priv = obj->driver_private;
2501 ret = i915_gem_object_pin(obj, 4096);
2503 drm_gem_object_unreference(obj);
2507 /* Set up the kernel mapping for the ring. */
2508 dev_priv->ring.Size = obj->size;
2509 dev_priv->ring.tail_mask = obj->size - 1;
2511 dev_priv->ring.map.offset = dev->agp->base + obj_priv->gtt_offset;
2512 dev_priv->ring.map.size = obj->size;
2513 dev_priv->ring.map.type = 0;
2514 dev_priv->ring.map.flags = 0;
2515 dev_priv->ring.map.mtrr = 0;
2517 drm_core_ioremap_wc(&dev_priv->ring.map, dev);
2518 if (dev_priv->ring.map.handle == NULL) {
2519 DRM_ERROR("Failed to map ringbuffer.\n");
2520 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
2521 drm_gem_object_unreference(obj);
2524 dev_priv->ring.ring_obj = obj;
2525 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
2527 /* Stop the ring if it's running. */
2528 I915_WRITE(PRB0_CTL, 0);
2529 I915_WRITE(PRB0_TAIL, 0);
2530 I915_WRITE(PRB0_HEAD, 0);
2532 /* Initialize the ring. */
2533 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
2534 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
2536 /* G45 ring initialization fails to reset head to zero */
2538 DRM_ERROR("Ring head not reset to zero "
2539 "ctl %08x head %08x tail %08x start %08x\n",
2540 I915_READ(PRB0_CTL),
2541 I915_READ(PRB0_HEAD),
2542 I915_READ(PRB0_TAIL),
2543 I915_READ(PRB0_START));
2544 I915_WRITE(PRB0_HEAD, 0);
2546 DRM_ERROR("Ring head forced to zero "
2547 "ctl %08x head %08x tail %08x start %08x\n",
2548 I915_READ(PRB0_CTL),
2549 I915_READ(PRB0_HEAD),
2550 I915_READ(PRB0_TAIL),
2551 I915_READ(PRB0_START));
2554 I915_WRITE(PRB0_CTL,
2555 ((obj->size - 4096) & RING_NR_PAGES) |
2559 head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
2561 /* If the head is still not zero, the ring is dead */
2563 DRM_ERROR("Ring initialization failed "
2564 "ctl %08x head %08x tail %08x start %08x\n",
2565 I915_READ(PRB0_CTL),
2566 I915_READ(PRB0_HEAD),
2567 I915_READ(PRB0_TAIL),
2568 I915_READ(PRB0_START));
2572 /* Update our cache of the ring state */
2573 i915_kernel_lost_context(dev);
2579 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
2581 drm_i915_private_t *dev_priv = dev->dev_private;
2583 if (dev_priv->ring.ring_obj == NULL)
2586 drm_core_ioremapfree(&dev_priv->ring.map, dev);
2588 i915_gem_object_unpin(dev_priv->ring.ring_obj);
2589 drm_gem_object_unreference(dev_priv->ring.ring_obj);
2590 dev_priv->ring.ring_obj = NULL;
2591 memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
2593 if (dev_priv->hws_obj != NULL) {
2594 struct drm_gem_object *obj = dev_priv->hws_obj;
2595 struct drm_i915_gem_object *obj_priv = obj->driver_private;
2597 kunmap(obj_priv->page_list[0]);
2598 i915_gem_object_unpin(obj);
2599 drm_gem_object_unreference(obj);
2600 dev_priv->hws_obj = NULL;
2601 memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
2602 dev_priv->hw_status_page = NULL;
2604 /* Write high address into HWS_PGA when disabling. */
2605 I915_WRITE(HWS_PGA, 0x1ffff000);
2610 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
2611 struct drm_file *file_priv)
2613 drm_i915_private_t *dev_priv = dev->dev_private;
2616 if (dev_priv->mm.wedged) {
2617 DRM_ERROR("Reenabling wedged hardware, good luck\n");
2618 dev_priv->mm.wedged = 0;
2621 ret = i915_gem_init_ringbuffer(dev);
2625 dev_priv->mm.gtt_mapping = io_mapping_create_wc(dev->agp->base,
2626 dev->agp->agp_info.aper_size
2629 mutex_lock(&dev->struct_mutex);
2630 BUG_ON(!list_empty(&dev_priv->mm.active_list));
2631 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
2632 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
2633 BUG_ON(!list_empty(&dev_priv->mm.request_list));
2634 dev_priv->mm.suspended = 0;
2635 mutex_unlock(&dev->struct_mutex);
2637 drm_irq_install(dev);
2643 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
2644 struct drm_file *file_priv)
2646 drm_i915_private_t *dev_priv = dev->dev_private;
2649 ret = i915_gem_idle(dev);
2650 drm_irq_uninstall(dev);
2652 io_mapping_free(dev_priv->mm.gtt_mapping);
2657 i915_gem_lastclose(struct drm_device *dev)
2661 ret = i915_gem_idle(dev);
2663 DRM_ERROR("failed to idle hardware: %d\n", ret);
2667 i915_gem_load(struct drm_device *dev)
2669 drm_i915_private_t *dev_priv = dev->dev_private;
2671 INIT_LIST_HEAD(&dev_priv->mm.active_list);
2672 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
2673 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
2674 INIT_LIST_HEAD(&dev_priv->mm.request_list);
2675 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
2676 i915_gem_retire_work_handler);
2677 dev_priv->mm.next_gem_seqno = 1;
2679 i915_gem_detect_bit_6_swizzle(dev);