1 /**************************************************************************
3 * Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
34 * Locking may look a bit complicated but isn't really:
36 * The buffer usage atomic_t needs to be protected by dev->struct_mutex
37 * when there is a chance that it can be zero before or after the operation.
39 * dev->struct_mutex also protects all lists and list heads. Hash tables and hash
42 * bo->mutex protects the buffer object itself excluding the usage field.
43 * bo->mutex does also protect the buffer list heads, so to manipulate those, we need
44 * both the bo->mutex and the dev->struct_mutex.
46 * Locking order is bo->mutex, dev->struct_mutex. Therefore list traversal is a bit
47 * complicated. When dev->struct_mutex is released to grab bo->mutex, the list
48 * traversal will, in general, need to be restarted.
52 static void drm_bo_destroy_locked(drm_buffer_object_t * bo);
53 static int drm_bo_setup_vm_locked(drm_buffer_object_t * bo);
54 static void drm_bo_takedown_vm_locked(drm_buffer_object_t * bo);
55 static void drm_bo_unmap_virtual(drm_buffer_object_t * bo);
57 static inline uint32_t drm_bo_type_flags(unsigned type)
59 return (1 << (24 + type));
63 * bo locked. dev->struct_mutex locked.
66 void drm_bo_add_to_pinned_lru(drm_buffer_object_t * bo)
68 drm_mem_type_manager_t *man;
70 man = &bo->dev->bm.man[bo->pinned_mem_type];
71 list_add_tail(&bo->pinned_lru, &man->pinned);
74 void drm_bo_add_to_lru(drm_buffer_object_t * bo)
76 drm_mem_type_manager_t *man;
78 if (!(bo->mem.mask & (DRM_BO_FLAG_NO_MOVE | DRM_BO_FLAG_NO_EVICT))) {
79 man = &bo->dev->bm.man[bo->mem.mem_type];
80 list_add_tail(&bo->lru, &man->lru);
82 INIT_LIST_HEAD(&bo->lru);
86 static int drm_bo_vm_pre_move(drm_buffer_object_t * bo, int old_is_pci)
88 #ifdef DRM_ODD_MM_COMPAT
91 ret = drm_bo_lock_kmm(bo);
94 drm_bo_unmap_virtual(bo);
96 drm_bo_finish_unmap(bo);
98 drm_bo_unmap_virtual(bo);
103 static void drm_bo_vm_post_move(drm_buffer_object_t * bo)
105 #ifdef DRM_ODD_MM_COMPAT
108 ret = drm_bo_remap_bound(bo);
110 DRM_ERROR("Failed to remap a bound buffer object.\n"
111 "\tThis might cause a sigbus later.\n");
113 drm_bo_unlock_kmm(bo);
118 * Call bo->mutex locked.
121 static int drm_bo_add_ttm(drm_buffer_object_t * bo)
123 drm_device_t *dev = bo->dev;
129 bo->ttm = drm_ttm_init(dev, bo->mem.num_pages << PAGE_SHIFT);
133 case drm_bo_type_user:
134 case drm_bo_type_fake:
137 DRM_ERROR("Illegal buffer object type\n");
145 static int drm_bo_handle_move_mem(drm_buffer_object_t * bo,
146 drm_bo_mem_reg_t * mem,
147 int evict, int no_wait)
149 drm_device_t *dev = bo->dev;
150 drm_buffer_manager_t *bm = &dev->bm;
151 int old_is_pci = drm_mem_reg_is_pci(dev, &bo->mem);
152 int new_is_pci = drm_mem_reg_is_pci(dev, mem);
153 drm_mem_type_manager_t *old_man = &bm->man[bo->mem.mem_type];
154 drm_mem_type_manager_t *new_man = &bm->man[mem->mem_type];
157 if (old_is_pci || new_is_pci)
158 ret = drm_bo_vm_pre_move(bo, old_is_pci);
163 * Create and bind a ttm if required.
166 if (!(new_man->flags & _DRM_FLAG_MEMTYPE_FIXED) && (bo->ttm == NULL)) {
167 ret = drm_bo_add_ttm(bo);
171 if (mem->mem_type != DRM_BO_MEM_LOCAL) {
172 ret = drm_bind_ttm(bo->ttm, new_man->flags &
174 mem->mm_node->start);
180 if ((bo->mem.mem_type == DRM_BO_MEM_LOCAL) && bo->ttm == NULL) {
182 drm_bo_mem_reg_t *old_mem = &bo->mem;
183 uint32_t save_flags = old_mem->flags;
184 uint32_t save_mask = old_mem->mask;
188 old_mem->mask = save_mask;
189 DRM_FLAG_MASKED(save_flags, mem->flags, DRM_BO_MASK_MEMTYPE);
191 } else if (!(old_man->flags & _DRM_FLAG_MEMTYPE_FIXED) &&
192 !(new_man->flags & _DRM_FLAG_MEMTYPE_FIXED)) {
194 ret = drm_bo_move_ttm(bo, evict, no_wait, mem);
196 } else if (dev->driver->bo_driver->move) {
197 ret = dev->driver->bo_driver->move(bo, evict, no_wait, mem);
201 ret = drm_bo_move_memcpy(bo, evict, no_wait, mem);
208 if (old_is_pci || new_is_pci)
209 drm_bo_vm_post_move(bo);
211 if (bo->priv_flags & _DRM_BO_FLAG_EVICTED) {
213 dev->driver->bo_driver->invalidate_caches(dev,
216 DRM_ERROR("Can not flush read caches\n");
219 DRM_FLAG_MASKED(bo->priv_flags,
220 (evict) ? _DRM_BO_FLAG_EVICTED : 0,
221 _DRM_BO_FLAG_EVICTED);
224 bo->offset = bo->mem.mm_node->start << PAGE_SHIFT;
229 if (old_is_pci || new_is_pci)
230 drm_bo_vm_post_move(bo);
232 new_man = &bm->man[bo->mem.mem_type];
233 if ((new_man->flags & _DRM_FLAG_MEMTYPE_FIXED) && bo->ttm) {
234 drm_ttm_unbind(bo->ttm);
235 drm_destroy_ttm(bo->ttm);
243 * Call bo->mutex locked.
244 * Wait until the buffer is idle.
247 int drm_bo_wait(drm_buffer_object_t * bo, int lazy, int ignore_signals,
251 drm_fence_object_t *fence = bo->fence;
255 drm_device_t *dev = bo->dev;
256 if (drm_fence_object_signaled(fence, bo->fence_type)) {
257 drm_fence_usage_deref_unlocked(dev, fence);
265 drm_fence_object_wait(dev, fence, lazy, ignore_signals,
270 drm_fence_usage_deref_unlocked(dev, fence);
277 static int drm_bo_expire_fence(drm_buffer_object_t * bo, int allow_errors)
279 drm_device_t *dev = bo->dev;
280 drm_buffer_manager_t *bm = &dev->bm;
284 unsigned long _end = jiffies + 3 * DRM_HZ;
287 ret = drm_bo_wait(bo, 0, 1, 0);
288 if (ret && allow_errors)
291 } while (ret && !time_after_eq(jiffies, _end));
295 DRM_ERROR("Detected GPU lockup or "
296 "fence driver was taken down. "
297 "Evicting buffer.\n");
301 drm_fence_usage_deref_unlocked(dev, bo->fence);
309 * Call dev->struct_mutex locked.
310 * Attempts to remove all private references to a buffer by expiring its
311 * fence object and removing from lru lists and memory managers.
314 static void drm_bo_cleanup_refs(drm_buffer_object_t * bo, int remove_all)
316 drm_device_t *dev = bo->dev;
317 drm_buffer_manager_t *bm = &dev->bm;
319 atomic_inc(&bo->usage);
320 mutex_unlock(&dev->struct_mutex);
321 mutex_lock(&bo->mutex);
323 DRM_FLAG_MASKED(bo->priv_flags, 0, _DRM_BO_FLAG_UNFENCED);
325 if (bo->fence && drm_fence_object_signaled(bo->fence, bo->fence_type)) {
326 drm_fence_usage_deref_locked(dev, bo->fence);
330 if (bo->fence && remove_all)
331 (void)drm_bo_expire_fence(bo, 0);
333 mutex_lock(&dev->struct_mutex);
335 if (!atomic_dec_and_test(&bo->usage)) {
340 list_del_init(&bo->lru);
341 if (bo->mem.mm_node) {
342 drm_mm_put_block(bo->mem.mm_node);
343 if (bo->pinned_node == bo->mem.mm_node)
344 bo->pinned_node = NULL;
345 bo->mem.mm_node = NULL;
347 list_del_init(&bo->pinned_lru);
348 if (bo->pinned_node) {
349 drm_mm_put_block(bo->pinned_node);
350 bo->pinned_node = NULL;
352 list_del_init(&bo->ddestroy);
353 mutex_unlock(&bo->mutex);
354 drm_bo_destroy_locked(bo);
358 if (list_empty(&bo->ddestroy)) {
359 drm_fence_object_flush(dev, bo->fence, bo->fence_type);
360 list_add_tail(&bo->ddestroy, &bm->ddestroy);
361 schedule_delayed_work(&bm->wq,
362 ((DRM_HZ / 100) < 1) ? 1 : DRM_HZ / 100);
366 mutex_unlock(&bo->mutex);
371 * Verify that refcount is 0 and that there are no internal references
372 * to the buffer object. Then destroy it.
375 static void drm_bo_destroy_locked(drm_buffer_object_t * bo)
377 drm_device_t *dev = bo->dev;
378 drm_buffer_manager_t *bm = &dev->bm;
380 if (list_empty(&bo->lru) && bo->mem.mm_node == NULL &&
381 list_empty(&bo->pinned_lru) && bo->pinned_node == NULL &&
382 list_empty(&bo->ddestroy) && atomic_read(&bo->usage) == 0) {
383 if (bo->fence != NULL) {
384 DRM_ERROR("Fence was non-zero.\n");
385 drm_bo_cleanup_refs(bo, 0);
389 #ifdef DRM_ODD_MM_COMPAT
390 BUG_ON(!list_empty(&bo->vma_list));
391 BUG_ON(!list_empty(&bo->p_mm_list));
395 drm_ttm_unbind(bo->ttm);
396 drm_destroy_ttm(bo->ttm);
400 atomic_dec(&bm->count);
402 drm_ctl_free(bo, sizeof(*bo), DRM_MEM_BUFOBJ);
408 * Some stuff is still trying to reference the buffer object.
409 * Get rid of those references.
412 drm_bo_cleanup_refs(bo, 0);
418 * Call dev->struct_mutex locked.
421 static void drm_bo_delayed_delete(drm_device_t * dev, int remove_all)
423 drm_buffer_manager_t *bm = &dev->bm;
425 drm_buffer_object_t *entry, *nentry;
426 struct list_head *list, *next;
428 list_for_each_safe(list, next, &bm->ddestroy) {
429 entry = list_entry(list, drm_buffer_object_t, ddestroy);
432 if (next != &bm->ddestroy) {
433 nentry = list_entry(next, drm_buffer_object_t,
435 atomic_inc(&nentry->usage);
438 drm_bo_cleanup_refs(entry, remove_all);
441 atomic_dec(&nentry->usage);
446 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
447 static void drm_bo_delayed_workqueue(void *data)
449 static void drm_bo_delayed_workqueue(struct work_struct *work)
452 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
453 drm_device_t *dev = (drm_device_t *) data;
454 drm_buffer_manager_t *bm = &dev->bm;
456 drm_buffer_manager_t *bm =
457 container_of(work, drm_buffer_manager_t, wq.work);
458 drm_device_t *dev = container_of(bm, drm_device_t, bm);
461 DRM_DEBUG("Delayed delete Worker\n");
463 mutex_lock(&dev->struct_mutex);
464 if (!bm->initialized) {
465 mutex_unlock(&dev->struct_mutex);
468 drm_bo_delayed_delete(dev, 0);
469 if (bm->initialized && !list_empty(&bm->ddestroy)) {
470 schedule_delayed_work(&bm->wq,
471 ((DRM_HZ / 100) < 1) ? 1 : DRM_HZ / 100);
473 mutex_unlock(&dev->struct_mutex);
476 void drm_bo_usage_deref_locked(drm_buffer_object_t * bo)
478 if (atomic_dec_and_test(&bo->usage)) {
479 drm_bo_destroy_locked(bo);
483 static void drm_bo_base_deref_locked(drm_file_t * priv, drm_user_object_t * uo)
485 drm_buffer_object_t *bo =
486 drm_user_object_entry(uo, drm_buffer_object_t, base);
488 drm_bo_takedown_vm_locked(bo);
489 drm_bo_usage_deref_locked(bo);
492 static void drm_bo_usage_deref_unlocked(drm_buffer_object_t * bo)
494 drm_device_t *dev = bo->dev;
496 if (atomic_dec_and_test(&bo->usage)) {
497 mutex_lock(&dev->struct_mutex);
498 if (atomic_read(&bo->usage) == 0)
499 drm_bo_destroy_locked(bo);
500 mutex_unlock(&dev->struct_mutex);
505 * Note. The caller has to register (if applicable)
506 * and deregister fence object usage.
509 int drm_fence_buffer_objects(drm_file_t * priv,
510 struct list_head *list,
511 uint32_t fence_flags,
512 drm_fence_object_t * fence,
513 drm_fence_object_t ** used_fence)
515 drm_device_t *dev = priv->head->dev;
516 drm_buffer_manager_t *bm = &dev->bm;
518 drm_buffer_object_t *entry;
519 uint32_t fence_type = 0;
525 mutex_lock(&dev->struct_mutex);
528 list = &bm->unfenced;
530 list_for_each_entry(entry, list, lru) {
531 BUG_ON(!(entry->priv_flags & _DRM_BO_FLAG_UNFENCED));
532 fence_type |= entry->fence_type;
533 if (entry->fence_class != 0) {
534 DRM_ERROR("Fence class %d is not implemented yet.\n",
548 * Transfer to a local list before we release the dev->struct_mutex;
549 * This is so we don't get any new unfenced objects while fencing
550 * the ones we already have..
553 list_splice_init(list, &f_list);
556 if ((fence_type & fence->type) != fence_type) {
557 DRM_ERROR("Given fence doesn't match buffers "
558 "on unfenced list.\n");
563 mutex_unlock(&dev->struct_mutex);
564 ret = drm_fence_object_create(dev, fence_type,
565 fence_flags | DRM_FENCE_FLAG_EMIT,
567 mutex_lock(&dev->struct_mutex);
574 while (l != &f_list) {
576 entry = list_entry(l, drm_buffer_object_t, lru);
577 atomic_inc(&entry->usage);
578 mutex_unlock(&dev->struct_mutex);
579 mutex_lock(&entry->mutex);
580 mutex_lock(&dev->struct_mutex);
582 if (entry->priv_flags & _DRM_BO_FLAG_UNFENCED) {
585 drm_fence_usage_deref_locked(dev, entry->fence);
586 entry->fence = fence;
587 DRM_FLAG_MASKED(entry->priv_flags, 0,
588 _DRM_BO_FLAG_UNFENCED);
589 DRM_WAKEUP(&entry->event_queue);
590 drm_bo_add_to_lru(entry);
592 mutex_unlock(&entry->mutex);
593 drm_bo_usage_deref_locked(entry);
596 atomic_add(count, &fence->usage);
597 DRM_DEBUG("Fenced %d buffers\n", count);
599 mutex_unlock(&dev->struct_mutex);
604 EXPORT_SYMBOL(drm_fence_buffer_objects);
610 static int drm_bo_evict(drm_buffer_object_t * bo, unsigned mem_type,
614 drm_device_t *dev = bo->dev;
615 drm_bo_mem_reg_t evict_mem;
618 * Someone might have modified the buffer before we took the buffer mutex.
621 if (bo->priv_flags & _DRM_BO_FLAG_UNFENCED)
623 if (bo->mem.mem_type != mem_type)
626 ret = drm_bo_wait(bo, 0, 0, no_wait);
628 if (ret && ret != -EAGAIN) {
629 DRM_ERROR("Failed to expire fence before "
630 "buffer eviction.\n");
635 evict_mem.mm_node = NULL;
637 if (bo->type == drm_bo_type_fake) {
638 bo->mem.mem_type = DRM_BO_MEM_LOCAL;
639 bo->mem.mm_node = NULL;
644 evict_mem.mask = dev->driver->bo_driver->evict_flags(dev, mem_type);
645 ret = drm_bo_mem_space(bo, &evict_mem, no_wait);
649 DRM_ERROR("Failed to find memory space for "
650 "buffer 0x%p eviction.\n", bo);
654 ret = drm_bo_handle_move_mem(bo, &evict_mem, 1, no_wait);
658 DRM_ERROR("Buffer eviction failed\n");
663 mutex_lock(&dev->struct_mutex);
664 if (evict_mem.mm_node) {
665 if (evict_mem.mm_node != bo->pinned_node)
666 drm_mm_put_block(evict_mem.mm_node);
667 evict_mem.mm_node = NULL;
670 drm_bo_add_to_lru(bo);
671 mutex_unlock(&dev->struct_mutex);
673 DRM_FLAG_MASKED(bo->priv_flags, _DRM_BO_FLAG_EVICTED,
674 _DRM_BO_FLAG_EVICTED);
680 static int drm_bo_mem_force_space(drm_device_t * dev,
681 drm_bo_mem_reg_t * mem,
682 uint32_t mem_type, int no_wait)
685 drm_buffer_manager_t *bm = &dev->bm;
686 drm_buffer_object_t *entry;
687 drm_mem_type_manager_t *man = &bm->man[mem_type];
688 struct list_head *lru;
689 unsigned long num_pages = mem->num_pages;
692 mutex_lock(&dev->struct_mutex);
694 node = drm_mm_search_free(&man->manager, num_pages,
695 mem->page_alignment, 1);
700 if (lru->next == lru)
703 entry = list_entry(lru->next, drm_buffer_object_t, lru);
704 atomic_inc(&entry->usage);
705 mutex_unlock(&dev->struct_mutex);
706 mutex_lock(&entry->mutex);
707 BUG_ON(entry->mem.flags & (DRM_BO_FLAG_NO_MOVE | DRM_BO_FLAG_NO_EVICT));
709 ret = drm_bo_evict(entry, mem_type, no_wait);
710 mutex_unlock(&entry->mutex);
711 drm_bo_usage_deref_unlocked(entry);
714 mutex_lock(&dev->struct_mutex);
718 mutex_unlock(&dev->struct_mutex);
722 node = drm_mm_get_block(node, num_pages, mem->page_alignment);
723 mutex_unlock(&dev->struct_mutex);
725 mem->mem_type = mem_type;
729 static int drm_bo_mt_compatible(drm_mem_type_manager_t * man,
731 uint32_t mask, uint32_t * res_mask)
733 uint32_t cur_flags = drm_bo_type_flags(mem_type);
736 if (man->flags & _DRM_FLAG_MEMTYPE_CACHED)
737 cur_flags |= DRM_BO_FLAG_CACHED;
738 if (man->flags & _DRM_FLAG_MEMTYPE_MAPPABLE)
739 cur_flags |= DRM_BO_FLAG_MAPPABLE;
740 if (man->flags & _DRM_FLAG_MEMTYPE_CSELECT)
741 DRM_FLAG_MASKED(cur_flags, mask, DRM_BO_FLAG_CACHED);
743 if ((cur_flags & mask & DRM_BO_MASK_MEM) == 0)
746 if (mem_type == DRM_BO_MEM_LOCAL) {
747 *res_mask = cur_flags;
751 flag_diff = (mask ^ cur_flags);
752 if ((flag_diff & DRM_BO_FLAG_CACHED) &&
753 (!(mask & DRM_BO_FLAG_CACHED) ||
754 (mask & DRM_BO_FLAG_FORCE_CACHING)))
757 if ((flag_diff & DRM_BO_FLAG_MAPPABLE) &&
758 ((mask & DRM_BO_FLAG_MAPPABLE) ||
759 (mask & DRM_BO_FLAG_FORCE_MAPPABLE)) )
762 *res_mask = cur_flags;
766 int drm_bo_mem_space(drm_buffer_object_t * bo,
767 drm_bo_mem_reg_t * mem, int no_wait)
769 drm_device_t *dev = bo->dev;
770 drm_buffer_manager_t *bm = &dev->bm;
771 drm_mem_type_manager_t *man;
773 uint32_t num_prios = dev->driver->bo_driver->num_mem_type_prio;
774 const uint32_t *prios = dev->driver->bo_driver->mem_type_prio;
776 uint32_t mem_type = DRM_BO_MEM_LOCAL;
781 drm_mm_node_t *node = NULL;
785 for (i = 0; i < num_prios; ++i) {
787 man = &bm->man[mem_type];
789 type_ok = drm_bo_mt_compatible(man, mem_type, mem->mask,
795 if (mem_type == DRM_BO_MEM_LOCAL)
798 if ((mem_type == bo->pinned_mem_type) &&
799 (bo->pinned_node != NULL)) {
800 node = bo->pinned_node;
804 mutex_lock(&dev->struct_mutex);
805 if (man->has_type && man->use_type) {
807 node = drm_mm_search_free(&man->manager, mem->num_pages,
808 mem->page_alignment, 1);
810 node = drm_mm_get_block(node, mem->num_pages,
811 mem->page_alignment);
813 mutex_unlock(&dev->struct_mutex);
818 if ((type_ok && (mem_type == DRM_BO_MEM_LOCAL)) || node) {
820 mem->mem_type = mem_type;
821 mem->flags = cur_flags;
828 num_prios = dev->driver->bo_driver->num_mem_busy_prio;
829 prios = dev->driver->bo_driver->mem_busy_prio;
831 for (i = 0; i < num_prios; ++i) {
833 man = &bm->man[mem_type];
835 if (!drm_bo_mt_compatible(man, mem_type, mem->mask, &cur_flags))
838 ret = drm_bo_mem_force_space(dev, mem, mem_type, no_wait);
841 mem->flags = cur_flags;
849 ret = (has_eagain) ? -EAGAIN : -ENOMEM;
853 EXPORT_SYMBOL(drm_bo_mem_space);
855 static int drm_bo_new_mask(drm_buffer_object_t * bo,
856 uint32_t new_mask, uint32_t hint)
860 if (bo->type == drm_bo_type_user) {
861 DRM_ERROR("User buffers are not supported yet\n");
864 if (bo->type == drm_bo_type_fake &&
865 !(new_mask & (DRM_BO_FLAG_NO_MOVE | DRM_BO_FLAG_NO_EVICT))) {
866 DRM_ERROR("Fake buffers must be pinned.\n");
870 if ((new_mask & DRM_BO_FLAG_NO_EVICT) && !DRM_SUSER(DRM_CURPROC)) {
872 ("DRM_BO_FLAG_NO_EVICT is only available to priviliged "
877 new_props = new_mask & (DRM_BO_FLAG_EXE | DRM_BO_FLAG_WRITE |
881 DRM_ERROR("Invalid buffer object rwx properties\n");
885 bo->mem.mask = new_mask;
890 * Call dev->struct_mutex locked.
893 drm_buffer_object_t *drm_lookup_buffer_object(drm_file_t * priv,
894 uint32_t handle, int check_owner)
896 drm_user_object_t *uo;
897 drm_buffer_object_t *bo;
899 uo = drm_lookup_user_object(priv, handle);
901 if (!uo || (uo->type != drm_buffer_type)) {
902 DRM_ERROR("Could not find buffer object 0x%08x\n", handle);
906 if (check_owner && priv != uo->owner) {
907 if (!drm_lookup_ref_object(priv, uo, _DRM_REF_USE))
911 bo = drm_user_object_entry(uo, drm_buffer_object_t, base);
912 atomic_inc(&bo->usage);
917 * Call bo->mutex locked.
918 * Returns 1 if the buffer is currently rendered to or from. 0 otherwise.
919 * Doesn't do any fence flushing as opposed to the drm_bo_busy function.
922 static int drm_bo_quick_busy(drm_buffer_object_t * bo)
924 drm_fence_object_t *fence = bo->fence;
926 BUG_ON(bo->priv_flags & _DRM_BO_FLAG_UNFENCED);
928 drm_device_t *dev = bo->dev;
929 if (drm_fence_object_signaled(fence, bo->fence_type)) {
930 drm_fence_usage_deref_unlocked(dev, fence);
940 * Call bo->mutex locked.
941 * Returns 1 if the buffer is currently rendered to or from. 0 otherwise.
944 static int drm_bo_busy(drm_buffer_object_t * bo)
946 drm_fence_object_t *fence = bo->fence;
948 BUG_ON(bo->priv_flags & _DRM_BO_FLAG_UNFENCED);
950 drm_device_t *dev = bo->dev;
951 if (drm_fence_object_signaled(fence, bo->fence_type)) {
952 drm_fence_usage_deref_unlocked(dev, fence);
956 drm_fence_object_flush(dev, fence, DRM_FENCE_TYPE_EXE);
957 if (drm_fence_object_signaled(fence, bo->fence_type)) {
958 drm_fence_usage_deref_unlocked(dev, fence);
967 static int drm_bo_read_cached(drm_buffer_object_t * bo)
971 BUG_ON(bo->priv_flags & _DRM_BO_FLAG_UNFENCED);
973 ret = drm_bo_evict(bo, DRM_BO_MEM_TT, 1);
978 * Wait until a buffer is unmapped.
981 static int drm_bo_wait_unmapped(drm_buffer_object_t * bo, int no_wait)
985 if ((atomic_read(&bo->mapped) >= 0) && no_wait)
988 DRM_WAIT_ON(ret, bo->event_queue, 3 * DRM_HZ,
989 atomic_read(&bo->mapped) == -1);
997 static int drm_bo_check_unfenced(drm_buffer_object_t * bo)
1001 mutex_lock(&bo->mutex);
1002 ret = (bo->priv_flags & _DRM_BO_FLAG_UNFENCED);
1003 mutex_unlock(&bo->mutex);
1008 * Wait until a buffer, scheduled to be fenced moves off the unfenced list.
1009 * Until then, we cannot really do anything with it except delete it.
1010 * The unfenced list is a PITA, and the operations
1012 * 2) submitting commands
1014 * Should really be an atomic operation.
1015 * We now "solve" this problem by keeping
1016 * the buffer "unfenced" after validating, but before fencing.
1019 static int drm_bo_wait_unfenced(drm_buffer_object_t * bo, int no_wait,
1022 int ret = (bo->priv_flags & _DRM_BO_FLAG_UNFENCED);
1023 unsigned long _end = jiffies + 3 * DRM_HZ;
1031 mutex_unlock(&bo->mutex);
1032 DRM_WAIT_ON(ret, bo->event_queue, 3 * DRM_HZ,
1033 !drm_bo_check_unfenced(bo));
1034 mutex_lock(&bo->mutex);
1039 ("Error waiting for buffer to become fenced\n");
1042 ret = (bo->priv_flags & _DRM_BO_FLAG_UNFENCED);
1043 } while (ret && !time_after_eq(jiffies, _end));
1045 DRM_ERROR("Timeout waiting for buffer to become fenced\n");
1055 * Fill in the ioctl reply argument with buffer info.
1059 static void drm_bo_fill_rep_arg(drm_buffer_object_t * bo,
1060 drm_bo_arg_reply_t * rep)
1062 rep->handle = bo->base.hash.key;
1063 rep->flags = bo->mem.flags;
1064 rep->size = bo->mem.num_pages * PAGE_SIZE;
1065 rep->offset = bo->offset;
1066 rep->arg_handle = bo->map_list.user_token;
1067 rep->mask = bo->mem.mask;
1068 rep->buffer_start = bo->buffer_start;
1069 rep->fence_flags = bo->fence_type;
1071 rep->page_alignment = bo->mem.page_alignment;
1073 if ((bo->priv_flags & _DRM_BO_FLAG_UNFENCED) || drm_bo_quick_busy(bo)) {
1074 DRM_FLAG_MASKED(rep->rep_flags, DRM_BO_REP_BUSY,
1080 * Wait for buffer idle and register that we've mapped the buffer.
1081 * Mapping is registered as a drm_ref_object with type _DRM_REF_TYPE1,
1082 * so that if the client dies, the mapping is automatically
1086 static int drm_buffer_object_map(drm_file_t * priv, uint32_t handle,
1087 uint32_t map_flags, unsigned hint,
1088 drm_bo_arg_reply_t * rep)
1090 drm_buffer_object_t *bo;
1091 drm_device_t *dev = priv->head->dev;
1093 int no_wait = hint & DRM_BO_HINT_DONT_BLOCK;
1095 mutex_lock(&dev->struct_mutex);
1096 bo = drm_lookup_buffer_object(priv, handle, 1);
1097 mutex_unlock(&dev->struct_mutex);
1102 mutex_lock(&bo->mutex);
1103 if (!(hint & DRM_BO_HINT_ALLOW_UNFENCED_MAP)) {
1104 ret = drm_bo_wait_unfenced(bo, no_wait, 0);
1110 * If this returns true, we are currently unmapped.
1111 * We need to do this test, because unmapping can
1112 * be done without the bo->mutex held.
1116 if (atomic_inc_and_test(&bo->mapped)) {
1117 if (no_wait && drm_bo_busy(bo)) {
1118 atomic_dec(&bo->mapped);
1122 ret = drm_bo_wait(bo, 0, 0, no_wait);
1124 atomic_dec(&bo->mapped);
1128 if ((map_flags & DRM_BO_FLAG_READ) &&
1129 (bo->mem.flags & DRM_BO_FLAG_READ_CACHED) &&
1130 (!(bo->mem.flags & DRM_BO_FLAG_CACHED))) {
1131 drm_bo_read_cached(bo);
1134 } else if ((map_flags & DRM_BO_FLAG_READ) &&
1135 (bo->mem.flags & DRM_BO_FLAG_READ_CACHED) &&
1136 (!(bo->mem.flags & DRM_BO_FLAG_CACHED))) {
1139 * We are already mapped with different flags.
1140 * need to wait for unmap.
1143 ret = drm_bo_wait_unmapped(bo, no_wait);
1152 mutex_lock(&dev->struct_mutex);
1153 ret = drm_add_ref_object(priv, &bo->base, _DRM_REF_TYPE1);
1154 mutex_unlock(&dev->struct_mutex);
1156 if (atomic_add_negative(-1, &bo->mapped))
1157 DRM_WAKEUP(&bo->event_queue);
1160 drm_bo_fill_rep_arg(bo, rep);
1162 mutex_unlock(&bo->mutex);
1163 drm_bo_usage_deref_unlocked(bo);
1167 static int drm_buffer_object_unmap(drm_file_t * priv, uint32_t handle)
1169 drm_device_t *dev = priv->head->dev;
1170 drm_buffer_object_t *bo;
1171 drm_ref_object_t *ro;
1174 mutex_lock(&dev->struct_mutex);
1176 bo = drm_lookup_buffer_object(priv, handle, 1);
1182 ro = drm_lookup_ref_object(priv, &bo->base, _DRM_REF_TYPE1);
1188 drm_remove_ref_object(priv, ro);
1189 drm_bo_usage_deref_locked(bo);
1191 mutex_unlock(&dev->struct_mutex);
1196 * Call struct-sem locked.
1199 static void drm_buffer_user_object_unmap(drm_file_t * priv,
1200 drm_user_object_t * uo,
1203 drm_buffer_object_t *bo =
1204 drm_user_object_entry(uo, drm_buffer_object_t, base);
1207 * We DON'T want to take the bo->lock here, because we want to
1208 * hold it when we wait for unmapped buffer.
1211 BUG_ON(action != _DRM_REF_TYPE1);
1213 if (atomic_add_negative(-1, &bo->mapped))
1214 DRM_WAKEUP(&bo->event_queue);
1219 * Note that new_mem_flags are NOT transferred to the bo->mem.mask.
1222 int drm_bo_move_buffer(drm_buffer_object_t * bo, uint32_t new_mem_flags,
1223 int no_wait, int move_unfenced)
1225 drm_device_t *dev = bo->dev;
1226 drm_buffer_manager_t *bm = &dev->bm;
1228 drm_bo_mem_reg_t mem;
1230 * Flush outstanding fences.
1236 * Wait for outstanding fences.
1239 ret = drm_bo_wait(bo, 0, 0, no_wait);
1243 mem.num_pages = bo->mem.num_pages;
1244 mem.size = mem.num_pages << PAGE_SHIFT;
1245 mem.mask = new_mem_flags;
1246 mem.page_alignment = bo->mem.page_alignment;
1248 mutex_lock(&bm->evict_mutex);
1249 mutex_lock(&dev->struct_mutex);
1251 list_add_tail(&bo->lru, &bm->unfenced);
1252 DRM_FLAG_MASKED(bo->priv_flags, _DRM_BO_FLAG_UNFENCED,
1253 _DRM_BO_FLAG_UNFENCED);
1254 mutex_unlock(&dev->struct_mutex);
1257 * Determine where to move the buffer.
1259 ret = drm_bo_mem_space(bo, &mem, no_wait);
1263 ret = drm_bo_handle_move_mem(bo, &mem, 0, no_wait);
1266 if (ret || !move_unfenced) {
1267 mutex_lock(&dev->struct_mutex);
1269 if (mem.mm_node != bo->pinned_node)
1270 drm_mm_put_block(mem.mm_node);
1273 DRM_FLAG_MASKED(bo->priv_flags, 0, _DRM_BO_FLAG_UNFENCED);
1274 DRM_WAKEUP(&bo->event_queue);
1276 drm_bo_add_to_lru(bo);
1277 mutex_unlock(&dev->struct_mutex);
1280 mutex_unlock(&bm->evict_mutex);
1284 static int drm_bo_mem_compat(drm_bo_mem_reg_t * mem)
1286 uint32_t flag_diff = (mem->mask ^ mem->flags);
1288 if ((mem->mask & mem->flags & DRM_BO_MASK_MEM) == 0)
1290 if ((flag_diff & DRM_BO_FLAG_CACHED) &&
1291 (!(mem->mask & DRM_BO_FLAG_CACHED) ||
1292 (mem->mask & DRM_BO_FLAG_FORCE_CACHING))) {
1295 if ((flag_diff & DRM_BO_FLAG_MAPPABLE) &&
1296 ((mem->mask & DRM_BO_FLAG_MAPPABLE) ||
1297 (mem->mask & DRM_BO_FLAG_FORCE_MAPPABLE)))
1302 static int drm_bo_check_fake(drm_device_t * dev, drm_bo_mem_reg_t * mem)
1304 drm_buffer_manager_t *bm = &dev->bm;
1305 drm_mem_type_manager_t *man;
1306 uint32_t num_prios = dev->driver->bo_driver->num_mem_type_prio;
1307 const uint32_t *prios = dev->driver->bo_driver->mem_type_prio;
1310 uint32_t mem_type = 0;
1313 if (drm_bo_mem_compat(mem))
1316 BUG_ON(mem->mm_node);
1318 for (i = 0; i < num_prios; ++i) {
1319 mem_type = prios[i];
1320 man = &bm->man[mem_type];
1321 type_ok = drm_bo_mt_compatible(man, mem_type, mem->mask,
1328 mem->mm_node = NULL;
1329 mem->mem_type = mem_type;
1330 mem->flags = cur_flags;
1331 DRM_FLAG_MASKED(mem->flags, mem->mask, ~DRM_BO_MASK_MEMTYPE);
1335 DRM_ERROR("Illegal fake buffer flags 0x%08x\n", mem->mask);
1343 static int drm_buffer_object_validate(drm_buffer_object_t * bo,
1344 int move_unfenced, int no_wait)
1346 drm_device_t *dev = bo->dev;
1347 drm_buffer_manager_t *bm = &dev->bm;
1348 drm_bo_driver_t *driver = dev->driver->bo_driver;
1351 DRM_DEBUG("New flags 0x%08x, Old flags 0x%08x\n", bo->mem.mask,
1354 driver->fence_type(bo->mem.mask, &bo->fence_class, &bo->fence_type);
1356 DRM_ERROR("Driver did not support given buffer permissions\n");
1360 ret = drm_bo_wait_unmapped(bo, no_wait);
1364 if (bo->type == drm_bo_type_fake) {
1365 ret = drm_bo_check_fake(dev, &bo->mem);
1371 * Check whether we need to move buffer.
1374 if (!drm_bo_mem_compat(&bo->mem)) {
1375 ret = drm_bo_move_buffer(bo, bo->mem.mask, no_wait,
1379 DRM_ERROR("Failed moving buffer.\n");
1388 if (bo->mem.mask & (DRM_BO_FLAG_NO_EVICT | DRM_BO_FLAG_NO_MOVE)) {
1389 bo->pinned_mem_type = bo->mem.mem_type;
1390 mutex_lock(&dev->struct_mutex);
1391 list_del_init(&bo->pinned_lru);
1392 drm_bo_add_to_pinned_lru(bo);
1394 if (bo->pinned_node != bo->mem.mm_node) {
1395 if (bo->pinned_node != NULL)
1396 drm_mm_put_block(bo->pinned_node);
1397 bo->pinned_node = bo->mem.mm_node;
1400 mutex_unlock(&dev->struct_mutex);
1402 } else if (bo->pinned_node != NULL) {
1404 mutex_lock(&dev->struct_mutex);
1405 drm_mm_put_block(bo->pinned_node);
1406 list_del_init(&bo->pinned_lru);
1407 bo->pinned_node = NULL;
1408 mutex_unlock(&dev->struct_mutex);
1413 * We might need to add a TTM.
1416 if (bo->mem.mem_type == DRM_BO_MEM_LOCAL && bo->ttm == NULL) {
1417 ret = drm_bo_add_ttm(bo);
1421 DRM_FLAG_MASKED(bo->mem.flags, bo->mem.mask, ~DRM_BO_MASK_MEMTYPE);
1424 * Finally, adjust lru to be sure.
1427 mutex_lock(&dev->struct_mutex);
1429 if (move_unfenced) {
1430 list_add_tail(&bo->lru, &bm->unfenced);
1431 DRM_FLAG_MASKED(bo->priv_flags, _DRM_BO_FLAG_UNFENCED,
1432 _DRM_BO_FLAG_UNFENCED);
1434 drm_bo_add_to_lru(bo);
1435 if (bo->priv_flags & _DRM_BO_FLAG_UNFENCED) {
1436 DRM_WAKEUP(&bo->event_queue);
1437 DRM_FLAG_MASKED(bo->priv_flags, 0,
1438 _DRM_BO_FLAG_UNFENCED);
1441 mutex_unlock(&dev->struct_mutex);
1446 static int drm_bo_handle_validate(drm_file_t * priv, uint32_t handle,
1447 uint32_t flags, uint32_t mask, uint32_t hint,
1448 drm_bo_arg_reply_t * rep)
1450 drm_buffer_object_t *bo;
1452 int no_wait = hint & DRM_BO_HINT_DONT_BLOCK;
1454 bo = drm_lookup_buffer_object(priv, handle, 1);
1459 mutex_lock(&bo->mutex);
1460 ret = drm_bo_wait_unfenced(bo, no_wait, 0);
1465 DRM_FLAG_MASKED(flags, bo->mem.mask, ~mask);
1466 ret = drm_bo_new_mask(bo, flags, hint);
1471 drm_buffer_object_validate(bo, !(hint & DRM_BO_HINT_DONT_FENCE),
1473 drm_bo_fill_rep_arg(bo, rep);
1477 mutex_unlock(&bo->mutex);
1479 drm_bo_usage_deref_unlocked(bo);
1483 static int drm_bo_handle_info(drm_file_t * priv, uint32_t handle,
1484 drm_bo_arg_reply_t * rep)
1486 drm_buffer_object_t *bo;
1488 bo = drm_lookup_buffer_object(priv, handle, 1);
1492 mutex_lock(&bo->mutex);
1493 if (!(bo->priv_flags & _DRM_BO_FLAG_UNFENCED))
1494 (void)drm_bo_busy(bo);
1495 drm_bo_fill_rep_arg(bo, rep);
1496 mutex_unlock(&bo->mutex);
1497 drm_bo_usage_deref_unlocked(bo);
1501 static int drm_bo_handle_wait(drm_file_t * priv, uint32_t handle,
1502 uint32_t hint, drm_bo_arg_reply_t * rep)
1504 drm_buffer_object_t *bo;
1505 int no_wait = hint & DRM_BO_HINT_DONT_BLOCK;
1508 bo = drm_lookup_buffer_object(priv, handle, 1);
1513 mutex_lock(&bo->mutex);
1514 ret = drm_bo_wait_unfenced(bo, no_wait, 0);
1517 ret = drm_bo_wait(bo, hint & DRM_BO_HINT_WAIT_LAZY, 0, no_wait);
1521 drm_bo_fill_rep_arg(bo, rep);
1524 mutex_unlock(&bo->mutex);
1525 drm_bo_usage_deref_unlocked(bo);
1529 int drm_buffer_object_create(drm_file_t * priv,
1534 uint32_t page_alignment,
1535 unsigned long buffer_start,
1536 drm_buffer_object_t ** buf_obj)
1538 drm_device_t *dev = priv->head->dev;
1539 drm_buffer_manager_t *bm = &dev->bm;
1540 drm_buffer_object_t *bo;
1542 unsigned long num_pages;
1544 if ((buffer_start & ~PAGE_MASK) && (type != drm_bo_type_fake)) {
1545 DRM_ERROR("Invalid buffer object start.\n");
1548 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1549 if (num_pages == 0) {
1550 DRM_ERROR("Illegal buffer object size.\n");
1554 bo = drm_ctl_calloc(1, sizeof(*bo), DRM_MEM_BUFOBJ);
1559 mutex_init(&bo->mutex);
1560 mutex_lock(&bo->mutex);
1562 atomic_set(&bo->usage, 1);
1563 atomic_set(&bo->mapped, -1);
1564 DRM_INIT_WAITQUEUE(&bo->event_queue);
1565 INIT_LIST_HEAD(&bo->lru);
1566 INIT_LIST_HEAD(&bo->pinned_lru);
1567 INIT_LIST_HEAD(&bo->ddestroy);
1568 #ifdef DRM_ODD_MM_COMPAT
1569 INIT_LIST_HEAD(&bo->p_mm_list);
1570 INIT_LIST_HEAD(&bo->vma_list);
1574 bo->mem.mem_type = DRM_BO_MEM_LOCAL;
1575 bo->mem.num_pages = num_pages;
1576 bo->mem.mm_node = NULL;
1577 bo->mem.page_alignment = page_alignment;
1578 if (bo->type == drm_bo_type_fake) {
1579 bo->offset = buffer_start;
1580 bo->buffer_start = 0;
1582 bo->buffer_start = buffer_start;
1587 atomic_inc(&bm->count);
1588 ret = drm_bo_new_mask(bo, mask, hint);
1593 if (bo->type == drm_bo_type_dc) {
1594 mutex_lock(&dev->struct_mutex);
1595 ret = drm_bo_setup_vm_locked(bo);
1596 mutex_unlock(&dev->struct_mutex);
1600 ret = drm_buffer_object_validate(bo, 0, hint & DRM_BO_HINT_DONT_BLOCK);
1604 mutex_unlock(&bo->mutex);
1609 mutex_unlock(&bo->mutex);
1611 drm_bo_usage_deref_unlocked(bo);
1615 static int drm_bo_add_user_object(drm_file_t * priv, drm_buffer_object_t * bo,
1618 drm_device_t *dev = priv->head->dev;
1621 mutex_lock(&dev->struct_mutex);
1622 ret = drm_add_user_object(priv, &bo->base, shareable);
1626 bo->base.remove = drm_bo_base_deref_locked;
1627 bo->base.type = drm_buffer_type;
1628 bo->base.ref_struct_locked = NULL;
1629 bo->base.unref = drm_buffer_user_object_unmap;
1632 mutex_unlock(&dev->struct_mutex);
1636 static int drm_bo_lock_test(drm_device_t * dev, struct file *filp)
1638 LOCK_TEST_WITH_RETURN(dev, filp);
1642 int drm_bo_ioctl(DRM_IOCTL_ARGS)
1646 drm_bo_arg_request_t *req = &arg.d.req;
1647 drm_bo_arg_reply_t rep;
1649 drm_user_object_t *uo;
1650 drm_buffer_object_t *entry;
1652 if (!dev->bm.initialized) {
1653 DRM_ERROR("Buffer object manager is not initialized.\n");
1658 DRM_COPY_FROM_USER_IOCTL(arg, (void __user *)data, sizeof(arg));
1669 drm_buffer_object_create(priv, req->size,
1673 req->page_alignment,
1674 req->buffer_start, &entry);
1679 drm_bo_add_user_object(priv, entry,
1682 DRM_BO_FLAG_SHAREABLE);
1684 drm_bo_usage_deref_unlocked(entry);
1689 mutex_lock(&entry->mutex);
1690 drm_bo_fill_rep_arg(entry, &rep);
1691 mutex_unlock(&entry->mutex);
1694 rep.ret = drm_buffer_object_unmap(priv, req->handle);
1697 rep.ret = drm_buffer_object_map(priv, req->handle,
1701 case drm_bo_destroy:
1702 mutex_lock(&dev->struct_mutex);
1703 uo = drm_lookup_user_object(priv, req->handle);
1704 if (!uo || (uo->type != drm_buffer_type)
1705 || uo->owner != priv) {
1706 mutex_unlock(&dev->struct_mutex);
1710 rep.ret = drm_remove_user_object(priv, uo);
1711 mutex_unlock(&dev->struct_mutex);
1713 case drm_bo_reference:
1714 rep.ret = drm_user_object_ref(priv, req->handle,
1715 drm_buffer_type, &uo);
1718 mutex_lock(&dev->struct_mutex);
1719 uo = drm_lookup_user_object(priv, req->handle);
1721 drm_user_object_entry(uo, drm_buffer_object_t,
1723 atomic_dec(&entry->usage);
1724 mutex_unlock(&dev->struct_mutex);
1725 mutex_lock(&entry->mutex);
1726 drm_bo_fill_rep_arg(entry, &rep);
1727 mutex_unlock(&entry->mutex);
1729 case drm_bo_unreference:
1730 rep.ret = drm_user_object_unref(priv, req->handle,
1733 case drm_bo_validate:
1734 rep.ret = drm_bo_lock_test(dev, filp);
1739 drm_bo_handle_validate(priv, req->handle, req->mask,
1740 req->arg_handle, req->hint,
1744 rep.ret = drm_bo_lock_test(dev, filp);
1749 rep.ret = drm_bo_handle_info(priv, req->handle, &rep);
1751 case drm_bo_wait_idle:
1752 rep.ret = drm_bo_handle_wait(priv, req->handle,
1755 case drm_bo_ref_fence:
1757 DRM_ERROR("Function is not implemented yet.\n");
1764 * A signal interrupted us. Make sure the ioctl is restartable.
1767 if (rep.ret == -EAGAIN)
1772 DRM_COPY_TO_USER_IOCTL((void __user *)data, arg, sizeof(arg));
1779 *Clean the unfenced list and put on regular LRU.
1780 *This is part of the memory manager cleanup and should only be
1781 *called with the DRI lock held.
1782 *Call dev->struct_sem locked.
1785 static void drm_bo_clean_unfenced(drm_device_t *dev)
1787 drm_buffer_manager_t *bm = &dev->bm;
1788 struct list_head *head, *list;
1789 drm_buffer_object_t *entry;
1791 head = &bm->unfenced;
1794 while(list != head) {
1795 prefetch(list->next);
1796 entry = list_entry(list, drm_buffer_object_t, lru);
1798 atomic_inc(&entry->usage);
1799 mutex_unlock(&dev->struct_mutex);
1800 mutex_lock(&entry->mutex);
1801 mutex_lock(&dev->struct_mutex);
1803 list_del(&entry->lru);
1804 DRM_FLAG_MASKED(entry->priv_flags, 0, _DRM_BO_FLAG_UNFENCED);
1805 drm_bo_add_to_lru(entry);
1806 mutex_unlock(&entry->mutex);
1811 static int drm_bo_leave_list(drm_buffer_object_t * bo,
1813 int free_pinned, int allow_errors)
1815 drm_device_t *dev = bo->dev;
1818 mutex_lock(&bo->mutex);
1820 ret = drm_bo_expire_fence(bo, allow_errors);
1825 DRM_FLAG_MASKED(bo->mem.flags, 0, DRM_BO_FLAG_NO_MOVE);
1826 mutex_lock(&dev->struct_mutex);
1827 list_del_init(&bo->pinned_lru);
1828 if (bo->pinned_node == bo->mem.mm_node)
1829 bo->pinned_node = NULL;
1830 if (bo->pinned_node != NULL) {
1831 drm_mm_put_block(bo->pinned_node);
1832 bo->pinned_node = NULL;
1834 mutex_unlock(&dev->struct_mutex);
1837 if (bo->mem.flags & DRM_BO_FLAG_NO_EVICT) {
1838 DRM_ERROR("A DRM_BO_NO_EVICT buffer present at "
1839 "cleanup. Removing flag and evicting.\n");
1840 bo->mem.flags &= ~DRM_BO_FLAG_NO_EVICT;
1841 bo->mem.mask &= ~DRM_BO_FLAG_NO_EVICT;
1844 if (bo->mem.mem_type == mem_type)
1845 ret = drm_bo_evict(bo, mem_type, 0);
1852 DRM_ERROR("Cleanup eviction failed\n");
1857 mutex_unlock(&bo->mutex);
1862 static drm_buffer_object_t *drm_bo_entry(struct list_head *list,
1866 return list_entry(list, drm_buffer_object_t, pinned_lru);
1868 return list_entry(list, drm_buffer_object_t, lru);
1872 * dev->struct_mutex locked.
1875 static int drm_bo_force_list_clean(drm_device_t * dev,
1876 struct list_head *head,
1882 struct list_head *list, *next, *prev;
1883 drm_buffer_object_t *entry, *nentry;
1888 * The list traversal is a bit odd here, because an item may
1889 * disappear from the list when we release the struct_mutex or
1890 * when we decrease the usage count. Also we're not guaranteed
1891 * to drain pinned lists, so we can't always restart.
1896 list_for_each_safe(list, next, head) {
1899 entry = (nentry != NULL) ? nentry: drm_bo_entry(list, pinned_list);
1900 atomic_inc(&entry->usage);
1902 atomic_dec(&nentry->usage);
1907 * Protect the next item from destruction, so we can check
1908 * its list pointers later on.
1912 nentry = drm_bo_entry(next, pinned_list);
1913 atomic_inc(&nentry->usage);
1915 mutex_unlock(&dev->struct_mutex);
1917 ret = drm_bo_leave_list(entry, mem_type, free_pinned,
1919 mutex_lock(&dev->struct_mutex);
1921 drm_bo_usage_deref_locked(entry);
1926 * Has the next item disappeared from the list?
1929 do_restart = ((next->prev != list) && (next->prev != prev));
1931 if (nentry != NULL && do_restart) {
1932 drm_bo_usage_deref_locked(nentry);
1942 int drm_bo_clean_mm(drm_device_t * dev, unsigned mem_type)
1944 drm_buffer_manager_t *bm = &dev->bm;
1945 drm_mem_type_manager_t *man = &bm->man[mem_type];
1948 if (mem_type >= DRM_BO_MEM_TYPES) {
1949 DRM_ERROR("Illegal memory type %d\n", mem_type);
1953 if (!man->has_type) {
1954 DRM_ERROR("Trying to take down uninitialized "
1955 "memory manager type\n");
1964 drm_bo_clean_unfenced(dev);
1965 drm_bo_force_list_clean(dev, &man->lru, mem_type, 1, 0, 0);
1966 drm_bo_force_list_clean(dev, &man->pinned, mem_type, 1, 0, 1);
1968 if (drm_mm_clean(&man->manager)) {
1969 drm_mm_takedown(&man->manager);
1979 *Evict all buffers of a particular mem_type, but leave memory manager
1980 *regions for NO_MOVE buffers intact. New buffers cannot be added at this
1981 *point since we have the hardware lock.
1984 static int drm_bo_lock_mm(drm_device_t * dev, unsigned mem_type)
1987 drm_buffer_manager_t *bm = &dev->bm;
1988 drm_mem_type_manager_t *man = &bm->man[mem_type];
1990 if (mem_type == 0 || mem_type >= DRM_BO_MEM_TYPES) {
1991 DRM_ERROR("Illegal memory manager memory type %u,\n", mem_type);
1995 drm_bo_clean_unfenced(dev);
1996 ret = drm_bo_force_list_clean(dev, &man->lru, mem_type, 0, 1, 0);
1999 ret = drm_bo_force_list_clean(dev, &man->pinned, mem_type, 0, 1, 1);
2004 static int drm_bo_init_mm(drm_device_t * dev,
2006 unsigned long p_offset, unsigned long p_size)
2008 drm_buffer_manager_t *bm = &dev->bm;
2010 drm_mem_type_manager_t *man;
2012 if (type >= DRM_BO_MEM_TYPES) {
2013 DRM_ERROR("Illegal memory type %d\n", type);
2017 man = &bm->man[type];
2018 if (man->has_type) {
2019 DRM_ERROR("Memory manager already initialized for type %d\n",
2024 ret = dev->driver->bo_driver->init_mem_type(dev, type, man);
2029 if (type != DRM_BO_MEM_LOCAL) {
2031 DRM_ERROR("Zero size memory manager type %d\n", type);
2034 ret = drm_mm_init(&man->manager, p_offset, p_size);
2041 INIT_LIST_HEAD(&man->lru);
2042 INIT_LIST_HEAD(&man->pinned);
2048 * This is called from lastclose, so we don't need to bother about
2049 * any clients still running when we set the initialized flag to zero.
2052 int drm_bo_driver_finish(drm_device_t * dev)
2054 drm_buffer_manager_t *bm = &dev->bm;
2056 unsigned i = DRM_BO_MEM_TYPES;
2057 drm_mem_type_manager_t *man;
2059 mutex_lock(&dev->bm.init_mutex);
2060 mutex_lock(&dev->struct_mutex);
2062 if (!bm->initialized)
2064 bm->initialized = 0;
2068 if (man->has_type) {
2070 if ((i != DRM_BO_MEM_LOCAL) && drm_bo_clean_mm(dev, i)) {
2072 DRM_ERROR("DRM memory manager type %d "
2073 "is not clean.\n", i);
2078 mutex_unlock(&dev->struct_mutex);
2080 if (!cancel_delayed_work(&bm->wq)) {
2081 flush_scheduled_work();
2083 mutex_lock(&dev->struct_mutex);
2084 drm_bo_delayed_delete(dev, 1);
2085 if (list_empty(&bm->ddestroy)) {
2086 DRM_DEBUG("Delayed destroy list was clean\n");
2088 if (list_empty(&bm->man[0].lru)) {
2089 DRM_DEBUG("Swap list was clean\n");
2091 if (list_empty(&bm->man[0].pinned)) {
2092 DRM_DEBUG("NO_MOVE list was clean\n");
2094 if (list_empty(&bm->unfenced)) {
2095 DRM_DEBUG("Unfenced list was clean\n");
2098 mutex_unlock(&dev->struct_mutex);
2099 mutex_unlock(&dev->bm.init_mutex);
2103 int drm_bo_driver_init(drm_device_t * dev)
2105 drm_bo_driver_t *driver = dev->driver->bo_driver;
2106 drm_buffer_manager_t *bm = &dev->bm;
2109 mutex_lock(&dev->bm.init_mutex);
2110 mutex_lock(&dev->struct_mutex);
2115 * Initialize the system memory buffer type.
2116 * Other types need to be driver / IOCTL initialized.
2119 ret = drm_bo_init_mm(dev, 0, 0, 0);
2123 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
2124 INIT_WORK(&bm->wq, &drm_bo_delayed_workqueue, dev);
2126 INIT_DELAYED_WORK(&bm->wq, drm_bo_delayed_workqueue);
2128 bm->initialized = 1;
2130 atomic_set(&bm->count, 0);
2132 INIT_LIST_HEAD(&bm->unfenced);
2133 INIT_LIST_HEAD(&bm->ddestroy);
2135 mutex_unlock(&dev->struct_mutex);
2136 mutex_unlock(&dev->bm.init_mutex);
2140 EXPORT_SYMBOL(drm_bo_driver_init);
2142 int drm_mm_init_ioctl(DRM_IOCTL_ARGS)
2147 drm_mm_init_arg_t arg;
2148 drm_buffer_manager_t *bm = &dev->bm;
2149 drm_bo_driver_t *driver = dev->driver->bo_driver;
2152 DRM_ERROR("Buffer objects are not supported by this driver\n");
2156 DRM_COPY_FROM_USER_IOCTL(arg, (void __user *)data, sizeof(arg));
2158 switch (arg.req.op) {
2161 mutex_lock(&dev->bm.init_mutex);
2162 mutex_lock(&dev->struct_mutex);
2163 if (!bm->initialized) {
2164 DRM_ERROR("DRM memory manager was not initialized.\n");
2167 if (arg.req.mem_type == 0) {
2169 ("System memory buffers already initialized.\n");
2172 ret = drm_bo_init_mm(dev, arg.req.mem_type,
2173 arg.req.p_offset, arg.req.p_size);
2176 LOCK_TEST_WITH_RETURN(dev, filp);
2177 mutex_lock(&dev->bm.init_mutex);
2178 mutex_lock(&dev->struct_mutex);
2180 if (!bm->initialized) {
2181 DRM_ERROR("DRM memory manager was not initialized\n");
2184 if (arg.req.mem_type == 0) {
2185 DRM_ERROR("No takedown for System memory buffers.\n");
2189 if (drm_bo_clean_mm(dev, arg.req.mem_type)) {
2190 DRM_ERROR("Memory manager type %d not clean. "
2191 "Delaying takedown\n", arg.req.mem_type);
2195 LOCK_TEST_WITH_RETURN(dev, filp);
2196 mutex_lock(&dev->bm.init_mutex);
2197 mutex_lock(&dev->struct_mutex);
2198 ret = drm_bo_lock_mm(dev, arg.req.mem_type);
2201 LOCK_TEST_WITH_RETURN(dev, filp);
2202 mutex_lock(&dev->bm.init_mutex);
2203 mutex_lock(&dev->struct_mutex);
2207 DRM_ERROR("Function not implemented yet\n");
2211 mutex_unlock(&dev->struct_mutex);
2212 mutex_unlock(&dev->bm.init_mutex);
2216 DRM_COPY_TO_USER_IOCTL((void __user *)data, arg, sizeof(arg));
2221 * buffer object vm functions.
2224 int drm_mem_reg_is_pci(drm_device_t * dev, drm_bo_mem_reg_t * mem)
2226 drm_buffer_manager_t *bm = &dev->bm;
2227 drm_mem_type_manager_t *man = &bm->man[mem->mem_type];
2229 if (!(man->flags & _DRM_FLAG_MEMTYPE_FIXED)) {
2230 if (mem->mem_type == DRM_BO_MEM_LOCAL)
2233 if (man->flags & _DRM_FLAG_MEMTYPE_CMA)
2236 if (mem->flags & DRM_BO_FLAG_CACHED)
2242 EXPORT_SYMBOL(drm_mem_reg_is_pci);
2245 * \c Get the PCI offset for the buffer object memory.
2247 * \param bo The buffer object.
2248 * \param bus_base On return the base of the PCI region
2249 * \param bus_offset On return the byte offset into the PCI region
2250 * \param bus_size On return the byte size of the buffer object or zero if
2251 * the buffer object memory is not accessible through a PCI region.
2252 * \return Failure indication.
2254 * Returns -EINVAL if the buffer object is currently not mappable.
2255 * Otherwise returns zero.
2258 int drm_bo_pci_offset(drm_device_t * dev,
2259 drm_bo_mem_reg_t * mem,
2260 unsigned long *bus_base,
2261 unsigned long *bus_offset, unsigned long *bus_size)
2263 drm_buffer_manager_t *bm = &dev->bm;
2264 drm_mem_type_manager_t *man = &bm->man[mem->mem_type];
2267 if (!(man->flags & _DRM_FLAG_MEMTYPE_MAPPABLE))
2270 if (drm_mem_reg_is_pci(dev, mem)) {
2271 *bus_offset = mem->mm_node->start << PAGE_SHIFT;
2272 *bus_size = mem->num_pages << PAGE_SHIFT;
2273 *bus_base = man->io_offset;
2280 * \c Kill all user-space virtual mappings of this buffer object.
2282 * \param bo The buffer object.
2284 * Call bo->mutex locked.
2287 void drm_bo_unmap_virtual(drm_buffer_object_t * bo)
2289 drm_device_t *dev = bo->dev;
2290 loff_t offset = ((loff_t) bo->map_list.hash.key) << PAGE_SHIFT;
2291 loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
2293 unmap_mapping_range(dev->dev_mapping, offset, holelen, 1);
2296 static void drm_bo_takedown_vm_locked(drm_buffer_object_t * bo)
2298 drm_map_list_t *list = &bo->map_list;
2299 drm_local_map_t *map;
2300 drm_device_t *dev = bo->dev;
2302 if (list->user_token) {
2303 drm_ht_remove_item(&dev->map_hash, &list->hash);
2304 list->user_token = 0;
2306 if (list->file_offset_node) {
2307 drm_mm_put_block(list->file_offset_node);
2308 list->file_offset_node = NULL;
2315 drm_ctl_free(map, sizeof(*map), DRM_MEM_BUFOBJ);
2317 list->user_token = 0ULL;
2318 drm_bo_usage_deref_locked(bo);
2321 static int drm_bo_setup_vm_locked(drm_buffer_object_t * bo)
2323 drm_map_list_t *list = &bo->map_list;
2324 drm_local_map_t *map;
2325 drm_device_t *dev = bo->dev;
2327 list->map = drm_ctl_calloc(1, sizeof(*map), DRM_MEM_BUFOBJ);
2333 map->type = _DRM_TTM;
2334 map->flags = _DRM_REMOVABLE;
2335 map->size = bo->mem.num_pages * PAGE_SIZE;
2336 atomic_inc(&bo->usage);
2337 map->handle = (void *)bo;
2339 list->file_offset_node = drm_mm_search_free(&dev->offset_manager,
2340 bo->mem.num_pages, 0, 0);
2342 if (!list->file_offset_node) {
2343 drm_bo_takedown_vm_locked(bo);
2347 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
2348 bo->mem.num_pages, 0);
2350 list->hash.key = list->file_offset_node->start;
2351 if (drm_ht_insert_item(&dev->map_hash, &list->hash)) {
2352 drm_bo_takedown_vm_locked(bo);
2356 list->user_token = ((drm_u64_t) list->hash.key) << PAGE_SHIFT;