ttm_bo_manager.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2007-2010 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 "drm_mm.h"
  34. #include <linux/slab.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/module.h>
  37. /**
  38. * Currently we use a spinlock for the lock, but a mutex *may* be
  39. * more appropriate to reduce scheduling latency if the range manager
  40. * ends up with very fragmented allocation patterns.
  41. */
  42. struct ttm_range_manager {
  43. struct drm_mm mm;
  44. spinlock_t lock;
  45. };
  46. static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
  47. struct ttm_buffer_object *bo,
  48. struct ttm_placement *placement,
  49. struct ttm_mem_reg *mem)
  50. {
  51. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  52. struct drm_mm *mm = &rman->mm;
  53. struct drm_mm_node *node = NULL;
  54. unsigned long lpfn;
  55. int ret;
  56. lpfn = placement->lpfn;
  57. if (!lpfn)
  58. lpfn = man->size;
  59. do {
  60. ret = drm_mm_pre_get(mm);
  61. if (unlikely(ret))
  62. return ret;
  63. spin_lock(&rman->lock);
  64. node = drm_mm_search_free_in_range(mm,
  65. mem->num_pages, mem->page_alignment,
  66. placement->fpfn, lpfn, 1);
  67. if (unlikely(node == NULL)) {
  68. spin_unlock(&rman->lock);
  69. return 0;
  70. }
  71. node = drm_mm_get_block_atomic_range(node, mem->num_pages,
  72. mem->page_alignment,
  73. placement->fpfn,
  74. lpfn);
  75. spin_unlock(&rman->lock);
  76. } while (node == NULL);
  77. mem->mm_node = node;
  78. mem->start = node->start;
  79. return 0;
  80. }
  81. static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
  82. struct ttm_mem_reg *mem)
  83. {
  84. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  85. if (mem->mm_node) {
  86. spin_lock(&rman->lock);
  87. drm_mm_put_block(mem->mm_node);
  88. spin_unlock(&rman->lock);
  89. mem->mm_node = NULL;
  90. }
  91. }
  92. static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
  93. unsigned long p_size)
  94. {
  95. struct ttm_range_manager *rman;
  96. int ret;
  97. rman = kzalloc(sizeof(*rman), GFP_KERNEL);
  98. if (!rman)
  99. return -ENOMEM;
  100. ret = drm_mm_init(&rman->mm, 0, p_size);
  101. if (ret) {
  102. kfree(rman);
  103. return ret;
  104. }
  105. spin_lock_init(&rman->lock);
  106. man->priv = rman;
  107. return 0;
  108. }
  109. static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
  110. {
  111. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  112. struct drm_mm *mm = &rman->mm;
  113. spin_lock(&rman->lock);
  114. if (drm_mm_clean(mm)) {
  115. drm_mm_takedown(mm);
  116. spin_unlock(&rman->lock);
  117. kfree(rman);
  118. man->priv = NULL;
  119. return 0;
  120. }
  121. spin_unlock(&rman->lock);
  122. return -EBUSY;
  123. }
  124. static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
  125. const char *prefix)
  126. {
  127. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  128. spin_lock(&rman->lock);
  129. drm_mm_debug_table(&rman->mm, prefix);
  130. spin_unlock(&rman->lock);
  131. }
  132. const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
  133. ttm_bo_man_init,
  134. ttm_bo_man_takedown,
  135. ttm_bo_man_get_node,
  136. ttm_bo_man_put_node,
  137. ttm_bo_man_debug
  138. };
  139. EXPORT_SYMBOL(ttm_bo_manager_func);