vmwgfx_gmrid_manager.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_placement.h>
  32. #include <linux/idr.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/kernel.h>
  35. struct vmwgfx_gmrid_man {
  36. struct ttm_resource_manager manager;
  37. spinlock_t lock;
  38. struct ida gmr_ida;
  39. uint32_t max_gmr_ids;
  40. uint32_t max_gmr_pages;
  41. uint32_t used_gmr_pages;
  42. uint8_t type;
  43. };
  44. static struct vmwgfx_gmrid_man *to_gmrid_manager(struct ttm_resource_manager *man)
  45. {
  46. return container_of(man, struct vmwgfx_gmrid_man, manager);
  47. }
  48. static int vmw_gmrid_man_get_node(struct ttm_resource_manager *man,
  49. struct ttm_buffer_object *bo,
  50. const struct ttm_place *place,
  51. struct ttm_resource **res)
  52. {
  53. struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
  54. int id;
  55. *res = kmalloc(sizeof(**res), GFP_KERNEL);
  56. if (!*res)
  57. return -ENOMEM;
  58. ttm_resource_init(bo, place, *res);
  59. id = ida_alloc_max(&gman->gmr_ida, gman->max_gmr_ids - 1, GFP_KERNEL);
  60. if (id < 0) {
  61. ttm_resource_fini(man, *res);
  62. kfree(*res);
  63. return id;
  64. }
  65. spin_lock(&gman->lock);
  66. if (gman->max_gmr_pages > 0) {
  67. gman->used_gmr_pages += PFN_UP((*res)->size);
  68. /*
  69. * Because the graphics memory is a soft limit we can try to
  70. * expand it instead of letting the userspace apps crash.
  71. * We're just going to have a sane limit (half of RAM)
  72. * on the number of MOB's that we create and will try to keep
  73. * the system running until we reach that.
  74. */
  75. if (unlikely(gman->used_gmr_pages > gman->max_gmr_pages)) {
  76. const unsigned long max_graphics_pages = totalram_pages() / 2;
  77. uint32_t new_max_pages = 0;
  78. DRM_WARN("vmwgfx: mob memory overflow. Consider increasing guest RAM and graphicsMemory.\n");
  79. vmw_host_printf("vmwgfx, warning: mob memory overflow. Consider increasing guest RAM and graphicsMemory.\n");
  80. if (gman->max_gmr_pages > (max_graphics_pages / 2)) {
  81. DRM_WARN("vmwgfx: guest requires more than half of RAM for graphics.\n");
  82. new_max_pages = max_graphics_pages;
  83. } else
  84. new_max_pages = gman->max_gmr_pages * 2;
  85. if (new_max_pages > gman->max_gmr_pages && new_max_pages >= gman->used_gmr_pages) {
  86. DRM_WARN("vmwgfx: increasing guest mob limits to %u KiB.\n",
  87. ((new_max_pages) << (PAGE_SHIFT - 10)));
  88. gman->max_gmr_pages = new_max_pages;
  89. } else {
  90. char buf[256];
  91. snprintf(buf, sizeof(buf),
  92. "vmwgfx, error: guest graphics is out of memory (mob limit at: %u KiB).\n",
  93. ((gman->max_gmr_pages) << (PAGE_SHIFT - 10)));
  94. vmw_host_printf(buf);
  95. DRM_WARN("%s", buf);
  96. goto nospace;
  97. }
  98. }
  99. }
  100. (*res)->start = id;
  101. spin_unlock(&gman->lock);
  102. return 0;
  103. nospace:
  104. gman->used_gmr_pages -= PFN_UP((*res)->size);
  105. spin_unlock(&gman->lock);
  106. ida_free(&gman->gmr_ida, id);
  107. ttm_resource_fini(man, *res);
  108. kfree(*res);
  109. return -ENOSPC;
  110. }
  111. static void vmw_gmrid_man_put_node(struct ttm_resource_manager *man,
  112. struct ttm_resource *res)
  113. {
  114. struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
  115. ida_free(&gman->gmr_ida, res->start);
  116. spin_lock(&gman->lock);
  117. gman->used_gmr_pages -= PFN_UP(res->size);
  118. spin_unlock(&gman->lock);
  119. ttm_resource_fini(man, res);
  120. kfree(res);
  121. }
  122. static void vmw_gmrid_man_debug(struct ttm_resource_manager *man,
  123. struct drm_printer *printer)
  124. {
  125. struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
  126. BUG_ON(gman->type != VMW_PL_GMR && gman->type != VMW_PL_MOB);
  127. drm_printf(printer, "%s's used: %u pages, max: %u pages, %u id's\n",
  128. (gman->type == VMW_PL_MOB) ? "Mob" : "GMR",
  129. gman->used_gmr_pages, gman->max_gmr_pages, gman->max_gmr_ids);
  130. }
  131. static const struct ttm_resource_manager_func vmw_gmrid_manager_func;
  132. int vmw_gmrid_man_init(struct vmw_private *dev_priv, int type)
  133. {
  134. struct ttm_resource_manager *man;
  135. struct vmwgfx_gmrid_man *gman =
  136. kzalloc(sizeof(*gman), GFP_KERNEL);
  137. if (unlikely(!gman))
  138. return -ENOMEM;
  139. man = &gman->manager;
  140. man->func = &vmw_gmrid_manager_func;
  141. man->use_tt = true;
  142. ttm_resource_manager_init(man, &dev_priv->bdev, 0);
  143. spin_lock_init(&gman->lock);
  144. gman->used_gmr_pages = 0;
  145. ida_init(&gman->gmr_ida);
  146. gman->type = type;
  147. switch (type) {
  148. case VMW_PL_GMR:
  149. gman->max_gmr_ids = dev_priv->max_gmr_ids;
  150. gman->max_gmr_pages = dev_priv->max_gmr_pages;
  151. break;
  152. case VMW_PL_MOB:
  153. gman->max_gmr_ids = VMWGFX_NUM_MOB;
  154. gman->max_gmr_pages = dev_priv->max_mob_pages;
  155. break;
  156. default:
  157. BUG();
  158. }
  159. ttm_set_driver_manager(&dev_priv->bdev, type, &gman->manager);
  160. ttm_resource_manager_set_used(man, true);
  161. return 0;
  162. }
  163. void vmw_gmrid_man_fini(struct vmw_private *dev_priv, int type)
  164. {
  165. struct ttm_resource_manager *man = ttm_manager_type(&dev_priv->bdev, type);
  166. struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
  167. ttm_resource_manager_set_used(man, false);
  168. ttm_resource_manager_evict_all(&dev_priv->bdev, man);
  169. ttm_resource_manager_cleanup(man);
  170. ttm_set_driver_manager(&dev_priv->bdev, type, NULL);
  171. ida_destroy(&gman->gmr_ida);
  172. kfree(gman);
  173. }
  174. static const struct ttm_resource_manager_func vmw_gmrid_manager_func = {
  175. .alloc = vmw_gmrid_man_get_node,
  176. .free = vmw_gmrid_man_put_node,
  177. .debug = vmw_gmrid_man_debug
  178. };