blob: fba6edd9f8199780f43ff5cbc548b4237985156e [file] [log] [blame]
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001/*
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 "i915_drv.h"
26#include "intel_ringbuffer.h"
27#include "intel_lrc.h"
28
29static const struct engine_info {
30 const char *name;
31 unsigned exec_id;
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +010032 enum intel_engine_hw_id hw_id;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010033 u32 mmio_base;
34 unsigned irq_shift;
35 int (*init_legacy)(struct intel_engine_cs *engine);
36 int (*init_execlists)(struct intel_engine_cs *engine);
37} intel_engines[] = {
38 [RCS] = {
39 .name = "render ring",
40 .exec_id = I915_EXEC_RENDER,
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +010041 .hw_id = RCS_HW,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010042 .mmio_base = RENDER_RING_BASE,
43 .irq_shift = GEN8_RCS_IRQ_SHIFT,
44 .init_execlists = logical_render_ring_init,
45 .init_legacy = intel_init_render_ring_buffer,
46 },
47 [BCS] = {
48 .name = "blitter ring",
49 .exec_id = I915_EXEC_BLT,
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +010050 .hw_id = BCS_HW,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010051 .mmio_base = BLT_RING_BASE,
52 .irq_shift = GEN8_BCS_IRQ_SHIFT,
53 .init_execlists = logical_xcs_ring_init,
54 .init_legacy = intel_init_blt_ring_buffer,
55 },
56 [VCS] = {
57 .name = "bsd ring",
58 .exec_id = I915_EXEC_BSD,
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +010059 .hw_id = VCS_HW,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010060 .mmio_base = GEN6_BSD_RING_BASE,
61 .irq_shift = GEN8_VCS1_IRQ_SHIFT,
62 .init_execlists = logical_xcs_ring_init,
63 .init_legacy = intel_init_bsd_ring_buffer,
64 },
65 [VCS2] = {
66 .name = "bsd2 ring",
67 .exec_id = I915_EXEC_BSD,
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +010068 .hw_id = VCS2_HW,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010069 .mmio_base = GEN8_BSD2_RING_BASE,
70 .irq_shift = GEN8_VCS2_IRQ_SHIFT,
71 .init_execlists = logical_xcs_ring_init,
72 .init_legacy = intel_init_bsd2_ring_buffer,
73 },
74 [VECS] = {
75 .name = "video enhancement ring",
76 .exec_id = I915_EXEC_VEBOX,
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +010077 .hw_id = VECS_HW,
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010078 .mmio_base = VEBOX_RING_BASE,
79 .irq_shift = GEN8_VECS_IRQ_SHIFT,
80 .init_execlists = logical_xcs_ring_init,
81 .init_legacy = intel_init_vebox_ring_buffer,
82 },
83};
84
Akash Goel3b3f1652016-10-13 22:44:48 +053085static int
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010086intel_engine_setup(struct drm_i915_private *dev_priv,
87 enum intel_engine_id id)
88{
89 const struct engine_info *info = &intel_engines[id];
Akash Goel3b3f1652016-10-13 22:44:48 +053090 struct intel_engine_cs *engine;
91
92 GEM_BUG_ON(dev_priv->engine[id]);
93 engine = kzalloc(sizeof(*engine), GFP_KERNEL);
94 if (!engine)
95 return -ENOMEM;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +010096
97 engine->id = id;
98 engine->i915 = dev_priv;
99 engine->name = info->name;
100 engine->exec_id = info->exec_id;
Tvrtko Ursulin5ec2cf72016-08-16 17:04:20 +0100101 engine->hw_id = engine->guc_id = info->hw_id;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100102 engine->mmio_base = info->mmio_base;
103 engine->irq_shift = info->irq_shift;
104
Akash Goel3b3f1652016-10-13 22:44:48 +0530105 dev_priv->engine[id] = engine;
106 return 0;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100107}
108
109/**
110 * intel_engines_init() - allocate, populate and init the Engine Command Streamers
111 * @dev: DRM device.
112 *
113 * Return: non-zero if the initialization failed.
114 */
115int intel_engines_init(struct drm_device *dev)
116{
117 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100118 struct intel_device_info *device_info = mkwrite_device_info(dev_priv);
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100119 unsigned int mask = 0;
120 int (*init)(struct intel_engine_cs *engine);
Akash Goel3b3f1652016-10-13 22:44:48 +0530121 struct intel_engine_cs *engine;
122 enum intel_engine_id id;
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100123 unsigned int i;
124 int ret;
125
Chris Wilson6ce21352016-07-29 00:45:35 +0100126 WARN_ON(INTEL_INFO(dev_priv)->ring_mask == 0);
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100127 WARN_ON(INTEL_INFO(dev_priv)->ring_mask &
128 GENMASK(sizeof(mask) * BITS_PER_BYTE - 1, I915_NUM_ENGINES));
129
130 for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
131 if (!HAS_ENGINE(dev_priv, i))
132 continue;
133
134 if (i915.enable_execlists)
135 init = intel_engines[i].init_execlists;
136 else
137 init = intel_engines[i].init_legacy;
138
139 if (!init)
140 continue;
141
Akash Goel3b3f1652016-10-13 22:44:48 +0530142 ret = intel_engine_setup(dev_priv, i);
143 if (ret)
144 goto cleanup;
145
146 ret = init(dev_priv->engine[i]);
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100147 if (ret)
148 goto cleanup;
149
150 mask |= ENGINE_MASK(i);
151 }
152
153 /*
154 * Catch failures to update intel_engines table when the new engines
155 * are added to the driver by a warning and disabling the forgotten
156 * engines.
157 */
Tvrtko Ursulinc1bb1142016-08-10 16:22:10 +0100158 if (WARN_ON(mask != INTEL_INFO(dev_priv)->ring_mask))
159 device_info->ring_mask = mask;
160
161 device_info->num_rings = hweight32(mask);
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100162
163 return 0;
164
165cleanup:
Akash Goel3b3f1652016-10-13 22:44:48 +0530166 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100167 if (i915.enable_execlists)
Akash Goel3b3f1652016-10-13 22:44:48 +0530168 intel_logical_ring_cleanup(engine);
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100169 else
Akash Goel3b3f1652016-10-13 22:44:48 +0530170 intel_engine_cleanup(engine);
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +0100171 }
172
173 return ret;
174}
175
Chris Wilson57f275a2016-08-15 10:49:00 +0100176void intel_engine_init_seqno(struct intel_engine_cs *engine, u32 seqno)
177{
178 struct drm_i915_private *dev_priv = engine->i915;
179
180 /* Our semaphore implementation is strictly monotonic (i.e. we proceed
181 * so long as the semaphore value in the register/page is greater
182 * than the sync value), so whenever we reset the seqno,
183 * so long as we reset the tracking semaphore value to 0, it will
184 * always be before the next request's seqno. If we don't reset
185 * the semaphore value, then when the seqno moves backwards all
186 * future waits will complete instantly (causing rendering corruption).
187 */
188 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
189 I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
190 I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
191 if (HAS_VEBOX(dev_priv))
192 I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
193 }
Chris Wilson51d545d2016-08-15 10:49:02 +0100194 if (dev_priv->semaphore) {
195 struct page *page = i915_vma_first_page(dev_priv->semaphore);
196 void *semaphores;
197
198 /* Semaphores are in noncoherent memory, flush to be safe */
199 semaphores = kmap(page);
Chris Wilson57f275a2016-08-15 10:49:00 +0100200 memset(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
201 0, I915_NUM_ENGINES * gen8_semaphore_seqno_size);
Chris Wilson51d545d2016-08-15 10:49:02 +0100202 drm_clflush_virt_range(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
203 I915_NUM_ENGINES * gen8_semaphore_seqno_size);
Chris Wilson57f275a2016-08-15 10:49:00 +0100204 kunmap(page);
205 }
206 memset(engine->semaphore.sync_seqno, 0,
207 sizeof(engine->semaphore.sync_seqno));
208
209 intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
210 if (engine->irq_seqno_barrier)
211 engine->irq_seqno_barrier(engine);
212 engine->last_submitted_seqno = seqno;
213
214 engine->hangcheck.seqno = seqno;
215
216 /* After manually advancing the seqno, fake the interrupt in case
217 * there are any waiters for that seqno.
218 */
219 intel_engine_wakeup(engine);
220}
221
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100222void intel_engine_init_hangcheck(struct intel_engine_cs *engine)
223{
224 memset(&engine->hangcheck, 0, sizeof(engine->hangcheck));
225}
226
Chris Wilsondcff85c2016-08-05 10:14:11 +0100227static void intel_engine_init_requests(struct intel_engine_cs *engine)
228{
229 init_request_active(&engine->last_request, NULL);
230 INIT_LIST_HEAD(&engine->request_list);
231}
232
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100233/**
234 * intel_engines_setup_common - setup engine state not requiring hw access
235 * @engine: Engine to setup.
236 *
237 * Initializes @engine@ structure members shared between legacy and execlists
238 * submission modes which do not require hardware access.
239 *
240 * Typically done early in the submission mode specific engine setup stage.
241 */
242void intel_engine_setup_common(struct intel_engine_cs *engine)
243{
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100244 INIT_LIST_HEAD(&engine->execlist_queue);
245 spin_lock_init(&engine->execlist_lock);
246
Chris Wilson04769652016-07-20 09:21:11 +0100247 engine->fence_context = fence_context_alloc(1);
248
Chris Wilsondcff85c2016-08-05 10:14:11 +0100249 intel_engine_init_requests(engine);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100250 intel_engine_init_hangcheck(engine);
Chris Wilson115003e92016-08-04 16:32:19 +0100251 i915_gem_batch_pool_init(engine, &engine->batch_pool);
Chris Wilson7756e452016-08-18 17:17:10 +0100252
253 intel_engine_init_cmd_parser(engine);
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100254}
255
Chris Wilsonadc320c2016-08-15 10:48:59 +0100256int intel_engine_create_scratch(struct intel_engine_cs *engine, int size)
257{
258 struct drm_i915_gem_object *obj;
259 struct i915_vma *vma;
260 int ret;
261
262 WARN_ON(engine->scratch);
263
264 obj = i915_gem_object_create_stolen(&engine->i915->drm, size);
265 if (!obj)
266 obj = i915_gem_object_create(&engine->i915->drm, size);
267 if (IS_ERR(obj)) {
268 DRM_ERROR("Failed to allocate scratch page\n");
269 return PTR_ERR(obj);
270 }
271
272 vma = i915_vma_create(obj, &engine->i915->ggtt.base, NULL);
273 if (IS_ERR(vma)) {
274 ret = PTR_ERR(vma);
275 goto err_unref;
276 }
277
278 ret = i915_vma_pin(vma, 0, 4096, PIN_GLOBAL | PIN_HIGH);
279 if (ret)
280 goto err_unref;
281
282 engine->scratch = vma;
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100283 DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
284 engine->name, i915_ggtt_offset(vma));
Chris Wilsonadc320c2016-08-15 10:48:59 +0100285 return 0;
286
287err_unref:
288 i915_gem_object_put(obj);
289 return ret;
290}
291
292static void intel_engine_cleanup_scratch(struct intel_engine_cs *engine)
293{
Chris Wilson19880c42016-08-15 10:49:05 +0100294 i915_vma_unpin_and_release(&engine->scratch);
Chris Wilsonadc320c2016-08-15 10:48:59 +0100295}
296
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100297/**
298 * intel_engines_init_common - initialize cengine state which might require hw access
299 * @engine: Engine to initialize.
300 *
301 * Initializes @engine@ structure members shared between legacy and execlists
302 * submission modes which do require hardware access.
303 *
304 * Typcally done at later stages of submission mode specific engine setup.
305 *
306 * Returns zero on success or an error code on failure.
307 */
308int intel_engine_init_common(struct intel_engine_cs *engine)
309{
310 int ret;
311
312 ret = intel_engine_init_breadcrumbs(engine);
313 if (ret)
314 return ret;
315
Chris Wilson7756e452016-08-18 17:17:10 +0100316 return 0;
Tvrtko Ursulin019bf272016-07-13 16:03:41 +0100317}
Chris Wilson96a945a2016-08-03 13:19:16 +0100318
319/**
320 * intel_engines_cleanup_common - cleans up the engine state created by
321 * the common initiailizers.
322 * @engine: Engine to cleanup.
323 *
324 * This cleans up everything created by the common helpers.
325 */
326void intel_engine_cleanup_common(struct intel_engine_cs *engine)
327{
Chris Wilsonadc320c2016-08-15 10:48:59 +0100328 intel_engine_cleanup_scratch(engine);
329
Chris Wilson96a945a2016-08-03 13:19:16 +0100330 intel_engine_fini_breadcrumbs(engine);
Chris Wilson7756e452016-08-18 17:17:10 +0100331 intel_engine_cleanup_cmd_parser(engine);
Chris Wilson96a945a2016-08-03 13:19:16 +0100332 i915_gem_batch_pool_fini(&engine->batch_pool);
333}
Chris Wilson1b365952016-10-04 21:11:31 +0100334
335u64 intel_engine_get_active_head(struct intel_engine_cs *engine)
336{
337 struct drm_i915_private *dev_priv = engine->i915;
338 u64 acthd;
339
340 if (INTEL_GEN(dev_priv) >= 8)
341 acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
342 RING_ACTHD_UDW(engine->mmio_base));
343 else if (INTEL_GEN(dev_priv) >= 4)
344 acthd = I915_READ(RING_ACTHD(engine->mmio_base));
345 else
346 acthd = I915_READ(ACTHD);
347
348 return acthd;
349}
350
351u64 intel_engine_get_last_batch_head(struct intel_engine_cs *engine)
352{
353 struct drm_i915_private *dev_priv = engine->i915;
354 u64 bbaddr;
355
356 if (INTEL_GEN(dev_priv) >= 8)
357 bbaddr = I915_READ64_2x32(RING_BBADDR(engine->mmio_base),
358 RING_BBADDR_UDW(engine->mmio_base));
359 else
360 bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
361
362 return bbaddr;
363}
Chris Wilson0e704472016-10-12 10:05:17 +0100364
365const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
366{
367 switch (type) {
368 case I915_CACHE_NONE: return " uncached";
369 case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped";
370 case I915_CACHE_L3_LLC: return " L3+LLC";
371 case I915_CACHE_WT: return " WT";
372 default: return "";
373 }
374}
375
376static inline uint32_t
377read_subslice_reg(struct drm_i915_private *dev_priv, int slice,
378 int subslice, i915_reg_t reg)
379{
380 uint32_t mcr;
381 uint32_t ret;
382 enum forcewake_domains fw_domains;
383
384 fw_domains = intel_uncore_forcewake_for_reg(dev_priv, reg,
385 FW_REG_READ);
386 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
387 GEN8_MCR_SELECTOR,
388 FW_REG_READ | FW_REG_WRITE);
389
390 spin_lock_irq(&dev_priv->uncore.lock);
391 intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
392
393 mcr = I915_READ_FW(GEN8_MCR_SELECTOR);
394 /*
395 * The HW expects the slice and sublice selectors to be reset to 0
396 * after reading out the registers.
397 */
398 WARN_ON_ONCE(mcr & (GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK));
399 mcr &= ~(GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK);
400 mcr |= GEN8_MCR_SLICE(slice) | GEN8_MCR_SUBSLICE(subslice);
401 I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
402
403 ret = I915_READ_FW(reg);
404
405 mcr &= ~(GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK);
406 I915_WRITE_FW(GEN8_MCR_SELECTOR, mcr);
407
408 intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
409 spin_unlock_irq(&dev_priv->uncore.lock);
410
411 return ret;
412}
413
414/* NB: please notice the memset */
415void intel_engine_get_instdone(struct intel_engine_cs *engine,
416 struct intel_instdone *instdone)
417{
418 struct drm_i915_private *dev_priv = engine->i915;
419 u32 mmio_base = engine->mmio_base;
420 int slice;
421 int subslice;
422
423 memset(instdone, 0, sizeof(*instdone));
424
425 switch (INTEL_GEN(dev_priv)) {
426 default:
427 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
428
429 if (engine->id != RCS)
430 break;
431
432 instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
433 for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
434 instdone->sampler[slice][subslice] =
435 read_subslice_reg(dev_priv, slice, subslice,
436 GEN7_SAMPLER_INSTDONE);
437 instdone->row[slice][subslice] =
438 read_subslice_reg(dev_priv, slice, subslice,
439 GEN7_ROW_INSTDONE);
440 }
441 break;
442 case 7:
443 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
444
445 if (engine->id != RCS)
446 break;
447
448 instdone->slice_common = I915_READ(GEN7_SC_INSTDONE);
449 instdone->sampler[0][0] = I915_READ(GEN7_SAMPLER_INSTDONE);
450 instdone->row[0][0] = I915_READ(GEN7_ROW_INSTDONE);
451
452 break;
453 case 6:
454 case 5:
455 case 4:
456 instdone->instdone = I915_READ(RING_INSTDONE(mmio_base));
457
458 if (engine->id == RCS)
459 /* HACK: Using the wrong struct member */
460 instdone->slice_common = I915_READ(GEN4_INSTDONE1);
461 break;
462 case 3:
463 case 2:
464 instdone->instdone = I915_READ(GEN2_INSTDONE);
465 break;
466 }
467}