mock_engine.c 5.5 KB

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