1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2014 Intel Corporation
4 */
5
6 #include <linux/circ_buf.h>
7
8 #include "gem/i915_gem_context.h"
9 #include "gem/i915_gem_lmem.h"
10 #include "gt/gen8_engine_cs.h"
11 #include "gt/intel_breadcrumbs.h"
12 #include "gt/intel_context.h"
13 #include "gt/intel_engine_heartbeat.h"
14 #include "gt/intel_engine_pm.h"
15 #include "gt/intel_engine_regs.h"
16 #include "gt/intel_gpu_commands.h"
17 #include "gt/intel_gt.h"
18 #include "gt/intel_gt_clock_utils.h"
19 #include "gt/intel_gt_irq.h"
20 #include "gt/intel_gt_pm.h"
21 #include "gt/intel_gt_regs.h"
22 #include "gt/intel_gt_requests.h"
23 #include "gt/intel_lrc.h"
24 #include "gt/intel_lrc_reg.h"
25 #include "gt/intel_mocs.h"
26 #include "gt/intel_ring.h"
27
28 #include "intel_guc_ads.h"
29 #include "intel_guc_capture.h"
30 #include "intel_guc_print.h"
31 #include "intel_guc_submission.h"
32
33 #include "i915_drv.h"
34 #include "i915_reg.h"
35 #include "i915_irq.h"
36 #include "i915_trace.h"
37
38 /**
39 * DOC: GuC-based command submission
40 *
41 * The Scratch registers:
42 * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
43 * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
44 * triggers an interrupt on the GuC via another register write (0xC4C8).
45 * Firmware writes a success/fail code back to the action register after
46 * processes the request. The kernel driver polls waiting for this update and
47 * then proceeds.
48 *
49 * Command Transport buffers (CTBs):
50 * Covered in detail in other sections but CTBs (Host to GuC - H2G, GuC to Host
51 * - G2H) are a message interface between the i915 and GuC.
52 *
53 * Context registration:
54 * Before a context can be submitted it must be registered with the GuC via a
55 * H2G. A unique guc_id is associated with each context. The context is either
56 * registered at request creation time (normal operation) or at submission time
57 * (abnormal operation, e.g. after a reset).
58 *
59 * Context submission:
60 * The i915 updates the LRC tail value in memory. The i915 must enable the
61 * scheduling of the context within the GuC for the GuC to actually consider it.
62 * Therefore, the first time a disabled context is submitted we use a schedule
63 * enable H2G, while follow up submissions are done via the context submit H2G,
64 * which informs the GuC that a previously enabled context has new work
65 * available.
66 *
67 * Context unpin:
68 * To unpin a context a H2G is used to disable scheduling. When the
69 * corresponding G2H returns indicating the scheduling disable operation has
70 * completed it is safe to unpin the context. While a disable is in flight it
71 * isn't safe to resubmit the context so a fence is used to stall all future
72 * requests of that context until the G2H is returned. Because this interaction
73 * with the GuC takes a non-zero amount of time we delay the disabling of
74 * scheduling after the pin count goes to zero by a configurable period of time
75 * (see SCHED_DISABLE_DELAY_MS). The thought is this gives the user a window of
76 * time to resubmit something on the context before doing this costly operation.
77 * This delay is only done if the context isn't closed and the guc_id usage is
78 * less than a threshold (see NUM_SCHED_DISABLE_GUC_IDS_THRESHOLD).
79 *
80 * Context deregistration:
81 * Before a context can be destroyed or if we steal its guc_id we must
82 * deregister the context with the GuC via H2G. If stealing the guc_id it isn't
83 * safe to submit anything to this guc_id until the deregister completes so a
84 * fence is used to stall all requests associated with this guc_id until the
85 * corresponding G2H returns indicating the guc_id has been deregistered.
86 *
87 * submission_state.guc_ids:
88 * Unique number associated with private GuC context data passed in during
89 * context registration / submission / deregistration. 64k available. Simple ida
90 * is used for allocation.
91 *
92 * Stealing guc_ids:
93 * If no guc_ids are available they can be stolen from another context at
94 * request creation time if that context is unpinned. If a guc_id can't be found
95 * we punt this problem to the user as we believe this is near impossible to hit
96 * during normal use cases.
97 *
98 * Locking:
99 * In the GuC submission code we have 3 basic spin locks which protect
100 * everything. Details about each below.
101 *
102 * sched_engine->lock
103 * This is the submission lock for all contexts that share an i915 schedule
104 * engine (sched_engine), thus only one of the contexts which share a
105 * sched_engine can be submitting at a time. Currently only one sched_engine is
106 * used for all of GuC submission but that could change in the future.
107 *
108 * guc->submission_state.lock
109 * Global lock for GuC submission state. Protects guc_ids and destroyed contexts
110 * list.
111 *
112 * ce->guc_state.lock
113 * Protects everything under ce->guc_state. Ensures that a context is in the
114 * correct state before issuing a H2G. e.g. We don't issue a schedule disable
115 * on a disabled context (bad idea), we don't issue a schedule enable when a
116 * schedule disable is in flight, etc... Also protects list of inflight requests
117 * on the context and the priority management state. Lock is individual to each
118 * context.
119 *
120 * Lock ordering rules:
121 * sched_engine->lock -> ce->guc_state.lock
122 * guc->submission_state.lock -> ce->guc_state.lock
123 *
124 * Reset races:
125 * When a full GT reset is triggered it is assumed that some G2H responses to
126 * H2Gs can be lost as the GuC is also reset. Losing these G2H can prove to be
127 * fatal as we do certain operations upon receiving a G2H (e.g. destroy
128 * contexts, release guc_ids, etc...). When this occurs we can scrub the
129 * context state and cleanup appropriately, however this is quite racey.
130 * To avoid races, the reset code must disable submission before scrubbing for
131 * the missing G2H, while the submission code must check for submission being
132 * disabled and skip sending H2Gs and updating context states when it is. Both
133 * sides must also make sure to hold the relevant locks.
134 */
135
136 /* GuC Virtual Engine */
137 struct guc_virtual_engine {
138 struct intel_engine_cs base;
139 struct intel_context context;
140 };
141
142 static struct intel_context *
143 guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count,
144 unsigned long flags);
145
146 static struct intel_context *
147 guc_create_parallel(struct intel_engine_cs **engines,
148 unsigned int num_siblings,
149 unsigned int width);
150
151 #define GUC_REQUEST_SIZE 64 /* bytes */
152
153 /*
154 * We reserve 1/16 of the guc_ids for multi-lrc as these need to be contiguous
155 * per the GuC submission interface. A different allocation algorithm is used
156 * (bitmap vs. ida) between multi-lrc and single-lrc hence the reason to
157 * partition the guc_id space. We believe the number of multi-lrc contexts in
158 * use should be low and 1/16 should be sufficient. Minimum of 32 guc_ids for
159 * multi-lrc.
160 */
161 #define NUMBER_MULTI_LRC_GUC_ID(guc) \
162 ((guc)->submission_state.num_guc_ids / 16)
163
164 /*
165 * Below is a set of functions which control the GuC scheduling state which
166 * require a lock.
167 */
168 #define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER BIT(0)
169 #define SCHED_STATE_DESTROYED BIT(1)
170 #define SCHED_STATE_PENDING_DISABLE BIT(2)
171 #define SCHED_STATE_BANNED BIT(3)
172 #define SCHED_STATE_ENABLED BIT(4)
173 #define SCHED_STATE_PENDING_ENABLE BIT(5)
174 #define SCHED_STATE_REGISTERED BIT(6)
175 #define SCHED_STATE_POLICY_REQUIRED BIT(7)
176 #define SCHED_STATE_CLOSED BIT(8)
177 #define SCHED_STATE_BLOCKED_SHIFT 9
178 #define SCHED_STATE_BLOCKED BIT(SCHED_STATE_BLOCKED_SHIFT)
179 #define SCHED_STATE_BLOCKED_MASK (0xfff << SCHED_STATE_BLOCKED_SHIFT)
180
init_sched_state(struct intel_context * ce)181 static inline void init_sched_state(struct intel_context *ce)
182 {
183 lockdep_assert_held(&ce->guc_state.lock);
184 ce->guc_state.sched_state &= SCHED_STATE_BLOCKED_MASK;
185 }
186
187 /*
188 * Kernel contexts can have SCHED_STATE_REGISTERED after suspend.
189 * A context close can race with the submission path, so SCHED_STATE_CLOSED
190 * can be set immediately before we try to register.
191 */
192 #define SCHED_STATE_VALID_INIT \
193 (SCHED_STATE_BLOCKED_MASK | \
194 SCHED_STATE_CLOSED | \
195 SCHED_STATE_REGISTERED)
196
197 __maybe_unused
sched_state_is_init(struct intel_context * ce)198 static bool sched_state_is_init(struct intel_context *ce)
199 {
200 return !(ce->guc_state.sched_state & ~SCHED_STATE_VALID_INIT);
201 }
202
203 static inline bool
context_wait_for_deregister_to_register(struct intel_context * ce)204 context_wait_for_deregister_to_register(struct intel_context *ce)
205 {
206 return ce->guc_state.sched_state &
207 SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
208 }
209
210 static inline void
set_context_wait_for_deregister_to_register(struct intel_context * ce)211 set_context_wait_for_deregister_to_register(struct intel_context *ce)
212 {
213 lockdep_assert_held(&ce->guc_state.lock);
214 ce->guc_state.sched_state |=
215 SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
216 }
217
218 static inline void
clr_context_wait_for_deregister_to_register(struct intel_context * ce)219 clr_context_wait_for_deregister_to_register(struct intel_context *ce)
220 {
221 lockdep_assert_held(&ce->guc_state.lock);
222 ce->guc_state.sched_state &=
223 ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
224 }
225
226 static inline bool
context_destroyed(struct intel_context * ce)227 context_destroyed(struct intel_context *ce)
228 {
229 return ce->guc_state.sched_state & SCHED_STATE_DESTROYED;
230 }
231
232 static inline void
set_context_destroyed(struct intel_context * ce)233 set_context_destroyed(struct intel_context *ce)
234 {
235 lockdep_assert_held(&ce->guc_state.lock);
236 ce->guc_state.sched_state |= SCHED_STATE_DESTROYED;
237 }
238
239 static inline void
clr_context_destroyed(struct intel_context * ce)240 clr_context_destroyed(struct intel_context *ce)
241 {
242 lockdep_assert_held(&ce->guc_state.lock);
243 ce->guc_state.sched_state &= ~SCHED_STATE_DESTROYED;
244 }
245
context_pending_disable(struct intel_context * ce)246 static inline bool context_pending_disable(struct intel_context *ce)
247 {
248 return ce->guc_state.sched_state & SCHED_STATE_PENDING_DISABLE;
249 }
250
set_context_pending_disable(struct intel_context * ce)251 static inline void set_context_pending_disable(struct intel_context *ce)
252 {
253 lockdep_assert_held(&ce->guc_state.lock);
254 ce->guc_state.sched_state |= SCHED_STATE_PENDING_DISABLE;
255 }
256
clr_context_pending_disable(struct intel_context * ce)257 static inline void clr_context_pending_disable(struct intel_context *ce)
258 {
259 lockdep_assert_held(&ce->guc_state.lock);
260 ce->guc_state.sched_state &= ~SCHED_STATE_PENDING_DISABLE;
261 }
262
context_banned(struct intel_context * ce)263 static inline bool context_banned(struct intel_context *ce)
264 {
265 return ce->guc_state.sched_state & SCHED_STATE_BANNED;
266 }
267
set_context_banned(struct intel_context * ce)268 static inline void set_context_banned(struct intel_context *ce)
269 {
270 lockdep_assert_held(&ce->guc_state.lock);
271 ce->guc_state.sched_state |= SCHED_STATE_BANNED;
272 }
273
clr_context_banned(struct intel_context * ce)274 static inline void clr_context_banned(struct intel_context *ce)
275 {
276 lockdep_assert_held(&ce->guc_state.lock);
277 ce->guc_state.sched_state &= ~SCHED_STATE_BANNED;
278 }
279
context_enabled(struct intel_context * ce)280 static inline bool context_enabled(struct intel_context *ce)
281 {
282 return ce->guc_state.sched_state & SCHED_STATE_ENABLED;
283 }
284
set_context_enabled(struct intel_context * ce)285 static inline void set_context_enabled(struct intel_context *ce)
286 {
287 lockdep_assert_held(&ce->guc_state.lock);
288 ce->guc_state.sched_state |= SCHED_STATE_ENABLED;
289 }
290
clr_context_enabled(struct intel_context * ce)291 static inline void clr_context_enabled(struct intel_context *ce)
292 {
293 lockdep_assert_held(&ce->guc_state.lock);
294 ce->guc_state.sched_state &= ~SCHED_STATE_ENABLED;
295 }
296
context_pending_enable(struct intel_context * ce)297 static inline bool context_pending_enable(struct intel_context *ce)
298 {
299 return ce->guc_state.sched_state & SCHED_STATE_PENDING_ENABLE;
300 }
301
set_context_pending_enable(struct intel_context * ce)302 static inline void set_context_pending_enable(struct intel_context *ce)
303 {
304 lockdep_assert_held(&ce->guc_state.lock);
305 ce->guc_state.sched_state |= SCHED_STATE_PENDING_ENABLE;
306 }
307
clr_context_pending_enable(struct intel_context * ce)308 static inline void clr_context_pending_enable(struct intel_context *ce)
309 {
310 lockdep_assert_held(&ce->guc_state.lock);
311 ce->guc_state.sched_state &= ~SCHED_STATE_PENDING_ENABLE;
312 }
313
context_registered(struct intel_context * ce)314 static inline bool context_registered(struct intel_context *ce)
315 {
316 return ce->guc_state.sched_state & SCHED_STATE_REGISTERED;
317 }
318
set_context_registered(struct intel_context * ce)319 static inline void set_context_registered(struct intel_context *ce)
320 {
321 lockdep_assert_held(&ce->guc_state.lock);
322 ce->guc_state.sched_state |= SCHED_STATE_REGISTERED;
323 }
324
clr_context_registered(struct intel_context * ce)325 static inline void clr_context_registered(struct intel_context *ce)
326 {
327 lockdep_assert_held(&ce->guc_state.lock);
328 ce->guc_state.sched_state &= ~SCHED_STATE_REGISTERED;
329 }
330
context_policy_required(struct intel_context * ce)331 static inline bool context_policy_required(struct intel_context *ce)
332 {
333 return ce->guc_state.sched_state & SCHED_STATE_POLICY_REQUIRED;
334 }
335
set_context_policy_required(struct intel_context * ce)336 static inline void set_context_policy_required(struct intel_context *ce)
337 {
338 lockdep_assert_held(&ce->guc_state.lock);
339 ce->guc_state.sched_state |= SCHED_STATE_POLICY_REQUIRED;
340 }
341
clr_context_policy_required(struct intel_context * ce)342 static inline void clr_context_policy_required(struct intel_context *ce)
343 {
344 lockdep_assert_held(&ce->guc_state.lock);
345 ce->guc_state.sched_state &= ~SCHED_STATE_POLICY_REQUIRED;
346 }
347
context_close_done(struct intel_context * ce)348 static inline bool context_close_done(struct intel_context *ce)
349 {
350 return ce->guc_state.sched_state & SCHED_STATE_CLOSED;
351 }
352
set_context_close_done(struct intel_context * ce)353 static inline void set_context_close_done(struct intel_context *ce)
354 {
355 lockdep_assert_held(&ce->guc_state.lock);
356 ce->guc_state.sched_state |= SCHED_STATE_CLOSED;
357 }
358
context_blocked(struct intel_context * ce)359 static inline u32 context_blocked(struct intel_context *ce)
360 {
361 return (ce->guc_state.sched_state & SCHED_STATE_BLOCKED_MASK) >>
362 SCHED_STATE_BLOCKED_SHIFT;
363 }
364
incr_context_blocked(struct intel_context * ce)365 static inline void incr_context_blocked(struct intel_context *ce)
366 {
367 lockdep_assert_held(&ce->guc_state.lock);
368
369 ce->guc_state.sched_state += SCHED_STATE_BLOCKED;
370
371 GEM_BUG_ON(!context_blocked(ce)); /* Overflow check */
372 }
373
decr_context_blocked(struct intel_context * ce)374 static inline void decr_context_blocked(struct intel_context *ce)
375 {
376 lockdep_assert_held(&ce->guc_state.lock);
377
378 GEM_BUG_ON(!context_blocked(ce)); /* Underflow check */
379
380 ce->guc_state.sched_state -= SCHED_STATE_BLOCKED;
381 }
382
383 static struct intel_context *
request_to_scheduling_context(struct i915_request * rq)384 request_to_scheduling_context(struct i915_request *rq)
385 {
386 return intel_context_to_parent(rq->context);
387 }
388
context_guc_id_invalid(struct intel_context * ce)389 static inline bool context_guc_id_invalid(struct intel_context *ce)
390 {
391 return ce->guc_id.id == GUC_INVALID_CONTEXT_ID;
392 }
393
set_context_guc_id_invalid(struct intel_context * ce)394 static inline void set_context_guc_id_invalid(struct intel_context *ce)
395 {
396 ce->guc_id.id = GUC_INVALID_CONTEXT_ID;
397 }
398
ce_to_guc(struct intel_context * ce)399 static inline struct intel_guc *ce_to_guc(struct intel_context *ce)
400 {
401 return gt_to_guc(ce->engine->gt);
402 }
403
to_priolist(struct rb_node * rb)404 static inline struct i915_priolist *to_priolist(struct rb_node *rb)
405 {
406 return rb_entry(rb, struct i915_priolist, node);
407 }
408
409 /*
410 * When using multi-lrc submission a scratch memory area is reserved in the
411 * parent's context state for the process descriptor, work queue, and handshake
412 * between the parent + children contexts to insert safe preemption points
413 * between each of the BBs. Currently the scratch area is sized to a page.
414 *
415 * The layout of this scratch area is below:
416 * 0 guc_process_desc
417 * + sizeof(struct guc_process_desc) child go
418 * + CACHELINE_BYTES child join[0]
419 * ...
420 * + CACHELINE_BYTES child join[n - 1]
421 * ... unused
422 * PARENT_SCRATCH_SIZE / 2 work queue start
423 * ... work queue
424 * PARENT_SCRATCH_SIZE - 1 work queue end
425 */
426 #define WQ_SIZE (PARENT_SCRATCH_SIZE / 2)
427 #define WQ_OFFSET (PARENT_SCRATCH_SIZE - WQ_SIZE)
428
429 struct sync_semaphore {
430 u32 semaphore;
431 u8 unused[CACHELINE_BYTES - sizeof(u32)];
432 };
433
434 struct parent_scratch {
435 union guc_descs {
436 struct guc_sched_wq_desc wq_desc;
437 struct guc_process_desc_v69 pdesc;
438 } descs;
439
440 struct sync_semaphore go;
441 struct sync_semaphore join[MAX_ENGINE_INSTANCE + 1];
442
443 u8 unused[WQ_OFFSET - sizeof(union guc_descs) -
444 sizeof(struct sync_semaphore) * (MAX_ENGINE_INSTANCE + 2)];
445
446 u32 wq[WQ_SIZE / sizeof(u32)];
447 };
448
__get_parent_scratch_offset(struct intel_context * ce)449 static u32 __get_parent_scratch_offset(struct intel_context *ce)
450 {
451 GEM_BUG_ON(!ce->parallel.guc.parent_page);
452
453 return ce->parallel.guc.parent_page * PAGE_SIZE;
454 }
455
__get_wq_offset(struct intel_context * ce)456 static u32 __get_wq_offset(struct intel_context *ce)
457 {
458 BUILD_BUG_ON(offsetof(struct parent_scratch, wq) != WQ_OFFSET);
459
460 return __get_parent_scratch_offset(ce) + WQ_OFFSET;
461 }
462
463 static struct parent_scratch *
__get_parent_scratch(struct intel_context * ce)464 __get_parent_scratch(struct intel_context *ce)
465 {
466 BUILD_BUG_ON(sizeof(struct parent_scratch) != PARENT_SCRATCH_SIZE);
467 BUILD_BUG_ON(sizeof(struct sync_semaphore) != CACHELINE_BYTES);
468
469 /*
470 * Need to subtract LRC_STATE_OFFSET here as the
471 * parallel.guc.parent_page is the offset into ce->state while
472 * ce->lrc_reg_reg is ce->state + LRC_STATE_OFFSET.
473 */
474 return (struct parent_scratch *)
475 (ce->lrc_reg_state +
476 ((__get_parent_scratch_offset(ce) -
477 LRC_STATE_OFFSET) / sizeof(u32)));
478 }
479
480 static struct guc_process_desc_v69 *
__get_process_desc_v69(struct intel_context * ce)481 __get_process_desc_v69(struct intel_context *ce)
482 {
483 struct parent_scratch *ps = __get_parent_scratch(ce);
484
485 return &ps->descs.pdesc;
486 }
487
488 static struct guc_sched_wq_desc *
__get_wq_desc_v70(struct intel_context * ce)489 __get_wq_desc_v70(struct intel_context *ce)
490 {
491 struct parent_scratch *ps = __get_parent_scratch(ce);
492
493 return &ps->descs.wq_desc;
494 }
495
get_wq_pointer(struct intel_context * ce,u32 wqi_size)496 static u32 *get_wq_pointer(struct intel_context *ce, u32 wqi_size)
497 {
498 /*
499 * Check for space in work queue. Caching a value of head pointer in
500 * intel_context structure in order reduce the number accesses to shared
501 * GPU memory which may be across a PCIe bus.
502 */
503 #define AVAILABLE_SPACE \
504 CIRC_SPACE(ce->parallel.guc.wqi_tail, ce->parallel.guc.wqi_head, WQ_SIZE)
505 if (wqi_size > AVAILABLE_SPACE) {
506 ce->parallel.guc.wqi_head = READ_ONCE(*ce->parallel.guc.wq_head);
507
508 if (wqi_size > AVAILABLE_SPACE)
509 return NULL;
510 }
511 #undef AVAILABLE_SPACE
512
513 return &__get_parent_scratch(ce)->wq[ce->parallel.guc.wqi_tail / sizeof(u32)];
514 }
515
__get_context(struct intel_guc * guc,u32 id)516 static inline struct intel_context *__get_context(struct intel_guc *guc, u32 id)
517 {
518 struct intel_context *ce = xa_load(&guc->context_lookup, id);
519
520 GEM_BUG_ON(id >= GUC_MAX_CONTEXT_ID);
521
522 return ce;
523 }
524
__get_lrc_desc_v69(struct intel_guc * guc,u32 index)525 static struct guc_lrc_desc_v69 *__get_lrc_desc_v69(struct intel_guc *guc, u32 index)
526 {
527 struct guc_lrc_desc_v69 *base = guc->lrc_desc_pool_vaddr_v69;
528
529 if (!base)
530 return NULL;
531
532 GEM_BUG_ON(index >= GUC_MAX_CONTEXT_ID);
533
534 return &base[index];
535 }
536
guc_lrc_desc_pool_create_v69(struct intel_guc * guc)537 static int guc_lrc_desc_pool_create_v69(struct intel_guc *guc)
538 {
539 u32 size;
540 int ret;
541
542 size = PAGE_ALIGN(sizeof(struct guc_lrc_desc_v69) *
543 GUC_MAX_CONTEXT_ID);
544 ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool_v69,
545 (void **)&guc->lrc_desc_pool_vaddr_v69);
546 if (ret)
547 return ret;
548
549 return 0;
550 }
551
guc_lrc_desc_pool_destroy_v69(struct intel_guc * guc)552 static void guc_lrc_desc_pool_destroy_v69(struct intel_guc *guc)
553 {
554 if (!guc->lrc_desc_pool_vaddr_v69)
555 return;
556
557 guc->lrc_desc_pool_vaddr_v69 = NULL;
558 i915_vma_unpin_and_release(&guc->lrc_desc_pool_v69, I915_VMA_RELEASE_MAP);
559 }
560
guc_submission_initialized(struct intel_guc * guc)561 static inline bool guc_submission_initialized(struct intel_guc *guc)
562 {
563 return guc->submission_initialized;
564 }
565
_reset_lrc_desc_v69(struct intel_guc * guc,u32 id)566 static inline void _reset_lrc_desc_v69(struct intel_guc *guc, u32 id)
567 {
568 struct guc_lrc_desc_v69 *desc = __get_lrc_desc_v69(guc, id);
569
570 if (desc)
571 memset(desc, 0, sizeof(*desc));
572 }
573
ctx_id_mapped(struct intel_guc * guc,u32 id)574 static inline bool ctx_id_mapped(struct intel_guc *guc, u32 id)
575 {
576 return __get_context(guc, id);
577 }
578
set_ctx_id_mapping(struct intel_guc * guc,u32 id,struct intel_context * ce)579 static inline void set_ctx_id_mapping(struct intel_guc *guc, u32 id,
580 struct intel_context *ce)
581 {
582 unsigned long flags;
583
584 /*
585 * xarray API doesn't have xa_save_irqsave wrapper, so calling the
586 * lower level functions directly.
587 */
588 xa_lock_irqsave(&guc->context_lookup, flags);
589 __xa_store(&guc->context_lookup, id, ce, GFP_ATOMIC);
590 xa_unlock_irqrestore(&guc->context_lookup, flags);
591 }
592
clr_ctx_id_mapping(struct intel_guc * guc,u32 id)593 static inline void clr_ctx_id_mapping(struct intel_guc *guc, u32 id)
594 {
595 unsigned long flags;
596
597 if (unlikely(!guc_submission_initialized(guc)))
598 return;
599
600 _reset_lrc_desc_v69(guc, id);
601
602 /*
603 * xarray API doesn't have xa_erase_irqsave wrapper, so calling
604 * the lower level functions directly.
605 */
606 xa_lock_irqsave(&guc->context_lookup, flags);
607 __xa_erase(&guc->context_lookup, id);
608 xa_unlock_irqrestore(&guc->context_lookup, flags);
609 }
610
decr_outstanding_submission_g2h(struct intel_guc * guc)611 static void decr_outstanding_submission_g2h(struct intel_guc *guc)
612 {
613 if (atomic_dec_and_test(&guc->outstanding_submission_g2h))
614 wake_up_all(&guc->ct.wq);
615 }
616
guc_submission_send_busy_loop(struct intel_guc * guc,const u32 * action,u32 len,u32 g2h_len_dw,bool loop)617 static int guc_submission_send_busy_loop(struct intel_guc *guc,
618 const u32 *action,
619 u32 len,
620 u32 g2h_len_dw,
621 bool loop)
622 {
623 int ret;
624
625 /*
626 * We always loop when a send requires a reply (i.e. g2h_len_dw > 0),
627 * so we don't handle the case where we don't get a reply because we
628 * aborted the send due to the channel being busy.
629 */
630 GEM_BUG_ON(g2h_len_dw && !loop);
631
632 if (g2h_len_dw)
633 atomic_inc(&guc->outstanding_submission_g2h);
634
635 ret = intel_guc_send_busy_loop(guc, action, len, g2h_len_dw, loop);
636 if (ret)
637 atomic_dec(&guc->outstanding_submission_g2h);
638
639 return ret;
640 }
641
intel_guc_wait_for_pending_msg(struct intel_guc * guc,atomic_t * wait_var,bool interruptible,long timeout)642 int intel_guc_wait_for_pending_msg(struct intel_guc *guc,
643 atomic_t *wait_var,
644 bool interruptible,
645 long timeout)
646 {
647 const int state = interruptible ?
648 TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
649 DEFINE_WAIT(wait);
650
651 might_sleep();
652 GEM_BUG_ON(timeout < 0);
653
654 if (!atomic_read(wait_var))
655 return 0;
656
657 if (!timeout)
658 return -ETIME;
659
660 for (;;) {
661 prepare_to_wait(&guc->ct.wq, &wait, state);
662
663 if (!atomic_read(wait_var))
664 break;
665
666 if (signal_pending_state(state, current)) {
667 timeout = -EINTR;
668 break;
669 }
670
671 if (!timeout) {
672 timeout = -ETIME;
673 break;
674 }
675
676 timeout = io_schedule_timeout(timeout);
677 }
678 finish_wait(&guc->ct.wq, &wait);
679
680 return (timeout < 0) ? timeout : 0;
681 }
682
intel_guc_wait_for_idle(struct intel_guc * guc,long timeout)683 int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
684 {
685 if (!intel_uc_uses_guc_submission(&guc_to_gt(guc)->uc))
686 return 0;
687
688 return intel_guc_wait_for_pending_msg(guc,
689 &guc->outstanding_submission_g2h,
690 true, timeout);
691 }
692
693 static int guc_context_policy_init_v70(struct intel_context *ce, bool loop);
694 static int try_context_registration(struct intel_context *ce, bool loop);
695
__guc_add_request(struct intel_guc * guc,struct i915_request * rq)696 static int __guc_add_request(struct intel_guc *guc, struct i915_request *rq)
697 {
698 int err = 0;
699 struct intel_context *ce = request_to_scheduling_context(rq);
700 u32 action[3];
701 int len = 0;
702 u32 g2h_len_dw = 0;
703 bool enabled;
704
705 lockdep_assert_held(&rq->engine->sched_engine->lock);
706
707 /*
708 * Corner case where requests were sitting in the priority list or a
709 * request resubmitted after the context was banned.
710 */
711 if (unlikely(!intel_context_is_schedulable(ce))) {
712 i915_request_put(i915_request_mark_eio(rq));
713 intel_engine_signal_breadcrumbs(ce->engine);
714 return 0;
715 }
716
717 GEM_BUG_ON(!atomic_read(&ce->guc_id.ref));
718 GEM_BUG_ON(context_guc_id_invalid(ce));
719
720 if (context_policy_required(ce)) {
721 err = guc_context_policy_init_v70(ce, false);
722 if (err)
723 return err;
724 }
725
726 spin_lock(&ce->guc_state.lock);
727
728 /*
729 * The request / context will be run on the hardware when scheduling
730 * gets enabled in the unblock. For multi-lrc we still submit the
731 * context to move the LRC tails.
732 */
733 if (unlikely(context_blocked(ce) && !intel_context_is_parent(ce)))
734 goto out;
735
736 enabled = context_enabled(ce) || context_blocked(ce);
737
738 if (!enabled) {
739 action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
740 action[len++] = ce->guc_id.id;
741 action[len++] = GUC_CONTEXT_ENABLE;
742 set_context_pending_enable(ce);
743 intel_context_get(ce);
744 g2h_len_dw = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET;
745 } else {
746 action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT;
747 action[len++] = ce->guc_id.id;
748 }
749
750 err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
751 if (!enabled && !err) {
752 trace_intel_context_sched_enable(ce);
753 atomic_inc(&guc->outstanding_submission_g2h);
754 set_context_enabled(ce);
755
756 /*
757 * Without multi-lrc KMD does the submission step (moving the
758 * lrc tail) so enabling scheduling is sufficient to submit the
759 * context. This isn't the case in multi-lrc submission as the
760 * GuC needs to move the tails, hence the need for another H2G
761 * to submit a multi-lrc context after enabling scheduling.
762 */
763 if (intel_context_is_parent(ce)) {
764 action[0] = INTEL_GUC_ACTION_SCHED_CONTEXT;
765 err = intel_guc_send_nb(guc, action, len - 1, 0);
766 }
767 } else if (!enabled) {
768 clr_context_pending_enable(ce);
769 intel_context_put(ce);
770 }
771 if (likely(!err))
772 trace_i915_request_guc_submit(rq);
773
774 out:
775 spin_unlock(&ce->guc_state.lock);
776 return err;
777 }
778
guc_add_request(struct intel_guc * guc,struct i915_request * rq)779 static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
780 {
781 int ret = __guc_add_request(guc, rq);
782
783 if (unlikely(ret == -EBUSY)) {
784 guc->stalled_request = rq;
785 guc->submission_stall_reason = STALL_ADD_REQUEST;
786 }
787
788 return ret;
789 }
790
guc_set_lrc_tail(struct i915_request * rq)791 static inline void guc_set_lrc_tail(struct i915_request *rq)
792 {
793 rq->context->lrc_reg_state[CTX_RING_TAIL] =
794 intel_ring_set_tail(rq->ring, rq->tail);
795 }
796
rq_prio(const struct i915_request * rq)797 static inline int rq_prio(const struct i915_request *rq)
798 {
799 return rq->sched.attr.priority;
800 }
801
is_multi_lrc_rq(struct i915_request * rq)802 static bool is_multi_lrc_rq(struct i915_request *rq)
803 {
804 return intel_context_is_parallel(rq->context);
805 }
806
can_merge_rq(struct i915_request * rq,struct i915_request * last)807 static bool can_merge_rq(struct i915_request *rq,
808 struct i915_request *last)
809 {
810 return request_to_scheduling_context(rq) ==
811 request_to_scheduling_context(last);
812 }
813
wq_space_until_wrap(struct intel_context * ce)814 static u32 wq_space_until_wrap(struct intel_context *ce)
815 {
816 return (WQ_SIZE - ce->parallel.guc.wqi_tail);
817 }
818
write_wqi(struct intel_context * ce,u32 wqi_size)819 static void write_wqi(struct intel_context *ce, u32 wqi_size)
820 {
821 BUILD_BUG_ON(!is_power_of_2(WQ_SIZE));
822
823 /*
824 * Ensure WQI are visible before updating tail
825 */
826 intel_guc_write_barrier(ce_to_guc(ce));
827
828 ce->parallel.guc.wqi_tail = (ce->parallel.guc.wqi_tail + wqi_size) &
829 (WQ_SIZE - 1);
830 WRITE_ONCE(*ce->parallel.guc.wq_tail, ce->parallel.guc.wqi_tail);
831 }
832
guc_wq_noop_append(struct intel_context * ce)833 static int guc_wq_noop_append(struct intel_context *ce)
834 {
835 u32 *wqi = get_wq_pointer(ce, wq_space_until_wrap(ce));
836 u32 len_dw = wq_space_until_wrap(ce) / sizeof(u32) - 1;
837
838 if (!wqi)
839 return -EBUSY;
840
841 GEM_BUG_ON(!FIELD_FIT(WQ_LEN_MASK, len_dw));
842
843 *wqi = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) |
844 FIELD_PREP(WQ_LEN_MASK, len_dw);
845 ce->parallel.guc.wqi_tail = 0;
846
847 return 0;
848 }
849
__guc_wq_item_append(struct i915_request * rq)850 static int __guc_wq_item_append(struct i915_request *rq)
851 {
852 struct intel_context *ce = request_to_scheduling_context(rq);
853 struct intel_context *child;
854 unsigned int wqi_size = (ce->parallel.number_children + 4) *
855 sizeof(u32);
856 u32 *wqi;
857 u32 len_dw = (wqi_size / sizeof(u32)) - 1;
858 int ret;
859
860 /* Ensure context is in correct state updating work queue */
861 GEM_BUG_ON(!atomic_read(&ce->guc_id.ref));
862 GEM_BUG_ON(context_guc_id_invalid(ce));
863 GEM_BUG_ON(context_wait_for_deregister_to_register(ce));
864 GEM_BUG_ON(!ctx_id_mapped(ce_to_guc(ce), ce->guc_id.id));
865
866 /* Insert NOOP if this work queue item will wrap the tail pointer. */
867 if (wqi_size > wq_space_until_wrap(ce)) {
868 ret = guc_wq_noop_append(ce);
869 if (ret)
870 return ret;
871 }
872
873 wqi = get_wq_pointer(ce, wqi_size);
874 if (!wqi)
875 return -EBUSY;
876
877 GEM_BUG_ON(!FIELD_FIT(WQ_LEN_MASK, len_dw));
878
879 *wqi++ = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_MULTI_LRC) |
880 FIELD_PREP(WQ_LEN_MASK, len_dw);
881 *wqi++ = ce->lrc.lrca;
882 *wqi++ = FIELD_PREP(WQ_GUC_ID_MASK, ce->guc_id.id) |
883 FIELD_PREP(WQ_RING_TAIL_MASK, ce->ring->tail / sizeof(u64));
884 *wqi++ = 0; /* fence_id */
885 for_each_child(ce, child)
886 *wqi++ = child->ring->tail / sizeof(u64);
887
888 write_wqi(ce, wqi_size);
889
890 return 0;
891 }
892
guc_wq_item_append(struct intel_guc * guc,struct i915_request * rq)893 static int guc_wq_item_append(struct intel_guc *guc,
894 struct i915_request *rq)
895 {
896 struct intel_context *ce = request_to_scheduling_context(rq);
897 int ret;
898
899 if (unlikely(!intel_context_is_schedulable(ce)))
900 return 0;
901
902 ret = __guc_wq_item_append(rq);
903 if (unlikely(ret == -EBUSY)) {
904 guc->stalled_request = rq;
905 guc->submission_stall_reason = STALL_MOVE_LRC_TAIL;
906 }
907
908 return ret;
909 }
910
multi_lrc_submit(struct i915_request * rq)911 static bool multi_lrc_submit(struct i915_request *rq)
912 {
913 struct intel_context *ce = request_to_scheduling_context(rq);
914
915 intel_ring_set_tail(rq->ring, rq->tail);
916
917 /*
918 * We expect the front end (execbuf IOCTL) to set this flag on the last
919 * request generated from a multi-BB submission. This indicates to the
920 * backend (GuC interface) that we should submit this context thus
921 * submitting all the requests generated in parallel.
922 */
923 return test_bit(I915_FENCE_FLAG_SUBMIT_PARALLEL, &rq->fence.flags) ||
924 !intel_context_is_schedulable(ce);
925 }
926
guc_dequeue_one_context(struct intel_guc * guc)927 static int guc_dequeue_one_context(struct intel_guc *guc)
928 {
929 struct i915_sched_engine * const sched_engine = guc->sched_engine;
930 struct i915_request *last = NULL;
931 bool submit = false;
932 struct rb_node *rb;
933 int ret;
934
935 lockdep_assert_held(&sched_engine->lock);
936
937 if (guc->stalled_request) {
938 submit = true;
939 last = guc->stalled_request;
940
941 switch (guc->submission_stall_reason) {
942 case STALL_REGISTER_CONTEXT:
943 goto register_context;
944 case STALL_MOVE_LRC_TAIL:
945 goto move_lrc_tail;
946 case STALL_ADD_REQUEST:
947 goto add_request;
948 default:
949 MISSING_CASE(guc->submission_stall_reason);
950 }
951 }
952
953 while ((rb = rb_first_cached(&sched_engine->queue))) {
954 struct i915_priolist *p = to_priolist(rb);
955 struct i915_request *rq, *rn;
956
957 priolist_for_each_request_consume(rq, rn, p) {
958 if (last && !can_merge_rq(rq, last))
959 goto register_context;
960
961 list_del_init(&rq->sched.link);
962
963 __i915_request_submit(rq);
964
965 trace_i915_request_in(rq, 0);
966 last = rq;
967
968 if (is_multi_lrc_rq(rq)) {
969 /*
970 * We need to coalesce all multi-lrc requests in
971 * a relationship into a single H2G. We are
972 * guaranteed that all of these requests will be
973 * submitted sequentially.
974 */
975 if (multi_lrc_submit(rq)) {
976 submit = true;
977 goto register_context;
978 }
979 } else {
980 submit = true;
981 }
982 }
983
984 rb_erase_cached(&p->node, &sched_engine->queue);
985 i915_priolist_free(p);
986 }
987
988 register_context:
989 if (submit) {
990 struct intel_context *ce = request_to_scheduling_context(last);
991
992 if (unlikely(!ctx_id_mapped(guc, ce->guc_id.id) &&
993 intel_context_is_schedulable(ce))) {
994 ret = try_context_registration(ce, false);
995 if (unlikely(ret == -EPIPE)) {
996 goto deadlk;
997 } else if (ret == -EBUSY) {
998 guc->stalled_request = last;
999 guc->submission_stall_reason =
1000 STALL_REGISTER_CONTEXT;
1001 goto schedule_tasklet;
1002 } else if (ret != 0) {
1003 GEM_WARN_ON(ret); /* Unexpected */
1004 goto deadlk;
1005 }
1006 }
1007
1008 move_lrc_tail:
1009 if (is_multi_lrc_rq(last)) {
1010 ret = guc_wq_item_append(guc, last);
1011 if (ret == -EBUSY) {
1012 goto schedule_tasklet;
1013 } else if (ret != 0) {
1014 GEM_WARN_ON(ret); /* Unexpected */
1015 goto deadlk;
1016 }
1017 } else {
1018 guc_set_lrc_tail(last);
1019 }
1020
1021 add_request:
1022 ret = guc_add_request(guc, last);
1023 if (unlikely(ret == -EPIPE)) {
1024 goto deadlk;
1025 } else if (ret == -EBUSY) {
1026 goto schedule_tasklet;
1027 } else if (ret != 0) {
1028 GEM_WARN_ON(ret); /* Unexpected */
1029 goto deadlk;
1030 }
1031 }
1032
1033 guc->stalled_request = NULL;
1034 guc->submission_stall_reason = STALL_NONE;
1035 return submit;
1036
1037 deadlk:
1038 sched_engine->tasklet.callback = NULL;
1039 tasklet_disable_nosync(&sched_engine->tasklet);
1040 return false;
1041
1042 schedule_tasklet:
1043 tasklet_schedule(&sched_engine->tasklet);
1044 return false;
1045 }
1046
guc_submission_tasklet(struct tasklet_struct * t)1047 static void guc_submission_tasklet(struct tasklet_struct *t)
1048 {
1049 struct i915_sched_engine *sched_engine =
1050 from_tasklet(sched_engine, t, tasklet);
1051 unsigned long flags;
1052 bool loop;
1053
1054 spin_lock_irqsave(&sched_engine->lock, flags);
1055
1056 do {
1057 loop = guc_dequeue_one_context(sched_engine->private_data);
1058 } while (loop);
1059
1060 i915_sched_engine_reset_on_empty(sched_engine);
1061
1062 spin_unlock_irqrestore(&sched_engine->lock, flags);
1063 }
1064
cs_irq_handler(struct intel_engine_cs * engine,u16 iir)1065 static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
1066 {
1067 if (iir & GT_RENDER_USER_INTERRUPT)
1068 intel_engine_signal_breadcrumbs(engine);
1069 }
1070
1071 static void __guc_context_destroy(struct intel_context *ce);
1072 static void release_guc_id(struct intel_guc *guc, struct intel_context *ce);
1073 static void guc_signal_context_fence(struct intel_context *ce);
1074 static void guc_cancel_context_requests(struct intel_context *ce);
1075 static void guc_blocked_fence_complete(struct intel_context *ce);
1076
scrub_guc_desc_for_outstanding_g2h(struct intel_guc * guc)1077 static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
1078 {
1079 struct intel_context *ce;
1080 unsigned long index, flags;
1081 bool pending_disable, pending_enable, deregister, destroyed, banned;
1082
1083 xa_lock_irqsave(&guc->context_lookup, flags);
1084 xa_for_each(&guc->context_lookup, index, ce) {
1085 /*
1086 * Corner case where the ref count on the object is zero but and
1087 * deregister G2H was lost. In this case we don't touch the ref
1088 * count and finish the destroy of the context.
1089 */
1090 bool do_put = kref_get_unless_zero(&ce->ref);
1091
1092 xa_unlock(&guc->context_lookup);
1093
1094 if (test_bit(CONTEXT_GUC_INIT, &ce->flags) &&
1095 (cancel_delayed_work(&ce->guc_state.sched_disable_delay_work))) {
1096 /* successful cancel so jump straight to close it */
1097 intel_context_sched_disable_unpin(ce);
1098 }
1099
1100 spin_lock(&ce->guc_state.lock);
1101
1102 /*
1103 * Once we are at this point submission_disabled() is guaranteed
1104 * to be visible to all callers who set the below flags (see above
1105 * flush and flushes in reset_prepare). If submission_disabled()
1106 * is set, the caller shouldn't set these flags.
1107 */
1108
1109 destroyed = context_destroyed(ce);
1110 pending_enable = context_pending_enable(ce);
1111 pending_disable = context_pending_disable(ce);
1112 deregister = context_wait_for_deregister_to_register(ce);
1113 banned = context_banned(ce);
1114 init_sched_state(ce);
1115
1116 spin_unlock(&ce->guc_state.lock);
1117
1118 if (pending_enable || destroyed || deregister) {
1119 decr_outstanding_submission_g2h(guc);
1120 if (deregister)
1121 guc_signal_context_fence(ce);
1122 if (destroyed) {
1123 intel_gt_pm_put_async_untracked(guc_to_gt(guc));
1124 release_guc_id(guc, ce);
1125 __guc_context_destroy(ce);
1126 }
1127 if (pending_enable || deregister)
1128 intel_context_put(ce);
1129 }
1130
1131 /* Not mutualy exclusive with above if statement. */
1132 if (pending_disable) {
1133 guc_signal_context_fence(ce);
1134 if (banned) {
1135 guc_cancel_context_requests(ce);
1136 intel_engine_signal_breadcrumbs(ce->engine);
1137 }
1138 intel_context_sched_disable_unpin(ce);
1139 decr_outstanding_submission_g2h(guc);
1140
1141 spin_lock(&ce->guc_state.lock);
1142 guc_blocked_fence_complete(ce);
1143 spin_unlock(&ce->guc_state.lock);
1144
1145 intel_context_put(ce);
1146 }
1147
1148 if (do_put)
1149 intel_context_put(ce);
1150 xa_lock(&guc->context_lookup);
1151 }
1152 xa_unlock_irqrestore(&guc->context_lookup, flags);
1153 }
1154
1155 /*
1156 * GuC stores busyness stats for each engine at context in/out boundaries. A
1157 * context 'in' logs execution start time, 'out' adds in -> out delta to total.
1158 * i915/kmd accesses 'start', 'total' and 'context id' from memory shared with
1159 * GuC.
1160 *
1161 * __i915_pmu_event_read samples engine busyness. When sampling, if context id
1162 * is valid (!= ~0) and start is non-zero, the engine is considered to be
1163 * active. For an active engine total busyness = total + (now - start), where
1164 * 'now' is the time at which the busyness is sampled. For inactive engine,
1165 * total busyness = total.
1166 *
1167 * All times are captured from GUCPMTIMESTAMP reg and are in gt clock domain.
1168 *
1169 * The start and total values provided by GuC are 32 bits and wrap around in a
1170 * few minutes. Since perf pmu provides busyness as 64 bit monotonically
1171 * increasing ns values, there is a need for this implementation to account for
1172 * overflows and extend the GuC provided values to 64 bits before returning
1173 * busyness to the user. In order to do that, a worker runs periodically at
1174 * frequency = 1/8th the time it takes for the timestamp to wrap (i.e. once in
1175 * 27 seconds for a gt clock frequency of 19.2 MHz).
1176 */
1177
1178 #define WRAP_TIME_CLKS U32_MAX
1179 #define POLL_TIME_CLKS (WRAP_TIME_CLKS >> 3)
1180
1181 static void
__extend_last_switch(struct intel_guc * guc,u64 * prev_start,u32 new_start)1182 __extend_last_switch(struct intel_guc *guc, u64 *prev_start, u32 new_start)
1183 {
1184 u32 gt_stamp_hi = upper_32_bits(guc->timestamp.gt_stamp);
1185 u32 gt_stamp_last = lower_32_bits(guc->timestamp.gt_stamp);
1186
1187 if (new_start == lower_32_bits(*prev_start))
1188 return;
1189
1190 /*
1191 * When gt is unparked, we update the gt timestamp and start the ping
1192 * worker that updates the gt_stamp every POLL_TIME_CLKS. As long as gt
1193 * is unparked, all switched in contexts will have a start time that is
1194 * within +/- POLL_TIME_CLKS of the most recent gt_stamp.
1195 *
1196 * If neither gt_stamp nor new_start has rolled over, then the
1197 * gt_stamp_hi does not need to be adjusted, however if one of them has
1198 * rolled over, we need to adjust gt_stamp_hi accordingly.
1199 *
1200 * The below conditions address the cases of new_start rollover and
1201 * gt_stamp_last rollover respectively.
1202 */
1203 if (new_start < gt_stamp_last &&
1204 (new_start - gt_stamp_last) <= POLL_TIME_CLKS)
1205 gt_stamp_hi++;
1206
1207 if (new_start > gt_stamp_last &&
1208 (gt_stamp_last - new_start) <= POLL_TIME_CLKS && gt_stamp_hi)
1209 gt_stamp_hi--;
1210
1211 *prev_start = ((u64)gt_stamp_hi << 32) | new_start;
1212 }
1213
1214 #define record_read(map_, field_) \
1215 iosys_map_rd_field(map_, 0, struct guc_engine_usage_record, field_)
1216
1217 /*
1218 * GuC updates shared memory and KMD reads it. Since this is not synchronized,
1219 * we run into a race where the value read is inconsistent. Sometimes the
1220 * inconsistency is in reading the upper MSB bytes of the last_in value when
1221 * this race occurs. 2 types of cases are seen - upper 8 bits are zero and upper
1222 * 24 bits are zero. Since these are non-zero values, it is non-trivial to
1223 * determine validity of these values. Instead we read the values multiple times
1224 * until they are consistent. In test runs, 3 attempts results in consistent
1225 * values. The upper bound is set to 6 attempts and may need to be tuned as per
1226 * any new occurences.
1227 */
__get_engine_usage_record(struct intel_engine_cs * engine,u32 * last_in,u32 * id,u32 * total)1228 static void __get_engine_usage_record(struct intel_engine_cs *engine,
1229 u32 *last_in, u32 *id, u32 *total)
1230 {
1231 struct iosys_map rec_map = intel_guc_engine_usage_record_map(engine);
1232 int i = 0;
1233
1234 do {
1235 *last_in = record_read(&rec_map, last_switch_in_stamp);
1236 *id = record_read(&rec_map, current_context_index);
1237 *total = record_read(&rec_map, total_runtime);
1238
1239 if (record_read(&rec_map, last_switch_in_stamp) == *last_in &&
1240 record_read(&rec_map, current_context_index) == *id &&
1241 record_read(&rec_map, total_runtime) == *total)
1242 break;
1243 } while (++i < 6);
1244 }
1245
__set_engine_usage_record(struct intel_engine_cs * engine,u32 last_in,u32 id,u32 total)1246 static void __set_engine_usage_record(struct intel_engine_cs *engine,
1247 u32 last_in, u32 id, u32 total)
1248 {
1249 struct iosys_map rec_map = intel_guc_engine_usage_record_map(engine);
1250
1251 #define record_write(map_, field_, val_) \
1252 iosys_map_wr_field(map_, 0, struct guc_engine_usage_record, field_, val_)
1253
1254 record_write(&rec_map, last_switch_in_stamp, last_in);
1255 record_write(&rec_map, current_context_index, id);
1256 record_write(&rec_map, total_runtime, total);
1257
1258 #undef record_write
1259 }
1260
guc_update_engine_gt_clks(struct intel_engine_cs * engine)1261 static void guc_update_engine_gt_clks(struct intel_engine_cs *engine)
1262 {
1263 struct intel_engine_guc_stats *stats = &engine->stats.guc;
1264 struct intel_guc *guc = gt_to_guc(engine->gt);
1265 u32 last_switch, ctx_id, total;
1266
1267 lockdep_assert_held(&guc->timestamp.lock);
1268
1269 __get_engine_usage_record(engine, &last_switch, &ctx_id, &total);
1270
1271 stats->running = ctx_id != ~0U && last_switch;
1272 if (stats->running)
1273 __extend_last_switch(guc, &stats->start_gt_clk, last_switch);
1274
1275 /*
1276 * Instead of adjusting the total for overflow, just add the
1277 * difference from previous sample stats->total_gt_clks
1278 */
1279 if (total && total != ~0U) {
1280 stats->total_gt_clks += (u32)(total - stats->prev_total);
1281 stats->prev_total = total;
1282 }
1283 }
1284
gpm_timestamp_shift(struct intel_gt * gt)1285 static u32 gpm_timestamp_shift(struct intel_gt *gt)
1286 {
1287 intel_wakeref_t wakeref;
1288 u32 reg, shift;
1289
1290 with_intel_runtime_pm(gt->uncore->rpm, wakeref)
1291 reg = intel_uncore_read(gt->uncore, RPM_CONFIG0);
1292
1293 shift = (reg & GEN10_RPM_CONFIG0_CTC_SHIFT_PARAMETER_MASK) >>
1294 GEN10_RPM_CONFIG0_CTC_SHIFT_PARAMETER_SHIFT;
1295
1296 return 3 - shift;
1297 }
1298
guc_update_pm_timestamp(struct intel_guc * guc,ktime_t * now)1299 static void guc_update_pm_timestamp(struct intel_guc *guc, ktime_t *now)
1300 {
1301 struct intel_gt *gt = guc_to_gt(guc);
1302 u32 gt_stamp_lo, gt_stamp_hi;
1303 u64 gpm_ts;
1304
1305 lockdep_assert_held(&guc->timestamp.lock);
1306
1307 gt_stamp_hi = upper_32_bits(guc->timestamp.gt_stamp);
1308 gpm_ts = intel_uncore_read64_2x32(gt->uncore, MISC_STATUS0,
1309 MISC_STATUS1) >> guc->timestamp.shift;
1310 gt_stamp_lo = lower_32_bits(gpm_ts);
1311 *now = ktime_get();
1312
1313 if (gt_stamp_lo < lower_32_bits(guc->timestamp.gt_stamp))
1314 gt_stamp_hi++;
1315
1316 guc->timestamp.gt_stamp = ((u64)gt_stamp_hi << 32) | gt_stamp_lo;
1317 }
1318
1319 /*
1320 * Unlike the execlist mode of submission total and active times are in terms of
1321 * gt clocks. The *now parameter is retained to return the cpu time at which the
1322 * busyness was sampled.
1323 */
guc_engine_busyness(struct intel_engine_cs * engine,ktime_t * now)1324 static ktime_t guc_engine_busyness(struct intel_engine_cs *engine, ktime_t *now)
1325 {
1326 struct intel_engine_guc_stats stats_saved, *stats = &engine->stats.guc;
1327 struct i915_gpu_error *gpu_error = &engine->i915->gpu_error;
1328 struct intel_gt *gt = engine->gt;
1329 struct intel_guc *guc = gt_to_guc(gt);
1330 u64 total, gt_stamp_saved;
1331 unsigned long flags;
1332 u32 reset_count;
1333 bool in_reset;
1334 intel_wakeref_t wakeref;
1335
1336 spin_lock_irqsave(&guc->timestamp.lock, flags);
1337
1338 /*
1339 * If a reset happened, we risk reading partially updated engine
1340 * busyness from GuC, so we just use the driver stored copy of busyness.
1341 * Synchronize with gt reset using reset_count and the
1342 * I915_RESET_BACKOFF flag. Note that reset flow updates the reset_count
1343 * after I915_RESET_BACKOFF flag, so ensure that the reset_count is
1344 * usable by checking the flag afterwards.
1345 */
1346 reset_count = i915_reset_count(gpu_error);
1347 in_reset = test_bit(I915_RESET_BACKOFF, >->reset.flags);
1348
1349 *now = ktime_get();
1350
1351 /*
1352 * The active busyness depends on start_gt_clk and gt_stamp.
1353 * gt_stamp is updated by i915 only when gt is awake and the
1354 * start_gt_clk is derived from GuC state. To get a consistent
1355 * view of activity, we query the GuC state only if gt is awake.
1356 */
1357 wakeref = in_reset ? NULL : intel_gt_pm_get_if_awake(gt);
1358 if (wakeref) {
1359 stats_saved = *stats;
1360 gt_stamp_saved = guc->timestamp.gt_stamp;
1361 /*
1362 * Update gt_clks, then gt timestamp to simplify the 'gt_stamp -
1363 * start_gt_clk' calculation below for active engines.
1364 */
1365 guc_update_engine_gt_clks(engine);
1366 guc_update_pm_timestamp(guc, now);
1367 intel_gt_pm_put_async(gt, wakeref);
1368 if (i915_reset_count(gpu_error) != reset_count) {
1369 *stats = stats_saved;
1370 guc->timestamp.gt_stamp = gt_stamp_saved;
1371 }
1372 }
1373
1374 total = intel_gt_clock_interval_to_ns(gt, stats->total_gt_clks);
1375 if (stats->running) {
1376 u64 clk = guc->timestamp.gt_stamp - stats->start_gt_clk;
1377
1378 total += intel_gt_clock_interval_to_ns(gt, clk);
1379 }
1380
1381 if (total > stats->total)
1382 stats->total = total;
1383
1384 spin_unlock_irqrestore(&guc->timestamp.lock, flags);
1385
1386 return ns_to_ktime(stats->total);
1387 }
1388
guc_enable_busyness_worker(struct intel_guc * guc)1389 static void guc_enable_busyness_worker(struct intel_guc *guc)
1390 {
1391 mod_delayed_work(system_highpri_wq, &guc->timestamp.work, guc->timestamp.ping_delay);
1392 }
1393
guc_cancel_busyness_worker(struct intel_guc * guc)1394 static void guc_cancel_busyness_worker(struct intel_guc *guc)
1395 {
1396 /*
1397 * There are many different call stacks that can get here. Some of them
1398 * hold the reset mutex. The busyness worker also attempts to acquire the
1399 * reset mutex. Synchronously flushing a worker thread requires acquiring
1400 * the worker mutex. Lockdep sees this as a conflict. It thinks that the
1401 * flush can deadlock because it holds the worker mutex while waiting for
1402 * the reset mutex, but another thread is holding the reset mutex and might
1403 * attempt to use other worker functions.
1404 *
1405 * In practice, this scenario does not exist because the busyness worker
1406 * does not block waiting for the reset mutex. It does a try-lock on it and
1407 * immediately exits if the lock is already held. Unfortunately, the mutex
1408 * in question (I915_RESET_BACKOFF) is an i915 implementation which has lockdep
1409 * annotation but not to the extent of explaining the 'might lock' is also a
1410 * 'does not need to lock'. So one option would be to add more complex lockdep
1411 * annotations to ignore the issue (if at all possible). A simpler option is to
1412 * just not flush synchronously when a rest in progress. Given that the worker
1413 * will just early exit and re-schedule itself anyway, there is no advantage
1414 * to running it immediately.
1415 *
1416 * If a reset is not in progress, then the synchronous flush may be required.
1417 * As noted many call stacks lead here, some during suspend and driver unload
1418 * which do require a synchronous flush to make sure the worker is stopped
1419 * before memory is freed.
1420 *
1421 * Trying to pass a 'need_sync' or 'in_reset' flag all the way down through
1422 * every possible call stack is unfeasible. It would be too intrusive to many
1423 * areas that really don't care about the GuC backend. However, there is the
1424 * I915_RESET_BACKOFF flag and the gt->reset.mutex can be tested for is_locked.
1425 * So just use those. Note that testing both is required due to the hideously
1426 * complex nature of the i915 driver's reset code paths.
1427 *
1428 * And note that in the case of a reset occurring during driver unload
1429 * (wedged_on_fini), skipping the cancel in reset_prepare/reset_fini (when the
1430 * reset flag/mutex are set) is fine because there is another explicit cancel in
1431 * intel_guc_submission_fini (when the reset flag/mutex are not).
1432 */
1433 if (mutex_is_locked(&guc_to_gt(guc)->reset.mutex) ||
1434 test_bit(I915_RESET_BACKOFF, &guc_to_gt(guc)->reset.flags))
1435 cancel_delayed_work(&guc->timestamp.work);
1436 else
1437 cancel_delayed_work_sync(&guc->timestamp.work);
1438 }
1439
__reset_guc_busyness_stats(struct intel_guc * guc)1440 static void __reset_guc_busyness_stats(struct intel_guc *guc)
1441 {
1442 struct intel_gt *gt = guc_to_gt(guc);
1443 struct intel_engine_cs *engine;
1444 enum intel_engine_id id;
1445 unsigned long flags;
1446 ktime_t unused;
1447
1448 spin_lock_irqsave(&guc->timestamp.lock, flags);
1449
1450 guc_update_pm_timestamp(guc, &unused);
1451 for_each_engine(engine, gt, id) {
1452 struct intel_engine_guc_stats *stats = &engine->stats.guc;
1453
1454 guc_update_engine_gt_clks(engine);
1455
1456 /*
1457 * If resetting a running context, accumulate the active
1458 * time as well since there will be no context switch.
1459 */
1460 if (stats->running) {
1461 u64 clk = guc->timestamp.gt_stamp - stats->start_gt_clk;
1462
1463 stats->total_gt_clks += clk;
1464 }
1465 stats->prev_total = 0;
1466 stats->running = 0;
1467 }
1468
1469 spin_unlock_irqrestore(&guc->timestamp.lock, flags);
1470 }
1471
__update_guc_busyness_stats(struct intel_guc * guc)1472 static void __update_guc_busyness_stats(struct intel_guc *guc)
1473 {
1474 struct intel_gt *gt = guc_to_gt(guc);
1475 struct intel_engine_cs *engine;
1476 enum intel_engine_id id;
1477 unsigned long flags;
1478 ktime_t unused;
1479
1480 guc->timestamp.last_stat_jiffies = jiffies;
1481
1482 spin_lock_irqsave(&guc->timestamp.lock, flags);
1483
1484 guc_update_pm_timestamp(guc, &unused);
1485 for_each_engine(engine, gt, id)
1486 guc_update_engine_gt_clks(engine);
1487
1488 spin_unlock_irqrestore(&guc->timestamp.lock, flags);
1489 }
1490
__guc_context_update_stats(struct intel_context * ce)1491 static void __guc_context_update_stats(struct intel_context *ce)
1492 {
1493 struct intel_guc *guc = ce_to_guc(ce);
1494 unsigned long flags;
1495
1496 spin_lock_irqsave(&guc->timestamp.lock, flags);
1497 lrc_update_runtime(ce);
1498 spin_unlock_irqrestore(&guc->timestamp.lock, flags);
1499 }
1500
guc_context_update_stats(struct intel_context * ce)1501 static void guc_context_update_stats(struct intel_context *ce)
1502 {
1503 if (!intel_context_pin_if_active(ce))
1504 return;
1505
1506 __guc_context_update_stats(ce);
1507 intel_context_unpin(ce);
1508 }
1509
guc_timestamp_ping(struct work_struct * wrk)1510 static void guc_timestamp_ping(struct work_struct *wrk)
1511 {
1512 struct intel_guc *guc = container_of(wrk, typeof(*guc),
1513 timestamp.work.work);
1514 struct intel_uc *uc = container_of(guc, typeof(*uc), guc);
1515 struct intel_gt *gt = guc_to_gt(guc);
1516 struct intel_context *ce;
1517 intel_wakeref_t wakeref;
1518 unsigned long index;
1519 int srcu, ret;
1520
1521 /*
1522 * Ideally the busyness worker should take a gt pm wakeref because the
1523 * worker only needs to be active while gt is awake. However, the
1524 * gt_park path cancels the worker synchronously and this complicates
1525 * the flow if the worker is also running at the same time. The cancel
1526 * waits for the worker and when the worker releases the wakeref, that
1527 * would call gt_park and would lead to a deadlock.
1528 *
1529 * The resolution is to take the global pm wakeref if runtime pm is
1530 * already active. If not, we don't need to update the busyness stats as
1531 * the stats would already be updated when the gt was parked.
1532 *
1533 * Note:
1534 * - We do not requeue the worker if we cannot take a reference to runtime
1535 * pm since intel_guc_busyness_unpark would requeue the worker in the
1536 * resume path.
1537 *
1538 * - If the gt was parked longer than time taken for GT timestamp to roll
1539 * over, we ignore those rollovers since we don't care about tracking
1540 * the exact GT time. We only care about roll overs when the gt is
1541 * active and running workloads.
1542 *
1543 * - There is a window of time between gt_park and runtime suspend,
1544 * where the worker may run. This is acceptable since the worker will
1545 * not find any new data to update busyness.
1546 */
1547 wakeref = intel_runtime_pm_get_if_active(>->i915->runtime_pm);
1548 if (!wakeref)
1549 return;
1550
1551 /*
1552 * Synchronize with gt reset to make sure the worker does not
1553 * corrupt the engine/guc stats. NB: can't actually block waiting
1554 * for a reset to complete as the reset requires flushing out
1555 * this worker thread if started. So waiting would deadlock.
1556 */
1557 ret = intel_gt_reset_trylock(gt, &srcu);
1558 if (ret)
1559 goto err_trylock;
1560
1561 __update_guc_busyness_stats(guc);
1562
1563 /* adjust context stats for overflow */
1564 xa_for_each(&guc->context_lookup, index, ce)
1565 guc_context_update_stats(ce);
1566
1567 intel_gt_reset_unlock(gt, srcu);
1568
1569 guc_enable_busyness_worker(guc);
1570
1571 err_trylock:
1572 intel_runtime_pm_put(>->i915->runtime_pm, wakeref);
1573 }
1574
guc_action_enable_usage_stats(struct intel_guc * guc)1575 static int guc_action_enable_usage_stats(struct intel_guc *guc)
1576 {
1577 struct intel_gt *gt = guc_to_gt(guc);
1578 struct intel_engine_cs *engine;
1579 enum intel_engine_id id;
1580 u32 offset = intel_guc_engine_usage_offset(guc);
1581 u32 action[] = {
1582 INTEL_GUC_ACTION_SET_ENG_UTIL_BUFF,
1583 offset,
1584 0,
1585 };
1586
1587 for_each_engine(engine, gt, id)
1588 __set_engine_usage_record(engine, 0, 0xffffffff, 0);
1589
1590 return intel_guc_send(guc, action, ARRAY_SIZE(action));
1591 }
1592
guc_init_engine_stats(struct intel_guc * guc)1593 static int guc_init_engine_stats(struct intel_guc *guc)
1594 {
1595 struct intel_gt *gt = guc_to_gt(guc);
1596 intel_wakeref_t wakeref;
1597 int ret;
1598
1599 with_intel_runtime_pm(>->i915->runtime_pm, wakeref)
1600 ret = guc_action_enable_usage_stats(guc);
1601
1602 if (ret)
1603 guc_err(guc, "Failed to enable usage stats: %pe\n", ERR_PTR(ret));
1604 else
1605 guc_enable_busyness_worker(guc);
1606
1607 return ret;
1608 }
1609
guc_fini_engine_stats(struct intel_guc * guc)1610 static void guc_fini_engine_stats(struct intel_guc *guc)
1611 {
1612 guc_cancel_busyness_worker(guc);
1613 }
1614
intel_guc_busyness_park(struct intel_gt * gt)1615 void intel_guc_busyness_park(struct intel_gt *gt)
1616 {
1617 struct intel_guc *guc = gt_to_guc(gt);
1618
1619 if (!guc_submission_initialized(guc))
1620 return;
1621
1622 /*
1623 * There is a race with suspend flow where the worker runs after suspend
1624 * and causes an unclaimed register access warning. Cancel the worker
1625 * synchronously here.
1626 */
1627 guc_cancel_busyness_worker(guc);
1628
1629 /*
1630 * Before parking, we should sample engine busyness stats if we need to.
1631 * We can skip it if we are less than half a ping from the last time we
1632 * sampled the busyness stats.
1633 */
1634 if (guc->timestamp.last_stat_jiffies &&
1635 !time_after(jiffies, guc->timestamp.last_stat_jiffies +
1636 (guc->timestamp.ping_delay / 2)))
1637 return;
1638
1639 __update_guc_busyness_stats(guc);
1640 }
1641
intel_guc_busyness_unpark(struct intel_gt * gt)1642 void intel_guc_busyness_unpark(struct intel_gt *gt)
1643 {
1644 struct intel_guc *guc = gt_to_guc(gt);
1645 unsigned long flags;
1646 ktime_t unused;
1647
1648 if (!guc_submission_initialized(guc))
1649 return;
1650
1651 spin_lock_irqsave(&guc->timestamp.lock, flags);
1652 guc_update_pm_timestamp(guc, &unused);
1653 spin_unlock_irqrestore(&guc->timestamp.lock, flags);
1654 guc_enable_busyness_worker(guc);
1655 }
1656
1657 static inline bool
submission_disabled(struct intel_guc * guc)1658 submission_disabled(struct intel_guc *guc)
1659 {
1660 struct i915_sched_engine * const sched_engine = guc->sched_engine;
1661
1662 return unlikely(!sched_engine ||
1663 !__tasklet_is_enabled(&sched_engine->tasklet) ||
1664 intel_gt_is_wedged(guc_to_gt(guc)));
1665 }
1666
disable_submission(struct intel_guc * guc)1667 static void disable_submission(struct intel_guc *guc)
1668 {
1669 struct i915_sched_engine * const sched_engine = guc->sched_engine;
1670
1671 if (__tasklet_is_enabled(&sched_engine->tasklet)) {
1672 GEM_BUG_ON(!guc->ct.enabled);
1673 __tasklet_disable_sync_once(&sched_engine->tasklet);
1674 sched_engine->tasklet.callback = NULL;
1675 }
1676 }
1677
enable_submission(struct intel_guc * guc)1678 static void enable_submission(struct intel_guc *guc)
1679 {
1680 struct i915_sched_engine * const sched_engine = guc->sched_engine;
1681 unsigned long flags;
1682
1683 spin_lock_irqsave(&guc->sched_engine->lock, flags);
1684 sched_engine->tasklet.callback = guc_submission_tasklet;
1685 wmb(); /* Make sure callback visible */
1686 if (!__tasklet_is_enabled(&sched_engine->tasklet) &&
1687 __tasklet_enable(&sched_engine->tasklet)) {
1688 GEM_BUG_ON(!guc->ct.enabled);
1689
1690 /* And kick in case we missed a new request submission. */
1691 tasklet_hi_schedule(&sched_engine->tasklet);
1692 }
1693 spin_unlock_irqrestore(&guc->sched_engine->lock, flags);
1694 }
1695
guc_flush_submissions(struct intel_guc * guc)1696 static void guc_flush_submissions(struct intel_guc *guc)
1697 {
1698 struct i915_sched_engine * const sched_engine = guc->sched_engine;
1699 unsigned long flags;
1700
1701 spin_lock_irqsave(&sched_engine->lock, flags);
1702 spin_unlock_irqrestore(&sched_engine->lock, flags);
1703 }
1704
intel_guc_submission_flush_work(struct intel_guc * guc)1705 void intel_guc_submission_flush_work(struct intel_guc *guc)
1706 {
1707 flush_work(&guc->submission_state.destroyed_worker);
1708 }
1709
1710 static void guc_flush_destroyed_contexts(struct intel_guc *guc);
1711
intel_guc_submission_reset_prepare(struct intel_guc * guc)1712 void intel_guc_submission_reset_prepare(struct intel_guc *guc)
1713 {
1714 if (unlikely(!guc_submission_initialized(guc))) {
1715 /* Reset called during driver load? GuC not yet initialised! */
1716 return;
1717 }
1718
1719 intel_gt_park_heartbeats(guc_to_gt(guc));
1720 disable_submission(guc);
1721 guc->interrupts.disable(guc);
1722 __reset_guc_busyness_stats(guc);
1723
1724 /* Flush IRQ handler */
1725 spin_lock_irq(guc_to_gt(guc)->irq_lock);
1726 spin_unlock_irq(guc_to_gt(guc)->irq_lock);
1727
1728 guc_flush_submissions(guc);
1729 guc_flush_destroyed_contexts(guc);
1730 flush_work(&guc->ct.requests.worker);
1731
1732 scrub_guc_desc_for_outstanding_g2h(guc);
1733 }
1734
1735 static struct intel_engine_cs *
guc_virtual_get_sibling(struct intel_engine_cs * ve,unsigned int sibling)1736 guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
1737 {
1738 struct intel_engine_cs *engine;
1739 intel_engine_mask_t tmp, mask = ve->mask;
1740 unsigned int num_siblings = 0;
1741
1742 for_each_engine_masked(engine, ve->gt, mask, tmp)
1743 if (num_siblings++ == sibling)
1744 return engine;
1745
1746 return NULL;
1747 }
1748
1749 static inline struct intel_engine_cs *
__context_to_physical_engine(struct intel_context * ce)1750 __context_to_physical_engine(struct intel_context *ce)
1751 {
1752 struct intel_engine_cs *engine = ce->engine;
1753
1754 if (intel_engine_is_virtual(engine))
1755 engine = guc_virtual_get_sibling(engine, 0);
1756
1757 return engine;
1758 }
1759
guc_reset_state(struct intel_context * ce,u32 head,bool scrub)1760 static void guc_reset_state(struct intel_context *ce, u32 head, bool scrub)
1761 {
1762 struct intel_engine_cs *engine = __context_to_physical_engine(ce);
1763
1764 if (!intel_context_is_schedulable(ce))
1765 return;
1766
1767 GEM_BUG_ON(!intel_context_is_pinned(ce));
1768
1769 /*
1770 * We want a simple context + ring to execute the breadcrumb update.
1771 * We cannot rely on the context being intact across the GPU hang,
1772 * so clear it and rebuild just what we need for the breadcrumb.
1773 * All pending requests for this context will be zapped, and any
1774 * future request will be after userspace has had the opportunity
1775 * to recreate its own state.
1776 */
1777 if (scrub)
1778 lrc_init_regs(ce, engine, true);
1779
1780 /* Rerun the request; its payload has been neutered (if guilty). */
1781 lrc_update_regs(ce, engine, head);
1782 }
1783
guc_engine_reset_prepare(struct intel_engine_cs * engine)1784 static void guc_engine_reset_prepare(struct intel_engine_cs *engine)
1785 {
1786 /*
1787 * Wa_22011802037: In addition to stopping the cs, we need
1788 * to wait for any pending mi force wakeups
1789 */
1790 if (intel_engine_reset_needs_wa_22011802037(engine->gt)) {
1791 intel_engine_stop_cs(engine);
1792 intel_engine_wait_for_pending_mi_fw(engine);
1793 }
1794 }
1795
guc_reset_nop(struct intel_engine_cs * engine)1796 static void guc_reset_nop(struct intel_engine_cs *engine)
1797 {
1798 }
1799
guc_rewind_nop(struct intel_engine_cs * engine,bool stalled)1800 static void guc_rewind_nop(struct intel_engine_cs *engine, bool stalled)
1801 {
1802 }
1803
1804 static void
__unwind_incomplete_requests(struct intel_context * ce)1805 __unwind_incomplete_requests(struct intel_context *ce)
1806 {
1807 struct i915_request *rq, *rn;
1808 struct list_head *pl;
1809 int prio = I915_PRIORITY_INVALID;
1810 struct i915_sched_engine * const sched_engine =
1811 ce->engine->sched_engine;
1812 unsigned long flags;
1813
1814 spin_lock_irqsave(&sched_engine->lock, flags);
1815 spin_lock(&ce->guc_state.lock);
1816 list_for_each_entry_safe_reverse(rq, rn,
1817 &ce->guc_state.requests,
1818 sched.link) {
1819 if (i915_request_completed(rq))
1820 continue;
1821
1822 list_del_init(&rq->sched.link);
1823 __i915_request_unsubmit(rq);
1824
1825 /* Push the request back into the queue for later resubmission. */
1826 GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
1827 if (rq_prio(rq) != prio) {
1828 prio = rq_prio(rq);
1829 pl = i915_sched_lookup_priolist(sched_engine, prio);
1830 }
1831 GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));
1832
1833 list_add(&rq->sched.link, pl);
1834 set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
1835 }
1836 spin_unlock(&ce->guc_state.lock);
1837 spin_unlock_irqrestore(&sched_engine->lock, flags);
1838 }
1839
__guc_reset_context(struct intel_context * ce,intel_engine_mask_t stalled)1840 static void __guc_reset_context(struct intel_context *ce, intel_engine_mask_t stalled)
1841 {
1842 bool guilty;
1843 struct i915_request *rq;
1844 unsigned long flags;
1845 u32 head;
1846 int i, number_children = ce->parallel.number_children;
1847 struct intel_context *parent = ce;
1848
1849 GEM_BUG_ON(intel_context_is_child(ce));
1850
1851 intel_context_get(ce);
1852
1853 /*
1854 * GuC will implicitly mark the context as non-schedulable when it sends
1855 * the reset notification. Make sure our state reflects this change. The
1856 * context will be marked enabled on resubmission.
1857 */
1858 spin_lock_irqsave(&ce->guc_state.lock, flags);
1859 clr_context_enabled(ce);
1860 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
1861
1862 /*
1863 * For each context in the relationship find the hanging request
1864 * resetting each context / request as needed
1865 */
1866 for (i = 0; i < number_children + 1; ++i) {
1867 if (!intel_context_is_pinned(ce))
1868 goto next_context;
1869
1870 guilty = false;
1871 rq = intel_context_get_active_request(ce);
1872 if (!rq) {
1873 head = ce->ring->tail;
1874 goto out_replay;
1875 }
1876
1877 if (i915_request_started(rq))
1878 guilty = stalled & ce->engine->mask;
1879
1880 GEM_BUG_ON(i915_active_is_idle(&ce->active));
1881 head = intel_ring_wrap(ce->ring, rq->head);
1882
1883 __i915_request_reset(rq, guilty);
1884 i915_request_put(rq);
1885 out_replay:
1886 guc_reset_state(ce, head, guilty);
1887 next_context:
1888 if (i != number_children)
1889 ce = list_next_entry(ce, parallel.child_link);
1890 }
1891
1892 __unwind_incomplete_requests(parent);
1893 intel_context_put(parent);
1894 }
1895
wake_up_all_tlb_invalidate(struct intel_guc * guc)1896 void wake_up_all_tlb_invalidate(struct intel_guc *guc)
1897 {
1898 struct intel_guc_tlb_wait *wait;
1899 unsigned long i;
1900
1901 if (!intel_guc_tlb_invalidation_is_available(guc))
1902 return;
1903
1904 xa_lock_irq(&guc->tlb_lookup);
1905 xa_for_each(&guc->tlb_lookup, i, wait)
1906 wake_up(&wait->wq);
1907 xa_unlock_irq(&guc->tlb_lookup);
1908 }
1909
intel_guc_submission_reset(struct intel_guc * guc,intel_engine_mask_t stalled)1910 void intel_guc_submission_reset(struct intel_guc *guc, intel_engine_mask_t stalled)
1911 {
1912 struct intel_context *ce;
1913 unsigned long index;
1914 unsigned long flags;
1915
1916 if (unlikely(!guc_submission_initialized(guc))) {
1917 /* Reset called during driver load? GuC not yet initialised! */
1918 return;
1919 }
1920
1921 xa_lock_irqsave(&guc->context_lookup, flags);
1922 xa_for_each(&guc->context_lookup, index, ce) {
1923 if (!kref_get_unless_zero(&ce->ref))
1924 continue;
1925
1926 xa_unlock(&guc->context_lookup);
1927
1928 if (intel_context_is_pinned(ce) &&
1929 !intel_context_is_child(ce))
1930 __guc_reset_context(ce, stalled);
1931
1932 intel_context_put(ce);
1933
1934 xa_lock(&guc->context_lookup);
1935 }
1936 xa_unlock_irqrestore(&guc->context_lookup, flags);
1937
1938 /* GuC is blown away, drop all references to contexts */
1939 xa_destroy(&guc->context_lookup);
1940 }
1941
guc_cancel_context_requests(struct intel_context * ce)1942 static void guc_cancel_context_requests(struct intel_context *ce)
1943 {
1944 struct i915_sched_engine *sched_engine = ce_to_guc(ce)->sched_engine;
1945 struct i915_request *rq;
1946 unsigned long flags;
1947
1948 /* Mark all executing requests as skipped. */
1949 spin_lock_irqsave(&sched_engine->lock, flags);
1950 spin_lock(&ce->guc_state.lock);
1951 list_for_each_entry(rq, &ce->guc_state.requests, sched.link)
1952 i915_request_put(i915_request_mark_eio(rq));
1953 spin_unlock(&ce->guc_state.lock);
1954 spin_unlock_irqrestore(&sched_engine->lock, flags);
1955 }
1956
1957 static void
guc_cancel_sched_engine_requests(struct i915_sched_engine * sched_engine)1958 guc_cancel_sched_engine_requests(struct i915_sched_engine *sched_engine)
1959 {
1960 struct i915_request *rq, *rn;
1961 struct rb_node *rb;
1962 unsigned long flags;
1963
1964 /* Can be called during boot if GuC fails to load */
1965 if (!sched_engine)
1966 return;
1967
1968 /*
1969 * Before we call engine->cancel_requests(), we should have exclusive
1970 * access to the submission state. This is arranged for us by the
1971 * caller disabling the interrupt generation, the tasklet and other
1972 * threads that may then access the same state, giving us a free hand
1973 * to reset state. However, we still need to let lockdep be aware that
1974 * we know this state may be accessed in hardirq context, so we
1975 * disable the irq around this manipulation and we want to keep
1976 * the spinlock focused on its duties and not accidentally conflate
1977 * coverage to the submission's irq state. (Similarly, although we
1978 * shouldn't need to disable irq around the manipulation of the
1979 * submission's irq state, we also wish to remind ourselves that
1980 * it is irq state.)
1981 */
1982 spin_lock_irqsave(&sched_engine->lock, flags);
1983
1984 /* Flush the queued requests to the timeline list (for retiring). */
1985 while ((rb = rb_first_cached(&sched_engine->queue))) {
1986 struct i915_priolist *p = to_priolist(rb);
1987
1988 priolist_for_each_request_consume(rq, rn, p) {
1989 list_del_init(&rq->sched.link);
1990
1991 __i915_request_submit(rq);
1992
1993 i915_request_put(i915_request_mark_eio(rq));
1994 }
1995
1996 rb_erase_cached(&p->node, &sched_engine->queue);
1997 i915_priolist_free(p);
1998 }
1999
2000 /* Remaining _unready_ requests will be nop'ed when submitted */
2001
2002 sched_engine->queue_priority_hint = INT_MIN;
2003 sched_engine->queue = RB_ROOT_CACHED;
2004
2005 spin_unlock_irqrestore(&sched_engine->lock, flags);
2006 }
2007
intel_guc_submission_cancel_requests(struct intel_guc * guc)2008 void intel_guc_submission_cancel_requests(struct intel_guc *guc)
2009 {
2010 struct intel_context *ce;
2011 unsigned long index;
2012 unsigned long flags;
2013
2014 xa_lock_irqsave(&guc->context_lookup, flags);
2015 xa_for_each(&guc->context_lookup, index, ce) {
2016 if (!kref_get_unless_zero(&ce->ref))
2017 continue;
2018
2019 xa_unlock(&guc->context_lookup);
2020
2021 if (intel_context_is_pinned(ce) &&
2022 !intel_context_is_child(ce))
2023 guc_cancel_context_requests(ce);
2024
2025 intel_context_put(ce);
2026
2027 xa_lock(&guc->context_lookup);
2028 }
2029 xa_unlock_irqrestore(&guc->context_lookup, flags);
2030
2031 guc_cancel_sched_engine_requests(guc->sched_engine);
2032
2033 /* GuC is blown away, drop all references to contexts */
2034 xa_destroy(&guc->context_lookup);
2035
2036 /*
2037 * Wedged GT won't respond to any TLB invalidation request. Simply
2038 * release all the blocked waiters.
2039 */
2040 wake_up_all_tlb_invalidate(guc);
2041 }
2042
intel_guc_submission_reset_finish(struct intel_guc * guc)2043 void intel_guc_submission_reset_finish(struct intel_guc *guc)
2044 {
2045 /* Reset called during driver load or during wedge? */
2046 if (unlikely(!guc_submission_initialized(guc) ||
2047 !intel_guc_is_fw_running(guc) ||
2048 intel_gt_is_wedged(guc_to_gt(guc)))) {
2049 return;
2050 }
2051
2052 /*
2053 * Technically possible for either of these values to be non-zero here,
2054 * but very unlikely + harmless. Regardless let's add an error so we can
2055 * see in CI if this happens frequently / a precursor to taking down the
2056 * machine.
2057 */
2058 if (atomic_read(&guc->outstanding_submission_g2h))
2059 guc_err(guc, "Unexpected outstanding GuC to Host in reset finish\n");
2060 atomic_set(&guc->outstanding_submission_g2h, 0);
2061
2062 intel_guc_global_policies_update(guc);
2063 enable_submission(guc);
2064 intel_gt_unpark_heartbeats(guc_to_gt(guc));
2065
2066 /*
2067 * The full GT reset will have cleared the TLB caches and flushed the
2068 * G2H message queue; we can release all the blocked waiters.
2069 */
2070 wake_up_all_tlb_invalidate(guc);
2071 }
2072
2073 static void destroyed_worker_func(struct work_struct *w);
2074 static void reset_fail_worker_func(struct work_struct *w);
2075
intel_guc_tlb_invalidation_is_available(struct intel_guc * guc)2076 bool intel_guc_tlb_invalidation_is_available(struct intel_guc *guc)
2077 {
2078 return HAS_GUC_TLB_INVALIDATION(guc_to_gt(guc)->i915) &&
2079 intel_guc_is_ready(guc);
2080 }
2081
init_tlb_lookup(struct intel_guc * guc)2082 static int init_tlb_lookup(struct intel_guc *guc)
2083 {
2084 struct intel_guc_tlb_wait *wait;
2085 int err;
2086
2087 if (!HAS_GUC_TLB_INVALIDATION(guc_to_gt(guc)->i915))
2088 return 0;
2089
2090 xa_init_flags(&guc->tlb_lookup, XA_FLAGS_ALLOC);
2091
2092 wait = kzalloc(sizeof(*wait), GFP_KERNEL);
2093 if (!wait)
2094 return -ENOMEM;
2095
2096 init_waitqueue_head(&wait->wq);
2097
2098 /* Preallocate a shared id for use under memory pressure. */
2099 err = xa_alloc_cyclic_irq(&guc->tlb_lookup, &guc->serial_slot, wait,
2100 xa_limit_32b, &guc->next_seqno, GFP_KERNEL);
2101 if (err < 0) {
2102 kfree(wait);
2103 return err;
2104 }
2105
2106 return 0;
2107 }
2108
fini_tlb_lookup(struct intel_guc * guc)2109 static void fini_tlb_lookup(struct intel_guc *guc)
2110 {
2111 struct intel_guc_tlb_wait *wait;
2112
2113 if (!HAS_GUC_TLB_INVALIDATION(guc_to_gt(guc)->i915))
2114 return;
2115
2116 wait = xa_load(&guc->tlb_lookup, guc->serial_slot);
2117 if (wait && wait->busy)
2118 guc_err(guc, "Unexpected busy item in tlb_lookup on fini\n");
2119 kfree(wait);
2120
2121 xa_destroy(&guc->tlb_lookup);
2122 }
2123
2124 /*
2125 * Set up the memory resources to be shared with the GuC (via the GGTT)
2126 * at firmware loading time.
2127 */
intel_guc_submission_init(struct intel_guc * guc)2128 int intel_guc_submission_init(struct intel_guc *guc)
2129 {
2130 struct intel_gt *gt = guc_to_gt(guc);
2131 int ret;
2132
2133 if (guc->submission_initialized)
2134 return 0;
2135
2136 if (GUC_SUBMIT_VER(guc) < MAKE_GUC_VER(1, 0, 0)) {
2137 ret = guc_lrc_desc_pool_create_v69(guc);
2138 if (ret)
2139 return ret;
2140 }
2141
2142 ret = init_tlb_lookup(guc);
2143 if (ret)
2144 goto destroy_pool;
2145
2146 guc->submission_state.guc_ids_bitmap =
2147 bitmap_zalloc(NUMBER_MULTI_LRC_GUC_ID(guc), GFP_KERNEL);
2148 if (!guc->submission_state.guc_ids_bitmap) {
2149 ret = -ENOMEM;
2150 goto destroy_tlb;
2151 }
2152
2153 guc->timestamp.ping_delay = (POLL_TIME_CLKS / gt->clock_frequency + 1) * HZ;
2154 guc->timestamp.shift = gpm_timestamp_shift(gt);
2155 guc->submission_initialized = true;
2156
2157 return 0;
2158
2159 destroy_tlb:
2160 fini_tlb_lookup(guc);
2161 destroy_pool:
2162 guc_lrc_desc_pool_destroy_v69(guc);
2163 return ret;
2164 }
2165
intel_guc_submission_fini(struct intel_guc * guc)2166 void intel_guc_submission_fini(struct intel_guc *guc)
2167 {
2168 if (!guc->submission_initialized)
2169 return;
2170
2171 guc_fini_engine_stats(guc);
2172 guc_flush_destroyed_contexts(guc);
2173 guc_lrc_desc_pool_destroy_v69(guc);
2174 i915_sched_engine_put(guc->sched_engine);
2175 bitmap_free(guc->submission_state.guc_ids_bitmap);
2176 fini_tlb_lookup(guc);
2177 guc->submission_initialized = false;
2178 }
2179
queue_request(struct i915_sched_engine * sched_engine,struct i915_request * rq,int prio)2180 static inline void queue_request(struct i915_sched_engine *sched_engine,
2181 struct i915_request *rq,
2182 int prio)
2183 {
2184 GEM_BUG_ON(!list_empty(&rq->sched.link));
2185 list_add_tail(&rq->sched.link,
2186 i915_sched_lookup_priolist(sched_engine, prio));
2187 set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
2188 tasklet_hi_schedule(&sched_engine->tasklet);
2189 }
2190
guc_bypass_tasklet_submit(struct intel_guc * guc,struct i915_request * rq)2191 static int guc_bypass_tasklet_submit(struct intel_guc *guc,
2192 struct i915_request *rq)
2193 {
2194 int ret = 0;
2195
2196 __i915_request_submit(rq);
2197
2198 trace_i915_request_in(rq, 0);
2199
2200 if (is_multi_lrc_rq(rq)) {
2201 if (multi_lrc_submit(rq)) {
2202 ret = guc_wq_item_append(guc, rq);
2203 if (!ret)
2204 ret = guc_add_request(guc, rq);
2205 }
2206 } else {
2207 guc_set_lrc_tail(rq);
2208 ret = guc_add_request(guc, rq);
2209 }
2210
2211 if (unlikely(ret == -EPIPE))
2212 disable_submission(guc);
2213
2214 return ret;
2215 }
2216
need_tasklet(struct intel_guc * guc,struct i915_request * rq)2217 static bool need_tasklet(struct intel_guc *guc, struct i915_request *rq)
2218 {
2219 struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
2220 struct intel_context *ce = request_to_scheduling_context(rq);
2221
2222 return submission_disabled(guc) || guc->stalled_request ||
2223 !i915_sched_engine_is_empty(sched_engine) ||
2224 !ctx_id_mapped(guc, ce->guc_id.id);
2225 }
2226
guc_submit_request(struct i915_request * rq)2227 static void guc_submit_request(struct i915_request *rq)
2228 {
2229 struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
2230 struct intel_guc *guc = gt_to_guc(rq->engine->gt);
2231 unsigned long flags;
2232
2233 /* Will be called from irq-context when using foreign fences. */
2234 spin_lock_irqsave(&sched_engine->lock, flags);
2235
2236 if (need_tasklet(guc, rq))
2237 queue_request(sched_engine, rq, rq_prio(rq));
2238 else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
2239 tasklet_hi_schedule(&sched_engine->tasklet);
2240
2241 spin_unlock_irqrestore(&sched_engine->lock, flags);
2242 }
2243
new_guc_id(struct intel_guc * guc,struct intel_context * ce)2244 static int new_guc_id(struct intel_guc *guc, struct intel_context *ce)
2245 {
2246 int ret;
2247
2248 GEM_BUG_ON(intel_context_is_child(ce));
2249
2250 if (intel_context_is_parent(ce))
2251 ret = bitmap_find_free_region(guc->submission_state.guc_ids_bitmap,
2252 NUMBER_MULTI_LRC_GUC_ID(guc),
2253 order_base_2(ce->parallel.number_children
2254 + 1));
2255 else
2256 ret = ida_alloc_range(&guc->submission_state.guc_ids,
2257 NUMBER_MULTI_LRC_GUC_ID(guc),
2258 guc->submission_state.num_guc_ids - 1,
2259 GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
2260 if (unlikely(ret < 0))
2261 return ret;
2262
2263 if (!intel_context_is_parent(ce))
2264 ++guc->submission_state.guc_ids_in_use;
2265
2266 ce->guc_id.id = ret;
2267 return 0;
2268 }
2269
__release_guc_id(struct intel_guc * guc,struct intel_context * ce)2270 static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce)
2271 {
2272 GEM_BUG_ON(intel_context_is_child(ce));
2273
2274 if (!context_guc_id_invalid(ce)) {
2275 if (intel_context_is_parent(ce)) {
2276 bitmap_release_region(guc->submission_state.guc_ids_bitmap,
2277 ce->guc_id.id,
2278 order_base_2(ce->parallel.number_children
2279 + 1));
2280 } else {
2281 --guc->submission_state.guc_ids_in_use;
2282 ida_free(&guc->submission_state.guc_ids,
2283 ce->guc_id.id);
2284 }
2285 clr_ctx_id_mapping(guc, ce->guc_id.id);
2286 set_context_guc_id_invalid(ce);
2287 }
2288 if (!list_empty(&ce->guc_id.link))
2289 list_del_init(&ce->guc_id.link);
2290 }
2291
release_guc_id(struct intel_guc * guc,struct intel_context * ce)2292 static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
2293 {
2294 unsigned long flags;
2295
2296 spin_lock_irqsave(&guc->submission_state.lock, flags);
2297 __release_guc_id(guc, ce);
2298 spin_unlock_irqrestore(&guc->submission_state.lock, flags);
2299 }
2300
steal_guc_id(struct intel_guc * guc,struct intel_context * ce)2301 static int steal_guc_id(struct intel_guc *guc, struct intel_context *ce)
2302 {
2303 struct intel_context *cn;
2304
2305 lockdep_assert_held(&guc->submission_state.lock);
2306 GEM_BUG_ON(intel_context_is_child(ce));
2307 GEM_BUG_ON(intel_context_is_parent(ce));
2308
2309 if (!list_empty(&guc->submission_state.guc_id_list)) {
2310 cn = list_first_entry(&guc->submission_state.guc_id_list,
2311 struct intel_context,
2312 guc_id.link);
2313
2314 GEM_BUG_ON(atomic_read(&cn->guc_id.ref));
2315 GEM_BUG_ON(context_guc_id_invalid(cn));
2316 GEM_BUG_ON(intel_context_is_child(cn));
2317 GEM_BUG_ON(intel_context_is_parent(cn));
2318
2319 list_del_init(&cn->guc_id.link);
2320 ce->guc_id.id = cn->guc_id.id;
2321
2322 spin_lock(&cn->guc_state.lock);
2323 clr_context_registered(cn);
2324 spin_unlock(&cn->guc_state.lock);
2325
2326 set_context_guc_id_invalid(cn);
2327
2328 #ifdef CONFIG_DRM_I915_SELFTEST
2329 guc->number_guc_id_stolen++;
2330 #endif
2331
2332 return 0;
2333 } else {
2334 return -EAGAIN;
2335 }
2336 }
2337
assign_guc_id(struct intel_guc * guc,struct intel_context * ce)2338 static int assign_guc_id(struct intel_guc *guc, struct intel_context *ce)
2339 {
2340 int ret;
2341
2342 lockdep_assert_held(&guc->submission_state.lock);
2343 GEM_BUG_ON(intel_context_is_child(ce));
2344
2345 ret = new_guc_id(guc, ce);
2346 if (unlikely(ret < 0)) {
2347 if (intel_context_is_parent(ce))
2348 return -ENOSPC;
2349
2350 ret = steal_guc_id(guc, ce);
2351 if (ret < 0)
2352 return ret;
2353 }
2354
2355 if (intel_context_is_parent(ce)) {
2356 struct intel_context *child;
2357 int i = 1;
2358
2359 for_each_child(ce, child)
2360 child->guc_id.id = ce->guc_id.id + i++;
2361 }
2362
2363 return 0;
2364 }
2365
2366 #define PIN_GUC_ID_TRIES 4
pin_guc_id(struct intel_guc * guc,struct intel_context * ce)2367 static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
2368 {
2369 int ret = 0;
2370 unsigned long flags, tries = PIN_GUC_ID_TRIES;
2371
2372 GEM_BUG_ON(atomic_read(&ce->guc_id.ref));
2373
2374 try_again:
2375 spin_lock_irqsave(&guc->submission_state.lock, flags);
2376
2377 might_lock(&ce->guc_state.lock);
2378
2379 if (context_guc_id_invalid(ce)) {
2380 ret = assign_guc_id(guc, ce);
2381 if (ret)
2382 goto out_unlock;
2383 ret = 1; /* Indidcates newly assigned guc_id */
2384 }
2385 if (!list_empty(&ce->guc_id.link))
2386 list_del_init(&ce->guc_id.link);
2387 atomic_inc(&ce->guc_id.ref);
2388
2389 out_unlock:
2390 spin_unlock_irqrestore(&guc->submission_state.lock, flags);
2391
2392 /*
2393 * -EAGAIN indicates no guc_id are available, let's retire any
2394 * outstanding requests to see if that frees up a guc_id. If the first
2395 * retire didn't help, insert a sleep with the timeslice duration before
2396 * attempting to retire more requests. Double the sleep period each
2397 * subsequent pass before finally giving up. The sleep period has max of
2398 * 100ms and minimum of 1ms.
2399 */
2400 if (ret == -EAGAIN && --tries) {
2401 if (PIN_GUC_ID_TRIES - tries > 1) {
2402 unsigned int timeslice_shifted =
2403 ce->engine->props.timeslice_duration_ms <<
2404 (PIN_GUC_ID_TRIES - tries - 2);
2405 unsigned int max = min_t(unsigned int, 100,
2406 timeslice_shifted);
2407
2408 msleep(max_t(unsigned int, max, 1));
2409 }
2410 intel_gt_retire_requests(guc_to_gt(guc));
2411 goto try_again;
2412 }
2413
2414 return ret;
2415 }
2416
unpin_guc_id(struct intel_guc * guc,struct intel_context * ce)2417 static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
2418 {
2419 unsigned long flags;
2420
2421 GEM_BUG_ON(atomic_read(&ce->guc_id.ref) < 0);
2422 GEM_BUG_ON(intel_context_is_child(ce));
2423
2424 if (unlikely(context_guc_id_invalid(ce) ||
2425 intel_context_is_parent(ce)))
2426 return;
2427
2428 spin_lock_irqsave(&guc->submission_state.lock, flags);
2429 if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id.link) &&
2430 !atomic_read(&ce->guc_id.ref))
2431 list_add_tail(&ce->guc_id.link,
2432 &guc->submission_state.guc_id_list);
2433 spin_unlock_irqrestore(&guc->submission_state.lock, flags);
2434 }
2435
__guc_action_register_multi_lrc_v69(struct intel_guc * guc,struct intel_context * ce,u32 guc_id,u32 offset,bool loop)2436 static int __guc_action_register_multi_lrc_v69(struct intel_guc *guc,
2437 struct intel_context *ce,
2438 u32 guc_id,
2439 u32 offset,
2440 bool loop)
2441 {
2442 struct intel_context *child;
2443 u32 action[4 + MAX_ENGINE_INSTANCE];
2444 int len = 0;
2445
2446 GEM_BUG_ON(ce->parallel.number_children > MAX_ENGINE_INSTANCE);
2447
2448 action[len++] = INTEL_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC;
2449 action[len++] = guc_id;
2450 action[len++] = ce->parallel.number_children + 1;
2451 action[len++] = offset;
2452 for_each_child(ce, child) {
2453 offset += sizeof(struct guc_lrc_desc_v69);
2454 action[len++] = offset;
2455 }
2456
2457 return guc_submission_send_busy_loop(guc, action, len, 0, loop);
2458 }
2459
__guc_action_register_multi_lrc_v70(struct intel_guc * guc,struct intel_context * ce,struct guc_ctxt_registration_info * info,bool loop)2460 static int __guc_action_register_multi_lrc_v70(struct intel_guc *guc,
2461 struct intel_context *ce,
2462 struct guc_ctxt_registration_info *info,
2463 bool loop)
2464 {
2465 struct intel_context *child;
2466 u32 action[13 + (MAX_ENGINE_INSTANCE * 2)];
2467 int len = 0;
2468 u32 next_id;
2469
2470 GEM_BUG_ON(ce->parallel.number_children > MAX_ENGINE_INSTANCE);
2471
2472 action[len++] = INTEL_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC;
2473 action[len++] = info->flags;
2474 action[len++] = info->context_idx;
2475 action[len++] = info->engine_class;
2476 action[len++] = info->engine_submit_mask;
2477 action[len++] = info->wq_desc_lo;
2478 action[len++] = info->wq_desc_hi;
2479 action[len++] = info->wq_base_lo;
2480 action[len++] = info->wq_base_hi;
2481 action[len++] = info->wq_size;
2482 action[len++] = ce->parallel.number_children + 1;
2483 action[len++] = info->hwlrca_lo;
2484 action[len++] = info->hwlrca_hi;
2485
2486 next_id = info->context_idx + 1;
2487 for_each_child(ce, child) {
2488 GEM_BUG_ON(next_id++ != child->guc_id.id);
2489
2490 /*
2491 * NB: GuC interface supports 64 bit LRCA even though i915/HW
2492 * only supports 32 bit currently.
2493 */
2494 action[len++] = lower_32_bits(child->lrc.lrca);
2495 action[len++] = upper_32_bits(child->lrc.lrca);
2496 }
2497
2498 GEM_BUG_ON(len > ARRAY_SIZE(action));
2499
2500 return guc_submission_send_busy_loop(guc, action, len, 0, loop);
2501 }
2502
__guc_action_register_context_v69(struct intel_guc * guc,u32 guc_id,u32 offset,bool loop)2503 static int __guc_action_register_context_v69(struct intel_guc *guc,
2504 u32 guc_id,
2505 u32 offset,
2506 bool loop)
2507 {
2508 u32 action[] = {
2509 INTEL_GUC_ACTION_REGISTER_CONTEXT,
2510 guc_id,
2511 offset,
2512 };
2513
2514 return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
2515 0, loop);
2516 }
2517
__guc_action_register_context_v70(struct intel_guc * guc,struct guc_ctxt_registration_info * info,bool loop)2518 static int __guc_action_register_context_v70(struct intel_guc *guc,
2519 struct guc_ctxt_registration_info *info,
2520 bool loop)
2521 {
2522 u32 action[] = {
2523 INTEL_GUC_ACTION_REGISTER_CONTEXT,
2524 info->flags,
2525 info->context_idx,
2526 info->engine_class,
2527 info->engine_submit_mask,
2528 info->wq_desc_lo,
2529 info->wq_desc_hi,
2530 info->wq_base_lo,
2531 info->wq_base_hi,
2532 info->wq_size,
2533 info->hwlrca_lo,
2534 info->hwlrca_hi,
2535 };
2536
2537 return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
2538 0, loop);
2539 }
2540
2541 static void prepare_context_registration_info_v69(struct intel_context *ce);
2542 static void prepare_context_registration_info_v70(struct intel_context *ce,
2543 struct guc_ctxt_registration_info *info);
2544
2545 static int
register_context_v69(struct intel_guc * guc,struct intel_context * ce,bool loop)2546 register_context_v69(struct intel_guc *guc, struct intel_context *ce, bool loop)
2547 {
2548 u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool_v69) +
2549 ce->guc_id.id * sizeof(struct guc_lrc_desc_v69);
2550
2551 prepare_context_registration_info_v69(ce);
2552
2553 if (intel_context_is_parent(ce))
2554 return __guc_action_register_multi_lrc_v69(guc, ce, ce->guc_id.id,
2555 offset, loop);
2556 else
2557 return __guc_action_register_context_v69(guc, ce->guc_id.id,
2558 offset, loop);
2559 }
2560
2561 static int
register_context_v70(struct intel_guc * guc,struct intel_context * ce,bool loop)2562 register_context_v70(struct intel_guc *guc, struct intel_context *ce, bool loop)
2563 {
2564 struct guc_ctxt_registration_info info;
2565
2566 prepare_context_registration_info_v70(ce, &info);
2567
2568 if (intel_context_is_parent(ce))
2569 return __guc_action_register_multi_lrc_v70(guc, ce, &info, loop);
2570 else
2571 return __guc_action_register_context_v70(guc, &info, loop);
2572 }
2573
register_context(struct intel_context * ce,bool loop)2574 static int register_context(struct intel_context *ce, bool loop)
2575 {
2576 struct intel_guc *guc = ce_to_guc(ce);
2577 int ret;
2578
2579 GEM_BUG_ON(intel_context_is_child(ce));
2580 trace_intel_context_register(ce);
2581
2582 if (GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 0, 0))
2583 ret = register_context_v70(guc, ce, loop);
2584 else
2585 ret = register_context_v69(guc, ce, loop);
2586
2587 if (likely(!ret)) {
2588 unsigned long flags;
2589
2590 spin_lock_irqsave(&ce->guc_state.lock, flags);
2591 set_context_registered(ce);
2592 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2593
2594 if (GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 0, 0))
2595 guc_context_policy_init_v70(ce, loop);
2596 }
2597
2598 return ret;
2599 }
2600
__guc_action_deregister_context(struct intel_guc * guc,u32 guc_id)2601 static int __guc_action_deregister_context(struct intel_guc *guc,
2602 u32 guc_id)
2603 {
2604 u32 action[] = {
2605 INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
2606 guc_id,
2607 };
2608
2609 return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
2610 G2H_LEN_DW_DEREGISTER_CONTEXT,
2611 true);
2612 }
2613
deregister_context(struct intel_context * ce,u32 guc_id)2614 static int deregister_context(struct intel_context *ce, u32 guc_id)
2615 {
2616 struct intel_guc *guc = ce_to_guc(ce);
2617
2618 GEM_BUG_ON(intel_context_is_child(ce));
2619 trace_intel_context_deregister(ce);
2620
2621 return __guc_action_deregister_context(guc, guc_id);
2622 }
2623
clear_children_join_go_memory(struct intel_context * ce)2624 static inline void clear_children_join_go_memory(struct intel_context *ce)
2625 {
2626 struct parent_scratch *ps = __get_parent_scratch(ce);
2627 int i;
2628
2629 ps->go.semaphore = 0;
2630 for (i = 0; i < ce->parallel.number_children + 1; ++i)
2631 ps->join[i].semaphore = 0;
2632 }
2633
get_children_go_value(struct intel_context * ce)2634 static inline u32 get_children_go_value(struct intel_context *ce)
2635 {
2636 return __get_parent_scratch(ce)->go.semaphore;
2637 }
2638
get_children_join_value(struct intel_context * ce,u8 child_index)2639 static inline u32 get_children_join_value(struct intel_context *ce,
2640 u8 child_index)
2641 {
2642 return __get_parent_scratch(ce)->join[child_index].semaphore;
2643 }
2644
2645 struct context_policy {
2646 u32 count;
2647 struct guc_update_context_policy h2g;
2648 };
2649
__guc_context_policy_action_size(struct context_policy * policy)2650 static u32 __guc_context_policy_action_size(struct context_policy *policy)
2651 {
2652 size_t bytes = sizeof(policy->h2g.header) +
2653 (sizeof(policy->h2g.klv[0]) * policy->count);
2654
2655 return bytes / sizeof(u32);
2656 }
2657
__guc_context_policy_start_klv(struct context_policy * policy,u16 guc_id)2658 static void __guc_context_policy_start_klv(struct context_policy *policy, u16 guc_id)
2659 {
2660 policy->h2g.header.action = INTEL_GUC_ACTION_HOST2GUC_UPDATE_CONTEXT_POLICIES;
2661 policy->h2g.header.ctx_id = guc_id;
2662 policy->count = 0;
2663 }
2664
2665 #define MAKE_CONTEXT_POLICY_ADD(func, id) \
2666 static void __guc_context_policy_add_##func(struct context_policy *policy, u32 data) \
2667 { \
2668 GEM_BUG_ON(policy->count >= GUC_CONTEXT_POLICIES_KLV_NUM_IDS); \
2669 policy->h2g.klv[policy->count].kl = \
2670 FIELD_PREP(GUC_KLV_0_KEY, GUC_CONTEXT_POLICIES_KLV_ID_##id) | \
2671 FIELD_PREP(GUC_KLV_0_LEN, 1); \
2672 policy->h2g.klv[policy->count].value = data; \
2673 policy->count++; \
2674 }
2675
MAKE_CONTEXT_POLICY_ADD(execution_quantum,EXECUTION_QUANTUM)2676 MAKE_CONTEXT_POLICY_ADD(execution_quantum, EXECUTION_QUANTUM)
2677 MAKE_CONTEXT_POLICY_ADD(preemption_timeout, PREEMPTION_TIMEOUT)
2678 MAKE_CONTEXT_POLICY_ADD(priority, SCHEDULING_PRIORITY)
2679 MAKE_CONTEXT_POLICY_ADD(preempt_to_idle, PREEMPT_TO_IDLE_ON_QUANTUM_EXPIRY)
2680 MAKE_CONTEXT_POLICY_ADD(slpc_ctx_freq_req, SLPM_GT_FREQUENCY)
2681
2682 #undef MAKE_CONTEXT_POLICY_ADD
2683
2684 static int __guc_context_set_context_policies(struct intel_guc *guc,
2685 struct context_policy *policy,
2686 bool loop)
2687 {
2688 return guc_submission_send_busy_loop(guc, (u32 *)&policy->h2g,
2689 __guc_context_policy_action_size(policy),
2690 0, loop);
2691 }
2692
guc_context_policy_init_v70(struct intel_context * ce,bool loop)2693 static int guc_context_policy_init_v70(struct intel_context *ce, bool loop)
2694 {
2695 struct intel_engine_cs *engine = ce->engine;
2696 struct intel_guc *guc = gt_to_guc(engine->gt);
2697 struct context_policy policy;
2698 u32 execution_quantum;
2699 u32 preemption_timeout;
2700 u32 slpc_ctx_freq_req = 0;
2701 unsigned long flags;
2702 int ret;
2703
2704 /* NB: For both of these, zero means disabled. */
2705 GEM_BUG_ON(overflows_type(engine->props.timeslice_duration_ms * 1000,
2706 execution_quantum));
2707 GEM_BUG_ON(overflows_type(engine->props.preempt_timeout_ms * 1000,
2708 preemption_timeout));
2709 execution_quantum = engine->props.timeslice_duration_ms * 1000;
2710 preemption_timeout = engine->props.preempt_timeout_ms * 1000;
2711
2712 if (ce->flags & BIT(CONTEXT_LOW_LATENCY))
2713 slpc_ctx_freq_req |= SLPC_CTX_FREQ_REQ_IS_COMPUTE;
2714
2715 __guc_context_policy_start_klv(&policy, ce->guc_id.id);
2716
2717 __guc_context_policy_add_priority(&policy, ce->guc_state.prio);
2718 __guc_context_policy_add_execution_quantum(&policy, execution_quantum);
2719 __guc_context_policy_add_preemption_timeout(&policy, preemption_timeout);
2720 __guc_context_policy_add_slpc_ctx_freq_req(&policy, slpc_ctx_freq_req);
2721
2722 if (engine->flags & I915_ENGINE_WANT_FORCED_PREEMPTION)
2723 __guc_context_policy_add_preempt_to_idle(&policy, 1);
2724
2725 ret = __guc_context_set_context_policies(guc, &policy, loop);
2726
2727 spin_lock_irqsave(&ce->guc_state.lock, flags);
2728 if (ret != 0)
2729 set_context_policy_required(ce);
2730 else
2731 clr_context_policy_required(ce);
2732 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2733
2734 return ret;
2735 }
2736
guc_context_policy_init_v69(struct intel_engine_cs * engine,struct guc_lrc_desc_v69 * desc)2737 static void guc_context_policy_init_v69(struct intel_engine_cs *engine,
2738 struct guc_lrc_desc_v69 *desc)
2739 {
2740 desc->policy_flags = 0;
2741
2742 if (engine->flags & I915_ENGINE_WANT_FORCED_PREEMPTION)
2743 desc->policy_flags |= CONTEXT_POLICY_FLAG_PREEMPT_TO_IDLE_V69;
2744
2745 /* NB: For both of these, zero means disabled. */
2746 GEM_BUG_ON(overflows_type(engine->props.timeslice_duration_ms * 1000,
2747 desc->execution_quantum));
2748 GEM_BUG_ON(overflows_type(engine->props.preempt_timeout_ms * 1000,
2749 desc->preemption_timeout));
2750 desc->execution_quantum = engine->props.timeslice_duration_ms * 1000;
2751 desc->preemption_timeout = engine->props.preempt_timeout_ms * 1000;
2752 }
2753
map_guc_prio_to_lrc_desc_prio(u8 prio)2754 static u32 map_guc_prio_to_lrc_desc_prio(u8 prio)
2755 {
2756 /*
2757 * this matches the mapping we do in map_i915_prio_to_guc_prio()
2758 * (e.g. prio < I915_PRIORITY_NORMAL maps to GUC_CLIENT_PRIORITY_NORMAL)
2759 */
2760 switch (prio) {
2761 default:
2762 MISSING_CASE(prio);
2763 fallthrough;
2764 case GUC_CLIENT_PRIORITY_KMD_NORMAL:
2765 return GEN12_CTX_PRIORITY_NORMAL;
2766 case GUC_CLIENT_PRIORITY_NORMAL:
2767 return GEN12_CTX_PRIORITY_LOW;
2768 case GUC_CLIENT_PRIORITY_HIGH:
2769 case GUC_CLIENT_PRIORITY_KMD_HIGH:
2770 return GEN12_CTX_PRIORITY_HIGH;
2771 }
2772 }
2773
prepare_context_registration_info_v69(struct intel_context * ce)2774 static void prepare_context_registration_info_v69(struct intel_context *ce)
2775 {
2776 struct intel_engine_cs *engine = ce->engine;
2777 struct intel_guc *guc = gt_to_guc(engine->gt);
2778 u32 ctx_id = ce->guc_id.id;
2779 struct guc_lrc_desc_v69 *desc;
2780 struct intel_context *child;
2781
2782 GEM_BUG_ON(!engine->mask);
2783
2784 /*
2785 * Ensure LRC + CT vmas are is same region as write barrier is done
2786 * based on CT vma region.
2787 */
2788 GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) !=
2789 i915_gem_object_is_lmem(ce->ring->vma->obj));
2790
2791 desc = __get_lrc_desc_v69(guc, ctx_id);
2792 GEM_BUG_ON(!desc);
2793 desc->engine_class = engine_class_to_guc_class(engine->class);
2794 desc->engine_submit_mask = engine->logical_mask;
2795 desc->hw_context_desc = ce->lrc.lrca;
2796 desc->priority = ce->guc_state.prio;
2797 desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
2798 guc_context_policy_init_v69(engine, desc);
2799
2800 /*
2801 * If context is a parent, we need to register a process descriptor
2802 * describing a work queue and register all child contexts.
2803 */
2804 if (intel_context_is_parent(ce)) {
2805 struct guc_process_desc_v69 *pdesc;
2806
2807 ce->parallel.guc.wqi_tail = 0;
2808 ce->parallel.guc.wqi_head = 0;
2809
2810 desc->process_desc = i915_ggtt_offset(ce->state) +
2811 __get_parent_scratch_offset(ce);
2812 desc->wq_addr = i915_ggtt_offset(ce->state) +
2813 __get_wq_offset(ce);
2814 desc->wq_size = WQ_SIZE;
2815
2816 pdesc = __get_process_desc_v69(ce);
2817 memset(pdesc, 0, sizeof(*(pdesc)));
2818 pdesc->stage_id = ce->guc_id.id;
2819 pdesc->wq_base_addr = desc->wq_addr;
2820 pdesc->wq_size_bytes = desc->wq_size;
2821 pdesc->wq_status = WQ_STATUS_ACTIVE;
2822
2823 ce->parallel.guc.wq_head = &pdesc->head;
2824 ce->parallel.guc.wq_tail = &pdesc->tail;
2825 ce->parallel.guc.wq_status = &pdesc->wq_status;
2826
2827 for_each_child(ce, child) {
2828 desc = __get_lrc_desc_v69(guc, child->guc_id.id);
2829
2830 desc->engine_class =
2831 engine_class_to_guc_class(engine->class);
2832 desc->hw_context_desc = child->lrc.lrca;
2833 desc->priority = ce->guc_state.prio;
2834 desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
2835 guc_context_policy_init_v69(engine, desc);
2836 }
2837
2838 clear_children_join_go_memory(ce);
2839 }
2840 }
2841
prepare_context_registration_info_v70(struct intel_context * ce,struct guc_ctxt_registration_info * info)2842 static void prepare_context_registration_info_v70(struct intel_context *ce,
2843 struct guc_ctxt_registration_info *info)
2844 {
2845 struct intel_engine_cs *engine = ce->engine;
2846 struct intel_guc *guc = gt_to_guc(engine->gt);
2847 u32 ctx_id = ce->guc_id.id;
2848
2849 GEM_BUG_ON(!engine->mask);
2850
2851 /*
2852 * Ensure LRC + CT vmas are is same region as write barrier is done
2853 * based on CT vma region.
2854 */
2855 GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) !=
2856 i915_gem_object_is_lmem(ce->ring->vma->obj));
2857
2858 memset(info, 0, sizeof(*info));
2859 info->context_idx = ctx_id;
2860 info->engine_class = engine_class_to_guc_class(engine->class);
2861 info->engine_submit_mask = engine->logical_mask;
2862 /*
2863 * NB: GuC interface supports 64 bit LRCA even though i915/HW
2864 * only supports 32 bit currently.
2865 */
2866 info->hwlrca_lo = lower_32_bits(ce->lrc.lrca);
2867 info->hwlrca_hi = upper_32_bits(ce->lrc.lrca);
2868 if (engine->flags & I915_ENGINE_HAS_EU_PRIORITY)
2869 info->hwlrca_lo |= map_guc_prio_to_lrc_desc_prio(ce->guc_state.prio);
2870 info->flags = CONTEXT_REGISTRATION_FLAG_KMD;
2871
2872 /*
2873 * If context is a parent, we need to register a process descriptor
2874 * describing a work queue and register all child contexts.
2875 */
2876 if (intel_context_is_parent(ce)) {
2877 struct guc_sched_wq_desc *wq_desc;
2878 u64 wq_desc_offset, wq_base_offset;
2879
2880 ce->parallel.guc.wqi_tail = 0;
2881 ce->parallel.guc.wqi_head = 0;
2882
2883 wq_desc_offset = (u64)i915_ggtt_offset(ce->state) +
2884 __get_parent_scratch_offset(ce);
2885 wq_base_offset = (u64)i915_ggtt_offset(ce->state) +
2886 __get_wq_offset(ce);
2887 info->wq_desc_lo = lower_32_bits(wq_desc_offset);
2888 info->wq_desc_hi = upper_32_bits(wq_desc_offset);
2889 info->wq_base_lo = lower_32_bits(wq_base_offset);
2890 info->wq_base_hi = upper_32_bits(wq_base_offset);
2891 info->wq_size = WQ_SIZE;
2892
2893 wq_desc = __get_wq_desc_v70(ce);
2894 memset(wq_desc, 0, sizeof(*wq_desc));
2895 wq_desc->wq_status = WQ_STATUS_ACTIVE;
2896
2897 ce->parallel.guc.wq_head = &wq_desc->head;
2898 ce->parallel.guc.wq_tail = &wq_desc->tail;
2899 ce->parallel.guc.wq_status = &wq_desc->wq_status;
2900
2901 clear_children_join_go_memory(ce);
2902 }
2903 }
2904
try_context_registration(struct intel_context * ce,bool loop)2905 static int try_context_registration(struct intel_context *ce, bool loop)
2906 {
2907 struct intel_engine_cs *engine = ce->engine;
2908 struct intel_runtime_pm *runtime_pm = engine->uncore->rpm;
2909 struct intel_guc *guc = gt_to_guc(engine->gt);
2910 intel_wakeref_t wakeref;
2911 u32 ctx_id = ce->guc_id.id;
2912 bool context_registered;
2913 int ret = 0;
2914
2915 GEM_BUG_ON(!sched_state_is_init(ce));
2916
2917 context_registered = ctx_id_mapped(guc, ctx_id);
2918
2919 clr_ctx_id_mapping(guc, ctx_id);
2920 set_ctx_id_mapping(guc, ctx_id, ce);
2921
2922 /*
2923 * The context_lookup xarray is used to determine if the hardware
2924 * context is currently registered. There are two cases in which it
2925 * could be registered either the guc_id has been stolen from another
2926 * context or the lrc descriptor address of this context has changed. In
2927 * either case the context needs to be deregistered with the GuC before
2928 * registering this context.
2929 */
2930 if (context_registered) {
2931 bool disabled;
2932 unsigned long flags;
2933
2934 trace_intel_context_steal_guc_id(ce);
2935 GEM_BUG_ON(!loop);
2936
2937 /* Seal race with Reset */
2938 spin_lock_irqsave(&ce->guc_state.lock, flags);
2939 disabled = submission_disabled(guc);
2940 if (likely(!disabled)) {
2941 set_context_wait_for_deregister_to_register(ce);
2942 intel_context_get(ce);
2943 }
2944 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
2945 if (unlikely(disabled)) {
2946 clr_ctx_id_mapping(guc, ctx_id);
2947 return 0; /* Will get registered later */
2948 }
2949
2950 /*
2951 * If stealing the guc_id, this ce has the same guc_id as the
2952 * context whose guc_id was stolen.
2953 */
2954 with_intel_runtime_pm(runtime_pm, wakeref)
2955 ret = deregister_context(ce, ce->guc_id.id);
2956 if (unlikely(ret == -ENODEV))
2957 ret = 0; /* Will get registered later */
2958 } else {
2959 with_intel_runtime_pm(runtime_pm, wakeref)
2960 ret = register_context(ce, loop);
2961 if (unlikely(ret == -EBUSY)) {
2962 clr_ctx_id_mapping(guc, ctx_id);
2963 } else if (unlikely(ret == -ENODEV)) {
2964 clr_ctx_id_mapping(guc, ctx_id);
2965 ret = 0; /* Will get registered later */
2966 }
2967 }
2968
2969 return ret;
2970 }
2971
__guc_context_pre_pin(struct intel_context * ce,struct intel_engine_cs * engine,struct i915_gem_ww_ctx * ww,void ** vaddr)2972 static int __guc_context_pre_pin(struct intel_context *ce,
2973 struct intel_engine_cs *engine,
2974 struct i915_gem_ww_ctx *ww,
2975 void **vaddr)
2976 {
2977 return lrc_pre_pin(ce, engine, ww, vaddr);
2978 }
2979
__guc_context_pin(struct intel_context * ce,struct intel_engine_cs * engine,void * vaddr)2980 static int __guc_context_pin(struct intel_context *ce,
2981 struct intel_engine_cs *engine,
2982 void *vaddr)
2983 {
2984 if (i915_ggtt_offset(ce->state) !=
2985 (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK))
2986 set_bit(CONTEXT_LRCA_DIRTY, &ce->flags);
2987
2988 /*
2989 * GuC context gets pinned in guc_request_alloc. See that function for
2990 * explaination of why.
2991 */
2992
2993 return lrc_pin(ce, engine, vaddr);
2994 }
2995
guc_context_pre_pin(struct intel_context * ce,struct i915_gem_ww_ctx * ww,void ** vaddr)2996 static int guc_context_pre_pin(struct intel_context *ce,
2997 struct i915_gem_ww_ctx *ww,
2998 void **vaddr)
2999 {
3000 return __guc_context_pre_pin(ce, ce->engine, ww, vaddr);
3001 }
3002
guc_context_pin(struct intel_context * ce,void * vaddr)3003 static int guc_context_pin(struct intel_context *ce, void *vaddr)
3004 {
3005 int ret = __guc_context_pin(ce, ce->engine, vaddr);
3006
3007 if (likely(!ret && !intel_context_is_barrier(ce)))
3008 intel_engine_pm_get(ce->engine);
3009
3010 return ret;
3011 }
3012
guc_context_unpin(struct intel_context * ce)3013 static void guc_context_unpin(struct intel_context *ce)
3014 {
3015 struct intel_guc *guc = ce_to_guc(ce);
3016
3017 __guc_context_update_stats(ce);
3018 unpin_guc_id(guc, ce);
3019 lrc_unpin(ce);
3020
3021 if (likely(!intel_context_is_barrier(ce)))
3022 intel_engine_pm_put_async(ce->engine);
3023 }
3024
guc_context_post_unpin(struct intel_context * ce)3025 static void guc_context_post_unpin(struct intel_context *ce)
3026 {
3027 lrc_post_unpin(ce);
3028 }
3029
__guc_context_sched_enable(struct intel_guc * guc,struct intel_context * ce)3030 static void __guc_context_sched_enable(struct intel_guc *guc,
3031 struct intel_context *ce)
3032 {
3033 u32 action[] = {
3034 INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET,
3035 ce->guc_id.id,
3036 GUC_CONTEXT_ENABLE
3037 };
3038
3039 trace_intel_context_sched_enable(ce);
3040
3041 guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
3042 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
3043 }
3044
__guc_context_sched_disable(struct intel_guc * guc,struct intel_context * ce,u16 guc_id)3045 static void __guc_context_sched_disable(struct intel_guc *guc,
3046 struct intel_context *ce,
3047 u16 guc_id)
3048 {
3049 u32 action[] = {
3050 INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET,
3051 guc_id, /* ce->guc_id.id not stable */
3052 GUC_CONTEXT_DISABLE
3053 };
3054
3055 GEM_BUG_ON(guc_id == GUC_INVALID_CONTEXT_ID);
3056
3057 GEM_BUG_ON(intel_context_is_child(ce));
3058 trace_intel_context_sched_disable(ce);
3059
3060 guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
3061 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
3062 }
3063
guc_blocked_fence_complete(struct intel_context * ce)3064 static void guc_blocked_fence_complete(struct intel_context *ce)
3065 {
3066 lockdep_assert_held(&ce->guc_state.lock);
3067
3068 if (!i915_sw_fence_done(&ce->guc_state.blocked))
3069 i915_sw_fence_complete(&ce->guc_state.blocked);
3070 }
3071
guc_blocked_fence_reinit(struct intel_context * ce)3072 static void guc_blocked_fence_reinit(struct intel_context *ce)
3073 {
3074 lockdep_assert_held(&ce->guc_state.lock);
3075 GEM_BUG_ON(!i915_sw_fence_done(&ce->guc_state.blocked));
3076
3077 /*
3078 * This fence is always complete unless a pending schedule disable is
3079 * outstanding. We arm the fence here and complete it when we receive
3080 * the pending schedule disable complete message.
3081 */
3082 i915_sw_fence_fini(&ce->guc_state.blocked);
3083 i915_sw_fence_reinit(&ce->guc_state.blocked);
3084 i915_sw_fence_await(&ce->guc_state.blocked);
3085 i915_sw_fence_commit(&ce->guc_state.blocked);
3086 }
3087
prep_context_pending_disable(struct intel_context * ce)3088 static u16 prep_context_pending_disable(struct intel_context *ce)
3089 {
3090 lockdep_assert_held(&ce->guc_state.lock);
3091
3092 set_context_pending_disable(ce);
3093 clr_context_enabled(ce);
3094 guc_blocked_fence_reinit(ce);
3095 intel_context_get(ce);
3096
3097 return ce->guc_id.id;
3098 }
3099
guc_context_block(struct intel_context * ce)3100 static struct i915_sw_fence *guc_context_block(struct intel_context *ce)
3101 {
3102 struct intel_guc *guc = ce_to_guc(ce);
3103 unsigned long flags;
3104 struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm;
3105 intel_wakeref_t wakeref;
3106 u16 guc_id;
3107 bool enabled;
3108
3109 GEM_BUG_ON(intel_context_is_child(ce));
3110
3111 spin_lock_irqsave(&ce->guc_state.lock, flags);
3112
3113 incr_context_blocked(ce);
3114
3115 enabled = context_enabled(ce);
3116 if (unlikely(!enabled || submission_disabled(guc))) {
3117 if (enabled)
3118 clr_context_enabled(ce);
3119 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3120 return &ce->guc_state.blocked;
3121 }
3122
3123 /*
3124 * We add +2 here as the schedule disable complete CTB handler calls
3125 * intel_context_sched_disable_unpin (-2 to pin_count).
3126 */
3127 atomic_add(2, &ce->pin_count);
3128
3129 guc_id = prep_context_pending_disable(ce);
3130
3131 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3132
3133 with_intel_runtime_pm(runtime_pm, wakeref)
3134 __guc_context_sched_disable(guc, ce, guc_id);
3135
3136 return &ce->guc_state.blocked;
3137 }
3138
3139 #define SCHED_STATE_MULTI_BLOCKED_MASK \
3140 (SCHED_STATE_BLOCKED_MASK & ~SCHED_STATE_BLOCKED)
3141 #define SCHED_STATE_NO_UNBLOCK \
3142 (SCHED_STATE_MULTI_BLOCKED_MASK | \
3143 SCHED_STATE_PENDING_DISABLE | \
3144 SCHED_STATE_BANNED)
3145
context_cant_unblock(struct intel_context * ce)3146 static bool context_cant_unblock(struct intel_context *ce)
3147 {
3148 lockdep_assert_held(&ce->guc_state.lock);
3149
3150 return (ce->guc_state.sched_state & SCHED_STATE_NO_UNBLOCK) ||
3151 context_guc_id_invalid(ce) ||
3152 !ctx_id_mapped(ce_to_guc(ce), ce->guc_id.id) ||
3153 !intel_context_is_pinned(ce);
3154 }
3155
guc_context_unblock(struct intel_context * ce)3156 static void guc_context_unblock(struct intel_context *ce)
3157 {
3158 struct intel_guc *guc = ce_to_guc(ce);
3159 unsigned long flags;
3160 struct intel_runtime_pm *runtime_pm = ce->engine->uncore->rpm;
3161 intel_wakeref_t wakeref;
3162 bool enable;
3163
3164 GEM_BUG_ON(context_enabled(ce));
3165 GEM_BUG_ON(intel_context_is_child(ce));
3166
3167 spin_lock_irqsave(&ce->guc_state.lock, flags);
3168
3169 if (unlikely(submission_disabled(guc) ||
3170 context_cant_unblock(ce))) {
3171 enable = false;
3172 } else {
3173 enable = true;
3174 set_context_pending_enable(ce);
3175 set_context_enabled(ce);
3176 intel_context_get(ce);
3177 }
3178
3179 decr_context_blocked(ce);
3180
3181 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3182
3183 if (enable) {
3184 with_intel_runtime_pm(runtime_pm, wakeref)
3185 __guc_context_sched_enable(guc, ce);
3186 }
3187 }
3188
guc_context_cancel_request(struct intel_context * ce,struct i915_request * rq)3189 static void guc_context_cancel_request(struct intel_context *ce,
3190 struct i915_request *rq)
3191 {
3192 struct intel_context *block_context =
3193 request_to_scheduling_context(rq);
3194
3195 if (i915_sw_fence_signaled(&rq->submit)) {
3196 struct i915_sw_fence *fence;
3197
3198 intel_context_get(ce);
3199 fence = guc_context_block(block_context);
3200 i915_sw_fence_wait(fence);
3201 if (!i915_request_completed(rq)) {
3202 __i915_request_skip(rq);
3203 guc_reset_state(ce, intel_ring_wrap(ce->ring, rq->head),
3204 true);
3205 }
3206
3207 guc_context_unblock(block_context);
3208 intel_context_put(ce);
3209 }
3210 }
3211
__guc_context_set_preemption_timeout(struct intel_guc * guc,u16 guc_id,u32 preemption_timeout)3212 static void __guc_context_set_preemption_timeout(struct intel_guc *guc,
3213 u16 guc_id,
3214 u32 preemption_timeout)
3215 {
3216 if (GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 0, 0)) {
3217 struct context_policy policy;
3218
3219 __guc_context_policy_start_klv(&policy, guc_id);
3220 __guc_context_policy_add_preemption_timeout(&policy, preemption_timeout);
3221 __guc_context_set_context_policies(guc, &policy, true);
3222 } else {
3223 u32 action[] = {
3224 INTEL_GUC_ACTION_V69_SET_CONTEXT_PREEMPTION_TIMEOUT,
3225 guc_id,
3226 preemption_timeout
3227 };
3228
3229 intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
3230 }
3231 }
3232
3233 static void
guc_context_revoke(struct intel_context * ce,struct i915_request * rq,unsigned int preempt_timeout_ms)3234 guc_context_revoke(struct intel_context *ce, struct i915_request *rq,
3235 unsigned int preempt_timeout_ms)
3236 {
3237 struct intel_guc *guc = ce_to_guc(ce);
3238 struct intel_runtime_pm *runtime_pm =
3239 &ce->engine->gt->i915->runtime_pm;
3240 intel_wakeref_t wakeref;
3241 unsigned long flags;
3242
3243 GEM_BUG_ON(intel_context_is_child(ce));
3244
3245 guc_flush_submissions(guc);
3246
3247 spin_lock_irqsave(&ce->guc_state.lock, flags);
3248 set_context_banned(ce);
3249
3250 if (submission_disabled(guc) ||
3251 (!context_enabled(ce) && !context_pending_disable(ce))) {
3252 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3253
3254 guc_cancel_context_requests(ce);
3255 intel_engine_signal_breadcrumbs(ce->engine);
3256 } else if (!context_pending_disable(ce)) {
3257 u16 guc_id;
3258
3259 /*
3260 * We add +2 here as the schedule disable complete CTB handler
3261 * calls intel_context_sched_disable_unpin (-2 to pin_count).
3262 */
3263 atomic_add(2, &ce->pin_count);
3264
3265 guc_id = prep_context_pending_disable(ce);
3266 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3267
3268 /*
3269 * In addition to disabling scheduling, set the preemption
3270 * timeout to the minimum value (1 us) so the banned context
3271 * gets kicked off the HW ASAP.
3272 */
3273 with_intel_runtime_pm(runtime_pm, wakeref) {
3274 __guc_context_set_preemption_timeout(guc, guc_id,
3275 preempt_timeout_ms);
3276 __guc_context_sched_disable(guc, ce, guc_id);
3277 }
3278 } else {
3279 if (!context_guc_id_invalid(ce))
3280 with_intel_runtime_pm(runtime_pm, wakeref)
3281 __guc_context_set_preemption_timeout(guc,
3282 ce->guc_id.id,
3283 preempt_timeout_ms);
3284 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3285 }
3286 }
3287
do_sched_disable(struct intel_guc * guc,struct intel_context * ce,unsigned long flags)3288 static void do_sched_disable(struct intel_guc *guc, struct intel_context *ce,
3289 unsigned long flags)
3290 __releases(ce->guc_state.lock)
3291 {
3292 struct intel_runtime_pm *runtime_pm = &ce->engine->gt->i915->runtime_pm;
3293 intel_wakeref_t wakeref;
3294 u16 guc_id;
3295
3296 lockdep_assert_held(&ce->guc_state.lock);
3297 guc_id = prep_context_pending_disable(ce);
3298
3299 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3300
3301 with_intel_runtime_pm(runtime_pm, wakeref)
3302 __guc_context_sched_disable(guc, ce, guc_id);
3303 }
3304
bypass_sched_disable(struct intel_guc * guc,struct intel_context * ce)3305 static bool bypass_sched_disable(struct intel_guc *guc,
3306 struct intel_context *ce)
3307 {
3308 lockdep_assert_held(&ce->guc_state.lock);
3309 GEM_BUG_ON(intel_context_is_child(ce));
3310
3311 if (submission_disabled(guc) || context_guc_id_invalid(ce) ||
3312 !ctx_id_mapped(guc, ce->guc_id.id)) {
3313 clr_context_enabled(ce);
3314 return true;
3315 }
3316
3317 return !context_enabled(ce);
3318 }
3319
__delay_sched_disable(struct work_struct * wrk)3320 static void __delay_sched_disable(struct work_struct *wrk)
3321 {
3322 struct intel_context *ce =
3323 container_of(wrk, typeof(*ce), guc_state.sched_disable_delay_work.work);
3324 struct intel_guc *guc = ce_to_guc(ce);
3325 unsigned long flags;
3326
3327 spin_lock_irqsave(&ce->guc_state.lock, flags);
3328
3329 if (bypass_sched_disable(guc, ce)) {
3330 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3331 intel_context_sched_disable_unpin(ce);
3332 } else {
3333 do_sched_disable(guc, ce, flags);
3334 }
3335 }
3336
guc_id_pressure(struct intel_guc * guc,struct intel_context * ce)3337 static bool guc_id_pressure(struct intel_guc *guc, struct intel_context *ce)
3338 {
3339 /*
3340 * parent contexts are perma-pinned, if we are unpinning do schedule
3341 * disable immediately.
3342 */
3343 if (intel_context_is_parent(ce))
3344 return true;
3345
3346 /*
3347 * If we are beyond the threshold for avail guc_ids, do schedule disable immediately.
3348 */
3349 return guc->submission_state.guc_ids_in_use >
3350 guc->submission_state.sched_disable_gucid_threshold;
3351 }
3352
guc_context_sched_disable(struct intel_context * ce)3353 static void guc_context_sched_disable(struct intel_context *ce)
3354 {
3355 struct intel_guc *guc = ce_to_guc(ce);
3356 u64 delay = guc->submission_state.sched_disable_delay_ms;
3357 unsigned long flags;
3358
3359 spin_lock_irqsave(&ce->guc_state.lock, flags);
3360
3361 if (bypass_sched_disable(guc, ce)) {
3362 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3363 intel_context_sched_disable_unpin(ce);
3364 } else if (!intel_context_is_closed(ce) && !guc_id_pressure(guc, ce) &&
3365 delay) {
3366 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3367 mod_delayed_work(system_unbound_wq,
3368 &ce->guc_state.sched_disable_delay_work,
3369 msecs_to_jiffies(delay));
3370 } else {
3371 do_sched_disable(guc, ce, flags);
3372 }
3373 }
3374
guc_context_close(struct intel_context * ce)3375 static void guc_context_close(struct intel_context *ce)
3376 {
3377 unsigned long flags;
3378
3379 if (test_bit(CONTEXT_GUC_INIT, &ce->flags) &&
3380 cancel_delayed_work(&ce->guc_state.sched_disable_delay_work))
3381 __delay_sched_disable(&ce->guc_state.sched_disable_delay_work.work);
3382
3383 spin_lock_irqsave(&ce->guc_state.lock, flags);
3384 set_context_close_done(ce);
3385 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3386 }
3387
guc_lrc_desc_unpin(struct intel_context * ce)3388 static inline int guc_lrc_desc_unpin(struct intel_context *ce)
3389 {
3390 struct intel_guc *guc = ce_to_guc(ce);
3391 struct intel_gt *gt = guc_to_gt(guc);
3392 unsigned long flags;
3393 bool disabled;
3394 int ret;
3395
3396 GEM_BUG_ON(!intel_gt_pm_is_awake(gt));
3397 GEM_BUG_ON(!ctx_id_mapped(guc, ce->guc_id.id));
3398 GEM_BUG_ON(ce != __get_context(guc, ce->guc_id.id));
3399 GEM_BUG_ON(context_enabled(ce));
3400
3401 /* Seal race with Reset */
3402 spin_lock_irqsave(&ce->guc_state.lock, flags);
3403 disabled = submission_disabled(guc);
3404 if (likely(!disabled)) {
3405 /*
3406 * Take a gt-pm ref and change context state to be destroyed.
3407 * NOTE: a G2H IRQ that comes after will put this gt-pm ref back
3408 */
3409 __intel_gt_pm_get(gt);
3410 set_context_destroyed(ce);
3411 clr_context_registered(ce);
3412 }
3413 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3414
3415 if (unlikely(disabled)) {
3416 release_guc_id(guc, ce);
3417 __guc_context_destroy(ce);
3418 return 0;
3419 }
3420
3421 /*
3422 * GuC is active, lets destroy this context, but at this point we can still be racing
3423 * with suspend, so we undo everything if the H2G fails in deregister_context so
3424 * that GuC reset will find this context during clean up.
3425 */
3426 ret = deregister_context(ce, ce->guc_id.id);
3427 if (ret) {
3428 spin_lock(&ce->guc_state.lock);
3429 set_context_registered(ce);
3430 clr_context_destroyed(ce);
3431 spin_unlock(&ce->guc_state.lock);
3432 /*
3433 * As gt-pm is awake at function entry, intel_wakeref_put_async merely decrements
3434 * the wakeref immediately but per function spec usage call this after unlock.
3435 */
3436 intel_wakeref_put_async(>->wakeref);
3437 }
3438
3439 return ret;
3440 }
3441
__guc_context_destroy(struct intel_context * ce)3442 static void __guc_context_destroy(struct intel_context *ce)
3443 {
3444 GEM_BUG_ON(ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_KMD_HIGH] ||
3445 ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_HIGH] ||
3446 ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_KMD_NORMAL] ||
3447 ce->guc_state.prio_count[GUC_CLIENT_PRIORITY_NORMAL]);
3448
3449 lrc_fini(ce);
3450 intel_context_fini(ce);
3451
3452 if (intel_engine_is_virtual(ce->engine)) {
3453 struct guc_virtual_engine *ve =
3454 container_of(ce, typeof(*ve), context);
3455
3456 if (ve->base.breadcrumbs)
3457 intel_breadcrumbs_put(ve->base.breadcrumbs);
3458
3459 kfree(ve);
3460 } else {
3461 intel_context_free(ce);
3462 }
3463 }
3464
guc_flush_destroyed_contexts(struct intel_guc * guc)3465 static void guc_flush_destroyed_contexts(struct intel_guc *guc)
3466 {
3467 struct intel_context *ce;
3468 unsigned long flags;
3469
3470 GEM_BUG_ON(!submission_disabled(guc) &&
3471 guc_submission_initialized(guc));
3472
3473 while (!list_empty(&guc->submission_state.destroyed_contexts)) {
3474 spin_lock_irqsave(&guc->submission_state.lock, flags);
3475 ce = list_first_entry_or_null(&guc->submission_state.destroyed_contexts,
3476 struct intel_context,
3477 destroyed_link);
3478 if (ce)
3479 list_del_init(&ce->destroyed_link);
3480 spin_unlock_irqrestore(&guc->submission_state.lock, flags);
3481
3482 if (!ce)
3483 break;
3484
3485 release_guc_id(guc, ce);
3486 __guc_context_destroy(ce);
3487 }
3488 }
3489
deregister_destroyed_contexts(struct intel_guc * guc)3490 static void deregister_destroyed_contexts(struct intel_guc *guc)
3491 {
3492 struct intel_context *ce;
3493 unsigned long flags;
3494
3495 while (!list_empty(&guc->submission_state.destroyed_contexts)) {
3496 spin_lock_irqsave(&guc->submission_state.lock, flags);
3497 ce = list_first_entry_or_null(&guc->submission_state.destroyed_contexts,
3498 struct intel_context,
3499 destroyed_link);
3500 if (ce)
3501 list_del_init(&ce->destroyed_link);
3502 spin_unlock_irqrestore(&guc->submission_state.lock, flags);
3503
3504 if (!ce)
3505 break;
3506
3507 if (guc_lrc_desc_unpin(ce)) {
3508 /*
3509 * This means GuC's CT link severed mid-way which could happen
3510 * in suspend-resume corner cases. In this case, put the
3511 * context back into the destroyed_contexts list which will
3512 * get picked up on the next context deregistration event or
3513 * purged in a GuC sanitization event (reset/unload/wedged/...).
3514 */
3515 spin_lock_irqsave(&guc->submission_state.lock, flags);
3516 list_add_tail(&ce->destroyed_link,
3517 &guc->submission_state.destroyed_contexts);
3518 spin_unlock_irqrestore(&guc->submission_state.lock, flags);
3519 /* Bail now since the list might never be emptied if h2gs fail */
3520 break;
3521 }
3522
3523 }
3524 }
3525
destroyed_worker_func(struct work_struct * w)3526 static void destroyed_worker_func(struct work_struct *w)
3527 {
3528 struct intel_guc *guc = container_of(w, struct intel_guc,
3529 submission_state.destroyed_worker);
3530 struct intel_gt *gt = guc_to_gt(guc);
3531 intel_wakeref_t wakeref;
3532
3533 /*
3534 * In rare cases we can get here via async context-free fence-signals that
3535 * come very late in suspend flow or very early in resume flows. In these
3536 * cases, GuC won't be ready but just skipping it here is fine as these
3537 * pending-destroy-contexts get destroyed totally at GuC reset time at the
3538 * end of suspend.. OR.. this worker can be picked up later on the next
3539 * context destruction trigger after resume-completes
3540 */
3541 if (!intel_guc_is_ready(guc))
3542 return;
3543
3544 with_intel_gt_pm(gt, wakeref)
3545 deregister_destroyed_contexts(guc);
3546 }
3547
guc_context_destroy(struct kref * kref)3548 static void guc_context_destroy(struct kref *kref)
3549 {
3550 struct intel_context *ce = container_of(kref, typeof(*ce), ref);
3551 struct intel_guc *guc = ce_to_guc(ce);
3552 unsigned long flags;
3553 bool destroy;
3554
3555 /*
3556 * If the guc_id is invalid this context has been stolen and we can free
3557 * it immediately. Also can be freed immediately if the context is not
3558 * registered with the GuC or the GuC is in the middle of a reset.
3559 */
3560 spin_lock_irqsave(&guc->submission_state.lock, flags);
3561 destroy = submission_disabled(guc) || context_guc_id_invalid(ce) ||
3562 !ctx_id_mapped(guc, ce->guc_id.id);
3563 if (likely(!destroy)) {
3564 if (!list_empty(&ce->guc_id.link))
3565 list_del_init(&ce->guc_id.link);
3566 list_add_tail(&ce->destroyed_link,
3567 &guc->submission_state.destroyed_contexts);
3568 } else {
3569 __release_guc_id(guc, ce);
3570 }
3571 spin_unlock_irqrestore(&guc->submission_state.lock, flags);
3572 if (unlikely(destroy)) {
3573 __guc_context_destroy(ce);
3574 return;
3575 }
3576
3577 /*
3578 * We use a worker to issue the H2G to deregister the context as we can
3579 * take the GT PM for the first time which isn't allowed from an atomic
3580 * context.
3581 */
3582 queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
3583 }
3584
guc_context_alloc(struct intel_context * ce)3585 static int guc_context_alloc(struct intel_context *ce)
3586 {
3587 return lrc_alloc(ce, ce->engine);
3588 }
3589
__guc_context_set_prio(struct intel_guc * guc,struct intel_context * ce)3590 static void __guc_context_set_prio(struct intel_guc *guc,
3591 struct intel_context *ce)
3592 {
3593 if (GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 0, 0)) {
3594 struct context_policy policy;
3595
3596 __guc_context_policy_start_klv(&policy, ce->guc_id.id);
3597 __guc_context_policy_add_priority(&policy, ce->guc_state.prio);
3598 __guc_context_set_context_policies(guc, &policy, true);
3599 } else {
3600 u32 action[] = {
3601 INTEL_GUC_ACTION_V69_SET_CONTEXT_PRIORITY,
3602 ce->guc_id.id,
3603 ce->guc_state.prio,
3604 };
3605
3606 guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
3607 }
3608 }
3609
guc_context_set_prio(struct intel_guc * guc,struct intel_context * ce,u8 prio)3610 static void guc_context_set_prio(struct intel_guc *guc,
3611 struct intel_context *ce,
3612 u8 prio)
3613 {
3614 GEM_BUG_ON(prio < GUC_CLIENT_PRIORITY_KMD_HIGH ||
3615 prio > GUC_CLIENT_PRIORITY_NORMAL);
3616 lockdep_assert_held(&ce->guc_state.lock);
3617
3618 if (ce->guc_state.prio == prio || submission_disabled(guc) ||
3619 !context_registered(ce)) {
3620 ce->guc_state.prio = prio;
3621 return;
3622 }
3623
3624 ce->guc_state.prio = prio;
3625 __guc_context_set_prio(guc, ce);
3626
3627 trace_intel_context_set_prio(ce);
3628 }
3629
map_i915_prio_to_guc_prio(int prio)3630 static inline u8 map_i915_prio_to_guc_prio(int prio)
3631 {
3632 if (prio == I915_PRIORITY_NORMAL)
3633 return GUC_CLIENT_PRIORITY_KMD_NORMAL;
3634 else if (prio < I915_PRIORITY_NORMAL)
3635 return GUC_CLIENT_PRIORITY_NORMAL;
3636 else if (prio < I915_PRIORITY_DISPLAY)
3637 return GUC_CLIENT_PRIORITY_HIGH;
3638 else
3639 return GUC_CLIENT_PRIORITY_KMD_HIGH;
3640 }
3641
add_context_inflight_prio(struct intel_context * ce,u8 guc_prio)3642 static inline void add_context_inflight_prio(struct intel_context *ce,
3643 u8 guc_prio)
3644 {
3645 lockdep_assert_held(&ce->guc_state.lock);
3646 GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_state.prio_count));
3647
3648 ++ce->guc_state.prio_count[guc_prio];
3649
3650 /* Overflow protection */
3651 GEM_WARN_ON(!ce->guc_state.prio_count[guc_prio]);
3652 }
3653
sub_context_inflight_prio(struct intel_context * ce,u8 guc_prio)3654 static inline void sub_context_inflight_prio(struct intel_context *ce,
3655 u8 guc_prio)
3656 {
3657 lockdep_assert_held(&ce->guc_state.lock);
3658 GEM_BUG_ON(guc_prio >= ARRAY_SIZE(ce->guc_state.prio_count));
3659
3660 /* Underflow protection */
3661 GEM_WARN_ON(!ce->guc_state.prio_count[guc_prio]);
3662
3663 --ce->guc_state.prio_count[guc_prio];
3664 }
3665
update_context_prio(struct intel_context * ce)3666 static inline void update_context_prio(struct intel_context *ce)
3667 {
3668 struct intel_guc *guc = &ce->engine->gt->uc.guc;
3669 int i;
3670
3671 BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH != 0);
3672 BUILD_BUG_ON(GUC_CLIENT_PRIORITY_KMD_HIGH > GUC_CLIENT_PRIORITY_NORMAL);
3673
3674 lockdep_assert_held(&ce->guc_state.lock);
3675
3676 for (i = 0; i < ARRAY_SIZE(ce->guc_state.prio_count); ++i) {
3677 if (ce->guc_state.prio_count[i]) {
3678 guc_context_set_prio(guc, ce, i);
3679 break;
3680 }
3681 }
3682 }
3683
new_guc_prio_higher(u8 old_guc_prio,u8 new_guc_prio)3684 static inline bool new_guc_prio_higher(u8 old_guc_prio, u8 new_guc_prio)
3685 {
3686 /* Lower value is higher priority */
3687 return new_guc_prio < old_guc_prio;
3688 }
3689
add_to_context(struct i915_request * rq)3690 static void add_to_context(struct i915_request *rq)
3691 {
3692 struct intel_context *ce = request_to_scheduling_context(rq);
3693 u8 new_guc_prio = map_i915_prio_to_guc_prio(rq_prio(rq));
3694
3695 GEM_BUG_ON(intel_context_is_child(ce));
3696 GEM_BUG_ON(rq->guc_prio == GUC_PRIO_FINI);
3697
3698 spin_lock(&ce->guc_state.lock);
3699 list_move_tail(&rq->sched.link, &ce->guc_state.requests);
3700
3701 if (rq->guc_prio == GUC_PRIO_INIT) {
3702 rq->guc_prio = new_guc_prio;
3703 add_context_inflight_prio(ce, rq->guc_prio);
3704 } else if (new_guc_prio_higher(rq->guc_prio, new_guc_prio)) {
3705 sub_context_inflight_prio(ce, rq->guc_prio);
3706 rq->guc_prio = new_guc_prio;
3707 add_context_inflight_prio(ce, rq->guc_prio);
3708 }
3709 update_context_prio(ce);
3710
3711 spin_unlock(&ce->guc_state.lock);
3712 }
3713
guc_prio_fini(struct i915_request * rq,struct intel_context * ce)3714 static void guc_prio_fini(struct i915_request *rq, struct intel_context *ce)
3715 {
3716 lockdep_assert_held(&ce->guc_state.lock);
3717
3718 if (rq->guc_prio != GUC_PRIO_INIT &&
3719 rq->guc_prio != GUC_PRIO_FINI) {
3720 sub_context_inflight_prio(ce, rq->guc_prio);
3721 update_context_prio(ce);
3722 }
3723 rq->guc_prio = GUC_PRIO_FINI;
3724 }
3725
remove_from_context(struct i915_request * rq)3726 static void remove_from_context(struct i915_request *rq)
3727 {
3728 struct intel_context *ce = request_to_scheduling_context(rq);
3729
3730 GEM_BUG_ON(intel_context_is_child(ce));
3731
3732 spin_lock_irq(&ce->guc_state.lock);
3733
3734 list_del_init(&rq->sched.link);
3735 clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
3736
3737 /* Prevent further __await_execution() registering a cb, then flush */
3738 set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
3739
3740 guc_prio_fini(rq, ce);
3741
3742 spin_unlock_irq(&ce->guc_state.lock);
3743
3744 atomic_dec(&ce->guc_id.ref);
3745 i915_request_notify_execute_cb_imm(rq);
3746 }
3747
3748 static const struct intel_context_ops guc_context_ops = {
3749 .flags = COPS_RUNTIME_CYCLES,
3750 .alloc = guc_context_alloc,
3751
3752 .close = guc_context_close,
3753
3754 .pre_pin = guc_context_pre_pin,
3755 .pin = guc_context_pin,
3756 .unpin = guc_context_unpin,
3757 .post_unpin = guc_context_post_unpin,
3758
3759 .revoke = guc_context_revoke,
3760
3761 .cancel_request = guc_context_cancel_request,
3762
3763 .enter = intel_context_enter_engine,
3764 .exit = intel_context_exit_engine,
3765
3766 .sched_disable = guc_context_sched_disable,
3767
3768 .update_stats = guc_context_update_stats,
3769
3770 .reset = lrc_reset,
3771 .destroy = guc_context_destroy,
3772
3773 .create_virtual = guc_create_virtual,
3774 .create_parallel = guc_create_parallel,
3775 };
3776
submit_work_cb(struct irq_work * wrk)3777 static void submit_work_cb(struct irq_work *wrk)
3778 {
3779 struct i915_request *rq = container_of(wrk, typeof(*rq), submit_work);
3780
3781 might_lock(&rq->engine->sched_engine->lock);
3782 i915_sw_fence_complete(&rq->submit);
3783 }
3784
__guc_signal_context_fence(struct intel_context * ce)3785 static void __guc_signal_context_fence(struct intel_context *ce)
3786 {
3787 struct i915_request *rq, *rn;
3788
3789 lockdep_assert_held(&ce->guc_state.lock);
3790
3791 if (!list_empty(&ce->guc_state.fences))
3792 trace_intel_context_fence_release(ce);
3793
3794 /*
3795 * Use an IRQ to ensure locking order of sched_engine->lock ->
3796 * ce->guc_state.lock is preserved.
3797 */
3798 list_for_each_entry_safe(rq, rn, &ce->guc_state.fences,
3799 guc_fence_link) {
3800 list_del(&rq->guc_fence_link);
3801 irq_work_queue(&rq->submit_work);
3802 }
3803
3804 INIT_LIST_HEAD(&ce->guc_state.fences);
3805 }
3806
guc_signal_context_fence(struct intel_context * ce)3807 static void guc_signal_context_fence(struct intel_context *ce)
3808 {
3809 unsigned long flags;
3810
3811 GEM_BUG_ON(intel_context_is_child(ce));
3812
3813 spin_lock_irqsave(&ce->guc_state.lock, flags);
3814 clr_context_wait_for_deregister_to_register(ce);
3815 __guc_signal_context_fence(ce);
3816 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3817 }
3818
context_needs_register(struct intel_context * ce,bool new_guc_id)3819 static bool context_needs_register(struct intel_context *ce, bool new_guc_id)
3820 {
3821 return (new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
3822 !ctx_id_mapped(ce_to_guc(ce), ce->guc_id.id)) &&
3823 !submission_disabled(ce_to_guc(ce));
3824 }
3825
guc_context_init(struct intel_context * ce)3826 static void guc_context_init(struct intel_context *ce)
3827 {
3828 const struct i915_gem_context *ctx;
3829 int prio = I915_CONTEXT_DEFAULT_PRIORITY;
3830
3831 rcu_read_lock();
3832 ctx = rcu_dereference(ce->gem_context);
3833 if (ctx)
3834 prio = ctx->sched.priority;
3835 rcu_read_unlock();
3836
3837 ce->guc_state.prio = map_i915_prio_to_guc_prio(prio);
3838
3839 INIT_DELAYED_WORK(&ce->guc_state.sched_disable_delay_work,
3840 __delay_sched_disable);
3841
3842 set_bit(CONTEXT_GUC_INIT, &ce->flags);
3843 }
3844
guc_request_alloc(struct i915_request * rq)3845 static int guc_request_alloc(struct i915_request *rq)
3846 {
3847 struct intel_context *ce = request_to_scheduling_context(rq);
3848 struct intel_guc *guc = ce_to_guc(ce);
3849 unsigned long flags;
3850 int ret;
3851
3852 GEM_BUG_ON(!intel_context_is_pinned(rq->context));
3853
3854 /*
3855 * Flush enough space to reduce the likelihood of waiting after
3856 * we start building the request - in which case we will just
3857 * have to repeat work.
3858 */
3859 rq->reserved_space += GUC_REQUEST_SIZE;
3860
3861 /*
3862 * Note that after this point, we have committed to using
3863 * this request as it is being used to both track the
3864 * state of engine initialisation and liveness of the
3865 * golden renderstate above. Think twice before you try
3866 * to cancel/unwind this request now.
3867 */
3868
3869 /* Unconditionally invalidate GPU caches and TLBs. */
3870 ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
3871 if (ret)
3872 return ret;
3873
3874 rq->reserved_space -= GUC_REQUEST_SIZE;
3875
3876 if (unlikely(!test_bit(CONTEXT_GUC_INIT, &ce->flags)))
3877 guc_context_init(ce);
3878
3879 /*
3880 * If the context gets closed while the execbuf is ongoing, the context
3881 * close code will race with the below code to cancel the delayed work.
3882 * If the context close wins the race and cancels the work, it will
3883 * immediately call the sched disable (see guc_context_close), so there
3884 * is a chance we can get past this check while the sched_disable code
3885 * is being executed. To make sure that code completes before we check
3886 * the status further down, we wait for the close process to complete.
3887 * Else, this code path could send a request down thinking that the
3888 * context is still in a schedule-enable mode while the GuC ends up
3889 * dropping the request completely because the disable did go from the
3890 * context_close path right to GuC just prior. In the event the CT is
3891 * full, we could potentially need to wait up to 1.5 seconds.
3892 */
3893 if (cancel_delayed_work_sync(&ce->guc_state.sched_disable_delay_work))
3894 intel_context_sched_disable_unpin(ce);
3895 else if (intel_context_is_closed(ce))
3896 if (wait_for(context_close_done(ce), 1500))
3897 guc_warn(guc, "timed out waiting on context sched close before realloc\n");
3898 /*
3899 * Call pin_guc_id here rather than in the pinning step as with
3900 * dma_resv, contexts can be repeatedly pinned / unpinned trashing the
3901 * guc_id and creating horrible race conditions. This is especially bad
3902 * when guc_id are being stolen due to over subscription. By the time
3903 * this function is reached, it is guaranteed that the guc_id will be
3904 * persistent until the generated request is retired. Thus, sealing these
3905 * race conditions. It is still safe to fail here if guc_id are
3906 * exhausted and return -EAGAIN to the user indicating that they can try
3907 * again in the future.
3908 *
3909 * There is no need for a lock here as the timeline mutex ensures at
3910 * most one context can be executing this code path at once. The
3911 * guc_id_ref is incremented once for every request in flight and
3912 * decremented on each retire. When it is zero, a lock around the
3913 * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id.
3914 */
3915 if (atomic_add_unless(&ce->guc_id.ref, 1, 0))
3916 goto out;
3917
3918 ret = pin_guc_id(guc, ce); /* returns 1 if new guc_id assigned */
3919 if (unlikely(ret < 0))
3920 return ret;
3921 if (context_needs_register(ce, !!ret)) {
3922 ret = try_context_registration(ce, true);
3923 if (unlikely(ret)) { /* unwind */
3924 if (ret == -EPIPE) {
3925 disable_submission(guc);
3926 goto out; /* GPU will be reset */
3927 }
3928 atomic_dec(&ce->guc_id.ref);
3929 unpin_guc_id(guc, ce);
3930 return ret;
3931 }
3932 }
3933
3934 clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags);
3935
3936 out:
3937 /*
3938 * We block all requests on this context if a G2H is pending for a
3939 * schedule disable or context deregistration as the GuC will fail a
3940 * schedule enable or context registration if either G2H is pending
3941 * respectfully. Once a G2H returns, the fence is released that is
3942 * blocking these requests (see guc_signal_context_fence).
3943 */
3944 spin_lock_irqsave(&ce->guc_state.lock, flags);
3945 if (context_wait_for_deregister_to_register(ce) ||
3946 context_pending_disable(ce)) {
3947 init_irq_work(&rq->submit_work, submit_work_cb);
3948 i915_sw_fence_await(&rq->submit);
3949
3950 list_add_tail(&rq->guc_fence_link, &ce->guc_state.fences);
3951 }
3952 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
3953
3954 return 0;
3955 }
3956
guc_virtual_context_pre_pin(struct intel_context * ce,struct i915_gem_ww_ctx * ww,void ** vaddr)3957 static int guc_virtual_context_pre_pin(struct intel_context *ce,
3958 struct i915_gem_ww_ctx *ww,
3959 void **vaddr)
3960 {
3961 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
3962
3963 return __guc_context_pre_pin(ce, engine, ww, vaddr);
3964 }
3965
guc_virtual_context_pin(struct intel_context * ce,void * vaddr)3966 static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr)
3967 {
3968 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
3969 int ret = __guc_context_pin(ce, engine, vaddr);
3970 intel_engine_mask_t tmp, mask = ce->engine->mask;
3971
3972 if (likely(!ret))
3973 for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
3974 intel_engine_pm_get(engine);
3975
3976 return ret;
3977 }
3978
guc_virtual_context_unpin(struct intel_context * ce)3979 static void guc_virtual_context_unpin(struct intel_context *ce)
3980 {
3981 intel_engine_mask_t tmp, mask = ce->engine->mask;
3982 struct intel_engine_cs *engine;
3983 struct intel_guc *guc = ce_to_guc(ce);
3984
3985 GEM_BUG_ON(context_enabled(ce));
3986 GEM_BUG_ON(intel_context_is_barrier(ce));
3987
3988 unpin_guc_id(guc, ce);
3989 lrc_unpin(ce);
3990
3991 for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
3992 intel_engine_pm_put_async(engine);
3993 }
3994
guc_virtual_context_enter(struct intel_context * ce)3995 static void guc_virtual_context_enter(struct intel_context *ce)
3996 {
3997 intel_engine_mask_t tmp, mask = ce->engine->mask;
3998 struct intel_engine_cs *engine;
3999
4000 for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
4001 intel_engine_pm_get(engine);
4002
4003 intel_timeline_enter(ce->timeline);
4004 }
4005
guc_virtual_context_exit(struct intel_context * ce)4006 static void guc_virtual_context_exit(struct intel_context *ce)
4007 {
4008 intel_engine_mask_t tmp, mask = ce->engine->mask;
4009 struct intel_engine_cs *engine;
4010
4011 for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
4012 intel_engine_pm_put(engine);
4013
4014 intel_timeline_exit(ce->timeline);
4015 }
4016
guc_virtual_context_alloc(struct intel_context * ce)4017 static int guc_virtual_context_alloc(struct intel_context *ce)
4018 {
4019 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
4020
4021 return lrc_alloc(ce, engine);
4022 }
4023
4024 static const struct intel_context_ops virtual_guc_context_ops = {
4025 .flags = COPS_RUNTIME_CYCLES,
4026 .alloc = guc_virtual_context_alloc,
4027
4028 .close = guc_context_close,
4029
4030 .pre_pin = guc_virtual_context_pre_pin,
4031 .pin = guc_virtual_context_pin,
4032 .unpin = guc_virtual_context_unpin,
4033 .post_unpin = guc_context_post_unpin,
4034
4035 .revoke = guc_context_revoke,
4036
4037 .cancel_request = guc_context_cancel_request,
4038
4039 .enter = guc_virtual_context_enter,
4040 .exit = guc_virtual_context_exit,
4041
4042 .sched_disable = guc_context_sched_disable,
4043 .update_stats = guc_context_update_stats,
4044
4045 .destroy = guc_context_destroy,
4046
4047 .get_sibling = guc_virtual_get_sibling,
4048 };
4049
guc_parent_context_pin(struct intel_context * ce,void * vaddr)4050 static int guc_parent_context_pin(struct intel_context *ce, void *vaddr)
4051 {
4052 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
4053 struct intel_guc *guc = ce_to_guc(ce);
4054 int ret;
4055
4056 GEM_BUG_ON(!intel_context_is_parent(ce));
4057 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
4058
4059 ret = pin_guc_id(guc, ce);
4060 if (unlikely(ret < 0))
4061 return ret;
4062
4063 return __guc_context_pin(ce, engine, vaddr);
4064 }
4065
guc_child_context_pin(struct intel_context * ce,void * vaddr)4066 static int guc_child_context_pin(struct intel_context *ce, void *vaddr)
4067 {
4068 struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
4069
4070 GEM_BUG_ON(!intel_context_is_child(ce));
4071 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
4072
4073 __intel_context_pin(ce->parallel.parent);
4074 return __guc_context_pin(ce, engine, vaddr);
4075 }
4076
guc_parent_context_unpin(struct intel_context * ce)4077 static void guc_parent_context_unpin(struct intel_context *ce)
4078 {
4079 struct intel_guc *guc = ce_to_guc(ce);
4080
4081 GEM_BUG_ON(context_enabled(ce));
4082 GEM_BUG_ON(intel_context_is_barrier(ce));
4083 GEM_BUG_ON(!intel_context_is_parent(ce));
4084 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
4085
4086 unpin_guc_id(guc, ce);
4087 lrc_unpin(ce);
4088 }
4089
guc_child_context_unpin(struct intel_context * ce)4090 static void guc_child_context_unpin(struct intel_context *ce)
4091 {
4092 GEM_BUG_ON(context_enabled(ce));
4093 GEM_BUG_ON(intel_context_is_barrier(ce));
4094 GEM_BUG_ON(!intel_context_is_child(ce));
4095 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
4096
4097 lrc_unpin(ce);
4098 }
4099
guc_child_context_post_unpin(struct intel_context * ce)4100 static void guc_child_context_post_unpin(struct intel_context *ce)
4101 {
4102 GEM_BUG_ON(!intel_context_is_child(ce));
4103 GEM_BUG_ON(!intel_context_is_pinned(ce->parallel.parent));
4104 GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
4105
4106 lrc_post_unpin(ce);
4107 intel_context_unpin(ce->parallel.parent);
4108 }
4109
guc_child_context_destroy(struct kref * kref)4110 static void guc_child_context_destroy(struct kref *kref)
4111 {
4112 struct intel_context *ce = container_of(kref, typeof(*ce), ref);
4113
4114 __guc_context_destroy(ce);
4115 }
4116
4117 static const struct intel_context_ops virtual_parent_context_ops = {
4118 .alloc = guc_virtual_context_alloc,
4119
4120 .close = guc_context_close,
4121
4122 .pre_pin = guc_context_pre_pin,
4123 .pin = guc_parent_context_pin,
4124 .unpin = guc_parent_context_unpin,
4125 .post_unpin = guc_context_post_unpin,
4126
4127 .revoke = guc_context_revoke,
4128
4129 .cancel_request = guc_context_cancel_request,
4130
4131 .enter = guc_virtual_context_enter,
4132 .exit = guc_virtual_context_exit,
4133
4134 .sched_disable = guc_context_sched_disable,
4135
4136 .destroy = guc_context_destroy,
4137
4138 .get_sibling = guc_virtual_get_sibling,
4139 };
4140
4141 static const struct intel_context_ops virtual_child_context_ops = {
4142 .alloc = guc_virtual_context_alloc,
4143
4144 .pre_pin = guc_context_pre_pin,
4145 .pin = guc_child_context_pin,
4146 .unpin = guc_child_context_unpin,
4147 .post_unpin = guc_child_context_post_unpin,
4148
4149 .cancel_request = guc_context_cancel_request,
4150
4151 .enter = guc_virtual_context_enter,
4152 .exit = guc_virtual_context_exit,
4153
4154 .destroy = guc_child_context_destroy,
4155
4156 .get_sibling = guc_virtual_get_sibling,
4157 };
4158
4159 /*
4160 * The below override of the breadcrumbs is enabled when the user configures a
4161 * context for parallel submission (multi-lrc, parent-child).
4162 *
4163 * The overridden breadcrumbs implements an algorithm which allows the GuC to
4164 * safely preempt all the hw contexts configured for parallel submission
4165 * between each BB. The contract between the i915 and GuC is if the parent
4166 * context can be preempted, all the children can be preempted, and the GuC will
4167 * always try to preempt the parent before the children. A handshake between the
4168 * parent / children breadcrumbs ensures the i915 holds up its end of the deal
4169 * creating a window to preempt between each set of BBs.
4170 */
4171 static int emit_bb_start_parent_no_preempt_mid_batch(struct i915_request *rq,
4172 u64 offset, u32 len,
4173 const unsigned int flags);
4174 static int emit_bb_start_child_no_preempt_mid_batch(struct i915_request *rq,
4175 u64 offset, u32 len,
4176 const unsigned int flags);
4177 static u32 *
4178 emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq,
4179 u32 *cs);
4180 static u32 *
4181 emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq,
4182 u32 *cs);
4183
4184 static struct intel_context *
guc_create_parallel(struct intel_engine_cs ** engines,unsigned int num_siblings,unsigned int width)4185 guc_create_parallel(struct intel_engine_cs **engines,
4186 unsigned int num_siblings,
4187 unsigned int width)
4188 {
4189 struct intel_engine_cs **siblings = NULL;
4190 struct intel_context *parent = NULL, *ce, *err;
4191 int i, j;
4192
4193 siblings = kmalloc_array(num_siblings,
4194 sizeof(*siblings),
4195 GFP_KERNEL);
4196 if (!siblings)
4197 return ERR_PTR(-ENOMEM);
4198
4199 for (i = 0; i < width; ++i) {
4200 for (j = 0; j < num_siblings; ++j)
4201 siblings[j] = engines[i * num_siblings + j];
4202
4203 ce = intel_engine_create_virtual(siblings, num_siblings,
4204 FORCE_VIRTUAL);
4205 if (IS_ERR(ce)) {
4206 err = ERR_CAST(ce);
4207 goto unwind;
4208 }
4209
4210 if (i == 0) {
4211 parent = ce;
4212 parent->ops = &virtual_parent_context_ops;
4213 } else {
4214 ce->ops = &virtual_child_context_ops;
4215 intel_context_bind_parent_child(parent, ce);
4216 }
4217 }
4218
4219 parent->parallel.fence_context = dma_fence_context_alloc(1);
4220
4221 parent->engine->emit_bb_start =
4222 emit_bb_start_parent_no_preempt_mid_batch;
4223 parent->engine->emit_fini_breadcrumb =
4224 emit_fini_breadcrumb_parent_no_preempt_mid_batch;
4225 parent->engine->emit_fini_breadcrumb_dw =
4226 12 + 4 * parent->parallel.number_children;
4227 for_each_child(parent, ce) {
4228 ce->engine->emit_bb_start =
4229 emit_bb_start_child_no_preempt_mid_batch;
4230 ce->engine->emit_fini_breadcrumb =
4231 emit_fini_breadcrumb_child_no_preempt_mid_batch;
4232 ce->engine->emit_fini_breadcrumb_dw = 16;
4233 }
4234
4235 kfree(siblings);
4236 return parent;
4237
4238 unwind:
4239 if (parent)
4240 intel_context_put(parent);
4241 kfree(siblings);
4242 return err;
4243 }
4244
4245 static bool
guc_irq_enable_breadcrumbs(struct intel_breadcrumbs * b)4246 guc_irq_enable_breadcrumbs(struct intel_breadcrumbs *b)
4247 {
4248 struct intel_engine_cs *sibling;
4249 intel_engine_mask_t tmp, mask = b->engine_mask;
4250 bool result = false;
4251
4252 for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp)
4253 result |= intel_engine_irq_enable(sibling);
4254
4255 return result;
4256 }
4257
4258 static void
guc_irq_disable_breadcrumbs(struct intel_breadcrumbs * b)4259 guc_irq_disable_breadcrumbs(struct intel_breadcrumbs *b)
4260 {
4261 struct intel_engine_cs *sibling;
4262 intel_engine_mask_t tmp, mask = b->engine_mask;
4263
4264 for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp)
4265 intel_engine_irq_disable(sibling);
4266 }
4267
guc_init_breadcrumbs(struct intel_engine_cs * engine)4268 static void guc_init_breadcrumbs(struct intel_engine_cs *engine)
4269 {
4270 int i;
4271
4272 /*
4273 * In GuC submission mode we do not know which physical engine a request
4274 * will be scheduled on, this creates a problem because the breadcrumb
4275 * interrupt is per physical engine. To work around this we attach
4276 * requests and direct all breadcrumb interrupts to the first instance
4277 * of an engine per class. In addition all breadcrumb interrupts are
4278 * enabled / disabled across an engine class in unison.
4279 */
4280 for (i = 0; i < MAX_ENGINE_INSTANCE; ++i) {
4281 struct intel_engine_cs *sibling =
4282 engine->gt->engine_class[engine->class][i];
4283
4284 if (sibling) {
4285 if (engine->breadcrumbs != sibling->breadcrumbs) {
4286 intel_breadcrumbs_put(engine->breadcrumbs);
4287 engine->breadcrumbs =
4288 intel_breadcrumbs_get(sibling->breadcrumbs);
4289 }
4290 break;
4291 }
4292 }
4293
4294 if (engine->breadcrumbs) {
4295 engine->breadcrumbs->engine_mask |= engine->mask;
4296 engine->breadcrumbs->irq_enable = guc_irq_enable_breadcrumbs;
4297 engine->breadcrumbs->irq_disable = guc_irq_disable_breadcrumbs;
4298 }
4299 }
4300
guc_bump_inflight_request_prio(struct i915_request * rq,int prio)4301 static void guc_bump_inflight_request_prio(struct i915_request *rq,
4302 int prio)
4303 {
4304 struct intel_context *ce = request_to_scheduling_context(rq);
4305 u8 new_guc_prio = map_i915_prio_to_guc_prio(prio);
4306
4307 /* Short circuit function */
4308 if (prio < I915_PRIORITY_NORMAL)
4309 return;
4310
4311 spin_lock(&ce->guc_state.lock);
4312
4313 if (rq->guc_prio == GUC_PRIO_FINI)
4314 goto exit;
4315
4316 if (!new_guc_prio_higher(rq->guc_prio, new_guc_prio))
4317 goto exit;
4318
4319 if (rq->guc_prio != GUC_PRIO_INIT)
4320 sub_context_inflight_prio(ce, rq->guc_prio);
4321
4322 rq->guc_prio = new_guc_prio;
4323 add_context_inflight_prio(ce, rq->guc_prio);
4324 update_context_prio(ce);
4325
4326 exit:
4327 spin_unlock(&ce->guc_state.lock);
4328 }
4329
guc_retire_inflight_request_prio(struct i915_request * rq)4330 static void guc_retire_inflight_request_prio(struct i915_request *rq)
4331 {
4332 struct intel_context *ce = request_to_scheduling_context(rq);
4333
4334 spin_lock(&ce->guc_state.lock);
4335 guc_prio_fini(rq, ce);
4336 spin_unlock(&ce->guc_state.lock);
4337 }
4338
sanitize_hwsp(struct intel_engine_cs * engine)4339 static void sanitize_hwsp(struct intel_engine_cs *engine)
4340 {
4341 struct intel_timeline *tl;
4342
4343 list_for_each_entry(tl, &engine->status_page.timelines, engine_link)
4344 intel_timeline_reset_seqno(tl);
4345 }
4346
guc_sanitize(struct intel_engine_cs * engine)4347 static void guc_sanitize(struct intel_engine_cs *engine)
4348 {
4349 /*
4350 * Poison residual state on resume, in case the suspend didn't!
4351 *
4352 * We have to assume that across suspend/resume (or other loss
4353 * of control) that the contents of our pinned buffers has been
4354 * lost, replaced by garbage. Since this doesn't always happen,
4355 * let's poison such state so that we more quickly spot when
4356 * we falsely assume it has been preserved.
4357 */
4358 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
4359 memset(engine->status_page.addr, POISON_INUSE, PAGE_SIZE);
4360
4361 /*
4362 * The kernel_context HWSP is stored in the status_page. As above,
4363 * that may be lost on resume/initialisation, and so we need to
4364 * reset the value in the HWSP.
4365 */
4366 sanitize_hwsp(engine);
4367
4368 /* And scrub the dirty cachelines for the HWSP */
4369 drm_clflush_virt_range(engine->status_page.addr, PAGE_SIZE);
4370
4371 intel_engine_reset_pinned_contexts(engine);
4372 }
4373
setup_hwsp(struct intel_engine_cs * engine)4374 static void setup_hwsp(struct intel_engine_cs *engine)
4375 {
4376 intel_engine_set_hwsp_writemask(engine, ~0u); /* HWSTAM */
4377
4378 ENGINE_WRITE_FW(engine,
4379 RING_HWS_PGA,
4380 i915_ggtt_offset(engine->status_page.vma));
4381 }
4382
start_engine(struct intel_engine_cs * engine)4383 static void start_engine(struct intel_engine_cs *engine)
4384 {
4385 ENGINE_WRITE_FW(engine,
4386 RING_MODE_GEN7,
4387 _MASKED_BIT_ENABLE(GEN11_GFX_DISABLE_LEGACY_MODE));
4388
4389 ENGINE_WRITE_FW(engine, RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING));
4390 ENGINE_POSTING_READ(engine, RING_MI_MODE);
4391 }
4392
guc_resume(struct intel_engine_cs * engine)4393 static int guc_resume(struct intel_engine_cs *engine)
4394 {
4395 assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL);
4396
4397 intel_mocs_init_engine(engine);
4398
4399 intel_breadcrumbs_reset(engine->breadcrumbs);
4400
4401 setup_hwsp(engine);
4402 start_engine(engine);
4403
4404 if (engine->flags & I915_ENGINE_FIRST_RENDER_COMPUTE)
4405 xehp_enable_ccs_engines(engine);
4406
4407 return 0;
4408 }
4409
guc_sched_engine_disabled(struct i915_sched_engine * sched_engine)4410 static bool guc_sched_engine_disabled(struct i915_sched_engine *sched_engine)
4411 {
4412 return !sched_engine->tasklet.callback;
4413 }
4414
guc_set_default_submission(struct intel_engine_cs * engine)4415 static void guc_set_default_submission(struct intel_engine_cs *engine)
4416 {
4417 engine->submit_request = guc_submit_request;
4418 }
4419
guc_kernel_context_pin(struct intel_guc * guc,struct intel_context * ce)4420 static inline int guc_kernel_context_pin(struct intel_guc *guc,
4421 struct intel_context *ce)
4422 {
4423 int ret;
4424
4425 /*
4426 * Note: we purposefully do not check the returns below because
4427 * the registration can only fail if a reset is just starting.
4428 * This is called at the end of reset so presumably another reset
4429 * isn't happening and even it did this code would be run again.
4430 */
4431
4432 if (context_guc_id_invalid(ce)) {
4433 ret = pin_guc_id(guc, ce);
4434
4435 if (ret < 0)
4436 return ret;
4437 }
4438
4439 if (!test_bit(CONTEXT_GUC_INIT, &ce->flags))
4440 guc_context_init(ce);
4441
4442 ret = try_context_registration(ce, true);
4443 if (ret)
4444 unpin_guc_id(guc, ce);
4445
4446 return ret;
4447 }
4448
guc_init_submission(struct intel_guc * guc)4449 static inline int guc_init_submission(struct intel_guc *guc)
4450 {
4451 struct intel_gt *gt = guc_to_gt(guc);
4452 struct intel_engine_cs *engine;
4453 enum intel_engine_id id;
4454
4455 /* make sure all descriptors are clean... */
4456 xa_destroy(&guc->context_lookup);
4457
4458 /*
4459 * A reset might have occurred while we had a pending stalled request,
4460 * so make sure we clean that up.
4461 */
4462 guc->stalled_request = NULL;
4463 guc->submission_stall_reason = STALL_NONE;
4464
4465 /*
4466 * Some contexts might have been pinned before we enabled GuC
4467 * submission, so we need to add them to the GuC bookeeping.
4468 * Also, after a reset the of the GuC we want to make sure that the
4469 * information shared with GuC is properly reset. The kernel LRCs are
4470 * not attached to the gem_context, so they need to be added separately.
4471 */
4472 for_each_engine(engine, gt, id) {
4473 struct intel_context *ce;
4474
4475 list_for_each_entry(ce, &engine->pinned_contexts_list,
4476 pinned_contexts_link) {
4477 int ret = guc_kernel_context_pin(guc, ce);
4478
4479 if (ret) {
4480 /* No point in trying to clean up as i915 will wedge on failure */
4481 return ret;
4482 }
4483 }
4484 }
4485
4486 return 0;
4487 }
4488
guc_release(struct intel_engine_cs * engine)4489 static void guc_release(struct intel_engine_cs *engine)
4490 {
4491 engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
4492
4493 intel_engine_cleanup_common(engine);
4494 lrc_fini_wa_ctx(engine);
4495 }
4496
virtual_guc_bump_serial(struct intel_engine_cs * engine)4497 static void virtual_guc_bump_serial(struct intel_engine_cs *engine)
4498 {
4499 struct intel_engine_cs *e;
4500 intel_engine_mask_t tmp, mask = engine->mask;
4501
4502 for_each_engine_masked(e, engine->gt, mask, tmp)
4503 e->serial++;
4504 }
4505
guc_default_vfuncs(struct intel_engine_cs * engine)4506 static void guc_default_vfuncs(struct intel_engine_cs *engine)
4507 {
4508 /* Default vfuncs which can be overridden by each engine. */
4509
4510 engine->resume = guc_resume;
4511
4512 engine->cops = &guc_context_ops;
4513 engine->request_alloc = guc_request_alloc;
4514 engine->add_active_request = add_to_context;
4515 engine->remove_active_request = remove_from_context;
4516
4517 engine->sched_engine->schedule = i915_schedule;
4518
4519 engine->reset.prepare = guc_engine_reset_prepare;
4520 engine->reset.rewind = guc_rewind_nop;
4521 engine->reset.cancel = guc_reset_nop;
4522 engine->reset.finish = guc_reset_nop;
4523
4524 engine->emit_flush = gen8_emit_flush_xcs;
4525 engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb;
4526 engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_xcs;
4527 if (GRAPHICS_VER(engine->i915) >= 12) {
4528 engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_xcs;
4529 engine->emit_flush = gen12_emit_flush_xcs;
4530 }
4531 engine->set_default_submission = guc_set_default_submission;
4532 engine->busyness = guc_engine_busyness;
4533
4534 engine->flags |= I915_ENGINE_SUPPORTS_STATS;
4535 engine->flags |= I915_ENGINE_HAS_PREEMPTION;
4536 engine->flags |= I915_ENGINE_HAS_TIMESLICES;
4537
4538 /* Wa_14014475959:dg2 */
4539 if (engine->class == COMPUTE_CLASS)
4540 if (IS_GFX_GT_IP_STEP(engine->gt, IP_VER(12, 70), STEP_A0, STEP_B0) ||
4541 IS_DG2(engine->i915))
4542 engine->flags |= I915_ENGINE_USES_WA_HOLD_SWITCHOUT;
4543
4544 /* Wa_16019325821 */
4545 /* Wa_14019159160 */
4546 if ((engine->class == COMPUTE_CLASS || engine->class == RENDER_CLASS) &&
4547 IS_GFX_GT_IP_RANGE(engine->gt, IP_VER(12, 70), IP_VER(12, 74)))
4548 engine->flags |= I915_ENGINE_USES_WA_HOLD_SWITCHOUT;
4549
4550 /*
4551 * TODO: GuC supports timeslicing and semaphores as well, but they're
4552 * handled by the firmware so some minor tweaks are required before
4553 * enabling.
4554 *
4555 * engine->flags |= I915_ENGINE_HAS_SEMAPHORES;
4556 */
4557
4558 engine->emit_bb_start = gen8_emit_bb_start;
4559 if (GRAPHICS_VER_FULL(engine->i915) >= IP_VER(12, 55))
4560 engine->emit_bb_start = xehp_emit_bb_start;
4561 }
4562
rcs_submission_override(struct intel_engine_cs * engine)4563 static void rcs_submission_override(struct intel_engine_cs *engine)
4564 {
4565 switch (GRAPHICS_VER(engine->i915)) {
4566 case 12:
4567 engine->emit_flush = gen12_emit_flush_rcs;
4568 engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_rcs;
4569 break;
4570 case 11:
4571 engine->emit_flush = gen11_emit_flush_rcs;
4572 engine->emit_fini_breadcrumb = gen11_emit_fini_breadcrumb_rcs;
4573 break;
4574 default:
4575 engine->emit_flush = gen8_emit_flush_rcs;
4576 engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_rcs;
4577 break;
4578 }
4579 }
4580
guc_default_irqs(struct intel_engine_cs * engine)4581 static inline void guc_default_irqs(struct intel_engine_cs *engine)
4582 {
4583 engine->irq_keep_mask = GT_RENDER_USER_INTERRUPT;
4584 intel_engine_set_irq_handler(engine, cs_irq_handler);
4585 }
4586
guc_sched_engine_destroy(struct kref * kref)4587 static void guc_sched_engine_destroy(struct kref *kref)
4588 {
4589 struct i915_sched_engine *sched_engine =
4590 container_of(kref, typeof(*sched_engine), ref);
4591 struct intel_guc *guc = sched_engine->private_data;
4592
4593 guc->sched_engine = NULL;
4594 tasklet_kill(&sched_engine->tasklet); /* flush the callback */
4595 kfree(sched_engine);
4596 }
4597
intel_guc_submission_setup(struct intel_engine_cs * engine)4598 int intel_guc_submission_setup(struct intel_engine_cs *engine)
4599 {
4600 struct drm_i915_private *i915 = engine->i915;
4601 struct intel_guc *guc = gt_to_guc(engine->gt);
4602
4603 /*
4604 * The setup relies on several assumptions (e.g. irqs always enabled)
4605 * that are only valid on gen11+
4606 */
4607 GEM_BUG_ON(GRAPHICS_VER(i915) < 11);
4608
4609 if (!guc->sched_engine) {
4610 guc->sched_engine = i915_sched_engine_create(ENGINE_VIRTUAL);
4611 if (!guc->sched_engine)
4612 return -ENOMEM;
4613
4614 guc->sched_engine->schedule = i915_schedule;
4615 guc->sched_engine->disabled = guc_sched_engine_disabled;
4616 guc->sched_engine->private_data = guc;
4617 guc->sched_engine->destroy = guc_sched_engine_destroy;
4618 guc->sched_engine->bump_inflight_request_prio =
4619 guc_bump_inflight_request_prio;
4620 guc->sched_engine->retire_inflight_request_prio =
4621 guc_retire_inflight_request_prio;
4622 tasklet_setup(&guc->sched_engine->tasklet,
4623 guc_submission_tasklet);
4624 }
4625 i915_sched_engine_put(engine->sched_engine);
4626 engine->sched_engine = i915_sched_engine_get(guc->sched_engine);
4627
4628 guc_default_vfuncs(engine);
4629 guc_default_irqs(engine);
4630 guc_init_breadcrumbs(engine);
4631
4632 if (engine->flags & I915_ENGINE_HAS_RCS_REG_STATE)
4633 rcs_submission_override(engine);
4634
4635 lrc_init_wa_ctx(engine);
4636
4637 /* Finally, take ownership and responsibility for cleanup! */
4638 engine->sanitize = guc_sanitize;
4639 engine->release = guc_release;
4640
4641 return 0;
4642 }
4643
4644 struct scheduling_policy {
4645 /* internal data */
4646 u32 max_words, num_words;
4647 u32 count;
4648 /* API data */
4649 struct guc_update_scheduling_policy h2g;
4650 };
4651
__guc_scheduling_policy_action_size(struct scheduling_policy * policy)4652 static u32 __guc_scheduling_policy_action_size(struct scheduling_policy *policy)
4653 {
4654 u32 *start = (void *)&policy->h2g;
4655 u32 *end = policy->h2g.data + policy->num_words;
4656 size_t delta = end - start;
4657
4658 return delta;
4659 }
4660
__guc_scheduling_policy_start_klv(struct scheduling_policy * policy)4661 static struct scheduling_policy *__guc_scheduling_policy_start_klv(struct scheduling_policy *policy)
4662 {
4663 policy->h2g.header.action = INTEL_GUC_ACTION_UPDATE_SCHEDULING_POLICIES_KLV;
4664 policy->max_words = ARRAY_SIZE(policy->h2g.data);
4665 policy->num_words = 0;
4666 policy->count = 0;
4667
4668 return policy;
4669 }
4670
__guc_scheduling_policy_add_klv(struct scheduling_policy * policy,u32 action,u32 * data,u32 len)4671 static void __guc_scheduling_policy_add_klv(struct scheduling_policy *policy,
4672 u32 action, u32 *data, u32 len)
4673 {
4674 u32 *klv_ptr = policy->h2g.data + policy->num_words;
4675
4676 GEM_BUG_ON((policy->num_words + 1 + len) > policy->max_words);
4677 *(klv_ptr++) = FIELD_PREP(GUC_KLV_0_KEY, action) |
4678 FIELD_PREP(GUC_KLV_0_LEN, len);
4679 memcpy(klv_ptr, data, sizeof(u32) * len);
4680 policy->num_words += 1 + len;
4681 policy->count++;
4682 }
4683
__guc_action_set_scheduling_policies(struct intel_guc * guc,struct scheduling_policy * policy)4684 static int __guc_action_set_scheduling_policies(struct intel_guc *guc,
4685 struct scheduling_policy *policy)
4686 {
4687 int ret;
4688
4689 ret = intel_guc_send(guc, (u32 *)&policy->h2g,
4690 __guc_scheduling_policy_action_size(policy));
4691 if (ret < 0) {
4692 guc_probe_error(guc, "Failed to configure global scheduling policies: %pe!\n",
4693 ERR_PTR(ret));
4694 return ret;
4695 }
4696
4697 if (ret != policy->count) {
4698 guc_warn(guc, "global scheduler policy processed %d of %d KLVs!",
4699 ret, policy->count);
4700 if (ret > policy->count)
4701 return -EPROTO;
4702 }
4703
4704 return 0;
4705 }
4706
guc_init_global_schedule_policy(struct intel_guc * guc)4707 static int guc_init_global_schedule_policy(struct intel_guc *guc)
4708 {
4709 struct scheduling_policy policy;
4710 struct intel_gt *gt = guc_to_gt(guc);
4711 intel_wakeref_t wakeref;
4712 int ret;
4713
4714 if (GUC_SUBMIT_VER(guc) < MAKE_GUC_VER(1, 1, 0))
4715 return 0;
4716
4717 __guc_scheduling_policy_start_klv(&policy);
4718
4719 with_intel_runtime_pm(>->i915->runtime_pm, wakeref) {
4720 u32 yield[] = {
4721 GLOBAL_SCHEDULE_POLICY_RC_YIELD_DURATION,
4722 GLOBAL_SCHEDULE_POLICY_RC_YIELD_RATIO,
4723 };
4724
4725 __guc_scheduling_policy_add_klv(&policy,
4726 GUC_SCHEDULING_POLICIES_KLV_ID_RENDER_COMPUTE_YIELD,
4727 yield, ARRAY_SIZE(yield));
4728
4729 ret = __guc_action_set_scheduling_policies(guc, &policy);
4730 }
4731
4732 return ret;
4733 }
4734
guc_route_semaphores(struct intel_guc * guc,bool to_guc)4735 static void guc_route_semaphores(struct intel_guc *guc, bool to_guc)
4736 {
4737 struct intel_gt *gt = guc_to_gt(guc);
4738 u32 val;
4739
4740 if (GRAPHICS_VER(gt->i915) < 12)
4741 return;
4742
4743 if (to_guc)
4744 val = GUC_SEM_INTR_ROUTE_TO_GUC | GUC_SEM_INTR_ENABLE_ALL;
4745 else
4746 val = 0;
4747
4748 intel_uncore_write(gt->uncore, GEN12_GUC_SEM_INTR_ENABLES, val);
4749 }
4750
intel_guc_submission_enable(struct intel_guc * guc)4751 int intel_guc_submission_enable(struct intel_guc *guc)
4752 {
4753 int ret;
4754
4755 /* Semaphore interrupt enable and route to GuC */
4756 guc_route_semaphores(guc, true);
4757
4758 ret = guc_init_submission(guc);
4759 if (ret)
4760 goto fail_sem;
4761
4762 ret = guc_init_engine_stats(guc);
4763 if (ret)
4764 goto fail_sem;
4765
4766 ret = guc_init_global_schedule_policy(guc);
4767 if (ret)
4768 goto fail_stats;
4769
4770 return 0;
4771
4772 fail_stats:
4773 guc_fini_engine_stats(guc);
4774 fail_sem:
4775 guc_route_semaphores(guc, false);
4776 return ret;
4777 }
4778
4779 /* Note: By the time we're here, GuC may have already been reset */
intel_guc_submission_disable(struct intel_guc * guc)4780 void intel_guc_submission_disable(struct intel_guc *guc)
4781 {
4782 guc_cancel_busyness_worker(guc);
4783
4784 /* Semaphore interrupt disable and route to host */
4785 guc_route_semaphores(guc, false);
4786 }
4787
__guc_submission_supported(struct intel_guc * guc)4788 static bool __guc_submission_supported(struct intel_guc *guc)
4789 {
4790 /* GuC submission is unavailable for pre-Gen11 */
4791 return intel_guc_is_supported(guc) &&
4792 GRAPHICS_VER(guc_to_i915(guc)) >= 11;
4793 }
4794
__guc_submission_selected(struct intel_guc * guc)4795 static bool __guc_submission_selected(struct intel_guc *guc)
4796 {
4797 struct drm_i915_private *i915 = guc_to_i915(guc);
4798
4799 if (!intel_guc_submission_is_supported(guc))
4800 return false;
4801
4802 return i915->params.enable_guc & ENABLE_GUC_SUBMISSION;
4803 }
4804
intel_guc_sched_disable_gucid_threshold_max(struct intel_guc * guc)4805 int intel_guc_sched_disable_gucid_threshold_max(struct intel_guc *guc)
4806 {
4807 return guc->submission_state.num_guc_ids - NUMBER_MULTI_LRC_GUC_ID(guc);
4808 }
4809
4810 /*
4811 * This default value of 33 milisecs (+1 milisec round up) ensures 30fps or higher
4812 * workloads are able to enjoy the latency reduction when delaying the schedule-disable
4813 * operation. This matches the 30fps game-render + encode (real world) workload this
4814 * knob was tested against.
4815 */
4816 #define SCHED_DISABLE_DELAY_MS 34
4817
4818 /*
4819 * A threshold of 75% is a reasonable starting point considering that real world apps
4820 * generally don't get anywhere near this.
4821 */
4822 #define NUM_SCHED_DISABLE_GUCIDS_DEFAULT_THRESHOLD(__guc) \
4823 (((intel_guc_sched_disable_gucid_threshold_max(guc)) * 3) / 4)
4824
intel_guc_submission_init_early(struct intel_guc * guc)4825 void intel_guc_submission_init_early(struct intel_guc *guc)
4826 {
4827 xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);
4828
4829 spin_lock_init(&guc->submission_state.lock);
4830 INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
4831 ida_init(&guc->submission_state.guc_ids);
4832 INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
4833 INIT_WORK(&guc->submission_state.destroyed_worker,
4834 destroyed_worker_func);
4835 INIT_WORK(&guc->submission_state.reset_fail_worker,
4836 reset_fail_worker_func);
4837
4838 spin_lock_init(&guc->timestamp.lock);
4839 INIT_DELAYED_WORK(&guc->timestamp.work, guc_timestamp_ping);
4840
4841 guc->submission_state.sched_disable_delay_ms = SCHED_DISABLE_DELAY_MS;
4842 guc->submission_state.num_guc_ids = GUC_MAX_CONTEXT_ID;
4843 guc->submission_state.sched_disable_gucid_threshold =
4844 NUM_SCHED_DISABLE_GUCIDS_DEFAULT_THRESHOLD(guc);
4845 guc->submission_supported = __guc_submission_supported(guc);
4846 guc->submission_selected = __guc_submission_selected(guc);
4847 }
4848
4849 static inline struct intel_context *
g2h_context_lookup(struct intel_guc * guc,u32 ctx_id)4850 g2h_context_lookup(struct intel_guc *guc, u32 ctx_id)
4851 {
4852 struct intel_context *ce;
4853
4854 if (unlikely(ctx_id >= GUC_MAX_CONTEXT_ID)) {
4855 guc_err(guc, "Invalid ctx_id %u\n", ctx_id);
4856 return NULL;
4857 }
4858
4859 ce = __get_context(guc, ctx_id);
4860 if (unlikely(!ce)) {
4861 guc_err(guc, "Context is NULL, ctx_id %u\n", ctx_id);
4862 return NULL;
4863 }
4864
4865 if (unlikely(intel_context_is_child(ce))) {
4866 guc_err(guc, "Context is child, ctx_id %u\n", ctx_id);
4867 return NULL;
4868 }
4869
4870 return ce;
4871 }
4872
wait_wake_outstanding_tlb_g2h(struct intel_guc * guc,u32 seqno)4873 static void wait_wake_outstanding_tlb_g2h(struct intel_guc *guc, u32 seqno)
4874 {
4875 struct intel_guc_tlb_wait *wait;
4876 unsigned long flags;
4877
4878 xa_lock_irqsave(&guc->tlb_lookup, flags);
4879 wait = xa_load(&guc->tlb_lookup, seqno);
4880
4881 if (wait)
4882 wake_up(&wait->wq);
4883 else
4884 guc_dbg(guc,
4885 "Stale TLB invalidation response with seqno %d\n", seqno);
4886
4887 xa_unlock_irqrestore(&guc->tlb_lookup, flags);
4888 }
4889
intel_guc_tlb_invalidation_done(struct intel_guc * guc,const u32 * payload,u32 len)4890 int intel_guc_tlb_invalidation_done(struct intel_guc *guc,
4891 const u32 *payload, u32 len)
4892 {
4893 if (len < 1)
4894 return -EPROTO;
4895
4896 wait_wake_outstanding_tlb_g2h(guc, payload[0]);
4897 return 0;
4898 }
4899
must_wait_woken(struct wait_queue_entry * wq_entry,long timeout)4900 static long must_wait_woken(struct wait_queue_entry *wq_entry, long timeout)
4901 {
4902 /*
4903 * This is equivalent to wait_woken() with the exception that
4904 * we do not wake up early if the kthread task has been completed.
4905 * As we are called from page reclaim in any task context,
4906 * we may be invoked from stopped kthreads, but we *must*
4907 * complete the wait from the HW.
4908 */
4909 do {
4910 set_current_state(TASK_UNINTERRUPTIBLE);
4911 if (wq_entry->flags & WQ_FLAG_WOKEN)
4912 break;
4913
4914 timeout = schedule_timeout(timeout);
4915 } while (timeout);
4916
4917 /* See wait_woken() and woken_wake_function() */
4918 __set_current_state(TASK_RUNNING);
4919 smp_store_mb(wq_entry->flags, wq_entry->flags & ~WQ_FLAG_WOKEN);
4920
4921 return timeout;
4922 }
4923
intel_gt_is_enabled(const struct intel_gt * gt)4924 static bool intel_gt_is_enabled(const struct intel_gt *gt)
4925 {
4926 /* Check if GT is wedged or suspended */
4927 if (intel_gt_is_wedged(gt) || !intel_irqs_enabled(gt->i915))
4928 return false;
4929 return true;
4930 }
4931
guc_send_invalidate_tlb(struct intel_guc * guc,enum intel_guc_tlb_invalidation_type type)4932 static int guc_send_invalidate_tlb(struct intel_guc *guc,
4933 enum intel_guc_tlb_invalidation_type type)
4934 {
4935 struct intel_guc_tlb_wait _wq, *wq = &_wq;
4936 struct intel_gt *gt = guc_to_gt(guc);
4937 DEFINE_WAIT_FUNC(wait, woken_wake_function);
4938 int err;
4939 u32 seqno;
4940 u32 action[] = {
4941 INTEL_GUC_ACTION_TLB_INVALIDATION,
4942 0,
4943 REG_FIELD_PREP(INTEL_GUC_TLB_INVAL_TYPE_MASK, type) |
4944 REG_FIELD_PREP(INTEL_GUC_TLB_INVAL_MODE_MASK,
4945 INTEL_GUC_TLB_INVAL_MODE_HEAVY) |
4946 INTEL_GUC_TLB_INVAL_FLUSH_CACHE,
4947 };
4948 u32 size = ARRAY_SIZE(action);
4949
4950 /*
4951 * Early guard against GT enablement. TLB invalidation should not be
4952 * attempted if the GT is disabled due to suspend/wedge.
4953 */
4954 if (!intel_gt_is_enabled(gt))
4955 return -EINVAL;
4956
4957 init_waitqueue_head(&_wq.wq);
4958
4959 if (xa_alloc_cyclic_irq(&guc->tlb_lookup, &seqno, wq,
4960 xa_limit_32b, &guc->next_seqno,
4961 GFP_ATOMIC | __GFP_NOWARN) < 0) {
4962 /* Under severe memory pressure? Serialise TLB allocations */
4963 xa_lock_irq(&guc->tlb_lookup);
4964 wq = xa_load(&guc->tlb_lookup, guc->serial_slot);
4965 wait_event_lock_irq(wq->wq,
4966 !READ_ONCE(wq->busy),
4967 guc->tlb_lookup.xa_lock);
4968 /*
4969 * Update wq->busy under lock to ensure only one waiter can
4970 * issue the TLB invalidation command using the serial slot at a
4971 * time. The condition is set to true before releasing the lock
4972 * so that other caller continue to wait until woken up again.
4973 */
4974 wq->busy = true;
4975 xa_unlock_irq(&guc->tlb_lookup);
4976
4977 seqno = guc->serial_slot;
4978 }
4979
4980 action[1] = seqno;
4981
4982 add_wait_queue(&wq->wq, &wait);
4983
4984 /* This is a critical reclaim path and thus we must loop here. */
4985 err = intel_guc_send_busy_loop(guc, action, size, G2H_LEN_DW_INVALIDATE_TLB, true);
4986 if (err)
4987 goto out;
4988
4989 /*
4990 * Late guard against GT enablement. It is not an error for the TLB
4991 * invalidation to time out if the GT is disabled during the process
4992 * due to suspend/wedge. In fact, the TLB invalidation is cancelled
4993 * in this case.
4994 */
4995 if (!must_wait_woken(&wait, intel_guc_ct_max_queue_time_jiffies()) &&
4996 intel_gt_is_enabled(gt)) {
4997 guc_err(guc,
4998 "TLB invalidation response timed out for seqno %u\n", seqno);
4999 err = -ETIME;
5000 }
5001 out:
5002 remove_wait_queue(&wq->wq, &wait);
5003 if (seqno != guc->serial_slot)
5004 xa_erase_irq(&guc->tlb_lookup, seqno);
5005
5006 return err;
5007 }
5008
5009 /* Send a H2G command to invalidate the TLBs at engine level and beyond. */
intel_guc_invalidate_tlb_engines(struct intel_guc * guc)5010 int intel_guc_invalidate_tlb_engines(struct intel_guc *guc)
5011 {
5012 return guc_send_invalidate_tlb(guc, INTEL_GUC_TLB_INVAL_ENGINES);
5013 }
5014
5015 /* Send a H2G command to invalidate the GuC's internal TLB. */
intel_guc_invalidate_tlb_guc(struct intel_guc * guc)5016 int intel_guc_invalidate_tlb_guc(struct intel_guc *guc)
5017 {
5018 return guc_send_invalidate_tlb(guc, INTEL_GUC_TLB_INVAL_GUC);
5019 }
5020
intel_guc_deregister_done_process_msg(struct intel_guc * guc,const u32 * msg,u32 len)5021 int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
5022 const u32 *msg,
5023 u32 len)
5024 {
5025 struct intel_context *ce;
5026 u32 ctx_id;
5027
5028 if (unlikely(len < 1)) {
5029 guc_err(guc, "Invalid length %u\n", len);
5030 return -EPROTO;
5031 }
5032 ctx_id = msg[0];
5033
5034 ce = g2h_context_lookup(guc, ctx_id);
5035 if (unlikely(!ce))
5036 return -EPROTO;
5037
5038 trace_intel_context_deregister_done(ce);
5039
5040 #ifdef CONFIG_DRM_I915_SELFTEST
5041 if (unlikely(ce->drop_deregister)) {
5042 ce->drop_deregister = false;
5043 return 0;
5044 }
5045 #endif
5046
5047 if (context_wait_for_deregister_to_register(ce)) {
5048 struct intel_runtime_pm *runtime_pm =
5049 &ce->engine->gt->i915->runtime_pm;
5050 intel_wakeref_t wakeref;
5051
5052 /*
5053 * Previous owner of this guc_id has been deregistered, now safe
5054 * register this context.
5055 */
5056 with_intel_runtime_pm(runtime_pm, wakeref)
5057 register_context(ce, true);
5058 guc_signal_context_fence(ce);
5059 intel_context_put(ce);
5060 } else if (context_destroyed(ce)) {
5061 /* Context has been destroyed */
5062 intel_gt_pm_put_async_untracked(guc_to_gt(guc));
5063 release_guc_id(guc, ce);
5064 __guc_context_destroy(ce);
5065 }
5066
5067 decr_outstanding_submission_g2h(guc);
5068
5069 return 0;
5070 }
5071
intel_guc_sched_done_process_msg(struct intel_guc * guc,const u32 * msg,u32 len)5072 int intel_guc_sched_done_process_msg(struct intel_guc *guc,
5073 const u32 *msg,
5074 u32 len)
5075 {
5076 struct intel_context *ce;
5077 unsigned long flags;
5078 u32 ctx_id;
5079
5080 if (unlikely(len < 2)) {
5081 guc_err(guc, "Invalid length %u\n", len);
5082 return -EPROTO;
5083 }
5084 ctx_id = msg[0];
5085
5086 ce = g2h_context_lookup(guc, ctx_id);
5087 if (unlikely(!ce))
5088 return -EPROTO;
5089
5090 if (unlikely(context_destroyed(ce) ||
5091 (!context_pending_enable(ce) &&
5092 !context_pending_disable(ce)))) {
5093 guc_err(guc, "Bad context sched_state 0x%x, ctx_id %u\n",
5094 ce->guc_state.sched_state, ctx_id);
5095 return -EPROTO;
5096 }
5097
5098 trace_intel_context_sched_done(ce);
5099
5100 if (context_pending_enable(ce)) {
5101 #ifdef CONFIG_DRM_I915_SELFTEST
5102 if (unlikely(ce->drop_schedule_enable)) {
5103 ce->drop_schedule_enable = false;
5104 return 0;
5105 }
5106 #endif
5107
5108 spin_lock_irqsave(&ce->guc_state.lock, flags);
5109 clr_context_pending_enable(ce);
5110 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
5111 } else if (context_pending_disable(ce)) {
5112 bool banned;
5113
5114 #ifdef CONFIG_DRM_I915_SELFTEST
5115 if (unlikely(ce->drop_schedule_disable)) {
5116 ce->drop_schedule_disable = false;
5117 return 0;
5118 }
5119 #endif
5120
5121 /*
5122 * Unpin must be done before __guc_signal_context_fence,
5123 * otherwise a race exists between the requests getting
5124 * submitted + retired before this unpin completes resulting in
5125 * the pin_count going to zero and the context still being
5126 * enabled.
5127 */
5128 intel_context_sched_disable_unpin(ce);
5129
5130 spin_lock_irqsave(&ce->guc_state.lock, flags);
5131 banned = context_banned(ce);
5132 clr_context_banned(ce);
5133 clr_context_pending_disable(ce);
5134 __guc_signal_context_fence(ce);
5135 guc_blocked_fence_complete(ce);
5136 spin_unlock_irqrestore(&ce->guc_state.lock, flags);
5137
5138 if (banned) {
5139 guc_cancel_context_requests(ce);
5140 intel_engine_signal_breadcrumbs(ce->engine);
5141 }
5142 }
5143
5144 decr_outstanding_submission_g2h(guc);
5145 intel_context_put(ce);
5146
5147 return 0;
5148 }
5149
capture_error_state(struct intel_guc * guc,struct intel_context * ce)5150 static void capture_error_state(struct intel_guc *guc,
5151 struct intel_context *ce)
5152 {
5153 struct intel_gt *gt = guc_to_gt(guc);
5154 struct drm_i915_private *i915 = gt->i915;
5155 intel_wakeref_t wakeref;
5156 intel_engine_mask_t engine_mask;
5157
5158 if (intel_engine_is_virtual(ce->engine)) {
5159 struct intel_engine_cs *e;
5160 intel_engine_mask_t tmp, virtual_mask = ce->engine->mask;
5161
5162 engine_mask = 0;
5163 for_each_engine_masked(e, ce->engine->gt, virtual_mask, tmp) {
5164 bool match = intel_guc_capture_is_matching_engine(gt, ce, e);
5165
5166 if (match) {
5167 intel_engine_set_hung_context(e, ce);
5168 engine_mask |= e->mask;
5169 i915_increase_reset_engine_count(&i915->gpu_error,
5170 e);
5171 }
5172 }
5173
5174 if (!engine_mask) {
5175 guc_warn(guc, "No matching physical engine capture for virtual engine context 0x%04X / %s",
5176 ce->guc_id.id, ce->engine->name);
5177 engine_mask = ~0U;
5178 }
5179 } else {
5180 intel_engine_set_hung_context(ce->engine, ce);
5181 engine_mask = ce->engine->mask;
5182 i915_increase_reset_engine_count(&i915->gpu_error, ce->engine);
5183 }
5184
5185 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
5186 i915_capture_error_state(gt, engine_mask, CORE_DUMP_FLAG_IS_GUC_CAPTURE);
5187 }
5188
guc_context_replay(struct intel_context * ce)5189 static void guc_context_replay(struct intel_context *ce)
5190 {
5191 struct i915_sched_engine *sched_engine = ce->engine->sched_engine;
5192
5193 __guc_reset_context(ce, ce->engine->mask);
5194 tasklet_hi_schedule(&sched_engine->tasklet);
5195 }
5196
guc_handle_context_reset(struct intel_guc * guc,struct intel_context * ce)5197 static void guc_handle_context_reset(struct intel_guc *guc,
5198 struct intel_context *ce)
5199 {
5200 bool capture = intel_context_is_schedulable(ce);
5201
5202 trace_intel_context_reset(ce);
5203
5204 guc_dbg(guc, "%s context reset notification: 0x%04X on %s, exiting = %s, banned = %s\n",
5205 capture ? "Got" : "Ignoring",
5206 ce->guc_id.id, ce->engine->name,
5207 str_yes_no(intel_context_is_exiting(ce)),
5208 str_yes_no(intel_context_is_banned(ce)));
5209
5210 if (capture) {
5211 capture_error_state(guc, ce);
5212 guc_context_replay(ce);
5213 }
5214 }
5215
intel_guc_context_reset_process_msg(struct intel_guc * guc,const u32 * msg,u32 len)5216 int intel_guc_context_reset_process_msg(struct intel_guc *guc,
5217 const u32 *msg, u32 len)
5218 {
5219 struct intel_context *ce;
5220 unsigned long flags;
5221 int ctx_id;
5222
5223 if (unlikely(len != 1)) {
5224 guc_err(guc, "Invalid length %u", len);
5225 return -EPROTO;
5226 }
5227
5228 ctx_id = msg[0];
5229
5230 /*
5231 * The context lookup uses the xarray but lookups only require an RCU lock
5232 * not the full spinlock. So take the lock explicitly and keep it until the
5233 * context has been reference count locked to ensure it can't be destroyed
5234 * asynchronously until the reset is done.
5235 */
5236 xa_lock_irqsave(&guc->context_lookup, flags);
5237 ce = g2h_context_lookup(guc, ctx_id);
5238 if (ce)
5239 intel_context_get(ce);
5240 xa_unlock_irqrestore(&guc->context_lookup, flags);
5241
5242 if (unlikely(!ce))
5243 return -EPROTO;
5244
5245 guc_handle_context_reset(guc, ce);
5246 intel_context_put(ce);
5247
5248 return 0;
5249 }
5250
intel_guc_error_capture_process_msg(struct intel_guc * guc,const u32 * msg,u32 len)5251 int intel_guc_error_capture_process_msg(struct intel_guc *guc,
5252 const u32 *msg, u32 len)
5253 {
5254 u32 status;
5255
5256 if (unlikely(len != 1)) {
5257 guc_dbg(guc, "Invalid length %u", len);
5258 return -EPROTO;
5259 }
5260
5261 status = msg[0] & INTEL_GUC_STATE_CAPTURE_EVENT_STATUS_MASK;
5262 if (status == INTEL_GUC_STATE_CAPTURE_EVENT_STATUS_NOSPACE)
5263 guc_warn(guc, "No space for error capture");
5264
5265 intel_guc_capture_process(guc);
5266
5267 return 0;
5268 }
5269
5270 struct intel_engine_cs *
intel_guc_lookup_engine(struct intel_guc * guc,u8 guc_class,u8 instance)5271 intel_guc_lookup_engine(struct intel_guc *guc, u8 guc_class, u8 instance)
5272 {
5273 struct intel_gt *gt = guc_to_gt(guc);
5274 u8 engine_class = guc_class_to_engine_class(guc_class);
5275
5276 /* Class index is checked in class converter */
5277 GEM_BUG_ON(instance > MAX_ENGINE_INSTANCE);
5278
5279 return gt->engine_class[engine_class][instance];
5280 }
5281
reset_fail_worker_func(struct work_struct * w)5282 static void reset_fail_worker_func(struct work_struct *w)
5283 {
5284 struct intel_guc *guc = container_of(w, struct intel_guc,
5285 submission_state.reset_fail_worker);
5286 struct intel_gt *gt = guc_to_gt(guc);
5287 intel_engine_mask_t reset_fail_mask;
5288 unsigned long flags;
5289
5290 spin_lock_irqsave(&guc->submission_state.lock, flags);
5291 reset_fail_mask = guc->submission_state.reset_fail_mask;
5292 guc->submission_state.reset_fail_mask = 0;
5293 spin_unlock_irqrestore(&guc->submission_state.lock, flags);
5294
5295 if (likely(reset_fail_mask)) {
5296 struct intel_engine_cs *engine;
5297 enum intel_engine_id id;
5298
5299 /*
5300 * GuC is toast at this point - it dead loops after sending the failed
5301 * reset notification. So need to manually determine the guilty context.
5302 * Note that it should be reliable to do this here because the GuC is
5303 * toast and will not be scheduling behind the KMD's back.
5304 */
5305 for_each_engine_masked(engine, gt, reset_fail_mask, id)
5306 intel_guc_find_hung_context(engine);
5307
5308 intel_gt_handle_error(gt, reset_fail_mask,
5309 I915_ERROR_CAPTURE,
5310 "GuC failed to reset engine mask=0x%x",
5311 reset_fail_mask);
5312 }
5313 }
5314
intel_guc_engine_failure_process_msg(struct intel_guc * guc,const u32 * msg,u32 len)5315 int intel_guc_engine_failure_process_msg(struct intel_guc *guc,
5316 const u32 *msg, u32 len)
5317 {
5318 struct intel_engine_cs *engine;
5319 u8 guc_class, instance;
5320 u32 reason;
5321 unsigned long flags;
5322
5323 if (unlikely(len != 3)) {
5324 guc_err(guc, "Invalid length %u", len);
5325 return -EPROTO;
5326 }
5327
5328 guc_class = msg[0];
5329 instance = msg[1];
5330 reason = msg[2];
5331
5332 engine = intel_guc_lookup_engine(guc, guc_class, instance);
5333 if (unlikely(!engine)) {
5334 guc_err(guc, "Invalid engine %d:%d", guc_class, instance);
5335 return -EPROTO;
5336 }
5337
5338 /*
5339 * This is an unexpected failure of a hardware feature. So, log a real
5340 * error message not just the informational that comes with the reset.
5341 */
5342 guc_err(guc, "Engine reset failed on %d:%d (%s) because 0x%08X",
5343 guc_class, instance, engine->name, reason);
5344
5345 spin_lock_irqsave(&guc->submission_state.lock, flags);
5346 guc->submission_state.reset_fail_mask |= engine->mask;
5347 spin_unlock_irqrestore(&guc->submission_state.lock, flags);
5348
5349 /*
5350 * A GT reset flushes this worker queue (G2H handler) so we must use
5351 * another worker to trigger a GT reset.
5352 */
5353 queue_work(system_unbound_wq, &guc->submission_state.reset_fail_worker);
5354
5355 return 0;
5356 }
5357
intel_guc_find_hung_context(struct intel_engine_cs * engine)5358 void intel_guc_find_hung_context(struct intel_engine_cs *engine)
5359 {
5360 struct intel_guc *guc = gt_to_guc(engine->gt);
5361 struct intel_context *ce;
5362 struct i915_request *rq;
5363 unsigned long index;
5364 unsigned long flags;
5365
5366 /* Reset called during driver load? GuC not yet initialised! */
5367 if (unlikely(!guc_submission_initialized(guc)))
5368 return;
5369
5370 xa_lock_irqsave(&guc->context_lookup, flags);
5371 xa_for_each(&guc->context_lookup, index, ce) {
5372 bool found;
5373
5374 if (!kref_get_unless_zero(&ce->ref))
5375 continue;
5376
5377 xa_unlock(&guc->context_lookup);
5378
5379 if (!intel_context_is_pinned(ce))
5380 goto next;
5381
5382 if (intel_engine_is_virtual(ce->engine)) {
5383 if (!(ce->engine->mask & engine->mask))
5384 goto next;
5385 } else {
5386 if (ce->engine != engine)
5387 goto next;
5388 }
5389
5390 found = false;
5391 spin_lock(&ce->guc_state.lock);
5392 list_for_each_entry(rq, &ce->guc_state.requests, sched.link) {
5393 if (i915_test_request_state(rq) != I915_REQUEST_ACTIVE)
5394 continue;
5395
5396 found = true;
5397 break;
5398 }
5399 spin_unlock(&ce->guc_state.lock);
5400
5401 if (found) {
5402 intel_engine_set_hung_context(engine, ce);
5403
5404 /* Can only cope with one hang at a time... */
5405 intel_context_put(ce);
5406 xa_lock(&guc->context_lookup);
5407 goto done;
5408 }
5409
5410 next:
5411 intel_context_put(ce);
5412 xa_lock(&guc->context_lookup);
5413 }
5414 done:
5415 xa_unlock_irqrestore(&guc->context_lookup, flags);
5416 }
5417
intel_guc_dump_active_requests(struct intel_engine_cs * engine,struct i915_request * hung_rq,struct drm_printer * m)5418 void intel_guc_dump_active_requests(struct intel_engine_cs *engine,
5419 struct i915_request *hung_rq,
5420 struct drm_printer *m)
5421 {
5422 struct intel_guc *guc = gt_to_guc(engine->gt);
5423 struct intel_context *ce;
5424 unsigned long index;
5425 unsigned long flags;
5426
5427 /* Reset called during driver load? GuC not yet initialised! */
5428 if (unlikely(!guc_submission_initialized(guc)))
5429 return;
5430
5431 xa_lock_irqsave(&guc->context_lookup, flags);
5432 xa_for_each(&guc->context_lookup, index, ce) {
5433 if (!kref_get_unless_zero(&ce->ref))
5434 continue;
5435
5436 xa_unlock(&guc->context_lookup);
5437
5438 if (!intel_context_is_pinned(ce))
5439 goto next;
5440
5441 if (intel_engine_is_virtual(ce->engine)) {
5442 if (!(ce->engine->mask & engine->mask))
5443 goto next;
5444 } else {
5445 if (ce->engine != engine)
5446 goto next;
5447 }
5448
5449 spin_lock(&ce->guc_state.lock);
5450 intel_engine_dump_active_requests(&ce->guc_state.requests,
5451 hung_rq, m);
5452 spin_unlock(&ce->guc_state.lock);
5453
5454 next:
5455 intel_context_put(ce);
5456 xa_lock(&guc->context_lookup);
5457 }
5458 xa_unlock_irqrestore(&guc->context_lookup, flags);
5459 }
5460
intel_guc_submission_print_info(struct intel_guc * guc,struct drm_printer * p)5461 void intel_guc_submission_print_info(struct intel_guc *guc,
5462 struct drm_printer *p)
5463 {
5464 struct i915_sched_engine *sched_engine = guc->sched_engine;
5465 struct rb_node *rb;
5466 unsigned long flags;
5467
5468 if (!sched_engine)
5469 return;
5470
5471 drm_printf(p, "GuC Submission API Version: %d.%d.%d\n",
5472 guc->submission_version.major, guc->submission_version.minor,
5473 guc->submission_version.patch);
5474 drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
5475 atomic_read(&guc->outstanding_submission_g2h));
5476 drm_printf(p, "GuC tasklet count: %u\n",
5477 atomic_read(&sched_engine->tasklet.count));
5478
5479 spin_lock_irqsave(&sched_engine->lock, flags);
5480 drm_printf(p, "Requests in GuC submit tasklet:\n");
5481 for (rb = rb_first_cached(&sched_engine->queue); rb; rb = rb_next(rb)) {
5482 struct i915_priolist *pl = to_priolist(rb);
5483 struct i915_request *rq;
5484
5485 priolist_for_each_request(rq, pl)
5486 drm_printf(p, "guc_id=%u, seqno=%llu\n",
5487 rq->context->guc_id.id,
5488 rq->fence.seqno);
5489 }
5490 spin_unlock_irqrestore(&sched_engine->lock, flags);
5491 drm_printf(p, "\n");
5492 }
5493
guc_log_context_priority(struct drm_printer * p,struct intel_context * ce)5494 static inline void guc_log_context_priority(struct drm_printer *p,
5495 struct intel_context *ce)
5496 {
5497 int i;
5498
5499 drm_printf(p, "\t\tPriority: %d\n", ce->guc_state.prio);
5500 drm_printf(p, "\t\tNumber Requests (lower index == higher priority)\n");
5501 for (i = GUC_CLIENT_PRIORITY_KMD_HIGH;
5502 i < GUC_CLIENT_PRIORITY_NUM; ++i) {
5503 drm_printf(p, "\t\tNumber requests in priority band[%d]: %d\n",
5504 i, ce->guc_state.prio_count[i]);
5505 }
5506 drm_printf(p, "\n");
5507 }
5508
guc_log_context(struct drm_printer * p,struct intel_context * ce)5509 static inline void guc_log_context(struct drm_printer *p,
5510 struct intel_context *ce)
5511 {
5512 drm_printf(p, "GuC lrc descriptor %u:\n", ce->guc_id.id);
5513 drm_printf(p, "\tHW Context Desc: 0x%08x\n", ce->lrc.lrca);
5514 drm_printf(p, "\t\tLRC Head: Internal %u, Memory %u\n",
5515 ce->ring->head,
5516 ce->lrc_reg_state[CTX_RING_HEAD]);
5517 drm_printf(p, "\t\tLRC Tail: Internal %u, Memory %u\n",
5518 ce->ring->tail,
5519 ce->lrc_reg_state[CTX_RING_TAIL]);
5520 drm_printf(p, "\t\tContext Pin Count: %u\n",
5521 atomic_read(&ce->pin_count));
5522 drm_printf(p, "\t\tGuC ID Ref Count: %u\n",
5523 atomic_read(&ce->guc_id.ref));
5524 drm_printf(p, "\t\tSchedule State: 0x%x\n",
5525 ce->guc_state.sched_state);
5526 }
5527
intel_guc_submission_print_context_info(struct intel_guc * guc,struct drm_printer * p)5528 void intel_guc_submission_print_context_info(struct intel_guc *guc,
5529 struct drm_printer *p)
5530 {
5531 struct intel_context *ce;
5532 unsigned long index;
5533 unsigned long flags;
5534
5535 xa_lock_irqsave(&guc->context_lookup, flags);
5536 xa_for_each(&guc->context_lookup, index, ce) {
5537 GEM_BUG_ON(intel_context_is_child(ce));
5538
5539 guc_log_context(p, ce);
5540 guc_log_context_priority(p, ce);
5541
5542 if (intel_context_is_parent(ce)) {
5543 struct intel_context *child;
5544
5545 drm_printf(p, "\t\tNumber children: %u\n",
5546 ce->parallel.number_children);
5547
5548 if (ce->parallel.guc.wq_status) {
5549 drm_printf(p, "\t\tWQI Head: %u\n",
5550 READ_ONCE(*ce->parallel.guc.wq_head));
5551 drm_printf(p, "\t\tWQI Tail: %u\n",
5552 READ_ONCE(*ce->parallel.guc.wq_tail));
5553 drm_printf(p, "\t\tWQI Status: %u\n",
5554 READ_ONCE(*ce->parallel.guc.wq_status));
5555 }
5556
5557 if (ce->engine->emit_bb_start ==
5558 emit_bb_start_parent_no_preempt_mid_batch) {
5559 u8 i;
5560
5561 drm_printf(p, "\t\tChildren Go: %u\n",
5562 get_children_go_value(ce));
5563 for (i = 0; i < ce->parallel.number_children; ++i)
5564 drm_printf(p, "\t\tChildren Join: %u\n",
5565 get_children_join_value(ce, i));
5566 }
5567
5568 for_each_child(ce, child)
5569 guc_log_context(p, child);
5570 }
5571 }
5572 xa_unlock_irqrestore(&guc->context_lookup, flags);
5573 }
5574
get_children_go_addr(struct intel_context * ce)5575 static inline u32 get_children_go_addr(struct intel_context *ce)
5576 {
5577 GEM_BUG_ON(!intel_context_is_parent(ce));
5578
5579 return i915_ggtt_offset(ce->state) +
5580 __get_parent_scratch_offset(ce) +
5581 offsetof(struct parent_scratch, go.semaphore);
5582 }
5583
get_children_join_addr(struct intel_context * ce,u8 child_index)5584 static inline u32 get_children_join_addr(struct intel_context *ce,
5585 u8 child_index)
5586 {
5587 GEM_BUG_ON(!intel_context_is_parent(ce));
5588
5589 return i915_ggtt_offset(ce->state) +
5590 __get_parent_scratch_offset(ce) +
5591 offsetof(struct parent_scratch, join[child_index].semaphore);
5592 }
5593
5594 #define PARENT_GO_BB 1
5595 #define PARENT_GO_FINI_BREADCRUMB 0
5596 #define CHILD_GO_BB 1
5597 #define CHILD_GO_FINI_BREADCRUMB 0
emit_bb_start_parent_no_preempt_mid_batch(struct i915_request * rq,u64 offset,u32 len,const unsigned int flags)5598 static int emit_bb_start_parent_no_preempt_mid_batch(struct i915_request *rq,
5599 u64 offset, u32 len,
5600 const unsigned int flags)
5601 {
5602 struct intel_context *ce = rq->context;
5603 u32 *cs;
5604 u8 i;
5605
5606 GEM_BUG_ON(!intel_context_is_parent(ce));
5607
5608 cs = intel_ring_begin(rq, 10 + 4 * ce->parallel.number_children);
5609 if (IS_ERR(cs))
5610 return PTR_ERR(cs);
5611
5612 /* Wait on children */
5613 for (i = 0; i < ce->parallel.number_children; ++i) {
5614 *cs++ = (MI_SEMAPHORE_WAIT |
5615 MI_SEMAPHORE_GLOBAL_GTT |
5616 MI_SEMAPHORE_POLL |
5617 MI_SEMAPHORE_SAD_EQ_SDD);
5618 *cs++ = PARENT_GO_BB;
5619 *cs++ = get_children_join_addr(ce, i);
5620 *cs++ = 0;
5621 }
5622
5623 /* Turn off preemption */
5624 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
5625 *cs++ = MI_NOOP;
5626
5627 /* Tell children go */
5628 cs = gen8_emit_ggtt_write(cs,
5629 CHILD_GO_BB,
5630 get_children_go_addr(ce),
5631 0);
5632
5633 /* Jump to batch */
5634 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
5635 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8));
5636 *cs++ = lower_32_bits(offset);
5637 *cs++ = upper_32_bits(offset);
5638 *cs++ = MI_NOOP;
5639
5640 intel_ring_advance(rq, cs);
5641
5642 return 0;
5643 }
5644
emit_bb_start_child_no_preempt_mid_batch(struct i915_request * rq,u64 offset,u32 len,const unsigned int flags)5645 static int emit_bb_start_child_no_preempt_mid_batch(struct i915_request *rq,
5646 u64 offset, u32 len,
5647 const unsigned int flags)
5648 {
5649 struct intel_context *ce = rq->context;
5650 struct intel_context *parent = intel_context_to_parent(ce);
5651 u32 *cs;
5652
5653 GEM_BUG_ON(!intel_context_is_child(ce));
5654
5655 cs = intel_ring_begin(rq, 12);
5656 if (IS_ERR(cs))
5657 return PTR_ERR(cs);
5658
5659 /* Signal parent */
5660 cs = gen8_emit_ggtt_write(cs,
5661 PARENT_GO_BB,
5662 get_children_join_addr(parent,
5663 ce->parallel.child_index),
5664 0);
5665
5666 /* Wait on parent for go */
5667 *cs++ = (MI_SEMAPHORE_WAIT |
5668 MI_SEMAPHORE_GLOBAL_GTT |
5669 MI_SEMAPHORE_POLL |
5670 MI_SEMAPHORE_SAD_EQ_SDD);
5671 *cs++ = CHILD_GO_BB;
5672 *cs++ = get_children_go_addr(parent);
5673 *cs++ = 0;
5674
5675 /* Turn off preemption */
5676 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
5677
5678 /* Jump to batch */
5679 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
5680 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8));
5681 *cs++ = lower_32_bits(offset);
5682 *cs++ = upper_32_bits(offset);
5683
5684 intel_ring_advance(rq, cs);
5685
5686 return 0;
5687 }
5688
5689 static u32 *
__emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request * rq,u32 * cs)5690 __emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq,
5691 u32 *cs)
5692 {
5693 struct intel_context *ce = rq->context;
5694 u8 i;
5695
5696 GEM_BUG_ON(!intel_context_is_parent(ce));
5697
5698 /* Wait on children */
5699 for (i = 0; i < ce->parallel.number_children; ++i) {
5700 *cs++ = (MI_SEMAPHORE_WAIT |
5701 MI_SEMAPHORE_GLOBAL_GTT |
5702 MI_SEMAPHORE_POLL |
5703 MI_SEMAPHORE_SAD_EQ_SDD);
5704 *cs++ = PARENT_GO_FINI_BREADCRUMB;
5705 *cs++ = get_children_join_addr(ce, i);
5706 *cs++ = 0;
5707 }
5708
5709 /* Turn on preemption */
5710 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
5711 *cs++ = MI_NOOP;
5712
5713 /* Tell children go */
5714 cs = gen8_emit_ggtt_write(cs,
5715 CHILD_GO_FINI_BREADCRUMB,
5716 get_children_go_addr(ce),
5717 0);
5718
5719 return cs;
5720 }
5721
5722 /*
5723 * If this true, a submission of multi-lrc requests had an error and the
5724 * requests need to be skipped. The front end (execuf IOCTL) should've called
5725 * i915_request_skip which squashes the BB but we still need to emit the fini
5726 * breadrcrumbs seqno write. At this point we don't know how many of the
5727 * requests in the multi-lrc submission were generated so we can't do the
5728 * handshake between the parent and children (e.g. if 4 requests should be
5729 * generated but 2nd hit an error only 1 would be seen by the GuC backend).
5730 * Simply skip the handshake, but still emit the breadcrumbd seqno, if an error
5731 * has occurred on any of the requests in submission / relationship.
5732 */
skip_handshake(struct i915_request * rq)5733 static inline bool skip_handshake(struct i915_request *rq)
5734 {
5735 return test_bit(I915_FENCE_FLAG_SKIP_PARALLEL, &rq->fence.flags);
5736 }
5737
5738 #define NON_SKIP_LEN 6
5739 static u32 *
emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request * rq,u32 * cs)5740 emit_fini_breadcrumb_parent_no_preempt_mid_batch(struct i915_request *rq,
5741 u32 *cs)
5742 {
5743 struct intel_context *ce = rq->context;
5744 __maybe_unused u32 *before_fini_breadcrumb_user_interrupt_cs;
5745 __maybe_unused u32 *start_fini_breadcrumb_cs = cs;
5746
5747 GEM_BUG_ON(!intel_context_is_parent(ce));
5748
5749 if (unlikely(skip_handshake(rq))) {
5750 /*
5751 * NOP everything in __emit_fini_breadcrumb_parent_no_preempt_mid_batch,
5752 * the NON_SKIP_LEN comes from the length of the emits below.
5753 */
5754 memset(cs, 0, sizeof(u32) *
5755 (ce->engine->emit_fini_breadcrumb_dw - NON_SKIP_LEN));
5756 cs += ce->engine->emit_fini_breadcrumb_dw - NON_SKIP_LEN;
5757 } else {
5758 cs = __emit_fini_breadcrumb_parent_no_preempt_mid_batch(rq, cs);
5759 }
5760
5761 /* Emit fini breadcrumb */
5762 before_fini_breadcrumb_user_interrupt_cs = cs;
5763 cs = gen8_emit_ggtt_write(cs,
5764 rq->fence.seqno,
5765 i915_request_active_timeline(rq)->hwsp_offset,
5766 0);
5767
5768 /* User interrupt */
5769 *cs++ = MI_USER_INTERRUPT;
5770 *cs++ = MI_NOOP;
5771
5772 /* Ensure our math for skip + emit is correct */
5773 GEM_BUG_ON(before_fini_breadcrumb_user_interrupt_cs + NON_SKIP_LEN !=
5774 cs);
5775 GEM_BUG_ON(start_fini_breadcrumb_cs +
5776 ce->engine->emit_fini_breadcrumb_dw != cs);
5777
5778 rq->tail = intel_ring_offset(rq, cs);
5779
5780 return cs;
5781 }
5782
5783 static u32 *
__emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request * rq,u32 * cs)5784 __emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq,
5785 u32 *cs)
5786 {
5787 struct intel_context *ce = rq->context;
5788 struct intel_context *parent = intel_context_to_parent(ce);
5789
5790 GEM_BUG_ON(!intel_context_is_child(ce));
5791
5792 /* Turn on preemption */
5793 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
5794 *cs++ = MI_NOOP;
5795
5796 /* Signal parent */
5797 cs = gen8_emit_ggtt_write(cs,
5798 PARENT_GO_FINI_BREADCRUMB,
5799 get_children_join_addr(parent,
5800 ce->parallel.child_index),
5801 0);
5802
5803 /* Wait parent on for go */
5804 *cs++ = (MI_SEMAPHORE_WAIT |
5805 MI_SEMAPHORE_GLOBAL_GTT |
5806 MI_SEMAPHORE_POLL |
5807 MI_SEMAPHORE_SAD_EQ_SDD);
5808 *cs++ = CHILD_GO_FINI_BREADCRUMB;
5809 *cs++ = get_children_go_addr(parent);
5810 *cs++ = 0;
5811
5812 return cs;
5813 }
5814
5815 static u32 *
emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request * rq,u32 * cs)5816 emit_fini_breadcrumb_child_no_preempt_mid_batch(struct i915_request *rq,
5817 u32 *cs)
5818 {
5819 struct intel_context *ce = rq->context;
5820 __maybe_unused u32 *before_fini_breadcrumb_user_interrupt_cs;
5821 __maybe_unused u32 *start_fini_breadcrumb_cs = cs;
5822
5823 GEM_BUG_ON(!intel_context_is_child(ce));
5824
5825 if (unlikely(skip_handshake(rq))) {
5826 /*
5827 * NOP everything in __emit_fini_breadcrumb_child_no_preempt_mid_batch,
5828 * the NON_SKIP_LEN comes from the length of the emits below.
5829 */
5830 memset(cs, 0, sizeof(u32) *
5831 (ce->engine->emit_fini_breadcrumb_dw - NON_SKIP_LEN));
5832 cs += ce->engine->emit_fini_breadcrumb_dw - NON_SKIP_LEN;
5833 } else {
5834 cs = __emit_fini_breadcrumb_child_no_preempt_mid_batch(rq, cs);
5835 }
5836
5837 /* Emit fini breadcrumb */
5838 before_fini_breadcrumb_user_interrupt_cs = cs;
5839 cs = gen8_emit_ggtt_write(cs,
5840 rq->fence.seqno,
5841 i915_request_active_timeline(rq)->hwsp_offset,
5842 0);
5843
5844 /* User interrupt */
5845 *cs++ = MI_USER_INTERRUPT;
5846 *cs++ = MI_NOOP;
5847
5848 /* Ensure our math for skip + emit is correct */
5849 GEM_BUG_ON(before_fini_breadcrumb_user_interrupt_cs + NON_SKIP_LEN !=
5850 cs);
5851 GEM_BUG_ON(start_fini_breadcrumb_cs +
5852 ce->engine->emit_fini_breadcrumb_dw != cs);
5853
5854 rq->tail = intel_ring_offset(rq, cs);
5855
5856 return cs;
5857 }
5858
5859 #undef NON_SKIP_LEN
5860
5861 static struct intel_context *
guc_create_virtual(struct intel_engine_cs ** siblings,unsigned int count,unsigned long flags)5862 guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count,
5863 unsigned long flags)
5864 {
5865 struct guc_virtual_engine *ve;
5866 struct intel_guc *guc;
5867 unsigned int n;
5868 int err;
5869
5870 ve = kzalloc(sizeof(*ve), GFP_KERNEL);
5871 if (!ve)
5872 return ERR_PTR(-ENOMEM);
5873
5874 guc = gt_to_guc(siblings[0]->gt);
5875
5876 ve->base.i915 = siblings[0]->i915;
5877 ve->base.gt = siblings[0]->gt;
5878 ve->base.uncore = siblings[0]->uncore;
5879 ve->base.id = -1;
5880
5881 ve->base.uabi_class = I915_ENGINE_CLASS_INVALID;
5882 ve->base.instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
5883 ve->base.uabi_instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
5884 ve->base.saturated = ALL_ENGINES;
5885
5886 snprintf(ve->base.name, sizeof(ve->base.name), "virtual");
5887
5888 ve->base.sched_engine = i915_sched_engine_get(guc->sched_engine);
5889
5890 ve->base.cops = &virtual_guc_context_ops;
5891 ve->base.request_alloc = guc_request_alloc;
5892 ve->base.bump_serial = virtual_guc_bump_serial;
5893
5894 ve->base.submit_request = guc_submit_request;
5895
5896 ve->base.flags = I915_ENGINE_IS_VIRTUAL;
5897
5898 BUILD_BUG_ON(ilog2(VIRTUAL_ENGINES) < I915_NUM_ENGINES);
5899 ve->base.mask = VIRTUAL_ENGINES;
5900
5901 intel_context_init(&ve->context, &ve->base);
5902
5903 for (n = 0; n < count; n++) {
5904 struct intel_engine_cs *sibling = siblings[n];
5905
5906 GEM_BUG_ON(!is_power_of_2(sibling->mask));
5907 if (sibling->mask & ve->base.mask) {
5908 guc_dbg(guc, "duplicate %s entry in load balancer\n",
5909 sibling->name);
5910 err = -EINVAL;
5911 goto err_put;
5912 }
5913
5914 ve->base.mask |= sibling->mask;
5915 ve->base.logical_mask |= sibling->logical_mask;
5916
5917 if (n != 0 && ve->base.class != sibling->class) {
5918 guc_dbg(guc, "invalid mixing of engine class, sibling %d, already %d\n",
5919 sibling->class, ve->base.class);
5920 err = -EINVAL;
5921 goto err_put;
5922 } else if (n == 0) {
5923 ve->base.class = sibling->class;
5924 ve->base.uabi_class = sibling->uabi_class;
5925 snprintf(ve->base.name, sizeof(ve->base.name),
5926 "v%dx%d", ve->base.class, count);
5927 ve->base.context_size = sibling->context_size;
5928
5929 ve->base.add_active_request =
5930 sibling->add_active_request;
5931 ve->base.remove_active_request =
5932 sibling->remove_active_request;
5933 ve->base.emit_bb_start = sibling->emit_bb_start;
5934 ve->base.emit_flush = sibling->emit_flush;
5935 ve->base.emit_init_breadcrumb =
5936 sibling->emit_init_breadcrumb;
5937 ve->base.emit_fini_breadcrumb =
5938 sibling->emit_fini_breadcrumb;
5939 ve->base.emit_fini_breadcrumb_dw =
5940 sibling->emit_fini_breadcrumb_dw;
5941 ve->base.breadcrumbs =
5942 intel_breadcrumbs_get(sibling->breadcrumbs);
5943
5944 ve->base.flags |= sibling->flags;
5945
5946 ve->base.props.timeslice_duration_ms =
5947 sibling->props.timeslice_duration_ms;
5948 ve->base.props.preempt_timeout_ms =
5949 sibling->props.preempt_timeout_ms;
5950 }
5951 }
5952
5953 return &ve->context;
5954
5955 err_put:
5956 intel_context_put(&ve->context);
5957 return ERR_PTR(err);
5958 }
5959
intel_guc_virtual_engine_has_heartbeat(const struct intel_engine_cs * ve)5960 bool intel_guc_virtual_engine_has_heartbeat(const struct intel_engine_cs *ve)
5961 {
5962 struct intel_engine_cs *engine;
5963 intel_engine_mask_t tmp, mask = ve->mask;
5964
5965 for_each_engine_masked(engine, ve->gt, mask, tmp)
5966 if (READ_ONCE(engine->props.heartbeat_interval_ms))
5967 return true;
5968
5969 return false;
5970 }
5971
5972 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5973 #include "selftest_guc.c"
5974 #include "selftest_guc_multi_lrc.c"
5975 #include "selftest_guc_hangcheck.c"
5976 #endif
5977