i915_gem_batch_pool.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include "i915_gem_batch_pool.h"
  26. /**
  27. * DOC: batch pool
  28. *
  29. * In order to submit batch buffers as 'secure', the software command parser
  30. * must ensure that a batch buffer cannot be modified after parsing. It does
  31. * this by copying the user provided batch buffer contents to a kernel owned
  32. * buffer from which the hardware will actually execute, and by carefully
  33. * managing the address space bindings for such buffers.
  34. *
  35. * The batch pool framework provides a mechanism for the driver to manage a
  36. * set of scratch buffers to use for this purpose. The framework can be
  37. * extended to support other uses cases should they arise.
  38. */
  39. /**
  40. * i915_gem_batch_pool_init() - initialize a batch buffer pool
  41. * @engine: the associated request submission engine
  42. * @pool: the batch buffer pool
  43. */
  44. void i915_gem_batch_pool_init(struct intel_engine_cs *engine,
  45. struct i915_gem_batch_pool *pool)
  46. {
  47. int n;
  48. pool->engine = engine;
  49. for (n = 0; n < ARRAY_SIZE(pool->cache_list); n++)
  50. INIT_LIST_HEAD(&pool->cache_list[n]);
  51. }
  52. /**
  53. * i915_gem_batch_pool_fini() - clean up a batch buffer pool
  54. * @pool: the pool to clean up
  55. *
  56. * Note: Callers must hold the struct_mutex.
  57. */
  58. void i915_gem_batch_pool_fini(struct i915_gem_batch_pool *pool)
  59. {
  60. int n;
  61. lockdep_assert_held(&pool->engine->i915->drm.struct_mutex);
  62. for (n = 0; n < ARRAY_SIZE(pool->cache_list); n++) {
  63. struct drm_i915_gem_object *obj, *next;
  64. list_for_each_entry_safe(obj, next,
  65. &pool->cache_list[n],
  66. batch_pool_link)
  67. __i915_gem_object_release_unless_active(obj);
  68. INIT_LIST_HEAD(&pool->cache_list[n]);
  69. }
  70. }
  71. /**
  72. * i915_gem_batch_pool_get() - allocate a buffer from the pool
  73. * @pool: the batch buffer pool
  74. * @size: the minimum desired size of the returned buffer
  75. *
  76. * Returns an inactive buffer from @pool with at least @size bytes,
  77. * with the pages pinned. The caller must i915_gem_object_unpin_pages()
  78. * on the returned object.
  79. *
  80. * Note: Callers must hold the struct_mutex
  81. *
  82. * Return: the buffer object or an error pointer
  83. */
  84. struct drm_i915_gem_object *
  85. i915_gem_batch_pool_get(struct i915_gem_batch_pool *pool,
  86. size_t size)
  87. {
  88. struct drm_i915_gem_object *obj;
  89. struct list_head *list;
  90. int n, ret;
  91. lockdep_assert_held(&pool->engine->i915->drm.struct_mutex);
  92. /* Compute a power-of-two bucket, but throw everything greater than
  93. * 16KiB into the same bucket: i.e. the the buckets hold objects of
  94. * (1 page, 2 pages, 4 pages, 8+ pages).
  95. */
  96. n = fls(size >> PAGE_SHIFT) - 1;
  97. if (n >= ARRAY_SIZE(pool->cache_list))
  98. n = ARRAY_SIZE(pool->cache_list) - 1;
  99. list = &pool->cache_list[n];
  100. list_for_each_entry(obj, list, batch_pool_link) {
  101. /* The batches are strictly LRU ordered */
  102. if (i915_gem_object_is_active(obj)) {
  103. struct reservation_object *resv = obj->resv;
  104. if (!reservation_object_test_signaled_rcu(resv, true))
  105. break;
  106. i915_gem_retire_requests(pool->engine->i915);
  107. GEM_BUG_ON(i915_gem_object_is_active(obj));
  108. /*
  109. * The object is now idle, clear the array of shared
  110. * fences before we add a new request. Although, we
  111. * remain on the same engine, we may be on a different
  112. * timeline and so may continually grow the array,
  113. * trapping a reference to all the old fences, rather
  114. * than replace the existing fence.
  115. */
  116. if (rcu_access_pointer(resv->fence)) {
  117. reservation_object_lock(resv, NULL);
  118. reservation_object_add_excl_fence(resv, NULL);
  119. reservation_object_unlock(resv);
  120. }
  121. }
  122. GEM_BUG_ON(!reservation_object_test_signaled_rcu(obj->resv,
  123. true));
  124. if (obj->base.size >= size)
  125. goto found;
  126. }
  127. obj = i915_gem_object_create_internal(pool->engine->i915, size);
  128. if (IS_ERR(obj))
  129. return obj;
  130. found:
  131. ret = i915_gem_object_pin_pages(obj);
  132. if (ret)
  133. return ERR_PTR(ret);
  134. list_move_tail(&obj->batch_pool_link, list);
  135. return obj;
  136. }