vmwgfx_gmrid_manager.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2007-2010 VMware, Inc., Palo Alto, CA., USA
  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 id;
  52. mem->mm_node = NULL;
  53. id = ida_alloc_max(&gman->gmr_ida, gman->max_gmr_ids - 1, GFP_KERNEL);
  54. if (id < 0)
  55. return (id != -ENOMEM ? 0 : id);
  56. spin_lock(&gman->lock);
  57. if (gman->max_gmr_pages > 0) {
  58. gman->used_gmr_pages += bo->num_pages;
  59. if (unlikely(gman->used_gmr_pages > gman->max_gmr_pages))
  60. goto nospace;
  61. }
  62. mem->mm_node = gman;
  63. mem->start = id;
  64. mem->num_pages = bo->num_pages;
  65. spin_unlock(&gman->lock);
  66. return 0;
  67. nospace:
  68. gman->used_gmr_pages -= bo->num_pages;
  69. spin_unlock(&gman->lock);
  70. ida_free(&gman->gmr_ida, id);
  71. return 0;
  72. }
  73. static void vmw_gmrid_man_put_node(struct ttm_mem_type_manager *man,
  74. struct ttm_mem_reg *mem)
  75. {
  76. struct vmwgfx_gmrid_man *gman =
  77. (struct vmwgfx_gmrid_man *)man->priv;
  78. if (mem->mm_node) {
  79. ida_free(&gman->gmr_ida, mem->start);
  80. spin_lock(&gman->lock);
  81. gman->used_gmr_pages -= mem->num_pages;
  82. spin_unlock(&gman->lock);
  83. mem->mm_node = NULL;
  84. }
  85. }
  86. static int vmw_gmrid_man_init(struct ttm_mem_type_manager *man,
  87. unsigned long p_size)
  88. {
  89. struct vmw_private *dev_priv =
  90. container_of(man->bdev, struct vmw_private, bdev);
  91. struct vmwgfx_gmrid_man *gman =
  92. kzalloc(sizeof(*gman), GFP_KERNEL);
  93. if (unlikely(!gman))
  94. return -ENOMEM;
  95. spin_lock_init(&gman->lock);
  96. gman->used_gmr_pages = 0;
  97. ida_init(&gman->gmr_ida);
  98. switch (p_size) {
  99. case VMW_PL_GMR:
  100. gman->max_gmr_ids = dev_priv->max_gmr_ids;
  101. gman->max_gmr_pages = dev_priv->max_gmr_pages;
  102. break;
  103. case VMW_PL_MOB:
  104. gman->max_gmr_ids = VMWGFX_NUM_MOB;
  105. gman->max_gmr_pages = dev_priv->max_mob_pages;
  106. break;
  107. default:
  108. BUG();
  109. }
  110. man->priv = (void *) gman;
  111. return 0;
  112. }
  113. static int vmw_gmrid_man_takedown(struct ttm_mem_type_manager *man)
  114. {
  115. struct vmwgfx_gmrid_man *gman =
  116. (struct vmwgfx_gmrid_man *)man->priv;
  117. if (gman) {
  118. ida_destroy(&gman->gmr_ida);
  119. kfree(gman);
  120. }
  121. return 0;
  122. }
  123. static void vmw_gmrid_man_debug(struct ttm_mem_type_manager *man,
  124. struct drm_printer *printer)
  125. {
  126. drm_printf(printer, "No debug info available for the GMR id manager\n");
  127. }
  128. const struct ttm_mem_type_manager_func vmw_gmrid_manager_func = {
  129. .init = vmw_gmrid_man_init,
  130. .takedown = vmw_gmrid_man_takedown,
  131. .get_node = vmw_gmrid_man_get_node,
  132. .put_node = vmw_gmrid_man_put_node,
  133. .debug = vmw_gmrid_man_debug
  134. };