mock_engine.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright © 2016 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 "mock_engine.h"
  25. #include "mock_request.h"
  26. struct mock_ring {
  27. struct intel_ring base;
  28. struct i915_timeline timeline;
  29. };
  30. static struct mock_request *first_request(struct mock_engine *engine)
  31. {
  32. return list_first_entry_or_null(&engine->hw_queue,
  33. struct mock_request,
  34. link);
  35. }
  36. static void advance(struct mock_engine *engine,
  37. struct mock_request *request)
  38. {
  39. list_del_init(&request->link);
  40. mock_seqno_advance(&engine->base, request->base.global_seqno);
  41. }
  42. static void hw_delay_complete(struct timer_list *t)
  43. {
  44. struct mock_engine *engine = from_timer(engine, t, hw_delay);
  45. struct mock_request *request;
  46. spin_lock(&engine->hw_lock);
  47. /* Timer fired, first request is complete */
  48. request = first_request(engine);
  49. if (request)
  50. advance(engine, request);
  51. /*
  52. * Also immediately signal any subsequent 0-delay requests, but
  53. * requeue the timer for the next delayed request.
  54. */
  55. while ((request = first_request(engine))) {
  56. if (request->delay) {
  57. mod_timer(&engine->hw_delay, jiffies + request->delay);
  58. break;
  59. }
  60. advance(engine, request);
  61. }
  62. spin_unlock(&engine->hw_lock);
  63. }
  64. static void mock_context_unpin(struct intel_context *ce)
  65. {
  66. i915_gem_context_put(ce->gem_context);
  67. }
  68. static void mock_context_destroy(struct intel_context *ce)
  69. {
  70. GEM_BUG_ON(ce->pin_count);
  71. }
  72. static const struct intel_context_ops mock_context_ops = {
  73. .unpin = mock_context_unpin,
  74. .destroy = mock_context_destroy,
  75. };
  76. static struct intel_context *
  77. mock_context_pin(struct intel_engine_cs *engine,
  78. struct i915_gem_context *ctx)
  79. {
  80. struct intel_context *ce = to_intel_context(ctx, engine);
  81. if (!ce->pin_count++) {
  82. i915_gem_context_get(ctx);
  83. ce->ring = engine->buffer;
  84. ce->ops = &mock_context_ops;
  85. }
  86. return ce;
  87. }
  88. static int mock_request_alloc(struct i915_request *request)
  89. {
  90. struct mock_request *mock = container_of(request, typeof(*mock), base);
  91. INIT_LIST_HEAD(&mock->link);
  92. mock->delay = 0;
  93. return 0;
  94. }
  95. static int mock_emit_flush(struct i915_request *request,
  96. unsigned int flags)
  97. {
  98. return 0;
  99. }
  100. static void mock_emit_breadcrumb(struct i915_request *request,
  101. u32 *flags)
  102. {
  103. }
  104. static void mock_submit_request(struct i915_request *request)
  105. {
  106. struct mock_request *mock = container_of(request, typeof(*mock), base);
  107. struct mock_engine *engine =
  108. container_of(request->engine, typeof(*engine), base);
  109. i915_request_submit(request);
  110. GEM_BUG_ON(!request->global_seqno);
  111. spin_lock_irq(&engine->hw_lock);
  112. list_add_tail(&mock->link, &engine->hw_queue);
  113. if (mock->link.prev == &engine->hw_queue) {
  114. if (mock->delay)
  115. mod_timer(&engine->hw_delay, jiffies + mock->delay);
  116. else
  117. advance(engine, mock);
  118. }
  119. spin_unlock_irq(&engine->hw_lock);
  120. }
  121. static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
  122. {
  123. const unsigned long sz = PAGE_SIZE / 2;
  124. struct mock_ring *ring;
  125. BUILD_BUG_ON(MIN_SPACE_FOR_ADD_REQUEST > sz);
  126. ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
  127. if (!ring)
  128. return NULL;
  129. i915_timeline_init(engine->i915, &ring->timeline, engine->name);
  130. ring->base.size = sz;
  131. ring->base.effective_size = sz;
  132. ring->base.vaddr = (void *)(ring + 1);
  133. ring->base.timeline = &ring->timeline;
  134. INIT_LIST_HEAD(&ring->base.request_list);
  135. intel_ring_update_space(&ring->base);
  136. return &ring->base;
  137. }
  138. static void mock_ring_free(struct intel_ring *base)
  139. {
  140. struct mock_ring *ring = container_of(base, typeof(*ring), base);
  141. i915_timeline_fini(&ring->timeline);
  142. kfree(ring);
  143. }
  144. struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
  145. const char *name,
  146. int id)
  147. {
  148. struct mock_engine *engine;
  149. GEM_BUG_ON(id >= I915_NUM_ENGINES);
  150. engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
  151. if (!engine)
  152. return NULL;
  153. /* minimal engine setup for requests */
  154. engine->base.i915 = i915;
  155. snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
  156. engine->base.id = id;
  157. engine->base.status_page.page_addr = (void *)(engine + 1);
  158. engine->base.context_pin = mock_context_pin;
  159. engine->base.request_alloc = mock_request_alloc;
  160. engine->base.emit_flush = mock_emit_flush;
  161. engine->base.emit_breadcrumb = mock_emit_breadcrumb;
  162. engine->base.submit_request = mock_submit_request;
  163. i915_timeline_init(i915, &engine->base.timeline, engine->base.name);
  164. lockdep_set_subclass(&engine->base.timeline.lock, TIMELINE_ENGINE);
  165. intel_engine_init_breadcrumbs(&engine->base);
  166. engine->base.breadcrumbs.mock = true; /* prevent touching HW for irqs */
  167. /* fake hw queue */
  168. spin_lock_init(&engine->hw_lock);
  169. timer_setup(&engine->hw_delay, hw_delay_complete, 0);
  170. INIT_LIST_HEAD(&engine->hw_queue);
  171. engine->base.buffer = mock_ring(&engine->base);
  172. if (!engine->base.buffer)
  173. goto err_breadcrumbs;
  174. if (IS_ERR(intel_context_pin(i915->kernel_context, &engine->base)))
  175. goto err_ring;
  176. return &engine->base;
  177. err_ring:
  178. mock_ring_free(engine->base.buffer);
  179. err_breadcrumbs:
  180. intel_engine_fini_breadcrumbs(&engine->base);
  181. i915_timeline_fini(&engine->base.timeline);
  182. kfree(engine);
  183. return NULL;
  184. }
  185. void mock_engine_flush(struct intel_engine_cs *engine)
  186. {
  187. struct mock_engine *mock =
  188. container_of(engine, typeof(*mock), base);
  189. struct mock_request *request, *rn;
  190. del_timer_sync(&mock->hw_delay);
  191. spin_lock_irq(&mock->hw_lock);
  192. list_for_each_entry_safe(request, rn, &mock->hw_queue, link) {
  193. list_del_init(&request->link);
  194. mock_seqno_advance(&mock->base, request->base.global_seqno);
  195. }
  196. spin_unlock_irq(&mock->hw_lock);
  197. }
  198. void mock_engine_reset(struct intel_engine_cs *engine)
  199. {
  200. intel_write_status_page(engine, I915_GEM_HWS_INDEX, 0);
  201. }
  202. void mock_engine_free(struct intel_engine_cs *engine)
  203. {
  204. struct mock_engine *mock =
  205. container_of(engine, typeof(*mock), base);
  206. struct intel_context *ce;
  207. GEM_BUG_ON(timer_pending(&mock->hw_delay));
  208. ce = fetch_and_zero(&engine->last_retired_context);
  209. if (ce)
  210. intel_context_unpin(ce);
  211. __intel_context_unpin(engine->i915->kernel_context, engine);
  212. mock_ring_free(engine->buffer);
  213. intel_engine_fini_breadcrumbs(engine);
  214. i915_timeline_fini(&engine->timeline);
  215. kfree(engine);
  216. }