vmwgfx_gmrid_manager.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "ttm/ttm_module.h"
  32. #include "ttm/ttm_bo_driver.h"
  33. #include "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. };
  42. static int vmw_gmrid_man_get_node(struct ttm_mem_type_manager *man,
  43. struct ttm_buffer_object *bo,
  44. struct ttm_placement *placement,
  45. struct ttm_mem_reg *mem)
  46. {
  47. struct vmwgfx_gmrid_man *gman =
  48. (struct vmwgfx_gmrid_man *)man->priv;
  49. int ret;
  50. int id;
  51. mem->mm_node = NULL;
  52. do {
  53. if (unlikely(ida_pre_get(&gman->gmr_ida, GFP_KERNEL) == 0))
  54. return -ENOMEM;
  55. spin_lock(&gman->lock);
  56. ret = ida_get_new(&gman->gmr_ida, &id);
  57. if (unlikely(ret == 0 && id >= gman->max_gmr_ids)) {
  58. ida_remove(&gman->gmr_ida, id);
  59. spin_unlock(&gman->lock);
  60. return 0;
  61. }
  62. spin_unlock(&gman->lock);
  63. } while (ret == -EAGAIN);
  64. if (likely(ret == 0)) {
  65. mem->mm_node = gman;
  66. mem->start = id;
  67. }
  68. return ret;
  69. }
  70. static void vmw_gmrid_man_put_node(struct ttm_mem_type_manager *man,
  71. struct ttm_mem_reg *mem)
  72. {
  73. struct vmwgfx_gmrid_man *gman =
  74. (struct vmwgfx_gmrid_man *)man->priv;
  75. if (mem->mm_node) {
  76. spin_lock(&gman->lock);
  77. ida_remove(&gman->gmr_ida, mem->start);
  78. spin_unlock(&gman->lock);
  79. mem->mm_node = NULL;
  80. }
  81. }
  82. static int vmw_gmrid_man_init(struct ttm_mem_type_manager *man,
  83. unsigned long p_size)
  84. {
  85. struct vmwgfx_gmrid_man *gman =
  86. kzalloc(sizeof(*gman), GFP_KERNEL);
  87. if (unlikely(gman == NULL))
  88. return -ENOMEM;
  89. spin_lock_init(&gman->lock);
  90. ida_init(&gman->gmr_ida);
  91. gman->max_gmr_ids = p_size;
  92. man->priv = (void *) gman;
  93. return 0;
  94. }
  95. static int vmw_gmrid_man_takedown(struct ttm_mem_type_manager *man)
  96. {
  97. struct vmwgfx_gmrid_man *gman =
  98. (struct vmwgfx_gmrid_man *)man->priv;
  99. if (gman) {
  100. ida_destroy(&gman->gmr_ida);
  101. kfree(gman);
  102. }
  103. return 0;
  104. }
  105. static void vmw_gmrid_man_debug(struct ttm_mem_type_manager *man,
  106. const char *prefix)
  107. {
  108. printk(KERN_INFO "%s: No debug info available for the GMR "
  109. "id manager.\n", prefix);
  110. }
  111. const struct ttm_mem_type_manager_func vmw_gmrid_manager_func = {
  112. vmw_gmrid_man_init,
  113. vmw_gmrid_man_takedown,
  114. vmw_gmrid_man_get_node,
  115. vmw_gmrid_man_put_node,
  116. vmw_gmrid_man_debug
  117. };