1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2016 Intel Corporation
4 */
5
6 #include <linux/string_helpers.h>
7
8 #include <drm/drm_print.h>
9
10 #include "gem/i915_gem_context.h"
11 #include "gem/i915_gem_internal.h"
12 #include "gt/intel_gt_print.h"
13 #include "gt/intel_gt_regs.h"
14
15 #include "i915_cmd_parser.h"
16 #include "i915_drv.h"
17 #include "i915_irq.h"
18 #include "i915_reg.h"
19 #include "intel_breadcrumbs.h"
20 #include "intel_context.h"
21 #include "intel_engine.h"
22 #include "intel_engine_pm.h"
23 #include "intel_engine_regs.h"
24 #include "intel_engine_user.h"
25 #include "intel_execlists_submission.h"
26 #include "intel_gt.h"
27 #include "intel_gt_mcr.h"
28 #include "intel_gt_pm.h"
29 #include "intel_gt_requests.h"
30 #include "intel_lrc.h"
31 #include "intel_lrc_reg.h"
32 #include "intel_reset.h"
33 #include "intel_ring.h"
34 #include "uc/intel_guc_submission.h"
35
36 /* Haswell does have the CXT_SIZE register however it does not appear to be
37 * valid. Now, docs explain in dwords what is in the context object. The full
38 * size is 70720 bytes, however, the power context and execlist context will
39 * never be saved (power context is stored elsewhere, and execlists don't work
40 * on HSW) - so the final size, including the extra state required for the
41 * Resource Streamer, is 66944 bytes, which rounds to 17 pages.
42 */
43 #define HSW_CXT_TOTAL_SIZE (17 * PAGE_SIZE)
44
45 #define DEFAULT_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
46 #define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
47 #define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
48 #define GEN11_LR_CONTEXT_RENDER_SIZE (14 * PAGE_SIZE)
49
50 #define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
51
52 #define MAX_MMIO_BASES 3
53 struct engine_info {
54 u8 class;
55 u8 instance;
56 /* mmio bases table *must* be sorted in reverse graphics_ver order */
57 struct engine_mmio_base {
58 u32 graphics_ver : 8;
59 u32 base : 24;
60 } mmio_bases[MAX_MMIO_BASES];
61 };
62
63 static const struct engine_info intel_engines[] = {
64 [RCS0] = {
65 .class = RENDER_CLASS,
66 .instance = 0,
67 .mmio_bases = {
68 { .graphics_ver = 1, .base = RENDER_RING_BASE }
69 },
70 },
71 [BCS0] = {
72 .class = COPY_ENGINE_CLASS,
73 .instance = 0,
74 .mmio_bases = {
75 { .graphics_ver = 6, .base = BLT_RING_BASE }
76 },
77 },
78 [BCS1] = {
79 .class = COPY_ENGINE_CLASS,
80 .instance = 1,
81 .mmio_bases = {
82 { .graphics_ver = 12, .base = XEHPC_BCS1_RING_BASE }
83 },
84 },
85 [BCS2] = {
86 .class = COPY_ENGINE_CLASS,
87 .instance = 2,
88 .mmio_bases = {
89 { .graphics_ver = 12, .base = XEHPC_BCS2_RING_BASE }
90 },
91 },
92 [BCS3] = {
93 .class = COPY_ENGINE_CLASS,
94 .instance = 3,
95 .mmio_bases = {
96 { .graphics_ver = 12, .base = XEHPC_BCS3_RING_BASE }
97 },
98 },
99 [BCS4] = {
100 .class = COPY_ENGINE_CLASS,
101 .instance = 4,
102 .mmio_bases = {
103 { .graphics_ver = 12, .base = XEHPC_BCS4_RING_BASE }
104 },
105 },
106 [BCS5] = {
107 .class = COPY_ENGINE_CLASS,
108 .instance = 5,
109 .mmio_bases = {
110 { .graphics_ver = 12, .base = XEHPC_BCS5_RING_BASE }
111 },
112 },
113 [BCS6] = {
114 .class = COPY_ENGINE_CLASS,
115 .instance = 6,
116 .mmio_bases = {
117 { .graphics_ver = 12, .base = XEHPC_BCS6_RING_BASE }
118 },
119 },
120 [BCS7] = {
121 .class = COPY_ENGINE_CLASS,
122 .instance = 7,
123 .mmio_bases = {
124 { .graphics_ver = 12, .base = XEHPC_BCS7_RING_BASE }
125 },
126 },
127 [BCS8] = {
128 .class = COPY_ENGINE_CLASS,
129 .instance = 8,
130 .mmio_bases = {
131 { .graphics_ver = 12, .base = XEHPC_BCS8_RING_BASE }
132 },
133 },
134 [VCS0] = {
135 .class = VIDEO_DECODE_CLASS,
136 .instance = 0,
137 .mmio_bases = {
138 { .graphics_ver = 11, .base = GEN11_BSD_RING_BASE },
139 { .graphics_ver = 6, .base = GEN6_BSD_RING_BASE },
140 { .graphics_ver = 4, .base = BSD_RING_BASE }
141 },
142 },
143 [VCS1] = {
144 .class = VIDEO_DECODE_CLASS,
145 .instance = 1,
146 .mmio_bases = {
147 { .graphics_ver = 11, .base = GEN11_BSD2_RING_BASE },
148 { .graphics_ver = 8, .base = GEN8_BSD2_RING_BASE }
149 },
150 },
151 [VCS2] = {
152 .class = VIDEO_DECODE_CLASS,
153 .instance = 2,
154 .mmio_bases = {
155 { .graphics_ver = 11, .base = GEN11_BSD3_RING_BASE }
156 },
157 },
158 [VCS3] = {
159 .class = VIDEO_DECODE_CLASS,
160 .instance = 3,
161 .mmio_bases = {
162 { .graphics_ver = 11, .base = GEN11_BSD4_RING_BASE }
163 },
164 },
165 [VCS4] = {
166 .class = VIDEO_DECODE_CLASS,
167 .instance = 4,
168 .mmio_bases = {
169 { .graphics_ver = 12, .base = XEHP_BSD5_RING_BASE }
170 },
171 },
172 [VCS5] = {
173 .class = VIDEO_DECODE_CLASS,
174 .instance = 5,
175 .mmio_bases = {
176 { .graphics_ver = 12, .base = XEHP_BSD6_RING_BASE }
177 },
178 },
179 [VCS6] = {
180 .class = VIDEO_DECODE_CLASS,
181 .instance = 6,
182 .mmio_bases = {
183 { .graphics_ver = 12, .base = XEHP_BSD7_RING_BASE }
184 },
185 },
186 [VCS7] = {
187 .class = VIDEO_DECODE_CLASS,
188 .instance = 7,
189 .mmio_bases = {
190 { .graphics_ver = 12, .base = XEHP_BSD8_RING_BASE }
191 },
192 },
193 [VECS0] = {
194 .class = VIDEO_ENHANCEMENT_CLASS,
195 .instance = 0,
196 .mmio_bases = {
197 { .graphics_ver = 11, .base = GEN11_VEBOX_RING_BASE },
198 { .graphics_ver = 7, .base = VEBOX_RING_BASE }
199 },
200 },
201 [VECS1] = {
202 .class = VIDEO_ENHANCEMENT_CLASS,
203 .instance = 1,
204 .mmio_bases = {
205 { .graphics_ver = 11, .base = GEN11_VEBOX2_RING_BASE }
206 },
207 },
208 [VECS2] = {
209 .class = VIDEO_ENHANCEMENT_CLASS,
210 .instance = 2,
211 .mmio_bases = {
212 { .graphics_ver = 12, .base = XEHP_VEBOX3_RING_BASE }
213 },
214 },
215 [VECS3] = {
216 .class = VIDEO_ENHANCEMENT_CLASS,
217 .instance = 3,
218 .mmio_bases = {
219 { .graphics_ver = 12, .base = XEHP_VEBOX4_RING_BASE }
220 },
221 },
222 [CCS0] = {
223 .class = COMPUTE_CLASS,
224 .instance = 0,
225 .mmio_bases = {
226 { .graphics_ver = 12, .base = GEN12_COMPUTE0_RING_BASE }
227 }
228 },
229 [CCS1] = {
230 .class = COMPUTE_CLASS,
231 .instance = 1,
232 .mmio_bases = {
233 { .graphics_ver = 12, .base = GEN12_COMPUTE1_RING_BASE }
234 }
235 },
236 [CCS2] = {
237 .class = COMPUTE_CLASS,
238 .instance = 2,
239 .mmio_bases = {
240 { .graphics_ver = 12, .base = GEN12_COMPUTE2_RING_BASE }
241 }
242 },
243 [CCS3] = {
244 .class = COMPUTE_CLASS,
245 .instance = 3,
246 .mmio_bases = {
247 { .graphics_ver = 12, .base = GEN12_COMPUTE3_RING_BASE }
248 }
249 },
250 [GSC0] = {
251 .class = OTHER_CLASS,
252 .instance = OTHER_GSC_INSTANCE,
253 .mmio_bases = {
254 { .graphics_ver = 12, .base = MTL_GSC_RING_BASE }
255 }
256 },
257 };
258
259 /**
260 * intel_engine_context_size() - return the size of the context for an engine
261 * @gt: the gt
262 * @class: engine class
263 *
264 * Each engine class may require a different amount of space for a context
265 * image.
266 *
267 * Return: size (in bytes) of an engine class specific context image
268 *
269 * Note: this size includes the HWSP, which is part of the context image
270 * in LRC mode, but does not include the "shared data page" used with
271 * GuC submission. The caller should account for this if using the GuC.
272 */
intel_engine_context_size(struct intel_gt * gt,u8 class)273 u32 intel_engine_context_size(struct intel_gt *gt, u8 class)
274 {
275 struct intel_uncore *uncore = gt->uncore;
276 u32 cxt_size;
277
278 BUILD_BUG_ON(I915_GTT_PAGE_SIZE != PAGE_SIZE);
279
280 switch (class) {
281 case COMPUTE_CLASS:
282 fallthrough;
283 case RENDER_CLASS:
284 switch (GRAPHICS_VER(gt->i915)) {
285 default:
286 MISSING_CASE(GRAPHICS_VER(gt->i915));
287 return DEFAULT_LR_CONTEXT_RENDER_SIZE;
288 case 12:
289 case 11:
290 return GEN11_LR_CONTEXT_RENDER_SIZE;
291 case 9:
292 return GEN9_LR_CONTEXT_RENDER_SIZE;
293 case 8:
294 return GEN8_LR_CONTEXT_RENDER_SIZE;
295 case 7:
296 if (IS_HASWELL(gt->i915))
297 return HSW_CXT_TOTAL_SIZE;
298
299 cxt_size = intel_uncore_read(uncore, GEN7_CXT_SIZE);
300 return round_up(GEN7_CXT_TOTAL_SIZE(cxt_size) * 64,
301 PAGE_SIZE);
302 case 6:
303 cxt_size = intel_uncore_read(uncore, CXT_SIZE);
304 return round_up(GEN6_CXT_TOTAL_SIZE(cxt_size) * 64,
305 PAGE_SIZE);
306 case 5:
307 case 4:
308 /*
309 * There is a discrepancy here between the size reported
310 * by the register and the size of the context layout
311 * in the docs. Both are described as authorative!
312 *
313 * The discrepancy is on the order of a few cachelines,
314 * but the total is under one page (4k), which is our
315 * minimum allocation anyway so it should all come
316 * out in the wash.
317 */
318 cxt_size = intel_uncore_read(uncore, CXT_SIZE) + 1;
319 gt_dbg(gt, "graphics_ver = %d CXT_SIZE = %d bytes [0x%08x]\n",
320 GRAPHICS_VER(gt->i915), cxt_size * 64,
321 cxt_size - 1);
322 return round_up(cxt_size * 64, PAGE_SIZE);
323 case 3:
324 case 2:
325 /* For the special day when i810 gets merged. */
326 case 1:
327 return 0;
328 }
329 break;
330 default:
331 MISSING_CASE(class);
332 fallthrough;
333 case VIDEO_DECODE_CLASS:
334 case VIDEO_ENHANCEMENT_CLASS:
335 case COPY_ENGINE_CLASS:
336 case OTHER_CLASS:
337 if (GRAPHICS_VER(gt->i915) < 8)
338 return 0;
339 return GEN8_LR_CONTEXT_OTHER_SIZE;
340 }
341 }
342
__engine_mmio_base(struct drm_i915_private * i915,const struct engine_mmio_base * bases)343 static u32 __engine_mmio_base(struct drm_i915_private *i915,
344 const struct engine_mmio_base *bases)
345 {
346 int i;
347
348 for (i = 0; i < MAX_MMIO_BASES; i++)
349 if (GRAPHICS_VER(i915) >= bases[i].graphics_ver)
350 break;
351
352 GEM_BUG_ON(i == MAX_MMIO_BASES);
353 GEM_BUG_ON(!bases[i].base);
354
355 return bases[i].base;
356 }
357
__sprint_engine_name(struct intel_engine_cs * engine)358 static void __sprint_engine_name(struct intel_engine_cs *engine)
359 {
360 /*
361 * Before we know what the uABI name for this engine will be,
362 * we still would like to keep track of this engine in the debug logs.
363 * We throw in a ' here as a reminder that this isn't its final name.
364 */
365 GEM_WARN_ON(snprintf(engine->name, sizeof(engine->name), "%s'%u",
366 intel_engine_class_repr(engine->class),
367 engine->instance) >= sizeof(engine->name));
368 }
369
intel_engine_set_hwsp_writemask(struct intel_engine_cs * engine,u32 mask)370 void intel_engine_set_hwsp_writemask(struct intel_engine_cs *engine, u32 mask)
371 {
372 /*
373 * Though they added more rings on g4x/ilk, they did not add
374 * per-engine HWSTAM until gen6.
375 */
376 if (GRAPHICS_VER(engine->i915) < 6 && engine->class != RENDER_CLASS)
377 return;
378
379 if (GRAPHICS_VER(engine->i915) >= 3)
380 ENGINE_WRITE(engine, RING_HWSTAM, mask);
381 else
382 ENGINE_WRITE16(engine, RING_HWSTAM, mask);
383 }
384
intel_engine_sanitize_mmio(struct intel_engine_cs * engine)385 static void intel_engine_sanitize_mmio(struct intel_engine_cs *engine)
386 {
387 /* Mask off all writes into the unknown HWSP */
388 intel_engine_set_hwsp_writemask(engine, ~0u);
389 }
390
nop_irq_handler(struct intel_engine_cs * engine,u16 iir)391 static void nop_irq_handler(struct intel_engine_cs *engine, u16 iir)
392 {
393 GEM_DEBUG_WARN_ON(iir);
394 }
395
get_reset_domain(u8 ver,enum intel_engine_id id)396 static u32 get_reset_domain(u8 ver, enum intel_engine_id id)
397 {
398 u32 reset_domain;
399
400 if (ver >= 11) {
401 static const u32 engine_reset_domains[] = {
402 [RCS0] = GEN11_GRDOM_RENDER,
403 [BCS0] = GEN11_GRDOM_BLT,
404 [BCS1] = XEHPC_GRDOM_BLT1,
405 [BCS2] = XEHPC_GRDOM_BLT2,
406 [BCS3] = XEHPC_GRDOM_BLT3,
407 [BCS4] = XEHPC_GRDOM_BLT4,
408 [BCS5] = XEHPC_GRDOM_BLT5,
409 [BCS6] = XEHPC_GRDOM_BLT6,
410 [BCS7] = XEHPC_GRDOM_BLT7,
411 [BCS8] = XEHPC_GRDOM_BLT8,
412 [VCS0] = GEN11_GRDOM_MEDIA,
413 [VCS1] = GEN11_GRDOM_MEDIA2,
414 [VCS2] = GEN11_GRDOM_MEDIA3,
415 [VCS3] = GEN11_GRDOM_MEDIA4,
416 [VCS4] = GEN11_GRDOM_MEDIA5,
417 [VCS5] = GEN11_GRDOM_MEDIA6,
418 [VCS6] = GEN11_GRDOM_MEDIA7,
419 [VCS7] = GEN11_GRDOM_MEDIA8,
420 [VECS0] = GEN11_GRDOM_VECS,
421 [VECS1] = GEN11_GRDOM_VECS2,
422 [VECS2] = GEN11_GRDOM_VECS3,
423 [VECS3] = GEN11_GRDOM_VECS4,
424 [CCS0] = GEN11_GRDOM_RENDER,
425 [CCS1] = GEN11_GRDOM_RENDER,
426 [CCS2] = GEN11_GRDOM_RENDER,
427 [CCS3] = GEN11_GRDOM_RENDER,
428 [GSC0] = GEN12_GRDOM_GSC,
429 };
430 GEM_BUG_ON(id >= ARRAY_SIZE(engine_reset_domains) ||
431 !engine_reset_domains[id]);
432 reset_domain = engine_reset_domains[id];
433 } else {
434 static const u32 engine_reset_domains[] = {
435 [RCS0] = GEN6_GRDOM_RENDER,
436 [BCS0] = GEN6_GRDOM_BLT,
437 [VCS0] = GEN6_GRDOM_MEDIA,
438 [VCS1] = GEN8_GRDOM_MEDIA2,
439 [VECS0] = GEN6_GRDOM_VECS,
440 };
441 GEM_BUG_ON(id >= ARRAY_SIZE(engine_reset_domains) ||
442 !engine_reset_domains[id]);
443 reset_domain = engine_reset_domains[id];
444 }
445
446 return reset_domain;
447 }
448
intel_engine_setup(struct intel_gt * gt,enum intel_engine_id id,u8 logical_instance)449 static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id,
450 u8 logical_instance)
451 {
452 const struct engine_info *info = &intel_engines[id];
453 struct drm_i915_private *i915 = gt->i915;
454 struct intel_engine_cs *engine;
455 u8 guc_class;
456
457 BUILD_BUG_ON(MAX_ENGINE_CLASS >= BIT(GEN11_ENGINE_CLASS_WIDTH));
458 BUILD_BUG_ON(MAX_ENGINE_INSTANCE >= BIT(GEN11_ENGINE_INSTANCE_WIDTH));
459 BUILD_BUG_ON(I915_MAX_VCS > (MAX_ENGINE_INSTANCE + 1));
460 BUILD_BUG_ON(I915_MAX_VECS > (MAX_ENGINE_INSTANCE + 1));
461
462 if (GEM_DEBUG_WARN_ON(id >= ARRAY_SIZE(gt->engine)))
463 return -EINVAL;
464
465 if (GEM_DEBUG_WARN_ON(info->class > MAX_ENGINE_CLASS))
466 return -EINVAL;
467
468 if (GEM_DEBUG_WARN_ON(info->instance > MAX_ENGINE_INSTANCE))
469 return -EINVAL;
470
471 if (GEM_DEBUG_WARN_ON(gt->engine_class[info->class][info->instance]))
472 return -EINVAL;
473
474 engine = kzalloc(sizeof(*engine), GFP_KERNEL);
475 if (!engine)
476 return -ENOMEM;
477
478 BUILD_BUG_ON(BITS_PER_TYPE(engine->mask) < I915_NUM_ENGINES);
479
480 INIT_LIST_HEAD(&engine->pinned_contexts_list);
481 engine->id = id;
482 engine->legacy_idx = INVALID_ENGINE;
483 engine->mask = BIT(id);
484 engine->reset_domain = get_reset_domain(GRAPHICS_VER(gt->i915),
485 id);
486 engine->i915 = i915;
487 engine->gt = gt;
488 engine->uncore = gt->uncore;
489 guc_class = engine_class_to_guc_class(info->class);
490 engine->guc_id = MAKE_GUC_ID(guc_class, info->instance);
491 engine->mmio_base = __engine_mmio_base(i915, info->mmio_bases);
492
493 engine->irq_handler = nop_irq_handler;
494
495 engine->class = info->class;
496 engine->instance = info->instance;
497 engine->logical_mask = BIT(logical_instance);
498 __sprint_engine_name(engine);
499
500 if ((engine->class == COMPUTE_CLASS || engine->class == RENDER_CLASS) &&
501 __ffs(CCS_MASK(engine->gt) | RCS_MASK(engine->gt)) == engine->instance)
502 engine->flags |= I915_ENGINE_FIRST_RENDER_COMPUTE;
503
504 /* features common between engines sharing EUs */
505 if (engine->class == RENDER_CLASS || engine->class == COMPUTE_CLASS) {
506 engine->flags |= I915_ENGINE_HAS_RCS_REG_STATE;
507 engine->flags |= I915_ENGINE_HAS_EU_PRIORITY;
508 }
509
510 engine->props.heartbeat_interval_ms =
511 CONFIG_DRM_I915_HEARTBEAT_INTERVAL;
512 engine->props.max_busywait_duration_ns =
513 CONFIG_DRM_I915_MAX_REQUEST_BUSYWAIT;
514 engine->props.preempt_timeout_ms =
515 CONFIG_DRM_I915_PREEMPT_TIMEOUT;
516 engine->props.stop_timeout_ms =
517 CONFIG_DRM_I915_STOP_TIMEOUT;
518 engine->props.timeslice_duration_ms =
519 CONFIG_DRM_I915_TIMESLICE_DURATION;
520
521 /*
522 * Mid-thread pre-emption is not available in Gen12. Unfortunately,
523 * some compute workloads run quite long threads. That means they get
524 * reset due to not pre-empting in a timely manner. So, bump the
525 * pre-emption timeout value to be much higher for compute engines.
526 */
527 if (GRAPHICS_VER(i915) == 12 && (engine->flags & I915_ENGINE_HAS_RCS_REG_STATE))
528 engine->props.preempt_timeout_ms = CONFIG_DRM_I915_PREEMPT_TIMEOUT_COMPUTE;
529
530 /* Cap properties according to any system limits */
531 #define CLAMP_PROP(field) \
532 do { \
533 u64 clamp = intel_clamp_##field(engine, engine->props.field); \
534 if (clamp != engine->props.field) { \
535 drm_notice(&engine->i915->drm, \
536 "Warning, clamping %s to %lld to prevent overflow\n", \
537 #field, clamp); \
538 engine->props.field = clamp; \
539 } \
540 } while (0)
541
542 CLAMP_PROP(heartbeat_interval_ms);
543 CLAMP_PROP(max_busywait_duration_ns);
544 CLAMP_PROP(preempt_timeout_ms);
545 CLAMP_PROP(stop_timeout_ms);
546 CLAMP_PROP(timeslice_duration_ms);
547
548 #undef CLAMP_PROP
549
550 engine->defaults = engine->props; /* never to change again */
551
552 engine->context_size = intel_engine_context_size(gt, engine->class);
553 if (WARN_ON(engine->context_size > BIT(20)))
554 engine->context_size = 0;
555 if (engine->context_size)
556 DRIVER_CAPS(i915)->has_logical_contexts = true;
557
558 ewma__engine_latency_init(&engine->latency);
559
560 ATOMIC_INIT_NOTIFIER_HEAD(&engine->context_status_notifier);
561
562 /* Scrub mmio state on takeover */
563 intel_engine_sanitize_mmio(engine);
564
565 gt->engine_class[info->class][info->instance] = engine;
566 gt->engine[id] = engine;
567
568 return 0;
569 }
570
intel_clamp_heartbeat_interval_ms(struct intel_engine_cs * engine,u64 value)571 u64 intel_clamp_heartbeat_interval_ms(struct intel_engine_cs *engine, u64 value)
572 {
573 value = min_t(u64, value, jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT));
574
575 return value;
576 }
577
intel_clamp_max_busywait_duration_ns(struct intel_engine_cs * engine,u64 value)578 u64 intel_clamp_max_busywait_duration_ns(struct intel_engine_cs *engine, u64 value)
579 {
580 value = min(value, jiffies_to_nsecs(2));
581
582 return value;
583 }
584
intel_clamp_preempt_timeout_ms(struct intel_engine_cs * engine,u64 value)585 u64 intel_clamp_preempt_timeout_ms(struct intel_engine_cs *engine, u64 value)
586 {
587 /*
588 * NB: The GuC API only supports 32bit values. However, the limit is further
589 * reduced due to internal calculations which would otherwise overflow.
590 */
591 if (intel_guc_submission_is_wanted(gt_to_guc(engine->gt)))
592 value = min_t(u64, value, guc_policy_max_preempt_timeout_ms());
593
594 value = min_t(u64, value, jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT));
595
596 return value;
597 }
598
intel_clamp_stop_timeout_ms(struct intel_engine_cs * engine,u64 value)599 u64 intel_clamp_stop_timeout_ms(struct intel_engine_cs *engine, u64 value)
600 {
601 value = min_t(u64, value, jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT));
602
603 return value;
604 }
605
intel_clamp_timeslice_duration_ms(struct intel_engine_cs * engine,u64 value)606 u64 intel_clamp_timeslice_duration_ms(struct intel_engine_cs *engine, u64 value)
607 {
608 /*
609 * NB: The GuC API only supports 32bit values. However, the limit is further
610 * reduced due to internal calculations which would otherwise overflow.
611 */
612 if (intel_guc_submission_is_wanted(gt_to_guc(engine->gt)))
613 value = min_t(u64, value, guc_policy_max_exec_quantum_ms());
614
615 value = min_t(u64, value, jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT));
616
617 return value;
618 }
619
__setup_engine_capabilities(struct intel_engine_cs * engine)620 static void __setup_engine_capabilities(struct intel_engine_cs *engine)
621 {
622 struct drm_i915_private *i915 = engine->i915;
623
624 if (engine->class == VIDEO_DECODE_CLASS) {
625 /*
626 * HEVC support is present on first engine instance
627 * before Gen11 and on all instances afterwards.
628 */
629 if (GRAPHICS_VER(i915) >= 11 ||
630 (GRAPHICS_VER(i915) >= 9 && engine->instance == 0))
631 engine->uabi_capabilities |=
632 I915_VIDEO_CLASS_CAPABILITY_HEVC;
633
634 /*
635 * SFC block is present only on even logical engine
636 * instances.
637 */
638 if ((GRAPHICS_VER(i915) >= 11 &&
639 (engine->gt->info.vdbox_sfc_access &
640 BIT(engine->instance))) ||
641 (GRAPHICS_VER(i915) >= 9 && engine->instance == 0))
642 engine->uabi_capabilities |=
643 I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC;
644 } else if (engine->class == VIDEO_ENHANCEMENT_CLASS) {
645 if (GRAPHICS_VER(i915) >= 9 &&
646 engine->gt->info.sfc_mask & BIT(engine->instance))
647 engine->uabi_capabilities |=
648 I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC;
649 }
650 }
651
intel_setup_engine_capabilities(struct intel_gt * gt)652 static void intel_setup_engine_capabilities(struct intel_gt *gt)
653 {
654 struct intel_engine_cs *engine;
655 enum intel_engine_id id;
656
657 for_each_engine(engine, gt, id)
658 __setup_engine_capabilities(engine);
659 }
660
661 /**
662 * intel_engines_release() - free the resources allocated for Command Streamers
663 * @gt: pointer to struct intel_gt
664 */
intel_engines_release(struct intel_gt * gt)665 void intel_engines_release(struct intel_gt *gt)
666 {
667 struct intel_engine_cs *engine;
668 enum intel_engine_id id;
669
670 /*
671 * Before we release the resources held by engine, we must be certain
672 * that the HW is no longer accessing them -- having the GPU scribble
673 * to or read from a page being used for something else causes no end
674 * of fun.
675 *
676 * The GPU should be reset by this point, but assume the worst just
677 * in case we aborted before completely initialising the engines.
678 */
679 GEM_BUG_ON(intel_gt_pm_is_awake(gt));
680 if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
681 intel_gt_reset_all_engines(gt);
682
683 /* Decouple the backend; but keep the layout for late GPU resets */
684 for_each_engine(engine, gt, id) {
685 if (!engine->release)
686 continue;
687
688 intel_wakeref_wait_for_idle(&engine->wakeref);
689 GEM_BUG_ON(intel_engine_pm_is_awake(engine));
690
691 engine->release(engine);
692 engine->release = NULL;
693
694 memset(&engine->reset, 0, sizeof(engine->reset));
695 }
696
697 llist_del_all(>->i915->uabi_engines_llist);
698 }
699
intel_engine_free_request_pool(struct intel_engine_cs * engine)700 void intel_engine_free_request_pool(struct intel_engine_cs *engine)
701 {
702 if (!engine->request_pool)
703 return;
704
705 #ifdef __linux__
706 kmem_cache_free(i915_request_slab_cache(), engine->request_pool);
707 #else
708 pool_put(i915_request_slab_cache(), engine->request_pool);
709 #endif
710 }
711
intel_engines_free(struct intel_gt * gt)712 void intel_engines_free(struct intel_gt *gt)
713 {
714 struct intel_engine_cs *engine;
715 enum intel_engine_id id;
716
717 /* Free the requests! dma-resv keeps fences around for an eternity */
718 rcu_barrier();
719
720 for_each_engine(engine, gt, id) {
721 intel_engine_free_request_pool(engine);
722 kfree(engine);
723 gt->engine[id] = NULL;
724 }
725 }
726
727 static
gen11_vdbox_has_sfc(struct intel_gt * gt,unsigned int physical_vdbox,unsigned int logical_vdbox,u16 vdbox_mask)728 bool gen11_vdbox_has_sfc(struct intel_gt *gt,
729 unsigned int physical_vdbox,
730 unsigned int logical_vdbox, u16 vdbox_mask)
731 {
732 struct drm_i915_private *i915 = gt->i915;
733
734 /*
735 * In Gen11, only even numbered logical VDBOXes are hooked
736 * up to an SFC (Scaler & Format Converter) unit.
737 * In Gen12, Even numbered physical instance always are connected
738 * to an SFC. Odd numbered physical instances have SFC only if
739 * previous even instance is fused off.
740 *
741 * Starting with Xe_HP, there's also a dedicated SFC_ENABLE field
742 * in the fuse register that tells us whether a specific SFC is present.
743 */
744 if ((gt->info.sfc_mask & BIT(physical_vdbox / 2)) == 0)
745 return false;
746 else if (MEDIA_VER(i915) >= 12)
747 return (physical_vdbox % 2 == 0) ||
748 !(BIT(physical_vdbox - 1) & vdbox_mask);
749 else if (MEDIA_VER(i915) == 11)
750 return logical_vdbox % 2 == 0;
751
752 return false;
753 }
754
engine_mask_apply_media_fuses(struct intel_gt * gt)755 static void engine_mask_apply_media_fuses(struct intel_gt *gt)
756 {
757 struct drm_i915_private *i915 = gt->i915;
758 unsigned int logical_vdbox = 0;
759 unsigned int i;
760 u32 media_fuse, fuse1;
761 u16 vdbox_mask;
762 u16 vebox_mask;
763
764 if (MEDIA_VER(gt->i915) < 11)
765 return;
766
767 /*
768 * On newer platforms the fusing register is called 'enable' and has
769 * enable semantics, while on older platforms it is called 'disable'
770 * and bits have disable semantices.
771 */
772 media_fuse = intel_uncore_read(gt->uncore, GEN11_GT_VEBOX_VDBOX_DISABLE);
773 if (MEDIA_VER_FULL(i915) < IP_VER(12, 55))
774 media_fuse = ~media_fuse;
775
776 vdbox_mask = media_fuse & GEN11_GT_VDBOX_DISABLE_MASK;
777 vebox_mask = (media_fuse & GEN11_GT_VEBOX_DISABLE_MASK) >>
778 GEN11_GT_VEBOX_DISABLE_SHIFT;
779
780 if (MEDIA_VER_FULL(i915) >= IP_VER(12, 55)) {
781 fuse1 = intel_uncore_read(gt->uncore, HSW_PAVP_FUSE1);
782 gt->info.sfc_mask = REG_FIELD_GET(XEHP_SFC_ENABLE_MASK, fuse1);
783 } else {
784 gt->info.sfc_mask = ~0;
785 }
786
787 for (i = 0; i < I915_MAX_VCS; i++) {
788 if (!HAS_ENGINE(gt, _VCS(i))) {
789 vdbox_mask &= ~BIT(i);
790 continue;
791 }
792
793 if (!(BIT(i) & vdbox_mask)) {
794 gt->info.engine_mask &= ~BIT(_VCS(i));
795 gt_dbg(gt, "vcs%u fused off\n", i);
796 continue;
797 }
798
799 if (gen11_vdbox_has_sfc(gt, i, logical_vdbox, vdbox_mask))
800 gt->info.vdbox_sfc_access |= BIT(i);
801 logical_vdbox++;
802 }
803 gt_dbg(gt, "vdbox enable: %04x, instances: %04lx\n", vdbox_mask, VDBOX_MASK(gt));
804 GEM_BUG_ON(vdbox_mask != VDBOX_MASK(gt));
805
806 for (i = 0; i < I915_MAX_VECS; i++) {
807 if (!HAS_ENGINE(gt, _VECS(i))) {
808 vebox_mask &= ~BIT(i);
809 continue;
810 }
811
812 if (!(BIT(i) & vebox_mask)) {
813 gt->info.engine_mask &= ~BIT(_VECS(i));
814 gt_dbg(gt, "vecs%u fused off\n", i);
815 }
816 }
817 gt_dbg(gt, "vebox enable: %04x, instances: %04lx\n", vebox_mask, VEBOX_MASK(gt));
818 GEM_BUG_ON(vebox_mask != VEBOX_MASK(gt));
819 }
820
engine_mask_apply_compute_fuses(struct intel_gt * gt)821 static void engine_mask_apply_compute_fuses(struct intel_gt *gt)
822 {
823 struct drm_i915_private *i915 = gt->i915;
824 struct intel_gt_info *info = >->info;
825 int ss_per_ccs = info->sseu.max_subslices / I915_MAX_CCS;
826 unsigned long ccs_mask;
827 unsigned int i;
828
829 if (GRAPHICS_VER(i915) < 11)
830 return;
831
832 if (hweight32(CCS_MASK(gt)) <= 1)
833 return;
834
835 ccs_mask = intel_slicemask_from_xehp_dssmask(info->sseu.compute_subslice_mask,
836 ss_per_ccs);
837 /*
838 * If all DSS in a quadrant are fused off, the corresponding CCS
839 * engine is not available for use.
840 */
841 for_each_clear_bit(i, &ccs_mask, I915_MAX_CCS) {
842 info->engine_mask &= ~BIT(_CCS(i));
843 gt_dbg(gt, "ccs%u fused off\n", i);
844 }
845 }
846
847 /*
848 * Determine which engines are fused off in our particular hardware.
849 * Note that we have a catch-22 situation where we need to be able to access
850 * the blitter forcewake domain to read the engine fuses, but at the same time
851 * we need to know which engines are available on the system to know which
852 * forcewake domains are present. We solve this by intializing the forcewake
853 * domains based on the full engine mask in the platform capabilities before
854 * calling this function and pruning the domains for fused-off engines
855 * afterwards.
856 */
init_engine_mask(struct intel_gt * gt)857 static intel_engine_mask_t init_engine_mask(struct intel_gt *gt)
858 {
859 struct intel_gt_info *info = >->info;
860
861 GEM_BUG_ON(!info->engine_mask);
862
863 engine_mask_apply_media_fuses(gt);
864 engine_mask_apply_compute_fuses(gt);
865
866 /*
867 * The only use of the GSC CS is to load and communicate with the GSC
868 * FW, so we have no use for it if we don't have the FW.
869 *
870 * IMPORTANT: in cases where we don't have the GSC FW, we have a
871 * catch-22 situation that breaks media C6 due to 2 requirements:
872 * 1) once turned on, the GSC power well will not go to sleep unless the
873 * GSC FW is loaded.
874 * 2) to enable idling (which is required for media C6) we need to
875 * initialize the IDLE_MSG register for the GSC CS and do at least 1
876 * submission, which will wake up the GSC power well.
877 */
878 if (__HAS_ENGINE(info->engine_mask, GSC0) && !intel_uc_wants_gsc_uc(>->uc)) {
879 gt_notice(gt, "No GSC FW selected, disabling GSC CS and media C6\n");
880 info->engine_mask &= ~BIT(GSC0);
881 }
882
883 /*
884 * Do not create the command streamer for CCS slices beyond the first.
885 * All the workload submitted to the first engine will be shared among
886 * all the slices.
887 *
888 * Once the user will be allowed to customize the CCS mode, then this
889 * check needs to be removed.
890 */
891 if (IS_DG2(gt->i915)) {
892 u8 first_ccs = __ffs(CCS_MASK(gt));
893
894 /*
895 * Store the number of active cslices before
896 * changing the CCS engine configuration
897 */
898 gt->ccs.cslices = CCS_MASK(gt);
899
900 /* Mask off all the CCS engine */
901 info->engine_mask &= ~GENMASK(CCS3, CCS0);
902 /* Put back in the first CCS engine */
903 info->engine_mask |= BIT(_CCS(first_ccs));
904 }
905
906 return info->engine_mask;
907 }
908
populate_logical_ids(struct intel_gt * gt,u8 * logical_ids,u8 class,const u8 * map,u8 num_instances)909 static void populate_logical_ids(struct intel_gt *gt, u8 *logical_ids,
910 u8 class, const u8 *map, u8 num_instances)
911 {
912 int i, j;
913 u8 current_logical_id = 0;
914
915 for (j = 0; j < num_instances; ++j) {
916 for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
917 if (!HAS_ENGINE(gt, i) ||
918 intel_engines[i].class != class)
919 continue;
920
921 if (intel_engines[i].instance == map[j]) {
922 logical_ids[intel_engines[i].instance] =
923 current_logical_id++;
924 break;
925 }
926 }
927 }
928 }
929
setup_logical_ids(struct intel_gt * gt,u8 * logical_ids,u8 class)930 static void setup_logical_ids(struct intel_gt *gt, u8 *logical_ids, u8 class)
931 {
932 /*
933 * Logical to physical mapping is needed for proper support
934 * to split-frame feature.
935 */
936 if (MEDIA_VER(gt->i915) >= 11 && class == VIDEO_DECODE_CLASS) {
937 const u8 map[] = { 0, 2, 4, 6, 1, 3, 5, 7 };
938
939 populate_logical_ids(gt, logical_ids, class,
940 map, ARRAY_SIZE(map));
941 } else {
942 int i;
943 u8 map[MAX_ENGINE_INSTANCE + 1];
944
945 for (i = 0; i < MAX_ENGINE_INSTANCE + 1; ++i)
946 map[i] = i;
947 populate_logical_ids(gt, logical_ids, class,
948 map, ARRAY_SIZE(map));
949 }
950 }
951
952 /**
953 * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
954 * @gt: pointer to struct intel_gt
955 *
956 * Return: non-zero if the initialization failed.
957 */
intel_engines_init_mmio(struct intel_gt * gt)958 int intel_engines_init_mmio(struct intel_gt *gt)
959 {
960 struct drm_i915_private *i915 = gt->i915;
961 const unsigned int engine_mask = init_engine_mask(gt);
962 unsigned int mask = 0;
963 unsigned int i, class;
964 u8 logical_ids[MAX_ENGINE_INSTANCE + 1];
965 int err;
966
967 drm_WARN_ON(&i915->drm, engine_mask == 0);
968 drm_WARN_ON(&i915->drm, engine_mask &
969 GENMASK(BITS_PER_TYPE(mask) - 1, I915_NUM_ENGINES));
970
971 if (i915_inject_probe_failure(i915))
972 return -ENODEV;
973
974 for (class = 0; class < MAX_ENGINE_CLASS + 1; ++class) {
975 setup_logical_ids(gt, logical_ids, class);
976
977 for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
978 u8 instance = intel_engines[i].instance;
979
980 if (intel_engines[i].class != class ||
981 !HAS_ENGINE(gt, i))
982 continue;
983
984 err = intel_engine_setup(gt, i,
985 logical_ids[instance]);
986 if (err)
987 goto cleanup;
988
989 mask |= BIT(i);
990 }
991 }
992
993 /*
994 * Catch failures to update intel_engines table when the new engines
995 * are added to the driver by a warning and disabling the forgotten
996 * engines.
997 */
998 if (drm_WARN_ON(&i915->drm, mask != engine_mask))
999 gt->info.engine_mask = mask;
1000
1001 gt->info.num_engines = hweight32(mask);
1002
1003 intel_gt_check_and_clear_faults(gt);
1004
1005 intel_setup_engine_capabilities(gt);
1006
1007 intel_uncore_prune_engine_fw_domains(gt->uncore, gt);
1008
1009 return 0;
1010
1011 cleanup:
1012 intel_engines_free(gt);
1013 return err;
1014 }
1015
intel_engine_init_execlists(struct intel_engine_cs * engine)1016 void intel_engine_init_execlists(struct intel_engine_cs *engine)
1017 {
1018 struct intel_engine_execlists * const execlists = &engine->execlists;
1019
1020 execlists->port_mask = 1;
1021 GEM_BUG_ON(!is_power_of_2(execlists_num_ports(execlists)));
1022 GEM_BUG_ON(execlists_num_ports(execlists) > EXECLIST_MAX_PORTS);
1023
1024 memset(execlists->pending, 0, sizeof(execlists->pending));
1025 execlists->active =
1026 memset(execlists->inflight, 0, sizeof(execlists->inflight));
1027 }
1028
cleanup_status_page(struct intel_engine_cs * engine)1029 static void cleanup_status_page(struct intel_engine_cs *engine)
1030 {
1031 struct i915_vma *vma;
1032
1033 /* Prevent writes into HWSP after returning the page to the system */
1034 intel_engine_set_hwsp_writemask(engine, ~0u);
1035
1036 vma = fetch_and_zero(&engine->status_page.vma);
1037 if (!vma)
1038 return;
1039
1040 if (!HWS_NEEDS_PHYSICAL(engine->i915))
1041 i915_vma_unpin(vma);
1042
1043 i915_gem_object_unpin_map(vma->obj);
1044 i915_gem_object_put(vma->obj);
1045 }
1046
pin_ggtt_status_page(struct intel_engine_cs * engine,struct i915_gem_ww_ctx * ww,struct i915_vma * vma)1047 static int pin_ggtt_status_page(struct intel_engine_cs *engine,
1048 struct i915_gem_ww_ctx *ww,
1049 struct i915_vma *vma)
1050 {
1051 unsigned int flags;
1052
1053 if (!HAS_LLC(engine->i915) && i915_ggtt_has_aperture(engine->gt->ggtt))
1054 /*
1055 * On g33, we cannot place HWS above 256MiB, so
1056 * restrict its pinning to the low mappable arena.
1057 * Though this restriction is not documented for
1058 * gen4, gen5, or byt, they also behave similarly
1059 * and hang if the HWS is placed at the top of the
1060 * GTT. To generalise, it appears that all !llc
1061 * platforms have issues with us placing the HWS
1062 * above the mappable region (even though we never
1063 * actually map it).
1064 */
1065 flags = PIN_MAPPABLE;
1066 else
1067 flags = PIN_HIGH;
1068
1069 return i915_ggtt_pin(vma, ww, 0, flags);
1070 }
1071
init_status_page(struct intel_engine_cs * engine)1072 static int init_status_page(struct intel_engine_cs *engine)
1073 {
1074 struct drm_i915_gem_object *obj;
1075 struct i915_gem_ww_ctx ww;
1076 struct i915_vma *vma;
1077 void *vaddr;
1078 int ret;
1079
1080 INIT_LIST_HEAD(&engine->status_page.timelines);
1081
1082 /*
1083 * Though the HWS register does support 36bit addresses, historically
1084 * we have had hangs and corruption reported due to wild writes if
1085 * the HWS is placed above 4G. We only allow objects to be allocated
1086 * in GFP_DMA32 for i965, and no earlier physical address users had
1087 * access to more than 4G.
1088 */
1089 obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
1090 if (IS_ERR(obj)) {
1091 gt_err(engine->gt, "Failed to allocate status page\n");
1092 return PTR_ERR(obj);
1093 }
1094
1095 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
1096
1097 vma = i915_vma_instance(obj, &engine->gt->ggtt->vm, NULL);
1098 if (IS_ERR(vma)) {
1099 ret = PTR_ERR(vma);
1100 goto err_put;
1101 }
1102
1103 i915_gem_ww_ctx_init(&ww, true);
1104 retry:
1105 ret = i915_gem_object_lock(obj, &ww);
1106 if (!ret && !HWS_NEEDS_PHYSICAL(engine->i915))
1107 ret = pin_ggtt_status_page(engine, &ww, vma);
1108 if (ret)
1109 goto err;
1110
1111 vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
1112 if (IS_ERR(vaddr)) {
1113 ret = PTR_ERR(vaddr);
1114 goto err_unpin;
1115 }
1116
1117 engine->status_page.addr = memset(vaddr, 0, PAGE_SIZE);
1118 engine->status_page.vma = vma;
1119
1120 err_unpin:
1121 if (ret)
1122 i915_vma_unpin(vma);
1123 err:
1124 if (ret == -EDEADLK) {
1125 ret = i915_gem_ww_ctx_backoff(&ww);
1126 if (!ret)
1127 goto retry;
1128 }
1129 i915_gem_ww_ctx_fini(&ww);
1130 err_put:
1131 if (ret)
1132 i915_gem_object_put(obj);
1133 return ret;
1134 }
1135
intel_engine_init_tlb_invalidation(struct intel_engine_cs * engine)1136 static int intel_engine_init_tlb_invalidation(struct intel_engine_cs *engine)
1137 {
1138 static const union intel_engine_tlb_inv_reg gen8_regs[] = {
1139 [RENDER_CLASS].reg = GEN8_RTCR,
1140 [VIDEO_DECODE_CLASS].reg = GEN8_M1TCR, /* , GEN8_M2TCR */
1141 [VIDEO_ENHANCEMENT_CLASS].reg = GEN8_VTCR,
1142 [COPY_ENGINE_CLASS].reg = GEN8_BTCR,
1143 };
1144 static const union intel_engine_tlb_inv_reg gen12_regs[] = {
1145 [RENDER_CLASS].reg = GEN12_GFX_TLB_INV_CR,
1146 [VIDEO_DECODE_CLASS].reg = GEN12_VD_TLB_INV_CR,
1147 [VIDEO_ENHANCEMENT_CLASS].reg = GEN12_VE_TLB_INV_CR,
1148 [COPY_ENGINE_CLASS].reg = GEN12_BLT_TLB_INV_CR,
1149 [COMPUTE_CLASS].reg = GEN12_COMPCTX_TLB_INV_CR,
1150 };
1151 static const union intel_engine_tlb_inv_reg xehp_regs[] = {
1152 [RENDER_CLASS].mcr_reg = XEHP_GFX_TLB_INV_CR,
1153 [VIDEO_DECODE_CLASS].mcr_reg = XEHP_VD_TLB_INV_CR,
1154 [VIDEO_ENHANCEMENT_CLASS].mcr_reg = XEHP_VE_TLB_INV_CR,
1155 [COPY_ENGINE_CLASS].mcr_reg = XEHP_BLT_TLB_INV_CR,
1156 [COMPUTE_CLASS].mcr_reg = XEHP_COMPCTX_TLB_INV_CR,
1157 };
1158 static const union intel_engine_tlb_inv_reg xelpmp_regs[] = {
1159 [VIDEO_DECODE_CLASS].reg = GEN12_VD_TLB_INV_CR,
1160 [VIDEO_ENHANCEMENT_CLASS].reg = GEN12_VE_TLB_INV_CR,
1161 [OTHER_CLASS].reg = XELPMP_GSC_TLB_INV_CR,
1162 };
1163 struct drm_i915_private *i915 = engine->i915;
1164 const unsigned int instance = engine->instance;
1165 const unsigned int class = engine->class;
1166 const union intel_engine_tlb_inv_reg *regs;
1167 union intel_engine_tlb_inv_reg reg;
1168 unsigned int num = 0;
1169 u32 val;
1170
1171 /*
1172 * New platforms should not be added with catch-all-newer (>=)
1173 * condition so that any later platform added triggers the below warning
1174 * and in turn mandates a human cross-check of whether the invalidation
1175 * flows have compatible semantics.
1176 *
1177 * For instance with the 11.00 -> 12.00 transition three out of five
1178 * respective engine registers were moved to masked type. Then after the
1179 * 12.00 -> 12.50 transition multi cast handling is required too.
1180 */
1181
1182 if (engine->gt->type == GT_MEDIA) {
1183 if (MEDIA_VER_FULL(i915) == IP_VER(13, 0)) {
1184 regs = xelpmp_regs;
1185 num = ARRAY_SIZE(xelpmp_regs);
1186 }
1187 } else {
1188 if (GRAPHICS_VER_FULL(i915) == IP_VER(12, 74) ||
1189 GRAPHICS_VER_FULL(i915) == IP_VER(12, 71) ||
1190 GRAPHICS_VER_FULL(i915) == IP_VER(12, 70) ||
1191 GRAPHICS_VER_FULL(i915) == IP_VER(12, 55)) {
1192 regs = xehp_regs;
1193 num = ARRAY_SIZE(xehp_regs);
1194 } else if (GRAPHICS_VER_FULL(i915) == IP_VER(12, 0) ||
1195 GRAPHICS_VER_FULL(i915) == IP_VER(12, 10)) {
1196 regs = gen12_regs;
1197 num = ARRAY_SIZE(gen12_regs);
1198 } else if (GRAPHICS_VER(i915) >= 8 && GRAPHICS_VER(i915) <= 11) {
1199 regs = gen8_regs;
1200 num = ARRAY_SIZE(gen8_regs);
1201 } else if (GRAPHICS_VER(i915) < 8) {
1202 return 0;
1203 }
1204 }
1205
1206 if (gt_WARN_ONCE(engine->gt, !num,
1207 "Platform does not implement TLB invalidation!"))
1208 return -ENODEV;
1209
1210 if (gt_WARN_ON_ONCE(engine->gt,
1211 class >= num ||
1212 (!regs[class].reg.reg &&
1213 !regs[class].mcr_reg.reg)))
1214 return -ERANGE;
1215
1216 reg = regs[class];
1217
1218 if (regs == xelpmp_regs && class == OTHER_CLASS) {
1219 /*
1220 * There's only a single GSC instance, but it uses register bit
1221 * 1 instead of either 0 or OTHER_GSC_INSTANCE.
1222 */
1223 GEM_WARN_ON(instance != OTHER_GSC_INSTANCE);
1224 val = 1;
1225 } else if (regs == gen8_regs && class == VIDEO_DECODE_CLASS && instance == 1) {
1226 reg.reg = GEN8_M2TCR;
1227 val = 0;
1228 } else {
1229 val = instance;
1230 }
1231
1232 val = BIT(val);
1233
1234 engine->tlb_inv.mcr = regs == xehp_regs;
1235 engine->tlb_inv.reg = reg;
1236 engine->tlb_inv.done = val;
1237
1238 if (GRAPHICS_VER(i915) >= 12 &&
1239 (engine->class == VIDEO_DECODE_CLASS ||
1240 engine->class == VIDEO_ENHANCEMENT_CLASS ||
1241 engine->class == COMPUTE_CLASS ||
1242 engine->class == OTHER_CLASS))
1243 engine->tlb_inv.request = _MASKED_BIT_ENABLE(val);
1244 else
1245 engine->tlb_inv.request = val;
1246
1247 return 0;
1248 }
1249
engine_setup_common(struct intel_engine_cs * engine)1250 static int engine_setup_common(struct intel_engine_cs *engine)
1251 {
1252 int err;
1253
1254 init_llist_head(&engine->barrier_tasks);
1255
1256 err = intel_engine_init_tlb_invalidation(engine);
1257 if (err)
1258 return err;
1259
1260 err = init_status_page(engine);
1261 if (err)
1262 return err;
1263
1264 engine->breadcrumbs = intel_breadcrumbs_create(engine);
1265 if (!engine->breadcrumbs) {
1266 err = -ENOMEM;
1267 goto err_status;
1268 }
1269
1270 engine->sched_engine = i915_sched_engine_create(ENGINE_PHYSICAL);
1271 if (!engine->sched_engine) {
1272 err = -ENOMEM;
1273 goto err_sched_engine;
1274 }
1275 engine->sched_engine->private_data = engine;
1276
1277 err = intel_engine_init_cmd_parser(engine);
1278 if (err)
1279 goto err_cmd_parser;
1280
1281 intel_engine_init_execlists(engine);
1282 intel_engine_init__pm(engine);
1283 intel_engine_init_retire(engine);
1284
1285 /* Use the whole device by default */
1286 engine->sseu =
1287 intel_sseu_from_device_info(&engine->gt->info.sseu);
1288
1289 intel_engine_init_workarounds(engine);
1290 intel_engine_init_whitelist(engine);
1291 intel_engine_init_ctx_wa(engine);
1292
1293 if (GRAPHICS_VER(engine->i915) >= 12)
1294 engine->flags |= I915_ENGINE_HAS_RELATIVE_MMIO;
1295
1296 return 0;
1297
1298 err_cmd_parser:
1299 i915_sched_engine_put(engine->sched_engine);
1300 err_sched_engine:
1301 intel_breadcrumbs_put(engine->breadcrumbs);
1302 err_status:
1303 cleanup_status_page(engine);
1304 return err;
1305 }
1306
1307 struct measure_breadcrumb {
1308 struct i915_request rq;
1309 struct intel_ring ring;
1310 u32 cs[2048];
1311 };
1312
measure_breadcrumb_dw(struct intel_context * ce)1313 static int measure_breadcrumb_dw(struct intel_context *ce)
1314 {
1315 struct intel_engine_cs *engine = ce->engine;
1316 struct measure_breadcrumb *frame;
1317 int dw;
1318
1319 GEM_BUG_ON(!engine->gt->scratch);
1320
1321 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1322 if (!frame)
1323 return -ENOMEM;
1324
1325 frame->rq.i915 = engine->i915;
1326 frame->rq.engine = engine;
1327 frame->rq.context = ce;
1328 rcu_assign_pointer(frame->rq.timeline, ce->timeline);
1329 frame->rq.hwsp_seqno = ce->timeline->hwsp_seqno;
1330
1331 frame->ring.vaddr = frame->cs;
1332 frame->ring.size = sizeof(frame->cs);
1333 frame->ring.wrap =
1334 BITS_PER_TYPE(frame->ring.size) - ilog2(frame->ring.size);
1335 frame->ring.effective_size = frame->ring.size;
1336 intel_ring_update_space(&frame->ring);
1337 frame->rq.ring = &frame->ring;
1338
1339 mutex_lock(&ce->timeline->mutex);
1340 spin_lock_irq(&engine->sched_engine->lock);
1341
1342 dw = engine->emit_fini_breadcrumb(&frame->rq, frame->cs) - frame->cs;
1343
1344 spin_unlock_irq(&engine->sched_engine->lock);
1345 mutex_unlock(&ce->timeline->mutex);
1346
1347 GEM_BUG_ON(dw & 1); /* RING_TAIL must be qword aligned */
1348
1349 kfree(frame);
1350 return dw;
1351 }
1352
1353 struct intel_context *
intel_engine_create_pinned_context(struct intel_engine_cs * engine,struct i915_address_space * vm,unsigned int ring_size,unsigned int hwsp,struct lock_class_key * key,const char * name)1354 intel_engine_create_pinned_context(struct intel_engine_cs *engine,
1355 struct i915_address_space *vm,
1356 unsigned int ring_size,
1357 unsigned int hwsp,
1358 struct lock_class_key *key,
1359 const char *name)
1360 {
1361 struct intel_context *ce;
1362 int err;
1363
1364 ce = intel_context_create(engine);
1365 if (IS_ERR(ce))
1366 return ce;
1367
1368 __set_bit(CONTEXT_BARRIER_BIT, &ce->flags);
1369 ce->timeline = page_pack_bits(NULL, hwsp);
1370 ce->ring = NULL;
1371 ce->ring_size = ring_size;
1372
1373 i915_vm_put(ce->vm);
1374 ce->vm = i915_vm_get(vm);
1375
1376 err = intel_context_pin(ce); /* perma-pin so it is always available */
1377 if (err) {
1378 intel_context_put(ce);
1379 return ERR_PTR(err);
1380 }
1381
1382 list_add_tail(&ce->pinned_contexts_link, &engine->pinned_contexts_list);
1383
1384 /*
1385 * Give our perma-pinned kernel timelines a separate lockdep class,
1386 * so that we can use them from within the normal user timelines
1387 * should we need to inject GPU operations during their request
1388 * construction.
1389 */
1390 lockdep_set_class_and_name(&ce->timeline->mutex, key, name);
1391
1392 return ce;
1393 }
1394
intel_engine_destroy_pinned_context(struct intel_context * ce)1395 void intel_engine_destroy_pinned_context(struct intel_context *ce)
1396 {
1397 struct intel_engine_cs *engine = ce->engine;
1398 struct i915_vma *hwsp = engine->status_page.vma;
1399
1400 GEM_BUG_ON(ce->timeline->hwsp_ggtt != hwsp);
1401
1402 mutex_lock(&hwsp->vm->mutex);
1403 list_del(&ce->timeline->engine_link);
1404 mutex_unlock(&hwsp->vm->mutex);
1405
1406 list_del(&ce->pinned_contexts_link);
1407 intel_context_unpin(ce);
1408 intel_context_put(ce);
1409 }
1410
1411 static struct intel_context *
create_ggtt_bind_context(struct intel_engine_cs * engine)1412 create_ggtt_bind_context(struct intel_engine_cs *engine)
1413 {
1414 static struct lock_class_key kernel;
1415
1416 /*
1417 * MI_UPDATE_GTT can insert up to 511 PTE entries and there could be multiple
1418 * bind requets at a time so get a bigger ring.
1419 */
1420 return intel_engine_create_pinned_context(engine, engine->gt->vm, SZ_512K,
1421 I915_GEM_HWS_GGTT_BIND_ADDR,
1422 &kernel, "ggtt_bind_context");
1423 }
1424
1425 static struct intel_context *
create_kernel_context(struct intel_engine_cs * engine)1426 create_kernel_context(struct intel_engine_cs *engine)
1427 {
1428 static struct lock_class_key kernel;
1429
1430 return intel_engine_create_pinned_context(engine, engine->gt->vm, SZ_4K,
1431 I915_GEM_HWS_SEQNO_ADDR,
1432 &kernel, "kernel_context");
1433 }
1434
1435 /*
1436 * engine_init_common - initialize engine state which might require hw access
1437 * @engine: Engine to initialize.
1438 *
1439 * Initializes @engine@ structure members shared between legacy and execlists
1440 * submission modes which do require hardware access.
1441 *
1442 * Typcally done at later stages of submission mode specific engine setup.
1443 *
1444 * Returns zero on success or an error code on failure.
1445 */
engine_init_common(struct intel_engine_cs * engine)1446 static int engine_init_common(struct intel_engine_cs *engine)
1447 {
1448 struct intel_context *ce, *bce = NULL;
1449 int ret;
1450
1451 engine->set_default_submission(engine);
1452
1453 /*
1454 * We may need to do things with the shrinker which
1455 * require us to immediately switch back to the default
1456 * context. This can cause a problem as pinning the
1457 * default context also requires GTT space which may not
1458 * be available. To avoid this we always pin the default
1459 * context.
1460 */
1461 ce = create_kernel_context(engine);
1462 if (IS_ERR(ce))
1463 return PTR_ERR(ce);
1464 /*
1465 * Create a separate pinned context for GGTT update with blitter engine
1466 * if a platform require such service. MI_UPDATE_GTT works on other
1467 * engines as well but BCS should be less busy engine so pick that for
1468 * GGTT updates.
1469 */
1470 if (i915_ggtt_require_binder(engine->i915) && engine->id == BCS0) {
1471 bce = create_ggtt_bind_context(engine);
1472 if (IS_ERR(bce)) {
1473 ret = PTR_ERR(bce);
1474 goto err_ce_context;
1475 }
1476 }
1477
1478 ret = measure_breadcrumb_dw(ce);
1479 if (ret < 0)
1480 goto err_bce_context;
1481
1482 engine->emit_fini_breadcrumb_dw = ret;
1483 engine->kernel_context = ce;
1484 engine->bind_context = bce;
1485
1486 return 0;
1487
1488 err_bce_context:
1489 if (bce)
1490 intel_engine_destroy_pinned_context(bce);
1491 err_ce_context:
1492 intel_engine_destroy_pinned_context(ce);
1493 return ret;
1494 }
1495
intel_engines_init(struct intel_gt * gt)1496 int intel_engines_init(struct intel_gt *gt)
1497 {
1498 int (*setup)(struct intel_engine_cs *engine);
1499 struct intel_engine_cs *engine;
1500 enum intel_engine_id id;
1501 int err;
1502
1503 if (intel_uc_uses_guc_submission(>->uc)) {
1504 gt->submission_method = INTEL_SUBMISSION_GUC;
1505 setup = intel_guc_submission_setup;
1506 } else if (HAS_EXECLISTS(gt->i915)) {
1507 gt->submission_method = INTEL_SUBMISSION_ELSP;
1508 setup = intel_execlists_submission_setup;
1509 } else {
1510 gt->submission_method = INTEL_SUBMISSION_RING;
1511 setup = intel_ring_submission_setup;
1512 }
1513
1514 for_each_engine(engine, gt, id) {
1515 err = engine_setup_common(engine);
1516 if (err)
1517 return err;
1518
1519 err = setup(engine);
1520 if (err) {
1521 intel_engine_cleanup_common(engine);
1522 return err;
1523 }
1524
1525 /* The backend should now be responsible for cleanup */
1526 GEM_BUG_ON(engine->release == NULL);
1527
1528 err = engine_init_common(engine);
1529 if (err)
1530 return err;
1531
1532 intel_engine_add_user(engine);
1533 }
1534
1535 return 0;
1536 }
1537
1538 /**
1539 * intel_engine_cleanup_common - cleans up the engine state created by
1540 * the common initiailizers.
1541 * @engine: Engine to cleanup.
1542 *
1543 * This cleans up everything created by the common helpers.
1544 */
intel_engine_cleanup_common(struct intel_engine_cs * engine)1545 void intel_engine_cleanup_common(struct intel_engine_cs *engine)
1546 {
1547 GEM_BUG_ON(!list_empty(&engine->sched_engine->requests));
1548
1549 i915_sched_engine_put(engine->sched_engine);
1550 intel_breadcrumbs_put(engine->breadcrumbs);
1551
1552 intel_engine_fini_retire(engine);
1553 intel_engine_cleanup_cmd_parser(engine);
1554
1555 if (engine->default_state)
1556 uao_detach(engine->default_state);
1557
1558 if (engine->kernel_context)
1559 intel_engine_destroy_pinned_context(engine->kernel_context);
1560
1561 if (engine->bind_context)
1562 intel_engine_destroy_pinned_context(engine->bind_context);
1563
1564
1565 GEM_BUG_ON(!llist_empty(&engine->barrier_tasks));
1566 cleanup_status_page(engine);
1567
1568 intel_wa_list_free(&engine->ctx_wa_list);
1569 intel_wa_list_free(&engine->wa_list);
1570 intel_wa_list_free(&engine->whitelist);
1571 }
1572
1573 /**
1574 * intel_engine_resume - re-initializes the HW state of the engine
1575 * @engine: Engine to resume.
1576 *
1577 * Returns zero on success or an error code on failure.
1578 */
intel_engine_resume(struct intel_engine_cs * engine)1579 int intel_engine_resume(struct intel_engine_cs *engine)
1580 {
1581 intel_engine_apply_workarounds(engine);
1582 intel_engine_apply_whitelist(engine);
1583
1584 return engine->resume(engine);
1585 }
1586
intel_engine_get_active_head(const struct intel_engine_cs * engine)1587 u64 intel_engine_get_active_head(const struct intel_engine_cs *engine)
1588 {
1589 struct drm_i915_private *i915 = engine->i915;
1590
1591 u64 acthd;
1592
1593 if (GRAPHICS_VER(i915) >= 8)
1594 acthd = ENGINE_READ64(engine, RING_ACTHD, RING_ACTHD_UDW);
1595 else if (GRAPHICS_VER(i915) >= 4)
1596 acthd = ENGINE_READ(engine, RING_ACTHD);
1597 else
1598 acthd = ENGINE_READ(engine, ACTHD);
1599
1600 return acthd;
1601 }
1602
intel_engine_get_last_batch_head(const struct intel_engine_cs * engine)1603 u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine)
1604 {
1605 u64 bbaddr;
1606
1607 if (GRAPHICS_VER(engine->i915) >= 8)
1608 bbaddr = ENGINE_READ64(engine, RING_BBADDR, RING_BBADDR_UDW);
1609 else
1610 bbaddr = ENGINE_READ(engine, RING_BBADDR);
1611
1612 return bbaddr;
1613 }
1614
stop_timeout(const struct intel_engine_cs * engine)1615 static unsigned long stop_timeout(const struct intel_engine_cs *engine)
1616 {
1617 if (in_atomic() || irqs_disabled()) /* inside atomic preempt-reset? */
1618 return 0;
1619
1620 /*
1621 * If we are doing a normal GPU reset, we can take our time and allow
1622 * the engine to quiesce. We've stopped submission to the engine, and
1623 * if we wait long enough an innocent context should complete and
1624 * leave the engine idle. So they should not be caught unaware by
1625 * the forthcoming GPU reset (which usually follows the stop_cs)!
1626 */
1627 return READ_ONCE(engine->props.stop_timeout_ms);
1628 }
1629
__intel_engine_stop_cs(struct intel_engine_cs * engine,int fast_timeout_us,int slow_timeout_ms)1630 static int __intel_engine_stop_cs(struct intel_engine_cs *engine,
1631 int fast_timeout_us,
1632 int slow_timeout_ms)
1633 {
1634 struct intel_uncore *uncore = engine->uncore;
1635 const i915_reg_t mode = RING_MI_MODE(engine->mmio_base);
1636 int err;
1637
1638 intel_uncore_write_fw(uncore, mode, _MASKED_BIT_ENABLE(STOP_RING));
1639
1640 /*
1641 * Wa_22011802037: Prior to doing a reset, ensure CS is
1642 * stopped, set ring stop bit and prefetch disable bit to halt CS
1643 */
1644 if (intel_engine_reset_needs_wa_22011802037(engine->gt))
1645 intel_uncore_write_fw(uncore, RING_MODE_GEN7(engine->mmio_base),
1646 _MASKED_BIT_ENABLE(GEN12_GFX_PREFETCH_DISABLE));
1647
1648 err = __intel_wait_for_register_fw(engine->uncore, mode,
1649 MODE_IDLE, MODE_IDLE,
1650 fast_timeout_us,
1651 slow_timeout_ms,
1652 NULL);
1653
1654 /* A final mmio read to let GPU writes be hopefully flushed to memory */
1655 intel_uncore_posting_read_fw(uncore, mode);
1656 return err;
1657 }
1658
intel_engine_stop_cs(struct intel_engine_cs * engine)1659 int intel_engine_stop_cs(struct intel_engine_cs *engine)
1660 {
1661 int err = 0;
1662
1663 if (GRAPHICS_VER(engine->i915) < 3)
1664 return -ENODEV;
1665
1666 ENGINE_TRACE(engine, "\n");
1667 /*
1668 * TODO: Find out why occasionally stopping the CS times out. Seen
1669 * especially with gem_eio tests.
1670 *
1671 * Occasionally trying to stop the cs times out, but does not adversely
1672 * affect functionality. The timeout is set as a config parameter that
1673 * defaults to 100ms. In most cases the follow up operation is to wait
1674 * for pending MI_FORCE_WAKES. The assumption is that this timeout is
1675 * sufficient for any pending MI_FORCEWAKEs to complete. Once root
1676 * caused, the caller must check and handle the return from this
1677 * function.
1678 */
1679 if (__intel_engine_stop_cs(engine, 1000, stop_timeout(engine))) {
1680 ENGINE_TRACE(engine,
1681 "timed out on STOP_RING -> IDLE; HEAD:%04x, TAIL:%04x\n",
1682 ENGINE_READ_FW(engine, RING_HEAD) & HEAD_ADDR,
1683 ENGINE_READ_FW(engine, RING_TAIL) & TAIL_ADDR);
1684
1685 /*
1686 * Sometimes we observe that the idle flag is not
1687 * set even though the ring is empty. So double
1688 * check before giving up.
1689 */
1690 if ((ENGINE_READ_FW(engine, RING_HEAD) & HEAD_ADDR) !=
1691 (ENGINE_READ_FW(engine, RING_TAIL) & TAIL_ADDR))
1692 err = -ETIMEDOUT;
1693 }
1694
1695 return err;
1696 }
1697
intel_engine_cancel_stop_cs(struct intel_engine_cs * engine)1698 void intel_engine_cancel_stop_cs(struct intel_engine_cs *engine)
1699 {
1700 ENGINE_TRACE(engine, "\n");
1701
1702 ENGINE_WRITE_FW(engine, RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING));
1703 }
1704
__cs_pending_mi_force_wakes(struct intel_engine_cs * engine)1705 static u32 __cs_pending_mi_force_wakes(struct intel_engine_cs *engine)
1706 {
1707 static const i915_reg_t _reg[I915_NUM_ENGINES] = {
1708 [RCS0] = MSG_IDLE_CS,
1709 [BCS0] = MSG_IDLE_BCS,
1710 [VCS0] = MSG_IDLE_VCS0,
1711 [VCS1] = MSG_IDLE_VCS1,
1712 [VCS2] = MSG_IDLE_VCS2,
1713 [VCS3] = MSG_IDLE_VCS3,
1714 [VCS4] = MSG_IDLE_VCS4,
1715 [VCS5] = MSG_IDLE_VCS5,
1716 [VCS6] = MSG_IDLE_VCS6,
1717 [VCS7] = MSG_IDLE_VCS7,
1718 [VECS0] = MSG_IDLE_VECS0,
1719 [VECS1] = MSG_IDLE_VECS1,
1720 [VECS2] = MSG_IDLE_VECS2,
1721 [VECS3] = MSG_IDLE_VECS3,
1722 [CCS0] = MSG_IDLE_CS,
1723 [CCS1] = MSG_IDLE_CS,
1724 [CCS2] = MSG_IDLE_CS,
1725 [CCS3] = MSG_IDLE_CS,
1726 };
1727 u32 val;
1728
1729 if (!_reg[engine->id].reg)
1730 return 0;
1731
1732 val = intel_uncore_read(engine->uncore, _reg[engine->id]);
1733
1734 /* bits[29:25] & bits[13:9] >> shift */
1735 return (val & (val >> 16) & MSG_IDLE_FW_MASK) >> MSG_IDLE_FW_SHIFT;
1736 }
1737
__gpm_wait_for_fw_complete(struct intel_gt * gt,u32 fw_mask)1738 static void __gpm_wait_for_fw_complete(struct intel_gt *gt, u32 fw_mask)
1739 {
1740 int ret;
1741
1742 /* Ensure GPM receives fw up/down after CS is stopped */
1743 udelay(1);
1744
1745 /* Wait for forcewake request to complete in GPM */
1746 ret = __intel_wait_for_register_fw(gt->uncore,
1747 GEN9_PWRGT_DOMAIN_STATUS,
1748 fw_mask, fw_mask, 5000, 0, NULL);
1749
1750 /* Ensure CS receives fw ack from GPM */
1751 udelay(1);
1752
1753 if (ret)
1754 GT_TRACE(gt, "Failed to complete pending forcewake %d\n", ret);
1755 }
1756
1757 /*
1758 * Wa_22011802037:gen12: In addition to stopping the cs, we need to wait for any
1759 * pending MI_FORCE_WAKEUP requests that the CS has initiated to complete. The
1760 * pending status is indicated by bits[13:9] (masked by bits[29:25]) in the
1761 * MSG_IDLE register. There's one MSG_IDLE register per reset domain. Since we
1762 * are concerned only with the gt reset here, we use a logical OR of pending
1763 * forcewakeups from all reset domains and then wait for them to complete by
1764 * querying PWRGT_DOMAIN_STATUS.
1765 */
intel_engine_wait_for_pending_mi_fw(struct intel_engine_cs * engine)1766 void intel_engine_wait_for_pending_mi_fw(struct intel_engine_cs *engine)
1767 {
1768 u32 fw_pending = __cs_pending_mi_force_wakes(engine);
1769
1770 if (fw_pending)
1771 __gpm_wait_for_fw_complete(engine->gt, fw_pending);
1772 }
1773
1774 /* NB: please notice the memset */
intel_engine_get_instdone(const struct intel_engine_cs * engine,struct intel_instdone * instdone)1775 void intel_engine_get_instdone(const struct intel_engine_cs *engine,
1776 struct intel_instdone *instdone)
1777 {
1778 struct drm_i915_private *i915 = engine->i915;
1779 struct intel_uncore *uncore = engine->uncore;
1780 u32 mmio_base = engine->mmio_base;
1781 int slice;
1782 int subslice;
1783 int iter;
1784
1785 memset(instdone, 0, sizeof(*instdone));
1786
1787 if (GRAPHICS_VER(i915) >= 8) {
1788 instdone->instdone =
1789 intel_uncore_read(uncore, RING_INSTDONE(mmio_base));
1790
1791 if (engine->id != RCS0)
1792 return;
1793
1794 instdone->slice_common =
1795 intel_uncore_read(uncore, GEN7_SC_INSTDONE);
1796 if (GRAPHICS_VER(i915) >= 12) {
1797 instdone->slice_common_extra[0] =
1798 intel_uncore_read(uncore, GEN12_SC_INSTDONE_EXTRA);
1799 instdone->slice_common_extra[1] =
1800 intel_uncore_read(uncore, GEN12_SC_INSTDONE_EXTRA2);
1801 }
1802
1803 for_each_ss_steering(iter, engine->gt, slice, subslice) {
1804 instdone->sampler[slice][subslice] =
1805 intel_gt_mcr_read(engine->gt,
1806 GEN8_SAMPLER_INSTDONE,
1807 slice, subslice);
1808 instdone->row[slice][subslice] =
1809 intel_gt_mcr_read(engine->gt,
1810 GEN8_ROW_INSTDONE,
1811 slice, subslice);
1812 }
1813
1814 if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 55)) {
1815 for_each_ss_steering(iter, engine->gt, slice, subslice)
1816 instdone->geom_svg[slice][subslice] =
1817 intel_gt_mcr_read(engine->gt,
1818 XEHPG_INSTDONE_GEOM_SVG,
1819 slice, subslice);
1820 }
1821 } else if (GRAPHICS_VER(i915) >= 7) {
1822 instdone->instdone =
1823 intel_uncore_read(uncore, RING_INSTDONE(mmio_base));
1824
1825 if (engine->id != RCS0)
1826 return;
1827
1828 instdone->slice_common =
1829 intel_uncore_read(uncore, GEN7_SC_INSTDONE);
1830 instdone->sampler[0][0] =
1831 intel_uncore_read(uncore, GEN7_SAMPLER_INSTDONE);
1832 instdone->row[0][0] =
1833 intel_uncore_read(uncore, GEN7_ROW_INSTDONE);
1834 } else if (GRAPHICS_VER(i915) >= 4) {
1835 instdone->instdone =
1836 intel_uncore_read(uncore, RING_INSTDONE(mmio_base));
1837 if (engine->id == RCS0)
1838 /* HACK: Using the wrong struct member */
1839 instdone->slice_common =
1840 intel_uncore_read(uncore, GEN4_INSTDONE1);
1841 } else {
1842 instdone->instdone = intel_uncore_read(uncore, GEN2_INSTDONE);
1843 }
1844 }
1845
ring_is_idle(struct intel_engine_cs * engine)1846 static bool ring_is_idle(struct intel_engine_cs *engine)
1847 {
1848 bool idle = true;
1849
1850 if (I915_SELFTEST_ONLY(!engine->mmio_base))
1851 return true;
1852
1853 if (!intel_engine_pm_get_if_awake(engine))
1854 return true;
1855
1856 /* First check that no commands are left in the ring */
1857 if ((ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR) !=
1858 (ENGINE_READ(engine, RING_TAIL) & TAIL_ADDR))
1859 idle = false;
1860
1861 /* No bit for gen2, so assume the CS parser is idle */
1862 if (GRAPHICS_VER(engine->i915) > 2 &&
1863 !(ENGINE_READ(engine, RING_MI_MODE) & MODE_IDLE))
1864 idle = false;
1865
1866 intel_engine_pm_put(engine);
1867
1868 return idle;
1869 }
1870
__intel_engine_flush_submission(struct intel_engine_cs * engine,bool sync)1871 void __intel_engine_flush_submission(struct intel_engine_cs *engine, bool sync)
1872 {
1873 struct tasklet_struct *t = &engine->sched_engine->tasklet;
1874
1875 if (!t->callback)
1876 return;
1877
1878 local_bh_disable();
1879 if (tasklet_trylock(t)) {
1880 /* Must wait for any GPU reset in progress. */
1881 if (__tasklet_is_enabled(t))
1882 t->callback(t);
1883 tasklet_unlock(t);
1884 }
1885 local_bh_enable();
1886
1887 /* Synchronise and wait for the tasklet on another CPU */
1888 if (sync)
1889 tasklet_unlock_wait(t);
1890 }
1891
1892 /**
1893 * intel_engine_is_idle() - Report if the engine has finished process all work
1894 * @engine: the intel_engine_cs
1895 *
1896 * Return true if there are no requests pending, nothing left to be submitted
1897 * to hardware, and that the engine is idle.
1898 */
intel_engine_is_idle(struct intel_engine_cs * engine)1899 bool intel_engine_is_idle(struct intel_engine_cs *engine)
1900 {
1901 /* More white lies, if wedged, hw state is inconsistent */
1902 if (intel_gt_is_wedged(engine->gt))
1903 return true;
1904
1905 if (!intel_engine_pm_is_awake(engine))
1906 return true;
1907
1908 /* Waiting to drain ELSP? */
1909 intel_synchronize_hardirq(engine->i915);
1910 intel_engine_flush_submission(engine);
1911
1912 /* ELSP is empty, but there are ready requests? E.g. after reset */
1913 if (!i915_sched_engine_is_empty(engine->sched_engine))
1914 return false;
1915
1916 /* Ring stopped? */
1917 return ring_is_idle(engine);
1918 }
1919
intel_engines_are_idle(struct intel_gt * gt)1920 bool intel_engines_are_idle(struct intel_gt *gt)
1921 {
1922 struct intel_engine_cs *engine;
1923 enum intel_engine_id id;
1924
1925 /*
1926 * If the driver is wedged, HW state may be very inconsistent and
1927 * report that it is still busy, even though we have stopped using it.
1928 */
1929 if (intel_gt_is_wedged(gt))
1930 return true;
1931
1932 /* Already parked (and passed an idleness test); must still be idle */
1933 if (!READ_ONCE(gt->awake))
1934 return true;
1935
1936 for_each_engine(engine, gt, id) {
1937 if (!intel_engine_is_idle(engine))
1938 return false;
1939 }
1940
1941 return true;
1942 }
1943
intel_engine_irq_enable(struct intel_engine_cs * engine)1944 bool intel_engine_irq_enable(struct intel_engine_cs *engine)
1945 {
1946 if (!engine->irq_enable)
1947 return false;
1948
1949 /* Caller disables interrupts */
1950 spin_lock(engine->gt->irq_lock);
1951 engine->irq_enable(engine);
1952 spin_unlock(engine->gt->irq_lock);
1953
1954 return true;
1955 }
1956
intel_engine_irq_disable(struct intel_engine_cs * engine)1957 void intel_engine_irq_disable(struct intel_engine_cs *engine)
1958 {
1959 if (!engine->irq_disable)
1960 return;
1961
1962 /* Caller disables interrupts */
1963 spin_lock(engine->gt->irq_lock);
1964 engine->irq_disable(engine);
1965 spin_unlock(engine->gt->irq_lock);
1966 }
1967
intel_engines_reset_default_submission(struct intel_gt * gt)1968 void intel_engines_reset_default_submission(struct intel_gt *gt)
1969 {
1970 struct intel_engine_cs *engine;
1971 enum intel_engine_id id;
1972
1973 for_each_engine(engine, gt, id) {
1974 if (engine->sanitize)
1975 engine->sanitize(engine);
1976
1977 engine->set_default_submission(engine);
1978 }
1979 }
1980
intel_engine_can_store_dword(struct intel_engine_cs * engine)1981 bool intel_engine_can_store_dword(struct intel_engine_cs *engine)
1982 {
1983 switch (GRAPHICS_VER(engine->i915)) {
1984 case 2:
1985 return false; /* uses physical not virtual addresses */
1986 case 3:
1987 /* maybe only uses physical not virtual addresses */
1988 return !(IS_I915G(engine->i915) || IS_I915GM(engine->i915));
1989 case 4:
1990 return !IS_I965G(engine->i915); /* who knows! */
1991 case 6:
1992 return engine->class != VIDEO_DECODE_CLASS; /* b0rked */
1993 default:
1994 return true;
1995 }
1996 }
1997
get_timeline(struct i915_request * rq)1998 static struct intel_timeline *get_timeline(struct i915_request *rq)
1999 {
2000 struct intel_timeline *tl;
2001
2002 /*
2003 * Even though we are holding the engine->sched_engine->lock here, there
2004 * is no control over the submission queue per-se and we are
2005 * inspecting the active state at a random point in time, with an
2006 * unknown queue. Play safe and make sure the timeline remains valid.
2007 * (Only being used for pretty printing, one extra kref shouldn't
2008 * cause a camel stampede!)
2009 */
2010 rcu_read_lock();
2011 tl = rcu_dereference(rq->timeline);
2012 if (!kref_get_unless_zero(&tl->kref))
2013 tl = NULL;
2014 rcu_read_unlock();
2015
2016 return tl;
2017 }
2018
print_ring(char * buf,int sz,struct i915_request * rq)2019 static int print_ring(char *buf, int sz, struct i915_request *rq)
2020 {
2021 int len = 0;
2022
2023 if (!i915_request_signaled(rq)) {
2024 struct intel_timeline *tl = get_timeline(rq);
2025
2026 len = scnprintf(buf, sz,
2027 "ring:{start:%08x, hwsp:%08x, seqno:%08x, runtime:%llums}, ",
2028 i915_ggtt_offset(rq->ring->vma),
2029 tl ? tl->hwsp_offset : 0,
2030 hwsp_seqno(rq),
2031 DIV_ROUND_CLOSEST_ULL(intel_context_get_total_runtime_ns(rq->context),
2032 1000 * 1000));
2033
2034 if (tl)
2035 intel_timeline_put(tl);
2036 }
2037
2038 return len;
2039 }
2040
hexdump(struct drm_printer * m,const void * buf,size_t len)2041 static void hexdump(struct drm_printer *m, const void *buf, size_t len)
2042 {
2043 STUB();
2044 #ifdef notyet
2045 const size_t rowsize = 8 * sizeof(u32);
2046 const void *prev = NULL;
2047 bool skip = false;
2048 size_t pos;
2049
2050 for (pos = 0; pos < len; pos += rowsize) {
2051 char line[128];
2052
2053 if (prev && !memcmp(prev, buf + pos, rowsize)) {
2054 if (!skip) {
2055 drm_printf(m, "*\n");
2056 skip = true;
2057 }
2058 continue;
2059 }
2060
2061 WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
2062 rowsize, sizeof(u32),
2063 line, sizeof(line),
2064 false) >= sizeof(line));
2065 drm_printf(m, "[%04zx] %s\n", pos, line);
2066
2067 prev = buf + pos;
2068 skip = false;
2069 }
2070 #endif
2071 }
2072
repr_timer(const struct timeout * t)2073 static const char *repr_timer(const struct timeout *t)
2074 {
2075 if (!READ_ONCE(t->to_time))
2076 return "inactive";
2077
2078 if (timer_pending(t))
2079 return "active";
2080
2081 return "expired";
2082 }
2083
intel_engine_print_registers(struct intel_engine_cs * engine,struct drm_printer * m)2084 static void intel_engine_print_registers(struct intel_engine_cs *engine,
2085 struct drm_printer *m)
2086 {
2087 struct drm_i915_private *i915 = engine->i915;
2088 struct intel_engine_execlists * const execlists = &engine->execlists;
2089 u64 addr;
2090
2091 if (engine->id == RENDER_CLASS && IS_GRAPHICS_VER(i915, 4, 7))
2092 drm_printf(m, "\tCCID: 0x%08x\n", ENGINE_READ(engine, CCID));
2093 if (HAS_EXECLISTS(i915)) {
2094 drm_printf(m, "\tEL_STAT_HI: 0x%08x\n",
2095 ENGINE_READ(engine, RING_EXECLIST_STATUS_HI));
2096 drm_printf(m, "\tEL_STAT_LO: 0x%08x\n",
2097 ENGINE_READ(engine, RING_EXECLIST_STATUS_LO));
2098 }
2099 drm_printf(m, "\tRING_START: 0x%08x\n",
2100 ENGINE_READ(engine, RING_START));
2101 drm_printf(m, "\tRING_HEAD: 0x%08x\n",
2102 ENGINE_READ(engine, RING_HEAD) & HEAD_ADDR);
2103 drm_printf(m, "\tRING_TAIL: 0x%08x\n",
2104 ENGINE_READ(engine, RING_TAIL) & TAIL_ADDR);
2105 drm_printf(m, "\tRING_CTL: 0x%08x%s\n",
2106 ENGINE_READ(engine, RING_CTL),
2107 ENGINE_READ(engine, RING_CTL) & (RING_WAIT | RING_WAIT_SEMAPHORE) ? " [waiting]" : "");
2108 if (GRAPHICS_VER(engine->i915) > 2) {
2109 drm_printf(m, "\tRING_MODE: 0x%08x%s\n",
2110 ENGINE_READ(engine, RING_MI_MODE),
2111 ENGINE_READ(engine, RING_MI_MODE) & (MODE_IDLE) ? " [idle]" : "");
2112 }
2113
2114 if (GRAPHICS_VER(i915) >= 6) {
2115 drm_printf(m, "\tRING_IMR: 0x%08x\n",
2116 ENGINE_READ(engine, RING_IMR));
2117 drm_printf(m, "\tRING_ESR: 0x%08x\n",
2118 ENGINE_READ(engine, RING_ESR));
2119 drm_printf(m, "\tRING_EMR: 0x%08x\n",
2120 ENGINE_READ(engine, RING_EMR));
2121 drm_printf(m, "\tRING_EIR: 0x%08x\n",
2122 ENGINE_READ(engine, RING_EIR));
2123 }
2124
2125 addr = intel_engine_get_active_head(engine);
2126 drm_printf(m, "\tACTHD: 0x%08x_%08x\n",
2127 upper_32_bits(addr), lower_32_bits(addr));
2128 addr = intel_engine_get_last_batch_head(engine);
2129 drm_printf(m, "\tBBADDR: 0x%08x_%08x\n",
2130 upper_32_bits(addr), lower_32_bits(addr));
2131 if (GRAPHICS_VER(i915) >= 8)
2132 addr = ENGINE_READ64(engine, RING_DMA_FADD, RING_DMA_FADD_UDW);
2133 else if (GRAPHICS_VER(i915) >= 4)
2134 addr = ENGINE_READ(engine, RING_DMA_FADD);
2135 else
2136 addr = ENGINE_READ(engine, DMA_FADD_I8XX);
2137 drm_printf(m, "\tDMA_FADDR: 0x%08x_%08x\n",
2138 upper_32_bits(addr), lower_32_bits(addr));
2139 if (GRAPHICS_VER(i915) >= 4) {
2140 drm_printf(m, "\tIPEIR: 0x%08x\n",
2141 ENGINE_READ(engine, RING_IPEIR));
2142 drm_printf(m, "\tIPEHR: 0x%08x\n",
2143 ENGINE_READ(engine, RING_IPEHR));
2144 } else {
2145 drm_printf(m, "\tIPEIR: 0x%08x\n", ENGINE_READ(engine, IPEIR));
2146 drm_printf(m, "\tIPEHR: 0x%08x\n", ENGINE_READ(engine, IPEHR));
2147 }
2148
2149 if (HAS_EXECLISTS(i915) && !intel_engine_uses_guc(engine)) {
2150 struct i915_request * const *port, *rq;
2151 const u32 *hws =
2152 &engine->status_page.addr[I915_HWS_CSB_BUF0_INDEX];
2153 const u8 num_entries = execlists->csb_size;
2154 unsigned int idx;
2155 u8 read, write;
2156
2157 drm_printf(m, "\tExeclist tasklet queued? %s (%s), preempt? %s, timeslice? %s\n",
2158 str_yes_no(test_bit(TASKLET_STATE_SCHED, &engine->sched_engine->tasklet.state)),
2159 str_enabled_disabled(!atomic_read(&engine->sched_engine->tasklet.count)),
2160 repr_timer(&engine->execlists.preempt),
2161 repr_timer(&engine->execlists.timer));
2162
2163 read = execlists->csb_head;
2164 write = READ_ONCE(*execlists->csb_write);
2165
2166 drm_printf(m, "\tExeclist status: 0x%08x %08x; CSB read:%d, write:%d, entries:%d\n",
2167 ENGINE_READ(engine, RING_EXECLIST_STATUS_LO),
2168 ENGINE_READ(engine, RING_EXECLIST_STATUS_HI),
2169 read, write, num_entries);
2170
2171 if (read >= num_entries)
2172 read = 0;
2173 if (write >= num_entries)
2174 write = 0;
2175 if (read > write)
2176 write += num_entries;
2177 while (read < write) {
2178 idx = ++read % num_entries;
2179 drm_printf(m, "\tExeclist CSB[%d]: 0x%08x, context: %d\n",
2180 idx, hws[idx * 2], hws[idx * 2 + 1]);
2181 }
2182
2183 i915_sched_engine_active_lock_bh(engine->sched_engine);
2184 rcu_read_lock();
2185 for (port = execlists->active; (rq = *port); port++) {
2186 char hdr[160];
2187 int len;
2188
2189 len = scnprintf(hdr, sizeof(hdr),
2190 "\t\tActive[%d]: ccid:%08x%s%s, ",
2191 (int)(port - execlists->active),
2192 rq->context->lrc.ccid,
2193 intel_context_is_closed(rq->context) ? "!" : "",
2194 intel_context_is_banned(rq->context) ? "*" : "");
2195 len += print_ring(hdr + len, sizeof(hdr) - len, rq);
2196 scnprintf(hdr + len, sizeof(hdr) - len, "rq: ");
2197 i915_request_show(m, rq, hdr, 0);
2198 }
2199 for (port = execlists->pending; (rq = *port); port++) {
2200 char hdr[160];
2201 int len;
2202
2203 len = scnprintf(hdr, sizeof(hdr),
2204 "\t\tPending[%d]: ccid:%08x%s%s, ",
2205 (int)(port - execlists->pending),
2206 rq->context->lrc.ccid,
2207 intel_context_is_closed(rq->context) ? "!" : "",
2208 intel_context_is_banned(rq->context) ? "*" : "");
2209 len += print_ring(hdr + len, sizeof(hdr) - len, rq);
2210 scnprintf(hdr + len, sizeof(hdr) - len, "rq: ");
2211 i915_request_show(m, rq, hdr, 0);
2212 }
2213 rcu_read_unlock();
2214 i915_sched_engine_active_unlock_bh(engine->sched_engine);
2215 } else if (GRAPHICS_VER(i915) > 6) {
2216 drm_printf(m, "\tPP_DIR_BASE: 0x%08x\n",
2217 ENGINE_READ(engine, RING_PP_DIR_BASE));
2218 drm_printf(m, "\tPP_DIR_BASE_READ: 0x%08x\n",
2219 ENGINE_READ(engine, RING_PP_DIR_BASE_READ));
2220 drm_printf(m, "\tPP_DIR_DCLV: 0x%08x\n",
2221 ENGINE_READ(engine, RING_PP_DIR_DCLV));
2222 }
2223 }
2224
print_request_ring(struct drm_printer * m,struct i915_request * rq)2225 static void print_request_ring(struct drm_printer *m, struct i915_request *rq)
2226 {
2227 struct i915_vma_resource *vma_res = rq->batch_res;
2228 void *ring;
2229 int size;
2230
2231 drm_printf(m,
2232 "[head %04x, postfix %04x, tail %04x, batch 0x%08x_%08x]:\n",
2233 rq->head, rq->postfix, rq->tail,
2234 vma_res ? upper_32_bits(vma_res->start) : ~0u,
2235 vma_res ? lower_32_bits(vma_res->start) : ~0u);
2236
2237 size = rq->tail - rq->head;
2238 if (rq->tail < rq->head)
2239 size += rq->ring->size;
2240
2241 ring = kmalloc(size, GFP_ATOMIC);
2242 if (ring) {
2243 const void *vaddr = rq->ring->vaddr;
2244 unsigned int head = rq->head;
2245 unsigned int len = 0;
2246
2247 if (rq->tail < head) {
2248 len = rq->ring->size - head;
2249 memcpy(ring, vaddr + head, len);
2250 head = 0;
2251 }
2252 memcpy(ring + len, vaddr + head, size - len);
2253
2254 hexdump(m, ring, size);
2255 kfree(ring);
2256 }
2257 }
2258
read_ul(void * p,size_t x)2259 static unsigned long read_ul(void *p, size_t x)
2260 {
2261 return *(unsigned long *)(p + x);
2262 }
2263
print_properties(struct intel_engine_cs * engine,struct drm_printer * m)2264 static void print_properties(struct intel_engine_cs *engine,
2265 struct drm_printer *m)
2266 {
2267 static const struct pmap {
2268 size_t offset;
2269 const char *name;
2270 } props[] = {
2271 #define P(x) { \
2272 .offset = offsetof(typeof(engine->props), x), \
2273 .name = #x \
2274 }
2275 P(heartbeat_interval_ms),
2276 P(max_busywait_duration_ns),
2277 P(preempt_timeout_ms),
2278 P(stop_timeout_ms),
2279 P(timeslice_duration_ms),
2280
2281 {},
2282 #undef P
2283 };
2284 const struct pmap *p;
2285
2286 drm_printf(m, "\tProperties:\n");
2287 for (p = props; p->name; p++)
2288 drm_printf(m, "\t\t%s: %lu [default %lu]\n",
2289 p->name,
2290 read_ul(&engine->props, p->offset),
2291 read_ul(&engine->defaults, p->offset));
2292 }
2293
engine_dump_request(struct i915_request * rq,struct drm_printer * m,const char * msg)2294 static void engine_dump_request(struct i915_request *rq, struct drm_printer *m, const char *msg)
2295 {
2296 struct intel_timeline *tl = get_timeline(rq);
2297
2298 i915_request_show(m, rq, msg, 0);
2299
2300 drm_printf(m, "\t\tring->start: 0x%08x\n",
2301 i915_ggtt_offset(rq->ring->vma));
2302 drm_printf(m, "\t\tring->head: 0x%08x\n",
2303 rq->ring->head);
2304 drm_printf(m, "\t\tring->tail: 0x%08x\n",
2305 rq->ring->tail);
2306 drm_printf(m, "\t\tring->emit: 0x%08x\n",
2307 rq->ring->emit);
2308 drm_printf(m, "\t\tring->space: 0x%08x\n",
2309 rq->ring->space);
2310
2311 if (tl) {
2312 drm_printf(m, "\t\tring->hwsp: 0x%08x\n",
2313 tl->hwsp_offset);
2314 intel_timeline_put(tl);
2315 }
2316
2317 print_request_ring(m, rq);
2318
2319 if (rq->context->lrc_reg_state) {
2320 drm_printf(m, "Logical Ring Context:\n");
2321 hexdump(m, rq->context->lrc_reg_state, PAGE_SIZE);
2322 }
2323 }
2324
intel_engine_dump_active_requests(struct list_head * requests,struct i915_request * hung_rq,struct drm_printer * m)2325 void intel_engine_dump_active_requests(struct list_head *requests,
2326 struct i915_request *hung_rq,
2327 struct drm_printer *m)
2328 {
2329 struct i915_request *rq;
2330 const char *msg;
2331 enum i915_request_state state;
2332
2333 list_for_each_entry(rq, requests, sched.link) {
2334 if (rq == hung_rq)
2335 continue;
2336
2337 state = i915_test_request_state(rq);
2338 if (state < I915_REQUEST_QUEUED)
2339 continue;
2340
2341 if (state == I915_REQUEST_ACTIVE)
2342 msg = "\t\tactive on engine";
2343 else
2344 msg = "\t\tactive in queue";
2345
2346 engine_dump_request(rq, m, msg);
2347 }
2348 }
2349
engine_dump_active_requests(struct intel_engine_cs * engine,struct drm_printer * m)2350 static void engine_dump_active_requests(struct intel_engine_cs *engine,
2351 struct drm_printer *m)
2352 {
2353 struct intel_context *hung_ce = NULL;
2354 struct i915_request *hung_rq = NULL;
2355
2356 /*
2357 * No need for an engine->irq_seqno_barrier() before the seqno reads.
2358 * The GPU is still running so requests are still executing and any
2359 * hardware reads will be out of date by the time they are reported.
2360 * But the intention here is just to report an instantaneous snapshot
2361 * so that's fine.
2362 */
2363 intel_engine_get_hung_entity(engine, &hung_ce, &hung_rq);
2364
2365 drm_printf(m, "\tRequests:\n");
2366
2367 if (hung_rq)
2368 engine_dump_request(hung_rq, m, "\t\thung");
2369 else if (hung_ce)
2370 drm_printf(m, "\t\tGot hung ce but no hung rq!\n");
2371
2372 if (intel_uc_uses_guc_submission(&engine->gt->uc))
2373 intel_guc_dump_active_requests(engine, hung_rq, m);
2374 else
2375 intel_execlists_dump_active_requests(engine, hung_rq, m);
2376
2377 if (hung_rq)
2378 i915_request_put(hung_rq);
2379 }
2380
intel_engine_dump(struct intel_engine_cs * engine,struct drm_printer * m,const char * header,...)2381 void intel_engine_dump(struct intel_engine_cs *engine,
2382 struct drm_printer *m,
2383 const char *header, ...)
2384 {
2385 struct i915_gpu_error * const error = &engine->i915->gpu_error;
2386 struct i915_request *rq;
2387 intel_wakeref_t wakeref;
2388 ktime_t dummy;
2389
2390 if (header) {
2391 va_list ap;
2392
2393 va_start(ap, header);
2394 drm_vprintf(m, header, &ap);
2395 va_end(ap);
2396 }
2397
2398 if (intel_gt_is_wedged(engine->gt))
2399 drm_printf(m, "*** WEDGED ***\n");
2400
2401 drm_printf(m, "\tAwake? %d\n", atomic_read(&engine->wakeref.count));
2402 drm_printf(m, "\tBarriers?: %s\n",
2403 str_yes_no(!llist_empty(&engine->barrier_tasks)));
2404 drm_printf(m, "\tLatency: %luus\n",
2405 ewma__engine_latency_read(&engine->latency));
2406 if (intel_engine_supports_stats(engine))
2407 drm_printf(m, "\tRuntime: %llums\n",
2408 ktime_to_ms(intel_engine_get_busy_time(engine,
2409 &dummy)));
2410 drm_printf(m, "\tForcewake: %x domains, %d active\n",
2411 engine->fw_domain, READ_ONCE(engine->fw_active));
2412
2413 rcu_read_lock();
2414 rq = READ_ONCE(engine->heartbeat.systole);
2415 if (rq)
2416 drm_printf(m, "\tHeartbeat: %d ms ago\n",
2417 jiffies_to_msecs(jiffies - rq->emitted_jiffies));
2418 rcu_read_unlock();
2419 drm_printf(m, "\tReset count: %d (global %d)\n",
2420 i915_reset_engine_count(error, engine),
2421 i915_reset_count(error));
2422 print_properties(engine, m);
2423
2424 engine_dump_active_requests(engine, m);
2425
2426 drm_printf(m, "\tMMIO base: 0x%08x\n", engine->mmio_base);
2427 wakeref = intel_runtime_pm_get_if_in_use(engine->uncore->rpm);
2428 if (wakeref) {
2429 intel_engine_print_registers(engine, m);
2430 intel_runtime_pm_put(engine->uncore->rpm, wakeref);
2431 } else {
2432 drm_printf(m, "\tDevice is asleep; skipping register dump\n");
2433 }
2434
2435 intel_execlists_show_requests(engine, m, i915_request_show, 8);
2436
2437 drm_printf(m, "HWSP:\n");
2438 hexdump(m, engine->status_page.addr, PAGE_SIZE);
2439
2440 drm_printf(m, "Idle? %s\n", str_yes_no(intel_engine_is_idle(engine)));
2441
2442 intel_engine_print_breadcrumbs(engine, m);
2443 }
2444
2445 /**
2446 * intel_engine_get_busy_time() - Return current accumulated engine busyness
2447 * @engine: engine to report on
2448 * @now: monotonic timestamp of sampling
2449 *
2450 * Returns accumulated time @engine was busy since engine stats were enabled.
2451 */
intel_engine_get_busy_time(struct intel_engine_cs * engine,ktime_t * now)2452 ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine, ktime_t *now)
2453 {
2454 return engine->busyness(engine, now);
2455 }
2456
2457 struct intel_context *
intel_engine_create_virtual(struct intel_engine_cs ** siblings,unsigned int count,unsigned long flags)2458 intel_engine_create_virtual(struct intel_engine_cs **siblings,
2459 unsigned int count, unsigned long flags)
2460 {
2461 if (count == 0)
2462 return ERR_PTR(-EINVAL);
2463
2464 if (count == 1 && !(flags & FORCE_VIRTUAL))
2465 return intel_context_create(siblings[0]);
2466
2467 GEM_BUG_ON(!siblings[0]->cops->create_virtual);
2468 return siblings[0]->cops->create_virtual(siblings, count, flags);
2469 }
2470
engine_execlist_find_hung_request(struct intel_engine_cs * engine)2471 static struct i915_request *engine_execlist_find_hung_request(struct intel_engine_cs *engine)
2472 {
2473 struct i915_request *request, *active = NULL;
2474
2475 /*
2476 * This search does not work in GuC submission mode. However, the GuC
2477 * will report the hanging context directly to the driver itself. So
2478 * the driver should never get here when in GuC mode.
2479 */
2480 GEM_BUG_ON(intel_uc_uses_guc_submission(&engine->gt->uc));
2481
2482 /*
2483 * We are called by the error capture, reset and to dump engine
2484 * state at random points in time. In particular, note that neither is
2485 * crucially ordered with an interrupt. After a hang, the GPU is dead
2486 * and we assume that no more writes can happen (we waited long enough
2487 * for all writes that were in transaction to be flushed) - adding an
2488 * extra delay for a recent interrupt is pointless. Hence, we do
2489 * not need an engine->irq_seqno_barrier() before the seqno reads.
2490 * At all other times, we must assume the GPU is still running, but
2491 * we only care about the snapshot of this moment.
2492 */
2493 lockdep_assert_held(&engine->sched_engine->lock);
2494
2495 rcu_read_lock();
2496 request = execlists_active(&engine->execlists);
2497 if (request) {
2498 struct intel_timeline *tl = request->context->timeline;
2499
2500 list_for_each_entry_from_reverse(request, &tl->requests, link) {
2501 if (__i915_request_is_complete(request))
2502 break;
2503
2504 active = request;
2505 }
2506 }
2507 rcu_read_unlock();
2508 if (active)
2509 return active;
2510
2511 list_for_each_entry(request, &engine->sched_engine->requests,
2512 sched.link) {
2513 if (i915_test_request_state(request) != I915_REQUEST_ACTIVE)
2514 continue;
2515
2516 active = request;
2517 break;
2518 }
2519
2520 return active;
2521 }
2522
intel_engine_get_hung_entity(struct intel_engine_cs * engine,struct intel_context ** ce,struct i915_request ** rq)2523 void intel_engine_get_hung_entity(struct intel_engine_cs *engine,
2524 struct intel_context **ce, struct i915_request **rq)
2525 {
2526 unsigned long flags;
2527
2528 *ce = intel_engine_get_hung_context(engine);
2529 if (*ce) {
2530 intel_engine_clear_hung_context(engine);
2531
2532 *rq = intel_context_get_active_request(*ce);
2533 return;
2534 }
2535
2536 /*
2537 * Getting here with GuC enabled means it is a forced error capture
2538 * with no actual hang. So, no need to attempt the execlist search.
2539 */
2540 if (intel_uc_uses_guc_submission(&engine->gt->uc))
2541 return;
2542
2543 spin_lock_irqsave(&engine->sched_engine->lock, flags);
2544 *rq = engine_execlist_find_hung_request(engine);
2545 if (*rq)
2546 *rq = i915_request_get_rcu(*rq);
2547 spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
2548 }
2549
xehp_enable_ccs_engines(struct intel_engine_cs * engine)2550 void xehp_enable_ccs_engines(struct intel_engine_cs *engine)
2551 {
2552 /*
2553 * If there are any non-fused-off CCS engines, we need to enable CCS
2554 * support in the RCU_MODE register. This only needs to be done once,
2555 * so for simplicity we'll take care of this in the RCS engine's
2556 * resume handler; since the RCS and all CCS engines belong to the
2557 * same reset domain and are reset together, this will also take care
2558 * of re-applying the setting after i915-triggered resets.
2559 */
2560 if (!CCS_MASK(engine->gt))
2561 return;
2562
2563 intel_uncore_write(engine->uncore, GEN12_RCU_MODE,
2564 _MASKED_BIT_ENABLE(GEN12_RCU_MODE_CCS_ENABLE));
2565 }
2566
2567 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2568 #include "mock_engine.c"
2569 #include "selftest_engine.c"
2570 #include "selftest_engine_cs.c"
2571 #endif
2572