mock_engine.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 struct intel_ring *
  65. mock_context_pin(struct intel_engine_cs *engine,
  66. struct i915_gem_context *ctx)
  67. {
  68. struct intel_context *ce = to_intel_context(ctx, engine);
  69. if (!ce->pin_count++)
  70. i915_gem_context_get(ctx);
  71. return engine->buffer;
  72. }
  73. static void mock_context_unpin(struct intel_engine_cs *engine,
  74. struct i915_gem_context *ctx)
  75. {
  76. struct intel_context *ce = to_intel_context(ctx, engine);
  77. if (!--ce->pin_count)
  78. i915_gem_context_put(ctx);
  79. }
  80. static int mock_request_alloc(struct i915_request *request)
  81. {
  82. struct mock_request *mock = container_of(request, typeof(*mock), base);
  83. INIT_LIST_HEAD(&mock->link);
  84. mock->delay = 0;
  85. return 0;
  86. }
  87. static int mock_emit_flush(struct i915_request *request,
  88. unsigned int flags)
  89. {
  90. return 0;
  91. }
  92. static void mock_emit_breadcrumb(struct i915_request *request,
  93. u32 *flags)
  94. {
  95. }
  96. static void mock_submit_request(struct i915_request *request)
  97. {
  98. struct mock_request *mock = container_of(request, typeof(*mock), base);
  99. struct mock_engine *engine =
  100. container_of(request->engine, typeof(*engine), base);
  101. i915_request_submit(request);
  102. GEM_BUG_ON(!request->global_seqno);
  103. spin_lock_irq(&engine->hw_lock);
  104. list_add_tail(&mock->link, &engine->hw_queue);
  105. if (mock->link.prev == &engine->hw_queue) {
  106. if (mock->delay)
  107. mod_timer(&engine->hw_delay, jiffies + mock->delay);
  108. else
  109. advance(engine, mock);
  110. }
  111. spin_unlock_irq(&engine->hw_lock);
  112. }
  113. static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
  114. {
  115. const unsigned long sz = PAGE_SIZE / 2;
  116. struct mock_ring *ring;
  117. BUILD_BUG_ON(MIN_SPACE_FOR_ADD_REQUEST > sz);
  118. ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
  119. if (!ring)
  120. return NULL;
  121. i915_timeline_init(engine->i915, &ring->timeline, engine->name);
  122. ring->base.size = sz;
  123. ring->base.effective_size = sz;
  124. ring->base.vaddr = (void *)(ring + 1);
  125. ring->base.timeline = &ring->timeline;
  126. INIT_LIST_HEAD(&ring->base.request_list);
  127. intel_ring_update_space(&ring->base);
  128. return &ring->base;
  129. }
  130. static void mock_ring_free(struct intel_ring *base)
  131. {
  132. struct mock_ring *ring = container_of(base, typeof(*ring), base);
  133. i915_timeline_fini(&ring->timeline);
  134. kfree(ring);
  135. }
  136. struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
  137. const char *name,
  138. int id)
  139. {
  140. struct mock_engine *engine;
  141. GEM_BUG_ON(id >= I915_NUM_ENGINES);
  142. engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
  143. if (!engine)
  144. return NULL;
  145. /* minimal engine setup for requests */
  146. engine->base.i915 = i915;
  147. snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
  148. engine->base.id = id;
  149. engine->base.status_page.page_addr = (void *)(engine + 1);
  150. engine->base.context_pin = mock_context_pin;
  151. engine->base.context_unpin = mock_context_unpin;
  152. engine->base.request_alloc = mock_request_alloc;
  153. engine->base.emit_flush = mock_emit_flush;
  154. engine->base.emit_breadcrumb = mock_emit_breadcrumb;
  155. engine->base.submit_request = mock_submit_request;
  156. i915_timeline_init(i915, &engine->base.timeline, engine->base.name);
  157. intel_engine_init_breadcrumbs(&engine->base);
  158. engine->base.breadcrumbs.mock = true; /* prevent touching HW for irqs */
  159. /* fake hw queue */
  160. spin_lock_init(&engine->hw_lock);
  161. timer_setup(&engine->hw_delay, hw_delay_complete, 0);
  162. INIT_LIST_HEAD(&engine->hw_queue);
  163. engine->base.buffer = mock_ring(&engine->base);
  164. if (!engine->base.buffer)
  165. goto err_breadcrumbs;
  166. return &engine->base;
  167. err_breadcrumbs:
  168. intel_engine_fini_breadcrumbs(&engine->base);
  169. i915_timeline_fini(&engine->base.timeline);
  170. kfree(engine);
  171. return NULL;
  172. }
  173. void mock_engine_flush(struct intel_engine_cs *engine)
  174. {
  175. struct mock_engine *mock =
  176. container_of(engine, typeof(*mock), base);
  177. struct mock_request *request, *rn;
  178. del_timer_sync(&mock->hw_delay);
  179. spin_lock_irq(&mock->hw_lock);
  180. list_for_each_entry_safe(request, rn, &mock->hw_queue, link) {
  181. list_del_init(&request->link);
  182. mock_seqno_advance(&mock->base, request->base.global_seqno);
  183. }
  184. spin_unlock_irq(&mock->hw_lock);
  185. }
  186. void mock_engine_reset(struct intel_engine_cs *engine)
  187. {
  188. intel_write_status_page(engine, I915_GEM_HWS_INDEX, 0);
  189. }
  190. void mock_engine_free(struct intel_engine_cs *engine)
  191. {
  192. struct mock_engine *mock =
  193. container_of(engine, typeof(*mock), base);
  194. GEM_BUG_ON(timer_pending(&mock->hw_delay));
  195. if (engine->last_retired_context)
  196. intel_context_unpin(engine->last_retired_context, engine);
  197. mock_ring_free(engine->buffer);
  198. intel_engine_fini_breadcrumbs(engine);
  199. i915_timeline_fini(&engine->timeline);
  200. kfree(engine);
  201. }