2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include <linux/circ_buf.h>
26 #include <trace/events/dma_fence.h>
28 #include "intel_guc_submission.h"
32 * DOC: GuC-based command submission
35 * A intel_guc_client refers to a submission path through GuC. Currently, there
36 * are two clients. One of them (the execbuf_client) is charged with all
37 * submissions to the GuC, the other one (preempt_client) is responsible for
38 * preempting the execbuf_client. This struct is the owner of a doorbell, a
39 * process descriptor and a workqueue (all of them inside a single gem object
40 * that contains all required pages for these elements).
42 * GuC stage descriptor:
43 * During initialization, the driver allocates a static pool of 1024 such
44 * descriptors, and shares them with the GuC.
45 * Currently, there exists a 1:1 mapping between a intel_guc_client and a
46 * guc_stage_desc (via the client's stage_id), so effectively only one
47 * gets used. This stage descriptor lets the GuC know about the doorbell,
48 * workqueue and process descriptor. Theoretically, it also lets the GuC
49 * know about our HW contexts (context ID, etc...), but we actually
50 * employ a kind of submission where the GuC uses the LRCA sent via the work
51 * item instead (the single guc_stage_desc associated to execbuf client
52 * contains information about the default kernel context only, but this is
53 * essentially unused). This is called a "proxy" submission.
55 * The Scratch registers:
56 * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
57 * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
58 * triggers an interrupt on the GuC via another register write (0xC4C8).
59 * Firmware writes a success/fail code back to the action register after
60 * processes the request. The kernel driver polls waiting for this update and
62 * See intel_guc_send()
65 * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
66 * mapped into process space.
69 * There are several types of work items that the host may place into a
70 * workqueue, each with its own requirements and limitations. Currently only
71 * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
72 * represents in-order queue. The kernel driver packs ring tail pointer and an
73 * ELSP context descriptor dword into Work Item.
74 * See guc_add_request()
77 * The Additional Data Struct (ADS) has pointers for different buffers used by
78 * the GuC. One single gem object contains the ADS struct itself (guc_ads), the
79 * scheduling policies (guc_policies), a structure describing a collection of
80 * register sets (guc_mmio_reg_state) and some extra pages for the GuC to save
81 * its internal state for sleep.
85 static inline bool is_high_priority(struct intel_guc_client
*client
)
87 return (client
->priority
== GUC_CLIENT_PRIORITY_KMD_HIGH
||
88 client
->priority
== GUC_CLIENT_PRIORITY_HIGH
);
91 static int reserve_doorbell(struct intel_guc_client
*client
)
97 GEM_BUG_ON(client
->doorbell_id
!= GUC_DOORBELL_INVALID
);
100 * The bitmap tracks which doorbell registers are currently in use.
101 * It is split into two halves; the first half is used for normal
102 * priority contexts, the second half for high-priority ones.
105 end
= GUC_NUM_DOORBELLS
/ 2;
106 if (is_high_priority(client
)) {
111 id
= find_next_zero_bit(client
->guc
->doorbell_bitmap
, end
, offset
);
115 __set_bit(id
, client
->guc
->doorbell_bitmap
);
116 client
->doorbell_id
= id
;
117 DRM_DEBUG_DRIVER("client %u (high prio=%s) reserved doorbell: %d\n",
118 client
->stage_id
, yesno(is_high_priority(client
)),
123 static void unreserve_doorbell(struct intel_guc_client
*client
)
125 GEM_BUG_ON(client
->doorbell_id
== GUC_DOORBELL_INVALID
);
127 __clear_bit(client
->doorbell_id
, client
->guc
->doorbell_bitmap
);
128 client
->doorbell_id
= GUC_DOORBELL_INVALID
;
132 * Tell the GuC to allocate or deallocate a specific doorbell
135 static int __guc_allocate_doorbell(struct intel_guc
*guc
, u32 stage_id
)
138 INTEL_GUC_ACTION_ALLOCATE_DOORBELL
,
142 return intel_guc_send(guc
, action
, ARRAY_SIZE(action
));
145 static int __guc_deallocate_doorbell(struct intel_guc
*guc
, u32 stage_id
)
148 INTEL_GUC_ACTION_DEALLOCATE_DOORBELL
,
152 return intel_guc_send(guc
, action
, ARRAY_SIZE(action
));
155 static struct guc_stage_desc
*__get_stage_desc(struct intel_guc_client
*client
)
157 struct guc_stage_desc
*base
= client
->guc
->stage_desc_pool_vaddr
;
159 return &base
[client
->stage_id
];
163 * Initialise, update, or clear doorbell data shared with the GuC
165 * These functions modify shared data and so need access to the mapped
166 * client object which contains the page being used for the doorbell
169 static void __update_doorbell_desc(struct intel_guc_client
*client
, u16 new_id
)
171 struct guc_stage_desc
*desc
;
173 /* Update the GuC's idea of the doorbell ID */
174 desc
= __get_stage_desc(client
);
175 desc
->db_id
= new_id
;
178 static struct guc_doorbell_info
*__get_doorbell(struct intel_guc_client
*client
)
180 return client
->vaddr
+ client
->doorbell_offset
;
183 static bool has_doorbell(struct intel_guc_client
*client
)
185 if (client
->doorbell_id
== GUC_DOORBELL_INVALID
)
188 return test_bit(client
->doorbell_id
, client
->guc
->doorbell_bitmap
);
191 static void __create_doorbell(struct intel_guc_client
*client
)
193 struct guc_doorbell_info
*doorbell
;
195 doorbell
= __get_doorbell(client
);
196 doorbell
->db_status
= GUC_DOORBELL_ENABLED
;
197 doorbell
->cookie
= 0;
200 static void __destroy_doorbell(struct intel_guc_client
*client
)
202 struct drm_i915_private
*dev_priv
= guc_to_i915(client
->guc
);
203 struct guc_doorbell_info
*doorbell
;
204 u16 db_id
= client
->doorbell_id
;
207 doorbell
= __get_doorbell(client
);
208 doorbell
->db_status
= GUC_DOORBELL_DISABLED
;
209 doorbell
->cookie
= 0;
211 /* Doorbell release flow requires that we wait for GEN8_DRB_VALID bit
212 * to go to zero after updating db_status before we call the GuC to
213 * release the doorbell
215 if (wait_for_us(!(I915_READ(GEN8_DRBREGL(db_id
)) & GEN8_DRB_VALID
), 10))
216 WARN_ONCE(true, "Doorbell never became invalid after disable\n");
219 static int create_doorbell(struct intel_guc_client
*client
)
223 __update_doorbell_desc(client
, client
->doorbell_id
);
224 __create_doorbell(client
);
226 ret
= __guc_allocate_doorbell(client
->guc
, client
->stage_id
);
228 __destroy_doorbell(client
);
229 __update_doorbell_desc(client
, GUC_DOORBELL_INVALID
);
230 DRM_ERROR("Couldn't create client %u doorbell: %d\n",
231 client
->stage_id
, ret
);
238 static int destroy_doorbell(struct intel_guc_client
*client
)
242 GEM_BUG_ON(!has_doorbell(client
));
244 __destroy_doorbell(client
);
245 ret
= __guc_deallocate_doorbell(client
->guc
, client
->stage_id
);
247 DRM_ERROR("Couldn't destroy client %u doorbell: %d\n",
248 client
->stage_id
, ret
);
250 __update_doorbell_desc(client
, GUC_DOORBELL_INVALID
);
255 static unsigned long __select_cacheline(struct intel_guc
*guc
)
257 unsigned long offset
;
259 /* Doorbell uses a single cache line within a page */
260 offset
= offset_in_page(guc
->db_cacheline
);
262 /* Moving to next cache line to reduce contention */
263 guc
->db_cacheline
+= cache_line_size();
265 DRM_DEBUG_DRIVER("reserved cacheline 0x%lx, next 0x%x, linesize %u\n",
266 offset
, guc
->db_cacheline
, cache_line_size());
270 static inline struct guc_process_desc
*
271 __get_process_desc(struct intel_guc_client
*client
)
273 return client
->vaddr
+ client
->proc_desc_offset
;
277 * Initialise the process descriptor shared with the GuC firmware.
279 static void guc_proc_desc_init(struct intel_guc
*guc
,
280 struct intel_guc_client
*client
)
282 struct guc_process_desc
*desc
;
284 desc
= memset(__get_process_desc(client
), 0, sizeof(*desc
));
287 * XXX: pDoorbell and WQVBaseAddress are pointers in process address
288 * space for ring3 clients (set them as in mmap_ioctl) or kernel
289 * space for kernel clients (map on demand instead? May make debug
290 * easier to have it mapped).
292 desc
->wq_base_addr
= 0;
293 desc
->db_base_addr
= 0;
295 desc
->stage_id
= client
->stage_id
;
296 desc
->wq_size_bytes
= GUC_WQ_SIZE
;
297 desc
->wq_status
= WQ_STATUS_ACTIVE
;
298 desc
->priority
= client
->priority
;
301 static int guc_stage_desc_pool_create(struct intel_guc
*guc
)
303 struct i915_vma
*vma
;
306 vma
= intel_guc_allocate_vma(guc
,
307 PAGE_ALIGN(sizeof(struct guc_stage_desc
) *
308 GUC_MAX_STAGE_DESCRIPTORS
));
312 vaddr
= i915_gem_object_pin_map(vma
->obj
, I915_MAP_WB
);
314 i915_vma_unpin_and_release(&vma
);
315 return PTR_ERR(vaddr
);
318 guc
->stage_desc_pool
= vma
;
319 guc
->stage_desc_pool_vaddr
= vaddr
;
320 ida_init(&guc
->stage_ids
);
325 static void guc_stage_desc_pool_destroy(struct intel_guc
*guc
)
327 ida_destroy(&guc
->stage_ids
);
328 i915_gem_object_unpin_map(guc
->stage_desc_pool
->obj
);
329 i915_vma_unpin_and_release(&guc
->stage_desc_pool
);
333 * Initialise/clear the stage descriptor shared with the GuC firmware.
335 * This descriptor tells the GuC where (in GGTT space) to find the important
336 * data structures relating to this client (doorbell, process descriptor,
339 static void guc_stage_desc_init(struct intel_guc
*guc
,
340 struct intel_guc_client
*client
)
342 struct drm_i915_private
*dev_priv
= guc_to_i915(guc
);
343 struct intel_engine_cs
*engine
;
344 struct i915_gem_context
*ctx
= client
->owner
;
345 struct guc_stage_desc
*desc
;
349 desc
= __get_stage_desc(client
);
350 memset(desc
, 0, sizeof(*desc
));
352 desc
->attribute
= GUC_STAGE_DESC_ATTR_ACTIVE
|
353 GUC_STAGE_DESC_ATTR_KERNEL
;
354 if (is_high_priority(client
))
355 desc
->attribute
|= GUC_STAGE_DESC_ATTR_PREEMPT
;
356 desc
->stage_id
= client
->stage_id
;
357 desc
->priority
= client
->priority
;
358 desc
->db_id
= client
->doorbell_id
;
360 for_each_engine_masked(engine
, dev_priv
, client
->engines
, tmp
) {
361 struct intel_context
*ce
= &ctx
->engine
[engine
->id
];
362 u32 guc_engine_id
= engine
->guc_id
;
363 struct guc_execlist_context
*lrc
= &desc
->lrc
[guc_engine_id
];
365 /* TODO: We have a design issue to be solved here. Only when we
366 * receive the first batch, we know which engine is used by the
367 * user. But here GuC expects the lrc and ring to be pinned. It
368 * is not an issue for default context, which is the only one
369 * for now who owns a GuC client. But for future owner of GuC
370 * client, need to make sure lrc is pinned prior to enter here.
373 break; /* XXX: continue? */
376 * XXX: When this is a GUC_STAGE_DESC_ATTR_KERNEL client (proxy
377 * submission or, in other words, not using a direct submission
378 * model) the KMD's LRCA is not used for any work submission.
379 * Instead, the GuC uses the LRCA of the user mode context (see
380 * guc_add_request below).
382 lrc
->context_desc
= lower_32_bits(ce
->lrc_desc
);
384 /* The state page is after PPHWSP */
386 guc_ggtt_offset(ce
->state
) + LRC_STATE_PN
* PAGE_SIZE
;
388 /* XXX: In direct submission, the GuC wants the HW context id
389 * here. In proxy submission, it wants the stage id
391 lrc
->context_id
= (client
->stage_id
<< GUC_ELC_CTXID_OFFSET
) |
392 (guc_engine_id
<< GUC_ELC_ENGINE_OFFSET
);
394 lrc
->ring_begin
= guc_ggtt_offset(ce
->ring
->vma
);
395 lrc
->ring_end
= lrc
->ring_begin
+ ce
->ring
->size
- 1;
396 lrc
->ring_next_free_location
= lrc
->ring_begin
;
397 lrc
->ring_current_tail_pointer_value
= 0;
399 desc
->engines_used
|= (1 << guc_engine_id
);
402 DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
403 client
->engines
, desc
->engines_used
);
404 WARN_ON(desc
->engines_used
== 0);
407 * The doorbell, process descriptor, and workqueue are all parts
408 * of the client object, which the GuC will reference via the GGTT
410 gfx_addr
= guc_ggtt_offset(client
->vma
);
411 desc
->db_trigger_phy
= sg_dma_address(client
->vma
->pages
->sgl
) +
412 client
->doorbell_offset
;
413 desc
->db_trigger_cpu
= ptr_to_u64(__get_doorbell(client
));
414 desc
->db_trigger_uk
= gfx_addr
+ client
->doorbell_offset
;
415 desc
->process_desc
= gfx_addr
+ client
->proc_desc_offset
;
416 desc
->wq_addr
= gfx_addr
+ GUC_DB_SIZE
;
417 desc
->wq_size
= GUC_WQ_SIZE
;
419 desc
->desc_private
= ptr_to_u64(client
);
422 static void guc_stage_desc_fini(struct intel_guc
*guc
,
423 struct intel_guc_client
*client
)
425 struct guc_stage_desc
*desc
;
427 desc
= __get_stage_desc(client
);
428 memset(desc
, 0, sizeof(*desc
));
431 /* Construct a Work Item and append it to the GuC's Work Queue */
432 static void guc_wq_item_append(struct intel_guc_client
*client
,
433 u32 target_engine
, u32 context_desc
,
434 u32 ring_tail
, u32 fence_id
)
436 /* wqi_len is in DWords, and does not include the one-word header */
437 const size_t wqi_size
= sizeof(struct guc_wq_item
);
438 const u32 wqi_len
= wqi_size
/ sizeof(u32
) - 1;
439 struct guc_process_desc
*desc
= __get_process_desc(client
);
440 struct guc_wq_item
*wqi
;
443 lockdep_assert_held(&client
->wq_lock
);
445 /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
446 * should not have the case where structure wqi is across page, neither
447 * wrapped to the beginning. This simplifies the implementation below.
449 * XXX: if not the case, we need save data to a temp wqi and copy it to
450 * workqueue buffer dw by dw.
452 BUILD_BUG_ON(wqi_size
!= 16);
454 /* Free space is guaranteed. */
455 wq_off
= READ_ONCE(desc
->tail
);
456 GEM_BUG_ON(CIRC_SPACE(wq_off
, READ_ONCE(desc
->head
),
457 GUC_WQ_SIZE
) < wqi_size
);
458 GEM_BUG_ON(wq_off
& (wqi_size
- 1));
460 /* WQ starts from the page after doorbell / process_desc */
461 wqi
= client
->vaddr
+ wq_off
+ GUC_DB_SIZE
;
463 /* Now fill in the 4-word work queue item */
464 wqi
->header
= WQ_TYPE_INORDER
|
465 (wqi_len
<< WQ_LEN_SHIFT
) |
466 (target_engine
<< WQ_TARGET_SHIFT
) |
468 wqi
->context_desc
= context_desc
;
469 wqi
->submit_element_info
= ring_tail
<< WQ_RING_TAIL_SHIFT
;
470 GEM_BUG_ON(ring_tail
> WQ_RING_TAIL_MAX
);
471 wqi
->fence_id
= fence_id
;
473 /* Make the update visible to GuC */
474 WRITE_ONCE(desc
->tail
, (wq_off
+ wqi_size
) & (GUC_WQ_SIZE
- 1));
477 static void guc_reset_wq(struct intel_guc_client
*client
)
479 struct guc_process_desc
*desc
= __get_process_desc(client
);
485 static void guc_ring_doorbell(struct intel_guc_client
*client
)
487 struct guc_doorbell_info
*db
;
490 lockdep_assert_held(&client
->wq_lock
);
492 /* pointer of current doorbell cacheline */
493 db
= __get_doorbell(client
);
496 * We're not expecting the doorbell cookie to change behind our back,
497 * we also need to treat 0 as a reserved value.
499 cookie
= READ_ONCE(db
->cookie
);
500 WARN_ON_ONCE(xchg(&db
->cookie
, cookie
+ 1 ?: cookie
+ 2) != cookie
);
502 /* XXX: doorbell was lost and need to acquire it again */
503 GEM_BUG_ON(db
->db_status
!= GUC_DOORBELL_ENABLED
);
506 static void guc_add_request(struct intel_guc
*guc
,
507 struct drm_i915_gem_request
*rq
)
509 struct intel_guc_client
*client
= guc
->execbuf_client
;
510 struct intel_engine_cs
*engine
= rq
->engine
;
511 u32 ctx_desc
= lower_32_bits(intel_lr_context_descriptor(rq
->ctx
,
513 u32 ring_tail
= intel_ring_set_tail(rq
->ring
, rq
->tail
) / sizeof(u64
);
515 spin_lock(&client
->wq_lock
);
517 guc_wq_item_append(client
, engine
->guc_id
, ctx_desc
,
518 ring_tail
, rq
->global_seqno
);
519 guc_ring_doorbell(client
);
521 client
->submissions
[engine
->id
] += 1;
523 spin_unlock(&client
->wq_lock
);
527 * When we're doing submissions using regular execlists backend, writing to
528 * ELSP from CPU side is enough to make sure that writes to ringbuffer pages
529 * pinned in mappable aperture portion of GGTT are visible to command streamer.
530 * Writes done by GuC on our behalf are not guaranteeing such ordering,
531 * therefore, to ensure the flush, we're issuing a POSTING READ.
533 static void flush_ggtt_writes(struct i915_vma
*vma
)
535 struct drm_i915_private
*dev_priv
= to_i915(vma
->obj
->base
.dev
);
537 if (i915_vma_is_map_and_fenceable(vma
))
538 POSTING_READ_FW(GUC_STATUS
);
541 #define GUC_PREEMPT_FINISHED 0x1
542 #define GUC_PREEMPT_BREADCRUMB_DWORDS 0x8
543 static void inject_preempt_context(struct work_struct
*work
)
545 struct guc_preempt_work
*preempt_work
=
546 container_of(work
, typeof(*preempt_work
), work
);
547 struct intel_engine_cs
*engine
= preempt_work
->engine
;
548 struct intel_guc
*guc
= container_of(preempt_work
, typeof(*guc
),
549 preempt_work
[engine
->id
]);
550 struct intel_guc_client
*client
= guc
->preempt_client
;
551 struct guc_stage_desc
*stage_desc
= __get_stage_desc(client
);
552 struct intel_ring
*ring
= client
->owner
->engine
[engine
->id
].ring
;
553 u32 ctx_desc
= lower_32_bits(intel_lr_context_descriptor(client
->owner
,
555 u32
*cs
= ring
->vaddr
+ ring
->tail
;
558 if (engine
->id
== RCS
) {
559 cs
= gen8_emit_ggtt_write_rcs(cs
, GUC_PREEMPT_FINISHED
,
560 intel_hws_preempt_done_address(engine
));
562 cs
= gen8_emit_ggtt_write(cs
, GUC_PREEMPT_FINISHED
,
563 intel_hws_preempt_done_address(engine
));
567 *cs
++ = MI_USER_INTERRUPT
;
570 GEM_BUG_ON(!IS_ALIGNED(ring
->size
,
571 GUC_PREEMPT_BREADCRUMB_DWORDS
* sizeof(u32
)));
572 GEM_BUG_ON((void *)cs
- (ring
->vaddr
+ ring
->tail
) !=
573 GUC_PREEMPT_BREADCRUMB_DWORDS
* sizeof(u32
));
575 ring
->tail
+= GUC_PREEMPT_BREADCRUMB_DWORDS
* sizeof(u32
);
576 ring
->tail
&= (ring
->size
- 1);
578 flush_ggtt_writes(ring
->vma
);
580 spin_lock_irq(&client
->wq_lock
);
581 guc_wq_item_append(client
, engine
->guc_id
, ctx_desc
,
582 ring
->tail
/ sizeof(u64
), 0);
583 spin_unlock_irq(&client
->wq_lock
);
586 * If GuC firmware performs an engine reset while that engine had
587 * a preemption pending, it will set the terminated attribute bit
588 * on our preemption stage descriptor. GuC firmware retains all
589 * pending work items for a high-priority GuC client, unlike the
590 * normal-priority GuC client where work items are dropped. It
591 * wants to make sure the preempt-to-idle work doesn't run when
592 * scheduling resumes, and uses this bit to inform its scheduler
593 * and presumably us as well. Our job is to clear it for the next
594 * preemption after reset, otherwise that and future preemptions
595 * will never complete. We'll just clear it every time.
597 stage_desc
->attribute
&= ~GUC_STAGE_DESC_ATTR_TERMINATED
;
599 data
[0] = INTEL_GUC_ACTION_REQUEST_PREEMPTION
;
600 data
[1] = client
->stage_id
;
601 data
[2] = INTEL_GUC_PREEMPT_OPTION_DROP_WORK_Q
|
602 INTEL_GUC_PREEMPT_OPTION_DROP_SUBMIT_Q
;
603 data
[3] = engine
->guc_id
;
604 data
[4] = guc
->execbuf_client
->priority
;
605 data
[5] = guc
->execbuf_client
->stage_id
;
606 data
[6] = guc_ggtt_offset(guc
->shared_data
);
608 if (WARN_ON(intel_guc_send(guc
, data
, ARRAY_SIZE(data
)))) {
609 execlists_clear_active(&engine
->execlists
,
610 EXECLISTS_ACTIVE_PREEMPT
);
611 tasklet_schedule(&engine
->execlists
.tasklet
);
616 * We're using user interrupt and HWSP value to mark that preemption has
617 * finished and GPU is idle. Normally, we could unwind and continue similar to
618 * execlists submission path. Unfortunately, with GuC we also need to wait for
619 * it to finish its own postprocessing, before attempting to submit. Otherwise
620 * GuC may silently ignore our submissions, and thus we risk losing request at
621 * best, executing out-of-order and causing kernel panic at worst.
623 #define GUC_PREEMPT_POSTPROCESS_DELAY_MS 10
624 static void wait_for_guc_preempt_report(struct intel_engine_cs
*engine
)
626 struct intel_guc
*guc
= &engine
->i915
->guc
;
627 struct guc_shared_ctx_data
*data
= guc
->shared_data_vaddr
;
628 struct guc_ctx_report
*report
=
629 &data
->preempt_ctx_report
[engine
->guc_id
];
631 WARN_ON(wait_for_atomic(report
->report_return_status
==
632 INTEL_GUC_REPORT_STATUS_COMPLETE
,
633 GUC_PREEMPT_POSTPROCESS_DELAY_MS
));
635 * GuC is expecting that we're also going to clear the affected context
636 * counter, let's also reset the return status to not depend on GuC
637 * resetting it after recieving another preempt action
639 report
->affected_count
= 0;
640 report
->report_return_status
= INTEL_GUC_REPORT_STATUS_UNKNOWN
;
644 * guc_submit() - Submit commands through GuC
645 * @engine: engine associated with the commands
647 * The only error here arises if the doorbell hardware isn't functioning
648 * as expected, which really shouln't happen.
650 static void guc_submit(struct intel_engine_cs
*engine
)
652 struct intel_guc
*guc
= &engine
->i915
->guc
;
653 struct intel_engine_execlists
* const execlists
= &engine
->execlists
;
654 struct execlist_port
*port
= execlists
->port
;
657 for (n
= 0; n
< execlists_num_ports(execlists
); n
++) {
658 struct drm_i915_gem_request
*rq
;
661 rq
= port_unpack(&port
[n
], &count
);
662 if (rq
&& count
== 0) {
663 port_set(&port
[n
], port_pack(rq
, ++count
));
665 flush_ggtt_writes(rq
->ring
->vma
);
667 guc_add_request(guc
, rq
);
672 static void port_assign(struct execlist_port
*port
,
673 struct drm_i915_gem_request
*rq
)
675 GEM_BUG_ON(port_isset(port
));
677 port_set(port
, i915_gem_request_get(rq
));
680 static void guc_dequeue(struct intel_engine_cs
*engine
)
682 struct intel_engine_execlists
* const execlists
= &engine
->execlists
;
683 struct execlist_port
*port
= execlists
->port
;
684 struct drm_i915_gem_request
*last
= NULL
;
685 const struct execlist_port
* const last_port
=
686 &execlists
->port
[execlists
->port_mask
];
690 spin_lock_irq(&engine
->timeline
->lock
);
691 rb
= execlists
->first
;
692 GEM_BUG_ON(rb_first(&execlists
->queue
) != rb
);
697 if (port_isset(port
)) {
698 if (HAS_LOGICAL_RING_PREEMPTION(engine
->i915
)) {
699 struct guc_preempt_work
*preempt_work
=
700 &engine
->i915
->guc
.preempt_work
[engine
->id
];
702 if (rb_entry(rb
, struct i915_priolist
, node
)->priority
>
703 max(port_request(port
)->priotree
.priority
, 0)) {
704 execlists_set_active(execlists
,
705 EXECLISTS_ACTIVE_PREEMPT
);
706 queue_work(engine
->i915
->guc
.preempt_wq
,
707 &preempt_work
->work
);
713 if (port_isset(port
))
716 GEM_BUG_ON(port_isset(port
));
719 struct i915_priolist
*p
= rb_entry(rb
, typeof(*p
), node
);
720 struct drm_i915_gem_request
*rq
, *rn
;
722 list_for_each_entry_safe(rq
, rn
, &p
->requests
, priotree
.link
) {
723 if (last
&& rq
->ctx
!= last
->ctx
) {
724 if (port
== last_port
) {
725 __list_del_many(&p
->requests
,
731 port_assign(port
, last
);
735 INIT_LIST_HEAD(&rq
->priotree
.link
);
737 __i915_gem_request_submit(rq
);
738 trace_i915_gem_request_in(rq
,
739 port_index(port
, execlists
));
745 rb_erase(&p
->node
, &execlists
->queue
);
746 INIT_LIST_HEAD(&p
->requests
);
747 if (p
->priority
!= I915_PRIORITY_NORMAL
)
748 kmem_cache_free(engine
->i915
->priorities
, p
);
751 execlists
->first
= rb
;
753 port_assign(port
, last
);
754 execlists_set_active(execlists
, EXECLISTS_ACTIVE_USER
);
758 spin_unlock_irq(&engine
->timeline
->lock
);
761 static void guc_submission_tasklet(unsigned long data
)
763 struct intel_engine_cs
* const engine
= (struct intel_engine_cs
*)data
;
764 struct intel_engine_execlists
* const execlists
= &engine
->execlists
;
765 struct execlist_port
*port
= execlists
->port
;
766 struct drm_i915_gem_request
*rq
;
768 rq
= port_request(&port
[0]);
769 while (rq
&& i915_gem_request_completed(rq
)) {
770 trace_i915_gem_request_out(rq
);
771 i915_gem_request_put(rq
);
773 execlists_port_complete(execlists
, port
);
775 rq
= port_request(&port
[0]);
778 execlists_clear_active(execlists
, EXECLISTS_ACTIVE_USER
);
780 if (execlists_is_active(execlists
, EXECLISTS_ACTIVE_PREEMPT
) &&
781 intel_read_status_page(engine
, I915_GEM_HWS_PREEMPT_INDEX
) ==
782 GUC_PREEMPT_FINISHED
) {
783 execlists_cancel_port_requests(&engine
->execlists
);
784 execlists_unwind_incomplete_requests(execlists
);
786 wait_for_guc_preempt_report(engine
);
788 execlists_clear_active(execlists
, EXECLISTS_ACTIVE_PREEMPT
);
789 intel_write_status_page(engine
, I915_GEM_HWS_PREEMPT_INDEX
, 0);
792 if (!execlists_is_active(execlists
, EXECLISTS_ACTIVE_PREEMPT
))
797 * Everything below here is concerned with setup & teardown, and is
798 * therefore not part of the somewhat time-critical batch-submission
799 * path of guc_submit() above.
802 /* Check that a doorbell register is in the expected state */
803 static bool doorbell_ok(struct intel_guc
*guc
, u16 db_id
)
805 struct drm_i915_private
*dev_priv
= guc_to_i915(guc
);
809 GEM_BUG_ON(db_id
>= GUC_DOORBELL_INVALID
);
811 drbregl
= I915_READ(GEN8_DRBREGL(db_id
));
812 valid
= drbregl
& GEN8_DRB_VALID
;
814 if (test_bit(db_id
, guc
->doorbell_bitmap
) == valid
)
817 DRM_DEBUG_DRIVER("Doorbell %d has unexpected state (0x%x): valid=%s\n",
818 db_id
, drbregl
, yesno(valid
));
823 static bool guc_verify_doorbells(struct intel_guc
*guc
)
827 for (db_id
= 0; db_id
< GUC_NUM_DOORBELLS
; ++db_id
)
828 if (!doorbell_ok(guc
, db_id
))
834 static int guc_clients_doorbell_init(struct intel_guc
*guc
)
838 ret
= create_doorbell(guc
->execbuf_client
);
842 ret
= create_doorbell(guc
->preempt_client
);
844 destroy_doorbell(guc
->execbuf_client
);
851 static void guc_clients_doorbell_fini(struct intel_guc
*guc
)
854 * By the time we're here, GuC has already been reset.
855 * Instead of trying (in vain) to communicate with it, let's just
856 * cleanup the doorbell HW and our internal state.
858 __destroy_doorbell(guc
->preempt_client
);
859 __update_doorbell_desc(guc
->preempt_client
, GUC_DOORBELL_INVALID
);
860 __destroy_doorbell(guc
->execbuf_client
);
861 __update_doorbell_desc(guc
->execbuf_client
, GUC_DOORBELL_INVALID
);
865 * guc_client_alloc() - Allocate an intel_guc_client
866 * @dev_priv: driver private data structure
867 * @engines: The set of engines to enable for this client
868 * @priority: four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
869 * The kernel client to replace ExecList submission is created with
870 * NORMAL priority. Priority of a client for scheduler can be HIGH,
871 * while a preemption context can use CRITICAL.
872 * @ctx: the context that owns the client (we use the default render
875 * Return: An intel_guc_client object if success, else NULL.
877 static struct intel_guc_client
*
878 guc_client_alloc(struct drm_i915_private
*dev_priv
,
881 struct i915_gem_context
*ctx
)
883 struct intel_guc_client
*client
;
884 struct intel_guc
*guc
= &dev_priv
->guc
;
885 struct i915_vma
*vma
;
889 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
891 return ERR_PTR(-ENOMEM
);
895 client
->engines
= engines
;
896 client
->priority
= priority
;
897 client
->doorbell_id
= GUC_DOORBELL_INVALID
;
898 spin_lock_init(&client
->wq_lock
);
900 ret
= ida_simple_get(&guc
->stage_ids
, 0, GUC_MAX_STAGE_DESCRIPTORS
,
905 client
->stage_id
= ret
;
907 /* The first page is doorbell/proc_desc. Two followed pages are wq. */
908 vma
= intel_guc_allocate_vma(guc
, GUC_DB_SIZE
+ GUC_WQ_SIZE
);
914 /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
917 vaddr
= i915_gem_object_pin_map(vma
->obj
, I915_MAP_WB
);
919 ret
= PTR_ERR(vaddr
);
922 client
->vaddr
= vaddr
;
924 client
->doorbell_offset
= __select_cacheline(guc
);
927 * Since the doorbell only requires a single cacheline, we can save
928 * space by putting the application process descriptor in the same
929 * page. Use the half of the page that doesn't include the doorbell.
931 if (client
->doorbell_offset
>= (GUC_DB_SIZE
/ 2))
932 client
->proc_desc_offset
= 0;
934 client
->proc_desc_offset
= (GUC_DB_SIZE
/ 2);
936 guc_proc_desc_init(guc
, client
);
937 guc_stage_desc_init(guc
, client
);
939 ret
= reserve_doorbell(client
);
943 DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: stage_id %u\n",
944 priority
, client
, client
->engines
, client
->stage_id
);
945 DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%lx\n",
946 client
->doorbell_id
, client
->doorbell_offset
);
951 i915_gem_object_unpin_map(client
->vma
->obj
);
953 i915_vma_unpin_and_release(&client
->vma
);
955 ida_simple_remove(&guc
->stage_ids
, client
->stage_id
);
961 static void guc_client_free(struct intel_guc_client
*client
)
963 unreserve_doorbell(client
);
964 guc_stage_desc_fini(client
->guc
, client
);
965 i915_gem_object_unpin_map(client
->vma
->obj
);
966 i915_vma_unpin_and_release(&client
->vma
);
967 ida_simple_remove(&client
->guc
->stage_ids
, client
->stage_id
);
971 static int guc_clients_create(struct intel_guc
*guc
)
973 struct drm_i915_private
*dev_priv
= guc_to_i915(guc
);
974 struct intel_guc_client
*client
;
976 GEM_BUG_ON(guc
->execbuf_client
);
977 GEM_BUG_ON(guc
->preempt_client
);
979 client
= guc_client_alloc(dev_priv
,
980 INTEL_INFO(dev_priv
)->ring_mask
,
981 GUC_CLIENT_PRIORITY_KMD_NORMAL
,
982 dev_priv
->kernel_context
);
983 if (IS_ERR(client
)) {
984 DRM_ERROR("Failed to create GuC client for submission!\n");
985 return PTR_ERR(client
);
987 guc
->execbuf_client
= client
;
989 client
= guc_client_alloc(dev_priv
,
990 INTEL_INFO(dev_priv
)->ring_mask
,
991 GUC_CLIENT_PRIORITY_KMD_HIGH
,
992 dev_priv
->preempt_context
);
993 if (IS_ERR(client
)) {
994 DRM_ERROR("Failed to create GuC client for preemption!\n");
995 guc_client_free(guc
->execbuf_client
);
996 guc
->execbuf_client
= NULL
;
997 return PTR_ERR(client
);
999 guc
->preempt_client
= client
;
1004 static void guc_clients_destroy(struct intel_guc
*guc
)
1006 struct intel_guc_client
*client
;
1008 client
= fetch_and_zero(&guc
->execbuf_client
);
1009 guc_client_free(client
);
1011 client
= fetch_and_zero(&guc
->preempt_client
);
1012 guc_client_free(client
);
1015 static void guc_policy_init(struct guc_policy
*policy
)
1017 policy
->execution_quantum
= POLICY_DEFAULT_EXECUTION_QUANTUM_US
;
1018 policy
->preemption_time
= POLICY_DEFAULT_PREEMPTION_TIME_US
;
1019 policy
->fault_time
= POLICY_DEFAULT_FAULT_TIME_US
;
1020 policy
->policy_flags
= 0;
1023 static void guc_policies_init(struct guc_policies
*policies
)
1025 struct guc_policy
*policy
;
1028 policies
->dpc_promote_time
= POLICY_DEFAULT_DPC_PROMOTE_TIME_US
;
1029 policies
->max_num_work_items
= POLICY_MAX_NUM_WI
;
1031 for (p
= 0; p
< GUC_CLIENT_PRIORITY_NUM
; p
++) {
1032 for (i
= GUC_RENDER_ENGINE
; i
< GUC_MAX_ENGINES_NUM
; i
++) {
1033 policy
= &policies
->policy
[p
][i
];
1035 guc_policy_init(policy
);
1039 policies
->is_valid
= 1;
1043 * The first 80 dwords of the register state context, containing the
1044 * execlists and ppgtt registers.
1046 #define LR_HW_CONTEXT_SIZE (80 * sizeof(u32))
1048 static int guc_ads_create(struct intel_guc
*guc
)
1050 struct drm_i915_private
*dev_priv
= guc_to_i915(guc
);
1051 struct i915_vma
*vma
;
1053 /* The ads obj includes the struct itself and buffers passed to GuC */
1056 struct guc_policies policies
;
1057 struct guc_mmio_reg_state reg_state
;
1058 u8 reg_state_buffer
[GUC_S3_SAVE_SPACE_PAGES
* PAGE_SIZE
];
1060 struct intel_engine_cs
*engine
;
1061 enum intel_engine_id id
;
1062 const u32 skipped_offset
= LRC_HEADER_PAGES
* PAGE_SIZE
;
1063 const u32 skipped_size
= LRC_PPHWSP_SZ
* PAGE_SIZE
+ LR_HW_CONTEXT_SIZE
;
1066 GEM_BUG_ON(guc
->ads_vma
);
1068 vma
= intel_guc_allocate_vma(guc
, PAGE_ALIGN(sizeof(*blob
)));
1070 return PTR_ERR(vma
);
1074 page
= i915_vma_first_page(vma
);
1077 /* GuC scheduling policies */
1078 guc_policies_init(&blob
->policies
);
1080 /* MMIO reg state */
1081 for_each_engine(engine
, dev_priv
, id
) {
1082 blob
->reg_state
.white_list
[engine
->guc_id
].mmio_start
=
1083 engine
->mmio_base
+ GUC_MMIO_WHITE_LIST_START
;
1085 /* Nothing to be saved or restored for now. */
1086 blob
->reg_state
.white_list
[engine
->guc_id
].count
= 0;
1090 * The GuC requires a "Golden Context" when it reinitialises
1091 * engines after a reset. Here we use the Render ring default
1092 * context, which must already exist and be pinned in the GGTT,
1093 * so its address won't change after we've told the GuC where
1094 * to find it. Note that we have to skip our header (1 page),
1095 * because our GuC shared data is there.
1097 blob
->ads
.golden_context_lrca
=
1098 guc_ggtt_offset(dev_priv
->kernel_context
->engine
[RCS
].state
) +
1102 * The GuC expects us to exclude the portion of the context image that
1103 * it skips from the size it is to read. It starts reading from after
1104 * the execlist context (so skipping the first page [PPHWSP] and 80
1105 * dwords). Weird guc is weird.
1107 for_each_engine(engine
, dev_priv
, id
)
1108 blob
->ads
.eng_state_size
[engine
->guc_id
] =
1109 engine
->context_size
- skipped_size
;
1111 base
= guc_ggtt_offset(vma
);
1112 blob
->ads
.scheduler_policies
= base
+ ptr_offset(blob
, policies
);
1113 blob
->ads
.reg_state_buffer
= base
+ ptr_offset(blob
, reg_state_buffer
);
1114 blob
->ads
.reg_state_addr
= base
+ ptr_offset(blob
, reg_state
);
1121 static void guc_ads_destroy(struct intel_guc
*guc
)
1123 i915_vma_unpin_and_release(&guc
->ads_vma
);
1127 * Set up the memory resources to be shared with the GuC (via the GGTT)
1128 * at firmware loading time.
1130 int intel_guc_submission_init(struct intel_guc
*guc
)
1132 struct drm_i915_private
*dev_priv
= guc_to_i915(guc
);
1133 struct intel_engine_cs
*engine
;
1134 enum intel_engine_id id
;
1137 if (guc
->stage_desc_pool
)
1140 ret
= guc_stage_desc_pool_create(guc
);
1144 * Keep static analysers happy, let them know that we allocated the
1145 * vma after testing that it didn't exist earlier.
1147 GEM_BUG_ON(!guc
->stage_desc_pool
);
1149 ret
= intel_guc_log_create(guc
);
1151 goto err_stage_desc_pool
;
1153 ret
= guc_ads_create(guc
);
1156 GEM_BUG_ON(!guc
->ads_vma
);
1158 WARN_ON(!guc_verify_doorbells(guc
));
1159 ret
= guc_clients_create(guc
);
1163 for_each_engine(engine
, dev_priv
, id
) {
1164 guc
->preempt_work
[id
].engine
= engine
;
1165 INIT_WORK(&guc
->preempt_work
[id
].work
, inject_preempt_context
);
1171 intel_guc_log_destroy(guc
);
1172 err_stage_desc_pool
:
1173 guc_stage_desc_pool_destroy(guc
);
1177 void intel_guc_submission_fini(struct intel_guc
*guc
)
1179 struct drm_i915_private
*dev_priv
= guc_to_i915(guc
);
1180 struct intel_engine_cs
*engine
;
1181 enum intel_engine_id id
;
1183 for_each_engine(engine
, dev_priv
, id
)
1184 cancel_work_sync(&guc
->preempt_work
[id
].work
);
1186 guc_clients_destroy(guc
);
1187 WARN_ON(!guc_verify_doorbells(guc
));
1189 guc_ads_destroy(guc
);
1190 intel_guc_log_destroy(guc
);
1191 guc_stage_desc_pool_destroy(guc
);
1194 static void guc_interrupts_capture(struct drm_i915_private
*dev_priv
)
1196 struct intel_rps
*rps
= &dev_priv
->gt_pm
.rps
;
1197 struct intel_engine_cs
*engine
;
1198 enum intel_engine_id id
;
1201 /* tell all command streamers to forward interrupts (but not vblank)
1204 irqs
= _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING
);
1205 for_each_engine(engine
, dev_priv
, id
)
1206 I915_WRITE(RING_MODE_GEN7(engine
), irqs
);
1208 /* route USER_INTERRUPT to Host, all others are sent to GuC. */
1209 irqs
= GT_RENDER_USER_INTERRUPT
<< GEN8_RCS_IRQ_SHIFT
|
1210 GT_RENDER_USER_INTERRUPT
<< GEN8_BCS_IRQ_SHIFT
;
1211 /* These three registers have the same bit definitions */
1212 I915_WRITE(GUC_BCS_RCS_IER
, ~irqs
);
1213 I915_WRITE(GUC_VCS2_VCS1_IER
, ~irqs
);
1214 I915_WRITE(GUC_WD_VECS_IER
, ~irqs
);
1217 * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
1218 * (unmasked) PM interrupts to the GuC. All other bits of this
1219 * register *disable* generation of a specific interrupt.
1221 * 'pm_intrmsk_mbz' indicates bits that are NOT to be set when
1222 * writing to the PM interrupt mask register, i.e. interrupts
1223 * that must not be disabled.
1225 * If the GuC is handling these interrupts, then we must not let
1226 * the PM code disable ANY interrupt that the GuC is expecting.
1227 * So for each ENABLED (0) bit in this register, we must SET the
1228 * bit in pm_intrmsk_mbz so that it's left enabled for the GuC.
1229 * GuC needs ARAT expired interrupt unmasked hence it is set in
1232 * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
1233 * result in the register bit being left SET!
1235 rps
->pm_intrmsk_mbz
|= ARAT_EXPIRED_INTRMSK
;
1236 rps
->pm_intrmsk_mbz
&= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC
;
1239 static void guc_interrupts_release(struct drm_i915_private
*dev_priv
)
1241 struct intel_rps
*rps
= &dev_priv
->gt_pm
.rps
;
1242 struct intel_engine_cs
*engine
;
1243 enum intel_engine_id id
;
1247 * tell all command streamers NOT to forward interrupts or vblank
1250 irqs
= _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK
, GFX_FORWARD_VBLANK_NEVER
);
1251 irqs
|= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING
);
1252 for_each_engine(engine
, dev_priv
, id
)
1253 I915_WRITE(RING_MODE_GEN7(engine
), irqs
);
1255 /* route all GT interrupts to the host */
1256 I915_WRITE(GUC_BCS_RCS_IER
, 0);
1257 I915_WRITE(GUC_VCS2_VCS1_IER
, 0);
1258 I915_WRITE(GUC_WD_VECS_IER
, 0);
1260 rps
->pm_intrmsk_mbz
|= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC
;
1261 rps
->pm_intrmsk_mbz
&= ~ARAT_EXPIRED_INTRMSK
;
1264 static void guc_submission_park(struct intel_engine_cs
*engine
)
1266 intel_engine_unpin_breadcrumbs_irq(engine
);
1269 static void guc_submission_unpark(struct intel_engine_cs
*engine
)
1271 intel_engine_pin_breadcrumbs_irq(engine
);
1274 int intel_guc_submission_enable(struct intel_guc
*guc
)
1276 struct drm_i915_private
*dev_priv
= guc_to_i915(guc
);
1277 struct intel_engine_cs
*engine
;
1278 enum intel_engine_id id
;
1282 * We're using GuC work items for submitting work through GuC. Since
1283 * we're coalescing multiple requests from a single context into a
1284 * single work item prior to assigning it to execlist_port, we can
1285 * never have more work items than the total number of ports (for all
1286 * engines). The GuC firmware is controlling the HEAD of work queue,
1287 * and it is guaranteed that it will remove the work item from the
1288 * queue before our request is completed.
1290 BUILD_BUG_ON(ARRAY_SIZE(engine
->execlists
.port
) *
1291 sizeof(struct guc_wq_item
) *
1292 I915_NUM_ENGINES
> GUC_WQ_SIZE
);
1294 GEM_BUG_ON(!guc
->execbuf_client
);
1296 guc_reset_wq(guc
->execbuf_client
);
1297 guc_reset_wq(guc
->preempt_client
);
1299 err
= intel_guc_sample_forcewake(guc
);
1303 err
= guc_clients_doorbell_init(guc
);
1307 /* Take over from manual control of ELSP (execlists) */
1308 guc_interrupts_capture(dev_priv
);
1310 for_each_engine(engine
, dev_priv
, id
) {
1311 struct intel_engine_execlists
* const execlists
=
1314 execlists
->tasklet
.func
= guc_submission_tasklet
;
1315 engine
->park
= guc_submission_park
;
1316 engine
->unpark
= guc_submission_unpark
;
1318 engine
->flags
&= ~I915_ENGINE_SUPPORTS_STATS
;
1324 void intel_guc_submission_disable(struct intel_guc
*guc
)
1326 struct drm_i915_private
*dev_priv
= guc_to_i915(guc
);
1328 GEM_BUG_ON(dev_priv
->gt
.awake
); /* GT should be parked first */
1330 guc_interrupts_release(dev_priv
);
1331 guc_clients_doorbell_fini(guc
);
1333 /* Revert back to manual ELSP submission */
1334 intel_engines_reset_default_submission(dev_priv
);
1337 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1338 #include "selftests/intel_guc.c"