ttm_range_manager.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. /*
  29. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  30. */
  31. #include <drm/ttm/ttm_module.h>
  32. #include <drm/ttm/ttm_bo_driver.h>
  33. #include <drm/ttm/ttm_placement.h>
  34. #include <drm/drm_mm.h>
  35. #include <linux/slab.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/module.h>
  38. /**
  39. * Currently we use a spinlock for the lock, but a mutex *may* be
  40. * more appropriate to reduce scheduling latency if the range manager
  41. * ends up with very fragmented allocation patterns.
  42. */
  43. struct ttm_range_manager {
  44. struct ttm_resource_manager manager;
  45. struct drm_mm mm;
  46. spinlock_t lock;
  47. };
  48. static inline struct ttm_range_manager *to_range_manager(struct ttm_resource_manager *man)
  49. {
  50. return container_of(man, struct ttm_range_manager, manager);
  51. }
  52. static int ttm_range_man_alloc(struct ttm_resource_manager *man,
  53. struct ttm_buffer_object *bo,
  54. const struct ttm_place *place,
  55. struct ttm_resource *mem)
  56. {
  57. struct ttm_range_manager *rman = to_range_manager(man);
  58. struct drm_mm *mm = &rman->mm;
  59. struct drm_mm_node *node;
  60. enum drm_mm_insert_mode mode;
  61. unsigned long lpfn;
  62. int ret;
  63. lpfn = place->lpfn;
  64. if (!lpfn)
  65. lpfn = man->size;
  66. node = kzalloc(sizeof(*node), GFP_KERNEL);
  67. if (!node)
  68. return -ENOMEM;
  69. mode = DRM_MM_INSERT_BEST;
  70. if (place->flags & TTM_PL_FLAG_TOPDOWN)
  71. mode = DRM_MM_INSERT_HIGH;
  72. spin_lock(&rman->lock);
  73. ret = drm_mm_insert_node_in_range(mm, node,
  74. mem->num_pages,
  75. mem->page_alignment, 0,
  76. place->fpfn, lpfn, mode);
  77. spin_unlock(&rman->lock);
  78. if (unlikely(ret)) {
  79. kfree(node);
  80. } else {
  81. mem->mm_node = node;
  82. mem->start = node->start;
  83. }
  84. return ret;
  85. }
  86. static void ttm_range_man_free(struct ttm_resource_manager *man,
  87. struct ttm_resource *mem)
  88. {
  89. struct ttm_range_manager *rman = to_range_manager(man);
  90. if (mem->mm_node) {
  91. spin_lock(&rman->lock);
  92. drm_mm_remove_node(mem->mm_node);
  93. spin_unlock(&rman->lock);
  94. kfree(mem->mm_node);
  95. mem->mm_node = NULL;
  96. }
  97. }
  98. static const struct ttm_resource_manager_func ttm_range_manager_func;
  99. int ttm_range_man_init(struct ttm_bo_device *bdev,
  100. unsigned type, bool use_tt,
  101. unsigned long p_size)
  102. {
  103. struct ttm_resource_manager *man;
  104. struct ttm_range_manager *rman;
  105. rman = kzalloc(sizeof(*rman), GFP_KERNEL);
  106. if (!rman)
  107. return -ENOMEM;
  108. man = &rman->manager;
  109. man->use_tt = use_tt;
  110. man->func = &ttm_range_manager_func;
  111. ttm_resource_manager_init(man, p_size);
  112. drm_mm_init(&rman->mm, 0, p_size);
  113. spin_lock_init(&rman->lock);
  114. ttm_set_driver_manager(bdev, type, &rman->manager);
  115. ttm_resource_manager_set_used(man, true);
  116. return 0;
  117. }
  118. EXPORT_SYMBOL(ttm_range_man_init);
  119. int ttm_range_man_fini(struct ttm_bo_device *bdev,
  120. unsigned type)
  121. {
  122. struct ttm_resource_manager *man = ttm_manager_type(bdev, type);
  123. struct ttm_range_manager *rman = to_range_manager(man);
  124. struct drm_mm *mm = &rman->mm;
  125. int ret;
  126. if (!man)
  127. return 0;
  128. ttm_resource_manager_set_used(man, false);
  129. ret = ttm_resource_manager_force_list_clean(bdev, man);
  130. if (ret)
  131. return ret;
  132. spin_lock(&rman->lock);
  133. drm_mm_clean(mm);
  134. drm_mm_takedown(mm);
  135. spin_unlock(&rman->lock);
  136. ttm_resource_manager_cleanup(man);
  137. ttm_set_driver_manager(bdev, type, NULL);
  138. kfree(rman);
  139. return 0;
  140. }
  141. EXPORT_SYMBOL(ttm_range_man_fini);
  142. static void ttm_range_man_debug(struct ttm_resource_manager *man,
  143. struct drm_printer *printer)
  144. {
  145. struct ttm_range_manager *rman = to_range_manager(man);
  146. spin_lock(&rman->lock);
  147. drm_mm_print(&rman->mm, printer);
  148. spin_unlock(&rman->lock);
  149. }
  150. static const struct ttm_resource_manager_func ttm_range_manager_func = {
  151. .alloc = ttm_range_man_alloc,
  152. .free = ttm_range_man_free,
  153. .debug = ttm_range_man_debug
  154. };