Merge remote-tracking branches 'asoc/topic/wm8753', 'asoc/topic/wm8770', 'asoc/topic...
[linux-block.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 "i915_drv.h"
30
31 /**
32  * DOC: GuC-based command submission
33  *
34  * GuC client:
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).
41  *
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.
54  *
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
61  * then proceeds.
62  * See intel_guc_send()
63  *
64  * Doorbells:
65  * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
66  * mapped into process space.
67  *
68  * Work Items:
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()
75  *
76  * ADS:
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.
82  *
83  */
84
85 static inline bool is_high_priority(struct intel_guc_client *client)
86 {
87         return (client->priority == GUC_CLIENT_PRIORITY_KMD_HIGH ||
88                 client->priority == GUC_CLIENT_PRIORITY_HIGH);
89 }
90
91 static int reserve_doorbell(struct intel_guc_client *client)
92 {
93         unsigned long offset;
94         unsigned long end;
95         u16 id;
96
97         GEM_BUG_ON(client->doorbell_id != GUC_DOORBELL_INVALID);
98
99         /*
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.
103          */
104         offset = 0;
105         end = GUC_NUM_DOORBELLS / 2;
106         if (is_high_priority(client)) {
107                 offset = end;
108                 end += offset;
109         }
110
111         id = find_next_zero_bit(client->guc->doorbell_bitmap, end, offset);
112         if (id == end)
113                 return -ENOSPC;
114
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)),
119                          id);
120         return 0;
121 }
122
123 static void unreserve_doorbell(struct intel_guc_client *client)
124 {
125         GEM_BUG_ON(client->doorbell_id == GUC_DOORBELL_INVALID);
126
127         __clear_bit(client->doorbell_id, client->guc->doorbell_bitmap);
128         client->doorbell_id = GUC_DOORBELL_INVALID;
129 }
130
131 /*
132  * Tell the GuC to allocate or deallocate a specific doorbell
133  */
134
135 static int __guc_allocate_doorbell(struct intel_guc *guc, u32 stage_id)
136 {
137         u32 action[] = {
138                 INTEL_GUC_ACTION_ALLOCATE_DOORBELL,
139                 stage_id
140         };
141
142         return intel_guc_send(guc, action, ARRAY_SIZE(action));
143 }
144
145 static int __guc_deallocate_doorbell(struct intel_guc *guc, u32 stage_id)
146 {
147         u32 action[] = {
148                 INTEL_GUC_ACTION_DEALLOCATE_DOORBELL,
149                 stage_id
150         };
151
152         return intel_guc_send(guc, action, ARRAY_SIZE(action));
153 }
154
155 static struct guc_stage_desc *__get_stage_desc(struct intel_guc_client *client)
156 {
157         struct guc_stage_desc *base = client->guc->stage_desc_pool_vaddr;
158
159         return &base[client->stage_id];
160 }
161
162 /*
163  * Initialise, update, or clear doorbell data shared with the GuC
164  *
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
167  */
168
169 static void __update_doorbell_desc(struct intel_guc_client *client, u16 new_id)
170 {
171         struct guc_stage_desc *desc;
172
173         /* Update the GuC's idea of the doorbell ID */
174         desc = __get_stage_desc(client);
175         desc->db_id = new_id;
176 }
177
178 static struct guc_doorbell_info *__get_doorbell(struct intel_guc_client *client)
179 {
180         return client->vaddr + client->doorbell_offset;
181 }
182
183 static bool has_doorbell(struct intel_guc_client *client)
184 {
185         if (client->doorbell_id == GUC_DOORBELL_INVALID)
186                 return false;
187
188         return test_bit(client->doorbell_id, client->guc->doorbell_bitmap);
189 }
190
191 static void __create_doorbell(struct intel_guc_client *client)
192 {
193         struct guc_doorbell_info *doorbell;
194
195         doorbell = __get_doorbell(client);
196         doorbell->db_status = GUC_DOORBELL_ENABLED;
197         doorbell->cookie = 0;
198 }
199
200 static void __destroy_doorbell(struct intel_guc_client *client)
201 {
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;
205
206
207         doorbell = __get_doorbell(client);
208         doorbell->db_status = GUC_DOORBELL_DISABLED;
209         doorbell->cookie = 0;
210
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
214          */
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");
217 }
218
219 static int create_doorbell(struct intel_guc_client *client)
220 {
221         int ret;
222
223         __update_doorbell_desc(client, client->doorbell_id);
224         __create_doorbell(client);
225
226         ret = __guc_allocate_doorbell(client->guc, client->stage_id);
227         if (ret) {
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);
232                 return ret;
233         }
234
235         return 0;
236 }
237
238 static int destroy_doorbell(struct intel_guc_client *client)
239 {
240         int ret;
241
242         GEM_BUG_ON(!has_doorbell(client));
243
244         __destroy_doorbell(client);
245         ret = __guc_deallocate_doorbell(client->guc, client->stage_id);
246         if (ret)
247                 DRM_ERROR("Couldn't destroy client %u doorbell: %d\n",
248                           client->stage_id, ret);
249
250         __update_doorbell_desc(client, GUC_DOORBELL_INVALID);
251
252         return ret;
253 }
254
255 static unsigned long __select_cacheline(struct intel_guc *guc)
256 {
257         unsigned long offset;
258
259         /* Doorbell uses a single cache line within a page */
260         offset = offset_in_page(guc->db_cacheline);
261
262         /* Moving to next cache line to reduce contention */
263         guc->db_cacheline += cache_line_size();
264
265         DRM_DEBUG_DRIVER("reserved cacheline 0x%lx, next 0x%x, linesize %u\n",
266                          offset, guc->db_cacheline, cache_line_size());
267         return offset;
268 }
269
270 static inline struct guc_process_desc *
271 __get_process_desc(struct intel_guc_client *client)
272 {
273         return client->vaddr + client->proc_desc_offset;
274 }
275
276 /*
277  * Initialise the process descriptor shared with the GuC firmware.
278  */
279 static void guc_proc_desc_init(struct intel_guc *guc,
280                                struct intel_guc_client *client)
281 {
282         struct guc_process_desc *desc;
283
284         desc = memset(__get_process_desc(client), 0, sizeof(*desc));
285
286         /*
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).
291          */
292         desc->wq_base_addr = 0;
293         desc->db_base_addr = 0;
294
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;
299 }
300
301 static int guc_stage_desc_pool_create(struct intel_guc *guc)
302 {
303         struct i915_vma *vma;
304         void *vaddr;
305
306         vma = intel_guc_allocate_vma(guc,
307                                      PAGE_ALIGN(sizeof(struct guc_stage_desc) *
308                                      GUC_MAX_STAGE_DESCRIPTORS));
309         if (IS_ERR(vma))
310                 return PTR_ERR(vma);
311
312         vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
313         if (IS_ERR(vaddr)) {
314                 i915_vma_unpin_and_release(&vma);
315                 return PTR_ERR(vaddr);
316         }
317
318         guc->stage_desc_pool = vma;
319         guc->stage_desc_pool_vaddr = vaddr;
320         ida_init(&guc->stage_ids);
321
322         return 0;
323 }
324
325 static void guc_stage_desc_pool_destroy(struct intel_guc *guc)
326 {
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);
330 }
331
332 /*
333  * Initialise/clear the stage descriptor shared with the GuC firmware.
334  *
335  * This descriptor tells the GuC where (in GGTT space) to find the important
336  * data structures relating to this client (doorbell, process descriptor,
337  * write queue, etc).
338  */
339 static void guc_stage_desc_init(struct intel_guc *guc,
340                                 struct intel_guc_client *client)
341 {
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;
346         unsigned int tmp;
347         u32 gfx_addr;
348
349         desc = __get_stage_desc(client);
350         memset(desc, 0, sizeof(*desc));
351
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;
359
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];
364
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.
371                  */
372                 if (!ce->state)
373                         break;  /* XXX: continue? */
374
375                 /*
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).
381                  */
382                 lrc->context_desc = lower_32_bits(ce->lrc_desc);
383
384                 /* The state page is after PPHWSP */
385                 lrc->ring_lrca =
386                         guc_ggtt_offset(ce->state) + LRC_STATE_PN * PAGE_SIZE;
387
388                 /* XXX: In direct submission, the GuC wants the HW context id
389                  * here. In proxy submission, it wants the stage id
390                  */
391                 lrc->context_id = (client->stage_id << GUC_ELC_CTXID_OFFSET) |
392                                 (guc_engine_id << GUC_ELC_ENGINE_OFFSET);
393
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;
398
399                 desc->engines_used |= (1 << guc_engine_id);
400         }
401
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);
405
406         /*
407          * The doorbell, process descriptor, and workqueue are all parts
408          * of the client object, which the GuC will reference via the GGTT
409          */
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;
418
419         desc->desc_private = ptr_to_u64(client);
420 }
421
422 static void guc_stage_desc_fini(struct intel_guc *guc,
423                                 struct intel_guc_client *client)
424 {
425         struct guc_stage_desc *desc;
426
427         desc = __get_stage_desc(client);
428         memset(desc, 0, sizeof(*desc));
429 }
430
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)
435 {
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;
441         u32 wq_off;
442
443         lockdep_assert_held(&client->wq_lock);
444
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.
448          *
449          * XXX: if not the case, we need save data to a temp wqi and copy it to
450          * workqueue buffer dw by dw.
451          */
452         BUILD_BUG_ON(wqi_size != 16);
453
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));
459
460         /* WQ starts from the page after doorbell / process_desc */
461         wqi = client->vaddr + wq_off + GUC_DB_SIZE;
462
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) |
467                       WQ_NO_WCFLUSH_WAIT;
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;
472
473         /* Make the update visible to GuC */
474         WRITE_ONCE(desc->tail, (wq_off + wqi_size) & (GUC_WQ_SIZE - 1));
475 }
476
477 static void guc_reset_wq(struct intel_guc_client *client)
478 {
479         struct guc_process_desc *desc = __get_process_desc(client);
480
481         desc->head = 0;
482         desc->tail = 0;
483 }
484
485 static void guc_ring_doorbell(struct intel_guc_client *client)
486 {
487         struct guc_doorbell_info *db;
488         u32 cookie;
489
490         lockdep_assert_held(&client->wq_lock);
491
492         /* pointer of current doorbell cacheline */
493         db = __get_doorbell(client);
494
495         /*
496          * We're not expecting the doorbell cookie to change behind our back,
497          * we also need to treat 0 as a reserved value.
498          */
499         cookie = READ_ONCE(db->cookie);
500         WARN_ON_ONCE(xchg(&db->cookie, cookie + 1 ?: cookie + 2) != cookie);
501
502         /* XXX: doorbell was lost and need to acquire it again */
503         GEM_BUG_ON(db->db_status != GUC_DOORBELL_ENABLED);
504 }
505
506 static void guc_add_request(struct intel_guc *guc,
507                             struct drm_i915_gem_request *rq)
508 {
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,
512                                                                  engine));
513         u32 ring_tail = intel_ring_set_tail(rq->ring, rq->tail) / sizeof(u64);
514
515         spin_lock(&client->wq_lock);
516
517         guc_wq_item_append(client, engine->guc_id, ctx_desc,
518                            ring_tail, rq->global_seqno);
519         guc_ring_doorbell(client);
520
521         client->submissions[engine->id] += 1;
522
523         spin_unlock(&client->wq_lock);
524 }
525
526 /*
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.
532  */
533 static void flush_ggtt_writes(struct i915_vma *vma)
534 {
535         struct drm_i915_private *dev_priv = to_i915(vma->obj->base.dev);
536
537         if (i915_vma_is_map_and_fenceable(vma))
538                 POSTING_READ_FW(GUC_STATUS);
539 }
540
541 #define GUC_PREEMPT_FINISHED 0x1
542 #define GUC_PREEMPT_BREADCRUMB_DWORDS 0x8
543 static void inject_preempt_context(struct work_struct *work)
544 {
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,
554                                                                  engine));
555         u32 *cs = ring->vaddr + ring->tail;
556         u32 data[7];
557
558         if (engine->id == RCS) {
559                 cs = gen8_emit_ggtt_write_rcs(cs, GUC_PREEMPT_FINISHED,
560                                 intel_hws_preempt_done_address(engine));
561         } else {
562                 cs = gen8_emit_ggtt_write(cs, GUC_PREEMPT_FINISHED,
563                                 intel_hws_preempt_done_address(engine));
564                 *cs++ = MI_NOOP;
565                 *cs++ = MI_NOOP;
566         }
567         *cs++ = MI_USER_INTERRUPT;
568         *cs++ = MI_NOOP;
569
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));
574
575         ring->tail += GUC_PREEMPT_BREADCRUMB_DWORDS * sizeof(u32);
576         ring->tail &= (ring->size - 1);
577
578         flush_ggtt_writes(ring->vma);
579
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);
584
585         /*
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.
596          */
597         stage_desc->attribute &= ~GUC_STAGE_DESC_ATTR_TERMINATED;
598
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);
607
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);
612         }
613 }
614
615 /*
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.
622  */
623 #define GUC_PREEMPT_POSTPROCESS_DELAY_MS 10
624 static void wait_for_guc_preempt_report(struct intel_engine_cs *engine)
625 {
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];
630
631         WARN_ON(wait_for_atomic(report->report_return_status ==
632                                 INTEL_GUC_REPORT_STATUS_COMPLETE,
633                                 GUC_PREEMPT_POSTPROCESS_DELAY_MS));
634         /*
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
638          */
639         report->affected_count = 0;
640         report->report_return_status = INTEL_GUC_REPORT_STATUS_UNKNOWN;
641 }
642
643 /**
644  * guc_submit() - Submit commands through GuC
645  * @engine: engine associated with the commands
646  *
647  * The only error here arises if the doorbell hardware isn't functioning
648  * as expected, which really shouln't happen.
649  */
650 static void guc_submit(struct intel_engine_cs *engine)
651 {
652         struct intel_guc *guc = &engine->i915->guc;
653         struct intel_engine_execlists * const execlists = &engine->execlists;
654         struct execlist_port *port = execlists->port;
655         unsigned int n;
656
657         for (n = 0; n < execlists_num_ports(execlists); n++) {
658                 struct drm_i915_gem_request *rq;
659                 unsigned int count;
660
661                 rq = port_unpack(&port[n], &count);
662                 if (rq && count == 0) {
663                         port_set(&port[n], port_pack(rq, ++count));
664
665                         flush_ggtt_writes(rq->ring->vma);
666
667                         guc_add_request(guc, rq);
668                 }
669         }
670 }
671
672 static void port_assign(struct execlist_port *port,
673                         struct drm_i915_gem_request *rq)
674 {
675         GEM_BUG_ON(port_isset(port));
676
677         port_set(port, i915_gem_request_get(rq));
678 }
679
680 static void guc_dequeue(struct intel_engine_cs *engine)
681 {
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];
687         bool submit = false;
688         struct rb_node *rb;
689
690         spin_lock_irq(&engine->timeline->lock);
691         rb = execlists->first;
692         GEM_BUG_ON(rb_first(&execlists->queue) != rb);
693
694         if (!rb)
695                 goto unlock;
696
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];
701
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);
708                                 goto unlock;
709                         }
710                 }
711
712                 port++;
713                 if (port_isset(port))
714                         goto unlock;
715         }
716         GEM_BUG_ON(port_isset(port));
717
718         do {
719                 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
720                 struct drm_i915_gem_request *rq, *rn;
721
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,
726                                                         &rq->priotree.link);
727                                         goto done;
728                                 }
729
730                                 if (submit)
731                                         port_assign(port, last);
732                                 port++;
733                         }
734
735                         INIT_LIST_HEAD(&rq->priotree.link);
736
737                         __i915_gem_request_submit(rq);
738                         trace_i915_gem_request_in(rq,
739                                                   port_index(port, execlists));
740                         last = rq;
741                         submit = true;
742                 }
743
744                 rb = rb_next(rb);
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);
749         } while (rb);
750 done:
751         execlists->first = rb;
752         if (submit) {
753                 port_assign(port, last);
754                 execlists_set_active(execlists, EXECLISTS_ACTIVE_USER);
755                 guc_submit(engine);
756         }
757 unlock:
758         spin_unlock_irq(&engine->timeline->lock);
759 }
760
761 static void guc_submission_tasklet(unsigned long data)
762 {
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;
767
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);
772
773                 execlists_port_complete(execlists, port);
774
775                 rq = port_request(&port[0]);
776         }
777         if (!rq)
778                 execlists_clear_active(execlists, EXECLISTS_ACTIVE_USER);
779
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);
785
786                 wait_for_guc_preempt_report(engine);
787
788                 execlists_clear_active(execlists, EXECLISTS_ACTIVE_PREEMPT);
789                 intel_write_status_page(engine, I915_GEM_HWS_PREEMPT_INDEX, 0);
790         }
791
792         if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
793                 guc_dequeue(engine);
794 }
795
796 /*
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.
800  */
801
802 /* Check that a doorbell register is in the expected state */
803 static bool doorbell_ok(struct intel_guc *guc, u16 db_id)
804 {
805         struct drm_i915_private *dev_priv = guc_to_i915(guc);
806         u32 drbregl;
807         bool valid;
808
809         GEM_BUG_ON(db_id >= GUC_DOORBELL_INVALID);
810
811         drbregl = I915_READ(GEN8_DRBREGL(db_id));
812         valid = drbregl & GEN8_DRB_VALID;
813
814         if (test_bit(db_id, guc->doorbell_bitmap) == valid)
815                 return true;
816
817         DRM_DEBUG_DRIVER("Doorbell %d has unexpected state (0x%x): valid=%s\n",
818                          db_id, drbregl, yesno(valid));
819
820         return false;
821 }
822
823 static bool guc_verify_doorbells(struct intel_guc *guc)
824 {
825         u16 db_id;
826
827         for (db_id = 0; db_id < GUC_NUM_DOORBELLS; ++db_id)
828                 if (!doorbell_ok(guc, db_id))
829                         return false;
830
831         return true;
832 }
833
834 static int guc_clients_doorbell_init(struct intel_guc *guc)
835 {
836         int ret;
837
838         ret = create_doorbell(guc->execbuf_client);
839         if (ret)
840                 return ret;
841
842         ret = create_doorbell(guc->preempt_client);
843         if (ret) {
844                 destroy_doorbell(guc->execbuf_client);
845                 return ret;
846         }
847
848         return 0;
849 }
850
851 static void guc_clients_doorbell_fini(struct intel_guc *guc)
852 {
853         /*
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.
857          */
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);
862 }
863
864 /**
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
873  *              context)
874  *
875  * Return:      An intel_guc_client object if success, else NULL.
876  */
877 static struct intel_guc_client *
878 guc_client_alloc(struct drm_i915_private *dev_priv,
879                  u32 engines,
880                  u32 priority,
881                  struct i915_gem_context *ctx)
882 {
883         struct intel_guc_client *client;
884         struct intel_guc *guc = &dev_priv->guc;
885         struct i915_vma *vma;
886         void *vaddr;
887         int ret;
888
889         client = kzalloc(sizeof(*client), GFP_KERNEL);
890         if (!client)
891                 return ERR_PTR(-ENOMEM);
892
893         client->guc = guc;
894         client->owner = ctx;
895         client->engines = engines;
896         client->priority = priority;
897         client->doorbell_id = GUC_DOORBELL_INVALID;
898         spin_lock_init(&client->wq_lock);
899
900         ret = ida_simple_get(&guc->stage_ids, 0, GUC_MAX_STAGE_DESCRIPTORS,
901                              GFP_KERNEL);
902         if (ret < 0)
903                 goto err_client;
904
905         client->stage_id = ret;
906
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);
909         if (IS_ERR(vma)) {
910                 ret = PTR_ERR(vma);
911                 goto err_id;
912         }
913
914         /* We'll keep just the first (doorbell/proc) page permanently kmap'd. */
915         client->vma = vma;
916
917         vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
918         if (IS_ERR(vaddr)) {
919                 ret = PTR_ERR(vaddr);
920                 goto err_vma;
921         }
922         client->vaddr = vaddr;
923
924         client->doorbell_offset = __select_cacheline(guc);
925
926         /*
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.
930          */
931         if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
932                 client->proc_desc_offset = 0;
933         else
934                 client->proc_desc_offset = (GUC_DB_SIZE / 2);
935
936         guc_proc_desc_init(guc, client);
937         guc_stage_desc_init(guc, client);
938
939         ret = reserve_doorbell(client);
940         if (ret)
941                 goto err_vaddr;
942
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);
947
948         return client;
949
950 err_vaddr:
951         i915_gem_object_unpin_map(client->vma->obj);
952 err_vma:
953         i915_vma_unpin_and_release(&client->vma);
954 err_id:
955         ida_simple_remove(&guc->stage_ids, client->stage_id);
956 err_client:
957         kfree(client);
958         return ERR_PTR(ret);
959 }
960
961 static void guc_client_free(struct intel_guc_client *client)
962 {
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);
968         kfree(client);
969 }
970
971 static int guc_clients_create(struct intel_guc *guc)
972 {
973         struct drm_i915_private *dev_priv = guc_to_i915(guc);
974         struct intel_guc_client *client;
975
976         GEM_BUG_ON(guc->execbuf_client);
977         GEM_BUG_ON(guc->preempt_client);
978
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);
986         }
987         guc->execbuf_client = client;
988
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);
998         }
999         guc->preempt_client = client;
1000
1001         return 0;
1002 }
1003
1004 static void guc_clients_destroy(struct intel_guc *guc)
1005 {
1006         struct intel_guc_client *client;
1007
1008         client = fetch_and_zero(&guc->execbuf_client);
1009         guc_client_free(client);
1010
1011         client = fetch_and_zero(&guc->preempt_client);
1012         guc_client_free(client);
1013 }
1014
1015 static void guc_policy_init(struct guc_policy *policy)
1016 {
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;
1021 }
1022
1023 static void guc_policies_init(struct guc_policies *policies)
1024 {
1025         struct guc_policy *policy;
1026         u32 p, i;
1027
1028         policies->dpc_promote_time = POLICY_DEFAULT_DPC_PROMOTE_TIME_US;
1029         policies->max_num_work_items = POLICY_MAX_NUM_WI;
1030
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];
1034
1035                         guc_policy_init(policy);
1036                 }
1037         }
1038
1039         policies->is_valid = 1;
1040 }
1041
1042 /*
1043  * The first 80 dwords of the register state context, containing the
1044  * execlists and ppgtt registers.
1045  */
1046 #define LR_HW_CONTEXT_SIZE      (80 * sizeof(u32))
1047
1048 static int guc_ads_create(struct intel_guc *guc)
1049 {
1050         struct drm_i915_private *dev_priv = guc_to_i915(guc);
1051         struct i915_vma *vma;
1052         struct page *page;
1053         /* The ads obj includes the struct itself and buffers passed to GuC */
1054         struct {
1055                 struct guc_ads ads;
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];
1059         } __packed *blob;
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;
1064         u32 base;
1065
1066         GEM_BUG_ON(guc->ads_vma);
1067
1068         vma = intel_guc_allocate_vma(guc, PAGE_ALIGN(sizeof(*blob)));
1069         if (IS_ERR(vma))
1070                 return PTR_ERR(vma);
1071
1072         guc->ads_vma = vma;
1073
1074         page = i915_vma_first_page(vma);
1075         blob = kmap(page);
1076
1077         /* GuC scheduling policies */
1078         guc_policies_init(&blob->policies);
1079
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;
1084
1085                 /* Nothing to be saved or restored for now. */
1086                 blob->reg_state.white_list[engine->guc_id].count = 0;
1087         }
1088
1089         /*
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.
1096          */
1097         blob->ads.golden_context_lrca =
1098                 guc_ggtt_offset(dev_priv->kernel_context->engine[RCS].state) +
1099                 skipped_offset;
1100
1101         /*
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.
1106          */
1107         for_each_engine(engine, dev_priv, id)
1108                 blob->ads.eng_state_size[engine->guc_id] =
1109                         engine->context_size - skipped_size;
1110
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);
1115
1116         kunmap(page);
1117
1118         return 0;
1119 }
1120
1121 static void guc_ads_destroy(struct intel_guc *guc)
1122 {
1123         i915_vma_unpin_and_release(&guc->ads_vma);
1124 }
1125
1126 /*
1127  * Set up the memory resources to be shared with the GuC (via the GGTT)
1128  * at firmware loading time.
1129  */
1130 int intel_guc_submission_init(struct intel_guc *guc)
1131 {
1132         struct drm_i915_private *dev_priv = guc_to_i915(guc);
1133         struct intel_engine_cs *engine;
1134         enum intel_engine_id id;
1135         int ret;
1136
1137         if (guc->stage_desc_pool)
1138                 return 0;
1139
1140         ret = guc_stage_desc_pool_create(guc);
1141         if (ret)
1142                 return ret;
1143         /*
1144          * Keep static analysers happy, let them know that we allocated the
1145          * vma after testing that it didn't exist earlier.
1146          */
1147         GEM_BUG_ON(!guc->stage_desc_pool);
1148
1149         ret = intel_guc_log_create(guc);
1150         if (ret < 0)
1151                 goto err_stage_desc_pool;
1152
1153         ret = guc_ads_create(guc);
1154         if (ret < 0)
1155                 goto err_log;
1156         GEM_BUG_ON(!guc->ads_vma);
1157
1158         WARN_ON(!guc_verify_doorbells(guc));
1159         ret = guc_clients_create(guc);
1160         if (ret)
1161                 return ret;
1162
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);
1166         }
1167
1168         return 0;
1169
1170 err_log:
1171         intel_guc_log_destroy(guc);
1172 err_stage_desc_pool:
1173         guc_stage_desc_pool_destroy(guc);
1174         return ret;
1175 }
1176
1177 void intel_guc_submission_fini(struct intel_guc *guc)
1178 {
1179         struct drm_i915_private *dev_priv = guc_to_i915(guc);
1180         struct intel_engine_cs *engine;
1181         enum intel_engine_id id;
1182
1183         for_each_engine(engine, dev_priv, id)
1184                 cancel_work_sync(&guc->preempt_work[id].work);
1185
1186         guc_clients_destroy(guc);
1187         WARN_ON(!guc_verify_doorbells(guc));
1188
1189         guc_ads_destroy(guc);
1190         intel_guc_log_destroy(guc);
1191         guc_stage_desc_pool_destroy(guc);
1192 }
1193
1194 static void guc_interrupts_capture(struct drm_i915_private *dev_priv)
1195 {
1196         struct intel_rps *rps = &dev_priv->gt_pm.rps;
1197         struct intel_engine_cs *engine;
1198         enum intel_engine_id id;
1199         int irqs;
1200
1201         /* tell all command streamers to forward interrupts (but not vblank)
1202          * to GuC
1203          */
1204         irqs = _MASKED_BIT_ENABLE(GFX_INTERRUPT_STEERING);
1205         for_each_engine(engine, dev_priv, id)
1206                 I915_WRITE(RING_MODE_GEN7(engine), irqs);
1207
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);
1215
1216         /*
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.
1220          *
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.
1224          *
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
1230          * pm_intrmsk_mbz.
1231          *
1232          * Here we CLEAR REDIRECT_TO_GUC bit in pm_intrmsk_mbz, which will
1233          * result in the register bit being left SET!
1234          */
1235         rps->pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
1236         rps->pm_intrmsk_mbz &= ~GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
1237 }
1238
1239 static void guc_interrupts_release(struct drm_i915_private *dev_priv)
1240 {
1241         struct intel_rps *rps = &dev_priv->gt_pm.rps;
1242         struct intel_engine_cs *engine;
1243         enum intel_engine_id id;
1244         int irqs;
1245
1246         /*
1247          * tell all command streamers NOT to forward interrupts or vblank
1248          * to GuC.
1249          */
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);
1254
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);
1259
1260         rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
1261         rps->pm_intrmsk_mbz &= ~ARAT_EXPIRED_INTRMSK;
1262 }
1263
1264 static void guc_submission_park(struct intel_engine_cs *engine)
1265 {
1266         intel_engine_unpin_breadcrumbs_irq(engine);
1267 }
1268
1269 static void guc_submission_unpark(struct intel_engine_cs *engine)
1270 {
1271         intel_engine_pin_breadcrumbs_irq(engine);
1272 }
1273
1274 int intel_guc_submission_enable(struct intel_guc *guc)
1275 {
1276         struct drm_i915_private *dev_priv = guc_to_i915(guc);
1277         struct intel_engine_cs *engine;
1278         enum intel_engine_id id;
1279         int err;
1280
1281         /*
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.
1289          */
1290         BUILD_BUG_ON(ARRAY_SIZE(engine->execlists.port) *
1291                      sizeof(struct guc_wq_item) *
1292                      I915_NUM_ENGINES > GUC_WQ_SIZE);
1293
1294         GEM_BUG_ON(!guc->execbuf_client);
1295
1296         guc_reset_wq(guc->execbuf_client);
1297         guc_reset_wq(guc->preempt_client);
1298
1299         err = intel_guc_sample_forcewake(guc);
1300         if (err)
1301                 return err;
1302
1303         err = guc_clients_doorbell_init(guc);
1304         if (err)
1305                 return err;
1306
1307         /* Take over from manual control of ELSP (execlists) */
1308         guc_interrupts_capture(dev_priv);
1309
1310         for_each_engine(engine, dev_priv, id) {
1311                 struct intel_engine_execlists * const execlists =
1312                         &engine->execlists;
1313
1314                 execlists->tasklet.func = guc_submission_tasklet;
1315                 engine->park = guc_submission_park;
1316                 engine->unpark = guc_submission_unpark;
1317
1318                 engine->flags &= ~I915_ENGINE_SUPPORTS_STATS;
1319         }
1320
1321         return 0;
1322 }
1323
1324 void intel_guc_submission_disable(struct intel_guc *guc)
1325 {
1326         struct drm_i915_private *dev_priv = guc_to_i915(guc);
1327
1328         GEM_BUG_ON(dev_priv->gt.awake); /* GT should be parked first */
1329
1330         guc_interrupts_release(dev_priv);
1331         guc_clients_doorbell_fini(guc);
1332
1333         /* Revert back to manual ELSP submission */
1334         intel_engines_reset_default_submission(dev_priv);
1335 }
1336
1337 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1338 #include "selftests/intel_guc.c"
1339 #endif