ttm_range_manager.c 6.6 KB

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