]> Git Repo - J-linux.git/blob - drivers/gpu/drm/i915/intel_guc_submission.c
Merge tag 'topic/drmp-cleanup-2019-01-02' of git://anongit.freedesktop.org/drm/drm...
[J-linux.git] / drivers / gpu / drm / i915 / intel_guc_submission.c
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24
25 #include <linux/circ_buf.h>
26 #include <trace/events/dma_fence.h>
27
28 #include "intel_guc_submission.h"
29 #include "intel_lrc_reg.h"
30 #include "i915_drv.h"
31
32 #define GUC_PREEMPT_FINISHED            0x1
33 #define GUC_PREEMPT_BREADCRUMB_DWORDS   0x8
34 #define GUC_PREEMPT_BREADCRUMB_BYTES    \
35         (sizeof(u32) * GUC_PREEMPT_BREADCRUMB_DWORDS)
36
37 /**
38  * DOC: GuC-based command submission
39  *
40  * GuC client:
41  * A intel_guc_client refers to a submission path through GuC. Currently, there
42  * are two clients. One of them (the execbuf_client) is charged with all
43  * submissions to the GuC, the other one (preempt_client) is responsible for
44  * preempting the execbuf_client. This struct is the owner of a doorbell, a
45  * process descriptor and a workqueue (all of them inside a single gem object
46  * that contains all required pages for these elements).
47  *
48  * GuC stage descriptor:
49  * During initialization, the driver allocates a static pool of 1024 such
50  * descriptors, and shares them with the GuC.
51  * Currently, there exists a 1:1 mapping between a intel_guc_client and a
52  * guc_stage_desc (via the client's stage_id), so effectively only one
53  * gets used. This stage descriptor lets the GuC know about the doorbell,
54  * workqueue and process descriptor. Theoretically, it also lets the GuC
55  * know about our HW contexts (context ID, etc...), but we actually
56  * employ a kind of submission where the GuC uses the LRCA sent via the work
57  * item instead (the single guc_stage_desc associated to execbuf client
58  * contains information about the default kernel context only, but this is
59  * essentially unused). This is called a "proxy" submission.
60  *
61  * The Scratch registers:
62  * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
63  * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
64  * triggers an interrupt on the GuC via another register write (0xC4C8).
65  * Firmware writes a success/fail code back to the action register after
66  * processes the request. The kernel driver polls waiting for this update and
67  * then proceeds.
68  * See intel_guc_send()
69  *
70  * Doorbells:
71  * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
72  * mapped into process space.
73  *
74  * Work Items:
75  * There are several types of work items that the host may place into a
76  * workqueue, each with its own requirements and limitations. Currently only
77  * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
78  * represents in-order queue. The kernel driver packs ring tail pointer and an
79  * ELSP context descriptor dword into Work Item.
80  * See guc_add_request()
81  *
82  */
83
84 static inline struct i915_priolist *to_priolist(struct rb_node *rb)
85 {
86         return rb_entry(rb, struct i915_priolist, node);
87 }
88
89 static inline bool is_high_priority(struct intel_guc_client *client)
90 {
91         return (client->priority == GUC_CLIENT_PRIORITY_KMD_HIGH ||
92                 client->priority == GUC_CLIENT_PRIORITY_HIGH);
93 }
94
95 static int reserve_doorbell(struct intel_guc_client *client)
96 {
97         unsigned long offset;
98         unsigned long end;
99         u16 id;
100
101         GEM_BUG_ON(client->doorbell_id != GUC_DOORBELL_INVALID);
102
103         /*
104          * The bitmap tracks which doorbell registers are currently in use.
105          * It is split into two halves; the first half is used for normal
106          * priority contexts, the second half for high-priority ones.
107          */
108         offset = 0;
109         end = GUC_NUM_DOORBELLS / 2;
110         if (is_high_priority(client)) {
111                 offset = end;
112                 end += offset;
113         }
114
115         id = find_next_zero_bit(client->guc->doorbell_bitmap, end, offset);
116         if (id == end)
117                 return -ENOSPC;
118
119         __set_bit(id, client->guc->doorbell_bitmap);
120         client->doorbell_id = id;
121         DRM_DEBUG_DRIVER("client %u (high prio=%s) reserved doorbell: %d\n",
122                          client->stage_id, yesno(is_high_priority(client)),
123                          id);
124         return 0;
125 }
126
127 static bool has_doorbell(struct intel_guc_client *client)
128 {
129         if (client->doorbell_id == GUC_DOORBELL_INVALID)
130                 return false;
131
132         return test_bit(client->doorbell_id, client->guc->doorbell_bitmap);
133 }
134
135 static void unreserve_doorbell(struct intel_guc_client *client)
136 {
137         GEM_BUG_ON(!has_doorbell(client));
138
139         __clear_bit(client->doorbell_id, client->guc->doorbell_bitmap);
140         client->doorbell_id = GUC_DOORBELL_INVALID;
141 }
142
143 /*
144  * Tell the GuC to allocate or deallocate a specific doorbell
145  */
146
147 static int __guc_allocate_doorbell(struct intel_guc *guc, u32 stage_id)
148 {
149         u32 action[] = {
150                 INTEL_GUC_ACTION_ALLOCATE_DOORBELL,
151                 stage_id
152         };
153
154         return intel_guc_send(guc, action, ARRAY_SIZE(action));
155 }
156
157 static int __guc_deallocate_doorbell(struct intel_guc *guc, u32 stage_id)
158 {
159         u32 action[] = {
160                 INTEL_GUC_ACTION_DEALLOCATE_DOORBELL,
161                 stage_id
162         };
163
164         return intel_guc_send(guc, action, ARRAY_SIZE(action));
165 }
166
167 static struct guc_stage_desc *__get_stage_desc(struct intel_guc_client *client)
168 {
169         struct guc_stage_desc *base = client->guc->stage_desc_pool_vaddr;
170
171         return &base[client->stage_id];
172 }
173
174 /*
175  * Initialise, update, or clear doorbell data shared with the GuC
176  *
177  * These functions modify shared data and so need access to the mapped
178  * client object which contains the page being used for the doorbell
179  */
180
181 static void __update_doorbell_desc(struct intel_guc_client *client, u16 new_id)
182 {
183         struct guc_stage_desc *desc;
184
185         /* Update the GuC's idea of the doorbell ID */
186         desc = __get_stage_desc(client);
187         desc->db_id = new_id;
188 }
189
190 static struct guc_doorbell_info *__get_doorbell(struct intel_guc_client *client)
191 {
192         return client->vaddr + client->doorbell_offset;
193 }
194
195 static bool __doorbell_valid(struct intel_guc *guc, u16 db_id)
196 {
197         struct drm_i915_private *dev_priv = guc_to_i915(guc);
198
199         GEM_BUG_ON(db_id >= GUC_NUM_DOORBELLS);
200         return I915_READ(GEN8_DRBREGL(db_id)) & GEN8_DRB_VALID;
201 }
202
203 static void __init_doorbell(struct intel_guc_client *client)
204 {
205         struct guc_doorbell_info *doorbell;
206
207         doorbell = __get_doorbell(client);
208         doorbell->db_status = GUC_DOORBELL_ENABLED;
209         doorbell->cookie = 0;
210 }
211
212 static void __fini_doorbell(struct intel_guc_client *client)
213 {
214         struct guc_doorbell_info *doorbell;
215         u16 db_id = client->doorbell_id;
216
217         doorbell = __get_doorbell(client);
218         doorbell->db_status = GUC_DOORBELL_DISABLED;
219
220         /* Doorbell release flow requires that we wait for GEN8_DRB_VALID bit
221          * to go to zero after updating db_status before we call the GuC to
222          * release the doorbell
223          */
224         if (wait_for_us(!__doorbell_valid(client->guc, db_id), 10))
225                 WARN_ONCE(true, "Doorbell never became invalid after disable\n");
226 }
227
228 static int create_doorbell(struct intel_guc_client *client)
229 {
230         int ret;
231
232         if (WARN_ON(!has_doorbell(client)))
233                 return -ENODEV; /* internal setup error, should never happen */
234
235         __update_doorbell_desc(client, client->doorbell_id);
236         __init_doorbell(client);
237
238         ret = __guc_allocate_doorbell(client->guc, client->stage_id);
239         if (ret) {
240                 __fini_doorbell(client);
241                 __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
242                 DRM_DEBUG_DRIVER("Couldn't create client %u doorbell: %d\n",
243                                  client->stage_id, ret);
244                 return ret;
245         }
246
247         return 0;
248 }
249
250 static int destroy_doorbell(struct intel_guc_client *client)
251 {
252         int ret;
253
254         GEM_BUG_ON(!has_doorbell(client));
255
256         __fini_doorbell(client);
257         ret = __guc_deallocate_doorbell(client->guc, client->stage_id);
258         if (ret)
259                 DRM_ERROR("Couldn't destroy client %u doorbell: %d\n",
260                           client->stage_id, ret);
261
262         __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
263
264         return ret;
265 }
266
267 static unsigned long __select_cacheline(struct intel_guc *guc)
268 {
269         unsigned long offset;
270
271         /* Doorbell uses a single cache line within a page */
272         offset = offset_in_page(guc->db_cacheline);
273
274         /* Moving to next cache line to reduce contention */
275         guc->db_cacheline += cache_line_size();
276
277         DRM_DEBUG_DRIVER("reserved cacheline 0x%lx, next 0x%x, linesize %u\n",
278                          offset, guc->db_cacheline, cache_line_size());
279         return offset;
280 }
281
282 static inline struct guc_process_desc *
283 __get_process_desc(struct intel_guc_client *client)
284 {
285         return client->vaddr + client->proc_desc_offset;
286 }
287
288 /*
289  * Initialise the process descriptor shared with the GuC firmware.
290  */
291 static void guc_proc_desc_init(struct intel_guc_client *client)
292 {
293         struct guc_process_desc *desc;
294
295         desc = memset(__get_process_desc(client), 0, sizeof(*desc));
296
297         /*
298          * XXX: pDoorbell and WQVBaseAddress are pointers in process address
299          * space for ring3 clients (set them as in mmap_ioctl) or kernel
300          * space for kernel clients (map on demand instead? May make debug
301          * easier to have it mapped).
302          */
303         desc->wq_base_addr = 0;
304         desc->db_base_addr = 0;
305
306         desc->stage_id = client->stage_id;
307         desc->wq_size_bytes = GUC_WQ_SIZE;
308         desc->wq_status = WQ_STATUS_ACTIVE;
309         desc->priority = client->priority;
310 }
311
312 static void guc_proc_desc_fini(struct intel_guc_client *client)
313 {
314         struct guc_process_desc *desc;
315
316         desc = __get_process_desc(client);
317         memset(desc, 0, sizeof(*desc));
318 }
319
320 static int guc_stage_desc_pool_create(struct intel_guc *guc)
321 {
322         struct i915_vma *vma;
323         void *vaddr;
324
325         vma = intel_guc_allocate_vma(guc,
326                                      PAGE_ALIGN(sizeof(struct guc_stage_desc) *
327                                      GUC_MAX_STAGE_DESCRIPTORS));
328         if (IS_ERR(vma))
329                 return PTR_ERR(vma);
330
331         vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
332         if (IS_ERR(vaddr)) {
333                 i915_vma_unpin_and_release(&vma, 0);
334                 return PTR_ERR(vaddr);
335         }
336
337         guc->stage_desc_pool = vma;
338         guc->stage_desc_pool_vaddr = vaddr;
339         ida_init(&guc->stage_ids);
340
341         return 0;
342 }
343
344 static void guc_stage_desc_pool_destroy(struct intel_guc *guc)
345 {
346         ida_destroy(&guc->stage_ids);
347         i915_vma_unpin_and_release(&guc->stage_desc_pool, I915_VMA_RELEASE_MAP);
348 }
349
350 /*
351  * Initialise/clear the stage descriptor shared with the GuC firmware.
352  *
353  * This descriptor tells the GuC where (in GGTT space) to find the important
354  * data structures relating to this client (doorbell, process descriptor,
355  * write queue, etc).
356  */
357 static void guc_stage_desc_init(struct intel_guc_client *client)
358 {
359         struct intel_guc *guc = client->guc;
360         struct drm_i915_private *dev_priv = guc_to_i915(guc);
361         struct intel_engine_cs *engine;
362         struct i915_gem_context *ctx = client->owner;
363         struct guc_stage_desc *desc;
364         unsigned int tmp;
365         u32 gfx_addr;
366
367         desc = __get_stage_desc(client);
368         memset(desc, 0, sizeof(*desc));
369
370         desc->attribute = GUC_STAGE_DESC_ATTR_ACTIVE |
371                           GUC_STAGE_DESC_ATTR_KERNEL;
372         if (is_high_priority(client))
373                 desc->attribute |= GUC_STAGE_DESC_ATTR_PREEMPT;
374         desc->stage_id = client->stage_id;
375         desc->priority = client->priority;
376         desc->db_id = client->doorbell_id;
377
378         for_each_engine_masked(engine, dev_priv, client->engines, tmp) {
379                 struct intel_context *ce = to_intel_context(ctx, engine);
380                 u32 guc_engine_id = engine->guc_id;
381                 struct guc_execlist_context *lrc = &desc->lrc[guc_engine_id];
382
383                 /* TODO: We have a design issue to be solved here. Only when we
384                  * receive the first batch, we know which engine is used by the
385                  * user. But here GuC expects the lrc and ring to be pinned. It
386                  * is not an issue for default context, which is the only one
387                  * for now who owns a GuC client. But for future owner of GuC
388                  * client, need to make sure lrc is pinned prior to enter here.
389                  */
390                 if (!ce->state)
391                         break;  /* XXX: continue? */
392
393                 /*
394                  * XXX: When this is a GUC_STAGE_DESC_ATTR_KERNEL client (proxy
395                  * submission or, in other words, not using a direct submission
396                  * model) the KMD's LRCA is not used for any work submission.
397                  * Instead, the GuC uses the LRCA of the user mode context (see
398                  * guc_add_request below).
399                  */
400                 lrc->context_desc = lower_32_bits(ce->lrc_desc);
401
402                 /* The state page is after PPHWSP */
403                 lrc->ring_lrca = intel_guc_ggtt_offset(guc, ce->state) +
404                                  LRC_STATE_PN * PAGE_SIZE;
405
406                 /* XXX: In direct submission, the GuC wants the HW context id
407                  * here. In proxy submission, it wants the stage id
408                  */
409                 lrc->context_id = (client->stage_id << GUC_ELC_CTXID_OFFSET) |
410                                 (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
411
412                 lrc->ring_begin = intel_guc_ggtt_offset(guc, ce->ring->vma);
413                 lrc->ring_end = lrc->ring_begin + ce->ring->size - 1;
414                 lrc->ring_next_free_location = lrc->ring_begin;
415                 lrc->ring_current_tail_pointer_value = 0;
416
417                 desc->engines_used |= (1 << guc_engine_id);
418         }
419
420         DRM_DEBUG_DRIVER("Host engines 0x%x => GuC engines used 0x%x\n",
421                          client->engines, desc->engines_used);
422         WARN_ON(desc->engines_used == 0);
423
424         /*
425          * The doorbell, process descriptor, and workqueue are all parts
426          * of the client object, which the GuC will reference via the GGTT
427          */
428         gfx_addr = intel_guc_ggtt_offset(guc, client->vma);
429         desc->db_trigger_phy = sg_dma_address(client->vma->pages->sgl) +
430                                 client->doorbell_offset;
431         desc->db_trigger_cpu = ptr_to_u64(__get_doorbell(client));
432         desc->db_trigger_uk = gfx_addr + client->doorbell_offset;
433         desc->process_desc = gfx_addr + client->proc_desc_offset;
434         desc->wq_addr = gfx_addr + GUC_DB_SIZE;
435         desc->wq_size = GUC_WQ_SIZE;
436
437         desc->desc_private = ptr_to_u64(client);
438 }
439
440 static void guc_stage_desc_fini(struct intel_guc_client *client)
441 {
442         struct guc_stage_desc *desc;
443
444         desc = __get_stage_desc(client);
445         memset(desc, 0, sizeof(*desc));
446 }
447
448 /* Construct a Work Item and append it to the GuC's Work Queue */
449 static void guc_wq_item_append(struct intel_guc_client *client,
450                                u32 target_engine, u32 context_desc,
451                                u32 ring_tail, u32 fence_id)
452 {
453         /* wqi_len is in DWords, and does not include the one-word header */
454         const size_t wqi_size = sizeof(struct guc_wq_item);
455         const u32 wqi_len = wqi_size / sizeof(u32) - 1;
456         struct guc_process_desc *desc = __get_process_desc(client);
457         struct guc_wq_item *wqi;
458         u32 wq_off;
459
460         lockdep_assert_held(&client->wq_lock);
461
462         /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
463          * should not have the case where structure wqi is across page, neither
464          * wrapped to the beginning. This simplifies the implementation below.
465          *
466          * XXX: if not the case, we need save data to a temp wqi and copy it to
467          * workqueue buffer dw by dw.
468          */
469         BUILD_BUG_ON(wqi_size != 16);
470
471         /* We expect the WQ to be active if we're appending items to it */
472         GEM_BUG_ON(desc->wq_status != WQ_STATUS_ACTIVE);
473
474         /* Free space is guaranteed. */
475         wq_off = READ_ONCE(desc->tail);
476         GEM_BUG_ON(CIRC_SPACE(wq_off, READ_ONCE(desc->head),
477                               GUC_WQ_SIZE) < wqi_size);
478         GEM_BUG_ON(wq_off & (wqi_size - 1));
479
480         /* WQ starts from the page after doorbell / process_desc */
481         wqi = client->vaddr + wq_off + GUC_DB_SIZE;
482
483         if (I915_SELFTEST_ONLY(client->use_nop_wqi)) {
484                 wqi->header = WQ_TYPE_NOOP | (wqi_len << WQ_LEN_SHIFT);
485         } else {
486                 /* Now fill in the 4-word work queue item */
487                 wqi->header = WQ_TYPE_INORDER |
488                               (wqi_len << WQ_LEN_SHIFT) |
489                               (target_engine << WQ_TARGET_SHIFT) |
490                               WQ_NO_WCFLUSH_WAIT;
491                 wqi->context_desc = context_desc;
492                 wqi->submit_element_info = ring_tail << WQ_RING_TAIL_SHIFT;
493                 GEM_BUG_ON(ring_tail > WQ_RING_TAIL_MAX);
494                 wqi->fence_id = fence_id;
495         }
496
497         /* Make the update visible to GuC */
498         WRITE_ONCE(desc->tail, (wq_off + wqi_size) & (GUC_WQ_SIZE - 1));
499 }
500
501 static void guc_ring_doorbell(struct intel_guc_client *client)
502 {
503         struct guc_doorbell_info *db;
504         u32 cookie;
505
506         lockdep_assert_held(&client->wq_lock);
507
508         /* pointer of current doorbell cacheline */
509         db = __get_doorbell(client);
510
511         /*
512          * We're not expecting the doorbell cookie to change behind our back,
513          * we also need to treat 0 as a reserved value.
514          */
515         cookie = READ_ONCE(db->cookie);
516         WARN_ON_ONCE(xchg(&db->cookie, cookie + 1 ?: cookie + 2) != cookie);
517
518         /* XXX: doorbell was lost and need to acquire it again */
519         GEM_BUG_ON(db->db_status != GUC_DOORBELL_ENABLED);
520 }
521
522 static void guc_add_request(struct intel_guc *guc, struct i915_request *rq)
523 {
524         struct intel_guc_client *client = guc->execbuf_client;
525         struct intel_engine_cs *engine = rq->engine;
526         u32 ctx_desc = lower_32_bits(rq->hw_context->lrc_desc);
527         u32 ring_tail = intel_ring_set_tail(rq->ring, rq->tail) / sizeof(u64);
528
529         spin_lock(&client->wq_lock);
530
531         guc_wq_item_append(client, engine->guc_id, ctx_desc,
532                            ring_tail, rq->global_seqno);
533         guc_ring_doorbell(client);
534
535         client->submissions[engine->id] += 1;
536
537         spin_unlock(&client->wq_lock);
538 }
539
540 /*
541  * When we're doing submissions using regular execlists backend, writing to
542  * ELSP from CPU side is enough to make sure that writes to ringbuffer pages
543  * pinned in mappable aperture portion of GGTT are visible to command streamer.
544  * Writes done by GuC on our behalf are not guaranteeing such ordering,
545  * therefore, to ensure the flush, we're issuing a POSTING READ.
546  */
547 static void flush_ggtt_writes(struct i915_vma *vma)
548 {
549         struct drm_i915_private *dev_priv = vma->vm->i915;
550
551         if (i915_vma_is_map_and_fenceable(vma))
552                 POSTING_READ_FW(GUC_STATUS);
553 }
554
555 static void inject_preempt_context(struct work_struct *work)
556 {
557         struct guc_preempt_work *preempt_work =
558                 container_of(work, typeof(*preempt_work), work);
559         struct intel_engine_cs *engine = preempt_work->engine;
560         struct intel_guc *guc = container_of(preempt_work, typeof(*guc),
561                                              preempt_work[engine->id]);
562         struct intel_guc_client *client = guc->preempt_client;
563         struct guc_stage_desc *stage_desc = __get_stage_desc(client);
564         struct intel_context *ce = to_intel_context(client->owner, engine);
565         u32 data[7];
566
567         if (!ce->ring->emit) { /* recreate upon load/resume */
568                 u32 addr = intel_hws_preempt_done_address(engine);
569                 u32 *cs;
570
571                 cs = ce->ring->vaddr;
572                 if (engine->id == RCS) {
573                         cs = gen8_emit_ggtt_write_rcs(cs,
574                                                       GUC_PREEMPT_FINISHED,
575                                                       addr);
576                 } else {
577                         cs = gen8_emit_ggtt_write(cs,
578                                                   GUC_PREEMPT_FINISHED,
579                                                   addr);
580                         *cs++ = MI_NOOP;
581                         *cs++ = MI_NOOP;
582                 }
583                 *cs++ = MI_USER_INTERRUPT;
584                 *cs++ = MI_NOOP;
585
586                 ce->ring->emit = GUC_PREEMPT_BREADCRUMB_BYTES;
587                 GEM_BUG_ON((void *)cs - ce->ring->vaddr != ce->ring->emit);
588
589                 flush_ggtt_writes(ce->ring->vma);
590         }
591
592         spin_lock_irq(&client->wq_lock);
593         guc_wq_item_append(client, engine->guc_id, lower_32_bits(ce->lrc_desc),
594                            GUC_PREEMPT_BREADCRUMB_BYTES / sizeof(u64), 0);
595         spin_unlock_irq(&client->wq_lock);
596
597         /*
598          * If GuC firmware performs an engine reset while that engine had
599          * a preemption pending, it will set the terminated attribute bit
600          * on our preemption stage descriptor. GuC firmware retains all
601          * pending work items for a high-priority GuC client, unlike the
602          * normal-priority GuC client where work items are dropped. It
603          * wants to make sure the preempt-to-idle work doesn't run when
604          * scheduling resumes, and uses this bit to inform its scheduler
605          * and presumably us as well. Our job is to clear it for the next
606          * preemption after reset, otherwise that and future preemptions
607          * will never complete. We'll just clear it every time.
608          */
609         stage_desc->attribute &= ~GUC_STAGE_DESC_ATTR_TERMINATED;
610
611         data[0] = INTEL_GUC_ACTION_REQUEST_PREEMPTION;
612         data[1] = client->stage_id;
613         data[2] = INTEL_GUC_PREEMPT_OPTION_DROP_WORK_Q |
614                   INTEL_GUC_PREEMPT_OPTION_DROP_SUBMIT_Q;
615         data[3] = engine->guc_id;
616         data[4] = guc->execbuf_client->priority;
617         data[5] = guc->execbuf_client->stage_id;
618         data[6] = intel_guc_ggtt_offset(guc, guc->shared_data);
619
620         if (WARN_ON(intel_guc_send(guc, data, ARRAY_SIZE(data)))) {
621                 execlists_clear_active(&engine->execlists,
622                                        EXECLISTS_ACTIVE_PREEMPT);
623                 tasklet_schedule(&engine->execlists.tasklet);
624         }
625 }
626
627 /*
628  * We're using user interrupt and HWSP value to mark that preemption has
629  * finished and GPU is idle. Normally, we could unwind and continue similar to
630  * execlists submission path. Unfortunately, with GuC we also need to wait for
631  * it to finish its own postprocessing, before attempting to submit. Otherwise
632  * GuC may silently ignore our submissions, and thus we risk losing request at
633  * best, executing out-of-order and causing kernel panic at worst.
634  */
635 #define GUC_PREEMPT_POSTPROCESS_DELAY_MS 10
636 static void wait_for_guc_preempt_report(struct intel_engine_cs *engine)
637 {
638         struct intel_guc *guc = &engine->i915->guc;
639         struct guc_shared_ctx_data *data = guc->shared_data_vaddr;
640         struct guc_ctx_report *report =
641                 &data->preempt_ctx_report[engine->guc_id];
642
643         WARN_ON(wait_for_atomic(report->report_return_status ==
644                                 INTEL_GUC_REPORT_STATUS_COMPLETE,
645                                 GUC_PREEMPT_POSTPROCESS_DELAY_MS));
646         /*
647          * GuC is expecting that we're also going to clear the affected context
648          * counter, let's also reset the return status to not depend on GuC
649          * resetting it after recieving another preempt action
650          */
651         report->affected_count = 0;
652         report->report_return_status = INTEL_GUC_REPORT_STATUS_UNKNOWN;
653 }
654
655 static void complete_preempt_context(struct intel_engine_cs *engine)
656 {
657         struct intel_engine_execlists *execlists = &engine->execlists;
658
659         GEM_BUG_ON(!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT));
660
661         if (inject_preempt_hang(execlists))
662                 return;
663
664         execlists_cancel_port_requests(execlists);
665         execlists_unwind_incomplete_requests(execlists);
666
667         wait_for_guc_preempt_report(engine);
668         intel_write_status_page(engine, I915_GEM_HWS_PREEMPT_INDEX, 0);
669 }
670
671 /**
672  * guc_submit() - Submit commands through GuC
673  * @engine: engine associated with the commands
674  *
675  * The only error here arises if the doorbell hardware isn't functioning
676  * as expected, which really shouln't happen.
677  */
678 static void guc_submit(struct intel_engine_cs *engine)
679 {
680         struct intel_guc *guc = &engine->i915->guc;
681         struct intel_engine_execlists * const execlists = &engine->execlists;
682         struct execlist_port *port = execlists->port;
683         unsigned int n;
684
685         for (n = 0; n < execlists_num_ports(execlists); n++) {
686                 struct i915_request *rq;
687                 unsigned int count;
688
689                 rq = port_unpack(&port[n], &count);
690                 if (rq && count == 0) {
691                         port_set(&port[n], port_pack(rq, ++count));
692
693                         flush_ggtt_writes(rq->ring->vma);
694
695                         guc_add_request(guc, rq);
696                 }
697         }
698 }
699
700 static void port_assign(struct execlist_port *port, struct i915_request *rq)
701 {
702         GEM_BUG_ON(port_isset(port));
703
704         port_set(port, i915_request_get(rq));
705 }
706
707 static inline int rq_prio(const struct i915_request *rq)
708 {
709         return rq->sched.attr.priority;
710 }
711
712 static inline int port_prio(const struct execlist_port *port)
713 {
714         return rq_prio(port_request(port));
715 }
716
717 static bool __guc_dequeue(struct intel_engine_cs *engine)
718 {
719         struct intel_engine_execlists * const execlists = &engine->execlists;
720         struct execlist_port *port = execlists->port;
721         struct i915_request *last = NULL;
722         const struct execlist_port * const last_port =
723                 &execlists->port[execlists->port_mask];
724         bool submit = false;
725         struct rb_node *rb;
726
727         lockdep_assert_held(&engine->timeline.lock);
728
729         if (port_isset(port)) {
730                 if (intel_engine_has_preemption(engine)) {
731                         struct guc_preempt_work *preempt_work =
732                                 &engine->i915->guc.preempt_work[engine->id];
733                         int prio = execlists->queue_priority;
734
735                         if (__execlists_need_preempt(prio, port_prio(port))) {
736                                 execlists_set_active(execlists,
737                                                      EXECLISTS_ACTIVE_PREEMPT);
738                                 queue_work(engine->i915->guc.preempt_wq,
739                                            &preempt_work->work);
740                                 return false;
741                         }
742                 }
743
744                 port++;
745                 if (port_isset(port))
746                         return false;
747         }
748         GEM_BUG_ON(port_isset(port));
749
750         while ((rb = rb_first_cached(&execlists->queue))) {
751                 struct i915_priolist *p = to_priolist(rb);
752                 struct i915_request *rq, *rn;
753                 int i;
754
755                 priolist_for_each_request_consume(rq, rn, p, i) {
756                         if (last && rq->hw_context != last->hw_context) {
757                                 if (port == last_port)
758                                         goto done;
759
760                                 if (submit)
761                                         port_assign(port, last);
762                                 port++;
763                         }
764
765                         list_del_init(&rq->sched.link);
766
767                         __i915_request_submit(rq);
768                         trace_i915_request_in(rq, port_index(port, execlists));
769
770                         last = rq;
771                         submit = true;
772                 }
773
774                 rb_erase_cached(&p->node, &execlists->queue);
775                 if (p->priority != I915_PRIORITY_NORMAL)
776                         kmem_cache_free(engine->i915->priorities, p);
777         }
778 done:
779         execlists->queue_priority = rb ? to_priolist(rb)->priority : INT_MIN;
780         if (submit)
781                 port_assign(port, last);
782         if (last)
783                 execlists_user_begin(execlists, execlists->port);
784
785         /* We must always keep the beast fed if we have work piled up */
786         GEM_BUG_ON(port_isset(execlists->port) &&
787                    !execlists_is_active(execlists, EXECLISTS_ACTIVE_USER));
788         GEM_BUG_ON(rb_first_cached(&execlists->queue) &&
789                    !port_isset(execlists->port));
790
791         return submit;
792 }
793
794 static void guc_dequeue(struct intel_engine_cs *engine)
795 {
796         if (__guc_dequeue(engine))
797                 guc_submit(engine);
798 }
799
800 static void guc_submission_tasklet(unsigned long data)
801 {
802         struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
803         struct intel_engine_execlists * const execlists = &engine->execlists;
804         struct execlist_port *port = execlists->port;
805         struct i915_request *rq;
806         unsigned long flags;
807
808         spin_lock_irqsave(&engine->timeline.lock, flags);
809
810         rq = port_request(port);
811         while (rq && i915_request_completed(rq)) {
812                 trace_i915_request_out(rq);
813                 i915_request_put(rq);
814
815                 port = execlists_port_complete(execlists, port);
816                 if (port_isset(port)) {
817                         execlists_user_begin(execlists, port);
818                         rq = port_request(port);
819                 } else {
820                         execlists_user_end(execlists);
821                         rq = NULL;
822                 }
823         }
824
825         if (execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT) &&
826             intel_read_status_page(engine, I915_GEM_HWS_PREEMPT_INDEX) ==
827             GUC_PREEMPT_FINISHED)
828                 complete_preempt_context(engine);
829
830         if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
831                 guc_dequeue(engine);
832
833         spin_unlock_irqrestore(&engine->timeline.lock, flags);
834 }
835
836 static struct i915_request *
837 guc_reset_prepare(struct intel_engine_cs *engine)
838 {
839         struct intel_engine_execlists * const execlists = &engine->execlists;
840
841         GEM_TRACE("%s\n", engine->name);
842
843         /*
844          * Prevent request submission to the hardware until we have
845          * completed the reset in i915_gem_reset_finish(). If a request
846          * is completed by one engine, it may then queue a request
847          * to a second via its execlists->tasklet *just* as we are
848          * calling engine->init_hw() and also writing the ELSP.
849          * Turning off the execlists->tasklet until the reset is over
850          * prevents the race.
851          */
852         __tasklet_disable_sync_once(&execlists->tasklet);
853
854         /*
855          * We're using worker to queue preemption requests from the tasklet in
856          * GuC submission mode.
857          * Even though tasklet was disabled, we may still have a worker queued.
858          * Let's make sure that all workers scheduled before disabling the
859          * tasklet are completed before continuing with the reset.
860          */
861         if (engine->i915->guc.preempt_wq)
862                 flush_workqueue(engine->i915->guc.preempt_wq);
863
864         return i915_gem_find_active_request(engine);
865 }
866
867 /*
868  * Everything below here is concerned with setup & teardown, and is
869  * therefore not part of the somewhat time-critical batch-submission
870  * path of guc_submit() above.
871  */
872
873 /* Check that a doorbell register is in the expected state */
874 static bool doorbell_ok(struct intel_guc *guc, u16 db_id)
875 {
876         bool valid;
877
878         GEM_BUG_ON(db_id >= GUC_NUM_DOORBELLS);
879
880         valid = __doorbell_valid(guc, db_id);
881
882         if (test_bit(db_id, guc->doorbell_bitmap) == valid)
883                 return true;
884
885         DRM_DEBUG_DRIVER("Doorbell %u has unexpected state: valid=%s\n",
886                          db_id, yesno(valid));
887
888         return false;
889 }
890
891 static bool guc_verify_doorbells(struct intel_guc *guc)
892 {
893         bool doorbells_ok = true;
894         u16 db_id;
895
896         for (db_id = 0; db_id < GUC_NUM_DOORBELLS; ++db_id)
897                 if (!doorbell_ok(guc, db_id))
898                         doorbells_ok = false;
899
900         return doorbells_ok;
901 }
902
903 /**
904  * guc_client_alloc() - Allocate an intel_guc_client
905  * @dev_priv:   driver private data structure
906  * @engines:    The set of engines to enable for this client
907  * @priority:   four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
908  *              The kernel client to replace ExecList submission is created with
909  *              NORMAL priority. Priority of a client for scheduler can be HIGH,
910  *              while a preemption context can use CRITICAL.
911  * @ctx:        the context that owns the client (we use the default render
912  *              context)
913  *
914  * Return:      An intel_guc_client object if success, else NULL.
915  */
916 static struct intel_guc_client *
917 guc_client_alloc(struct drm_i915_private *dev_priv,
918                  u32 engines,
919                  u32 priority,
920                  struct i915_gem_context *ctx)
921 {
922         struct intel_guc_client *client;
923         struct intel_guc *guc = &dev_priv->guc;
924         struct i915_vma *vma;
925         void *vaddr;
926         int ret;
927
928         client = kzalloc(sizeof(*client), GFP_KERNEL);
929         if (!client)
930                 return ERR_PTR(-ENOMEM);
931
932         client->guc = guc;
933         client->owner = ctx;
934         client->engines = engines;
935         client->priority = priority;
936         client->doorbell_id = GUC_DOORBELL_INVALID;
937         spin_lock_init(&client->wq_lock);
938
939         ret = ida_simple_get(&guc->stage_ids, 0, GUC_MAX_STAGE_DESCRIPTORS,
940                              GFP_KERNEL);
941         if (ret < 0)
942                 goto err_client;
943
944         client->stage_id = ret;
945
946         /* The first page is doorbell/proc_desc. Two followed pages are wq. */
947         vma = intel_guc_allocate_vma(guc, GUC_DB_SIZE + GUC_WQ_SIZE);
948         if (IS_ERR(vma)) {
949                 ret = PTR_ERR(vma);
950                 goto err_id;
951         }
952
953         /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
954         client->vma = vma;
955
956         vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
957         if (IS_ERR(vaddr)) {
958                 ret = PTR_ERR(vaddr);
959                 goto err_vma;
960         }
961         client->vaddr = vaddr;
962
963         ret = reserve_doorbell(client);
964         if (ret)
965                 goto err_vaddr;
966
967         client->doorbell_offset = __select_cacheline(guc);
968
969         /*
970          * Since the doorbell only requires a single cacheline, we can save
971          * space by putting the application process descriptor in the same
972          * page. Use the half of the page that doesn't include the doorbell.
973          */
974         if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
975                 client->proc_desc_offset = 0;
976         else
977                 client->proc_desc_offset = (GUC_DB_SIZE / 2);
978
979         DRM_DEBUG_DRIVER("new priority %u client %p for engine(s) 0x%x: stage_id %u\n",
980                          priority, client, client->engines, client->stage_id);
981         DRM_DEBUG_DRIVER("doorbell id %u, cacheline offset 0x%lx\n",
982                          client->doorbell_id, client->doorbell_offset);
983
984         return client;
985
986 err_vaddr:
987         i915_gem_object_unpin_map(client->vma->obj);
988 err_vma:
989         i915_vma_unpin_and_release(&client->vma, 0);
990 err_id:
991         ida_simple_remove(&guc->stage_ids, client->stage_id);
992 err_client:
993         kfree(client);
994         return ERR_PTR(ret);
995 }
996
997 static void guc_client_free(struct intel_guc_client *client)
998 {
999         unreserve_doorbell(client);
1000         i915_vma_unpin_and_release(&client->vma, I915_VMA_RELEASE_MAP);
1001         ida_simple_remove(&client->guc->stage_ids, client->stage_id);
1002         kfree(client);
1003 }
1004
1005 static inline bool ctx_save_restore_disabled(struct intel_context *ce)
1006 {
1007         u32 sr = ce->lrc_reg_state[CTX_CONTEXT_CONTROL + 1];
1008
1009 #define SR_DISABLED \
1010         _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT | \
1011                            CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT)
1012
1013         return (sr & SR_DISABLED) == SR_DISABLED;
1014
1015 #undef SR_DISABLED
1016 }
1017
1018 static int guc_clients_create(struct intel_guc *guc)
1019 {
1020         struct drm_i915_private *dev_priv = guc_to_i915(guc);
1021         struct intel_guc_client *client;
1022
1023         GEM_BUG_ON(guc->execbuf_client);
1024         GEM_BUG_ON(guc->preempt_client);
1025
1026         client = guc_client_alloc(dev_priv,
1027                                   INTEL_INFO(dev_priv)->ring_mask,
1028                                   GUC_CLIENT_PRIORITY_KMD_NORMAL,
1029                                   dev_priv->kernel_context);
1030         if (IS_ERR(client)) {
1031                 DRM_ERROR("Failed to create GuC client for submission!\n");
1032                 return PTR_ERR(client);
1033         }
1034         guc->execbuf_client = client;
1035
1036         if (dev_priv->preempt_context) {
1037                 client = guc_client_alloc(dev_priv,
1038                                           INTEL_INFO(dev_priv)->ring_mask,
1039                                           GUC_CLIENT_PRIORITY_KMD_HIGH,
1040                                           dev_priv->preempt_context);
1041                 if (IS_ERR(client)) {
1042                         DRM_ERROR("Failed to create GuC client for preemption!\n");
1043                         guc_client_free(guc->execbuf_client);
1044                         guc->execbuf_client = NULL;
1045                         return PTR_ERR(client);
1046                 }
1047                 guc->preempt_client = client;
1048         }
1049
1050         return 0;
1051 }
1052
1053 static void guc_clients_destroy(struct intel_guc *guc)
1054 {
1055         struct intel_guc_client *client;
1056
1057         client = fetch_and_zero(&guc->preempt_client);
1058         if (client)
1059                 guc_client_free(client);
1060
1061         client = fetch_and_zero(&guc->execbuf_client);
1062         if (client)
1063                 guc_client_free(client);
1064 }
1065
1066 static int __guc_client_enable(struct intel_guc_client *client)
1067 {
1068         int ret;
1069
1070         guc_proc_desc_init(client);
1071         guc_stage_desc_init(client);
1072
1073         ret = create_doorbell(client);
1074         if (ret)
1075                 goto fail;
1076
1077         return 0;
1078
1079 fail:
1080         guc_stage_desc_fini(client);
1081         guc_proc_desc_fini(client);
1082         return ret;
1083 }
1084
1085 static void __guc_client_disable(struct intel_guc_client *client)
1086 {
1087         /*
1088          * By the time we're here, GuC may have already been reset. if that is
1089          * the case, instead of trying (in vain) to communicate with it, let's
1090          * just cleanup the doorbell HW and our internal state.
1091          */
1092         if (intel_guc_is_alive(client->guc))
1093                 destroy_doorbell(client);
1094         else
1095                 __fini_doorbell(client);
1096
1097         guc_stage_desc_fini(client);
1098         guc_proc_desc_fini(client);
1099 }
1100
1101 static int guc_clients_enable(struct intel_guc *guc)
1102 {
1103         int ret;
1104
1105         ret = __guc_client_enable(guc->execbuf_client);
1106         if (ret)
1107                 return ret;
1108
1109         if (guc->preempt_client) {
1110                 ret = __guc_client_enable(guc->preempt_client);
1111                 if (ret) {
1112                         __guc_client_disable(guc->execbuf_client);
1113                         return ret;
1114                 }
1115         }
1116
1117         return 0;
1118 }
1119
1120 static void guc_clients_disable(struct intel_guc *guc)
1121 {
1122         if (guc->preempt_client)
1123                 __guc_client_disable(guc->preempt_client);
1124
1125         if (guc->execbuf_client)
1126                 __guc_client_disable(guc->execbuf_client);
1127 }
1128
1129 /*
1130  * Set up the memory resources to be shared with the GuC (via the GGTT)
1131  * at firmware loading time.
1132  */
1133 int intel_guc_submission_init(struct intel_guc *guc)
1134 {
1135         struct drm_i915_private *dev_priv = guc_to_i915(guc);
1136         struct intel_engine_cs *engine;
1137         enum intel_engine_id id;
1138         int ret;
1139
1140         if (guc->stage_desc_pool)
1141                 return 0;
1142
1143         ret = guc_stage_desc_pool_create(guc);
1144         if (ret)
1145                 return ret;
1146         /*
1147          * Keep static analysers happy, let them know that we allocated the
1148          * vma after testing that it didn't exist earlier.
1149          */
1150         GEM_BUG_ON(!guc->stage_desc_pool);
1151
1152         WARN_ON(!guc_verify_doorbells(guc));
1153         ret = guc_clients_create(guc);
1154         if (ret)
1155                 goto err_pool;
1156
1157         for_each_engine(engine, dev_priv, id) {
1158                 guc->preempt_work[id].engine = engine;
1159                 INIT_WORK(&guc->preempt_work[id].work, inject_preempt_context);
1160         }
1161
1162         return 0;
1163
1164 err_pool:
1165         guc_stage_desc_pool_destroy(guc);
1166         return ret;
1167 }
1168
1169 void intel_guc_submission_fini(struct intel_guc *guc)
1170 {
1171         struct drm_i915_private *dev_priv = guc_to_i915(guc);
1172         struct intel_engine_cs *engine;
1173         enum intel_engine_id id;
1174
1175         for_each_engine(engine, dev_priv, id)
1176                 cancel_work_sync(&guc->preempt_work[id].work);
1177
1178         guc_clients_destroy(guc);
1179         WARN_ON(!guc_verify_doorbells(guc));
1180
1181         if (guc->stage_desc_pool)
1182                 guc_stage_desc_pool_destroy(guc);
1183 }
1184
1185 static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
1186 {
1187         struct intel_rps *rps = &dev_priv->gt_pm.rps;
1188         struct intel_engine_cs *engine;
1189         enum intel_engine_id id;
1190         int irqs;
1191
1192         /* tell all command streamers to forward interrupts (but not vblank)
1193          * to GuC
1194          */
1195         irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
1196         for_each_engine(engine, dev_priv, id)
1197                 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1198
1199         /* route USER_INTERRUPT to Host, all others are sent to GuC. */
1200         irqs = GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
1201                GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
1202         /* These three registers have the same bit definitions */
1203         I915_WRITE(GUC_BCS_RCS_IER, ~irqs);
1204         I915_WRITE(GUC_VCS2_VCS1_IER, ~irqs);
1205         I915_WRITE(GUC_WD_VECS_IER, ~irqs);
1206
1207         /*
1208          * The REDIRECT_TO_GUC bit of the PMINTRMSK register directs all
1209          * (unmasked) PM interrupts to the GuC. All other bits of this
1210          * register *disable* generation of a specific interrupt.
1211          *
1212          * 'pm_intrmsk_mbz' indicates bits that are NOT to be set when
1213          * writing to the PM interrupt mask register, i.e. interrupts
1214          * that must not be disabled.
1215          *
1216          * If the GuC is handling these interrupts, then we must not let
1217          * the PM code disable ANY interrupt that the GuC is expecting.
1218          * So for each ENABLED (0) bit in this register, we must SET the
1219          * bit in pm_intrmsk_mbz so that it's left enabled for the GuC.
1220          * GuC needs ARAT expired interrupt unmasked hence it is set in
1221          * pm_intrmsk_mbz.
1222          *
1223          * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
1224          * result in the register bit being left SET!
1225          */
1226         rps->pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
1227         rps->pm_intrmsk_mbz &= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
1228 }
1229
1230 static void guc_interrupts_release(struct drm_i915_private *dev_priv)
1231 {
1232         struct intel_rps *rps = &dev_priv->gt_pm.rps;
1233         struct intel_engine_cs *engine;
1234         enum intel_engine_id id;
1235         int irqs;
1236
1237         /*
1238          * tell all command streamers NOT to forward interrupts or vblank
1239          * to GuC.
1240          */
1241         irqs = _MASKED_FIELD(GFX_FORWARD_VBLANK_MASK, GFX_FORWARD_VBLANK_NEVER);
1242         irqs |= _MASKED_BIT_DISABLE(GFX_INTERRUPT_STEERING);
1243         for_each_engine(engine, dev_priv, id)
1244                 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1245
1246         /* route all GT interrupts to the host */
1247         I915_WRITE(GUC_BCS_RCS_IER, 0);
1248         I915_WRITE(GUC_VCS2_VCS1_IER, 0);
1249         I915_WRITE(GUC_WD_VECS_IER, 0);
1250
1251         rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
1252         rps->pm_intrmsk_mbz &= ~ARAT_EXPIRED_INTRMSK;
1253 }
1254
1255 static void guc_submission_park(struct intel_engine_cs *engine)
1256 {
1257         intel_engine_unpin_breadcrumbs_irq(engine);
1258 }
1259
1260 static void guc_submission_unpark(struct intel_engine_cs *engine)
1261 {
1262         intel_engine_pin_breadcrumbs_irq(engine);
1263 }
1264
1265 static void guc_set_default_submission(struct intel_engine_cs *engine)
1266 {
1267         /*
1268          * We inherit a bunch of functions from execlists that we'd like
1269          * to keep using:
1270          *
1271          *    engine->submit_request = execlists_submit_request;
1272          *    engine->cancel_requests = execlists_cancel_requests;
1273          *    engine->schedule = execlists_schedule;
1274          *
1275          * But we need to override the actual submission backend in order
1276          * to talk to the GuC.
1277          */
1278         intel_execlists_set_default_submission(engine);
1279
1280         engine->execlists.tasklet.func = guc_submission_tasklet;
1281
1282         engine->park = guc_submission_park;
1283         engine->unpark = guc_submission_unpark;
1284
1285         engine->reset.prepare = guc_reset_prepare;
1286
1287         engine->flags &= ~I915_ENGINE_SUPPORTS_STATS;
1288 }
1289
1290 int intel_guc_submission_enable(struct intel_guc *guc)
1291 {
1292         struct drm_i915_private *dev_priv = guc_to_i915(guc);
1293         struct intel_engine_cs *engine;
1294         enum intel_engine_id id;
1295         int err;
1296
1297         /*
1298          * We're using GuC work items for submitting work through GuC. Since
1299          * we're coalescing multiple requests from a single context into a
1300          * single work item prior to assigning it to execlist_port, we can
1301          * never have more work items than the total number of ports (for all
1302          * engines). The GuC firmware is controlling the HEAD of work queue,
1303          * and it is guaranteed that it will remove the work item from the
1304          * queue before our request is completed.
1305          */
1306         BUILD_BUG_ON(ARRAY_SIZE(engine->execlists.port) *
1307                      sizeof(struct guc_wq_item) *
1308                      I915_NUM_ENGINES > GUC_WQ_SIZE);
1309
1310         GEM_BUG_ON(!guc->execbuf_client);
1311
1312         err = intel_guc_sample_forcewake(guc);
1313         if (err)
1314                 return err;
1315
1316         err = guc_clients_enable(guc);
1317         if (err)
1318                 return err;
1319
1320         /* Take over from manual control of ELSP (execlists) */
1321         guc_interrupts_capture(dev_priv);
1322
1323         for_each_engine(engine, dev_priv, id) {
1324                 engine->set_default_submission = guc_set_default_submission;
1325                 engine->set_default_submission(engine);
1326         }
1327
1328         return 0;
1329 }
1330
1331 void intel_guc_submission_disable(struct intel_guc *guc)
1332 {
1333         struct drm_i915_private *dev_priv = guc_to_i915(guc);
1334
1335         GEM_BUG_ON(dev_priv->gt.awake); /* GT should be parked first */
1336
1337         guc_interrupts_release(dev_priv);
1338         guc_clients_disable(guc);
1339 }
1340
1341 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1342 #include "selftests/intel_guc.c"
1343 #endif
This page took 0.115211 seconds and 4 git commands to generate.