ttm_bo_manager.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2007-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. #include "ttm/ttm_module.h"
  31. #include "ttm/ttm_bo_driver.h"
  32. #include "ttm/ttm_placement.h"
  33. #include <linux/jiffies.h>
  34. #include <linux/slab.h>
  35. #include <linux/sched.h>
  36. #include <linux/mm.h>
  37. #include <linux/file.h>
  38. #include <linux/module.h>
  39. static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
  40. struct ttm_buffer_object *bo,
  41. struct ttm_placement *placement,
  42. struct ttm_mem_reg *mem)
  43. {
  44. struct ttm_bo_global *glob = man->bdev->glob;
  45. struct drm_mm *mm = man->priv;
  46. struct drm_mm_node *node = NULL;
  47. unsigned long lpfn;
  48. int ret;
  49. lpfn = placement->lpfn;
  50. if (!lpfn)
  51. lpfn = man->size;
  52. do {
  53. ret = drm_mm_pre_get(mm);
  54. if (unlikely(ret))
  55. return ret;
  56. spin_lock(&glob->lru_lock);
  57. node = drm_mm_search_free_in_range(mm,
  58. mem->num_pages, mem->page_alignment,
  59. placement->fpfn, lpfn, 1);
  60. if (unlikely(node == NULL)) {
  61. spin_unlock(&glob->lru_lock);
  62. return 0;
  63. }
  64. node = drm_mm_get_block_atomic_range(node, mem->num_pages,
  65. mem->page_alignment,
  66. placement->fpfn,
  67. lpfn);
  68. spin_unlock(&glob->lru_lock);
  69. } while (node == NULL);
  70. mem->mm_node = node;
  71. mem->start = node->start;
  72. return 0;
  73. }
  74. static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
  75. struct ttm_mem_reg *mem)
  76. {
  77. struct ttm_bo_global *glob = man->bdev->glob;
  78. if (mem->mm_node) {
  79. spin_lock(&glob->lru_lock);
  80. drm_mm_put_block(mem->mm_node);
  81. spin_unlock(&glob->lru_lock);
  82. mem->mm_node = NULL;
  83. }
  84. }
  85. static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
  86. unsigned long p_size)
  87. {
  88. struct drm_mm *mm;
  89. int ret;
  90. mm = kzalloc(sizeof(*mm), GFP_KERNEL);
  91. if (!mm)
  92. return -ENOMEM;
  93. ret = drm_mm_init(mm, 0, p_size);
  94. if (ret) {
  95. kfree(mm);
  96. return ret;
  97. }
  98. man->priv = mm;
  99. return 0;
  100. }
  101. static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
  102. {
  103. struct ttm_bo_global *glob = man->bdev->glob;
  104. struct drm_mm *mm = man->priv;
  105. int ret = 0;
  106. spin_lock(&glob->lru_lock);
  107. if (drm_mm_clean(mm)) {
  108. drm_mm_takedown(mm);
  109. kfree(mm);
  110. man->priv = NULL;
  111. } else
  112. ret = -EBUSY;
  113. spin_unlock(&glob->lru_lock);
  114. return ret;
  115. }
  116. static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
  117. const char *prefix)
  118. {
  119. struct ttm_bo_global *glob = man->bdev->glob;
  120. struct drm_mm *mm = man->priv;
  121. spin_lock(&glob->lru_lock);
  122. drm_mm_debug_table(mm, prefix);
  123. spin_unlock(&glob->lru_lock);
  124. }
  125. const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
  126. ttm_bo_man_init,
  127. ttm_bo_man_takedown,
  128. ttm_bo_man_get_node,
  129. ttm_bo_man_put_node,
  130. ttm_bo_man_debug
  131. };
  132. EXPORT_SYMBOL(ttm_bo_manager_func);