ttm_range_manager.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_device.h>
  32. #include <drm/ttm/ttm_placement.h>
  33. #include <drm/ttm/ttm_range_manager.h>
  34. #include <drm/ttm/ttm_bo.h>
  35. #include <drm/drm_mm.h>
  36. #include <linux/export.h>
  37. #include <linux/slab.h>
  38. #include <linux/spinlock.h>
  39. /*
  40. * Currently we use a spinlock for the lock, but a mutex *may* be
  41. * more appropriate to reduce scheduling latency if the range manager
  42. * ends up with very fragmented allocation patterns.
  43. */
  44. struct ttm_range_manager {
  45. struct ttm_resource_manager manager;
  46. struct drm_mm mm;
  47. spinlock_t lock;
  48. };
  49. static inline struct ttm_range_manager *
  50. to_range_manager(struct ttm_resource_manager *man)
  51. {
  52. return container_of(man, struct ttm_range_manager, manager);
  53. }
  54. static int ttm_range_man_alloc(struct ttm_resource_manager *man,
  55. struct ttm_buffer_object *bo,
  56. const struct ttm_place *place,
  57. struct ttm_resource **res)
  58. {
  59. struct ttm_range_manager *rman = to_range_manager(man);
  60. struct ttm_range_mgr_node *node;
  61. struct drm_mm *mm = &rman->mm;
  62. enum drm_mm_insert_mode mode;
  63. unsigned long lpfn;
  64. int ret;
  65. lpfn = place->lpfn;
  66. if (!lpfn)
  67. lpfn = man->size;
  68. node = kzalloc_flex(*node, mm_nodes, 1);
  69. if (!node)
  70. return -ENOMEM;
  71. mode = DRM_MM_INSERT_BEST;
  72. if (place->flags & TTM_PL_FLAG_TOPDOWN)
  73. mode = DRM_MM_INSERT_HIGH;
  74. ttm_resource_init(bo, place, &node->base);
  75. spin_lock(&rman->lock);
  76. ret = drm_mm_insert_node_in_range(mm, &node->mm_nodes[0],
  77. PFN_UP(node->base.size),
  78. bo->page_alignment, 0,
  79. place->fpfn, lpfn, mode);
  80. spin_unlock(&rman->lock);
  81. if (unlikely(ret)) {
  82. ttm_resource_fini(man, &node->base);
  83. kfree(node);
  84. return ret;
  85. }
  86. node->base.start = node->mm_nodes[0].start;
  87. *res = &node->base;
  88. return 0;
  89. }
  90. static void ttm_range_man_free(struct ttm_resource_manager *man,
  91. struct ttm_resource *res)
  92. {
  93. struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res);
  94. struct ttm_range_manager *rman = to_range_manager(man);
  95. spin_lock(&rman->lock);
  96. drm_mm_remove_node(&node->mm_nodes[0]);
  97. spin_unlock(&rman->lock);
  98. ttm_resource_fini(man, res);
  99. kfree(node);
  100. }
  101. static bool ttm_range_man_intersects(struct ttm_resource_manager *man,
  102. struct ttm_resource *res,
  103. const struct ttm_place *place,
  104. size_t size)
  105. {
  106. struct drm_mm_node *node = &to_ttm_range_mgr_node(res)->mm_nodes[0];
  107. u32 num_pages = PFN_UP(size);
  108. /* Don't evict BOs outside of the requested placement range */
  109. if (place->fpfn >= (node->start + num_pages) ||
  110. (place->lpfn && place->lpfn <= node->start))
  111. return false;
  112. return true;
  113. }
  114. static bool ttm_range_man_compatible(struct ttm_resource_manager *man,
  115. struct ttm_resource *res,
  116. const struct ttm_place *place,
  117. size_t size)
  118. {
  119. struct drm_mm_node *node = &to_ttm_range_mgr_node(res)->mm_nodes[0];
  120. u32 num_pages = PFN_UP(size);
  121. if (node->start < place->fpfn ||
  122. (place->lpfn && (node->start + num_pages) > place->lpfn))
  123. return false;
  124. return true;
  125. }
  126. static void ttm_range_man_debug(struct ttm_resource_manager *man,
  127. struct drm_printer *printer)
  128. {
  129. struct ttm_range_manager *rman = to_range_manager(man);
  130. spin_lock(&rman->lock);
  131. drm_mm_print(&rman->mm, printer);
  132. spin_unlock(&rman->lock);
  133. }
  134. static const struct ttm_resource_manager_func ttm_range_manager_func = {
  135. .alloc = ttm_range_man_alloc,
  136. .free = ttm_range_man_free,
  137. .intersects = ttm_range_man_intersects,
  138. .compatible = ttm_range_man_compatible,
  139. .debug = ttm_range_man_debug
  140. };
  141. /**
  142. * ttm_range_man_init_nocheck - Initialise a generic range manager for the
  143. * selected memory type.
  144. *
  145. * @bdev: ttm device
  146. * @type: memory manager type
  147. * @use_tt: if the memory manager uses tt
  148. * @p_size: size of area to be managed in pages.
  149. *
  150. * The range manager is installed for this device in the type slot.
  151. *
  152. * Return: %0 on success or a negative error code on failure
  153. */
  154. int ttm_range_man_init_nocheck(struct ttm_device *bdev,
  155. unsigned type, bool use_tt,
  156. unsigned long p_size)
  157. {
  158. struct ttm_resource_manager *man;
  159. struct ttm_range_manager *rman;
  160. rman = kzalloc_obj(*rman);
  161. if (!rman)
  162. return -ENOMEM;
  163. man = &rman->manager;
  164. man->use_tt = use_tt;
  165. man->func = &ttm_range_manager_func;
  166. ttm_resource_manager_init(man, bdev, p_size);
  167. drm_mm_init(&rman->mm, 0, p_size);
  168. spin_lock_init(&rman->lock);
  169. ttm_set_driver_manager(bdev, type, &rman->manager);
  170. ttm_resource_manager_set_used(man, true);
  171. return 0;
  172. }
  173. EXPORT_SYMBOL(ttm_range_man_init_nocheck);
  174. /**
  175. * ttm_range_man_fini_nocheck - Remove the generic range manager from a slot
  176. * and tear it down.
  177. *
  178. * @bdev: ttm device
  179. * @type: memory manager type
  180. *
  181. * Return: %0 on success or a negative error code on failure
  182. */
  183. int ttm_range_man_fini_nocheck(struct ttm_device *bdev,
  184. unsigned type)
  185. {
  186. struct ttm_resource_manager *man = ttm_manager_type(bdev, type);
  187. struct ttm_range_manager *rman = to_range_manager(man);
  188. struct drm_mm *mm = &rman->mm;
  189. int ret;
  190. if (!man)
  191. return 0;
  192. ttm_resource_manager_set_used(man, false);
  193. ret = ttm_resource_manager_evict_all(bdev, man);
  194. if (ret)
  195. return ret;
  196. spin_lock(&rman->lock);
  197. drm_mm_takedown(mm);
  198. spin_unlock(&rman->lock);
  199. ttm_resource_manager_cleanup(man);
  200. ttm_set_driver_manager(bdev, type, NULL);
  201. kfree(rman);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL(ttm_range_man_fini_nocheck);