Chris Wilson | f97fbf9 | 2017-02-13 17:15:14 +0000 | [diff] [blame] | 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 | |
| 25 | #include "mock_engine.h" |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 26 | #include "mock_request.h" |
Chris Wilson | f97fbf9 | 2017-02-13 17:15:14 +0000 | [diff] [blame] | 27 | |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 28 | static struct mock_request *first_request(struct mock_engine *engine) |
Chris Wilson | f97fbf9 | 2017-02-13 17:15:14 +0000 | [diff] [blame] | 29 | { |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 30 | return list_first_entry_or_null(&engine->hw_queue, |
| 31 | struct mock_request, |
| 32 | link); |
| 33 | } |
| 34 | |
| 35 | static void hw_delay_complete(unsigned long data) |
| 36 | { |
| 37 | struct mock_engine *engine = (typeof(engine))data; |
| 38 | struct mock_request *request; |
| 39 | |
| 40 | spin_lock(&engine->hw_lock); |
| 41 | |
| 42 | request = first_request(engine); |
| 43 | if (request) { |
| 44 | list_del_init(&request->link); |
| 45 | mock_seqno_advance(&engine->base, request->base.global_seqno); |
| 46 | } |
| 47 | |
| 48 | request = first_request(engine); |
| 49 | if (request) |
| 50 | mod_timer(&engine->hw_delay, jiffies + request->delay); |
| 51 | |
| 52 | spin_unlock(&engine->hw_lock); |
| 53 | } |
| 54 | |
Chris Wilson | 266a240 | 2017-05-04 10:33:08 +0100 | [diff] [blame] | 55 | static struct intel_ring * |
| 56 | mock_context_pin(struct intel_engine_cs *engine, |
| 57 | struct i915_gem_context *ctx) |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 58 | { |
| 59 | i915_gem_context_get(ctx); |
Chris Wilson | 266a240 | 2017-05-04 10:33:08 +0100 | [diff] [blame] | 60 | return engine->buffer; |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static void mock_context_unpin(struct intel_engine_cs *engine, |
| 64 | struct i915_gem_context *ctx) |
| 65 | { |
| 66 | i915_gem_context_put(ctx); |
| 67 | } |
| 68 | |
| 69 | static int mock_request_alloc(struct drm_i915_gem_request *request) |
| 70 | { |
| 71 | struct mock_request *mock = container_of(request, typeof(*mock), base); |
| 72 | |
| 73 | INIT_LIST_HEAD(&mock->link); |
| 74 | mock->delay = 0; |
| 75 | |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int mock_emit_flush(struct drm_i915_gem_request *request, |
| 80 | unsigned int flags) |
| 81 | { |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static void mock_emit_breadcrumb(struct drm_i915_gem_request *request, |
| 86 | u32 *flags) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | static void mock_submit_request(struct drm_i915_gem_request *request) |
| 91 | { |
| 92 | struct mock_request *mock = container_of(request, typeof(*mock), base); |
| 93 | struct mock_engine *engine = |
| 94 | container_of(request->engine, typeof(*engine), base); |
| 95 | |
| 96 | i915_gem_request_submit(request); |
| 97 | GEM_BUG_ON(!request->global_seqno); |
| 98 | |
| 99 | spin_lock_irq(&engine->hw_lock); |
| 100 | list_add_tail(&mock->link, &engine->hw_queue); |
| 101 | if (mock->link.prev == &engine->hw_queue) |
| 102 | mod_timer(&engine->hw_delay, jiffies + mock->delay); |
| 103 | spin_unlock_irq(&engine->hw_lock); |
| 104 | } |
| 105 | |
| 106 | static struct intel_ring *mock_ring(struct intel_engine_cs *engine) |
| 107 | { |
| 108 | const unsigned long sz = roundup_pow_of_two(sizeof(struct intel_ring)); |
| 109 | struct intel_ring *ring; |
| 110 | |
| 111 | ring = kzalloc(sizeof(*ring) + sz, GFP_KERNEL); |
| 112 | if (!ring) |
| 113 | return NULL; |
| 114 | |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 115 | ring->size = sz; |
| 116 | ring->effective_size = sz; |
| 117 | ring->vaddr = (void *)(ring + 1); |
| 118 | |
| 119 | INIT_LIST_HEAD(&ring->request_list); |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 120 | intel_ring_update_space(ring); |
| 121 | |
| 122 | return ring; |
| 123 | } |
| 124 | |
| 125 | struct intel_engine_cs *mock_engine(struct drm_i915_private *i915, |
Chris Wilson | 3ec0af7 | 2017-08-09 17:39:30 +0100 | [diff] [blame^] | 126 | const char *name, |
| 127 | int id) |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 128 | { |
| 129 | struct mock_engine *engine; |
Chris Wilson | 3ec0af7 | 2017-08-09 17:39:30 +0100 | [diff] [blame^] | 130 | |
| 131 | GEM_BUG_ON(id >= I915_NUM_ENGINES); |
Chris Wilson | f97fbf9 | 2017-02-13 17:15:14 +0000 | [diff] [blame] | 132 | |
| 133 | engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL); |
| 134 | if (!engine) |
| 135 | return NULL; |
| 136 | |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 137 | engine->base.buffer = mock_ring(&engine->base); |
| 138 | if (!engine->base.buffer) { |
| 139 | kfree(engine); |
| 140 | return NULL; |
| 141 | } |
Chris Wilson | f97fbf9 | 2017-02-13 17:15:14 +0000 | [diff] [blame] | 142 | |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 143 | /* minimal engine setup for requests */ |
| 144 | engine->base.i915 = i915; |
Oscar Mateo | 6e51614 | 2017-04-10 07:34:31 -0700 | [diff] [blame] | 145 | snprintf(engine->base.name, sizeof(engine->base.name), "%s", name); |
Chris Wilson | 3ec0af7 | 2017-08-09 17:39:30 +0100 | [diff] [blame^] | 146 | engine->base.id = id; |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 147 | engine->base.status_page.page_addr = (void *)(engine + 1); |
Chris Wilson | f97fbf9 | 2017-02-13 17:15:14 +0000 | [diff] [blame] | 148 | |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 149 | engine->base.context_pin = mock_context_pin; |
| 150 | engine->base.context_unpin = mock_context_unpin; |
| 151 | engine->base.request_alloc = mock_request_alloc; |
| 152 | engine->base.emit_flush = mock_emit_flush; |
| 153 | engine->base.emit_breadcrumb = mock_emit_breadcrumb; |
| 154 | engine->base.submit_request = mock_submit_request; |
| 155 | |
| 156 | engine->base.timeline = |
| 157 | &i915->gt.global_timeline.engine[engine->base.id]; |
| 158 | |
| 159 | intel_engine_init_breadcrumbs(&engine->base); |
| 160 | engine->base.breadcrumbs.mock = true; /* prevent touching HW for irqs */ |
| 161 | |
| 162 | /* fake hw queue */ |
| 163 | spin_lock_init(&engine->hw_lock); |
| 164 | setup_timer(&engine->hw_delay, |
| 165 | hw_delay_complete, |
| 166 | (unsigned long)engine); |
| 167 | INIT_LIST_HEAD(&engine->hw_queue); |
| 168 | |
| 169 | return &engine->base; |
Chris Wilson | f97fbf9 | 2017-02-13 17:15:14 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void mock_engine_flush(struct intel_engine_cs *engine) |
| 173 | { |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 174 | struct mock_engine *mock = |
| 175 | container_of(engine, typeof(*mock), base); |
| 176 | struct mock_request *request, *rn; |
| 177 | |
| 178 | del_timer_sync(&mock->hw_delay); |
| 179 | |
| 180 | spin_lock_irq(&mock->hw_lock); |
| 181 | list_for_each_entry_safe(request, rn, &mock->hw_queue, link) { |
| 182 | list_del_init(&request->link); |
| 183 | mock_seqno_advance(&mock->base, request->base.global_seqno); |
| 184 | } |
| 185 | spin_unlock_irq(&mock->hw_lock); |
Chris Wilson | f97fbf9 | 2017-02-13 17:15:14 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void mock_engine_reset(struct intel_engine_cs *engine) |
| 189 | { |
| 190 | intel_write_status_page(engine, I915_GEM_HWS_INDEX, 0); |
| 191 | } |
Chris Wilson | 0daf011 | 2017-02-13 17:15:19 +0000 | [diff] [blame] | 192 | |
| 193 | void mock_engine_free(struct intel_engine_cs *engine) |
| 194 | { |
| 195 | struct mock_engine *mock = |
| 196 | container_of(engine, typeof(*mock), base); |
| 197 | |
| 198 | GEM_BUG_ON(timer_pending(&mock->hw_delay)); |
| 199 | |
| 200 | if (engine->last_retired_context) |
| 201 | engine->context_unpin(engine, engine->last_retired_context); |
| 202 | |
| 203 | intel_engine_fini_breadcrumbs(engine); |
| 204 | |
| 205 | kfree(engine->buffer); |
| 206 | kfree(engine); |
| 207 | } |