vmwgfx_gmrid_manager.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "vmwgfx_drv.h"
  31. #include <drm/ttm/ttm_module.h>
  32. #include <drm/ttm/ttm_bo_driver.h>
  33. #include <drm/ttm/ttm_placement.h>
  34. #include <linux/idr.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/kernel.h>
  37. struct vmwgfx_gmrid_man {
  38. spinlock_t lock;
  39. struct ida gmr_ida;
  40. uint32_t max_gmr_ids;
  41. uint32_t max_gmr_pages;
  42. uint32_t used_gmr_pages;
  43. };
  44. static int vmw_gmrid_man_get_node(struct ttm_mem_type_manager *man,
  45. struct ttm_buffer_object *bo,
  46. struct ttm_placement *placement,
  47. uint32_t flags,
  48. struct ttm_mem_reg *mem)
  49. {
  50. struct vmwgfx_gmrid_man *gman =
  51. (struct vmwgfx_gmrid_man *)man->priv;
  52. int ret = 0;
  53. int id;
  54. mem->mm_node = NULL;
  55. spin_lock(&gman->lock);
  56. if (gman->max_gmr_pages > 0) {
  57. gman->used_gmr_pages += bo->num_pages;
  58. if (unlikely(gman->used_gmr_pages > gman->max_gmr_pages))
  59. goto out_err_locked;
  60. }
  61. do {
  62. spin_unlock(&gman->lock);
  63. if (unlikely(ida_pre_get(&gman->gmr_ida, GFP_KERNEL) == 0)) {
  64. ret = -ENOMEM;
  65. goto out_err;
  66. }
  67. spin_lock(&gman->lock);
  68. ret = ida_get_new(&gman->gmr_ida, &id);
  69. if (unlikely(ret == 0 && id >= gman->max_gmr_ids)) {
  70. ida_remove(&gman->gmr_ida, id);
  71. ret = 0;
  72. goto out_err_locked;
  73. }
  74. } while (ret == -EAGAIN);
  75. if (likely(ret == 0)) {
  76. mem->mm_node = gman;
  77. mem->start = id;
  78. mem->num_pages = bo->num_pages;
  79. } else
  80. goto out_err_locked;
  81. spin_unlock(&gman->lock);
  82. return 0;
  83. out_err:
  84. spin_lock(&gman->lock);
  85. out_err_locked:
  86. gman->used_gmr_pages -= bo->num_pages;
  87. spin_unlock(&gman->lock);
  88. return ret;
  89. }
  90. static void vmw_gmrid_man_put_node(struct ttm_mem_type_manager *man,
  91. struct ttm_mem_reg *mem)
  92. {
  93. struct vmwgfx_gmrid_man *gman =
  94. (struct vmwgfx_gmrid_man *)man->priv;
  95. if (mem->mm_node) {
  96. spin_lock(&gman->lock);
  97. ida_remove(&gman->gmr_ida, mem->start);
  98. gman->used_gmr_pages -= mem->num_pages;
  99. spin_unlock(&gman->lock);
  100. mem->mm_node = NULL;
  101. }
  102. }
  103. static int vmw_gmrid_man_init(struct ttm_mem_type_manager *man,
  104. unsigned long p_size)
  105. {
  106. struct vmw_private *dev_priv =
  107. container_of(man->bdev, struct vmw_private, bdev);
  108. struct vmwgfx_gmrid_man *gman =
  109. kzalloc(sizeof(*gman), GFP_KERNEL);
  110. if (unlikely(gman == NULL))
  111. return -ENOMEM;
  112. spin_lock_init(&gman->lock);
  113. gman->used_gmr_pages = 0;
  114. ida_init(&gman->gmr_ida);
  115. switch (p_size) {
  116. case VMW_PL_GMR:
  117. gman->max_gmr_ids = dev_priv->max_gmr_ids;
  118. gman->max_gmr_pages = dev_priv->max_gmr_pages;
  119. break;
  120. case VMW_PL_MOB:
  121. gman->max_gmr_ids = VMWGFX_NUM_MOB;
  122. gman->max_gmr_pages = dev_priv->max_mob_pages;
  123. break;
  124. default:
  125. BUG();
  126. }
  127. man->priv = (void *) gman;
  128. return 0;
  129. }
  130. static int vmw_gmrid_man_takedown(struct ttm_mem_type_manager *man)
  131. {
  132. struct vmwgfx_gmrid_man *gman =
  133. (struct vmwgfx_gmrid_man *)man->priv;
  134. if (gman) {
  135. ida_destroy(&gman->gmr_ida);
  136. kfree(gman);
  137. }
  138. return 0;
  139. }
  140. static void vmw_gmrid_man_debug(struct ttm_mem_type_manager *man,
  141. const char *prefix)
  142. {
  143. printk(KERN_INFO "%s: No debug info available for the GMR "
  144. "id manager.\n", prefix);
  145. }
  146. const struct ttm_mem_type_manager_func vmw_gmrid_manager_func = {
  147. vmw_gmrid_man_init,
  148. vmw_gmrid_man_takedown,
  149. vmw_gmrid_man_get_node,
  150. vmw_gmrid_man_put_node,
  151. vmw_gmrid_man_debug
  152. };