ttm_bo_manager.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <drm/ttm/ttm_module.h>
  31. #include <drm/ttm/ttm_bo_driver.h>
  32. #include <drm/ttm/ttm_placement.h>
  33. #include <drm/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. const struct ttm_place *place,
  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. enum drm_mm_search_flags sflags = DRM_MM_SEARCH_BEST;
  55. enum drm_mm_allocator_flags aflags = DRM_MM_CREATE_DEFAULT;
  56. unsigned long lpfn;
  57. int ret;
  58. lpfn = place->lpfn;
  59. if (!lpfn)
  60. lpfn = man->size;
  61. node = kzalloc(sizeof(*node), GFP_KERNEL);
  62. if (!node)
  63. return -ENOMEM;
  64. if (place->flags & TTM_PL_FLAG_TOPDOWN) {
  65. sflags = DRM_MM_SEARCH_BELOW;
  66. aflags = DRM_MM_CREATE_TOP;
  67. }
  68. spin_lock(&rman->lock);
  69. ret = drm_mm_insert_node_in_range_generic(mm, node, mem->num_pages,
  70. mem->page_alignment, 0,
  71. place->fpfn, lpfn,
  72. sflags, aflags);
  73. spin_unlock(&rman->lock);
  74. if (unlikely(ret)) {
  75. kfree(node);
  76. } else {
  77. mem->mm_node = node;
  78. mem->start = node->start;
  79. }
  80. return 0;
  81. }
  82. static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
  83. struct ttm_mem_reg *mem)
  84. {
  85. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  86. if (mem->mm_node) {
  87. spin_lock(&rman->lock);
  88. drm_mm_remove_node(mem->mm_node);
  89. spin_unlock(&rman->lock);
  90. kfree(mem->mm_node);
  91. mem->mm_node = NULL;
  92. }
  93. }
  94. static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
  95. unsigned long p_size)
  96. {
  97. struct ttm_range_manager *rman;
  98. rman = kzalloc(sizeof(*rman), GFP_KERNEL);
  99. if (!rman)
  100. return -ENOMEM;
  101. drm_mm_init(&rman->mm, 0, p_size);
  102. spin_lock_init(&rman->lock);
  103. man->priv = rman;
  104. return 0;
  105. }
  106. static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
  107. {
  108. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  109. struct drm_mm *mm = &rman->mm;
  110. spin_lock(&rman->lock);
  111. if (drm_mm_clean(mm)) {
  112. drm_mm_takedown(mm);
  113. spin_unlock(&rman->lock);
  114. kfree(rman);
  115. man->priv = NULL;
  116. return 0;
  117. }
  118. spin_unlock(&rman->lock);
  119. return -EBUSY;
  120. }
  121. static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
  122. const char *prefix)
  123. {
  124. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  125. spin_lock(&rman->lock);
  126. drm_mm_debug_table(&rman->mm, prefix);
  127. spin_unlock(&rman->lock);
  128. }
  129. const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
  130. ttm_bo_man_init,
  131. ttm_bo_man_takedown,
  132. ttm_bo_man_get_node,
  133. ttm_bo_man_put_node,
  134. ttm_bo_man_debug
  135. };
  136. EXPORT_SYMBOL(ttm_bo_manager_func);