ttm_bo_manager.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 drm_mm mm;
  45. spinlock_t lock;
  46. };
  47. static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
  48. struct ttm_buffer_object *bo,
  49. const struct ttm_place *place,
  50. struct ttm_mem_reg *mem)
  51. {
  52. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  53. struct drm_mm *mm = &rman->mm;
  54. struct drm_mm_node *node;
  55. enum drm_mm_insert_mode mode;
  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. mode = DRM_MM_INSERT_BEST;
  65. if (place->flags & TTM_PL_FLAG_TOPDOWN)
  66. mode = DRM_MM_INSERT_HIGH;
  67. spin_lock(&rman->lock);
  68. ret = drm_mm_insert_node_in_range(mm, node,
  69. mem->num_pages,
  70. mem->page_alignment, 0,
  71. place->fpfn, lpfn, mode);
  72. spin_unlock(&rman->lock);
  73. if (unlikely(ret)) {
  74. kfree(node);
  75. } else {
  76. mem->mm_node = node;
  77. mem->start = node->start;
  78. }
  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_remove_node(mem->mm_node);
  88. spin_unlock(&rman->lock);
  89. kfree(mem->mm_node);
  90. mem->mm_node = NULL;
  91. }
  92. }
  93. static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
  94. unsigned long p_size)
  95. {
  96. struct ttm_range_manager *rman;
  97. rman = kzalloc(sizeof(*rman), GFP_KERNEL);
  98. if (!rman)
  99. return -ENOMEM;
  100. drm_mm_init(&rman->mm, 0, p_size);
  101. spin_lock_init(&rman->lock);
  102. man->priv = rman;
  103. return 0;
  104. }
  105. static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
  106. {
  107. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  108. struct drm_mm *mm = &rman->mm;
  109. spin_lock(&rman->lock);
  110. if (drm_mm_clean(mm)) {
  111. drm_mm_takedown(mm);
  112. spin_unlock(&rman->lock);
  113. kfree(rman);
  114. man->priv = NULL;
  115. return 0;
  116. }
  117. spin_unlock(&rman->lock);
  118. return -EBUSY;
  119. }
  120. static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
  121. struct drm_printer *printer)
  122. {
  123. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  124. spin_lock(&rman->lock);
  125. drm_mm_print(&rman->mm, printer);
  126. spin_unlock(&rman->lock);
  127. }
  128. const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
  129. .init = ttm_bo_man_init,
  130. .takedown = ttm_bo_man_takedown,
  131. .get_node = ttm_bo_man_get_node,
  132. .put_node = ttm_bo_man_put_node,
  133. .debug = ttm_bo_man_debug
  134. };
  135. EXPORT_SYMBOL(ttm_bo_manager_func);