i915_gem_batch_pool.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright © 2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include "i915_drv.h"
  25. /**
  26. * DOC: batch pool
  27. *
  28. * In order to submit batch buffers as 'secure', the software command parser
  29. * must ensure that a batch buffer cannot be modified after parsing. It does
  30. * this by copying the user provided batch buffer contents to a kernel owned
  31. * buffer from which the hardware will actually execute, and by carefully
  32. * managing the address space bindings for such buffers.
  33. *
  34. * The batch pool framework provides a mechanism for the driver to manage a
  35. * set of scratch buffers to use for this purpose. The framework can be
  36. * extended to support other uses cases should they arise.
  37. */
  38. /**
  39. * i915_gem_batch_pool_init() - initialize a batch buffer pool
  40. * @dev: the drm device
  41. * @pool: the batch buffer pool
  42. */
  43. void i915_gem_batch_pool_init(struct drm_device *dev,
  44. struct i915_gem_batch_pool *pool)
  45. {
  46. pool->dev = dev;
  47. INIT_LIST_HEAD(&pool->cache_list);
  48. }
  49. /**
  50. * i915_gem_batch_pool_fini() - clean up a batch buffer pool
  51. * @pool: the pool to clean up
  52. *
  53. * Note: Callers must hold the struct_mutex.
  54. */
  55. void i915_gem_batch_pool_fini(struct i915_gem_batch_pool *pool)
  56. {
  57. WARN_ON(!mutex_is_locked(&pool->dev->struct_mutex));
  58. while (!list_empty(&pool->cache_list)) {
  59. struct drm_i915_gem_object *obj =
  60. list_first_entry(&pool->cache_list,
  61. struct drm_i915_gem_object,
  62. batch_pool_list);
  63. WARN_ON(obj->active);
  64. list_del_init(&obj->batch_pool_list);
  65. drm_gem_object_unreference(&obj->base);
  66. }
  67. }
  68. /**
  69. * i915_gem_batch_pool_get() - select a buffer from the pool
  70. * @pool: the batch buffer pool
  71. * @size: the minimum desired size of the returned buffer
  72. *
  73. * Finds or allocates a batch buffer in the pool with at least the requested
  74. * size. The caller is responsible for any domain, active/inactive, or
  75. * purgeability management for the returned buffer.
  76. *
  77. * Note: Callers must hold the struct_mutex
  78. *
  79. * Return: the selected batch buffer object
  80. */
  81. struct drm_i915_gem_object *
  82. i915_gem_batch_pool_get(struct i915_gem_batch_pool *pool,
  83. size_t size)
  84. {
  85. struct drm_i915_gem_object *obj = NULL;
  86. struct drm_i915_gem_object *tmp, *next;
  87. WARN_ON(!mutex_is_locked(&pool->dev->struct_mutex));
  88. list_for_each_entry_safe(tmp, next,
  89. &pool->cache_list, batch_pool_list) {
  90. if (tmp->active)
  91. continue;
  92. /* While we're looping, do some clean up */
  93. if (tmp->madv == __I915_MADV_PURGED) {
  94. list_del(&tmp->batch_pool_list);
  95. drm_gem_object_unreference(&tmp->base);
  96. continue;
  97. }
  98. /*
  99. * Select a buffer that is at least as big as needed
  100. * but not 'too much' bigger. A better way to do this
  101. * might be to bucket the pool objects based on size.
  102. */
  103. if (tmp->base.size >= size &&
  104. tmp->base.size <= (2 * size)) {
  105. obj = tmp;
  106. break;
  107. }
  108. }
  109. if (!obj) {
  110. obj = i915_gem_alloc_object(pool->dev, size);
  111. if (!obj)
  112. return ERR_PTR(-ENOMEM);
  113. list_add_tail(&obj->batch_pool_list, &pool->cache_list);
  114. }
  115. else
  116. /* Keep list in LRU order */
  117. list_move_tail(&obj->batch_pool_list, &pool->cache_list);
  118. obj->madv = I915_MADV_WILLNEED;
  119. return obj;
  120. }