ttm_bo_manager.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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;
  54. enum drm_mm_insert_mode mode;
  55. unsigned long lpfn;
  56. int ret;
  57. lpfn = place->lpfn;
  58. if (!lpfn)
  59. lpfn = man->size;
  60. node = kzalloc(sizeof(*node), GFP_KERNEL);
  61. if (!node)
  62. return -ENOMEM;
  63. mode = DRM_MM_INSERT_BEST;
  64. if (place->flags & TTM_PL_FLAG_TOPDOWN)
  65. mode = DRM_MM_INSERT_HIGH;
  66. spin_lock(&rman->lock);
  67. ret = drm_mm_insert_node_in_range(mm, node,
  68. mem->num_pages,
  69. mem->page_alignment, 0,
  70. place->fpfn, lpfn, mode);
  71. spin_unlock(&rman->lock);
  72. if (unlikely(ret)) {
  73. kfree(node);
  74. } else {
  75. mem->mm_node = node;
  76. mem->start = node->start;
  77. }
  78. return 0;
  79. }
  80. static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
  81. struct ttm_mem_reg *mem)
  82. {
  83. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  84. if (mem->mm_node) {
  85. spin_lock(&rman->lock);
  86. drm_mm_remove_node(mem->mm_node);
  87. spin_unlock(&rman->lock);
  88. kfree(mem->mm_node);
  89. mem->mm_node = NULL;
  90. }
  91. }
  92. static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
  93. unsigned long p_size)
  94. {
  95. struct ttm_range_manager *rman;
  96. rman = kzalloc(sizeof(*rman), GFP_KERNEL);
  97. if (!rman)
  98. return -ENOMEM;
  99. drm_mm_init(&rman->mm, 0, p_size);
  100. spin_lock_init(&rman->lock);
  101. man->priv = rman;
  102. return 0;
  103. }
  104. static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
  105. {
  106. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  107. struct drm_mm *mm = &rman->mm;
  108. spin_lock(&rman->lock);
  109. if (drm_mm_clean(mm)) {
  110. drm_mm_takedown(mm);
  111. spin_unlock(&rman->lock);
  112. kfree(rman);
  113. man->priv = NULL;
  114. return 0;
  115. }
  116. spin_unlock(&rman->lock);
  117. return -EBUSY;
  118. }
  119. static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
  120. struct drm_printer *printer)
  121. {
  122. struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
  123. spin_lock(&rman->lock);
  124. drm_mm_print(&rman->mm, printer);
  125. spin_unlock(&rman->lock);
  126. }
  127. const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
  128. .init = ttm_bo_man_init,
  129. .takedown = ttm_bo_man_takedown,
  130. .get_node = ttm_bo_man_get_node,
  131. .put_node = ttm_bo_man_put_node,
  132. .debug = ttm_bo_man_debug
  133. };
  134. EXPORT_SYMBOL(ttm_bo_manager_func);