Subversion Repositories Kolibri OS

Rev

Rev 6938 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  4.  * All Rights Reserved.
  5.  *
  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:
  13.  *
  14.  * The above copyright notice and this permission notice (including the
  15.  * next paragraph) shall be included in all copies or substantial portions
  16.  * of the Software.
  17.  *
  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.
  25.  *
  26.  **************************************************************************/
  27. /*
  28.  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29.  */
  30.  
  31. #define pr_fmt(fmt) "[TTM] " fmt
  32.  
  33. #include <drm/ttm/ttm_module.h>
  34. #include <drm/ttm/ttm_bo_driver.h>
  35. #include <drm/ttm/ttm_placement.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/slab.h>
  38. #include <linux/sched.h>
  39. #include <linux/mm.h>
  40. #include <linux/file.h>
  41. #include <linux/module.h>
  42. #include <linux/atomic.h>
  43. #include <linux/reservation.h>
  44.  
  45. #define TTM_ASSERT_LOCKED(param)
  46. #define TTM_DEBUG(fmt, arg...)
  47. #define TTM_BO_HASH_ORDER 13
  48.  
  49.  
  50.  
  51. static inline int ttm_mem_type_from_place(const struct ttm_place *place,
  52.                                           uint32_t *mem_type)
  53. {
  54.         int i;
  55.  
  56.         for (i = 0; i <= TTM_PL_PRIV5; i++)
  57.                 if (place->flags & (1 << i)) {
  58.                         *mem_type = i;
  59.                         return 0;
  60.                 }
  61.         return -EINVAL;
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. static inline uint32_t ttm_bo_type_flags(unsigned type)
  73. {
  74.         return 1 << (type);
  75. }
  76.  
  77. static void ttm_bo_release_list(struct kref *list_kref)
  78. {
  79.         struct ttm_buffer_object *bo =
  80.             container_of(list_kref, struct ttm_buffer_object, list_kref);
  81.         struct ttm_bo_device *bdev = bo->bdev;
  82.         size_t acc_size = bo->acc_size;
  83.  
  84.         BUG_ON(atomic_read(&bo->list_kref.refcount));
  85.         BUG_ON(atomic_read(&bo->kref.refcount));
  86.         BUG_ON(atomic_read(&bo->cpu_writers));
  87.         BUG_ON(bo->mem.mm_node != NULL);
  88.         BUG_ON(!list_empty(&bo->lru));
  89.         BUG_ON(!list_empty(&bo->ddestroy));
  90.  
  91.         if (bo->ttm)
  92.                 ttm_tt_destroy(bo->ttm);
  93.         atomic_dec(&bo->glob->bo_count);
  94.         if (bo->resv == &bo->ttm_resv)
  95.                 reservation_object_fini(&bo->ttm_resv);
  96.         mutex_destroy(&bo->wu_mutex);
  97.         if (bo->destroy)
  98.                 bo->destroy(bo);
  99.         else {
  100.                 kfree(bo);
  101.         }
  102. }
  103.  
  104. void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
  105. {
  106.         struct ttm_bo_device *bdev = bo->bdev;
  107.         struct ttm_mem_type_manager *man;
  108.  
  109.         lockdep_assert_held(&bo->resv->lock.base);
  110.  
  111.         if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  112.  
  113.                 BUG_ON(!list_empty(&bo->lru));
  114.  
  115.                 man = &bdev->man[bo->mem.mem_type];
  116.                 list_add_tail(&bo->lru, &man->lru);
  117.                 kref_get(&bo->list_kref);
  118.  
  119.                 if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
  120.                         list_add_tail(&bo->swap, &bo->glob->swap_lru);
  121.                         kref_get(&bo->list_kref);
  122.                 }
  123.         }
  124. }
  125. EXPORT_SYMBOL(ttm_bo_add_to_lru);
  126.  
  127. int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
  128. {
  129.         int put_count = 0;
  130.  
  131.         if (!list_empty(&bo->swap)) {
  132.                 list_del_init(&bo->swap);
  133.                 ++put_count;
  134.         }
  135.         if (!list_empty(&bo->lru)) {
  136.                 list_del_init(&bo->lru);
  137.                 ++put_count;
  138.         }
  139.  
  140.         /*
  141.          * TODO: Add a driver hook to delete from
  142.          * driver-specific LRU's here.
  143.          */
  144.  
  145.         return put_count;
  146. }
  147.  
  148. static void ttm_bo_ref_bug(struct kref *list_kref)
  149. {
  150.         BUG();
  151. }
  152.  
  153. void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
  154.                          bool never_free)
  155. {
  156.         kref_sub(&bo->list_kref, count,
  157.                  (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
  158. }
  159.  
  160. void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
  161. {
  162.         int put_count;
  163.  
  164.         spin_lock(&bo->glob->lru_lock);
  165.         put_count = ttm_bo_del_from_lru(bo);
  166.         spin_unlock(&bo->glob->lru_lock);
  167.         ttm_bo_list_ref_sub(bo, put_count, true);
  168. }
  169. EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
  170.  
  171. /*
  172.  * Call bo->mutex locked.
  173.  */
  174. static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
  175. {
  176.         struct ttm_bo_device *bdev = bo->bdev;
  177.         struct ttm_bo_global *glob = bo->glob;
  178.         int ret = 0;
  179.         uint32_t page_flags = 0;
  180.  
  181.         TTM_ASSERT_LOCKED(&bo->mutex);
  182.         bo->ttm = NULL;
  183.  
  184.         if (bdev->need_dma32)
  185.                 page_flags |= TTM_PAGE_FLAG_DMA32;
  186.  
  187.         switch (bo->type) {
  188.         case ttm_bo_type_device:
  189.                 if (zero_alloc)
  190.                         page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
  191.         case ttm_bo_type_kernel:
  192.                 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  193.                                                       page_flags, glob->dummy_read_page);
  194.                 if (unlikely(bo->ttm == NULL))
  195.                         ret = -ENOMEM;
  196.                 break;
  197.         case ttm_bo_type_sg:
  198.                 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  199.                                                       page_flags | TTM_PAGE_FLAG_SG,
  200.                                                       glob->dummy_read_page);
  201.                 if (unlikely(bo->ttm == NULL)) {
  202.                         ret = -ENOMEM;
  203.                         break;
  204.                 }
  205.                 bo->ttm->sg = bo->sg;
  206.                 break;
  207.         default:
  208.                 pr_err("Illegal buffer object type\n");
  209.                 ret = -EINVAL;
  210.                 break;
  211.         }
  212.  
  213.         return ret;
  214. }
  215.  
  216. static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
  217.                                   struct ttm_mem_reg *mem,
  218.                                   bool evict, bool interruptible,
  219.                                   bool no_wait_gpu)
  220. {
  221.         struct ttm_bo_device *bdev = bo->bdev;
  222.         bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
  223.         bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
  224.         struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
  225.         struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
  226.         int ret = 0;
  227.  
  228.         if (old_is_pci || new_is_pci ||
  229.             ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
  230.                 ret = ttm_mem_io_lock(old_man, true);
  231.                 if (unlikely(ret != 0))
  232.                         goto out_err;
  233.                 ttm_bo_unmap_virtual_locked(bo);
  234.                 ttm_mem_io_unlock(old_man);
  235.         }
  236.  
  237.         /*
  238.          * Create and bind a ttm if required.
  239.          */
  240.  
  241.         if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  242.                 if (bo->ttm == NULL) {
  243.                         bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
  244.                         ret = ttm_bo_add_ttm(bo, zero);
  245.                         if (ret)
  246.                                 goto out_err;
  247.                 }
  248.  
  249.                 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
  250.                 if (ret)
  251.                         goto out_err;
  252.  
  253.                 if (mem->mem_type != TTM_PL_SYSTEM) {
  254.                         ret = ttm_tt_bind(bo->ttm, mem);
  255.                         if (ret)
  256.                                 goto out_err;
  257.                 }
  258.  
  259.                 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
  260.                         if (bdev->driver->move_notify)
  261.                                 bdev->driver->move_notify(bo, mem);
  262.                         bo->mem = *mem;
  263.                         mem->mm_node = NULL;
  264.                         goto moved;
  265.                 }
  266.         }
  267.  
  268.         if (bdev->driver->move_notify)
  269.                 bdev->driver->move_notify(bo, mem);
  270.  
  271.         if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
  272.             !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
  273.                 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
  274.         else if (bdev->driver->move)
  275.                 ret = bdev->driver->move(bo, evict, interruptible,
  276.                                          no_wait_gpu, mem);
  277.         else
  278.                 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
  279.  
  280.         if (ret) {
  281.                 if (bdev->driver->move_notify) {
  282.                         struct ttm_mem_reg tmp_mem = *mem;
  283.                         *mem = bo->mem;
  284.                         bo->mem = tmp_mem;
  285.                         bdev->driver->move_notify(bo, mem);
  286.                         bo->mem = *mem;
  287.                         *mem = tmp_mem;
  288.                 }
  289.  
  290.                 goto out_err;
  291.         }
  292.  
  293. moved:
  294.         if (bo->evicted) {
  295.                 if (bdev->driver->invalidate_caches) {
  296.                         ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
  297.                         if (ret)
  298.                                 pr_err("Can not flush read caches\n");
  299.                 }
  300.                 bo->evicted = false;
  301.         }
  302.  
  303.         if (bo->mem.mm_node) {
  304.                 bo->offset = (bo->mem.start << PAGE_SHIFT) +
  305.                     bdev->man[bo->mem.mem_type].gpu_offset;
  306.                 bo->cur_placement = bo->mem.placement;
  307.         } else
  308.                 bo->offset = 0;
  309.  
  310.         return 0;
  311.  
  312. out_err:
  313.         new_man = &bdev->man[bo->mem.mem_type];
  314.         if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
  315.                 ttm_tt_unbind(bo->ttm);
  316.                 ttm_tt_destroy(bo->ttm);
  317.                 bo->ttm = NULL;
  318.         }
  319.  
  320.         return ret;
  321. }
  322.  
  323. /**
  324.  * Call bo::reserved.
  325.  * Will release GPU memory type usage on destruction.
  326.  * This is the place to put in driver specific hooks to release
  327.  * driver private resources.
  328.  * Will release the bo::reserved lock.
  329.  */
  330.  
  331. static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  332. {
  333.         if (bo->bdev->driver->move_notify)
  334.                 bo->bdev->driver->move_notify(bo, NULL);
  335.  
  336.         if (bo->ttm) {
  337.                 ttm_tt_unbind(bo->ttm);
  338.                 ttm_tt_destroy(bo->ttm);
  339.                 bo->ttm = NULL;
  340.         }
  341.         ttm_bo_mem_put(bo, &bo->mem);
  342.  
  343.         ww_mutex_unlock (&bo->resv->lock);
  344. }
  345.  
  346. static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
  347. {
  348.         struct reservation_object_list *fobj;
  349.         struct fence *fence;
  350.         int i;
  351.  
  352.         fobj = reservation_object_get_list(bo->resv);
  353.         fence = reservation_object_get_excl(bo->resv);
  354.         if (fence && !fence->ops->signaled)
  355.                 fence_enable_sw_signaling(fence);
  356.  
  357.         for (i = 0; fobj && i < fobj->shared_count; ++i) {
  358.                 fence = rcu_dereference_protected(fobj->shared[i],
  359.                                         reservation_object_held(bo->resv));
  360.  
  361.                 if (!fence->ops->signaled)
  362.                         fence_enable_sw_signaling(fence);
  363.         }
  364. }
  365.  
  366. static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
  367. {
  368.         struct ttm_bo_device *bdev = bo->bdev;
  369.         struct ttm_bo_global *glob = bo->glob;
  370.         int put_count;
  371.         int ret;
  372.  
  373.         spin_lock(&glob->lru_lock);
  374.         ret = __ttm_bo_reserve(bo, false, true, false, NULL);
  375.  
  376.         if (!ret) {
  377.                 if (!ttm_bo_wait(bo, false, false, true)) {
  378.                         put_count = ttm_bo_del_from_lru(bo);
  379.  
  380.                         spin_unlock(&glob->lru_lock);
  381.                         ttm_bo_cleanup_memtype_use(bo);
  382.  
  383.                         ttm_bo_list_ref_sub(bo, put_count, true);
  384.  
  385.                         return;
  386.                 } else
  387.                         ttm_bo_flush_all_fences(bo);
  388.  
  389.                 /*
  390.                  * Make NO_EVICT bos immediately available to
  391.                  * shrinkers, now that they are queued for
  392.                  * destruction.
  393.                  */
  394.                 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
  395.                         bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
  396.                         ttm_bo_add_to_lru(bo);
  397.                 }
  398.  
  399.                 __ttm_bo_unreserve(bo);
  400.         }
  401.  
  402.         kref_get(&bo->list_kref);
  403.         list_add_tail(&bo->ddestroy, &bdev->ddestroy);
  404.         spin_unlock(&glob->lru_lock);
  405.  
  406. //      schedule_delayed_work(&bdev->wq,
  407. //                            ((HZ / 100) < 1) ? 1 : HZ / 100);
  408. }
  409.  
  410. /**
  411.  * function ttm_bo_cleanup_refs_and_unlock
  412.  * If bo idle, remove from delayed- and lru lists, and unref.
  413.  * If not idle, do nothing.
  414.  *
  415.  * Must be called with lru_lock and reservation held, this function
  416.  * will drop both before returning.
  417.  *
  418.  * @interruptible         Any sleeps should occur interruptibly.
  419.  * @no_wait_gpu           Never wait for gpu. Return -EBUSY instead.
  420.  */
  421.  
  422. static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
  423.                                           bool interruptible,
  424.                                           bool no_wait_gpu)
  425. {
  426.         struct ttm_bo_global *glob = bo->glob;
  427.         int put_count;
  428.         int ret;
  429.  
  430.         ret = ttm_bo_wait(bo, false, false, true);
  431.  
  432.         if (ret && !no_wait_gpu) {
  433.                 long lret;
  434.                 ww_mutex_unlock(&bo->resv->lock);
  435.                 spin_unlock(&glob->lru_lock);
  436.  
  437.                 lret = reservation_object_wait_timeout_rcu(bo->resv,
  438.                                                            true,
  439.                                                            interruptible,
  440.                                                            30 * HZ);
  441.  
  442.                 if (lret < 0)
  443.                         return lret;
  444.                 else if (lret == 0)
  445.                         return -EBUSY;
  446.  
  447.                 spin_lock(&glob->lru_lock);
  448.                 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
  449.  
  450.                 /*
  451.                  * We raced, and lost, someone else holds the reservation now,
  452.                  * and is probably busy in ttm_bo_cleanup_memtype_use.
  453.                  *
  454.                  * Even if it's not the case, because we finished waiting any
  455.                  * delayed destruction would succeed, so just return success
  456.                  * here.
  457.                  */
  458.                 if (ret) {
  459.                         spin_unlock(&glob->lru_lock);
  460.                         return 0;
  461.                 }
  462.  
  463.                 /*
  464.                  * remove sync_obj with ttm_bo_wait, the wait should be
  465.                  * finished, and no new wait object should have been added.
  466.                  */
  467.                 ret = ttm_bo_wait(bo, false, false, true);
  468.                 WARN_ON(ret);
  469.         }
  470.  
  471.         if (ret || unlikely(list_empty(&bo->ddestroy))) {
  472.                 __ttm_bo_unreserve(bo);
  473.                 spin_unlock(&glob->lru_lock);
  474.                 return ret;
  475.         }
  476.  
  477.         put_count = ttm_bo_del_from_lru(bo);
  478.         list_del_init(&bo->ddestroy);
  479.         ++put_count;
  480.  
  481.         spin_unlock(&glob->lru_lock);
  482.         ttm_bo_cleanup_memtype_use(bo);
  483.  
  484.         ttm_bo_list_ref_sub(bo, put_count, true);
  485.  
  486.         return 0;
  487. }
  488.  
  489. /**
  490.  * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
  491.  * encountered buffers.
  492.  */
  493.  
  494. static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
  495. {
  496.         struct ttm_bo_global *glob = bdev->glob;
  497.         struct ttm_buffer_object *entry = NULL;
  498.         int ret = 0;
  499.  
  500.         spin_lock(&glob->lru_lock);
  501.         if (list_empty(&bdev->ddestroy))
  502.                 goto out_unlock;
  503.  
  504.         entry = list_first_entry(&bdev->ddestroy,
  505.                 struct ttm_buffer_object, ddestroy);
  506.         kref_get(&entry->list_kref);
  507.  
  508.         for (;;) {
  509.                 struct ttm_buffer_object *nentry = NULL;
  510.  
  511.                 if (entry->ddestroy.next != &bdev->ddestroy) {
  512.                         nentry = list_first_entry(&entry->ddestroy,
  513.                                 struct ttm_buffer_object, ddestroy);
  514.                         kref_get(&nentry->list_kref);
  515.                 }
  516.  
  517.                 ret = __ttm_bo_reserve(entry, false, true, false, NULL);
  518.                 if (remove_all && ret) {
  519.                         spin_unlock(&glob->lru_lock);
  520.                         ret = __ttm_bo_reserve(entry, false, false,
  521.                                                false, NULL);
  522.                         spin_lock(&glob->lru_lock);
  523.                 }
  524.  
  525.                 if (!ret)
  526.                         ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
  527.                                                              !remove_all);
  528.                 else
  529.                         spin_unlock(&glob->lru_lock);
  530.  
  531.                 kref_put(&entry->list_kref, ttm_bo_release_list);
  532.                 entry = nentry;
  533.  
  534.                 if (ret || !entry)
  535.                         goto out;
  536.  
  537.                 spin_lock(&glob->lru_lock);
  538.                 if (list_empty(&entry->ddestroy))
  539.                         break;
  540.         }
  541.  
  542. out_unlock:
  543.         spin_unlock(&glob->lru_lock);
  544. out:
  545.         if (entry)
  546.                 kref_put(&entry->list_kref, ttm_bo_release_list);
  547.         return ret;
  548. }
  549.  
  550. static void ttm_bo_delayed_workqueue(struct work_struct *work)
  551. {
  552.         struct ttm_bo_device *bdev =
  553.             container_of(work, struct ttm_bo_device, wq.work);
  554.  
  555.         if (ttm_bo_delayed_delete(bdev, false)) {
  556.                 schedule_delayed_work(&bdev->wq,
  557.                                       ((HZ / 100) < 1) ? 1 : HZ / 100);
  558.         }
  559. }
  560.  
  561. static void ttm_bo_release(struct kref *kref)
  562. {
  563.         struct ttm_buffer_object *bo =
  564.             container_of(kref, struct ttm_buffer_object, kref);
  565.         struct ttm_bo_device *bdev = bo->bdev;
  566.         struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  567.  
  568.         drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
  569.         ttm_mem_io_lock(man, false);
  570.         ttm_mem_io_free_vm(bo);
  571.         ttm_mem_io_unlock(man);
  572.         ttm_bo_cleanup_refs_or_queue(bo);
  573.         kref_put(&bo->list_kref, ttm_bo_release_list);
  574. }
  575.  
  576. void ttm_bo_unref(struct ttm_buffer_object **p_bo)
  577. {
  578.         struct ttm_buffer_object *bo = *p_bo;
  579.  
  580.         *p_bo = NULL;
  581.         kref_put(&bo->kref, ttm_bo_release);
  582. }
  583. EXPORT_SYMBOL(ttm_bo_unref);
  584.  
  585. void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
  586. {
  587.         struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
  588.  
  589.         if (mem->mm_node)
  590.                 (*man->func->put_node)(man, mem);
  591. }
  592. EXPORT_SYMBOL(ttm_bo_mem_put);
  593.  
  594. /**
  595.  * Repeatedly evict memory from the LRU for @mem_type until we create enough
  596.  * space, or we've evicted everything and there isn't enough space.
  597.  */
  598. static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  599.                                         uint32_t mem_type,
  600.                                         const struct ttm_place *place,
  601.                                         struct ttm_mem_reg *mem,
  602.                                         bool interruptible,
  603.                                         bool no_wait_gpu)
  604. {
  605.         struct ttm_bo_device *bdev = bo->bdev;
  606.         struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  607.         int ret;
  608.  
  609.         do {
  610.                 ret = (*man->func->get_node)(man, bo, place, mem);
  611.                 if (unlikely(ret != 0))
  612.                         return ret;
  613.                 if (mem->mm_node)
  614.                         break;
  615. //              ret = ttm_mem_evict_first(bdev, mem_type,
  616. //                                        interruptible, no_wait_gpu);
  617. //              if (unlikely(ret != 0))
  618. //                      return ret;
  619.         } while (1);
  620.         if (mem->mm_node == NULL)
  621.                 return -ENOMEM;
  622.         mem->mem_type = mem_type;
  623.         return 0;
  624. }
  625.  
  626. static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
  627.                                       uint32_t cur_placement,
  628.                                       uint32_t proposed_placement)
  629. {
  630.         uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
  631.         uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
  632.  
  633.         /**
  634.          * Keep current caching if possible.
  635.          */
  636.  
  637.         if ((cur_placement & caching) != 0)
  638.                 result |= (cur_placement & caching);
  639.         else if ((man->default_caching & caching) != 0)
  640.                 result |= man->default_caching;
  641.         else if ((TTM_PL_FLAG_CACHED & caching) != 0)
  642.                 result |= TTM_PL_FLAG_CACHED;
  643.         else if ((TTM_PL_FLAG_WC & caching) != 0)
  644.                 result |= TTM_PL_FLAG_WC;
  645.         else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
  646.                 result |= TTM_PL_FLAG_UNCACHED;
  647.  
  648.         return result;
  649. }
  650.  
  651. static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
  652.                                  uint32_t mem_type,
  653.                                  const struct ttm_place *place,
  654.                                  uint32_t *masked_placement)
  655. {
  656.         uint32_t cur_flags = ttm_bo_type_flags(mem_type);
  657.  
  658.         if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
  659.                 return false;
  660.  
  661.         if ((place->flags & man->available_caching) == 0)
  662.                 return false;
  663.  
  664.         cur_flags |= (place->flags & man->available_caching);
  665.  
  666.         *masked_placement = cur_flags;
  667.         return true;
  668. }
  669.  
  670. /**
  671.  * Creates space for memory region @mem according to its type.
  672.  *
  673.  * This function first searches for free space in compatible memory types in
  674.  * the priority order defined by the driver.  If free space isn't found, then
  675.  * ttm_bo_mem_force_space is attempted in priority order to evict and find
  676.  * space.
  677.  */
  678. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  679.                         struct ttm_placement *placement,
  680.                         struct ttm_mem_reg *mem,
  681.                         bool interruptible,
  682.                         bool no_wait_gpu)
  683. {
  684.         struct ttm_bo_device *bdev = bo->bdev;
  685.         struct ttm_mem_type_manager *man;
  686.         uint32_t mem_type = TTM_PL_SYSTEM;
  687.         uint32_t cur_flags = 0;
  688.         bool type_found = false;
  689.         bool type_ok = false;
  690.         bool has_erestartsys = false;
  691.         int i, ret;
  692.  
  693.         mem->mm_node = NULL;
  694.         for (i = 0; i < placement->num_placement; ++i) {
  695.                 const struct ttm_place *place = &placement->placement[i];
  696.  
  697.                 ret = ttm_mem_type_from_place(place, &mem_type);
  698.                 if (ret)
  699.                         return ret;
  700.                 man = &bdev->man[mem_type];
  701.                 if (!man->has_type || !man->use_type)
  702.                         continue;
  703.  
  704.                 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
  705.                                                 &cur_flags);
  706.  
  707.                 if (!type_ok)
  708.                         continue;
  709.  
  710.                 type_found = true;
  711.                 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  712.                                                   cur_flags);
  713.                 /*
  714.                  * Use the access and other non-mapping-related flag bits from
  715.                  * the memory placement flags to the current flags
  716.                  */
  717.                 ttm_flag_masked(&cur_flags, place->flags,
  718.                                 ~TTM_PL_MASK_MEMTYPE);
  719.  
  720.                 if (mem_type == TTM_PL_SYSTEM)
  721.                         break;
  722.  
  723.                 ret = (*man->func->get_node)(man, bo, place, mem);
  724.                 if (unlikely(ret))
  725.                         return ret;
  726.                
  727.                 if (mem->mm_node)
  728.                         break;
  729.         }
  730.  
  731.         if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
  732.                 mem->mem_type = mem_type;
  733.                 mem->placement = cur_flags;
  734.                 return 0;
  735.         }
  736.  
  737.         for (i = 0; i < placement->num_busy_placement; ++i) {
  738.                 const struct ttm_place *place = &placement->busy_placement[i];
  739.  
  740.                 ret = ttm_mem_type_from_place(place, &mem_type);
  741.                 if (ret)
  742.                         return ret;
  743.                 man = &bdev->man[mem_type];
  744.                 if (!man->has_type || !man->use_type)
  745.                         continue;
  746.                 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
  747.                         continue;
  748.  
  749.                 type_found = true;
  750.                 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  751.                                                   cur_flags);
  752.                 /*
  753.                  * Use the access and other non-mapping-related flag bits from
  754.                  * the memory placement flags to the current flags
  755.                  */
  756.                 ttm_flag_masked(&cur_flags, place->flags,
  757.                                 ~TTM_PL_MASK_MEMTYPE);
  758.  
  759.                 if (mem_type == TTM_PL_SYSTEM) {
  760.                         mem->mem_type = mem_type;
  761.                         mem->placement = cur_flags;
  762.                         mem->mm_node = NULL;
  763.                         return 0;
  764.                 }
  765.  
  766.                 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
  767.                                                 interruptible, no_wait_gpu);
  768.                 if (ret == 0 && mem->mm_node) {
  769.                         mem->placement = cur_flags;
  770.                         return 0;
  771.                 }
  772.                 if (ret == -ERESTARTSYS)
  773.                         has_erestartsys = true;
  774.         }
  775.  
  776.         if (!type_found) {
  777.                 printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
  778.                 return -EINVAL;
  779.         }
  780.  
  781.         return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
  782. }
  783. EXPORT_SYMBOL(ttm_bo_mem_space);
  784.  
  785. static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  786.                         struct ttm_placement *placement,
  787.                         bool interruptible,
  788.                         bool no_wait_gpu)
  789. {
  790.         int ret = 0;
  791.         struct ttm_mem_reg mem;
  792.  
  793.         lockdep_assert_held(&bo->resv->lock.base);
  794.  
  795.         /*
  796.          * FIXME: It's possible to pipeline buffer moves.
  797.          * Have the driver move function wait for idle when necessary,
  798.          * instead of doing it here.
  799.          */
  800.         ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  801.         if (ret)
  802.                 return ret;
  803.         mem.num_pages = bo->num_pages;
  804.         mem.size = mem.num_pages << PAGE_SHIFT;
  805.         mem.page_alignment = bo->mem.page_alignment;
  806.         mem.bus.io_reserved_vm = false;
  807.         mem.bus.io_reserved_count = 0;
  808.         /*
  809.          * Determine where to move the buffer.
  810.          */
  811.         ret = ttm_bo_mem_space(bo, placement, &mem,
  812.                                interruptible, no_wait_gpu);
  813.         if (ret)
  814.                 goto out_unlock;
  815.         ret = ttm_bo_handle_move_mem(bo, &mem, false,
  816.                                      interruptible, no_wait_gpu);
  817. out_unlock:
  818.         if (ret && mem.mm_node)
  819.                 ttm_bo_mem_put(bo, &mem);
  820.         return ret;
  821. }
  822.  
  823. bool ttm_bo_mem_compat(struct ttm_placement *placement,
  824.                        struct ttm_mem_reg *mem,
  825.                        uint32_t *new_flags)
  826. {
  827.         int i;
  828.  
  829.         for (i = 0; i < placement->num_placement; i++) {
  830.                 const struct ttm_place *heap = &placement->placement[i];
  831.                 if (mem->mm_node &&
  832.                     (mem->start < heap->fpfn ||
  833.                      (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
  834.                         continue;
  835.  
  836.                 *new_flags = heap->flags;
  837.                 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  838.                     (*new_flags & mem->placement & TTM_PL_MASK_MEM))
  839.                         return true;
  840.         }
  841.  
  842.         for (i = 0; i < placement->num_busy_placement; i++) {
  843.                 const struct ttm_place *heap = &placement->busy_placement[i];
  844.                 if (mem->mm_node &&
  845.                     (mem->start < heap->fpfn ||
  846.                      (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
  847.                         continue;
  848.  
  849.                 *new_flags = heap->flags;
  850.                 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  851.                     (*new_flags & mem->placement & TTM_PL_MASK_MEM))
  852.                         return true;
  853.         }
  854.  
  855.         return false;
  856. }
  857. EXPORT_SYMBOL(ttm_bo_mem_compat);
  858.  
  859. int ttm_bo_validate(struct ttm_buffer_object *bo,
  860.                         struct ttm_placement *placement,
  861.                         bool interruptible,
  862.                         bool no_wait_gpu)
  863. {
  864.         int ret;
  865.         uint32_t new_flags;
  866.  
  867.         lockdep_assert_held(&bo->resv->lock.base);
  868.         /*
  869.          * Check whether we need to move buffer.
  870.          */
  871.         if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
  872.                 ret = ttm_bo_move_buffer(bo, placement, interruptible,
  873.                                          no_wait_gpu);
  874.                 if (ret)
  875.                         return ret;
  876.         } else {
  877.                 /*
  878.                  * Use the access and other non-mapping-related flag bits from
  879.                  * the compatible memory placement flags to the active flags
  880.                  */
  881.                 ttm_flag_masked(&bo->mem.placement, new_flags,
  882.                                 ~TTM_PL_MASK_MEMTYPE);
  883.         }
  884.         /*
  885.          * We might need to add a TTM.
  886.          */
  887.         if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  888.                 ret = ttm_bo_add_ttm(bo, true);
  889.                 if (ret)
  890.                         return ret;
  891.         }
  892.         return 0;
  893. }
  894. EXPORT_SYMBOL(ttm_bo_validate);
  895.  
  896. int ttm_bo_init(struct ttm_bo_device *bdev,
  897.                 struct ttm_buffer_object *bo,
  898.                 unsigned long size,
  899.                 enum ttm_bo_type type,
  900.                 struct ttm_placement *placement,
  901.                 uint32_t page_alignment,
  902.                 bool interruptible,
  903.                 struct file *persistent_swap_storage,
  904.                 size_t acc_size,
  905.                 struct sg_table *sg,
  906.                 struct reservation_object *resv,
  907.                 void (*destroy) (struct ttm_buffer_object *))
  908. {
  909.     int ret = 0;
  910.     unsigned long num_pages;
  911.     bool locked;
  912.  
  913.     num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  914.     if (num_pages == 0) {
  915.         pr_err("Illegal buffer object size\n");
  916.         if (destroy)
  917.             (*destroy)(bo);
  918.         else
  919.             kfree(bo);
  920.         return -EINVAL;
  921.     }
  922.     bo->destroy = destroy;
  923.  
  924.         kref_init(&bo->kref);
  925.         kref_init(&bo->list_kref);
  926.         atomic_set(&bo->cpu_writers, 0);
  927.         INIT_LIST_HEAD(&bo->lru);
  928.         INIT_LIST_HEAD(&bo->ddestroy);
  929.         INIT_LIST_HEAD(&bo->swap);
  930.         INIT_LIST_HEAD(&bo->io_reserve_lru);
  931.         mutex_init(&bo->wu_mutex);
  932.         bo->bdev = bdev;
  933.         bo->glob = bdev->glob;
  934.         bo->type = type;
  935.         bo->num_pages = num_pages;
  936.         bo->mem.size = num_pages << PAGE_SHIFT;
  937.         bo->mem.mem_type = TTM_PL_SYSTEM;
  938.         bo->mem.num_pages = bo->num_pages;
  939.         bo->mem.mm_node = NULL;
  940.         bo->mem.page_alignment = page_alignment;
  941.         bo->mem.bus.io_reserved_vm = false;
  942.         bo->mem.bus.io_reserved_count = 0;
  943.         bo->priv_flags = 0;
  944.         bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
  945.         bo->persistent_swap_storage = persistent_swap_storage;
  946.         bo->acc_size = acc_size;
  947.         bo->sg = sg;
  948.         if (resv) {
  949.                 bo->resv = resv;
  950.                 lockdep_assert_held(&bo->resv->lock.base);
  951.         } else {
  952.                 bo->resv = &bo->ttm_resv;
  953.                 reservation_object_init(&bo->ttm_resv);
  954.         }
  955.         atomic_inc(&bo->glob->bo_count);
  956.         drm_vma_node_reset(&bo->vma_node);
  957.  
  958.         /*
  959.          * For ttm_bo_type_device buffers, allocate
  960.          * address space from the device.
  961.          */
  962.         if (bo->type == ttm_bo_type_device ||
  963.             bo->type == ttm_bo_type_sg)
  964.                 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
  965.                                          bo->mem.num_pages);
  966.  
  967.         /* passed reservation objects should already be locked,
  968.          * since otherwise lockdep will be angered in radeon.
  969.          */
  970.         if (!resv) {
  971.                 locked = ww_mutex_trylock(&bo->resv->lock);
  972.                 WARN_ON(!locked);
  973.         }
  974.  
  975.         if (likely(!ret))
  976.                 ret = ttm_bo_validate(bo, placement, interruptible, false);
  977.  
  978.         if (!resv) {
  979.                 ttm_bo_unreserve(bo);
  980.  
  981.         } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  982.                 spin_lock(&bo->glob->lru_lock);
  983.                 ttm_bo_add_to_lru(bo);
  984.                 spin_unlock(&bo->glob->lru_lock);
  985.         }
  986.  
  987.         if (unlikely(ret))
  988.                 ttm_bo_unref(&bo);
  989.  
  990.         return ret;
  991. }
  992. EXPORT_SYMBOL(ttm_bo_init);
  993.  
  994. size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
  995.                        unsigned long bo_size,
  996.                        unsigned struct_size)
  997. {
  998.         unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  999.         size_t size = 0;
  1000.  
  1001.         size += ttm_round_pot(struct_size);
  1002.         size += PAGE_ALIGN(npages * sizeof(void *));
  1003.         size += ttm_round_pot(sizeof(struct ttm_tt));
  1004.         return size;
  1005. }
  1006. EXPORT_SYMBOL(ttm_bo_acc_size);
  1007.  
  1008. size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
  1009.                            unsigned long bo_size,
  1010.                            unsigned struct_size)
  1011. {
  1012.         unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1013.         size_t size = 0;
  1014.  
  1015.         size += ttm_round_pot(struct_size);
  1016.         size += PAGE_ALIGN(npages * sizeof(void *));
  1017.         size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
  1018.         size += ttm_round_pot(sizeof(struct ttm_dma_tt));
  1019.         return size;
  1020. }
  1021. EXPORT_SYMBOL(ttm_bo_dma_acc_size);
  1022.  
  1023. int ttm_bo_create(struct ttm_bo_device *bdev,
  1024.                         unsigned long size,
  1025.                         enum ttm_bo_type type,
  1026.                         struct ttm_placement *placement,
  1027.                         uint32_t page_alignment,
  1028.                         bool interruptible,
  1029.                         struct file *persistent_swap_storage,
  1030.                         struct ttm_buffer_object **p_bo)
  1031. {
  1032.         struct ttm_buffer_object *bo;
  1033.         size_t acc_size;
  1034.         int ret;
  1035.  
  1036.         bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  1037.         if (unlikely(bo == NULL))
  1038.                 return -ENOMEM;
  1039.  
  1040.         acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
  1041.         ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
  1042.                           interruptible, persistent_swap_storage, acc_size,
  1043.                           NULL, NULL, NULL);
  1044.         if (likely(ret == 0))
  1045.                 *p_bo = bo;
  1046.  
  1047.         return ret;
  1048. }
  1049. EXPORT_SYMBOL(ttm_bo_create);
  1050.  
  1051. static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
  1052.                                         unsigned mem_type, bool allow_errors)
  1053. {
  1054.         struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1055.         struct ttm_bo_global *glob = bdev->glob;
  1056.         int ret;
  1057.  
  1058.         /*
  1059.          * Can't use standard list traversal since we're unlocking.
  1060.          */
  1061.  
  1062.         spin_lock(&glob->lru_lock);
  1063.         while (!list_empty(&man->lru)) {
  1064.                 spin_unlock(&glob->lru_lock);
  1065.                 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
  1066.                 if (ret) {
  1067.                         if (allow_errors) {
  1068.                                 return ret;
  1069.                         } else {
  1070.                                 pr_err("Cleanup eviction failed\n");
  1071.                         }
  1072.                 }
  1073.                 spin_lock(&glob->lru_lock);
  1074.         }
  1075.         spin_unlock(&glob->lru_lock);
  1076.         return 0;
  1077. }
  1078. int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
  1079.                         unsigned long p_size)
  1080. {
  1081.         int ret = -EINVAL;
  1082.         struct ttm_mem_type_manager *man;
  1083.  
  1084.         BUG_ON(type >= TTM_NUM_MEM_TYPES);
  1085.         man = &bdev->man[type];
  1086.         BUG_ON(man->has_type);
  1087.         man->io_reserve_fastpath = true;
  1088.         man->use_io_reserve_lru = false;
  1089.         mutex_init(&man->io_reserve_mutex);
  1090.         INIT_LIST_HEAD(&man->io_reserve_lru);
  1091.  
  1092.         ret = bdev->driver->init_mem_type(bdev, type, man);
  1093.         if (ret)
  1094.                 return ret;
  1095.         man->bdev = bdev;
  1096.  
  1097.         ret = 0;
  1098.         if (type != TTM_PL_SYSTEM) {
  1099.                 ret = (*man->func->init)(man, p_size);
  1100.                 if (ret)
  1101.                         return ret;
  1102.         }
  1103.         man->has_type = true;
  1104.         man->use_type = true;
  1105.         man->size = p_size;
  1106.  
  1107.         INIT_LIST_HEAD(&man->lru);
  1108.  
  1109.         return 0;
  1110. }
  1111. EXPORT_SYMBOL(ttm_bo_init_mm);
  1112. void ttm_bo_global_release(struct drm_global_reference *ref)
  1113. {
  1114.         struct ttm_bo_global *glob = ref->object;
  1115.  
  1116. }
  1117. EXPORT_SYMBOL(ttm_bo_global_release);
  1118.  
  1119. int ttm_bo_global_init(struct drm_global_reference *ref)
  1120. {
  1121.         struct ttm_bo_global_ref *bo_ref =
  1122.                 container_of(ref, struct ttm_bo_global_ref, ref);
  1123.         struct ttm_bo_global *glob = ref->object;
  1124.         int ret;
  1125.  
  1126.         mutex_init(&glob->device_list_mutex);
  1127.         spin_lock_init(&glob->lru_lock);
  1128.         glob->mem_glob = bo_ref->mem_glob;
  1129.         glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
  1130.  
  1131.         if (unlikely(glob->dummy_read_page == NULL)) {
  1132.                 ret = -ENOMEM;
  1133.                 goto out_no_drp;
  1134.         }
  1135.  
  1136.         INIT_LIST_HEAD(&glob->swap_lru);
  1137.         INIT_LIST_HEAD(&glob->device_list);
  1138.  
  1139.     atomic_set(&glob->bo_count, 0);
  1140.  
  1141.     return 0;
  1142.  
  1143. out_no_drp:
  1144.         kfree(glob);
  1145.         return ret;
  1146. }
  1147. EXPORT_SYMBOL(ttm_bo_global_init);
  1148.  
  1149. int ttm_bo_device_init(struct ttm_bo_device *bdev,
  1150.                        struct ttm_bo_global *glob,
  1151.                        struct ttm_bo_driver *driver,
  1152.                        struct address_space *mapping,
  1153.                        uint64_t file_page_offset,
  1154.                        bool need_dma32)
  1155. {
  1156.         int ret = -EINVAL;
  1157.  
  1158.         bdev->driver = driver;
  1159.  
  1160.         memset(bdev->man, 0, sizeof(bdev->man));
  1161.  
  1162.         /*
  1163.          * Initialize the system memory buffer type.
  1164.          * Other types need to be driver / IOCTL initialized.
  1165.          */
  1166.         ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
  1167.         if (unlikely(ret != 0))
  1168.                 goto out_no_sys;
  1169.  
  1170.         drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
  1171.                                     0x10000000);
  1172.         INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
  1173.         INIT_LIST_HEAD(&bdev->ddestroy);
  1174.         bdev->dev_mapping = mapping;
  1175.         bdev->glob = glob;
  1176.         bdev->need_dma32 = need_dma32;
  1177.         bdev->val_seq = 0;
  1178.         mutex_lock(&glob->device_list_mutex);
  1179.         list_add_tail(&bdev->device_list, &glob->device_list);
  1180.         mutex_unlock(&glob->device_list_mutex);
  1181.  
  1182.         return 0;
  1183. out_no_sys:
  1184.         return ret;
  1185. }
  1186. EXPORT_SYMBOL(ttm_bo_device_init);
  1187.  
  1188. /*
  1189.  * buffer object vm functions.
  1190.  */
  1191.  
  1192. bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1193. {
  1194.         struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1195.  
  1196.         if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  1197.                 if (mem->mem_type == TTM_PL_SYSTEM)
  1198.                         return false;
  1199.  
  1200.                 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
  1201.                         return false;
  1202.  
  1203.                 if (mem->placement & TTM_PL_FLAG_CACHED)
  1204.                         return false;
  1205.         }
  1206.         return true;
  1207. }
  1208.  
  1209. void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
  1210. {
  1211.         struct ttm_bo_device *bdev = bo->bdev;
  1212.  
  1213.         drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
  1214.         ttm_mem_io_free_vm(bo);
  1215. }
  1216.  
  1217. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  1218. {
  1219.         struct ttm_bo_device *bdev = bo->bdev;
  1220.         struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  1221.  
  1222.         ttm_mem_io_lock(man, false);
  1223.         ttm_bo_unmap_virtual_locked(bo);
  1224.         ttm_mem_io_unlock(man);
  1225. }
  1226.  
  1227.  
  1228. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  1229.  
  1230. int ttm_bo_wait(struct ttm_buffer_object *bo,
  1231.                 bool lazy, bool interruptible, bool no_wait)
  1232. {
  1233.         struct reservation_object_list *fobj;
  1234.         struct reservation_object *resv;
  1235.         struct fence *excl;
  1236.         long timeout = 15 * HZ;
  1237.         int i;
  1238.  
  1239.         resv = bo->resv;
  1240.         fobj = reservation_object_get_list(resv);
  1241.         excl = reservation_object_get_excl(resv);
  1242.         if (excl) {
  1243.                 if (!fence_is_signaled(excl)) {
  1244.                         if (no_wait)
  1245.                                 return -EBUSY;
  1246.  
  1247.                         timeout = fence_wait_timeout(excl,
  1248.                                                      interruptible, timeout);
  1249.                 }
  1250.         }
  1251.  
  1252.         for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
  1253.                 struct fence *fence;
  1254.                 fence = rcu_dereference_protected(fobj->shared[i],
  1255.                                                 reservation_object_held(resv));
  1256.  
  1257.                 if (!fence_is_signaled(fence)) {
  1258.                         if (no_wait)
  1259.                                 return -EBUSY;
  1260.  
  1261.                         timeout = fence_wait_timeout(fence,
  1262.                                                      interruptible, timeout);
  1263.                 }
  1264.         }
  1265.  
  1266.         if (timeout < 0)
  1267.                 return timeout;
  1268.  
  1269.         if (timeout == 0)
  1270.                 return -EBUSY;
  1271.  
  1272.         reservation_object_add_excl_fence(resv, NULL);
  1273.         clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
  1274.         return 0;
  1275. }
  1276. EXPORT_SYMBOL(ttm_bo_wait);
  1277.  
  1278. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
  1279. {
  1280.         int ret = 0;
  1281.  
  1282.         /*
  1283.          * Using ttm_bo_reserve makes sure the lru lists are updated.
  1284.          */
  1285.  
  1286.         ret = ttm_bo_reserve(bo, true, no_wait, false, NULL);
  1287.         if (unlikely(ret != 0))
  1288.                 return ret;
  1289.         ret = ttm_bo_wait(bo, false, true, no_wait);
  1290.         if (likely(ret == 0))
  1291.                 atomic_inc(&bo->cpu_writers);
  1292.         ttm_bo_unreserve(bo);
  1293.         return ret;
  1294. }
  1295. EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
  1296.  
  1297. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
  1298. {
  1299.         atomic_dec(&bo->cpu_writers);
  1300. }
  1301. int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
  1302. {
  1303.         int ret;
  1304.  
  1305.         /*
  1306.          * In the absense of a wait_unlocked API,
  1307.          * Use the bo::wu_mutex to avoid triggering livelocks due to
  1308.          * concurrent use of this function. Note that this use of
  1309.          * bo::wu_mutex can go away if we change locking order to
  1310.          * mmap_sem -> bo::reserve.
  1311.          */
  1312.         ret = mutex_lock_interruptible(&bo->wu_mutex);
  1313.         if (unlikely(ret != 0))
  1314.                 return -ERESTARTSYS;
  1315.         if (!ww_mutex_is_locked(&bo->resv->lock))
  1316.                 goto out_unlock;
  1317.         ret = __ttm_bo_reserve(bo, true, false, false, NULL);
  1318.         if (unlikely(ret != 0))
  1319.                 goto out_unlock;
  1320.         __ttm_bo_unreserve(bo);
  1321.  
  1322. out_unlock:
  1323.         mutex_unlock(&bo->wu_mutex);
  1324.         return ret;
  1325. }
  1326.