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 <drm/drm_print.h>
26
27 #include "gem/i915_gem_context.h"
28
29 #include "i915_drv.h"
30
31 #include "intel_breadcrumbs.h"
32 #include "intel_context.h"
33 #include "intel_engine.h"
34 #include "intel_engine_pm.h"
35 #include "intel_engine_user.h"
36 #include "intel_gt.h"
37 #include "intel_gt_requests.h"
38 #include "intel_gt_pm.h"
39 #include "intel_lrc.h"
40 #include "intel_reset.h"
41 #include "intel_ring.h"
42
43 /* Haswell does have the CXT_SIZE register however it does not appear to be
44 * valid. Now, docs explain in dwords what is in the context object. The full
45 * size is 70720 bytes, however, the power context and execlist context will
46 * never be saved (power context is stored elsewhere, and execlists don't work
47 * on HSW) - so the final size, including the extra state required for the
48 * Resource Streamer, is 66944 bytes, which rounds to 17 pages.
49 */
50 #define HSW_CXT_TOTAL_SIZE (17 * PAGE_SIZE)
51
52 #define DEFAULT_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
53 #define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
54 #define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
55 #define GEN10_LR_CONTEXT_RENDER_SIZE (18 * PAGE_SIZE)
56 #define GEN11_LR_CONTEXT_RENDER_SIZE (14 * PAGE_SIZE)
57
58 #define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
59
60 #define MAX_MMIO_BASES 3
61 struct engine_info {
62 unsigned int hw_id;
63 u8 class;
64 u8 instance;
65 /* mmio bases table *must* be sorted in reverse gen order */
66 struct engine_mmio_base {
67 u32 gen : 8;
68 u32 base : 24;
69 } mmio_bases[MAX_MMIO_BASES];
70 };
71
72 static const struct engine_info intel_engines[] = {
73 [RCS0] =
74 {
75 .hw_id = RCS0_HW,
76 .class = RENDER_CLASS,
77 .instance = 0,
78 .mmio_bases = {{.gen = 1, .base = RENDER_RING_BASE}},
79 },
80 [BCS0] =
81 {
82 .hw_id = BCS0_HW,
83 .class = COPY_ENGINE_CLASS,
84 .instance = 0,
85 .mmio_bases = {{.gen = 6, .base = BLT_RING_BASE}},
86 },
87 [VCS0] =
88 {
89 .hw_id = VCS0_HW,
90 .class = VIDEO_DECODE_CLASS,
91 .instance = 0,
92 .mmio_bases = {
93 {.gen = 11, .base = GEN11_BSD_RING_BASE},
94 {.gen = 6, .base = GEN6_BSD_RING_BASE},
95 {.gen = 4, .base = BSD_RING_BASE}},
96 },
97 [VCS1] =
98 {
99 .hw_id = VCS1_HW,
100 .class = VIDEO_DECODE_CLASS,
101 .instance = 1,
102 .mmio_bases = {{.gen = 11, .base = GEN11_BSD2_RING_BASE}, {.gen = 8, .base = GEN8_BSD2_RING_BASE}},
103 },
104 [VCS2] =
105 {
106 .hw_id = VCS2_HW,
107 .class = VIDEO_DECODE_CLASS,
108 .instance = 2,
109 .mmio_bases = {{.gen = 11, .base = GEN11_BSD3_RING_BASE}},
110 },
111 [VCS3] =
112 {
113 .hw_id = VCS3_HW,
114 .class = VIDEO_DECODE_CLASS,
115 .instance = 3,
116 .mmio_bases = {{.gen = 11, .base = GEN11_BSD4_RING_BASE}},
117 },
118 [VECS0] =
119 {
120 .hw_id = VECS0_HW,
121 .class = VIDEO_ENHANCEMENT_CLASS,
122 .instance = 0,
123 .mmio_bases = {{.gen = 11, .base = GEN11_VEBOX_RING_BASE}, {.gen = 7, .base = VEBOX_RING_BASE}},
124 },
125 [VECS1] =
126 {
127 .hw_id = VECS1_HW,
128 .class = VIDEO_ENHANCEMENT_CLASS,
129 .instance = 1,
130 .mmio_bases = {{.gen = 11, .base = GEN11_VEBOX2_RING_BASE}},
131 },
132 };
133
134 /**
135 * intel_engine_context_size() - return the size of the context for an engine
136 * @gt: the gt
137 * @class: engine class
138 *
139 * Each engine class may require a different amount of space for a context
140 * image.
141 *
142 * Return: size (in bytes) of an engine class specific context image
143 *
144 * Note: this size includes the HWSP, which is part of the context image
145 * in LRC mode, but does not include the "shared data page" used with
146 * GuC submission. The caller should account for this if using the GuC.
147 */
intel_engine_context_size(struct intel_gt * gt,u8 class)148 u32 intel_engine_context_size(struct intel_gt *gt, u8 class)
149 {
150 struct intel_uncore *uncore = gt->uncore;
151 u32 cxt_size;
152
153 BUILD_BUG_ON(I915_GTT_PAGE_SIZE != PAGE_SIZE);
154
155 switch (class) {
156 case RENDER_CLASS:
157 switch (INTEL_GEN(gt->i915)) {
158 default:
159 MISSING_CASE(INTEL_GEN(gt->i915));
160 return DEFAULT_LR_CONTEXT_RENDER_SIZE;
161 case 0xc:
162 case 0xb:
163 return GEN11_LR_CONTEXT_RENDER_SIZE;
164 case 0xa:
165 return GEN10_LR_CONTEXT_RENDER_SIZE;
166 case 0x9:
167 return GEN9_LR_CONTEXT_RENDER_SIZE;
168 case 0x8:
169 return GEN8_LR_CONTEXT_RENDER_SIZE;
170 case 0x7:
171 if (IS_HASWELL(gt->i915)) {
172 return HSW_CXT_TOTAL_SIZE;
173 }
174
175 cxt_size = intel_uncore_read(uncore, GEN7_CXT_SIZE);
176 return round_up(GEN7_CXT_TOTAL_SIZE(cxt_size) * 0x40, PAGE_SIZE);
177 case 0x6:
178 cxt_size = intel_uncore_read(uncore, CXT_SIZE);
179 return round_up(GEN6_CXT_TOTAL_SIZE(cxt_size) * 0x40, PAGE_SIZE);
180 case 0x5:
181 case 0x4:
182 /*
183 * There is a discrepancy here between the size reported
184 * by the register and the size of the context layout
185 * in the docs. Both are described as authorative!
186 *
187 * The discrepancy is on the order of a few cachelines,
188 * but the total is under one page (4k), which is our
189 * minimum allocation anyway so it should all come
190 * out in the wash.
191 */
192 cxt_size = intel_uncore_read(uncore, CXT_SIZE) + 1;
193 drm_dbg(>->i915->drm, "gen%d CXT_SIZE = %d bytes [0x%08x]\n", INTEL_GEN(gt->i915),
194 cxt_size * 0x40 cxt_size - 1);
195 return round_up(cxt_size * 0x40, PAGE_SIZE);
196 case 0x3:
197 case 0x2:
198 /* For the special day when i810 gets merged. */
199 case 0x1:
200 return 0;
201 }
202 break;
203 default:
204 MISSING_CASE(class);
205 fallthrough;
206 case VIDEO_DECODE_CLASS:
207 case VIDEO_ENHANCEMENT_CLASS:
208 case COPY_ENGINE_CLASS:
209 if (INTEL_GEN(gt->i915) < 0x8) {
210 return 0;
211 }
212 return GEN8_LR_CONTEXT_OTHER_SIZE;
213 }
214 }
215
_engine_mmio_base(struct drm_i915_private * i915,const struct engine_mmio_base * bases)216 static u32 _engine_mmio_base(struct drm_i915_private *i915, const struct engine_mmio_base *bases)
217 {
218 int i;
219
220 for (i = 0; i < MAX_MMIO_BASES; i++) {
221 if (INTEL_GEN(i915) >= bases[i].gen) {
222 break;
223 }
224 }
225
226 GEM_BUG_ON(i == MAX_MMIO_BASES);
227 GEM_BUG_ON(!bases[i].base);
228
229 return bases[i].base;
230 }
231
_sprint_engine_name(struct intel_engine_cs * engine)232 static void _sprint_engine_name(struct intel_engine_cs *engine)
233 {
234 /*
235 * Before we know what the uABI name for this engine will be,
236 * we still would like to keep track of this engine in the debug logs.
237 * We throw in a ' here as a reminder that this isn't its final name.
238 */
239 GEM_WARN_ON(snprintf(engine->name, sizeof(engine->name), "%s'%u", intel_engine_class_repr(engine->class),
240 engine->instance) >= sizeof(engine->name));
241 }
242
intel_engine_set_hwsp_writemask(struct intel_engine_cs * engine,u32 mask)243 void intel_engine_set_hwsp_writemask(struct intel_engine_cs *engine, u32 mask)
244 {
245 /*
246 * Though they added more rings on g4x/ilk, they did not add
247 * per-engine HWSTAM until gen6.
248 */
249 if (INTEL_GEN(engine->i915) < 0x6 && engine->class != RENDER_CLASS) {
250 return;
251 }
252
253 if (INTEL_GEN(engine->i915) >= 0x3) {
254 ENGINE_WRITE(engine, RING_HWSTAM, mask);
255 } else {
256 ENGINE_WRITE16(engine, RING_HWSTAM, mask);
257 }
258 }
259
intel_engine_sanitize_mmio(struct intel_engine_cs * engine)260 static void intel_engine_sanitize_mmio(struct intel_engine_cs *engine)
261 {
262 /* Mask off all writes into the unknown HWSP */
263 intel_engine_set_hwsp_writemask(engine, ~0u);
264 }
265
intel_engine_setup(struct intel_gt * gt,enum intel_engine_id id)266 static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
267 {
268 const struct engine_info *info = &intel_engines[id];
269 struct drm_i915_private *i915 = gt->i915;
270 struct intel_engine_cs *engine;
271
272 BUILD_BUG_ON(MAX_ENGINE_CLASS >= BIT(GEN11_ENGINE_CLASS_WIDTH));
273 BUILD_BUG_ON(MAX_ENGINE_INSTANCE >= BIT(GEN11_ENGINE_INSTANCE_WIDTH));
274
275 if (GEM_DEBUG_WARN_ON(id >= ARRAY_SIZE(gt->engine))) {
276 return -EINVAL;
277 }
278
279 if (GEM_DEBUG_WARN_ON(info->class > MAX_ENGINE_CLASS)) {
280 return -EINVAL;
281 }
282
283 if (GEM_DEBUG_WARN_ON(info->instance > MAX_ENGINE_INSTANCE)) {
284 return -EINVAL;
285 }
286
287 if (GEM_DEBUG_WARN_ON(gt->engine_class[info->class][info->instance])) {
288 return -EINVAL;
289 }
290
291 engine = kzalloc(sizeof(*engine), GFP_KERNEL);
292 if (!engine) {
293 return -ENOMEM;
294 }
295
296 BUILD_BUG_ON(BITS_PER_TYPE(engine->mask) < I915_NUM_ENGINES);
297
298 engine->id = id;
299 engine->legacy_idx = INVALID_ENGINE;
300 engine->mask = BIT(id);
301 engine->i915 = i915;
302 engine->gt = gt;
303 engine->uncore = gt->uncore;
304 engine->hw_id = engine->guc_id = info->hw_id;
305 engine->mmio_base = _engine_mmio_base(i915, info->mmio_bases);
306
307 engine->class = info->class;
308 engine->instance = info->instance;
309 _sprint_engine_name(engine);
310
311 engine->props.heartbeat_interval_ms = CONFIG_DRM_I915_HEARTBEAT_INTERVAL;
312 engine->props.max_busywait_duration_ns = CONFIG_DRM_I915_MAX_REQUEST_BUSYWAIT;
313 engine->props.preempt_timeout_ms = CONFIG_DRM_I915_PREEMPT_TIMEOUT;
314 engine->props.stop_timeout_ms = CONFIG_DRM_I915_STOP_TIMEOUT;
315 engine->props.timeslice_duration_ms = CONFIG_DRM_I915_TIMESLICE_DURATION;
316
317 /* Override to uninterruptible for OpenCL workloads. */
318 if (INTEL_GEN(i915) == 0xc && engine->class == RENDER_CLASS) {
319 engine->props.preempt_timeout_ms = 0;
320 }
321
322 engine->defaults = engine->props; /* never to change again */
323
324 engine->context_size = intel_engine_context_size(gt, engine->class);
325 if (WARN_ON(engine->context_size > BIT(0x14))) {
326 engine->context_size = 0;
327 }
328 if (engine->context_size) {
329 DRIVER_CAPS(i915)->has_logical_contexts = true;
330 }
331
332 /* Nothing to do here, execute in order of dependencies */
333 engine->schedule = NULL;
334
335 ewma__engine_latency_init(&engine->latency);
336 seqlock_init(&engine->stats.lock);
337
338 ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier);
339
340 /* Scrub mmio state on takeover */
341 intel_engine_sanitize_mmio(engine);
342
343 gt->engine_class[info->class][info->instance] = engine;
344 gt->engine[id] = engine;
345
346 return 0;
347 }
348
_setup_engine_capabilities(struct intel_engine_cs * engine)349 static void _setup_engine_capabilities(struct intel_engine_cs *engine)
350 {
351 struct drm_i915_private *i915 = engine->i915;
352
353 if (engine->class == VIDEO_DECODE_CLASS) {
354 /*
355 * HEVC support is present on first engine instance
356 * before Gen11 and on all instances afterwards.
357 */
358 if (INTEL_GEN(i915) >= 0xb || (INTEL_GEN(i915) >= 0x9 && engine->instance == 0x0)) {
359 engine->uabi_capabilities |= I915_VIDEO_CLASS_CAPABILITY_HEVC;
360 }
361
362 /*
363 * SFC block is present only on even logical engine
364 * instances.
365 */
366 if ((INTEL_GEN(i915) >= 0xb && (engine->gt->info.vdbox_sfc_access & BIT(engine->instance))) ||
367 (INTEL_GEN(i915) >= 0x9 && engine->instance == 0x0)) {
368 engine->uabi_capabilities |= I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC;
369 }
370 } else if (engine->class == VIDEO_ENHANCEMENT_CLASS) {
371 if (INTEL_GEN(i915) >= 0x9) {
372 engine->uabi_capabilities |= I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC;
373 }
374 }
375 }
376
intel_setup_engine_capabilities(struct intel_gt * gt)377 static void intel_setup_engine_capabilities(struct intel_gt *gt)
378 {
379 struct intel_engine_cs *engine;
380 enum intel_engine_id id;
381
382 for_each_engine(engine, gt, id) _setup_engine_capabilities(engine);
383 }
384
385 /**
386 * intel_engines_release() - free the resources allocated for Command Streamers
387 * @gt: pointer to struct intel_gt
388 */
intel_engines_release(struct intel_gt * gt)389 void intel_engines_release(struct intel_gt *gt)
390 {
391 struct intel_engine_cs *engine;
392 enum intel_engine_id id;
393
394 /*
395 * Before we release the resources held by engine, we must be certain
396 * that the HW is no longer accessing them -- having the GPU scribble
397 * to or read from a page being used for something else causes no end
398 * of fun.
399 *
400 * The GPU should be reset by this point, but assume the worst just
401 * in case we aborted before completely initialising the engines.
402 */
403 GEM_BUG_ON(intel_gt_pm_is_awake(gt));
404 if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display) {
405 __intel_gt_reset(gt, ALL_ENGINES);
406 }
407
408 /* Decouple the backend; but keep the layout for late GPU resets */
409 for_each_engine(engine, gt, id)
410 {
411 if (!engine->release) {
412 continue;
413 }
414
415 intel_wakeref_wait_for_idle(&engine->wakeref);
416 GEM_BUG_ON(intel_engine_pm_is_awake(engine));
417
418 engine->release(engine);
419 engine->release = NULL;
420
421 memset(&engine->reset, 0, sizeof(engine->reset));
422 }
423 }
424
intel_engine_free_request_pool(struct intel_engine_cs * engine)425 void intel_engine_free_request_pool(struct intel_engine_cs *engine)
426 {
427 if (!engine->request_pool) {
428 return;
429 }
430
431 kmem_cache_free(i915_request_slab_cache(), engine->request_pool);
432 }
433
intel_engines_free(struct intel_gt * gt)434 void intel_engines_free(struct intel_gt *gt)
435 {
436 struct intel_engine_cs *engine;
437 enum intel_engine_id id;
438
439 /* Free the requests! dma-resv keeps fences around for an eternity */
440 rcu_barrier();
441
442 for_each_engine(engine, gt, id)
443 {
444 intel_engine_free_request_pool(engine);
445 kfree(engine);
446 gt->engine[id] = NULL;
447 }
448 }
449
450 /*
451 * Determine which engines are fused off in our particular hardware.
452 * Note that we have a catch-22 situation where we need to be able to access
453 * the blitter forcewake domain to read the engine fuses, but at the same time
454 * we need to know which engines are available on the system to know which
455 * forcewake domains are present. We solve this by intializing the forcewake
456 * domains based on the full engine mask in the platform capabilities before
457 * calling this function and pruning the domains for fused-off engines
458 * afterwards.
459 */
init_engine_mask(struct intel_gt * gt)460 static intel_engine_mask_t init_engine_mask(struct intel_gt *gt)
461 {
462 struct drm_i915_private *i915 = gt->i915;
463 struct intel_gt_info *info = >->info;
464 struct intel_uncore *uncore = gt->uncore;
465 unsigned int logical_vdbox = 0;
466 unsigned int i;
467 u32 media_fuse;
468 u16 vdbox_mask;
469 u16 vebox_mask;
470
471 info->engine_mask = INTEL_INFO(i915)->platform_engine_mask;
472
473 if (INTEL_GEN(i915) < 0xb) {
474 return info->engine_mask;
475 }
476
477 media_fuse = ~intel_uncore_read(uncore, GEN11_GT_VEBOX_VDBOX_DISABLE);
478
479 vdbox_mask = media_fuse & GEN11_GT_VDBOX_DISABLE_MASK;
480 vebox_mask = (media_fuse & GEN11_GT_VEBOX_DISABLE_MASK) >> GEN11_GT_VEBOX_DISABLE_SHIFT;
481
482 for (i = 0; i < I915_MAX_VCS; i++) {
483 if (!HAS_ENGINE(gt, _VCS(i))) {
484 vdbox_mask &= ~BIT(i);
485 continue;
486 }
487
488 if (!(BIT(i) & vdbox_mask)) {
489 info->engine_mask &= ~BIT(_VCS(i));
490 drm_dbg(&i915->drm, "vcs%u fused off\n", i);
491 continue;
492 }
493
494 /*
495 * In Gen11, only even numbered logical VDBOXes are
496 * hooked up to an SFC (Scaler & Format Converter) unit.
497 * In TGL each VDBOX has access to an SFC.
498 */
499 if (INTEL_GEN(i915) >= 0xc || logical_vdbox++ % 0x2 == 0) {
500 gt->info.vdbox_sfc_access |= BIT(i);
501 }
502 }
503 drm_dbg(&i915->drm, "vdbox enable: %04x, instances: %04lx\n", vdbox_mask, VDBOX_MASK(gt));
504 GEM_BUG_ON(vdbox_mask != VDBOX_MASK(gt));
505
506 for (i = 0; i < I915_MAX_VECS; i++) {
507 if (!HAS_ENGINE(gt, _VECS(i))) {
508 vebox_mask &= ~BIT(i);
509 continue;
510 }
511
512 if (!(BIT(i) & vebox_mask)) {
513 info->engine_mask &= ~BIT(_VECS(i));
514 drm_dbg(&i915->drm, "vecs%u fused off\n", i);
515 }
516 }
517 drm_dbg(&i915->drm, "vebox enable: %04x, instances: %04lx\n", vebox_mask, VEBOX_MASK(gt));
518 GEM_BUG_ON(vebox_mask != VEBOX_MASK(gt));
519
520 return info->engine_mask;
521 }
522
523 /**
524 * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
525 * @gt: pointer to struct intel_gt
526 *
527 * Return: non-zero if the initialization failed.
528 */
intel_engines_init_mmio(struct intel_gt * gt)529 int intel_engines_init_mmio(struct intel_gt *gt)
530 {
531 struct drm_i915_private *i915 = gt->i915;
532 const unsigned int engine_mask = init_engine_mask(gt);
533 unsigned int mask = 0;
534 unsigned int i;
535 int err;
536
537 drm_WARN_ON(&i915->drm, engine_mask == 0);
538 drm_WARN_ON(&i915->drm, engine_mask & GENMASK(BITS_PER_TYPE(mask) - 1, I915_NUM_ENGINES));
539
540 if (i915_inject_probe_failure(i915)) {
541 return -ENODEV;
542 }
543
544 for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
545 if (!HAS_ENGINE(gt, i)) {
546 continue;
547 }
548
549 err = intel_engine_setup(gt, i);
550 if (err) {
551 goto cleanup;
552 }
553
554 mask |= BIT(i);
555 }
556
557 /*
558 * Catch failures to update intel_engines table when the new engines
559 * are added to the driver by a warning and disabling the forgotten
560 * engines.
561 */
562 if (drm_WARN_ON(&i915->drm, mask != engine_mask)) {
563 gt->info.engine_mask = mask;
564 }
565
566 gt->info.num_engines = hweight32(mask);
567
568 intel_gt_check_and_clear_faults(gt);
569
570 intel_setup_engine_capabilities(gt);
571
572 intel_uncore_prune_engine_fw_domains(gt->uncore, gt);
573
574 return 0;
575
576 cleanup:
577 intel_engines_free(gt);
578 return err;
579 }
580
intel_engine_init_execlists(struct intel_engine_cs * engine)581 void intel_engine_init_execlists(struct intel_engine_cs *engine)
582 {
583 struct intel_engine_execlists *const execlists = &engine->execlists;
584
585 execlists->port_mask = 1;
586 GEM_BUG_ON(!is_power_of_2(execlists_num_ports(execlists)));
587 GEM_BUG_ON(execlists_num_ports(execlists) > EXECLIST_MAX_PORTS);
588
589 memset(execlists->pending, 0, sizeof(execlists->pending));
590 execlists->active = memset(execlists->inflight, 0, sizeof(execlists->inflight));
591
592 execlists->queue_priority_hint = INT_MIN;
593 execlists->queue = RB_ROOT_CACHED;
594 }
595
cleanup_status_page(struct intel_engine_cs * engine)596 static void cleanup_status_page(struct intel_engine_cs *engine)
597 {
598 struct i915_vma *vma;
599
600 /* Prevent writes into HWSP after returning the page to the system */
601 intel_engine_set_hwsp_writemask(engine, ~0u);
602
603 vma = fetch_and_zero(&engine->status_page.vma);
604 if (!vma) {
605 return;
606 }
607
608 if (!HWS_NEEDS_PHYSICAL(engine->i915)) {
609 i915_vma_unpin(vma);
610 }
611
612 i915_gem_object_unpin_map(vma->obj);
613 i915_gem_object_put(vma->obj);
614 }
615
pin_ggtt_status_page(struct intel_engine_cs * engine,struct i915_vma * vma)616 static int pin_ggtt_status_page(struct intel_engine_cs *engine, struct i915_vma *vma)
617 {
618 unsigned int flags;
619
620 if (!HAS_LLC(engine->i915) && i915_ggtt_has_aperture(engine->gt->ggtt)) {
621 /*
622 * On g33, we cannot place HWS above 256MiB, so
623 * restrict its pinning to the low mappable arena.
624 * Though this restriction is not documented for
625 * gen4, gen5, or byt, they also behave similarly
626 * and hang if the HWS is placed at the top of the
627 * GTT. To generalise, it appears that all !llc
628 * platforms have issues with us placing the HWS
629 * above the mappable region (even though we never
630 * actually map it).
631 */
632 flags = PIN_MAPPABLE;
633 } else {
634 flags = PIN_HIGH;
635 }
636
637 return i915_ggtt_pin(vma, NULL, 0, flags);
638 }
639
init_status_page(struct intel_engine_cs * engine)640 static int init_status_page(struct intel_engine_cs *engine)
641 {
642 struct drm_i915_gem_object *obj;
643 struct i915_vma *vma;
644 void *vaddr;
645 int ret;
646
647 /*
648 * Though the HWS register does support 36bit addresses, historically
649 * we have had hangs and corruption reported due to wild writes if
650 * the HWS is placed above 4G. We only allow objects to be allocated
651 * in GFP_DMA32 for i965, and no earlier physical address users had
652 * access to more than 4G.
653 */
654 obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
655 if (IS_ERR(obj)) {
656 drm_err(&engine->i915->drm, "Failed to allocate status page\n");
657 return PTR_ERR(obj);
658 }
659
660 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
661
662 vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
663 if (IS_ERR(vma)) {
664 ret = PTR_ERR(vma);
665 goto err;
666 }
667
668 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
669 if (IS_ERR(vaddr)) {
670 ret = PTR_ERR(vaddr);
671 goto err;
672 }
673
674 engine->status_page.addr = memset(vaddr, 0, PAGE_SIZE);
675 engine->status_page.vma = vma;
676
677 if (!HWS_NEEDS_PHYSICAL(engine->i915)) {
678 ret = pin_ggtt_status_page(engine, vma);
679 if (ret) {
680 goto err_unpin;
681 }
682 }
683
684 return 0;
685
686 err_unpin:
687 i915_gem_object_unpin_map(obj);
688 err:
689 i915_gem_object_put(obj);
690 return ret;
691 }
692
engine_setup_common(struct intel_engine_cs * engine)693 static int engine_setup_common(struct intel_engine_cs *engine)
694 {
695 int err;
696
697 init_llist_head(&engine->barrier_tasks);
698
699 err = init_status_page(engine);
700 if (err) {
701 return err;
702 }
703
704 engine->breadcrumbs = intel_breadcrumbs_create(engine);
705 if (!engine->breadcrumbs) {
706 err = -ENOMEM;
707 goto err_status;
708 }
709
710 err = intel_engine_init_cmd_parser(engine);
711 if (err) {
712 goto err_cmd_parser;
713 }
714
715 intel_engine_init_active(engine, ENGINE_PHYSICAL);
716 intel_engine_init_execlists(engine);
717 intel_engine_init__pm(engine);
718 intel_engine_init_retire(engine);
719
720 /* Use the whole device by default */
721 engine->sseu = intel_sseu_from_device_info(&engine->gt->info.sseu);
722
723 intel_engine_init_workarounds(engine);
724 intel_engine_init_whitelist(engine);
725 intel_engine_init_ctx_wa(engine);
726
727 return 0;
728
729 err_cmd_parser:
730 intel_breadcrumbs_free(engine->breadcrumbs);
731 err_status:
732 cleanup_status_page(engine);
733 return err;
734 }
735
736 struct measure_breadcrumb {
737 struct i915_request rq;
738 struct intel_ring ring;
739 u32 cs[2048];
740 };
741
measure_breadcrumb_dw(struct intel_context * ce)742 static int measure_breadcrumb_dw(struct intel_context *ce)
743 {
744 struct intel_engine_cs *engine = ce->engine;
745 struct measure_breadcrumb *frame;
746 int dw;
747
748 GEM_BUG_ON(!engine->gt->scratch);
749
750 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
751 if (!frame) {
752 return -ENOMEM;
753 }
754
755 frame->rq.engine = engine;
756 frame->rq.context = ce;
757 rcu_assign_pointer(frame->rq.timeline, ce->timeline);
758
759 frame->ring.vaddr = frame->cs;
760 frame->ring.size = sizeof(frame->cs);
761 frame->ring.wrap = BITS_PER_TYPE(frame->ring.size) - ilog2(frame->ring.size);
762 frame->ring.effective_size = frame->ring.size;
763 intel_ring_update_space(&frame->ring);
764 frame->rq.ring = &frame->ring;
765
766 mutex_lock(&ce->timeline->mutex);
767 spin_lock_irq(&engine->active.lock);
768
769 dw = engine->emit_fini_breadcrumb(&frame->rq, frame->cs) - frame->cs;
770
771 spin_unlock_irq(&engine->active.lock);
772 mutex_unlock(&ce->timeline->mutex);
773
774 GEM_BUG_ON(dw & 1); /* RING_TAIL must be qword aligned */
775
776 kfree(frame);
777 return dw;
778 }
779
intel_engine_init_active(struct intel_engine_cs * engine,unsigned int subclass)780 void intel_engine_init_active(struct intel_engine_cs *engine, unsigned int subclass)
781 {
782 INIT_LIST_HEAD(&engine->active.requests);
783 INIT_LIST_HEAD(&engine->active.hold);
784
785 spin_lock_init(&engine->active.lock);
786 lockdep_set_subclass(&engine->active.lock, subclass);
787
788 /*
789 * Due to an interesting quirk in lockdep's internal debug tracking,
790 * after setting a subclass we must ensure the lock is used. Otherwise,
791 * nr_unused_locks is incremented once too often.
792 */
793 #ifdef CONFIG_DEBUG_LOCK_ALLOC
794 local_irq_disable();
795 lock_map_acquire(&engine->active.lock.dep_map);
796 lock_map_release(&engine->active.lock.dep_map);
797 local_irq_enable();
798 #endif
799 }
800
create_pinned_context(struct intel_engine_cs * engine,unsigned int hwsp,struct lock_class_key * key,const char * name)801 static struct intel_context *create_pinned_context(struct intel_engine_cs *engine, unsigned int hwsp,
802 struct lock_class_key *key, const char *name)
803 {
804 struct intel_context *ce;
805 int err;
806
807 ce = intel_context_create(engine);
808 if (IS_ERR(ce)) {
809 return ce;
810 }
811
812 __set_bit(CONTEXT_BARRIER_BIT, &ce->flags);
813 ce->timeline = page_pack_bits(NULL, hwsp);
814
815 err = intel_context_pin(ce); /* perma-pin so it is always available */
816 if (err) {
817 intel_context_put(ce);
818 return ERR_PTR(err);
819 }
820
821 /*
822 * Give our perma-pinned kernel timelines a separate lockdep class,
823 * so that we can use them from within the normal user timelines
824 * should we need to inject GPU operations during their request
825 * construction.
826 */
827 lockdep_set_class_and_name(&ce->timeline->mutex, key, name);
828
829 return ce;
830 }
831
create_kernel_context(struct intel_engine_cs * engine)832 static struct intel_context *create_kernel_context(struct intel_engine_cs *engine)
833 {
834 static struct lock_class_key kernel;
835
836 return create_pinned_context(engine, I915_GEM_HWS_SEQNO_ADDR, &kernel, "kernel_context");
837 }
838
839 /**
840 * intel_engines_init_common - initialize cengine state which might require hw access
841 * @engine: Engine to initialize.
842 *
843 * Initializes @engine@ structure members shared between legacy and execlists
844 * submission modes which do require hardware access.
845 *
846 * Typcally done at later stages of submission mode specific engine setup.
847 *
848 * Returns zero on success or an error code on failure.
849 */
engine_init_common(struct intel_engine_cs * engine)850 static int engine_init_common(struct intel_engine_cs *engine)
851 {
852 struct intel_context *ce;
853 int ret;
854
855 engine->set_default_submission(engine);
856
857 /*
858 * We may need to do things with the shrinker which
859 * require us to immediately switch back to the default
860 * context. This can cause a problem as pinning the
861 * default context also requires GTT space which may not
862 * be available. To avoid this we always pin the default
863 * context.
864 */
865 ce = create_kernel_context(engine);
866 if (IS_ERR(ce)) {
867 return PTR_ERR(ce);
868 }
869
870 ret = measure_breadcrumb_dw(ce);
871 if (ret < 0) {
872 goto err_context;
873 }
874
875 engine->emit_fini_breadcrumb_dw = ret;
876 engine->kernel_context = ce;
877
878 return 0;
879
880 err_context:
881 intel_context_put(ce);
882 return ret;
883 }
884
intel_engines_init(struct intel_gt * gt)885 int intel_engines_init(struct intel_gt *gt)
886 {
887 int (*setup)(struct intel_engine_cs * engine);
888 struct intel_engine_cs *engine;
889 enum intel_engine_id id;
890 int err;
891
892 if (HAS_EXECLISTS(gt->i915)) {
893 setup = intel_execlists_submission_setup;
894 } else {
895 setup = intel_ring_submission_setup;
896 }
897
898 for_each_engine(engine, gt, id)
899 {
900 err = engine_setup_common(engine);
901 if (err) {
902 return err;
903 }
904
905 err = setup(engine);
906 if (err) {
907 return err;
908 }
909
910 err = engine_init_common(engine);
911 if (err) {
912 return err;
913 }
914
915 intel_engine_add_user(engine);
916 }
917
918 return 0;
919 }
920
921 /**
922 * intel_engines_cleanup_common - cleans up the engine state created by
923 * the common initiailizers.
924 * @engine: Engine to cleanup.
925 *
926 * This cleans up everything created by the common helpers.
927 */
intel_engine_cleanup_common(struct intel_engine_cs * engine)928 void intel_engine_cleanup_common(struct intel_engine_cs *engine)
929 {
930 GEM_BUG_ON(!list_empty(&engine->active.requests));
931 tasklet_kill(&engine->execlists.tasklet); /* flush the callback */
932
933 cleanup_status_page(engine);
934 intel_breadcrumbs_free(engine->breadcrumbs);
935
936 intel_engine_fini_retire(engine);
937 intel_engine_cleanup_cmd_parser(engine);
938
939 if (engine->default_state) {
940 fput(engine->default_state);
941 }
942
943 if (engine->kernel_context) {
944 intel_context_unpin(engine->kernel_context);
945 intel_context_put(engine->kernel_context);
946 }
947 GEM_BUG_ON(!llist_empty(&engine->barrier_tasks));
948
949 intel_wa_list_free(&engine->ctx_wa_list);
950 intel_wa_list_free(&engine->wa_list);
951 intel_wa_list_free(&engine->whitelist);
952 }
953
954 /**
955 * intel_engine_resume - re-initializes the HW state of the engine
956 * @engine: Engine to resume.
957 *
958 * Returns zero on success or an error code on failure.
959 */
intel_engine_resume(struct intel_engine_cs * engine)960 int intel_engine_resume(struct intel_engine_cs *engine)
961 {
962 intel_engine_apply_workarounds(engine);
963 intel_engine_apply_whitelist(engine);
964
965 return engine->resume(engine);
966 }
967
intel_engine_get_active_head(const struct intel_engine_cs * engine)968 u64 intel_engine_get_active_head(const struct intel_engine_cs *engine)
969 {
970 struct drm_i915_private *i915 = engine->i915;
971
972 u64 acthd;
973
974 if (INTEL_GEN(i915) >= 0x8) {
975 acthd = ENGINE_READ64(engine, RING_ACTHD, RING_ACTHD_UDW);
976 } else if (INTEL_GEN(i915) >= 0x4) {
977 acthd = ENGINE_READ(engine, RING_ACTHD);
978 } else {
979 acthd = ENGINE_READ(engine, ACTHD);
980 }
981
982 return acthd;
983 }
984
intel_engine_get_last_batch_head(const struct intel_engine_cs * engine)985 u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine)
986 {
987 u64 bbaddr;
988
989 if (INTEL_GEN(engine->i915) >= 0x8) {
990 bbaddr = ENGINE_READ64(engine, RING_BBADDR, RING_BBADDR_UDW);
991 } else {
992 bbaddr = ENGINE_READ(engine, RING_BBADDR);
993 }
994
995 return bbaddr;
996 }
997
stop_timeout(const struct intel_engine_cs * engine)998 static unsigned long stop_timeout(const struct intel_engine_cs *engine)
999 {
1000 if (in_atomic() || irqs_disabled()) { /* inside atomic preempt-reset? */
1001 return 0;
1002 }
1003
1004 /*
1005 * If we are doing a normal GPU reset, we can take our time and allow
1006 * the engine to quiesce. We've stopped submission to the engine, and
1007 * if we wait long enough an innocent context should complete and
1008 * leave the engine idle. So they should not be caught unaware by
1009 * the forthcoming GPU reset (which usually follows the stop_cs)!
1010 */
1011 return READ_ONCE(engine->props.stop_timeout_ms);
1012 }
1013
intel_engine_stop_cs(struct intel_engine_cs * engine)1014 int intel_engine_stop_cs(struct intel_engine_cs *engine)
1015 {
1016 struct intel_uncore *uncore = engine->uncore;
1017 const u32 base = engine->mmio_base;
1018 const i915_reg_t mode = RING_MI_MODE(base);
1019 int err;
1020
1021 if (INTEL_GEN(engine->i915) < 0x3) {
1022 return -ENODEV;
1023 }
1024
1025 ENGINE_TRACE(engine, "\n");
1026
1027 intel_uncore_write_fw(uncore, mode, _MASKED_BIT_ENABLE(STOP_RING));
1028
1029 err = 0;
1030 if (__intel_wait_for_register_fw(uncore, mode, MODE_IDLE, MODE_IDLE, 0x3e8, stop_timeout(engine), NULL)) {
1031 ENGINE_TRACE(engine, "timed out on STOP_RING -> IDLE\n");
1032 err = -ETIMEDOUT;
1033 }
1034
1035 /* A final mmio read to let GPU writes be hopefully flushed to memory */
1036 intel_uncore_posting_read_fw(uncore, mode);
1037
1038 return err;
1039 }
1040
intel_engine_cancel_stop_cs(struct intel_engine_cs * engine)1041 void intel_engine_cancel_stop_cs(struct intel_engine_cs *engine)
1042 {
1043 ENGINE_TRACE(engine, "\n");
1044
1045 ENGINE_WRITE_FW(engine, RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING));
1046 }
1047
i915_cache_level_str(struct drm_i915_private * i915,int type)1048 const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
1049 {
1050 switch (type) {
1051 case I915_CACHE_NONE:
1052 return " uncached";
1053 case I915_CACHE_LLC:
1054 return HAS_LLC(i915) ? " LLC" : " snooped";
1055 case I915_CACHE_L3_LLC:
1056 return " L3+LLC";
1057 case I915_CACHE_WT:
1058 return " WT";
1059 default:
1060 return "";
1061 }
1062 }
1063
read_subslice_reg(const struct intel_engine_cs * engine,int slice,int subslice,i915_reg_t reg)1064 static u32 read_subslice_reg(const struct intel_engine_cs *engine, int slice, int subslice, i915_reg_t reg)
1065 {
1066 struct drm_i915_private *i915 = engine->i915;
1067 struct intel_uncore *uncore = engine->uncore;
1068 u32 mcr_mask, mcr_ss, mcr, old_mcr, val;
1069 enum forcewake_domains fw_domains;
1070
1071 if (INTEL_GEN(i915) >= 0xb) {
1072 mcr_mask = GEN11_MCR_SLICE_MASK | GEN11_MCR_SUBSLICE_MASK;
1073 mcr_ss = GEN11_MCR_SLICE(slice) | GEN11_MCR_SUBSLICE(subslice);
1074 } else {
1075 mcr_mask = GEN8_MCR_SLICE_MASK | GEN8_MCR_SUBSLICE_MASK;
1076 mcr_ss = GEN8_MCR_SLICE(slice) | GEN8_MCR_SUBSLICE(subslice);
1077 }
1078
1079 fw_domains = intel_uncore_forcewake_for_reg(uncore, reg, FW_REG_READ);
1080 fw_domains |= intel_uncore_forcewake_for_reg(uncore, GEN8_MCR_SELECTOR, FW_REG_READ | FW_REG_WRITE);
1081
1082 spin_lock_irq(&uncore->lock);
1083 intel_uncore_forcewake_get__locked(uncore, fw_domains);
1084
1085 old_mcr = mcr = intel_uncore_read_fw(uncore, GEN8_MCR_SELECTOR);
1086
1087 mcr &= ~mcr_mask;
1088 mcr |= mcr_ss;
1089 intel_uncore_write_fw(uncore, GEN8_MCR_SELECTOR, mcr);
1090
1091 val = intel_uncore_read_fw(uncore, reg);
1092
1093 mcr &= ~mcr_mask;
1094 mcr |= old_mcr & mcr_mask;
1095
1096 intel_uncore_write_fw(uncore, GEN8_MCR_SELECTOR, mcr);
1097
1098 intel_uncore_forcewake_put__locked(uncore, fw_domains);
1099 spin_unlock_irq(&uncore->lock);
1100
1101 return val;
1102 }
1103
1104 /* NB: please notice the memset */
intel_engine_get_instdone(const struct intel_engine_cs * engine,struct intel_instdone * instdone)1105 void intel_engine_get_instdone(const struct intel_engine_cs *engine, struct intel_instdone *instdone)
1106 {
1107 struct drm_i915_private *i915 = engine->i915;
1108 const struct sseu_dev_info *sseu = &engine->gt->info.sseu;
1109 struct intel_uncore *uncore = engine->uncore;
1110 u32 mmio_base = engine->mmio_base;
1111 int slice;
1112 int subslice;
1113
1114 memset(instdone, 0, sizeof(*instdone));
1115
1116 switch (INTEL_GEN(i915)) {
1117 case 0x7:
1118 instdone->instdone = intel_uncore_read(uncore, RING_INSTDONE(mmio_base));
1119
1120 if (engine->id != RCS0) {
1121 break;
1122 }
1123
1124 instdone->slice_common = intel_uncore_read(uncore, GEN7_SC_INSTDONE);
1125 instdone->sampler[0][0] = intel_uncore_read(uncore, GEN7_SAMPLER_INSTDONE);
1126 instdone->row[0][0] = intel_uncore_read(uncore, GEN7_ROW_INSTDONE);
1127
1128 break;
1129 case 0x6:
1130 case 0x5:
1131 case 0x4:
1132 instdone->instdone = intel_uncore_read(uncore, RING_INSTDONE(mmio_base));
1133 if (engine->id == RCS0) {
1134 /* HACK: Using the wrong struct member */
1135 instdone->slice_common = intel_uncore_read(uncore, GEN4_INSTDONE1);
1136 }
1137 break;
1138 case 0x3:
1139 case 0x2:
1140 instdone->instdone = intel_uncore_read(uncore, GEN2_INSTDONE);
1141 break;
1142 default:
1143 instdone->instdone = intel_uncore_read(uncore, RING_INSTDONE(mmio_base));
1144
1145 if (engine->id != RCS0) {
1146 break;
1147 }
1148
1149 instdone->slice_common = intel_uncore_read(uncore, GEN7_SC_INSTDONE);
1150 if (INTEL_GEN(i915) >= 0xc) {
1151 instdone->slice_common_extra[0] = intel_uncore_read(uncore, GEN12_SC_INSTDONE_EXTRA);
1152 instdone->slice_common_extra[1] = intel_uncore_read(uncore, GEN12_SC_INSTDONE_EXTRA2);
1153 }
1154 for_each_instdone_slice_subslice(i915, sseu, slice, subslice)
1155 {
1156 instdone->sampler[slice][subslice] = read_subslice_reg(engine, slice, subslice, GEN7_SAMPLER_INSTDONE);
1157 instdone->row[slice][subslice] = read_subslice_reg(engine, slice, subslice, GEN7_ROW_INSTDONE);
1158 }
1159 break;
1160 }
1161 }
1162
ring_is_idle(struct intel_engine_cs * engine)1163 static bool ring_is_idle(struct intel_engine_cs *engine)
1164 {
1165 bool idle = true;
1166
1167 if (I915_SELFTEST_ONLY(!engine->mmio_base)) {
1168 return true;
1169 }
1170
1171 if (!intel_engine_pm_get_if_awake(engine)) {
1172 return true;
1173 }
1174
1175 /* First check that no commands are left in the ring */
1176 if ((ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR) != (ENGINE_READ(engine, RING_TAIL) & TAIL_ADDR)) {
1177 idle = false;
1178 }
1179
1180 /* No bit for gen2, so assume the CS parser is idle */
1181 if (INTEL_GEN(engine->i915) > 0x2 && !(ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE)) {
1182 idle = false;
1183 }
1184
1185 intel_engine_pm_put(engine);
1186
1187 return idle;
1188 }
1189
intel_engine_flush_submission(struct intel_engine_cs * engine)1190 void intel_engine_flush_submission(struct intel_engine_cs *engine)
1191 {
1192 struct tasklet_struct *t = &engine->execlists.tasklet;
1193
1194 if (!t->func) {
1195 return;
1196 }
1197
1198 /* Synchronise and wait for the tasklet on another CPU */
1199 tasklet_kill(t);
1200
1201 /* Having cancelled the tasklet, ensure that is run */
1202 local_bh_disable();
1203 if (tasklet_trylock(t)) {
1204 /* Must wait for any GPU reset in progress. */
1205 if (__tasklet_is_enabled(t)) {
1206 t->func(t->data);
1207 }
1208 tasklet_unlock(t);
1209 }
1210 local_bh_enable();
1211 }
1212
1213 /**
1214 * intel_engine_is_idle() - Report if the engine has finished process all work
1215 * @engine: the intel_engine_cs
1216 *
1217 * Return true if there are no requests pending, nothing left to be submitted
1218 * to hardware, and that the engine is idle.
1219 */
intel_engine_is_idle(struct intel_engine_cs * engine)1220 bool intel_engine_is_idle(struct intel_engine_cs *engine)
1221 {
1222 /* More white lies, if wedged, hw state is inconsistent */
1223 if (intel_gt_is_wedged(engine->gt)) {
1224 return true;
1225 }
1226
1227 if (!intel_engine_pm_is_awake(engine)) {
1228 return true;
1229 }
1230
1231 /* Waiting to drain ELSP? */
1232 if (execlists_active(&engine->execlists)) {
1233 synchronize_hardirq(engine->i915->drm.pdev->irq);
1234
1235 intel_engine_flush_submission(engine);
1236
1237 if (execlists_active(&engine->execlists)) {
1238 return false;
1239 }
1240 }
1241
1242 /* ELSP is empty, but there are ready requests? E.g. after reset */
1243 if (!RB_EMPTY_ROOT(&engine->execlists.queue.rb_root)) {
1244 return false;
1245 }
1246
1247 /* Ring stopped? */
1248 return ring_is_idle(engine);
1249 }
1250
intel_engines_are_idle(struct intel_gt * gt)1251 bool intel_engines_are_idle(struct intel_gt *gt)
1252 {
1253 struct intel_engine_cs *engine;
1254 enum intel_engine_id id;
1255
1256 /*
1257 * If the driver is wedged, HW state may be very inconsistent and
1258 * report that it is still busy, even though we have stopped using it.
1259 */
1260 if (intel_gt_is_wedged(gt)) {
1261 return true;
1262 }
1263
1264 /* Already parked (and passed an idleness test); must still be idle */
1265 if (!READ_ONCE(gt->awake)) {
1266 return true;
1267 }
1268
1269 for_each_engine(engine, gt, id)
1270 {
1271 if (!intel_engine_is_idle(engine)) {
1272 return false;
1273 }
1274 }
1275
1276 return true;
1277 }
1278
intel_engines_reset_default_submission(struct intel_gt * gt)1279 void intel_engines_reset_default_submission(struct intel_gt *gt)
1280 {
1281 struct intel_engine_cs *engine;
1282 enum intel_engine_id id;
1283
1284 for_each_engine(engine, gt, id) engine->set_default_submission(engine);
1285 }
1286
intel_engine_can_store_dword(struct intel_engine_cs * engine)1287 bool intel_engine_can_store_dword(struct intel_engine_cs *engine)
1288 {
1289 switch (INTEL_GEN(engine->i915)) {
1290 case 0x2:
1291 return false; /* uses physical not virtual addresses */
1292 case 0x3:
1293 /* maybe only uses physical not virtual addresses */
1294 return !(IS_I915G(engine->i915) || IS_I915GM(engine->i915));
1295 case 0x4:
1296 return !IS_I965G(engine->i915); /* who knows! */
1297 case 0x6:
1298 return engine->class != VIDEO_DECODE_CLASS; /* b0rked */
1299 default:
1300 return true;
1301 }
1302 }
1303
print_sched_attr(const struct i915_sched_attr * attr,char * buf,int x,int len)1304 static int print_sched_attr(const struct i915_sched_attr *attr, char *buf, int x, int len)
1305 {
1306 if (attr->priority == I915_PRIORITY_INVALID) {
1307 return x;
1308 }
1309
1310 x += snprintf(buf + x, len - x, " prio=%d", attr->priority);
1311
1312 return x;
1313 }
1314
print_request(struct drm_printer * m,struct i915_request * rq,const char * prefix)1315 static void print_request(struct drm_printer *m, struct i915_request *rq, const char *prefix)
1316 {
1317 const char *name = rq->fence.ops->get_timeline_name(&rq->fence);
1318 char buf[0x50] = "";
1319 int x = 0;
1320
1321 x = print_sched_attr(&rq->sched.attr, buf, x, sizeof(buf));
1322
1323 drm_printf(m, "%s %llx:%llx%s%s %s @ %dms: %s\n", prefix, rq->fence.context, rq->fence.seqno,
1324 i915_request_completed(rq) ? "!"
1325 : i915_request_started(rq) ? "*"
1326 : "",
1327 test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags) ? "+"
1328 : test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &rq->fence.flags) ? "-"
1329 : "",
1330 buf, jiffies_to_msecs(jiffies - rq->emitted_jiffies), name);
1331 }
1332
get_timeline(struct i915_request * rq)1333 static struct intel_timeline *get_timeline(struct i915_request *rq)
1334 {
1335 struct intel_timeline *tl;
1336
1337 /*
1338 * Even though we are holding the engine->active.lock here, there
1339 * is no control over the submission queue per-se and we are
1340 * inspecting the active state at a random point in time, with an
1341 * unknown queue. Play safe and make sure the timeline remains valid.
1342 * (Only being used for pretty printing, one extra kref shouldn't
1343 * cause a camel stampede!)
1344 */
1345 rcu_read_lock();
1346 tl = rcu_dereference(rq->timeline);
1347 if (!kref_get_unless_zero(&tl->kref)) {
1348 tl = NULL;
1349 }
1350 rcu_read_unlock();
1351
1352 return tl;
1353 }
1354
print_ring(char * buf,int sz,struct i915_request * rq)1355 static int print_ring(char *buf, int sz, struct i915_request *rq)
1356 {
1357 int len = 0;
1358
1359 if (!i915_request_signaled(rq)) {
1360 struct intel_timeline *tl = get_timeline(rq);
1361
1362 len = scnprintf(buf, sz, "ring:{start:%08x, hwsp:%08x, seqno:%08x, runtime:%llums}, ",
1363 i915_ggtt_offset(rq->ring->vma), tl ? tl->hwsp_offset : 0, hwsp_seqno(rq),
1364 DIV_ROUND_CLOSEST_ULL(intel_context_get_total_runtime_ns(rq->context), 0x3e8 * 0x3e8));
1365
1366 if (tl) {
1367 intel_timeline_put(tl);
1368 }
1369 }
1370
1371 return len;
1372 }
1373
hexdump(struct drm_printer * m,const void * buf,size_t len)1374 static void hexdump(struct drm_printer *m, const void *buf, size_t len)
1375 {
1376 const size_t rowsize = 0x8 * sizeof(u32);
1377 const void *prev = NULL;
1378 bool skip = false;
1379 size_t pos;
1380
1381 for (pos = 0; pos < len; pos += rowsize) {
1382 char line[0x80];
1383
1384 if (prev && !memcmp(prev, buf + pos, rowsize)) {
1385 if (!skip) {
1386 drm_printf(m, "*\n");
1387 skip = true;
1388 }
1389 continue;
1390 }
1391
1392 WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos, rowsize, sizeof(u32), line, sizeof(line), false) >=
1393 sizeof(line));
1394 drm_printf(m, "[%04zx] %s\n", pos, line);
1395
1396 prev = buf + pos;
1397 skip = false;
1398 }
1399 }
1400
repr_timer(const struct timer_list * t)1401 static const char *repr_timer(const struct timer_list *t)
1402 {
1403 if (!READ_ONCE(t->expires)) {
1404 return "inactive";
1405 }
1406
1407 if (timer_pending(t)) {
1408 return "active";
1409 }
1410
1411 return "expired";
1412 }
1413
intel_engine_print_registers(struct intel_engine_cs * engine,struct drm_printer * m)1414 static void intel_engine_print_registers(struct intel_engine_cs *engine, struct drm_printer *m)
1415 {
1416 struct drm_i915_private *dev_priv = engine->i915;
1417 struct intel_engine_execlists *const execlists = &engine->execlists;
1418 u64 addr;
1419
1420 if (engine->id == RENDER_CLASS && IS_GEN_RANGE(dev_priv, 0x4, 0x7)) {
1421 drm_printf(m, "\tCCID: 0x%08x\n", ENGINE_READ(engine, CCID));
1422 }
1423 if (HAS_EXECLISTS(dev_priv)) {
1424 drm_printf(m, "\tEL_STAT_HI: 0x%08x\n", ENGINE_READ(engine, RING_EXECLIST_STATUS_HI));
1425 drm_printf(m, "\tEL_STAT_LO: 0x%08x\n", ENGINE_READ(engine, RING_EXECLIST_STATUS_LO));
1426 }
1427 drm_printf(m, "\tRING_START: 0x%08x\n", ENGINE_READ(engine, RING_START));
1428 drm_printf(m, "\tRING_HEAD: 0x%08x\n", ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR);
1429 drm_printf(m, "\tRING_TAIL: 0x%08x\n", ENGINE_READ(engine, RING_TAIL) & TAIL_ADDR);
1430 drm_printf(m, "\tRING_CTL: 0x%08x%s\n", ENGINE_READ(engine, RING_CTL),
1431 ENGINE_READ(engine, RING_CTL) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : "");
1432 if (INTEL_GEN(engine->i915) > 0x2) {
1433 drm_printf(m, "\tRING_MODE: 0x%08x%s\n", ENGINE_READ(engine, RING_MI_MODE),
1434 ENGINE_READ(engine, RING_MI_MODE) & (MODE_IDLE) ? " [idle]" : "");
1435 }
1436
1437 if (INTEL_GEN(dev_priv) >= 0x6) {
1438 drm_printf(m, "\tRING_IMR: 0x%08x\n", ENGINE_READ(engine, RING_IMR));
1439 drm_printf(m, "\tRING_ESR: 0x%08x\n", ENGINE_READ(engine, RING_ESR));
1440 drm_printf(m, "\tRING_EMR: 0x%08x\n", ENGINE_READ(engine, RING_EMR));
1441 drm_printf(m, "\tRING_EIR: 0x%08x\n", ENGINE_READ(engine, RING_EIR));
1442 }
1443
1444 addr = intel_engine_get_active_head(engine);
1445 drm_printf(m, "\tACTHD: 0x%08x_%08x\n", upper_32_bits(addr), lower_32_bits(addr));
1446 addr = intel_engine_get_last_batch_head(engine);
1447 drm_printf(m, "\tBBADDR: 0x%08x_%08x\n", upper_32_bits(addr), lower_32_bits(addr));
1448 if (INTEL_GEN(dev_priv) >= 0x8) {
1449 addr = ENGINE_READ64(engine, RING_DMA_FADD, RING_DMA_FADD_UDW);
1450 } else if (INTEL_GEN(dev_priv) >= 0x4) {
1451 addr = ENGINE_READ(engine, RING_DMA_FADD);
1452 } else {
1453 addr = ENGINE_READ(engine, DMA_FADD_I8XX);
1454 }
1455 drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n", upper_32_bits(addr), lower_32_bits(addr));
1456 if (INTEL_GEN(dev_priv) >= 0x4) {
1457 drm_printf(m, "\tIPEIR: 0x%08x\n", ENGINE_READ(engine, RING_IPEIR));
1458 drm_printf(m, "\tIPEHR: 0x%08x\n", ENGINE_READ(engine, RING_IPEHR));
1459 } else {
1460 drm_printf(m, "\tIPEIR: 0x%08x\n", ENGINE_READ(engine, IPEIR));
1461 drm_printf(m, "\tIPEHR: 0x%08x\n", ENGINE_READ(engine, IPEHR));
1462 }
1463
1464 if (HAS_EXECLISTS(dev_priv)) {
1465 struct i915_request *const *port, *rq;
1466 const u32 *hws = &engine->status_page.addr[I915_HWS_CSB_BUF0_INDEX];
1467 const u8 num_entries = execlists->csb_size;
1468 unsigned int idx;
1469 u8 read, write;
1470
1471 drm_printf(m, "\tExeclist tasklet queued? %s (%s), preempt? %s, timeslice? %s\n",
1472 yesno(test_bit(TASKLET_STATE_SCHED, &engine->execlists.tasklet.state)),
1473 enableddisabled(!atomic_read(&engine->execlists.tasklet.count)),
1474 repr_timer(&engine->execlists.preempt), repr_timer(&engine->execlists.timer));
1475
1476 read = execlists->csb_head;
1477 write = READ_ONCE(*execlists->csb_write);
1478
1479 drm_printf(m, "\tExeclist status: 0x%08x %08x; CSB read:%d, write:%d, entries:%d\n",
1480 ENGINE_READ(engine, RING_EXECLIST_STATUS_LO), ENGINE_READ(engine, RING_EXECLIST_STATUS_HI), read,
1481 write, num_entries);
1482
1483 if (read >= num_entries) {
1484 read = 0;
1485 }
1486 if (write >= num_entries) {
1487 write = 0;
1488 }
1489 if (read > write) {
1490 write += num_entries;
1491 }
1492 while (read < write) {
1493 idx = ++read % num_entries;
1494 drm_printf(m, "\tExeclist CSB[%d]: 0x%08x, context: %d\n", idx, hws[idx * 0x2], hws[idx * 0x2 + 1]);
1495 }
1496
1497 execlists_active_lock_bh(execlists);
1498 rcu_read_lock();
1499 for (port = execlists->active; (rq = *port); port++) {
1500 char hdr[0xa0];
1501 int len;
1502
1503 len = scnprintf(hdr, sizeof(hdr), "\t\tActive[%d]: ccid:%08x%s%s, ", (int)(port - execlists->active),
1504 rq->context->lrc.ccid, intel_context_is_closed(rq->context) ? "!" : "",
1505 intel_context_is_banned(rq->context) ? "*" : "");
1506 len += print_ring(hdr + len, sizeof(hdr) - len, rq);
1507 scnprintf(hdr + len, sizeof(hdr) - len, "rq: ");
1508 print_request(m, rq, hdr);
1509 }
1510 for (port = execlists->pending; (rq = *port); port++) {
1511 char hdr[0xa0];
1512 int len;
1513
1514 len = scnprintf(hdr, sizeof(hdr), "\t\tPending[%d]: ccid:%08x%s%s, ", (int)(port - execlists->pending),
1515 rq->context->lrc.ccid, intel_context_is_closed(rq->context) ? "!" : "",
1516 intel_context_is_banned(rq->context) ? "*" : "");
1517 len += print_ring(hdr + len, sizeof(hdr) - len, rq);
1518 scnprintf(hdr + len, sizeof(hdr) - len, "rq: ");
1519 print_request(m, rq, hdr);
1520 }
1521 rcu_read_unlock();
1522 execlists_active_unlock_bh(execlists);
1523 } else if (INTEL_GEN(dev_priv) > 0x6) {
1524 drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n", ENGINE_READ(engine, RING_PP_DIR_BASE));
1525 drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n", ENGINE_READ(engine, RING_PP_DIR_BASE_READ));
1526 drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n", ENGINE_READ(engine, RING_PP_DIR_DCLV));
1527 }
1528 }
1529
print_request_ring(struct drm_printer * m,struct i915_request * rq)1530 static void print_request_ring(struct drm_printer *m, struct i915_request *rq)
1531 {
1532 void *ring;
1533 int size;
1534
1535 drm_printf(m, "[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]:\n", rq->head, rq->postfix, rq->tail,
1536 rq->batch ? upper_32_bits(rq->batch->node.start) : ~0u,
1537 rq->batch ? lower_32_bits(rq->batch->node.start) : ~0u);
1538
1539 size = rq->tail - rq->head;
1540 if (rq->tail < rq->head) {
1541 size += rq->ring->size;
1542 }
1543
1544 ring = kmalloc(size, GFP_ATOMIC);
1545 if (ring) {
1546 const void *vaddr = rq->ring->vaddr;
1547 unsigned int head = rq->head;
1548 unsigned int len = 0;
1549
1550 if (rq->tail < head) {
1551 len = rq->ring->size - head;
1552 memcpy(ring, vaddr + head, len);
1553 head = 0;
1554 }
1555 memcpy(ring + len, vaddr + head, size - len);
1556
1557 hexdump(m, ring, size);
1558 kfree(ring);
1559 }
1560 }
1561
list_count(struct list_head * list)1562 static unsigned long list_count(struct list_head *list)
1563 {
1564 struct list_head *pos;
1565 unsigned long count = 0;
1566
1567 list_for_each(pos, list) count++;
1568
1569 return count;
1570 }
1571
intel_engine_dump(struct intel_engine_cs * engine,struct drm_printer * m,const char * header,...)1572 void intel_engine_dump(struct intel_engine_cs *engine, struct drm_printer *m, const char *header, ...)
1573 {
1574 struct i915_gpu_error *const error = &engine->i915->gpu_error;
1575 struct i915_request *rq;
1576 intel_wakeref_t wakeref;
1577 unsigned long flags;
1578 ktime_t dummy;
1579
1580 if (header) {
1581 va_list ap;
1582
1583 va_start(ap, header);
1584 drm_vprintf(m, header, &ap);
1585 va_end(ap);
1586 }
1587
1588 if (intel_gt_is_wedged(engine->gt)) {
1589 drm_printf(m, "*** WEDGED ***\n");
1590 }
1591
1592 drm_printf(m, "\tAwake? %d\n", atomic_read(&engine->wakeref.count));
1593 drm_printf(m, "\tBarriers?: %s\n", yesno(!llist_empty(&engine->barrier_tasks)));
1594 drm_printf(m, "\tLatency: %luus\n", ewma__engine_latency_read(&engine->latency));
1595 if (intel_engine_supports_stats(engine)) {
1596 drm_printf(m, "\tRuntime: %llums\n", ktime_to_ms(intel_engine_get_busy_time(engine, &dummy)));
1597 }
1598 drm_printf(m, "\tForcewake: %x domains, %d active\n", engine->fw_domain, atomic_read(&engine->fw_active));
1599
1600 rcu_read_lock();
1601 rq = READ_ONCE(engine->heartbeat.systole);
1602 if (rq) {
1603 drm_printf(m, "\tHeartbeat: %d ms ago\n", jiffies_to_msecs(jiffies - rq->emitted_jiffies));
1604 }
1605 rcu_read_unlock();
1606 drm_printf(m, "\tReset count: %d (global %d)\n", i915_reset_engine_count(error, engine), i915_reset_count(error));
1607
1608 drm_printf(m, "\tRequests:\n");
1609
1610 spin_lock_irqsave(&engine->active.lock, flags);
1611 rq = intel_engine_find_active_request(engine);
1612 if (rq) {
1613 struct intel_timeline *tl = get_timeline(rq);
1614
1615 print_request(m, rq, "\t\tactive ");
1616
1617 drm_printf(m, "\t\tring->start: 0x%08x\n", i915_ggtt_offset(rq->ring->vma));
1618 drm_printf(m, "\t\tring->head: 0x%08x\n", rq->ring->head);
1619 drm_printf(m, "\t\tring->tail: 0x%08x\n", rq->ring->tail);
1620 drm_printf(m, "\t\tring->emit: 0x%08x\n", rq->ring->emit);
1621 drm_printf(m, "\t\tring->space: 0x%08x\n", rq->ring->space);
1622
1623 if (tl) {
1624 drm_printf(m, "\t\tring->hwsp: 0x%08x\n", tl->hwsp_offset);
1625 intel_timeline_put(tl);
1626 }
1627
1628 print_request_ring(m, rq);
1629
1630 if (rq->context->lrc_reg_state) {
1631 drm_printf(m, "Logical Ring Context:\n");
1632 hexdump(m, rq->context->lrc_reg_state, PAGE_SIZE);
1633 }
1634 }
1635 drm_printf(m, "\tOn hold?: %lu\n", list_count(&engine->active.hold));
1636 spin_unlock_irqrestore(&engine->active.lock, flags);
1637
1638 drm_printf(m, "\tMMIO base: 0x%08x\n", engine->mmio_base);
1639 wakeref = intel_runtime_pm_get_if_in_use(engine->uncore->rpm);
1640 if (wakeref) {
1641 intel_engine_print_registers(engine, m);
1642 intel_runtime_pm_put(engine->uncore->rpm, wakeref);
1643 } else {
1644 drm_printf(m, "\tDevice is asleep; skipping register dump\n");
1645 }
1646
1647 intel_execlists_show_requests(engine, m, print_request, 0x8);
1648
1649 drm_printf(m, "HWSP:\n");
1650 hexdump(m, engine->status_page.addr, PAGE_SIZE);
1651
1652 drm_printf(m, "Idle? %s\n", yesno(intel_engine_is_idle(engine)));
1653
1654 intel_engine_print_breadcrumbs(engine, m);
1655 }
1656
_intel_engine_get_busy_time(struct intel_engine_cs * engine,ktime_t * now)1657 static ktime_t _intel_engine_get_busy_time(struct intel_engine_cs *engine, ktime_t *now)
1658 {
1659 ktime_t total = engine->stats.total;
1660
1661 /*
1662 * If the engine is executing something at the moment
1663 * add it to the total.
1664 */
1665 *now = ktime_get();
1666 if (atomic_read(&engine->stats.active)) {
1667 total = ktime_add(total, ktime_sub(*now, engine->stats.start));
1668 }
1669
1670 return total;
1671 }
1672
1673 /**
1674 * intel_engine_get_busy_time() - Return current accumulated engine busyness
1675 * @engine: engine to report on
1676 * @now: monotonic timestamp of sampling
1677 *
1678 * Returns accumulated time @engine was busy since engine stats were enabled.
1679 */
intel_engine_get_busy_time(struct intel_engine_cs * engine,ktime_t * now)1680 ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine, ktime_t *now)
1681 {
1682 unsigned int seq;
1683 ktime_t total;
1684
1685 do {
1686 seq = read_seqbegin(&engine->stats.lock);
1687 total = _intel_engine_get_busy_time(engine, now);
1688 } while (read_seqretry(&engine->stats.lock, seq));
1689
1690 return total;
1691 }
1692
match_ring(struct i915_request * rq)1693 static bool match_ring(struct i915_request *rq)
1694 {
1695 u32 ring = ENGINE_READ(rq->engine, RING_START);
1696
1697 return ring == i915_ggtt_offset(rq->ring->vma);
1698 }
1699
intel_engine_find_active_request(struct intel_engine_cs * engine)1700 struct i915_request *intel_engine_find_active_request(struct intel_engine_cs *engine)
1701 {
1702 struct i915_request *request, *active = NULL;
1703
1704 /*
1705 * We are called by the error capture, reset and to dump engine
1706 * state at random points in time. In particular, note that neither is
1707 * crucially ordered with an interrupt. After a hang, the GPU is dead
1708 * and we assume that no more writes can happen (we waited long enough
1709 * for all writes that were in transaction to be flushed) - adding an
1710 * extra delay for a recent interrupt is pointless. Hence, we do
1711 * not need an engine->irq_seqno_barrier() before the seqno reads.
1712 * At all other times, we must assume the GPU is still running, but
1713 * we only care about the snapshot of this moment.
1714 */
1715 lockdep_assert_held(&engine->active.lock);
1716
1717 rcu_read_lock();
1718 request = execlists_active(&engine->execlists);
1719 if (request) {
1720 struct intel_timeline *tl = request->context->timeline;
1721
1722 list_for_each_entry_from_reverse(request, &tl->requests, link)
1723 {
1724 if (i915_request_completed(request)) {
1725 break;
1726 }
1727
1728 active = request;
1729 }
1730 }
1731 rcu_read_unlock();
1732 if (active) {
1733 return active;
1734 }
1735
1736 list_for_each_entry(request, &engine->active.requests, sched.link)
1737 {
1738 if (i915_request_completed(request)) {
1739 continue;
1740 }
1741
1742 if (!i915_request_started(request)) {
1743 continue;
1744 }
1745
1746 /* More than one preemptible request may match! */
1747 if (!match_ring(request)) {
1748 continue;
1749 }
1750
1751 active = request;
1752 break;
1753 }
1754
1755 return active;
1756 }
1757
1758 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1759 #include "mock_engine.c"
1760 #include "selftest_engine.c"
1761 #include "selftest_engine_cs.c"
1762 #endif
1763