vmwgfx_gmrid_manager.c 4.5 KB

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