mock_engine.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. static struct mock_request *first_request(struct mock_engine *engine)
  27. {
  28. return list_first_entry_or_null(&engine->hw_queue,
  29. struct mock_request,
  30. link);
  31. }
  32. static void advance(struct mock_engine *engine,
  33. struct mock_request *request)
  34. {
  35. list_del_init(&request->link);
  36. mock_seqno_advance(&engine->base, request->base.global_seqno);
  37. }
  38. static void hw_delay_complete(struct timer_list *t)
  39. {
  40. struct mock_engine *engine = from_timer(engine, t, hw_delay);
  41. struct mock_request *request;
  42. spin_lock(&engine->hw_lock);
  43. /* Timer fired, first request is complete */
  44. request = first_request(engine);
  45. if (request)
  46. advance(engine, request);
  47. /*
  48. * Also immediately signal any subsequent 0-delay requests, but
  49. * requeue the timer for the next delayed request.
  50. */
  51. while ((request = first_request(engine))) {
  52. if (request->delay) {
  53. mod_timer(&engine->hw_delay, jiffies + request->delay);
  54. break;
  55. }
  56. advance(engine, request);
  57. }
  58. spin_unlock(&engine->hw_lock);
  59. }
  60. static struct intel_ring *
  61. mock_context_pin(struct intel_engine_cs *engine,
  62. struct i915_gem_context *ctx)
  63. {
  64. i915_gem_context_get(ctx);
  65. return engine->buffer;
  66. }
  67. static void mock_context_unpin(struct intel_engine_cs *engine,
  68. struct i915_gem_context *ctx)
  69. {
  70. i915_gem_context_put(ctx);
  71. }
  72. static int mock_request_alloc(struct drm_i915_gem_request *request)
  73. {
  74. struct mock_request *mock = container_of(request, typeof(*mock), base);
  75. INIT_LIST_HEAD(&mock->link);
  76. mock->delay = 0;
  77. return 0;
  78. }
  79. static int mock_emit_flush(struct drm_i915_gem_request *request,
  80. unsigned int flags)
  81. {
  82. return 0;
  83. }
  84. static void mock_emit_breadcrumb(struct drm_i915_gem_request *request,
  85. u32 *flags)
  86. {
  87. }
  88. static void mock_submit_request(struct drm_i915_gem_request *request)
  89. {
  90. struct mock_request *mock = container_of(request, typeof(*mock), base);
  91. struct mock_engine *engine =
  92. container_of(request->engine, typeof(*engine), base);
  93. i915_gem_request_submit(request);
  94. GEM_BUG_ON(!request->global_seqno);
  95. spin_lock_irq(&engine->hw_lock);
  96. list_add_tail(&mock->link, &engine->hw_queue);
  97. if (mock->link.prev == &engine->hw_queue) {
  98. if (mock->delay)
  99. mod_timer(&engine->hw_delay, jiffies + mock->delay);
  100. else
  101. advance(engine, mock);
  102. }
  103. spin_unlock_irq(&engine->hw_lock);
  104. }
  105. static struct intel_ring *mock_ring(struct intel_engine_cs *engine)
  106. {
  107. const unsigned long sz = PAGE_SIZE / 2;
  108. struct intel_ring *ring;
  109. BUILD_BUG_ON(MIN_SPACE_FOR_ADD_REQUEST > sz);
  110. ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL);
  111. if (!ring)
  112. return NULL;
  113. ring->size = sz;
  114. ring->effective_size = sz;
  115. ring->vaddr = (void *)(ring + 1);
  116. INIT_LIST_HEAD(&ring->request_list);
  117. intel_ring_update_space(ring);
  118. return ring;
  119. }
  120. struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
  121. const char *name,
  122. int id)
  123. {
  124. struct mock_engine *engine;
  125. GEM_BUG_ON(id >= I915_NUM_ENGINES);
  126. engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
  127. if (!engine)
  128. return NULL;
  129. engine->base.buffer = mock_ring(&engine->base);
  130. if (!engine->base.buffer) {
  131. kfree(engine);
  132. return NULL;
  133. }
  134. /* minimal engine setup for requests */
  135. engine->base.i915 = i915;
  136. snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
  137. engine->base.id = id;
  138. engine->base.status_page.page_addr = (void *)(engine + 1);
  139. engine->base.context_pin = mock_context_pin;
  140. engine->base.context_unpin = mock_context_unpin;
  141. engine->base.request_alloc = mock_request_alloc;
  142. engine->base.emit_flush = mock_emit_flush;
  143. engine->base.emit_breadcrumb = mock_emit_breadcrumb;
  144. engine->base.submit_request = mock_submit_request;
  145. engine->base.timeline =
  146. &i915->gt.global_timeline.engine[engine->base.id];
  147. intel_engine_init_breadcrumbs(&engine->base);
  148. engine->base.breadcrumbs.mock = true; /* prevent touching HW for irqs */
  149. /* fake hw queue */
  150. spin_lock_init(&engine->hw_lock);
  151. timer_setup(&engine->hw_delay, hw_delay_complete, 0);
  152. INIT_LIST_HEAD(&engine->hw_queue);
  153. return &engine->base;
  154. }
  155. void mock_engine_flush(struct intel_engine_cs *engine)
  156. {
  157. struct mock_engine *mock =
  158. container_of(engine, typeof(*mock), base);
  159. struct mock_request *request, *rn;
  160. del_timer_sync(&mock->hw_delay);
  161. spin_lock_irq(&mock->hw_lock);
  162. list_for_each_entry_safe(request, rn, &mock->hw_queue, link) {
  163. list_del_init(&request->link);
  164. mock_seqno_advance(&mock->base, request->base.global_seqno);
  165. }
  166. spin_unlock_irq(&mock->hw_lock);
  167. }
  168. void mock_engine_reset(struct intel_engine_cs *engine)
  169. {
  170. intel_write_status_page(engine, I915_GEM_HWS_INDEX, 0);
  171. }
  172. void mock_engine_free(struct intel_engine_cs *engine)
  173. {
  174. struct mock_engine *mock =
  175. container_of(engine, typeof(*mock), base);
  176. GEM_BUG_ON(timer_pending(&mock->hw_delay));
  177. if (engine->last_retired_context)
  178. engine->context_unpin(engine, engine->last_retired_context);
  179. intel_engine_fini_breadcrumbs(engine);
  180. kfree(engine->buffer);
  181. kfree(engine);
  182. }