mock_engine.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 struct intel_ring *
  48. mock_context_pin(struct intel_engine_cs *engine,
  49. struct i915_gem_context *ctx)
  50. {
  51. i915_gem_context_get(ctx);
  52. return engine->buffer;
  53. }
  54. static void mock_context_unpin(struct intel_engine_cs *engine,
  55. struct i915_gem_context *ctx)
  56. {
  57. i915_gem_context_put(ctx);
  58. }
  59. static int mock_request_alloc(struct drm_i915_gem_request *request)
  60. {
  61. struct mock_request *mock = container_of(request, typeof(*mock), base);
  62. INIT_LIST_HEAD(&mock->link);
  63. mock->delay = 0;
  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->size = sz;
  96. ring->effective_size = sz;
  97. ring->vaddr = (void *)(ring + 1);
  98. INIT_LIST_HEAD(&ring->request_list);
  99. intel_ring_update_space(ring);
  100. return ring;
  101. }
  102. struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
  103. const char *name)
  104. {
  105. struct mock_engine *engine;
  106. static int id;
  107. engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
  108. if (!engine)
  109. return NULL;
  110. engine->base.buffer = mock_ring(&engine->base);
  111. if (!engine->base.buffer) {
  112. kfree(engine);
  113. return NULL;
  114. }
  115. /* minimal engine setup for requests */
  116. engine->base.i915 = i915;
  117. snprintf(engine->base.name, sizeof(engine->base.name), "%s", name);
  118. engine->base.id = id++;
  119. engine->base.status_page.page_addr = (void *)(engine + 1);
  120. engine->base.context_pin = mock_context_pin;
  121. engine->base.context_unpin = mock_context_unpin;
  122. engine->base.request_alloc = mock_request_alloc;
  123. engine->base.emit_flush = mock_emit_flush;
  124. engine->base.emit_breadcrumb = mock_emit_breadcrumb;
  125. engine->base.submit_request = mock_submit_request;
  126. engine->base.timeline =
  127. &i915->gt.global_timeline.engine[engine->base.id];
  128. intel_engine_init_breadcrumbs(&engine->base);
  129. engine->base.breadcrumbs.mock = true; /* prevent touching HW for irqs */
  130. /* fake hw queue */
  131. spin_lock_init(&engine->hw_lock);
  132. setup_timer(&engine->hw_delay,
  133. hw_delay_complete,
  134. (unsigned long)engine);
  135. INIT_LIST_HEAD(&engine->hw_queue);
  136. return &engine->base;
  137. }
  138. void mock_engine_flush(struct intel_engine_cs *engine)
  139. {
  140. struct mock_engine *mock =
  141. container_of(engine, typeof(*mock), base);
  142. struct mock_request *request, *rn;
  143. del_timer_sync(&mock->hw_delay);
  144. spin_lock_irq(&mock->hw_lock);
  145. list_for_each_entry_safe(request, rn, &mock->hw_queue, link) {
  146. list_del_init(&request->link);
  147. mock_seqno_advance(&mock->base, request->base.global_seqno);
  148. }
  149. spin_unlock_irq(&mock->hw_lock);
  150. }
  151. void mock_engine_reset(struct intel_engine_cs *engine)
  152. {
  153. intel_write_status_page(engine, I915_GEM_HWS_INDEX, 0);
  154. }
  155. void mock_engine_free(struct intel_engine_cs *engine)
  156. {
  157. struct mock_engine *mock =
  158. container_of(engine, typeof(*mock), base);
  159. GEM_BUG_ON(timer_pending(&mock->hw_delay));
  160. if (engine->last_retired_context)
  161. engine->context_unpin(engine, engine->last_retired_context);
  162. intel_engine_fini_breadcrumbs(engine);
  163. kfree(engine->buffer);
  164. kfree(engine);
  165. }